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
;
29 arrIdx
: Integer; // in ggItems
34 InitX
, InitY
: Integer;
38 QuietRespawn
: Boolean;
39 SpawnTrigger
: Integer;
41 Animation
: TAnimation
;
43 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
45 property myid
: Integer read arrIdx
;
48 procedure g_Items_LoadData();
49 procedure g_Items_FreeData();
50 procedure g_Items_Init();
51 procedure g_Items_Free();
52 function g_Items_Create(X
, Y
: Integer; ItemType
: Byte;
53 Fall
, Respawnable
: Boolean; AdjCoord
: Boolean = False; ForcedID
: Integer = -1): DWORD
;
54 procedure g_Items_Update();
55 procedure g_Items_Draw();
56 procedure g_Items_Pick(ID
: DWORD
);
57 procedure g_Items_Remove(ID
: DWORD
);
58 procedure g_Items_SaveState(var Mem
: TBinMemoryWriter
);
59 procedure g_Items_LoadState(var Mem
: TBinMemoryReader
);
61 procedure g_Items_RestartRound ();
63 function g_ItemValidId (idx
: Integer): Boolean; inline;
64 function g_ItemByIdx (idx
: Integer): PItem
;
65 function g_ItemObjByIdx (idx
: Integer): PObj
;
67 procedure g_Item_EmitPickupSound (idx
: Integer); // at item position
68 procedure g_Item_EmitPickupSoundAt (idx
, x
, y
: Integer);
72 TItemEachAliveCB
= function (it
: PItem
): Boolean is nested
; // return `true` to stop
74 function g_Items_ForEachAlive (cb
: TItemEachAliveCB
; backwards
: Boolean=false): Boolean;
78 gItemsTexturesID
: Array [1..ITEM_MAX
] of DWORD
;
79 gMaxDist
: Integer = 1;
80 ITEM_RESPAWNTIME
: Integer = 60 * 36;
85 g_basic
, e_graphics
, g_sound
, g_main
, g_gfx
, g_map
,
86 Math
, g_game
, g_triggers
, g_console
, SysUtils
, g_player
, g_net
, g_netmsg
,
88 g_grid
, z_aabbtree
, binheap
;
91 // ////////////////////////////////////////////////////////////////////////// //
93 itemTree
: TDynAABBTree
= nil;
94 ggItems
: Array of TItem
= nil;
95 freeIds
: TBinaryHeapInt
= nil; // free item ids
98 // ////////////////////////////////////////////////////////////////////////// //
99 function g_ItemValidId (idx
: Integer): Boolean; inline;
102 if (idx
< 0) or (idx
> High(ggItems
)) then exit
;
103 if (ggItems
[idx
].treeNode
= -1) then exit
;
108 function g_ItemByIdx (idx
: Integer): PItem
;
110 if (idx
< 0) or (idx
> High(ggItems
)) then raise Exception
.Create('g_ItemObjByIdx: invalid index');
111 result
:= @ggItems
[idx
];
112 if (result
.treeNode
= -1) then raise Exception
.Create('g_ItemObjByIdx: requested inexistent item');
116 function g_ItemObjByIdx (idx
: Integer): PObj
;
118 if (idx
< 0) or (idx
> High(ggItems
)) then raise Exception
.Create('g_ItemObjByIdx: invalid index');
119 if (ggItems
[idx
].treeNode
= -1) then raise Exception
.Create('g_ItemObjByIdx: requested inexistent item');
120 result
:= @ggItems
[idx
].Obj
;
124 // ////////////////////////////////////////////////////////////////////////// //
126 TDynAABBTreeItem
= class(TDynAABBTree
)
127 function getFleshAABB (var aabb
: AABB2D
; flesh
: TTreeFlesh
; tag
: Integer): Boolean; override;
130 function TDynAABBTreeItem
.getFleshAABB (var aabb
: AABB2D
; flesh
: TTreeFlesh
; tag
: Integer): Boolean;
135 if (flesh
= nil) then begin aabb
:= AABB2D
.Create(0, 0, 0, 0); exit
; end;
136 //if not g_ItemValidId(tag) then raise Exception.Create('DynTree: trying to get dimensions of inexistant item');
137 if (tag
< 0) or (tag
> High(ggItems
)) then raise Exception
.Create('DynTree: trying to get dimensions of inexistant item');
139 if (it
.Obj
.Rect
.Width
< 1) or (it
.Obj
.Rect
.Height
< 1) then exit
;
140 aabb
:= AABB2D
.Create(it
.Obj
.X
, it
.Obj
.Y
, it
.Obj
.X
+it
.Obj
.Rect
.Width
, it
.Obj
.Y
+it
.Obj
.Rect
.Height
);
141 if not aabb
.valid
then raise Exception
.Create('wutafuuuuuuu?!');
146 // ////////////////////////////////////////////////////////////////////////// //
147 procedure TItem
.positionChanged ();
151 if (treeNode
= -1) then
153 treeNode
:= itemTree
.insertObject(itemTree
{doesn't matter}, arrIdx
, true); // static object
154 itemTree
.getNodeXY(treeNode
, x
, y
);
155 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('item #%d: inserted into the tree; nodeid=%d; x=%d; y=%d', [arrIdx
, treeNode
, x
, y
]), MSG_NOTIFY
);{$ENDIF}
159 itemTree
.getNodeXY(treeNode
, x
, y
);
160 if (Obj
.X
= x
) and (Obj
.Y
= y
) then exit
; // nothing to do
161 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('item #%d: updating tree; nodeid=%d; x=%d; y=%d', [arrIdx
, treeNode
, x
, y
]), MSG_NOTIFY
);{$ENDIF}
164 itemTree
.updateObject(treeNode
);
166 itemTree
.removeObject(treeNode
);
167 treeNode
:= itemTree
.insertObject(itemTree
{doesn't matter}, arrIdx
, true); // static object
170 itemTree
.getNodeXY(treeNode
, x
, y
);
171 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('item #%d: updated tree; nodeid=%d; x=%d; y=%d', [arrIdx
, treeNode
, x
, y
]), MSG_NOTIFY
);{$ENDIF}
176 // ////////////////////////////////////////////////////////////////////////// //
178 ITEM_SIGNATURE
= $4D455449; // 'ITEM'
180 ITEMSIZE
: Array [ITEM_MEDKIT_SMALL
..ITEM_MAX
] of Array [0..1] of Byte =
181 (((14), (15)), // MEDKIT_SMALL
182 ((28), (19)), // MEDKIT_LARGE
183 ((28), (19)), // MEDKIT_BLACK
184 ((31), (16)), // ARMOR_GREEN
185 ((31), (16)), // ARMOR_BLUE
186 ((25), (25)), // SPHERE_BLUE
187 ((25), (25)), // SPHERE_WHITE
188 ((24), (47)), // SUIT
189 ((14), (27)), // OXYGEN
190 ((25), (25)), // INVUL
191 ((62), (24)), // WEAPON_SAW
192 ((63), (12)), // WEAPON_SHOTGUN1
193 ((54), (13)), // WEAPON_SHOTGUN2
194 ((54), (16)), // WEAPON_CHAINGUN
195 ((62), (16)), // WEAPON_ROCKETLAUNCHER
196 ((54), (16)), // WEAPON_PLASMA
197 ((61), (36)), // WEAPON_BFG
198 ((54), (16)), // WEAPON_SUPERPULEMET
199 (( 9), (11)), // AMMO_BULLETS
200 ((28), (16)), // AMMO_BULLETS_BOX
201 ((15), ( 7)), // AMMO_SHELLS
202 ((32), (12)), // AMMO_SHELLS_BOX
203 ((12), (27)), // AMMO_ROCKET
204 ((54), (21)), // AMMO_ROCKET_BOX
205 ((15), (12)), // AMMO_CELL
206 ((32), (21)), // AMMO_CELL_BIG
207 ((22), (29)), // AMMO_BACKPACK
208 ((16), (16)), // KEY_RED
209 ((16), (16)), // KEY_GREEN
210 ((16), (16)), // KEY_BLUE
211 (( 1), ( 1)), // WEAPON_KASTET
212 ((43), (16)), // WEAPON_PISTOL
213 ((14), (18)), // BOTTLE
214 ((16), (15)), // HELMET
215 ((32), (24)), // JETPACK
216 ((25), (25)), // INVIS
217 ((53), (20)), // WEAPON_FLAMETHROWER
218 ((13), (20))); // AMMO_FUELCAN
220 procedure InitTextures();
222 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID
[ITEM_MEDKIT_SMALL
]);
223 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID
[ITEM_MEDKIT_LARGE
]);
224 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID
[ITEM_MEDKIT_BLACK
]);
225 g_Texture_Get('ITEM_SUIT', gItemsTexturesID
[ITEM_SUIT
]);
226 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID
[ITEM_OXYGEN
]);
227 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID
[ITEM_WEAPON_SAW
]);
228 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID
[ITEM_WEAPON_SHOTGUN1
]);
229 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID
[ITEM_WEAPON_SHOTGUN2
]);
230 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID
[ITEM_WEAPON_CHAINGUN
]);
231 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID
[ITEM_WEAPON_ROCKETLAUNCHER
]);
232 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID
[ITEM_WEAPON_PLASMA
]);
233 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID
[ITEM_WEAPON_BFG
]);
234 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID
[ITEM_WEAPON_SUPERPULEMET
]);
235 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID
[ITEM_WEAPON_FLAMETHROWER
]);
236 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID
[ITEM_AMMO_BULLETS
]);
237 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID
[ITEM_AMMO_BULLETS_BOX
]);
238 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID
[ITEM_AMMO_SHELLS
]);
239 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID
[ITEM_AMMO_SHELLS_BOX
]);
240 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID
[ITEM_AMMO_ROCKET
]);
241 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID
[ITEM_AMMO_ROCKET_BOX
]);
242 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID
[ITEM_AMMO_CELL
]);
243 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID
[ITEM_AMMO_CELL_BIG
]);
244 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID
[ITEM_AMMO_FUELCAN
]);
245 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID
[ITEM_AMMO_BACKPACK
]);
246 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID
[ITEM_KEY_RED
]);
247 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID
[ITEM_KEY_GREEN
]);
248 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID
[ITEM_KEY_BLUE
]);
249 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID
[ITEM_WEAPON_KASTET
]);
250 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID
[ITEM_WEAPON_PISTOL
]);
251 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID
[ITEM_JETPACK
]);
254 procedure g_Items_LoadData();
256 e_WriteLog('Loading items data...', MSG_NOTIFY
);
258 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD
+':SOUNDS\RESPAWNITEM');
259 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD
+':SOUNDS\GETRULEZ');
260 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD
+':SOUNDS\GETWEAPON');
261 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD
+':SOUNDS\GETITEM');
263 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD
+':TEXTURES\SBLUE', 32, 32, 4, True);
264 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD
+':TEXTURES\SWHITE', 32, 32, 4, True);
265 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD
+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
266 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD
+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
267 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD
+':TEXTURES\INVUL', 32, 32, 4, True);
268 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD
+':TEXTURES\INVIS', 32, 32, 4, True);
269 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD
+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
270 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD
+':TEXTURES\BOTTLE', 16, 32, 4, True);
271 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD
+':TEXTURES\HELMET', 16, 16, 4, True);
272 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD
+':TEXTURES\FLAGRED', 64, 64, 5, False);
273 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD
+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
274 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD
+':TEXTURES\FLAGDOM', 64, 64, 5, False);
275 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD
+':TEXTURES\MED1');
276 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD
+':TEXTURES\MED2');
277 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD
+':TEXTURES\SAW');
278 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD
+':TEXTURES\PISTOL');
279 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD
+':TEXTURES\KASTET');
280 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD
+':TEXTURES\SHOTGUN1');
281 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD
+':TEXTURES\SHOTGUN2');
282 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD
+':TEXTURES\MGUN');
283 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD
+':TEXTURES\RLAUNCHER');
284 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD
+':TEXTURES\PGUN');
285 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD
+':TEXTURES\BFG');
286 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD
+':TEXTURES\SPULEMET');
287 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD
+':TEXTURES\FLAMETHROWER');
288 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD
+':TEXTURES\CLIP');
289 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD
+':TEXTURES\AMMO');
290 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD
+':TEXTURES\SHELL1');
291 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD
+':TEXTURES\SHELL2');
292 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD
+':TEXTURES\ROCKET');
293 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD
+':TEXTURES\ROCKETS');
294 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD
+':TEXTURES\CELL');
295 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD
+':TEXTURES\CELL2');
296 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD
+':TEXTURES\FUELCAN');
297 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD
+':TEXTURES\BPACK');
298 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD
+':TEXTURES\KEYR');
299 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD
+':TEXTURES\KEYG');
300 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD
+':TEXTURES\KEYB');
301 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD
+':TEXTURES\OXYGEN');
302 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD
+':TEXTURES\SUIT');
303 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD
+':TEXTURES\KASTET');
304 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD
+':TEXTURES\BMED');
305 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD
+':TEXTURES\JETPACK');
309 itemTree
:= TDynAABBTreeItem
.Create();
310 freeIds
:= binHeapNewIntLess();
314 procedure g_Items_FreeData();
316 e_WriteLog('Releasing items data...', MSG_NOTIFY
);
318 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
319 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
320 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
321 g_Sound_Delete('SOUND_ITEM_GETITEM');
323 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
324 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
325 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
326 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
327 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
328 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
329 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
330 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
331 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
332 g_Frames_DeleteByName('FRAMES_FLAG_RED');
333 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
334 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
335 g_Texture_Delete('ITEM_MEDKIT_SMALL');
336 g_Texture_Delete('ITEM_MEDKIT_LARGE');
337 g_Texture_Delete('ITEM_WEAPON_SAW');
338 g_Texture_Delete('ITEM_WEAPON_PISTOL');
339 g_Texture_Delete('ITEM_WEAPON_KASTET');
340 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
341 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
342 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
343 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
344 g_Texture_Delete('ITEM_WEAPON_PLASMA');
345 g_Texture_Delete('ITEM_WEAPON_BFG');
346 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
347 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
348 g_Texture_Delete('ITEM_AMMO_BULLETS');
349 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
350 g_Texture_Delete('ITEM_AMMO_SHELLS');
351 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
352 g_Texture_Delete('ITEM_AMMO_ROCKET');
353 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
354 g_Texture_Delete('ITEM_AMMO_CELL');
355 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
356 g_Texture_Delete('ITEM_AMMO_FUELCAN');
357 g_Texture_Delete('ITEM_AMMO_BACKPACK');
358 g_Texture_Delete('ITEM_KEY_RED');
359 g_Texture_Delete('ITEM_KEY_GREEN');
360 g_Texture_Delete('ITEM_KEY_BLUE');
361 g_Texture_Delete('ITEM_OXYGEN');
362 g_Texture_Delete('ITEM_SUIT');
363 g_Texture_Delete('ITEM_WEAPON_KASTET');
364 g_Texture_Delete('ITEM_MEDKIT_BLACK');
365 g_Texture_Delete('ITEM_JETPACK');
372 procedure releaseItem (idx
: Integer);
376 if (idx
< 0) or (idx
> High(ggItems
)) then raise Exception
.Create('releaseItem: invalid item id');
378 if (it
.treeNode
= -1) then raise Exception
.Create('releaseItem: trying to release unallocated item');
379 if (it
.arrIdx
<> idx
) then raise Exception
.Create('releaseItem: arrIdx inconsistency');
380 itemTree
.removeObject(it
.treeNode
);
382 if (it
.Animation
<> nil) then
388 it
.SpawnTrigger
:= -1;
389 it
.ItemType
:= ITEM_NONE
;
390 freeIds
.insert(it
.arrIdx
);
394 function allocItem (): DWORD
;
399 if (freeIds
.count
= 0) then
402 olen
:= Length(ggItems
);
403 SetLength(ggItems
, olen
+64);
404 for i
:= olen
to High(ggItems
) do
409 it
.ItemType
:= ITEM_NONE
;
412 it
.SpawnTrigger
:= -1;
413 it
.Respawnable
:= false;
418 result
:= freeIds
.front
;
421 if (result
> High(ggItems
)) then raise Exception
.Create('allocItem: freeid list corrupted');
422 if (ggItems
[result
].arrIdx
<> result
) then raise Exception
.Create('allocItem: arrIdx inconsistency');
426 // it will be slow if the slot is free (we have to rebuild the heap)
427 function wantItemSlot (slot
: Integer): Integer;
431 rebuildFreeList
: Boolean = true;
433 if (slot
< 0) or (slot
> $0fffffff) then raise Exception
.Create('wantItemSlot: bad item slot request');
434 // do we need to grow item storate?
435 olen
:= Length(ggItems
);
436 if (slot
>= olen
) then
439 SetLength(ggItems
, slot
+64);
440 // add free slots to free list
441 for i
:= olen
to High(ggItems
) do
446 it
.ItemType
:= ITEM_NONE
;
449 it
.SpawnTrigger
:= -1;
450 it
.Respawnable
:= false;
451 if (i
<> slot
) then freeIds
.insert(i
);
453 rebuildFreeList
:= false;
456 it
:= @ggItems
[slot
];
457 if (it
.treeNode
= -1) then
459 // this is unused slot; get it, and rebuild id list
460 if rebuildFreeList
then
463 for i
:= 0 to High(ggItems
) do
465 if (i
<> slot
) and (ggItems
[i
].treeNode
= -1) then freeIds
.insert(i
);
471 // it will be readded
472 itemTree
.removeObject(it
.treeNode
);
480 // ////////////////////////////////////////////////////////////////////////// //
481 procedure g_Items_Init ();
485 if gMapInfo
.Height
> gPlayerScreenSize
.Y
then a
:= gMapInfo
.Height
-gPlayerScreenSize
.Y
else a
:= gMapInfo
.Height
;
486 if gMapInfo
.Width
> gPlayerScreenSize
.X
then b
:= gMapInfo
.Width
-gPlayerScreenSize
.X
else b
:= gMapInfo
.Width
;
487 gMaxDist
:= Trunc(Hypot(a
, b
));
491 procedure g_Items_Free ();
495 if (ggItems
<> nil) then
497 for i
:= 0 to High(ggItems
) do ggItems
[i
].Animation
.Free();
500 if (itemTree
<> nil) then itemTree
.reset();
505 function g_Items_Create (X
, Y
: Integer; ItemType
: Byte;
506 Fall
, Respawnable
: Boolean; AdjCoord
: Boolean = False; ForcedID
: Integer = -1): DWORD
;
512 if ForcedID
< 0 then find_id
:= allocItem() else find_id
:= wantItemSlot(ForcedID
);
514 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id
)]), MSG_NOTIFY
);{$ENDIF}
516 it
:= @ggItems
[find_id
];
518 it
.ItemType
:= ItemType
;
519 it
.Respawnable
:= Respawnable
;
520 if g_Game_IsServer
and (ITEM_RESPAWNTIME
= 0) then it
.Respawnable
:= False;
526 it
.QuietRespawn
:= False;
528 if (it
.treeNode
<> -1) then raise Exception
.Create('g_Items_Create: trying to reuse already allocated item');
529 if (it
.arrIdx
<> find_id
) then raise Exception
.Create('g_Items_Create: arrIdx inconsistency');
531 //it.arrIdx := find_id;
536 it
.Obj
.Rect
.Width
:= ITEMSIZE
[ItemType
][0];
537 it
.Obj
.Rect
.Height
:= ITEMSIZE
[ItemType
][1];
540 it
.SpawnTrigger
:= -1;
542 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
547 Obj
.X
:= X
- (Obj
.Rect
.Width
div 2);
548 Obj
.Y
:= Y
- Obj
.Rect
.Height
;
554 // Óñòàíîâêà àíèìàöèè
556 ITEM_ARMOR_GREEN
: if g_Frames_Get(ID
, 'FRAMES_ITEM_ARMORGREEN') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
557 ITEM_ARMOR_BLUE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_ARMORBLUE') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
558 ITEM_SPHERE_BLUE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_BLUESPHERE') then it
.Animation
:= TAnimation
.Create(ID
, True, 15);
559 ITEM_SPHERE_WHITE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_WHITESPHERE') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
560 ITEM_INVUL
: if g_Frames_Get(ID
, 'FRAMES_ITEM_INVUL') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
561 ITEM_INVIS
: if g_Frames_Get(ID
, 'FRAMES_ITEM_INVIS') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
562 ITEM_BOTTLE
: if g_Frames_Get(ID
, 'FRAMES_ITEM_BOTTLE') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
563 ITEM_HELMET
: if g_Frames_Get(ID
, 'FRAMES_ITEM_HELMET') then it
.Animation
:= TAnimation
.Create(ID
, True, 20);
566 it
.positionChanged();
572 procedure g_Items_Update ();
580 if (ggItems
= nil) then exit
;
582 for i
:= 0 to High(ggItems
) do
584 if (ggItems
[i
].ItemType
= ITEM_NONE
) then continue
;
594 m
:= g_Obj_Move(@Obj
, True, True);
595 positionChanged(); // this updates spatial accelerators
597 // Ñîïðîòèâëåíèå âîçäóõà
598 if gTime
mod (GAME_TICK
*2) = 0 then Obj
.Vel
.X
:= z_dec(Obj
.Vel
.X
, 1);
600 // Åñëè âûïàë çà êàðòó
601 if WordBool(m
and MOVE_FALLOUT
) then
603 if SpawnTrigger
= -1 then
610 if g_Game_IsServer
and g_Game_IsNet
then MH_SEND_ItemDestroy(True, i
);
616 // Åñëè èãðîêè ïîáëèçîñòè
617 if (gPlayers
<> nil) then
619 j
:= Random(Length(gPlayers
))-1;
621 for k
:= 0 to High(gPlayers
) do
624 if j
> High(gPlayers
) then j
:= 0;
626 if (gPlayers
[j
] <> nil) and gPlayers
[j
].Live
and g_Obj_Collide(@gPlayers
[j
].Obj
, @Obj
) then
628 if g_Game_IsClient
then continue
;
630 if not gPlayers
[j
].PickItem(ItemType
, Respawnable
, r
) then continue
;
632 if g_Game_IsNet
then MH_SEND_PlayerStats(gPlayers
[j
].UID
);
636 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
637 +2. I_MEGA,I_INVL,I_SUPER
638 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
640 g_Item_EmitPickupSoundAt(i
, gPlayers
[j
].Obj
.X
, gPlayers
[j
].Obj
.Y
);
642 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
645 if not Respawnable
then g_Items_Remove(i
) else g_Items_Pick(i
);
646 if g_Game_IsNet
then MH_SEND_ItemDestroy(False, i
);
654 if nxt
then continue
;
657 if Respawnable
and g_Game_IsServer
then
659 DecMin(RespawnTime
, 0);
660 if (RespawnTime
= 0) and (not Live
) then
662 if not QuietRespawn
then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX
, InitY
);
664 if g_Frames_Get(ID
, 'FRAMES_ITEM_RESPAWN') then
666 Anim
:= TAnimation
.Create(ID
, False, 4);
667 g_GFX_OnceAnim(InitX
+(Obj
.Rect
.Width
div 2)-16, InitY
+(Obj
.Rect
.Height
div 2)-16, Anim
);
677 positionChanged(); // this updates spatial accelerators
681 if g_Game_IsNet
then MH_SEND_ItemSpawn(QuietRespawn
, i
);
682 QuietRespawn
:= false;
686 if (Animation
<> nil) then Animation
.Update();
692 procedure g_Items_Draw ();
696 if (ggItems
= nil) then exit
;
698 for i
:= 0 to High(ggItems
) do
700 if not ggItems
[i
].Live
then continue
;
704 if g_Collide(Obj
.X
, Obj
.Y
, Obj
.Rect
.Width
, Obj
.Rect
.Height
, sX
, sY
, sWidth
, sHeight
) then
706 if (Animation
= nil) then
708 e_Draw(gItemsTexturesID
[ItemType
], Obj
.X
, Obj
.Y
, 0, true, false);
712 Animation
.Draw(Obj
.X
, Obj
.Y
, M_NONE
);
715 if g_debug_Frames
then
717 e_DrawQuad(Obj
.X
+Obj
.Rect
.X
,
719 Obj
.X
+Obj
.Rect
.X
+Obj
.Rect
.Width
-1,
720 Obj
.Y
+Obj
.Rect
.Y
+Obj
.Rect
.Height
-1,
729 procedure g_Items_Pick (ID
: DWORD
);
731 ggItems
[ID
].Live
:= false;
732 ggItems
[ID
].RespawnTime
:= ITEM_RESPAWNTIME
;
736 procedure g_Items_Remove (ID
: DWORD
);
742 if not g_ItemValidId(ID
) then raise Exception
.Create('g_Items_Remove: invalid item id');
745 if (it
.arrIdx
<> ID
) then raise Exception
.Create('g_Items_Remove: arrIdx desync');
747 itemTree
.getNodeXY(it
.treeNode
, x
, y
);
748 {$IF DEFINED(D2F_DEBUG)}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
);{$ENDIF}
750 trig
:= it
.SpawnTrigger
;
754 if (trig
> -1) then g_Triggers_DecreaseSpawner(trig
);
758 procedure g_Items_SaveState (var Mem
: TBinMemoryWriter
);
763 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
765 if (ggItems
<> nil) then
767 for i
:= 0 to High(ggItems
) do if (ggItems
[i
].ItemType
<> ITEM_NONE
) then Inc(count
);
770 Mem
:= TBinMemoryWriter
.Create((count
+1) * 60);
772 // Êîëè÷åñòâî ïðåäìåòîâ
775 if (count
= 0) then exit
;
777 for i
:= 0 to High(ggItems
) do
779 if (ggItems
[i
].ItemType
<> ITEM_NONE
) then
781 // Ñèãíàòóðà ïðåäìåòà
782 sig
:= ITEM_SIGNATURE
; // 'ITEM'
785 Mem
.WriteByte(ggItems
[i
].ItemType
);
787 Mem
.WriteBoolean(ggItems
[i
].Respawnable
);
788 // Êîîðäèíàòû ðåñïóíà
789 Mem
.WriteInt(ggItems
[i
].InitX
);
790 Mem
.WriteInt(ggItems
[i
].InitY
);
792 Mem
.WriteWord(ggItems
[i
].RespawnTime
);
793 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
794 Mem
.WriteBoolean(ggItems
[i
].Live
);
795 // Ìîæåò ëè îí ïàäàòü
796 Mem
.WriteBoolean(ggItems
[i
].Fall
);
797 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
798 Mem
.WriteInt(ggItems
[i
].SpawnTrigger
);
800 Obj_SaveState(@ggItems
[i
].Obj
, Mem
);
806 procedure g_Items_LoadState (var Mem
: TBinMemoryReader
);
808 count
, i
, a
: Integer;
812 if (Mem
= nil) then exit
;
816 // Êîëè÷åñòâî ïðåäìåòîâ
819 if (count
= 0) then Exit
;
821 for a
:= 0 to count
-1 do
823 // Ñèãíàòóðà ïðåäìåòà
825 if (sig
<> ITEM_SIGNATURE
) then raise EBinSizeError
.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
829 i
:= g_Items_Create(0, 0, b
, False, False);
831 Mem
.ReadBoolean(ggItems
[i
].Respawnable
);
832 // Êîîðäèíàòû ðåñïóíà
833 Mem
.ReadInt(ggItems
[i
].InitX
);
834 Mem
.ReadInt(ggItems
[i
].InitY
);
836 Mem
.ReadWord(ggItems
[i
].RespawnTime
);
837 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
838 Mem
.ReadBoolean(ggItems
[i
].Live
);
839 // Ìîæåò ëè îí ïàäàòü
840 Mem
.ReadBoolean(ggItems
[i
].Fall
);
841 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
842 Mem
.ReadInt(ggItems
[i
].SpawnTrigger
);
844 Obj_LoadState(@ggItems
[i
].Obj
, Mem
);
849 procedure g_Items_RestartRound ();
854 for i
:= 0 to High(ggItems
) do
857 if it
.Respawnable
then
859 it
.QuietRespawn
:= True;
865 if g_Game_IsNet
then MH_SEND_ItemDestroy(True, i
);
871 function g_Items_ForEachAlive (cb
: TItemEachAliveCB
; backwards
: Boolean=false): Boolean;
876 if (ggItems
= nil) or not assigned(cb
) then exit
;
880 for idx
:= High(ggItems
) downto 0 do
882 if ggItems
[idx
].Live
then
884 result
:= cb(@ggItems
[idx
]);
891 for idx
:= 0 to High(ggItems
) do
893 if ggItems
[idx
].Live
then
895 result
:= cb(@ggItems
[idx
]);
903 // ////////////////////////////////////////////////////////////////////////// //
904 procedure g_Item_EmitPickupSound (idx
: Integer);
908 if not g_ItemValidId(idx
) then exit
;
910 g_Item_EmitPickupSoundAt(idx
, it
.Obj
.X
, it
.Obj
.Y
);
913 procedure g_Item_EmitPickupSoundAt (idx
, x
, y
: Integer);
917 if not g_ItemValidId(idx
) then exit
;
920 if gSoundEffectsDF
then
922 if it
.ItemType
in [ITEM_SPHERE_BLUE
, ITEM_SPHERE_WHITE
, ITEM_INVUL
,
923 ITEM_INVIS
, ITEM_MEDKIT_BLACK
, ITEM_JETPACK
] then
925 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x
, y
);
927 else if it
.ItemType
in [ITEM_WEAPON_SAW
, ITEM_WEAPON_PISTOL
, ITEM_WEAPON_SHOTGUN1
, ITEM_WEAPON_SHOTGUN2
,
928 ITEM_WEAPON_CHAINGUN
, ITEM_WEAPON_ROCKETLAUNCHER
, ITEM_WEAPON_PLASMA
,
929 ITEM_WEAPON_BFG
, ITEM_WEAPON_SUPERPULEMET
, ITEM_WEAPON_FLAMETHROWER
,
930 ITEM_AMMO_BACKPACK
] then
932 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x
, y
);
936 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x
, y
);
941 if it
.ItemType
in [ITEM_SPHERE_BLUE
, ITEM_SPHERE_WHITE
, ITEM_SUIT
,
942 ITEM_MEDKIT_BLACK
, ITEM_INVUL
, ITEM_INVIS
, ITEM_JETPACK
] then
944 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x
, y
);
946 else if it
.ItemType
in [ITEM_WEAPON_SAW
, ITEM_WEAPON_PISTOL
, ITEM_WEAPON_SHOTGUN1
, ITEM_WEAPON_SHOTGUN2
,
947 ITEM_WEAPON_CHAINGUN
, ITEM_WEAPON_ROCKETLAUNCHER
, ITEM_WEAPON_PLASMA
,
948 ITEM_WEAPON_BFG
, ITEM_WEAPON_SUPERPULEMET
, ITEM_WEAPON_FLAMETHROWER
] then
950 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x
, y
);
954 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x
, y
);