DEADSOFTWARE

no more tree for items (we don't need it)
[d2df-sdl.git] / src / game / g_items.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_items;
19 interface
21 uses
22 g_textures, g_phys, g_saveload, BinEditor, MAPDEF;
24 Type
25 PItem = ^TItem;
26 TItem = record
27 private
28 treeNode: Integer;
29 arrIdx: Integer; // in ggItems
31 public
32 ItemType: Byte;
33 Respawnable: Boolean;
34 InitX, InitY: Integer;
35 RespawnTime: Word;
36 Live: Boolean;
37 Fall: Boolean;
38 QuietRespawn: Boolean;
39 SpawnTrigger: Integer;
40 Obj: TObj;
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;
46 end;
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);
71 type
72 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
74 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
77 var
78 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
79 gMaxDist: Integer = 1;
80 ITEM_RESPAWNTIME: Integer = 60 * 36;
82 implementation
84 uses
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,
87 e_log,
88 g_grid, z_aabbtree, binheap;
91 var
92 ggItems: Array of TItem = nil;
95 // ////////////////////////////////////////////////////////////////////////// //
96 {
97 type
98 TDynAABBTreeItemBase = specialize TDynAABBTreeBase<Integer>;
100 TDynAABBTreeItem = class(TDynAABBTreeItemBase)
101 function getFleshAABB (out aabb: AABB2D; flesh: Integer; tag: Integer): Boolean; override;
102 end;
104 function TDynAABBTreeItem.getFleshAABB (out aabb: AABB2D; flesh: Integer; tag: Integer): Boolean;
105 var
106 it: PItem;
107 begin
108 result := false;
109 if (flesh < 0) or (flesh > High(ggItems)) then raise Exception.Create('DynTree: trying to get dimensions of inexistant item');
110 it := @ggItems[flesh];
111 if (it.Obj.Rect.Width < 1) or (it.Obj.Rect.Height < 1) then exit;
112 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);
113 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
114 result := true;
115 end;
118 // ////////////////////////////////////////////////////////////////////////// //
119 var
120 //itemTree: TDynAABBTreeItem = nil;
121 freeIds: TBinaryHeapInt = nil; // free item ids
124 // ////////////////////////////////////////////////////////////////////////// //
125 function g_ItemValidId (idx: Integer): Boolean; inline;
126 begin
127 result := false;
128 if (idx < 0) or (idx > High(ggItems)) then exit;
129 if (ggItems[idx].treeNode = -1) then exit;
130 result := true;
131 end;
134 function g_ItemByIdx (idx: Integer): PItem;
135 begin
136 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
137 result := @ggItems[idx];
138 if (result.treeNode = -1) then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
139 end;
142 function g_ItemObjByIdx (idx: Integer): PObj;
143 begin
144 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
145 if (ggItems[idx].treeNode = -1) then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
146 result := @ggItems[idx].Obj;
147 end;
150 // ////////////////////////////////////////////////////////////////////////// //
151 procedure TItem.positionChanged ();
152 //var
153 // x, y: Integer;
154 begin
155 (*
156 if (treeNode = -1) then
157 begin
158 treeNode := itemTree.insertObject(arrIdx, 0, true); // static object
159 {$IF DEFINED(D2F_DEBUG)}
160 itemTree.getNodeXY(treeNode, x, y);
161 e_WriteLog(Format('item #%d: inserted into the tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);
162 {$ENDIF}
163 end
164 else
165 begin
166 itemTree.getNodeXY(treeNode, x, y);
167 if (Obj.X = x) and (Obj.Y = y) then exit; // nothing to do
168 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('item #%d: updating tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);{$ENDIF}
170 {$IFDEF TRUE}
171 itemTree.updateObject(treeNode);
172 {$ELSE}
173 itemTree.removeObject(treeNode);
174 treeNode := itemTree.insertObject(arrIdx, 0, true); // static object
175 {$ENDIF}
177 {$IF DEFINED(D2F_DEBUG)}
178 itemTree.getNodeXY(treeNode, x, y);
179 e_WriteLog(Format('item #%d: updated tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);
180 {$ENDIF}
181 end;
182 *)
183 end;
186 // ////////////////////////////////////////////////////////////////////////// //
187 const
188 ITEM_SIGNATURE = $4D455449; // 'ITEM'
190 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
191 (((14), (15)), // MEDKIT_SMALL
192 ((28), (19)), // MEDKIT_LARGE
193 ((28), (19)), // MEDKIT_BLACK
194 ((31), (16)), // ARMOR_GREEN
195 ((31), (16)), // ARMOR_BLUE
196 ((25), (25)), // SPHERE_BLUE
197 ((25), (25)), // SPHERE_WHITE
198 ((24), (47)), // SUIT
199 ((14), (27)), // OXYGEN
200 ((25), (25)), // INVUL
201 ((62), (24)), // WEAPON_SAW
202 ((63), (12)), // WEAPON_SHOTGUN1
203 ((54), (13)), // WEAPON_SHOTGUN2
204 ((54), (16)), // WEAPON_CHAINGUN
205 ((62), (16)), // WEAPON_ROCKETLAUNCHER
206 ((54), (16)), // WEAPON_PLASMA
207 ((61), (36)), // WEAPON_BFG
208 ((54), (16)), // WEAPON_SUPERPULEMET
209 (( 9), (11)), // AMMO_BULLETS
210 ((28), (16)), // AMMO_BULLETS_BOX
211 ((15), ( 7)), // AMMO_SHELLS
212 ((32), (12)), // AMMO_SHELLS_BOX
213 ((12), (27)), // AMMO_ROCKET
214 ((54), (21)), // AMMO_ROCKET_BOX
215 ((15), (12)), // AMMO_CELL
216 ((32), (21)), // AMMO_CELL_BIG
217 ((22), (29)), // AMMO_BACKPACK
218 ((16), (16)), // KEY_RED
219 ((16), (16)), // KEY_GREEN
220 ((16), (16)), // KEY_BLUE
221 (( 1), ( 1)), // WEAPON_KASTET
222 ((43), (16)), // WEAPON_PISTOL
223 ((14), (18)), // BOTTLE
224 ((16), (15)), // HELMET
225 ((32), (24)), // JETPACK
226 ((25), (25)), // INVIS
227 ((53), (20)), // WEAPON_FLAMETHROWER
228 ((13), (20))); // AMMO_FUELCAN
230 procedure InitTextures();
231 begin
232 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
233 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
234 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
235 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
236 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
237 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
238 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
239 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
240 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
241 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
242 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
243 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
244 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
245 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
246 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
247 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
248 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
249 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
250 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
251 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
252 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
253 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
254 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
255 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
256 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
257 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
258 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
259 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
260 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
261 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID[ITEM_JETPACK]);
262 end;
264 procedure g_Items_LoadData();
265 begin
266 e_WriteLog('Loading items data...', MSG_NOTIFY);
268 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
269 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
270 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
271 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
273 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
274 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
275 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
276 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
277 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
278 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
279 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
280 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
281 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
282 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
283 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
284 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
285 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
286 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
287 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
288 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
289 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
290 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
291 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
292 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
293 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
294 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
295 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
296 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
297 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
298 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
299 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
300 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
301 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
302 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
303 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
304 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
305 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
306 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
307 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
308 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
309 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
310 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
311 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
312 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
313 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
314 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
315 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK');
317 InitTextures();
319 //itemTree := TDynAABBTreeItem.Create();
320 freeIds := binHeapNewIntLess();
321 end;
324 procedure g_Items_FreeData();
325 begin
326 e_WriteLog('Releasing items data...', MSG_NOTIFY);
328 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
329 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
330 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
331 g_Sound_Delete('SOUND_ITEM_GETITEM');
333 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
334 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
335 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
336 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
337 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
338 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
339 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
340 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
341 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
342 g_Frames_DeleteByName('FRAMES_FLAG_RED');
343 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
344 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
345 g_Texture_Delete('ITEM_MEDKIT_SMALL');
346 g_Texture_Delete('ITEM_MEDKIT_LARGE');
347 g_Texture_Delete('ITEM_WEAPON_SAW');
348 g_Texture_Delete('ITEM_WEAPON_PISTOL');
349 g_Texture_Delete('ITEM_WEAPON_KASTET');
350 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
351 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
352 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
353 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
354 g_Texture_Delete('ITEM_WEAPON_PLASMA');
355 g_Texture_Delete('ITEM_WEAPON_BFG');
356 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
357 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
358 g_Texture_Delete('ITEM_AMMO_BULLETS');
359 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
360 g_Texture_Delete('ITEM_AMMO_SHELLS');
361 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
362 g_Texture_Delete('ITEM_AMMO_ROCKET');
363 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
364 g_Texture_Delete('ITEM_AMMO_CELL');
365 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
366 g_Texture_Delete('ITEM_AMMO_FUELCAN');
367 g_Texture_Delete('ITEM_AMMO_BACKPACK');
368 g_Texture_Delete('ITEM_KEY_RED');
369 g_Texture_Delete('ITEM_KEY_GREEN');
370 g_Texture_Delete('ITEM_KEY_BLUE');
371 g_Texture_Delete('ITEM_OXYGEN');
372 g_Texture_Delete('ITEM_SUIT');
373 g_Texture_Delete('ITEM_WEAPON_KASTET');
374 g_Texture_Delete('ITEM_MEDKIT_BLACK');
375 g_Texture_Delete('ITEM_JETPACK');
377 //itemTree.Free();
378 freeIds.Free();
379 end;
382 procedure releaseItem (idx: Integer);
383 var
384 it: PItem;
385 begin
386 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
387 it := @ggItems[idx];
388 if (it.treeNode = -1) then raise Exception.Create('releaseItem: trying to release unallocated item');
389 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
390 //itemTree.removeObject(it.treeNode);
391 it.treeNode := -1;
392 if (it.Animation <> nil) then
393 begin
394 it.Animation.Free();
395 it.Animation := nil;
396 end;
397 it.Live := False;
398 it.SpawnTrigger := -1;
399 it.ItemType := ITEM_NONE;
400 freeIds.insert(it.arrIdx);
401 end;
404 function allocItem (): DWORD;
405 var
406 i, olen: Integer;
407 it: PItem;
408 begin
409 if (freeIds.count = 0) then
410 begin
411 // no free slots
412 olen := Length(ggItems);
413 SetLength(ggItems, olen+64);
414 for i := olen to High(ggItems) do
415 begin
416 it := @ggItems[i];
417 it.treeNode := -1;
418 it.arrIdx := i;
419 it.ItemType := ITEM_NONE;
420 it.Animation := nil;
421 it.Live := false;
422 it.SpawnTrigger := -1;
423 it.Respawnable := false;
424 freeIds.insert(i);
425 end;
426 end;
428 result := freeIds.front;
429 freeIds.popFront();
431 if (result > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
432 if (ggItems[result].arrIdx <> result) then raise Exception.Create('allocItem: arrIdx inconsistency');
433 end;
436 // it will be slow if the slot is free (we have to rebuild the heap)
437 function wantItemSlot (slot: Integer): Integer;
438 var
439 i, olen: Integer;
440 it: PItem;
441 rebuildFreeList: Boolean = true;
442 begin
443 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
444 // do we need to grow item storate?
445 olen := Length(ggItems);
446 if (slot >= olen) then
447 begin
448 // need more spice!
449 SetLength(ggItems, slot+64);
450 // add free slots to free list
451 for i := olen to High(ggItems) do
452 begin
453 it := @ggItems[i];
454 it.treeNode := -1;
455 it.arrIdx := i;
456 it.ItemType := ITEM_NONE;
457 it.Animation := nil;
458 it.Live := false;
459 it.SpawnTrigger := -1;
460 it.Respawnable := false;
461 if (i <> slot) then freeIds.insert(i);
462 end;
463 rebuildFreeList := false;
464 end;
466 it := @ggItems[slot];
467 if (it.treeNode = -1) then
468 begin
469 // this is unused slot; get it, and rebuild id list
470 if rebuildFreeList then
471 begin
472 freeIds.clear();
473 for i := 0 to High(ggItems) do
474 begin
475 if (i <> slot) and (ggItems[i].treeNode = -1) then freeIds.insert(i);
476 end;
477 end;
478 end
479 else
480 begin
481 // it will be readded
482 //itemTree.removeObject(it.treeNode);
483 it.treeNode := -1;
484 end;
486 result := slot;
487 end;
490 // ////////////////////////////////////////////////////////////////////////// //
491 procedure g_Items_Init ();
492 var
493 a, b: Integer;
494 begin
495 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
496 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
497 gMaxDist := Trunc(Hypot(a, b));
498 end;
501 procedure g_Items_Free ();
502 var
503 i: Integer;
504 begin
505 if (ggItems <> nil) then
506 begin
507 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
508 ggItems := nil;
509 end;
510 //if (itemTree <> nil) then itemTree.reset();
511 freeIds.clear();
512 end;
515 function g_Items_Create (X, Y: Integer; ItemType: Byte;
516 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
517 var
518 find_id: DWORD;
519 ID: DWORD;
520 it: PItem;
521 begin
522 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
524 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
526 it := @ggItems[find_id];
528 it.ItemType := ItemType;
529 it.Respawnable := Respawnable;
530 if g_Game_IsServer and (ITEM_RESPAWNTIME = 0) then it.Respawnable := False;
531 it.InitX := X;
532 it.InitY := Y;
533 it.RespawnTime := 0;
534 it.Fall := Fall;
535 it.Live := True;
536 it.QuietRespawn := False;
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');
540 //it.treeNode := -1;
541 //it.arrIdx := find_id;
543 g_Obj_Init(@it.Obj);
544 it.Obj.X := X;
545 it.Obj.Y := Y;
546 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
547 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
549 it.Animation := nil;
550 it.SpawnTrigger := -1;
552 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
553 if AdjCoord then
554 begin
555 with it^ do
556 begin
557 Obj.X := X - (Obj.Rect.Width div 2);
558 Obj.Y := Y - Obj.Rect.Height;
559 InitX := Obj.X;
560 InitY := Obj.Y;
561 end;
562 end;
564 // Óñòàíîâêà àíèìàöèè
565 case it.ItemType of
566 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
567 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
568 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
569 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
570 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
571 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
572 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
573 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
574 end;
576 it.positionChanged();
578 result := find_id;
579 end;
582 procedure g_Items_Update ();
583 var
584 i, j, k: Integer;
585 ID: DWord;
586 Anim: TAnimation;
587 m: Word;
588 r, nxt: Boolean;
589 begin
590 if (ggItems = nil) then exit;
592 for i := 0 to High(ggItems) do
593 begin
594 if (ggItems[i].ItemType = ITEM_NONE) then continue;
596 with ggItems[i] do
597 begin
598 nxt := False;
600 if Live then
601 begin
602 if Fall then
603 begin
604 m := g_Obj_Move(@Obj, True, True);
605 positionChanged(); // this updates spatial accelerators
607 // Ñîïðîòèâëåíèå âîçäóõà
608 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
610 // Åñëè âûïàë çà êàðòó
611 if WordBool(m and MOVE_FALLOUT) then
612 begin
613 if SpawnTrigger = -1 then
614 begin
615 g_Items_Pick(i);
616 end
617 else
618 begin
619 g_Items_Remove(i);
620 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
621 end;
622 continue;
623 end;
624 end;
626 // Åñëè èãðîêè ïîáëèçîñòè
627 if (gPlayers <> nil) then
628 begin
629 j := Random(Length(gPlayers))-1;
631 for k := 0 to High(gPlayers) do
632 begin
633 Inc(j);
634 if j > High(gPlayers) then j := 0;
636 if (gPlayers[j] <> nil) and gPlayers[j].Live and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
637 begin
638 if g_Game_IsClient then continue;
640 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
642 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
645 Doom 2D: Original:
646 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
647 +2. I_MEGA,I_INVL,I_SUPER
648 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
650 g_Item_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
652 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
653 if r then
654 begin
655 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
656 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
657 nxt := True;
658 break;
659 end;
660 end;
661 end;
662 end;
664 if nxt then continue;
665 end;
667 if Respawnable and g_Game_IsServer then
668 begin
669 DecMin(RespawnTime, 0);
670 if (RespawnTime = 0) and (not Live) then
671 begin
672 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
674 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
675 begin
676 Anim := TAnimation.Create(ID, False, 4);
677 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
678 Anim.Free();
679 end;
681 Obj.X := InitX;
682 Obj.Y := InitY;
683 Obj.Vel.X := 0;
684 Obj.Vel.Y := 0;
685 Obj.Accel.X := 0;
686 Obj.Accel.Y := 0;
687 positionChanged(); // this updates spatial accelerators
689 Live := true;
691 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
692 QuietRespawn := false;
693 end;
694 end;
696 if (Animation <> nil) then Animation.Update();
697 end;
698 end;
699 end;
702 procedure g_Items_Draw ();
703 var
704 i: Integer;
705 begin
706 if (ggItems = nil) then exit;
708 for i := 0 to High(ggItems) do
709 begin
710 if not ggItems[i].Live then continue;
712 with ggItems[i] do
713 begin
714 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
715 begin
716 if (Animation = nil) then
717 begin
718 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
719 end
720 else
721 begin
722 Animation.Draw(Obj.X, Obj.Y, M_NONE);
723 end;
725 if g_debug_Frames then
726 begin
727 e_DrawQuad(Obj.X+Obj.Rect.X,
728 Obj.Y+Obj.Rect.Y,
729 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
730 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
731 0, 255, 0);
732 end;
733 end;
734 end;
735 end;
736 end;
739 procedure g_Items_Pick (ID: DWORD);
740 begin
741 ggItems[ID].Live := false;
742 ggItems[ID].RespawnTime := ITEM_RESPAWNTIME;
743 end;
746 procedure g_Items_Remove (ID: DWORD);
747 var
748 it: PItem;
749 trig: Integer;
750 {$IF DEFINED(D2F_DEBUG)}
751 //x, y: Integer;
752 {$ENDIF}
753 begin
754 if not g_ItemValidId(ID) then raise Exception.Create('g_Items_Remove: invalid item id');
756 it := @ggItems[ID];
757 if (it.arrIdx <> ID) then raise Exception.Create('g_Items_Remove: arrIdx desync');
759 {$IF DEFINED(D2F_DEBUG)}
760 //itemTree.getNodeXY(it.treeNode, x, y);
761 //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);
762 {$ENDIF}
764 trig := it.SpawnTrigger;
766 releaseItem(ID);
768 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
769 end;
772 procedure g_Items_SaveState (var Mem: TBinMemoryWriter);
773 var
774 count, i: Integer;
775 sig: DWORD;
776 begin
777 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
778 count := 0;
779 if (ggItems <> nil) then
780 begin
781 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) then Inc(count);
782 end;
784 Mem := TBinMemoryWriter.Create((count+1) * 60);
786 // Êîëè÷åñòâî ïðåäìåòîâ
787 Mem.WriteInt(count);
789 if (count = 0) then exit;
791 for i := 0 to High(ggItems) do
792 begin
793 if (ggItems[i].ItemType <> ITEM_NONE) then
794 begin
795 // Ñèãíàòóðà ïðåäìåòà
796 sig := ITEM_SIGNATURE; // 'ITEM'
797 Mem.WriteDWORD(sig);
798 // Òèï ïðåäìåòà
799 Mem.WriteByte(ggItems[i].ItemType);
800 // Åñòü ëè ðåñïàóí
801 Mem.WriteBoolean(ggItems[i].Respawnable);
802 // Êîîðäèíàòû ðåñïóíà
803 Mem.WriteInt(ggItems[i].InitX);
804 Mem.WriteInt(ggItems[i].InitY);
805 // Âðåìÿ äî ðåñïàóíà
806 Mem.WriteWord(ggItems[i].RespawnTime);
807 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
808 Mem.WriteBoolean(ggItems[i].Live);
809 // Ìîæåò ëè îí ïàäàòü
810 Mem.WriteBoolean(ggItems[i].Fall);
811 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
812 Mem.WriteInt(ggItems[i].SpawnTrigger);
813 // Îáúåêò ïðåäìåòà
814 Obj_SaveState(@ggItems[i].Obj, Mem);
815 end;
816 end;
817 end;
820 procedure g_Items_LoadState (var Mem: TBinMemoryReader);
821 var
822 count, i, a: Integer;
823 sig: DWORD;
824 b: Byte;
825 begin
826 if (Mem = nil) then exit;
828 g_Items_Free();
830 // Êîëè÷åñòâî ïðåäìåòîâ
831 Mem.ReadInt(count);
833 if (count = 0) then Exit;
835 for a := 0 to count-1 do
836 begin
837 // Ñèãíàòóðà ïðåäìåòà
838 Mem.ReadDWORD(sig);
839 if (sig <> ITEM_SIGNATURE) then raise EBinSizeError.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
840 // Òèï ïðåäìåòà
841 Mem.ReadByte(b);
842 // Ñîçäàåì ïðåäìåò
843 i := g_Items_Create(0, 0, b, False, False);
844 // Åñòü ëè ðåñïàóí
845 Mem.ReadBoolean(ggItems[i].Respawnable);
846 // Êîîðäèíàòû ðåñïóíà
847 Mem.ReadInt(ggItems[i].InitX);
848 Mem.ReadInt(ggItems[i].InitY);
849 // Âðåìÿ äî ðåñïàóíà
850 Mem.ReadWord(ggItems[i].RespawnTime);
851 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
852 Mem.ReadBoolean(ggItems[i].Live);
853 // Ìîæåò ëè îí ïàäàòü
854 Mem.ReadBoolean(ggItems[i].Fall);
855 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
856 Mem.ReadInt(ggItems[i].SpawnTrigger);
857 // Îáúåêò ïðåäìåòà
858 Obj_LoadState(@ggItems[i].Obj, Mem);
859 end;
860 end;
863 procedure g_Items_RestartRound ();
864 var
865 i: Integer;
866 it: PItem;
867 begin
868 for i := 0 to High(ggItems) do
869 begin
870 it := @ggItems[i];
871 if it.Respawnable then
872 begin
873 it.QuietRespawn := True;
874 it.RespawnTime := 0;
875 end
876 else
877 begin
878 g_Items_Remove(i);
879 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
880 end;
881 end;
882 end;
885 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
886 var
887 idx: Integer;
888 begin
889 result := false;
890 if (ggItems = nil) or not assigned(cb) then exit;
892 if backwards then
893 begin
894 for idx := High(ggItems) downto 0 do
895 begin
896 if ggItems[idx].Live then
897 begin
898 result := cb(@ggItems[idx]);
899 if result then exit;
900 end;
901 end;
902 end
903 else
904 begin
905 for idx := 0 to High(ggItems) do
906 begin
907 if ggItems[idx].Live then
908 begin
909 result := cb(@ggItems[idx]);
910 if result then exit;
911 end;
912 end;
913 end;
914 end;
917 // ////////////////////////////////////////////////////////////////////////// //
918 procedure g_Item_EmitPickupSound (idx: Integer);
919 var
920 it: PItem;
921 begin
922 if not g_ItemValidId(idx) then exit;
923 it := @ggItems[idx];
924 g_Item_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
925 end;
927 procedure g_Item_EmitPickupSoundAt (idx, x, y: Integer);
928 var
929 it: PItem;
930 begin
931 if not g_ItemValidId(idx) then exit;
933 it := @ggItems[idx];
934 if gSoundEffectsDF then
935 begin
936 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
937 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
938 begin
939 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
940 end
941 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
942 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
943 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
944 ITEM_AMMO_BACKPACK] then
945 begin
946 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
947 end
948 else
949 begin
950 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
951 end;
952 end
953 else
954 begin
955 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
956 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
957 begin
958 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
959 end
960 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
961 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
962 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
963 begin
964 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
965 end
966 else
967 begin
968 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
969 end;
970 end;
971 end;
973 end.