DEADSOFTWARE

BUG|HACK|INVESTIGATE: don't crash on removing invalid item id (seems that something...
[d2df-sdl.git] / src / game / g_items.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_items;
19 interface
21 uses
22 SysUtils, Classes,
23 MAPDEF, g_textures, g_phys, g_saveload;
25 Type
26 PItem = ^TItem;
27 TItem = record
28 private
29 //treeNode: Integer;
30 slotIsUsed: Boolean;
31 arrIdx: Integer; // in ggItems
33 public
34 ItemType: Byte;
35 Respawnable: Boolean;
36 InitX, InitY: Integer;
37 RespawnTime: Word;
38 alive: Boolean;
39 Fall: Boolean;
40 QuietRespawn: Boolean;
41 SpawnTrigger: Integer;
42 Obj: TObj;
43 Animation: TAnimation;
44 dropped: Boolean; // dropped from the monster? drops should be rendered after corpses, so zombie corpse will not obscure ammo container, for example
46 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
48 property myid: Integer read arrIdx;
49 end;
51 procedure g_Items_LoadData();
52 procedure g_Items_FreeData();
53 procedure g_Items_Init();
54 procedure g_Items_Free();
55 function g_Items_Create(X, Y: Integer; ItemType: Byte;
56 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
57 procedure g_Items_SetDrop (ID: DWORD);
58 procedure g_Items_Update();
59 procedure g_Items_Draw();
60 procedure g_Items_DrawDrop();
61 procedure g_Items_Pick(ID: DWORD);
62 procedure g_Items_Remove(ID: DWORD);
63 procedure g_Items_SaveState (st: TStream);
64 procedure g_Items_LoadState (st: TStream);
66 procedure g_Items_RestartRound ();
68 function g_Items_ValidId (idx: Integer): Boolean; inline;
69 function g_Items_ByIdx (idx: Integer): PItem;
70 function g_Items_ObjByIdx (idx: Integer): PObj;
72 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
73 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
75 procedure g_Items_AddDynLights();
78 type
79 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
81 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
84 var
85 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
86 gMaxDist: Integer = 1; // for sounds
87 ITEM_RESPAWNTIME: Integer = 60 * 36;
89 implementation
91 uses
92 Math,
93 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
94 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
95 e_log,
96 g_grid, binheap, idpool, utils, xstreams;
99 var
100 ggItems: Array of TItem = nil;
103 // ////////////////////////////////////////////////////////////////////////// //
104 var
105 freeIds: TIdPool = nil;
108 // ////////////////////////////////////////////////////////////////////////// //
109 function g_Items_ValidId (idx: Integer): Boolean; inline;
110 begin
111 result := false;
112 if (idx < 0) or (idx > High(ggItems)) then exit;
113 if not ggItems[idx].slotIsUsed then exit;
114 result := true;
115 end;
118 function g_Items_ByIdx (idx: Integer): PItem;
119 begin
120 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
121 result := @ggItems[idx];
122 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
123 end;
126 function g_Items_ObjByIdx (idx: Integer): PObj;
127 begin
128 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
129 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
130 result := @ggItems[idx].Obj;
131 end;
134 // ////////////////////////////////////////////////////////////////////////// //
135 procedure TItem.positionChanged ();
136 begin
137 end;
140 // ////////////////////////////////////////////////////////////////////////// //
141 const
142 ITEM_SIGNATURE = $4D455449; // 'ITEM'
144 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
145 (((14), (15)), // MEDKIT_SMALL
146 ((28), (19)), // MEDKIT_LARGE
147 ((28), (19)), // MEDKIT_BLACK
148 ((31), (16)), // ARMOR_GREEN
149 ((31), (16)), // ARMOR_BLUE
150 ((25), (25)), // SPHERE_BLUE
151 ((25), (25)), // SPHERE_WHITE
152 ((24), (47)), // SUIT
153 ((14), (27)), // OXYGEN
154 ((25), (25)), // INVUL
155 ((62), (24)), // WEAPON_SAW
156 ((63), (12)), // WEAPON_SHOTGUN1
157 ((54), (13)), // WEAPON_SHOTGUN2
158 ((54), (16)), // WEAPON_CHAINGUN
159 ((62), (16)), // WEAPON_ROCKETLAUNCHER
160 ((54), (16)), // WEAPON_PLASMA
161 ((61), (36)), // WEAPON_BFG
162 ((54), (16)), // WEAPON_SUPERPULEMET
163 (( 9), (11)), // AMMO_BULLETS
164 ((28), (16)), // AMMO_BULLETS_BOX
165 ((15), ( 7)), // AMMO_SHELLS
166 ((32), (12)), // AMMO_SHELLS_BOX
167 ((12), (27)), // AMMO_ROCKET
168 ((54), (21)), // AMMO_ROCKET_BOX
169 ((15), (12)), // AMMO_CELL
170 ((32), (21)), // AMMO_CELL_BIG
171 ((22), (29)), // AMMO_BACKPACK
172 ((16), (16)), // KEY_RED
173 ((16), (16)), // KEY_GREEN
174 ((16), (16)), // KEY_BLUE
175 (( 1), ( 1)), // WEAPON_KASTET
176 ((43), (16)), // WEAPON_PISTOL
177 ((14), (18)), // BOTTLE
178 ((16), (15)), // HELMET
179 ((32), (24)), // JETPACK
180 ((25), (25)), // INVIS
181 ((53), (20)), // WEAPON_FLAMETHROWER
182 ((13), (20))); // AMMO_FUELCAN
184 procedure InitTextures();
185 begin
186 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
187 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
188 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
189 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
190 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
191 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
192 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
193 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
194 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
195 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
196 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
197 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
198 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
199 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
200 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
201 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
202 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
203 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
204 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
205 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
206 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
207 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
208 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
209 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
210 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
211 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
212 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
213 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
214 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
215 end;
217 procedure g_Items_LoadData();
218 begin
219 e_WriteLog('Loading items data...', TMsgType.Notify);
221 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
222 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
223 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
224 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
226 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
229 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
230 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK', 32, 32, 3, True);
231 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
232 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
233 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
234 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
235 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
236 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
237 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
238 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
239 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
240 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
241 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
242 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
243 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
244 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
245 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
246 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
247 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
248 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
249 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
250 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
251 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
252 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
253 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
254 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
255 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
256 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
257 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
258 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
259 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
260 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
261 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
262 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
263 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
264 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
265 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
266 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
267 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
268 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
270 InitTextures();
272 freeIds := TIdPool.Create();
273 end;
276 procedure g_Items_FreeData();
277 begin
278 e_WriteLog('Releasing items data...', TMsgType.Notify);
280 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
281 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
282 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
283 g_Sound_Delete('SOUND_ITEM_GETITEM');
285 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
286 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
287 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
288 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
289 g_Frames_DeleteByName('FRAMES_ITEM_JETPACK');
290 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
291 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
292 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
293 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
294 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
295 g_Frames_DeleteByName('FRAMES_FLAG_RED');
296 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
297 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
298 g_Texture_Delete('ITEM_MEDKIT_SMALL');
299 g_Texture_Delete('ITEM_MEDKIT_LARGE');
300 g_Texture_Delete('ITEM_WEAPON_SAW');
301 g_Texture_Delete('ITEM_WEAPON_PISTOL');
302 g_Texture_Delete('ITEM_WEAPON_KASTET');
303 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
304 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
305 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
306 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
307 g_Texture_Delete('ITEM_WEAPON_PLASMA');
308 g_Texture_Delete('ITEM_WEAPON_BFG');
309 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
310 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
311 g_Texture_Delete('ITEM_AMMO_BULLETS');
312 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
313 g_Texture_Delete('ITEM_AMMO_SHELLS');
314 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
315 g_Texture_Delete('ITEM_AMMO_ROCKET');
316 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
317 g_Texture_Delete('ITEM_AMMO_CELL');
318 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
319 g_Texture_Delete('ITEM_AMMO_FUELCAN');
320 g_Texture_Delete('ITEM_AMMO_BACKPACK');
321 g_Texture_Delete('ITEM_KEY_RED');
322 g_Texture_Delete('ITEM_KEY_GREEN');
323 g_Texture_Delete('ITEM_KEY_BLUE');
324 g_Texture_Delete('ITEM_OXYGEN');
325 g_Texture_Delete('ITEM_SUIT');
326 g_Texture_Delete('ITEM_WEAPON_KASTET');
327 g_Texture_Delete('ITEM_MEDKIT_BLACK');
329 freeIds.Free();
330 freeIds := nil;
331 end;
334 procedure releaseItem (idx: Integer);
335 var
336 it: PItem;
337 begin
338 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
339 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
340 it := @ggItems[idx];
341 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
342 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
343 it.slotIsUsed := false;
344 if (it.Animation <> nil) then
345 begin
346 it.Animation.Free();
347 it.Animation := nil;
348 end;
349 it.alive := False;
350 it.SpawnTrigger := -1;
351 it.ItemType := ITEM_NONE;
352 freeIds.release(LongWord(idx));
353 end;
356 procedure growItemArrayTo (newsz: Integer);
357 var
358 i, olen: Integer;
359 it: PItem;
360 begin
361 if (newsz < Length(ggItems)) then exit;
362 // no free slots
363 olen := Length(ggItems);
364 SetLength(ggItems, newsz);
365 for i := olen to High(ggItems) do
366 begin
367 it := @ggItems[i];
368 it.slotIsUsed := false;
369 it.arrIdx := i;
370 it.ItemType := ITEM_NONE;
371 it.Animation := nil;
372 it.alive := false;
373 it.SpawnTrigger := -1;
374 it.Respawnable := false;
375 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
376 end;
377 end;
380 function allocItem (): DWORD;
381 begin
382 result := freeIds.alloc();
383 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
384 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
385 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
386 end;
389 // it will be slow if the slot is free (we have to rebuild the heap)
390 function wantItemSlot (slot: Integer): Integer;
391 var
392 olen: Integer;
393 it: PItem;
394 begin
395 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
396 // do we need to grow item storate?
397 olen := Length(ggItems);
398 if (slot >= olen) then growItemArrayTo(slot+64);
400 it := @ggItems[slot];
401 if not it.slotIsUsed then
402 begin
403 freeIds.alloc(LongWord(slot));
404 end
405 else
406 begin
407 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
408 end;
409 it.slotIsUsed := false;
411 result := slot;
412 end;
415 // ////////////////////////////////////////////////////////////////////////// //
416 procedure g_Items_Init ();
417 var
418 a, b: Integer;
419 begin
420 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
421 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
422 gMaxDist := Trunc(Hypot(a, b));
423 end;
426 procedure g_Items_Free ();
427 var
428 i: Integer;
429 begin
430 if (ggItems <> nil) then
431 begin
432 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
433 ggItems := nil;
434 end;
435 freeIds.clear();
436 end;
439 function g_Items_Create (X, Y: Integer; ItemType: Byte;
440 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
441 var
442 find_id: DWORD;
443 ID: DWORD;
444 it: PItem;
445 begin
446 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
448 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
450 it := @ggItems[find_id];
452 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
453 //it.arrIdx := find_id;
454 it.slotIsUsed := true;
456 it.ItemType := ItemType;
457 it.Respawnable := Respawnable;
458 if g_Game_IsServer and (ITEM_RESPAWNTIME = 0) then it.Respawnable := False;
459 it.InitX := X;
460 it.InitY := Y;
461 it.RespawnTime := 0;
462 it.Fall := Fall;
463 it.alive := True;
464 it.QuietRespawn := False;
465 it.dropped := false;
467 g_Obj_Init(@it.Obj);
468 it.Obj.X := X;
469 it.Obj.Y := Y;
470 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
471 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
473 it.Animation := nil;
474 it.SpawnTrigger := -1;
476 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
477 if AdjCoord then
478 begin
479 with it^ do
480 begin
481 Obj.X := X - (Obj.Rect.Width div 2);
482 Obj.Y := Y - Obj.Rect.Height;
483 InitX := Obj.X;
484 InitY := Obj.Y;
485 end;
486 end;
488 // Óñòàíîâêà àíèìàöèè
489 case it.ItemType of
490 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
491 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
492 ITEM_JETPACK: if g_Frames_Get(ID, 'FRAMES_ITEM_JETPACK') then it.Animation := TAnimation.Create(ID, True, 15);
493 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
494 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
495 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
496 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
497 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
498 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
499 end;
501 it.positionChanged();
503 result := find_id;
504 end;
507 procedure g_Items_Update ();
508 var
509 i, j, k: Integer;
510 ID: DWord;
511 Anim: TAnimation;
512 m: Word;
513 r, nxt: Boolean;
514 begin
515 if (ggItems = nil) then exit;
517 for i := 0 to High(ggItems) do
518 begin
519 if (ggItems[i].ItemType = ITEM_NONE) then continue;
521 with ggItems[i] do
522 begin
523 nxt := False;
525 if alive then
526 begin
527 if Fall then
528 begin
529 m := g_Obj_Move(@Obj, True, True);
530 positionChanged(); // this updates spatial accelerators
532 // Ñîïðîòèâëåíèå âîçäóõà
533 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
535 // Åñëè âûïàë çà êàðòó
536 if WordBool(m and MOVE_FALLOUT) then
537 begin
538 if SpawnTrigger = -1 then
539 begin
540 g_Items_Pick(i);
541 end
542 else
543 begin
544 g_Items_Remove(i);
545 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
546 end;
547 continue;
548 end;
549 end;
551 // Åñëè èãðîêè ïîáëèçîñòè
552 if (gPlayers <> nil) then
553 begin
554 j := Random(Length(gPlayers))-1;
556 for k := 0 to High(gPlayers) do
557 begin
558 Inc(j);
559 if j > High(gPlayers) then j := 0;
561 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
562 begin
563 if g_Game_IsClient then continue;
565 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
567 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
570 Doom 2D: Original:
571 1. I_NONE,I_CLIP,I_SHEL,I_ROCKET,I_CELL,I_AMMO,I_SBOX,I_RBOX,I_CELP,I_BPACK,I_CSAW,I_SGUN,I_SGUN2,I_MGUN,I_LAUN,I_PLAS,I_BFG,I_GUN2
572 +2. I_MEGA,I_INVL,I_SUPER
573 3. I_STIM,I_MEDI,I_ARM1,I_ARM2,I_AQUA,I_KEYR,I_KEYG,I_KEYB,I_SUIT,I_RTORCH,I_GTORCH,I_BTORCH,I_GOR1,I_FCAN
575 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
577 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
578 if r then
579 begin
580 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
581 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
582 nxt := True;
583 break;
584 end;
585 end;
586 end;
587 end;
589 if nxt then continue;
590 end;
592 if Respawnable and g_Game_IsServer then
593 begin
594 DecMin(RespawnTime, 0);
595 if (RespawnTime = 0) and (not alive) then
596 begin
597 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
599 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
600 begin
601 Anim := TAnimation.Create(ID, False, 4);
602 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
603 Anim.Free();
604 end;
606 Obj.X := InitX;
607 Obj.Y := InitY;
608 Obj.Vel.X := 0;
609 Obj.Vel.Y := 0;
610 Obj.Accel.X := 0;
611 Obj.Accel.Y := 0;
612 positionChanged(); // this updates spatial accelerators
614 alive := true;
616 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
617 QuietRespawn := false;
618 end;
619 end;
621 if (Animation <> nil) then Animation.Update();
622 end;
623 end;
624 end;
627 procedure itemsDrawInternal (dropflag: Boolean);
628 var
629 i: Integer;
630 it: PItem;
631 begin
632 if (ggItems = nil) then exit;
634 for i := 0 to High(ggItems) do
635 begin
636 it := @ggItems[i];
637 if not it.alive then continue;
638 if (it.dropped <> dropflag) then continue;
640 with it^ do
641 begin
642 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
643 begin
644 if (Animation = nil) then
645 begin
646 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
647 end
648 else
649 begin
650 Animation.Draw(Obj.X, Obj.Y, TMirrorType.None);
651 end;
653 if g_debug_Frames then
654 begin
655 e_DrawQuad(Obj.X+Obj.Rect.X,
656 Obj.Y+Obj.Rect.Y,
657 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
658 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
659 0, 255, 0);
660 end;
661 end;
662 end;
663 end;
664 end;
667 procedure g_Items_Draw ();
668 begin
669 itemsDrawInternal(false);
670 end;
672 procedure g_Items_DrawDrop ();
673 begin
674 itemsDrawInternal(true);
675 end;
678 procedure g_Items_SetDrop (ID: DWORD);
679 begin
680 if (ID < Length(ggItems)) then
681 begin
682 ggItems[ID].dropped := true;
683 end;
684 end;
687 procedure g_Items_Pick (ID: DWORD);
688 begin
689 if (ID < Length(ggItems)) then
690 begin
691 ggItems[ID].alive := false;
692 ggItems[ID].RespawnTime := ITEM_RESPAWNTIME;
693 end;
694 end;
697 procedure g_Items_Remove (ID: DWORD);
698 var
699 it: PItem;
700 trig: Integer;
701 begin
702 if not g_Items_ValidId(ID) then
703 begin
704 //raise Exception.Create('g_Items_Remove: invalid item id');
705 writeln('g_Items_Remove: invalid item id: ', ID);
706 exit;
707 end;
709 it := @ggItems[ID];
710 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
712 trig := it.SpawnTrigger;
714 releaseItem(ID);
716 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
717 end;
720 procedure g_Items_SaveState (st: TStream);
721 var
722 count, i: Integer;
723 tt: Byte;
724 begin
725 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
726 count := 0;
727 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) then Inc(count);
729 // Êîëè÷åñòâî ïðåäìåòîâ
730 utils.writeInt(st, LongInt(count));
731 if (count = 0) then exit;
733 for i := 0 to High(ggItems) do
734 begin
735 if (ggItems[i].ItemType <> ITEM_NONE) then
736 begin
737 // Ñèãíàòóðà ïðåäìåòà
738 utils.writeSign(st, 'ITEM');
739 utils.writeInt(st, Byte(0));
740 // Òèï ïðåäìåòà
741 tt := ggItems[i].ItemType;
742 if ggItems[i].dropped then tt := tt or $80;
743 utils.writeInt(st, Byte(tt));
744 // Åñòü ëè ðåñïàóí
745 utils.writeBool(st, ggItems[i].Respawnable);
746 // Êîîðäèíàòû ðåñïóíà
747 utils.writeInt(st, LongInt(ggItems[i].InitX));
748 utils.writeInt(st, LongInt(ggItems[i].InitY));
749 // Âðåìÿ äî ðåñïàóíà
750 utils.writeInt(st, Word(ggItems[i].RespawnTime));
751 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
752 utils.writeBool(st, ggItems[i].alive);
753 // Ìîæåò ëè îí ïàäàòü
754 utils.writeBool(st, ggItems[i].Fall);
755 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
756 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
757 // Îáúåêò ïðåäìåòà
758 Obj_SaveState(st, @ggItems[i].Obj);
759 end;
760 end;
761 end;
764 procedure g_Items_LoadState (st: TStream);
765 var
766 count, i, a: Integer;
767 b: Byte;
768 begin
769 assert(st <> nil);
771 g_Items_Free();
773 // Êîëè÷åñòâî ïðåäìåòîâ
774 count := utils.readLongInt(st);
775 if (count = 0) then exit;
776 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
778 for a := 0 to count-1 do
779 begin
780 // Ñèãíàòóðà ïðåäìåòà
781 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
782 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
783 // Òèï ïðåäìåòà
784 b := utils.readByte(st); // bit7=1: monster drop
785 // Ñîçäàåì ïðåäìåò
786 i := g_Items_Create(0, 0, b and $7F, False, False);
787 if ((b and $80) <> 0) then g_Items_SetDrop(i);
788 // Åñòü ëè ðåñïàóí
789 ggItems[i].Respawnable := utils.readBool(st);
790 // Êîîðäèíàòû ðåñïóíà
791 ggItems[i].InitX := utils.readLongInt(st);
792 ggItems[i].InitY := utils.readLongInt(st);
793 // Âðåìÿ äî ðåñïàóíà
794 ggItems[i].RespawnTime := utils.readWord(st);
795 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
796 ggItems[i].alive := utils.readBool(st);
797 // Ìîæåò ëè îí ïàäàòü
798 ggItems[i].Fall := utils.readBool(st);
799 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
800 ggItems[i].SpawnTrigger := utils.readLongInt(st);
801 // Îáúåêò ïðåäìåòà
802 Obj_LoadState(@ggItems[i].Obj, st);
803 end;
804 end;
807 procedure g_Items_RestartRound ();
808 var
809 i: Integer;
810 it: PItem;
811 begin
812 for i := 0 to High(ggItems) do
813 begin
814 it := @ggItems[i];
815 if it.Respawnable then
816 begin
817 it.QuietRespawn := True;
818 it.RespawnTime := 0;
819 end
820 else
821 begin
822 g_Items_Remove(i);
823 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
824 end;
825 end;
826 end;
829 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
830 var
831 idx: Integer;
832 begin
833 result := false;
834 if (ggItems = nil) or not assigned(cb) then exit;
836 if backwards then
837 begin
838 for idx := High(ggItems) downto 0 do
839 begin
840 if ggItems[idx].alive then
841 begin
842 result := cb(@ggItems[idx]);
843 if result then exit;
844 end;
845 end;
846 end
847 else
848 begin
849 for idx := 0 to High(ggItems) do
850 begin
851 if ggItems[idx].alive then
852 begin
853 result := cb(@ggItems[idx]);
854 if result then exit;
855 end;
856 end;
857 end;
858 end;
861 // ////////////////////////////////////////////////////////////////////////// //
862 procedure g_Items_EmitPickupSound (idx: Integer);
863 var
864 it: PItem;
865 begin
866 if not g_Items_ValidId(idx) then exit;
867 it := @ggItems[idx];
868 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
869 end;
871 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
872 var
873 it: PItem;
874 begin
875 if not g_Items_ValidId(idx) then exit;
877 it := @ggItems[idx];
878 if gSoundEffectsDF then
879 begin
880 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
881 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
882 begin
883 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
884 end
885 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
886 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
887 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
888 ITEM_AMMO_BACKPACK] then
889 begin
890 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
891 end
892 else
893 begin
894 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
895 end;
896 end
897 else
898 begin
899 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
900 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
901 begin
902 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
903 end
904 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
905 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
906 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
907 begin
908 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
909 end
910 else
911 begin
912 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
913 end;
914 end;
915 end;
918 procedure g_Items_AddDynLights();
919 var
920 f: Integer;
921 it: PItem;
922 begin
923 for f := 0 to High(ggItems) do
924 begin
925 it := @ggItems[f];
926 if not it.alive then continue;
927 case it.ItemType of
928 ITEM_KEY_RED: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 24, 1.0, 0.0, 0.0, 0.6);
929 ITEM_KEY_GREEN: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 24, 0.0, 1.0, 0.0, 0.6);
930 ITEM_KEY_BLUE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 24, 0.0, 0.0, 1.0, 0.6);
931 ITEM_ARMOR_GREEN: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 42, 0.0, 1.0, 0.0, 0.6);
932 ITEM_ARMOR_BLUE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 42, 0.0, 0.0, 1.0, 0.6);
933 ITEM_JETPACK: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 1.0, 1.0, 1.0, 0.6);
934 ITEM_SPHERE_BLUE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 0.0, 1.0, 0.0, 0.6);
935 ITEM_SPHERE_WHITE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 1.0, 1.0, 1.0, 0.6);
936 ITEM_INVUL: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 1.0, 0.0, 0.0, 0.6);
937 ITEM_INVIS: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 1.0, 1.0, 0.0, 0.6);
938 ITEM_BOTTLE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 16, 0.0, 0.0, 0.8, 0.6);
939 ITEM_HELMET: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 16, 0.0, 0.8, 0.0, 0.6);
940 end;
941 end;
942 end;
945 end.