DEADSOFTWARE

0aa9d560ab9b00cd002fbca4ac994b9f80a76ede
[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!
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_PreUpdate();
59 procedure g_Items_Update();
60 procedure g_Items_Draw();
61 procedure g_Items_DrawDrop();
62 procedure g_Items_Pick(ID: DWORD);
63 procedure g_Items_Remove(ID: DWORD);
64 procedure g_Items_SaveState (st: TStream);
65 procedure g_Items_LoadState (st: TStream);
67 procedure g_Items_RestartRound ();
69 function g_Items_ValidId (idx: Integer): Boolean; inline;
70 function g_Items_ByIdx (idx: Integer): PItem;
71 function g_Items_ObjByIdx (idx: Integer): PObj;
73 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
74 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
76 procedure g_Items_AddDynLights();
79 type
80 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
82 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
85 var
86 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
87 gMaxDist: Integer = 1; // for sounds
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 NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
138 end;
141 // ////////////////////////////////////////////////////////////////////////// //
142 const
143 ITEM_SIGNATURE = $4D455449; // 'ITEM'
145 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
146 (((14), (15)), // MEDKIT_SMALL
147 ((28), (19)), // MEDKIT_LARGE
148 ((28), (19)), // MEDKIT_BLACK
149 ((31), (16)), // ARMOR_GREEN
150 ((31), (16)), // ARMOR_BLUE
151 ((25), (25)), // SPHERE_BLUE
152 ((25), (25)), // SPHERE_WHITE
153 ((24), (47)), // SUIT
154 ((14), (27)), // OXYGEN
155 ((25), (25)), // INVUL
156 ((62), (24)), // WEAPON_SAW
157 ((63), (12)), // WEAPON_SHOTGUN1
158 ((54), (13)), // WEAPON_SHOTGUN2
159 ((54), (16)), // WEAPON_CHAINGUN
160 ((62), (16)), // WEAPON_ROCKETLAUNCHER
161 ((54), (16)), // WEAPON_PLASMA
162 ((61), (36)), // WEAPON_BFG
163 ((54), (16)), // WEAPON_SUPERPULEMET
164 (( 9), (11)), // AMMO_BULLETS
165 ((28), (16)), // AMMO_BULLETS_BOX
166 ((15), ( 7)), // AMMO_SHELLS
167 ((32), (12)), // AMMO_SHELLS_BOX
168 ((12), (27)), // AMMO_ROCKET
169 ((54), (21)), // AMMO_ROCKET_BOX
170 ((15), (12)), // AMMO_CELL
171 ((32), (21)), // AMMO_CELL_BIG
172 ((22), (29)), // AMMO_BACKPACK
173 ((16), (16)), // KEY_RED
174 ((16), (16)), // KEY_GREEN
175 ((16), (16)), // KEY_BLUE
176 (( 1), ( 1)), // WEAPON_KASTET
177 ((43), (16)), // WEAPON_PISTOL
178 ((14), (18)), // BOTTLE
179 ((16), (15)), // HELMET
180 ((32), (24)), // JETPACK
181 ((25), (25)), // INVIS
182 ((53), (20)), // WEAPON_FLAMETHROWER
183 ((13), (20))); // AMMO_FUELCAN
185 procedure InitTextures();
186 begin
187 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
188 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
189 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
190 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
191 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
192 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
193 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
194 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
195 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
196 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
197 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
198 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
199 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
200 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
201 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
202 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
203 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
204 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
205 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
206 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
207 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
208 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
209 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
210 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
211 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
212 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
213 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
214 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
215 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
216 end;
218 procedure g_Items_LoadData();
219 begin
220 e_WriteLog('Loading items data...', TMsgType.Notify);
222 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
223 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
224 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
225 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
229 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
230 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
231 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK', 32, 32, 3, True);
232 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
233 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
234 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
235 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
236 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
237 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
238 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
239 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
240 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
241 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
242 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
243 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
244 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
245 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
246 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
247 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
248 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
249 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
250 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
251 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
252 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
253 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
254 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
255 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
256 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
257 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
258 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
259 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
260 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
261 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
262 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
263 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
264 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
265 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
266 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
267 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
268 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
269 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
271 InitTextures();
273 freeIds := TIdPool.Create();
274 end;
277 procedure g_Items_FreeData();
278 begin
279 e_WriteLog('Releasing items data...', TMsgType.Notify);
281 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
282 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
283 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
284 g_Sound_Delete('SOUND_ITEM_GETITEM');
286 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
287 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
288 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
289 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
290 g_Frames_DeleteByName('FRAMES_ITEM_JETPACK');
291 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
292 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
293 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
294 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
295 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
296 g_Frames_DeleteByName('FRAMES_FLAG_RED');
297 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
298 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
299 g_Texture_Delete('ITEM_MEDKIT_SMALL');
300 g_Texture_Delete('ITEM_MEDKIT_LARGE');
301 g_Texture_Delete('ITEM_WEAPON_SAW');
302 g_Texture_Delete('ITEM_WEAPON_PISTOL');
303 g_Texture_Delete('ITEM_WEAPON_KASTET');
304 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
305 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
306 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
307 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
308 g_Texture_Delete('ITEM_WEAPON_PLASMA');
309 g_Texture_Delete('ITEM_WEAPON_BFG');
310 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
311 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
312 g_Texture_Delete('ITEM_AMMO_BULLETS');
313 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
314 g_Texture_Delete('ITEM_AMMO_SHELLS');
315 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
316 g_Texture_Delete('ITEM_AMMO_ROCKET');
317 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
318 g_Texture_Delete('ITEM_AMMO_CELL');
319 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
320 g_Texture_Delete('ITEM_AMMO_FUELCAN');
321 g_Texture_Delete('ITEM_AMMO_BACKPACK');
322 g_Texture_Delete('ITEM_KEY_RED');
323 g_Texture_Delete('ITEM_KEY_GREEN');
324 g_Texture_Delete('ITEM_KEY_BLUE');
325 g_Texture_Delete('ITEM_OXYGEN');
326 g_Texture_Delete('ITEM_SUIT');
327 g_Texture_Delete('ITEM_WEAPON_KASTET');
328 g_Texture_Delete('ITEM_MEDKIT_BLACK');
330 freeIds.Free();
331 freeIds := nil;
332 end;
335 procedure releaseItem (idx: Integer);
336 var
337 it: PItem;
338 begin
339 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
340 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
341 it := @ggItems[idx];
342 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
343 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
344 it.slotIsUsed := false;
345 if (it.Animation <> nil) then
346 begin
347 it.Animation.Free();
348 it.Animation := nil;
349 end;
350 it.alive := False;
351 it.SpawnTrigger := -1;
352 it.ItemType := ITEM_NONE;
353 it.NeedSend := false;
354 freeIds.release(LongWord(idx));
355 end;
358 procedure growItemArrayTo (newsz: Integer);
359 var
360 i, olen: Integer;
361 it: PItem;
362 begin
363 if (newsz < Length(ggItems)) then exit;
364 // no free slots
365 olen := Length(ggItems);
366 SetLength(ggItems, newsz);
367 for i := olen to High(ggItems) do
368 begin
369 it := @ggItems[i];
370 it.slotIsUsed := false;
371 it.arrIdx := i;
372 it.ItemType := ITEM_NONE;
373 it.Animation := nil;
374 it.alive := false;
375 it.SpawnTrigger := -1;
376 it.Respawnable := false;
377 it.NeedSend := false;
378 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
379 end;
380 end;
383 function allocItem (): DWORD;
384 begin
385 result := freeIds.alloc();
386 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
387 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
388 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
389 end;
392 // it will be slow if the slot is free (we have to rebuild the heap)
393 function wantItemSlot (slot: Integer): Integer;
394 var
395 olen: Integer;
396 it: PItem;
397 begin
398 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
399 // do we need to grow item storate?
400 olen := Length(ggItems);
401 if (slot >= olen) then growItemArrayTo(slot+64);
403 it := @ggItems[slot];
404 if not it.slotIsUsed then
405 begin
406 freeIds.alloc(LongWord(slot));
407 end
408 else
409 begin
410 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
411 end;
412 it.slotIsUsed := false;
414 result := slot;
415 end;
418 // ////////////////////////////////////////////////////////////////////////// //
419 procedure g_Items_Init ();
420 var
421 a, b: Integer;
422 begin
423 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
424 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
425 gMaxDist := Trunc(Hypot(a, b));
426 end;
429 procedure g_Items_Free ();
430 var
431 i: Integer;
432 begin
433 if (ggItems <> nil) then
434 begin
435 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
436 ggItems := nil;
437 end;
438 freeIds.clear();
439 end;
442 function g_Items_Create (X, Y: Integer; ItemType: Byte;
443 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
444 var
445 find_id: DWORD;
446 ID: DWORD;
447 it: PItem;
448 begin
449 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
451 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
453 it := @ggItems[find_id];
455 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
456 //it.arrIdx := find_id;
457 it.slotIsUsed := true;
459 it.ItemType := ItemType;
460 it.Respawnable := Respawnable;
461 it.InitX := X;
462 it.InitY := Y;
463 it.RespawnTime := 0;
464 it.Fall := Fall;
465 it.alive := True;
466 it.QuietRespawn := False;
467 it.dropped := false;
468 it.NeedSend := false;
470 g_Obj_Init(@it.Obj);
471 it.Obj.X := X;
472 it.Obj.Y := Y;
473 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
474 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
476 it.Animation := nil;
477 it.SpawnTrigger := -1;
479 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
480 if AdjCoord then
481 begin
482 with it^ do
483 begin
484 Obj.X := X - (Obj.Rect.Width div 2);
485 Obj.Y := Y - Obj.Rect.Height;
486 InitX := Obj.X;
487 InitY := Obj.Y;
488 end;
489 end;
491 it.Obj.oldX := it.Obj.X;
492 it.Obj.oldY := it.Obj.Y;
494 // Óñòàíîâêà àíèìàöèè
495 case it.ItemType of
496 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
497 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
498 ITEM_JETPACK: if g_Frames_Get(ID, 'FRAMES_ITEM_JETPACK') then it.Animation := TAnimation.Create(ID, True, 15);
499 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
500 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
501 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
502 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
503 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
504 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
505 end;
507 it.positionChanged();
509 result := find_id;
510 end;
512 procedure g_Items_PreUpdate ();
513 var
514 i: Integer;
515 begin
516 if (ggItems = nil) then Exit;
517 for i := 0 to High(ggItems) do
518 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
519 begin
520 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
521 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
522 end;
523 end;
525 procedure g_Items_Update ();
526 var
527 i, j, k: Integer;
528 ID: DWord;
529 Anim: TAnimation;
530 m, ItemRespawnTime: Word;
531 r, nxt: Boolean;
532 begin
533 if (ggItems = nil) then exit;
535 // respawn items in 15 seconds regardless of settings during warmup
536 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
538 for i := 0 to High(ggItems) do
539 begin
540 if (ggItems[i].ItemType = ITEM_NONE) then continue;
541 if not ggItems[i].slotIsUsed then continue; // just in case
543 with ggItems[i] do
544 begin
545 nxt := False;
547 if alive then
548 begin
549 if Fall then
550 begin
551 m := g_Obj_Move(@Obj, True, True);
552 positionChanged(); // this updates spatial accelerators
554 // Ñîïðîòèâëåíèå âîçäóõà
555 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
557 // Åñëè âûïàë çà êàðòó
558 if WordBool(m and MOVE_FALLOUT) then
559 begin
560 if SpawnTrigger = -1 then
561 begin
562 g_Items_Pick(i);
563 end
564 else
565 begin
566 g_Items_Remove(i);
567 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
568 end;
569 continue;
570 end;
571 end;
573 // Åñëè èãðîêè ïîáëèçîñòè
574 if (gPlayers <> nil) then
575 begin
576 j := Random(Length(gPlayers))-1;
578 for k := 0 to High(gPlayers) do
579 begin
580 Inc(j);
581 if j > High(gPlayers) then j := 0;
583 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
584 begin
585 if g_Game_IsClient then continue;
587 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
589 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
592 Doom 2D: Original:
593 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
594 +2. I_MEGA,I_INVL,I_SUPER
595 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
597 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
599 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
600 if r then
601 begin
602 if not (Respawnable and (ItemRespawnTime > 0)) then
603 g_Items_Remove(i)
604 else
605 g_Items_Pick(i);
606 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
607 nxt := True;
608 break;
609 end;
610 end;
611 end;
612 end;
614 if nxt then continue;
615 end;
617 if Respawnable and g_Game_IsServer then
618 begin
619 DecMin(RespawnTime, 0);
620 if (RespawnTime = 0) and (not alive) then
621 begin
622 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
624 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
625 begin
626 Anim := TAnimation.Create(ID, False, 4);
627 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
628 Anim.Free();
629 end;
631 Obj.oldX := InitX;
632 Obj.oldY := InitY;
633 Obj.X := InitX;
634 Obj.Y := InitY;
635 Obj.Vel.X := 0;
636 Obj.Vel.Y := 0;
637 Obj.Accel.X := 0;
638 Obj.Accel.Y := 0;
639 positionChanged(); // this updates spatial accelerators
641 alive := true;
643 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
644 QuietRespawn := false;
645 end;
646 end;
648 if (Animation <> nil) then Animation.Update();
649 end;
650 end;
651 end;
654 procedure itemsDrawInternal (dropflag: Boolean);
655 var
656 i, fX, fY: Integer;
657 it: PItem;
658 begin
659 if (ggItems = nil) then exit;
661 for i := 0 to High(ggItems) do
662 begin
663 it := @ggItems[i];
664 if (not it.slotIsUsed) or (it.ItemType = ITEM_NONE) then continue; // just in case
665 if not it.alive then continue;
666 if (it.dropped <> dropflag) then continue;
668 with it^ do
669 begin
670 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
671 begin
672 Obj.lerp(gLerpFactor, fX, fY);
673 if (Animation = nil) then
674 begin
675 e_Draw(gItemsTexturesID[ItemType], fX, fY, 0, true, false);
676 end
677 else
678 begin
679 Animation.Draw(fX, fY, TMirrorType.None);
680 end;
682 if g_debug_Frames then
683 begin
684 e_DrawQuad(Obj.X+Obj.Rect.X,
685 Obj.Y+Obj.Rect.Y,
686 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
687 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
688 0, 255, 0);
689 end;
690 end;
691 end;
692 end;
693 end;
696 procedure g_Items_Draw ();
697 begin
698 itemsDrawInternal(false);
699 end;
701 procedure g_Items_DrawDrop ();
702 begin
703 itemsDrawInternal(true);
704 end;
707 procedure g_Items_SetDrop (ID: DWORD);
708 begin
709 if (ID < Length(ggItems)) then
710 begin
711 ggItems[ID].dropped := true;
712 end;
713 end;
716 procedure g_Items_Pick (ID: DWORD);
717 begin
718 if (ID < Length(ggItems)) then
719 begin
720 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
721 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
722 ggItems[ID].alive := false;
723 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
724 end;
725 end;
728 procedure g_Items_Remove (ID: DWORD);
729 var
730 it: PItem;
731 trig: Integer;
732 begin
733 if not g_Items_ValidId(ID) then
734 begin
735 //writeln('g_Items_Remove: invalid item id: ', ID);
736 raise Exception.Create('g_Items_Remove: invalid item id');
737 //exit;
738 end;
740 it := @ggItems[ID];
741 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
743 it.Obj.oldX := it.Obj.X;
744 it.Obj.oldY := it.Obj.Y;
745 trig := it.SpawnTrigger;
747 releaseItem(ID);
749 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
750 end;
753 procedure g_Items_SaveState (st: TStream);
754 var
755 count, i: Integer;
756 tt: Byte;
757 begin
758 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
759 count := 0;
760 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
762 // Êîëè÷åñòâî ïðåäìåòîâ
763 utils.writeInt(st, LongInt(count));
764 if (count = 0) then exit;
766 for i := 0 to High(ggItems) do
767 begin
768 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
769 begin
770 // Ñèãíàòóðà ïðåäìåòà
771 utils.writeSign(st, 'ITEM');
772 utils.writeInt(st, Byte(0));
773 // Òèï ïðåäìåòà
774 tt := ggItems[i].ItemType;
775 if ggItems[i].dropped then tt := tt or $80;
776 utils.writeInt(st, Byte(tt));
777 // Åñòü ëè ðåñïàóí
778 utils.writeBool(st, ggItems[i].Respawnable);
779 // Êîîðäèíàòû ðåñïóíà
780 utils.writeInt(st, LongInt(ggItems[i].InitX));
781 utils.writeInt(st, LongInt(ggItems[i].InitY));
782 // Âðåìÿ äî ðåñïàóíà
783 utils.writeInt(st, Word(ggItems[i].RespawnTime));
784 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
785 utils.writeBool(st, ggItems[i].alive);
786 // Ìîæåò ëè îí ïàäàòü
787 utils.writeBool(st, ggItems[i].Fall);
788 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
789 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
790 // Îáúåêò ïðåäìåòà
791 Obj_SaveState(st, @ggItems[i].Obj);
792 end;
793 end;
794 end;
797 procedure g_Items_LoadState (st: TStream);
798 var
799 count, i, a: Integer;
800 b: Byte;
801 begin
802 assert(st <> nil);
804 g_Items_Free();
806 // Êîëè÷åñòâî ïðåäìåòîâ
807 count := utils.readLongInt(st);
808 if (count = 0) then exit;
809 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
811 for a := 0 to count-1 do
812 begin
813 // Ñèãíàòóðà ïðåäìåòà
814 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
815 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
816 // Òèï ïðåäìåòà
817 b := utils.readByte(st); // bit7=1: monster drop
818 // Ñîçäàåì ïðåäìåò
819 i := g_Items_Create(0, 0, b and $7F, False, False);
820 if ((b and $80) <> 0) then g_Items_SetDrop(i);
821 // Åñòü ëè ðåñïàóí
822 ggItems[i].Respawnable := utils.readBool(st);
823 // Êîîðäèíàòû ðåñïóíà
824 ggItems[i].InitX := utils.readLongInt(st);
825 ggItems[i].InitY := utils.readLongInt(st);
826 // Âðåìÿ äî ðåñïàóíà
827 ggItems[i].RespawnTime := utils.readWord(st);
828 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
829 ggItems[i].alive := utils.readBool(st);
830 // Ìîæåò ëè îí ïàäàòü
831 ggItems[i].Fall := utils.readBool(st);
832 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
833 ggItems[i].SpawnTrigger := utils.readLongInt(st);
834 // Îáúåêò ïðåäìåòà
835 Obj_LoadState(@ggItems[i].Obj, st);
836 end;
837 end;
840 procedure g_Items_RestartRound ();
841 var
842 i: Integer;
843 it: PItem;
844 begin
845 for i := 0 to High(ggItems) do
846 begin
847 it := @ggItems[i];
848 it.Obj.oldX := it.Obj.X;
849 it.Obj.oldY := it.Obj.Y;
850 if not it.slotIsUsed then continue;
851 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
852 begin
853 it.QuietRespawn := True;
854 it.RespawnTime := 0;
855 end
856 else
857 begin
858 g_Items_Remove(i);
859 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
860 end;
861 end;
862 end;
865 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
866 var
867 idx: Integer;
868 begin
869 result := false;
870 if (ggItems = nil) or not assigned(cb) then exit;
872 if backwards then
873 begin
874 for idx := High(ggItems) downto 0 do
875 begin
876 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
877 begin
878 result := cb(@ggItems[idx]);
879 if result then exit;
880 end;
881 end;
882 end
883 else
884 begin
885 for idx := 0 to High(ggItems) do
886 begin
887 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
888 begin
889 result := cb(@ggItems[idx]);
890 if result then exit;
891 end;
892 end;
893 end;
894 end;
897 // ////////////////////////////////////////////////////////////////////////// //
898 procedure g_Items_EmitPickupSound (idx: Integer);
899 var
900 it: PItem;
901 begin
902 if not g_Items_ValidId(idx) then exit;
903 it := @ggItems[idx];
904 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
905 end;
907 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
908 var
909 it: PItem;
910 begin
911 if not g_Items_ValidId(idx) then exit;
913 it := @ggItems[idx];
914 if gSoundEffectsDF then
915 begin
916 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
917 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
918 begin
919 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
920 end
921 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
922 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
923 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
924 ITEM_AMMO_BACKPACK] then
925 begin
926 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
927 end
928 else
929 begin
930 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
931 end;
932 end
933 else
934 begin
935 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
936 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
937 begin
938 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
939 end
940 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
941 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
942 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
943 begin
944 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
945 end
946 else
947 begin
948 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
949 end;
950 end;
951 end;
954 procedure g_Items_AddDynLights();
955 var
956 f: Integer;
957 it: PItem;
958 begin
959 for f := 0 to High(ggItems) do
960 begin
961 it := @ggItems[f];
962 if not it.alive then continue;
963 case it.ItemType of
964 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);
965 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);
966 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);
967 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);
968 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);
969 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);
970 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);
971 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);
972 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);
973 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);
974 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);
975 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);
976 end;
977 end;
978 end;
981 end.