1 (* Copyright (C) DooM 2D:Forever Developers
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.
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.
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/>.
16 {$INCLUDE ../shared/a_modes.inc}
22 g_textures
, g_phys
, g_saveload
, BinEditor
, MAPDEF
;
30 arrIdx
: Integer; // in ggItems
35 InitX
, InitY
: Integer;
39 QuietRespawn
: Boolean;
40 SpawnTrigger
: Integer;
42 Animation
: TAnimation
;
44 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
46 property myid
: Integer read arrIdx
;
49 procedure g_Items_LoadData();
50 procedure g_Items_FreeData();
51 procedure g_Items_Init();
52 procedure g_Items_Free();
53 function g_Items_Create(X
, Y
: Integer; ItemType
: Byte;
54 Fall
, Respawnable
: Boolean; AdjCoord
: Boolean = False; ForcedID
: Integer = -1): DWORD
;
55 procedure g_Items_Update();
56 procedure g_Items_Draw();
57 procedure g_Items_Pick(ID
: DWORD
);
58 procedure g_Items_Remove(ID
: DWORD
);
59 procedure g_Items_SaveState(var Mem
: TBinMemoryWriter
);
60 procedure g_Items_LoadState(var Mem
: TBinMemoryReader
);
62 procedure g_Items_RestartRound ();
64 function g_Items_ValidId (idx
: Integer): Boolean; inline;
65 function g_Items_ByIdx (idx
: Integer): PItem
;
66 function g_Items_ObjByIdx (idx
: Integer): PObj
;
68 procedure g_Items_EmitPickupSound (idx
: Integer); // at item position
69 procedure g_Items_EmitPickupSoundAt (idx
, x
, y
: Integer);
73 TItemEachAliveCB
= function (it
: PItem
): Boolean is nested
; // return `true` to stop
75 function g_Items_ForEachAlive (cb
: TItemEachAliveCB
; backwards
: Boolean=false): Boolean;
79 gItemsTexturesID
: Array [1..ITEM_MAX
] of DWORD
;
80 gMaxDist
: Integer = 1;
81 ITEM_RESPAWNTIME
: Integer = 60 * 36;
86 g_basic
, e_graphics
, g_sound
, g_main
, g_gfx
, g_map
,
87 Math
, g_game
, g_triggers
, g_console
, SysUtils
, g_player
, g_net
, g_netmsg
,
89 g_grid
, z_aabbtree
, binheap
;
93 ggItems
: Array of TItem
= nil;
96 // ////////////////////////////////////////////////////////////////////////// //
99 TDynAABBTreeItemBase = specialize TDynAABBTreeBase<Integer>;
101 TDynAABBTreeItem = class(TDynAABBTreeItemBase)
102 function getFleshAABB (out aabb: AABB2D; flesh: Integer; tag: Integer): Boolean; override;
105 function TDynAABBTreeItem.getFleshAABB (out aabb: AABB2D; flesh: Integer; tag: Integer): Boolean;
110 if (flesh < 0) or (flesh > High(ggItems)) then raise Exception.Create('DynTree: trying to get dimensions of inexistant item');
111 it := @ggItems[flesh];
112 if (it.Obj.Rect.Width < 1) or (it.Obj.Rect.Height < 1) then exit;
113 aabb := AABB2D.Create(it.Obj.X, it.Obj.Y, it.Obj.X+it.Obj.Rect.Width-1, it.Obj.Y+it.Obj.Rect.Height-1);
114 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
120 // ////////////////////////////////////////////////////////////////////////// //
122 //itemTree: TDynAABBTreeItem = nil;
123 freeIds
: TBinaryHeapInt
= nil; // free item ids
126 // ////////////////////////////////////////////////////////////////////////// //
127 function g_Items_ValidId (idx
: Integer): Boolean; inline;
130 if (idx
< 0) or (idx
> High(ggItems
)) then exit
;
131 //if (ggItems[idx].treeNode = -1) then exit;
132 if not ggItems
[idx
].slotIsUsed
then exit
;
137 function g_Items_ByIdx (idx
: Integer): PItem
;
139 if (idx
< 0) or (idx
> High(ggItems
)) then raise Exception
.Create('g_ItemObjByIdx: invalid index');
140 result
:= @ggItems
[idx
];
141 //if (result.treeNode = -1) then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
142 if not result
.slotIsUsed
then raise Exception
.Create('g_ItemObjByIdx: requested inexistent item');
146 function g_Items_ObjByIdx (idx
: Integer): PObj
;
148 if (idx
< 0) or (idx
> High(ggItems
)) then raise Exception
.Create('g_ItemObjByIdx: invalid index');
149 //if (ggItems[idx].treeNode = -1) then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
150 if not ggItems
[idx
].slotIsUsed
then raise Exception
.Create('g_ItemObjByIdx: requested inexistent item');
151 result
:= @ggItems
[idx
].Obj
;
155 // ////////////////////////////////////////////////////////////////////////// //
156 procedure TItem
.positionChanged ();
161 if (treeNode = -1) then
163 treeNode := itemTree.insertObject(arrIdx, 0, true); // static object
164 {$IF DEFINED(D2F_DEBUG)}
165 itemTree.getNodeXY(treeNode, x, y);
166 e_WriteLog(Format('item #%d: inserted into the tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);
171 itemTree.getNodeXY(treeNode, x, y);
172 if (Obj.X = x) and (Obj.Y = y) then exit; // nothing to do
173 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('item #%d: updating tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);{$ENDIF}
176 itemTree.updateObject(treeNode);
178 itemTree.removeObject(treeNode);
179 treeNode := itemTree.insertObject(arrIdx, 0, true); // static object
182 {$IF DEFINED(D2F_DEBUG)}
183 itemTree.getNodeXY(treeNode, x, y);
184 e_WriteLog(Format('item #%d: updated tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);
191 // ////////////////////////////////////////////////////////////////////////// //
193 ITEM_SIGNATURE
= $4D455449; // 'ITEM'
195 ITEMSIZE
: Array [ITEM_MEDKIT_SMALL
..ITEM_MAX
] of Array [0..1] of Byte =
196 (((14), (15)), // MEDKIT_SMALL
197 ((28), (19)), // MEDKIT_LARGE
198 ((28), (19)), // MEDKIT_BLACK
199 ((31), (16)), // ARMOR_GREEN
200 ((31), (16)), // ARMOR_BLUE
201 ((25), (25)), // SPHERE_BLUE
202 ((25), (25)), // SPHERE_WHITE
203 ((24), (47)), // SUIT
204 ((14), (27)), // OXYGEN
205 ((25), (25)), // INVUL
206 ((62), (24)), // WEAPON_SAW
207 ((63), (12)), // WEAPON_SHOTGUN1
208 ((54), (13)), // WEAPON_SHOTGUN2
209 ((54), (16)), // WEAPON_CHAINGUN
210 ((62), (16)), // WEAPON_ROCKETLAUNCHER
211 ((54), (16)), // WEAPON_PLASMA
212 ((61), (36)), // WEAPON_BFG
213 ((54), (16)), // WEAPON_SUPERPULEMET
214 (( 9), (11)), // AMMO_BULLETS
215 ((28), (16)), // AMMO_BULLETS_BOX
216 ((15), ( 7)), // AMMO_SHELLS
217 ((32), (12)), // AMMO_SHELLS_BOX
218 ((12), (27)), // AMMO_ROCKET
219 ((54), (21)), // AMMO_ROCKET_BOX
220 ((15), (12)), // AMMO_CELL
221 ((32), (21)), // AMMO_CELL_BIG
222 ((22), (29)), // AMMO_BACKPACK
223 ((16), (16)), // KEY_RED
224 ((16), (16)), // KEY_GREEN
225 ((16), (16)), // KEY_BLUE
226 (( 1), ( 1)), // WEAPON_KASTET
227 ((43), (16)), // WEAPON_PISTOL
228 ((14), (18)), // BOTTLE
229 ((16), (15)), // HELMET
230 ((32), (24)), // JETPACK
231 ((25), (25)), // INVIS
232 ((53), (20)), // WEAPON_FLAMETHROWER
233 ((13), (20))); // AMMO_FUELCAN
235 procedure InitTextures();
237 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID
[ITEM_MEDKIT_SMALL
]);
238 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID
[ITEM_MEDKIT_LARGE
]);
239 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID
[ITEM_MEDKIT_BLACK
]);
240 g_Texture_Get('ITEM_SUIT', gItemsTexturesID
[ITEM_SUIT
]);
241 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID
[ITEM_OXYGEN
]);
242 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID
[ITEM_WEAPON_SAW
]);
243 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID
[ITEM_WEAPON_SHOTGUN1
]);
244 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID
[ITEM_WEAPON_SHOTGUN2
]);
245 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID
[ITEM_WEAPON_CHAINGUN
]);
246 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID
[ITEM_WEAPON_ROCKETLAUNCHER
]);
247 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID
[ITEM_WEAPON_PLASMA
]);
248 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID
[ITEM_WEAPON_BFG
]);
249 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID
[ITEM_WEAPON_SUPERPULEMET
]);
250 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID
[ITEM_WEAPON_FLAMETHROWER
]);
251 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID
[ITEM_AMMO_BULLETS
]);
252 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID
[ITEM_AMMO_BULLETS_BOX
]);
253 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID
[ITEM_AMMO_SHELLS
]);
254 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID
[ITEM_AMMO_SHELLS_BOX
]);
255 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID
[ITEM_AMMO_ROCKET
]);
256 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID
[ITEM_AMMO_ROCKET_BOX
]);
257 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID
[ITEM_AMMO_CELL
]);
258 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID
[ITEM_AMMO_CELL_BIG
]);
259 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID
[ITEM_AMMO_FUELCAN
]);
260 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID
[ITEM_AMMO_BACKPACK
]);
261 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID
[ITEM_KEY_RED
]);
262 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID
[ITEM_KEY_GREEN
]);
263 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID
[ITEM_KEY_BLUE
]);
264 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID
[ITEM_WEAPON_KASTET
]);
265 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID
[ITEM_WEAPON_PISTOL
]);
266 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID
[ITEM_JETPACK
]);
269 procedure g_Items_LoadData();
271 e_WriteLog('Loading items data...', MSG_NOTIFY
);
273 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD
+':SOUNDS\RESPAWNITEM');
274 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD
+':SOUNDS\GETRULEZ');
275 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD
+':SOUNDS\GETWEAPON');
276 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD
+':SOUNDS\GETITEM');
278 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD
+':TEXTURES\SBLUE', 32, 32, 4, True);
279 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD
+':TEXTURES\SWHITE', 32, 32, 4, True);
280 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD
+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
281 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD
+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
282 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD
+':TEXTURES\INVUL', 32, 32, 4, True);
283 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD
+':TEXTURES\INVIS', 32, 32, 4, True);
284 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD
+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
285 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD
+':TEXTURES\BOTTLE', 16, 32, 4, True);
286 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD
+':TEXTURES\HELMET', 16, 16, 4, True);
287 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD
+':TEXTURES\FLAGRED', 64, 64, 5, False);
288 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD
+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
289 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD
+':TEXTURES\FLAGDOM', 64, 64, 5, False);
290 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD
+':TEXTURES\MED1');
291 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD
+':TEXTURES\MED2');
292 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD
+':TEXTURES\SAW');
293 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD
+':TEXTURES\PISTOL');
294 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD
+':TEXTURES\KASTET');
295 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD
+':TEXTURES\SHOTGUN1');
296 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD
+':TEXTURES\SHOTGUN2');
297 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD
+':TEXTURES\MGUN');
298 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD
+':TEXTURES\RLAUNCHER');
299 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD
+':TEXTURES\PGUN');
300 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD
+':TEXTURES\BFG');
301 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD
+':TEXTURES\SPULEMET');
302 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD
+':TEXTURES\FLAMETHROWER');
303 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD
+':TEXTURES\CLIP');
304 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD
+':TEXTURES\AMMO');
305 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD
+':TEXTURES\SHELL1');
306 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD
+':TEXTURES\SHELL2');
307 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD
+':TEXTURES\ROCKET');
308 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD
+':TEXTURES\ROCKETS');
309 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD
+':TEXTURES\CELL');
310 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD
+':TEXTURES\CELL2');
311 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD
+':TEXTURES\FUELCAN');
312 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD
+':TEXTURES\BPACK');
313 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD
+':TEXTURES\KEYR');
314 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD
+':TEXTURES\KEYG');
315 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD
+':TEXTURES\KEYB');
316 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD
+':TEXTURES\OXYGEN');
317 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD
+':TEXTURES\SUIT');
318 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD
+':TEXTURES\KASTET');
319 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD
+':TEXTURES\BMED');
320 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD
+':TEXTURES\JETPACK');
324 //itemTree := TDynAABBTreeItem.Create();
325 freeIds
:= binHeapNewIntLess();
329 procedure g_Items_FreeData();
331 e_WriteLog('Releasing items data...', MSG_NOTIFY
);
333 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
334 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
335 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
336 g_Sound_Delete('SOUND_ITEM_GETITEM');
338 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
339 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
340 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
341 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
342 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
343 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
344 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
345 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
346 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
347 g_Frames_DeleteByName('FRAMES_FLAG_RED');
348 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
349 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
350 g_Texture_Delete('ITEM_MEDKIT_SMALL');
351 g_Texture_Delete('ITEM_MEDKIT_LARGE');
352 g_Texture_Delete('ITEM_WEAPON_SAW');
353 g_Texture_Delete('ITEM_WEAPON_PISTOL');
354 g_Texture_Delete('ITEM_WEAPON_KASTET');
355 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
356 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
357 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
358 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
359 g_Texture_Delete('ITEM_WEAPON_PLASMA');
360 g_Texture_Delete('ITEM_WEAPON_BFG');
361 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
362 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
363 g_Texture_Delete('ITEM_AMMO_BULLETS');
364 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
365 g_Texture_Delete('ITEM_AMMO_SHELLS');
366 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
367 g_Texture_Delete('ITEM_AMMO_ROCKET');
368 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
369 g_Texture_Delete('ITEM_AMMO_CELL');
370 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
371 g_Texture_Delete('ITEM_AMMO_FUELCAN');
372 g_Texture_Delete('ITEM_AMMO_BACKPACK');
373 g_Texture_Delete('ITEM_KEY_RED');
374 g_Texture_Delete('ITEM_KEY_GREEN');
375 g_Texture_Delete('ITEM_KEY_BLUE');
376 g_Texture_Delete('ITEM_OXYGEN');
377 g_Texture_Delete('ITEM_SUIT');
378 g_Texture_Delete('ITEM_WEAPON_KASTET');
379 g_Texture_Delete('ITEM_MEDKIT_BLACK');
380 g_Texture_Delete('ITEM_JETPACK');
387 procedure releaseItem (idx
: Integer);
391 if (idx
< 0) or (idx
> High(ggItems
)) then raise Exception
.Create('releaseItem: invalid item id');
393 //if (it.treeNode = -1) then raise Exception.Create('releaseItem: trying to release unallocated item');
394 if not it
.slotIsUsed
then raise Exception
.Create('releaseItem: trying to release unallocated item');
395 if (it
.arrIdx
<> idx
) then raise Exception
.Create('releaseItem: arrIdx inconsistency');
396 //itemTree.removeObject(it.treeNode);
398 it
.slotIsUsed
:= false;
399 if (it
.Animation
<> nil) then
405 it
.SpawnTrigger
:= -1;
406 it
.ItemType
:= ITEM_NONE
;
407 freeIds
.insert(it
.arrIdx
);
411 function allocItem (): DWORD
;
416 if (freeIds
.count
= 0) then
419 olen
:= Length(ggItems
);
420 SetLength(ggItems
, olen
+64);
421 for i
:= olen
to High(ggItems
) do
425 it
.slotIsUsed
:= false;
427 it
.ItemType
:= ITEM_NONE
;
430 it
.SpawnTrigger
:= -1;
431 it
.Respawnable
:= false;
436 result
:= freeIds
.front
;
439 if (result
> High(ggItems
)) then raise Exception
.Create('allocItem: freeid list corrupted');
440 if (ggItems
[result
].arrIdx
<> result
) then raise Exception
.Create('allocItem: arrIdx inconsistency');
444 // it will be slow if the slot is free (we have to rebuild the heap)
445 function wantItemSlot (slot
: Integer): Integer;
449 rebuildFreeList
: Boolean = true;
451 if (slot
< 0) or (slot
> $0fffffff) then raise Exception
.Create('wantItemSlot: bad item slot request');
452 // do we need to grow item storate?
453 olen
:= Length(ggItems
);
454 if (slot
>= olen
) then
457 SetLength(ggItems
, slot
+64);
458 // add free slots to free list
459 for i
:= olen
to High(ggItems
) do
463 it
.slotIsUsed
:= false;
465 it
.ItemType
:= ITEM_NONE
;
468 it
.SpawnTrigger
:= -1;
469 it
.Respawnable
:= false;
470 if (i
<> slot
) then freeIds
.insert(i
);
472 rebuildFreeList
:= false;
475 it
:= @ggItems
[slot
];
476 if {(it.treeNode = -1)} not it
.slotIsUsed
then
478 // this is unused slot; get it, and rebuild id list
479 if rebuildFreeList
then
482 for i
:= 0 to High(ggItems
) do
484 if (i
<> slot
) and {(ggItems[i].treeNode = -1)} (not it
.slotIsUsed
) then freeIds
.insert(i
);
490 // it will be readded
491 //itemTree.removeObject(it.treeNode);
493 it
.slotIsUsed
:= false;
500 // ////////////////////////////////////////////////////////////////////////// //
501 procedure g_Items_Init ();
505 if gMapInfo
.Height
> gPlayerScreenSize
.Y
then a
:= gMapInfo
.Height
-gPlayerScreenSize
.Y
else a
:= gMapInfo
.Height
;
506 if gMapInfo
.Width
> gPlayerScreenSize
.X
then b
:= gMapInfo
.Width
-gPlayerScreenSize
.X
else b
:= gMapInfo
.Width
;
507 gMaxDist
:= Trunc(Hypot(a
, b
));
511 procedure g_Items_Free ();
515 if (ggItems
<> nil) then
517 for i
:= 0 to High(ggItems
) do ggItems
[i
].Animation
.Free();
520 //if (itemTree <> nil) then itemTree.reset();
525 function g_Items_Create (X
, Y
: Integer; ItemType
: Byte;
526 Fall
, Respawnable
: Boolean; AdjCoord
: Boolean = False; ForcedID
: Integer = -1): DWORD
;
532 if ForcedID
< 0 then find_id
:= allocItem() else find_id
:= wantItemSlot(ForcedID
);
534 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id
)]), MSG_NOTIFY
);{$ENDIF}
536 it
:= @ggItems
[find_id
];
538 //if (it.treeNode <> -1) then raise Exception.Create('g_Items_Create: trying to reuse already allocated item');
539 if (it
.arrIdx
<> find_id
) then raise Exception
.Create('g_Items_Create: arrIdx inconsistency');
541 //it.arrIdx := find_id;
542 it
.slotIsUsed
:= true;
544 it
.ItemType
:= ItemType
;
545 it
.Respawnable
:= Respawnable
;
546 if g_Game_IsServer
and (ITEM_RESPAWNTIME
= 0) then it
.Respawnable
:= False;
552 it
.QuietRespawn
:= False;
557 it
.Obj
.Rect
.Width
:= ITEMSIZE
[ItemType
][0];
558 it
.Obj
.Rect
.Height
:= ITEMSIZE
[ItemType
][1];
561 it
.SpawnTrigger
:= -1;
563 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
568 Obj
.X
:= X
- (Obj
.Rect
.Width
div 2);
569 Obj
.Y
:= Y
- Obj
.Rect
.Height
;
575 // Óñòàíîâêà àíèìàöèè
577 ITEM_ARMOR_GREEN
: if g_Frames_Get(ID
, 'FRAMES_ITEM_ARMORGREEN') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
578 ITEM_ARMOR_BLUE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_ARMORBLUE') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
579 ITEM_SPHERE_BLUE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_BLUESPHERE') then it
.Animation
:= TAnimation
.Create(ID
, True, 15);
580 ITEM_SPHERE_WHITE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_WHITESPHERE') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
581 ITEM_INVUL
: if g_Frames_Get(ID
, 'FRAMES_ITEM_INVUL') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
582 ITEM_INVIS
: if g_Frames_Get(ID
, 'FRAMES_ITEM_INVIS') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
583 ITEM_BOTTLE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_BOTTLE') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
584 ITEM_HELMET
: if g_Frames_Get(ID
, 'FRAMES_ITEM_HELMET') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
587 it
.positionChanged();
593 procedure g_Items_Update ();
601 if (ggItems
= nil) then exit
;
603 for i
:= 0 to High(ggItems
) do
605 if (ggItems
[i
].ItemType
= ITEM_NONE
) then continue
;
615 m
:= g_Obj_Move(@Obj
, True, True);
616 positionChanged(); // this updates spatial accelerators
618 // Ñîïðîòèâëåíèå âîçäóõà
619 if gTime
mod (GAME_TICK
*2) = 0 then Obj
.Vel
.X
:= z_dec(Obj
.Vel
.X
, 1);
621 // Åñëè âûïàë çà êàðòó
622 if WordBool(m
and MOVE_FALLOUT
) then
624 if SpawnTrigger
= -1 then
631 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_ItemDestroy(True, i
);
637 // Åñëè èãðîêè ïîáëèçîñòè
638 if (gPlayers
<> nil) then
640 j
:= Random(Length(gPlayers
))-1;
642 for k
:= 0 to High(gPlayers
) do
645 if j
> High(gPlayers
) then j
:= 0;
647 if (gPlayers
[j
] <> nil) and gPlayers
[j
].Live
and g_Obj_Collide(@gPlayers
[j
].Obj
, @Obj
) then
649 if g_Game_IsClient
then continue
;
651 if not gPlayers
[j
].PickItem(ItemType
, Respawnable
, r
) then continue
;
653 if g_Game_IsNet
then MH_SEND_PlayerStats(gPlayers
[j
].UID
);
657 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
658 +2. I_MEGA,I_INVL,I_SUPER
659 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
661 g_Items_EmitPickupSoundAt(i
, gPlayers
[j
].Obj
.X
, gPlayers
[j
].Obj
.Y
);
663 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
666 if not Respawnable
then g_Items_Remove(i
) else g_Items_Pick(i
);
667 if g_Game_IsNet
then MH_SEND_ItemDestroy(False, i
);
675 if nxt
then continue
;
678 if Respawnable
and g_Game_IsServer
then
680 DecMin(RespawnTime
, 0);
681 if (RespawnTime
= 0) and (not Live
) then
683 if not QuietRespawn
then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX
, InitY
);
685 if g_Frames_Get(ID
, 'FRAMES_ITEM_RESPAWN') then
687 Anim
:= TAnimation
.Create(ID
, False, 4);
688 g_GFX_OnceAnim(InitX
+(Obj
.Rect
.Width
div 2)-16, InitY
+(Obj
.Rect
.Height
div 2)-16, Anim
);
698 positionChanged(); // this updates spatial accelerators
702 if g_Game_IsNet
then MH_SEND_ItemSpawn(QuietRespawn
, i
);
703 QuietRespawn
:= false;
707 if (Animation
<> nil) then Animation
.Update();
713 procedure g_Items_Draw ();
717 if (ggItems
= nil) then exit
;
719 for i
:= 0 to High(ggItems
) do
721 if not ggItems
[i
].Live
then continue
;
725 if g_Collide(Obj
.X
, Obj
.Y
, Obj
.Rect
.Width
, Obj
.Rect
.Height
, sX
, sY
, sWidth
, sHeight
) then
727 if (Animation
= nil) then
729 e_Draw(gItemsTexturesID
[ItemType
], Obj
.X
, Obj
.Y
, 0, true, false);
733 Animation
.Draw(Obj
.X
, Obj
.Y
, M_NONE
);
736 if g_debug_Frames
then
738 e_DrawQuad(Obj
.X
+Obj
.Rect
.X
,
740 Obj
.X
+Obj
.Rect
.X
+Obj
.Rect
.Width
-1,
741 Obj
.Y
+Obj
.Rect
.Y
+Obj
.Rect
.Height
-1,
750 procedure g_Items_Pick (ID
: DWORD
);
752 ggItems
[ID
].Live
:= false;
753 ggItems
[ID
].RespawnTime
:= ITEM_RESPAWNTIME
;
757 procedure g_Items_Remove (ID
: DWORD
);
761 {$IF DEFINED(D2F_DEBUG)}
765 if not g_Items_ValidId(ID
) then raise Exception
.Create('g_Items_Remove: invalid item id');
768 if (it
.arrIdx
<> ID
) then raise Exception
.Create('g_Items_Remove: arrIdx desync');
770 {$IF DEFINED(D2F_DEBUG)}
771 //itemTree.getNodeXY(it.treeNode, x, y);
772 //e_WriteLog(Format('removing item #%d: updating tree; nodeid=%d; x=%d; y=%d (%d,%d)', [it.arrIdx, it.treeNode, x, y, it.Obj.X, it.Obj.Y]), MSG_NOTIFY);
775 trig
:= it
.SpawnTrigger
;
779 if (trig
> -1) then g_Triggers_DecreaseSpawner(trig
);
783 procedure g_Items_SaveState (var Mem
: TBinMemoryWriter
);
788 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
790 if (ggItems
<> nil) then
792 for i
:= 0 to High(ggItems
) do if (ggItems
[i
].ItemType
<> ITEM_NONE
) then Inc(count
);
795 Mem
:= TBinMemoryWriter
.Create((count
+1) * 60);
797 // Êîëè÷åñòâî ïðåäìåòîâ
800 if (count
= 0) then exit
;
802 for i
:= 0 to High(ggItems
) do
804 if (ggItems
[i
].ItemType
<> ITEM_NONE
) then
806 // Ñèãíàòóðà ïðåäìåòà
807 sig
:= ITEM_SIGNATURE
; // 'ITEM'
810 Mem
.WriteByte(ggItems
[i
].ItemType
);
812 Mem
.WriteBoolean(ggItems
[i
].Respawnable
);
813 // Êîîðäèíàòû ðåñïóíà
814 Mem
.WriteInt(ggItems
[i
].InitX
);
815 Mem
.WriteInt(ggItems
[i
].InitY
);
817 Mem
.WriteWord(ggItems
[i
].RespawnTime
);
818 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
819 Mem
.WriteBoolean(ggItems
[i
].Live
);
820 // Ìîæåò ëè îí ïàäàòü
821 Mem
.WriteBoolean(ggItems
[i
].Fall
);
822 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
823 Mem
.WriteInt(ggItems
[i
].SpawnTrigger
);
825 Obj_SaveState(@ggItems
[i
].Obj
, Mem
);
831 procedure g_Items_LoadState (var Mem
: TBinMemoryReader
);
833 count
, i
, a
: Integer;
837 if (Mem
= nil) then exit
;
841 // Êîëè÷åñòâî ïðåäìåòîâ
844 if (count
= 0) then Exit
;
846 for a
:= 0 to count
-1 do
848 // Ñèãíàòóðà ïðåäìåòà
850 if (sig
<> ITEM_SIGNATURE
) then raise EBinSizeError
.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
854 i
:= g_Items_Create(0, 0, b
, False, False);
856 Mem
.ReadBoolean(ggItems
[i
].Respawnable
);
857 // Êîîðäèíàòû ðåñïóíà
858 Mem
.ReadInt(ggItems
[i
].InitX
);
859 Mem
.ReadInt(ggItems
[i
].InitY
);
861 Mem
.ReadWord(ggItems
[i
].RespawnTime
);
862 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
863 Mem
.ReadBoolean(ggItems
[i
].Live
);
864 // Ìîæåò ëè îí ïàäàòü
865 Mem
.ReadBoolean(ggItems
[i
].Fall
);
866 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
867 Mem
.ReadInt(ggItems
[i
].SpawnTrigger
);
869 Obj_LoadState(@ggItems
[i
].Obj
, Mem
);
874 procedure g_Items_RestartRound ();
879 for i
:= 0 to High(ggItems
) do
882 if it
.Respawnable
then
884 it
.QuietRespawn
:= True;
890 if g_Game_IsNet
then MH_SEND_ItemDestroy(True, i
);
896 function g_Items_ForEachAlive (cb
: TItemEachAliveCB
; backwards
: Boolean=false): Boolean;
901 if (ggItems
= nil) or not assigned(cb
) then exit
;
905 for idx
:= High(ggItems
) downto 0 do
907 if ggItems
[idx
].Live
then
909 result
:= cb(@ggItems
[idx
]);
916 for idx
:= 0 to High(ggItems
) do
918 if ggItems
[idx
].Live
then
920 result
:= cb(@ggItems
[idx
]);
928 // ////////////////////////////////////////////////////////////////////////// //
929 procedure g_Items_EmitPickupSound (idx
: Integer);
933 if not g_Items_ValidId(idx
) then exit
;
935 g_Items_EmitPickupSoundAt(idx
, it
.Obj
.X
, it
.Obj
.Y
);
938 procedure g_Items_EmitPickupSoundAt (idx
, x
, y
: Integer);
942 if not g_Items_ValidId(idx
) then exit
;
945 if gSoundEffectsDF
then
947 if it
.ItemType
in [ITEM_SPHERE_BLUE
, ITEM_SPHERE_WHITE
, ITEM_INVUL
,
948 ITEM_INVIS
, ITEM_MEDKIT_BLACK
, ITEM_JETPACK
] then
950 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x
, y
);
952 else if it
.ItemType
in [ITEM_WEAPON_SAW
, ITEM_WEAPON_PISTOL
, ITEM_WEAPON_SHOTGUN1
, ITEM_WEAPON_SHOTGUN2
,
953 ITEM_WEAPON_CHAINGUN
, ITEM_WEAPON_ROCKETLAUNCHER
, ITEM_WEAPON_PLASMA
,
954 ITEM_WEAPON_BFG
, ITEM_WEAPON_SUPERPULEMET
, ITEM_WEAPON_FLAMETHROWER
,
955 ITEM_AMMO_BACKPACK
] then
957 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x
, y
);
961 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x
, y
);
966 if it
.ItemType
in [ITEM_SPHERE_BLUE
, ITEM_SPHERE_WHITE
, ITEM_SUIT
,
967 ITEM_MEDKIT_BLACK
, ITEM_INVUL
, ITEM_INVIS
, ITEM_JETPACK
] then
969 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x
, y
);
971 else if it
.ItemType
in [ITEM_WEAPON_SAW
, ITEM_WEAPON_PISTOL
, ITEM_WEAPON_SHOTGUN1
, ITEM_WEAPON_SHOTGUN2
,
972 ITEM_WEAPON_CHAINGUN
, ITEM_WEAPON_ROCKETLAUNCHER
, ITEM_WEAPON_PLASMA
,
973 ITEM_WEAPON_BFG
, ITEM_WEAPON_SUPERPULEMET
, ITEM_WEAPON_FLAMETHROWER
] then
975 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x
, y
);
979 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x
, y
);