DEADSOFTWARE

Game: Use proper syntax of sets for game options instead of raw bitwise operations
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_items;
18 interface
20 uses
21 SysUtils, Classes,
22 MAPDEF, g_textures, g_phys, g_saveload;
24 Type
25 PItem = ^TItem;
26 TItem = record
27 private
28 //treeNode: Integer;
29 slotIsUsed: Boolean;
30 arrIdx: Integer; // in ggItems
32 public
33 ItemType: Byte;
34 Respawnable: Boolean;
35 InitX, InitY: Integer;
36 RespawnTime: Word;
37 alive: Boolean;
38 Fall: Boolean;
39 QuietRespawn: Boolean;
40 SpawnTrigger: Integer;
41 Obj: TObj;
42 Animation: TAnimation;
43 dropped: Boolean; // dropped from the monster? drops should be rendered after corpses, so zombie corpse will not obscure ammo container, for example
44 NeedSend: Boolean;
46 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
47 procedure getMapBox (out x, y, w, h: Integer); inline;
48 procedure moveBy (dx, dy: Integer); inline;
50 property myid: Integer read arrIdx;
51 end;
53 procedure g_Items_LoadData();
54 procedure g_Items_FreeData();
55 procedure g_Items_Init();
56 procedure g_Items_Free();
57 function g_Items_Create(X, Y: Integer; ItemType: Byte;
58 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
59 procedure g_Items_SetDrop (ID: DWORD);
60 procedure g_Items_PreUpdate();
61 procedure g_Items_Update();
62 procedure g_Items_Draw();
63 procedure g_Items_DrawDrop();
64 procedure g_Items_Pick(ID: DWORD);
65 procedure g_Items_Remove(ID: DWORD);
66 procedure g_Items_SaveState (st: TStream);
67 procedure g_Items_LoadState (st: TStream);
69 procedure g_Items_RestartRound ();
71 function g_Items_ValidId (idx: Integer): Boolean; inline;
72 function g_Items_ByIdx (idx: Integer): PItem;
73 function g_Items_ObjByIdx (idx: Integer): PObj;
75 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
76 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
78 procedure g_Items_AddDynLights();
81 type
82 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
84 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
85 function g_Items_NextAlive (startIdx: Integer): PItem;
87 var
88 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
89 gMaxDist: Integer = 1; // for sounds
91 implementation
93 uses
94 Math,
95 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
96 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
97 e_log,
98 g_grid, binheap, idpool, utils, xstreams;
101 var
102 ggItems: Array of TItem = nil;
105 // ////////////////////////////////////////////////////////////////////////// //
106 var
107 freeIds: TIdPool = nil;
110 // ////////////////////////////////////////////////////////////////////////// //
111 function g_Items_ValidId (idx: Integer): Boolean; inline;
112 begin
113 result := false;
114 if (idx < 0) or (idx > High(ggItems)) then exit;
115 if not ggItems[idx].slotIsUsed then exit;
116 result := true;
117 end;
120 function g_Items_ByIdx (idx: Integer): PItem;
121 begin
122 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
123 result := @ggItems[idx];
124 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
125 end;
128 function g_Items_ObjByIdx (idx: Integer): PObj;
129 begin
130 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
131 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
132 result := @ggItems[idx].Obj;
133 end;
136 // ////////////////////////////////////////////////////////////////////////// //
137 procedure TItem.positionChanged ();
138 begin
139 NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
140 end;
142 procedure TItem.getMapBox (out x, y, w, h: Integer); inline;
143 begin
144 x := Obj.X+Obj.Rect.X;
145 y := Obj.Y+Obj.Rect.Y;
146 w := Obj.Rect.Width;
147 h := Obj.Rect.Height;
148 end;
150 procedure TItem.moveBy (dx, dy: Integer); inline;
151 begin
152 if (dx <> 0) or (dy <> 0) then
153 begin
154 Obj.X += dx;
155 Obj.Y += dy;
156 positionChanged();
157 end;
158 end;
160 // ////////////////////////////////////////////////////////////////////////// //
161 const
162 ITEM_SIGNATURE = $4D455449; // 'ITEM'
164 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
165 (((14), (15)), // MEDKIT_SMALL
166 ((28), (19)), // MEDKIT_LARGE
167 ((28), (19)), // MEDKIT_BLACK
168 ((31), (16)), // ARMOR_GREEN
169 ((31), (16)), // ARMOR_BLUE
170 ((25), (25)), // SPHERE_BLUE
171 ((25), (25)), // SPHERE_WHITE
172 ((24), (47)), // SUIT
173 ((14), (27)), // OXYGEN
174 ((25), (25)), // INVUL
175 ((62), (24)), // WEAPON_SAW
176 ((63), (12)), // WEAPON_SHOTGUN1
177 ((54), (13)), // WEAPON_SHOTGUN2
178 ((54), (16)), // WEAPON_CHAINGUN
179 ((62), (16)), // WEAPON_ROCKETLAUNCHER
180 ((54), (16)), // WEAPON_PLASMA
181 ((61), (36)), // WEAPON_BFG
182 ((54), (16)), // WEAPON_SUPERCHAINGUN
183 (( 9), (11)), // AMMO_BULLETS
184 ((28), (16)), // AMMO_BULLETS_BOX
185 ((15), ( 7)), // AMMO_SHELLS
186 ((32), (12)), // AMMO_SHELLS_BOX
187 ((12), (27)), // AMMO_ROCKET
188 ((54), (21)), // AMMO_ROCKET_BOX
189 ((15), (12)), // AMMO_CELL
190 ((32), (21)), // AMMO_CELL_BIG
191 ((22), (29)), // AMMO_BACKPACK
192 ((16), (16)), // KEY_RED
193 ((16), (16)), // KEY_GREEN
194 ((16), (16)), // KEY_BLUE
195 (( 1), ( 1)), // WEAPON_IRONFIST
196 ((43), (16)), // WEAPON_PISTOL
197 ((14), (18)), // BOTTLE
198 ((16), (15)), // HELMET
199 ((32), (24)), // JETPACK
200 ((25), (25)), // INVIS
201 ((53), (20)), // WEAPON_FLAMETHROWER
202 ((13), (20))); // AMMO_FUELCAN
204 procedure InitTextures();
205 begin
206 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
207 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
208 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
209 g_Texture_Get('ITEM_WEAPON_IRONFIST', gItemsTexturesID[ITEM_WEAPON_IRONFIST]);
210 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
211 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
212 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
213 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
214 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
215 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
216 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
217 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
218 g_Texture_Get('ITEM_WEAPON_SUPERCHAINGUN', gItemsTexturesID[ITEM_WEAPON_SUPERCHAINGUN]);
219 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
220 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
221 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
222 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
223 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
224 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
225 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
226 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
227 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
228 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
229 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
230 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
231 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
232 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
233 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
234 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
235 end;
237 procedure g_Items_LoadData();
238 begin
239 e_WriteLog('Loading items data...', TMsgType.Notify);
241 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
242 g_Sound_CreateWADEx('SOUND_ITEM_GETPOWERUP', GameWAD+':SOUNDS\GETPOWERUP');
243 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
244 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
246 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
247 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
248 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
249 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
250 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK', 32, 32, 3, True);
251 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
252 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
253 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
254 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
255 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
256 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
257 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
258 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
259 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
260 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
261 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
262 g_Texture_CreateWADEx('ITEM_WEAPON_IRONFIST', GameWAD+':TEXTURES\IRONFIST');
263 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
264 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
265 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
266 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
267 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
268 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
269 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
270 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
271 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERCHAINGUN', GameWAD+':TEXTURES\SCHAINGUN');
272 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
273 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
274 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
275 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
276 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
277 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
278 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
279 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
280 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
281 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
282 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
283 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
284 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
285 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
286 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
287 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
289 InitTextures();
291 freeIds := TIdPool.Create();
292 end;
295 procedure g_Items_FreeData();
296 begin
297 e_WriteLog('Releasing items data...', TMsgType.Notify);
299 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
300 g_Sound_Delete('SOUND_ITEM_GETPOWERUP');
301 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
302 g_Sound_Delete('SOUND_ITEM_GETITEM');
304 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
305 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
306 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
307 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
308 g_Frames_DeleteByName('FRAMES_ITEM_JETPACK');
309 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
310 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
311 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
312 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
313 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
314 g_Frames_DeleteByName('FRAMES_FLAG_RED');
315 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
316 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
317 g_Texture_Delete('ITEM_MEDKIT_SMALL');
318 g_Texture_Delete('ITEM_MEDKIT_LARGE');
319 g_Texture_Delete('ITEM_MEDKIT_BLACK');
320 g_Texture_Delete('ITEM_WEAPON_IRONFIST');
321 g_Texture_Delete('ITEM_WEAPON_SAW');
322 g_Texture_Delete('ITEM_WEAPON_PISTOL');
323 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
324 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
325 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
326 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
327 g_Texture_Delete('ITEM_WEAPON_PLASMA');
328 g_Texture_Delete('ITEM_WEAPON_BFG');
329 g_Texture_Delete('ITEM_WEAPON_SUPERCHAINGUN');
330 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
331 g_Texture_Delete('ITEM_AMMO_BULLETS');
332 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
333 g_Texture_Delete('ITEM_AMMO_SHELLS');
334 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
335 g_Texture_Delete('ITEM_AMMO_ROCKET');
336 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
337 g_Texture_Delete('ITEM_AMMO_CELL');
338 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
339 g_Texture_Delete('ITEM_AMMO_FUELCAN');
340 g_Texture_Delete('ITEM_AMMO_BACKPACK');
341 g_Texture_Delete('ITEM_KEY_RED');
342 g_Texture_Delete('ITEM_KEY_GREEN');
343 g_Texture_Delete('ITEM_KEY_BLUE');
344 g_Texture_Delete('ITEM_OXYGEN');
345 g_Texture_Delete('ITEM_SUIT');
347 freeIds.Free();
348 freeIds := nil;
349 end;
352 procedure releaseItem (idx: Integer);
353 var
354 it: PItem;
355 begin
356 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
357 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
358 it := @ggItems[idx];
359 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
360 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
361 it.slotIsUsed := false;
362 if (it.Animation <> nil) then
363 begin
364 it.Animation.Free();
365 it.Animation := nil;
366 end;
367 it.alive := False;
368 it.SpawnTrigger := -1;
369 it.ItemType := ITEM_NONE;
370 it.NeedSend := false;
371 freeIds.release(LongWord(idx));
372 end;
375 procedure growItemArrayTo (newsz: Integer);
376 var
377 i, olen: Integer;
378 it: PItem;
379 begin
380 if (newsz < Length(ggItems)) then exit;
381 // no free slots
382 olen := Length(ggItems);
383 SetLength(ggItems, newsz);
384 for i := olen to High(ggItems) do
385 begin
386 it := @ggItems[i];
387 it.slotIsUsed := false;
388 it.arrIdx := i;
389 it.ItemType := ITEM_NONE;
390 it.Animation := nil;
391 it.alive := false;
392 it.SpawnTrigger := -1;
393 it.Respawnable := false;
394 it.NeedSend := false;
395 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
396 end;
397 end;
400 function allocItem (): DWORD;
401 begin
402 result := freeIds.alloc();
403 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
404 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
405 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
406 end;
409 // it will be slow if the slot is free (we have to rebuild the heap)
410 function wantItemSlot (slot: Integer): Integer;
411 var
412 olen: Integer;
413 it: PItem;
414 begin
415 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
416 // do we need to grow item storate?
417 olen := Length(ggItems);
418 if (slot >= olen) then growItemArrayTo(slot+64);
420 it := @ggItems[slot];
421 if not it.slotIsUsed then
422 begin
423 freeIds.alloc(LongWord(slot));
424 end
425 else
426 begin
427 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
428 end;
429 it.slotIsUsed := false;
431 result := slot;
432 end;
435 // ////////////////////////////////////////////////////////////////////////// //
436 procedure g_Items_Init ();
437 var
438 a, b: Integer;
439 begin
440 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
441 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
442 gMaxDist := Trunc(Hypot(a, b));
443 end;
446 procedure g_Items_Free ();
447 var
448 i: Integer;
449 begin
450 if (ggItems <> nil) then
451 begin
452 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
453 ggItems := nil;
454 end;
455 freeIds.clear();
456 end;
459 function g_Items_Create (X, Y: Integer; ItemType: Byte;
460 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
461 var
462 find_id: DWORD;
463 ID: DWORD;
464 it: PItem;
465 begin
466 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
468 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
470 it := @ggItems[find_id];
472 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
473 //it.arrIdx := find_id;
474 it.slotIsUsed := true;
476 it.ItemType := ItemType;
477 it.Respawnable := Respawnable;
478 it.InitX := X;
479 it.InitY := Y;
480 it.RespawnTime := 0;
481 it.Fall := Fall;
482 it.alive := True;
483 it.QuietRespawn := False;
484 it.dropped := false;
485 it.NeedSend := false;
487 g_Obj_Init(@it.Obj);
488 it.Obj.X := X;
489 it.Obj.Y := Y;
490 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
491 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
493 it.Animation := nil;
494 it.SpawnTrigger := -1;
496 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
497 if AdjCoord then
498 begin
499 with it^ do
500 begin
501 Obj.X := X - (Obj.Rect.Width div 2);
502 Obj.Y := Y - Obj.Rect.Height;
503 InitX := Obj.X;
504 InitY := Obj.Y;
505 end;
506 end;
508 it.Obj.oldX := it.Obj.X;
509 it.Obj.oldY := it.Obj.Y;
511 // Óñòàíîâêà àíèìàöèè
512 case it.ItemType of
513 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
514 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
515 ITEM_JETPACK: if g_Frames_Get(ID, 'FRAMES_ITEM_JETPACK') then it.Animation := TAnimation.Create(ID, True, 15);
516 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
517 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
518 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
519 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
520 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
521 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
522 end;
524 it.positionChanged();
526 result := find_id;
527 end;
529 procedure g_Items_PreUpdate ();
530 var
531 i: Integer;
532 begin
533 if (ggItems = nil) then Exit;
534 for i := 0 to High(ggItems) do
535 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
536 begin
537 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
538 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
539 end;
540 end;
542 procedure g_Items_Update ();
543 var
544 i, j, k: Integer;
545 ID: DWord;
546 Anim: TAnimation;
547 m, ItemRespawnTime{, PowerupRespawnTime}: Word;
548 r, nxt: Boolean;
549 begin
550 if (ggItems = nil) then exit;
552 // respawn items in 15 seconds regardless of settings during warmup
553 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
554 //PowerupRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.PowerupRespawnTime, 15);
556 for i := 0 to High(ggItems) do
557 begin
558 if (ggItems[i].ItemType = ITEM_NONE) then continue;
559 if not ggItems[i].slotIsUsed then continue; // just in case
561 with ggItems[i] do
562 begin
563 nxt := False;
565 if alive then
566 begin
567 if Fall then
568 begin
569 m := g_Obj_Move(@Obj, True, True);
570 positionChanged(); // this updates spatial accelerators
572 // Ñîïðîòèâëåíèå âîçäóõà
573 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
575 // Åñëè âûïàë çà êàðòó
576 if WordBool(m and MOVE_FALLOUT) then
577 begin
578 if SpawnTrigger = -1 then
579 begin
580 g_Items_Pick(i);
581 end
582 else
583 begin
584 g_Items_Remove(i);
585 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
586 end;
587 continue;
588 end;
589 end;
591 // Åñëè èãðîêè ïîáëèçîñòè
592 if (gPlayers <> nil) then
593 begin
594 j := Random(Length(gPlayers))-1;
596 for k := 0 to High(gPlayers) do
597 begin
598 Inc(j);
599 if j > High(gPlayers) then j := 0;
601 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
602 begin
603 if g_Game_IsClient then continue;
605 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
607 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
610 Doom 2D: Original:
611 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
612 +2. I_MEGA,I_INVL,I_SUPER
613 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
615 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
617 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
618 if r then
619 begin
620 if not (Respawnable and (ItemRespawnTime > 0)) then
621 g_Items_Remove(i)
622 else
623 g_Items_Pick(i);
624 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
625 nxt := True;
626 break;
627 end;
628 end;
629 end;
630 end;
632 if nxt then continue;
633 end;
635 if Respawnable and g_Game_IsServer then
636 begin
637 DecMin(RespawnTime, 0);
638 if (RespawnTime = 0) and (not alive) then
639 begin
640 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
642 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
643 begin
644 Anim := TAnimation.Create(ID, False, 4);
645 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
646 Anim.Free();
647 end;
649 Obj.oldX := InitX;
650 Obj.oldY := InitY;
651 Obj.X := InitX;
652 Obj.Y := InitY;
653 Obj.Vel.X := 0;
654 Obj.Vel.Y := 0;
655 Obj.Accel.X := 0;
656 Obj.Accel.Y := 0;
657 positionChanged(); // this updates spatial accelerators
659 alive := true;
661 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
662 QuietRespawn := false;
663 end;
664 end;
666 if (Animation <> nil) then Animation.Update();
667 end;
668 end;
669 end;
672 procedure itemsDrawInternal (dropflag: Boolean);
673 var
674 i, fX, fY: Integer;
675 it: PItem;
676 begin
677 if (ggItems = nil) then exit;
679 for i := 0 to High(ggItems) do
680 begin
681 it := @ggItems[i];
682 if (not it.slotIsUsed) or (it.ItemType = ITEM_NONE) then continue; // just in case
683 if not it.alive then continue;
684 if (it.dropped <> dropflag) then continue;
686 with it^ do
687 begin
688 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
689 begin
690 Obj.lerp(gLerpFactor, fX, fY);
691 if (Animation = nil) then
692 begin
693 e_Draw(gItemsTexturesID[ItemType], fX, fY, 0, true, false);
694 end
695 else
696 begin
697 Animation.Draw(fX, fY, TMirrorType.None);
698 end;
700 if g_debug_Frames then
701 begin
702 e_DrawQuad(Obj.X+Obj.Rect.X,
703 Obj.Y+Obj.Rect.Y,
704 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
705 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
706 0, 255, 0);
707 end;
708 end;
709 end;
710 end;
711 end;
714 procedure g_Items_Draw ();
715 begin
716 itemsDrawInternal(false);
717 end;
719 procedure g_Items_DrawDrop ();
720 begin
721 itemsDrawInternal(true);
722 end;
725 procedure g_Items_SetDrop (ID: DWORD);
726 begin
727 if (ID < Length(ggItems)) then
728 begin
729 ggItems[ID].dropped := true;
730 end;
731 end;
734 procedure g_Items_Pick (ID: DWORD);
735 begin
736 if ID >= Length(ggItems) then exit;
738 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
739 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
740 ggItems[ID].alive := false;
742 // Items respawn timer
743 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
745 // Powerup respawn timer
746 if ggItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL, ITEM_INVIS,
747 ITEM_MEDKIT_BLACK, ITEM_JETPACK, ITEM_SUIT] then
748 begin
749 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.PowerupRespawnTime, 15) * 36;
750 end;
752 // #### Random powerup respawn ####
753 if (TGameOption.POWERUP_RANDOM in gGameSettings.Options) and (ggItems[ID].ItemType in [
754 ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL, ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK,
755 ITEM_SUIT
756 ]) then
757 begin
758 ggItems[ID].RespawnTime := Max(1, (gGameSettings.PowerupRespawnTime +
759 RandomRange(-gGameSettings.PowerupRespawnRandom, gGameSettings.PowerupRespawnRandom + 1)) * 36);
760 //e_logwritefln ('Randomized powerup %s time: %s', [ggItems[ID].ItemType, ggItems[ID].RespawnTime]);
761 end;
763 // #### Random item respawn ####
764 // Randomize respawn for all items (excluding powerups)
765 if (TGameOption.ITEM_ALL_RANDOM in gGameSettings.Options) and (ggItems[ID].ItemType in [
766 ITEM_MEDKIT_SMALL, ITEM_MEDKIT_LARGE, ITEM_ARMOR_GREEN, ITEM_ARMOR_BLUE, ITEM_BOTTLE,
767 ITEM_HELMET, ITEM_OXYGEN, ITEM_AMMO_BULLETS, ITEM_AMMO_BULLETS_BOX, ITEM_AMMO_SHELLS,
768 ITEM_AMMO_SHELLS_BOX, ITEM_AMMO_ROCKET, ITEM_AMMO_ROCKET_BOX, ITEM_AMMO_CELL,
769 ITEM_AMMO_CELL_BIG, ITEM_AMMO_FUELCAN, ITEM_AMMO_BACKPACK, ITEM_WEAPON_SAW,
770 ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2, ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER,
771 ITEM_WEAPON_PLASMA, ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERCHAINGUN, ITEM_WEAPON_FLAMETHROWER
772 ]) then
773 begin
774 ggItems[ID].RespawnTime := Max(1, (gGameSettings.ItemRespawnTime +
775 RandomRange(-gGameSettings.ItemRespawnRandom, gGameSettings.ItemRespawnRandom + 1)) * 36);
776 //e_logwritefln ('Randomized item %s time: %s', [ggItems[ID].ItemType, ggItems[ID].RespawnTime]);
777 end;
779 // Randomize respawn for heal/armor
780 if (TGameOption.ITEM_LIFE_RANDOM in gGameSettings.Options) and (ggItems[ID].ItemType in [
781 ITEM_MEDKIT_SMALL, ITEM_MEDKIT_LARGE, ITEM_ARMOR_GREEN, ITEM_ARMOR_BLUE, ITEM_BOTTLE,
782 ITEM_HELMET, ITEM_OXYGEN, ITEM_AMMO_BACKPACK
783 ]) then
784 begin
785 ggItems[ID].RespawnTime := Max(1, (gGameSettings.ItemRespawnTime +
786 RandomRange(-gGameSettings.ItemRespawnRandom, gGameSettings.ItemRespawnRandom + 1)) * 36);
787 //e_logwritefln ('Randomized help item %s time: %s', [ggItems[ID].ItemType, ggItems[ID].RespawnTime]);
788 end;
790 // Randomize respawn for ammo
791 if (TGameOption.ITEM_AMMO_RANDOM in gGameSettings.Options) and (ggItems[ID].ItemType in [
792 ITEM_AMMO_BULLETS, ITEM_AMMO_BULLETS_BOX, ITEM_AMMO_SHELLS, ITEM_AMMO_SHELLS_BOX,
793 ITEM_AMMO_ROCKET, ITEM_AMMO_ROCKET_BOX, ITEM_AMMO_CELL, ITEM_AMMO_CELL_BIG, ITEM_AMMO_FUELCAN
794 ]) then
795 begin
796 ggItems[ID].RespawnTime := Max(1, (gGameSettings.ItemRespawnTime +
797 RandomRange(-gGameSettings.ItemRespawnRandom, gGameSettings.ItemRespawnRandom + 1)) * 36);
798 //e_logwritefln ('Randomized ammo %s time: %s', [ggItems[ID].ItemType, ggItems[ID].RespawnTime]);
799 end;
801 // Randomize respawn for weapons
802 if (TGameOption.ITEM_WEAPON_RANDOM in gGameSettings.Options) and (ggItems[ID].ItemType in [
803 ITEM_WEAPON_SAW, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2, ITEM_WEAPON_CHAINGUN,
804 ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA, ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERCHAINGUN,
805 ITEM_WEAPON_FLAMETHROWER
806 ]) then
807 begin
808 ggItems[ID].RespawnTime := Max(1, (gGameSettings.ItemRespawnTime +
809 RandomRange(-gGameSettings.ItemRespawnRandom, gGameSettings.ItemRespawnRandom + 1)) * 36);
810 //e_logwritefln ('Randomized weapon %s time: %s', [ggItems[ID].ItemType, ggItems[ID].RespawnTime]);
811 end;
812 end;
814 procedure g_Items_Remove (ID: DWORD);
815 var
816 it: PItem;
817 trig: Integer;
818 begin
819 if not g_Items_ValidId(ID) then
820 begin
821 //writeln('g_Items_Remove: invalid item id: ', ID);
822 raise Exception.Create('g_Items_Remove: invalid item id');
823 //exit;
824 end;
826 it := @ggItems[ID];
827 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
829 it.Obj.oldX := it.Obj.X;
830 it.Obj.oldY := it.Obj.Y;
831 trig := it.SpawnTrigger;
833 releaseItem(ID);
835 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
836 end;
839 procedure g_Items_SaveState (st: TStream);
840 var
841 count, i: Integer;
842 tt: Byte;
843 begin
844 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
845 count := 0;
846 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
848 // Êîëè÷åñòâî ïðåäìåòîâ
849 utils.writeInt(st, LongInt(count));
850 if (count = 0) then exit;
852 for i := 0 to High(ggItems) do
853 begin
854 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
855 begin
856 // Ñèãíàòóðà ïðåäìåòà
857 utils.writeSign(st, 'ITEM');
858 utils.writeInt(st, Byte(0));
859 // Òèï ïðåäìåòà
860 tt := ggItems[i].ItemType;
861 if ggItems[i].dropped then tt := tt or $80;
862 utils.writeInt(st, Byte(tt));
863 // Åñòü ëè ðåñïàóí
864 utils.writeBool(st, ggItems[i].Respawnable);
865 // Êîîðäèíàòû ðåñïóíà
866 utils.writeInt(st, LongInt(ggItems[i].InitX));
867 utils.writeInt(st, LongInt(ggItems[i].InitY));
868 // Âðåìÿ äî ðåñïàóíà
869 utils.writeInt(st, Word(ggItems[i].RespawnTime));
870 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
871 utils.writeBool(st, ggItems[i].alive);
872 // Ìîæåò ëè îí ïàäàòü
873 utils.writeBool(st, ggItems[i].Fall);
874 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
875 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
876 // Îáúåêò ïðåäìåòà
877 Obj_SaveState(st, @ggItems[i].Obj);
878 end;
879 end;
880 end;
883 procedure g_Items_LoadState (st: TStream);
884 var
885 count, i, a: Integer;
886 b: Byte;
887 begin
888 assert(st <> nil);
890 g_Items_Free();
892 // Êîëè÷åñòâî ïðåäìåòîâ
893 count := utils.readLongInt(st);
894 if (count = 0) then exit;
895 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
897 for a := 0 to count-1 do
898 begin
899 // Ñèãíàòóðà ïðåäìåòà
900 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
901 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
902 // Òèï ïðåäìåòà
903 b := utils.readByte(st); // bit7=1: monster drop
904 // Ñîçäàåì ïðåäìåò
905 i := g_Items_Create(0, 0, b and $7F, False, False);
906 if ((b and $80) <> 0) then g_Items_SetDrop(i);
907 // Åñòü ëè ðåñïàóí
908 ggItems[i].Respawnable := utils.readBool(st);
909 // Êîîðäèíàòû ðåñïóíà
910 ggItems[i].InitX := utils.readLongInt(st);
911 ggItems[i].InitY := utils.readLongInt(st);
912 // Âðåìÿ äî ðåñïàóíà
913 ggItems[i].RespawnTime := utils.readWord(st);
914 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
915 ggItems[i].alive := utils.readBool(st);
916 // Ìîæåò ëè îí ïàäàòü
917 ggItems[i].Fall := utils.readBool(st);
918 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
919 ggItems[i].SpawnTrigger := utils.readLongInt(st);
920 // Îáúåêò ïðåäìåòà
921 Obj_LoadState(@ggItems[i].Obj, st);
922 end;
923 end;
926 procedure g_Items_RestartRound ();
927 var
928 i: Integer;
929 it: PItem;
930 begin
931 for i := 0 to High(ggItems) do
932 begin
933 it := @ggItems[i];
934 it.Obj.oldX := it.Obj.X;
935 it.Obj.oldY := it.Obj.Y;
936 if not it.slotIsUsed then continue;
937 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
938 begin
939 it.QuietRespawn := True;
940 it.RespawnTime := 0;
941 end
942 else
943 begin
944 g_Items_Remove(i);
945 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
946 end;
947 end;
948 end;
951 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
952 var
953 idx: Integer;
954 begin
955 result := false;
956 if (ggItems = nil) or not assigned(cb) then exit;
958 if backwards then
959 begin
960 for idx := High(ggItems) downto 0 do
961 begin
962 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
963 begin
964 result := cb(@ggItems[idx]);
965 if result then exit;
966 end;
967 end;
968 end
969 else
970 begin
971 for idx := 0 to High(ggItems) do
972 begin
973 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
974 begin
975 result := cb(@ggItems[idx]);
976 if result then exit;
977 end;
978 end;
979 end;
980 end;
982 function g_Items_NextAlive (startIdx: Integer): PItem;
983 var
984 idx: Integer;
985 begin
986 result := nil;
987 if (ggItems = nil) or (startIdx >= High(ggItems)) then exit;
988 for idx := startIdx + 1 to High(ggItems) do
989 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
990 begin
991 result := @ggItems[idx];
992 exit;
993 end;
994 end;
996 // ////////////////////////////////////////////////////////////////////////// //
997 procedure g_Items_EmitPickupSound (idx: Integer);
998 var
999 it: PItem;
1000 begin
1001 if not g_Items_ValidId(idx) then exit;
1002 it := @ggItems[idx];
1003 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
1004 end;
1006 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
1007 var
1008 it: PItem;
1009 begin
1010 if not g_Items_ValidId(idx) then exit;
1012 it := @ggItems[idx];
1013 if gSoundEffectsDF then
1014 begin
1015 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL, ITEM_INVIS,
1016 ITEM_JETPACK, ITEM_MEDKIT_BLACK] then
1017 begin
1018 g_Sound_PlayExAt('SOUND_ITEM_GETPOWERUP', x, y);
1019 end
1020 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
1021 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
1022 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERCHAINGUN, ITEM_WEAPON_FLAMETHROWER,
1023 ITEM_AMMO_BACKPACK] then
1024 begin
1025 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
1026 end
1027 else
1028 begin
1029 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
1030 end;
1031 end
1032 else
1033 begin
1034 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL, ITEM_INVIS,
1035 ITEM_SUIT, ITEM_JETPACK, ITEM_MEDKIT_BLACK] then
1036 begin
1037 g_Sound_PlayExAt('SOUND_ITEM_GETPOWERUP', x, y);
1038 end
1039 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
1040 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
1041 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERCHAINGUN, ITEM_WEAPON_FLAMETHROWER] then
1042 begin
1043 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
1044 end
1045 else
1046 begin
1047 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
1048 end;
1049 end;
1050 end;
1053 procedure g_Items_AddDynLights();
1054 var
1055 f: Integer;
1056 it: PItem;
1057 begin
1058 for f := 0 to High(ggItems) do
1059 begin
1060 it := @ggItems[f];
1061 if not it.alive then continue;
1062 case it.ItemType of
1063 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);
1064 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);
1065 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);
1066 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);
1067 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);
1068 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);
1069 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);
1070 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);
1071 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);
1072 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);
1073 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);
1074 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);
1075 end;
1076 end;
1077 end;
1080 end.