DEADSOFTWARE

converted grid and tree to generics (fuck you, FPC! your generics fuckin' sux fuckin...
[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 type
97 TDynAABBTreeItemBase = specialize TDynAABBTreeBase<Integer>;
99 TDynAABBTreeItem = class(TDynAABBTreeItemBase)
100 function getFleshAABB (out aabb: AABB2D; flesh: Integer; tag: Integer): Boolean; override;
101 end;
103 function TDynAABBTreeItem.getFleshAABB (out aabb: AABB2D; flesh: Integer; tag: Integer): Boolean;
104 var
105 it: PItem;
106 begin
107 result := false;
108 if (flesh < 0) or (flesh > High(ggItems)) then raise Exception.Create('DynTree: trying to get dimensions of inexistant item');
109 it := @ggItems[flesh];
110 if (it.Obj.Rect.Width < 1) or (it.Obj.Rect.Height < 1) then exit;
111 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);
112 if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
113 result := true;
114 end;
117 // ////////////////////////////////////////////////////////////////////////// //
118 var
119 itemTree: TDynAABBTreeItem = nil;
120 freeIds: TBinaryHeapInt = nil; // free item ids
123 // ////////////////////////////////////////////////////////////////////////// //
124 function g_ItemValidId (idx: Integer): Boolean; inline;
125 begin
126 result := false;
127 if (idx < 0) or (idx > High(ggItems)) then exit;
128 if (ggItems[idx].treeNode = -1) then exit;
129 result := true;
130 end;
133 function g_ItemByIdx (idx: Integer): PItem;
134 begin
135 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
136 result := @ggItems[idx];
137 if (result.treeNode = -1) then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
138 end;
141 function g_ItemObjByIdx (idx: Integer): PObj;
142 begin
143 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
144 if (ggItems[idx].treeNode = -1) then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
145 result := @ggItems[idx].Obj;
146 end;
149 // ////////////////////////////////////////////////////////////////////////// //
150 procedure TItem.positionChanged ();
151 var
152 x, y: Integer;
153 begin
154 if (treeNode = -1) then
155 begin
156 treeNode := itemTree.insertObject(arrIdx, 0, true); // static object
157 itemTree.getNodeXY(treeNode, x, y);
158 {$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 end
160 else
161 begin
162 itemTree.getNodeXY(treeNode, x, y);
163 if (Obj.X = x) and (Obj.Y = y) then exit; // nothing to do
164 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('item #%d: updating tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);{$ENDIF}
166 {$IFDEF TRUE}
167 itemTree.updateObject(treeNode);
168 {$ELSE}
169 itemTree.removeObject(treeNode);
170 treeNode := itemTree.insertObject(arrIdx, 0, true); // static object
171 {$ENDIF}
173 itemTree.getNodeXY(treeNode, x, y);
174 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('item #%d: updated tree; nodeid=%d; x=%d; y=%d', [arrIdx, treeNode, x, y]), MSG_NOTIFY);{$ENDIF}
175 end;
176 end;
179 // ////////////////////////////////////////////////////////////////////////// //
180 const
181 ITEM_SIGNATURE = $4D455449; // 'ITEM'
183 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
184 (((14), (15)), // MEDKIT_SMALL
185 ((28), (19)), // MEDKIT_LARGE
186 ((28), (19)), // MEDKIT_BLACK
187 ((31), (16)), // ARMOR_GREEN
188 ((31), (16)), // ARMOR_BLUE
189 ((25), (25)), // SPHERE_BLUE
190 ((25), (25)), // SPHERE_WHITE
191 ((24), (47)), // SUIT
192 ((14), (27)), // OXYGEN
193 ((25), (25)), // INVUL
194 ((62), (24)), // WEAPON_SAW
195 ((63), (12)), // WEAPON_SHOTGUN1
196 ((54), (13)), // WEAPON_SHOTGUN2
197 ((54), (16)), // WEAPON_CHAINGUN
198 ((62), (16)), // WEAPON_ROCKETLAUNCHER
199 ((54), (16)), // WEAPON_PLASMA
200 ((61), (36)), // WEAPON_BFG
201 ((54), (16)), // WEAPON_SUPERPULEMET
202 (( 9), (11)), // AMMO_BULLETS
203 ((28), (16)), // AMMO_BULLETS_BOX
204 ((15), ( 7)), // AMMO_SHELLS
205 ((32), (12)), // AMMO_SHELLS_BOX
206 ((12), (27)), // AMMO_ROCKET
207 ((54), (21)), // AMMO_ROCKET_BOX
208 ((15), (12)), // AMMO_CELL
209 ((32), (21)), // AMMO_CELL_BIG
210 ((22), (29)), // AMMO_BACKPACK
211 ((16), (16)), // KEY_RED
212 ((16), (16)), // KEY_GREEN
213 ((16), (16)), // KEY_BLUE
214 (( 1), ( 1)), // WEAPON_KASTET
215 ((43), (16)), // WEAPON_PISTOL
216 ((14), (18)), // BOTTLE
217 ((16), (15)), // HELMET
218 ((32), (24)), // JETPACK
219 ((25), (25)), // INVIS
220 ((53), (20)), // WEAPON_FLAMETHROWER
221 ((13), (20))); // AMMO_FUELCAN
223 procedure InitTextures();
224 begin
225 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
226 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
227 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
228 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
229 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
230 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
231 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
232 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
233 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
234 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
235 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
236 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
237 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
238 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
239 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
240 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
241 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
242 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
243 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
244 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
245 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
246 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
247 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
248 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
249 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
250 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
251 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
252 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
253 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
254 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID[ITEM_JETPACK]);
255 end;
257 procedure g_Items_LoadData();
258 begin
259 e_WriteLog('Loading items data...', MSG_NOTIFY);
261 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
262 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
263 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
264 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
266 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
267 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
268 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
269 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
270 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
271 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
272 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
273 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
274 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
275 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
276 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
277 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
278 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
279 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
280 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
281 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
282 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
283 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
284 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
285 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
286 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
287 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
288 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
289 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
290 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
291 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
292 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
293 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
294 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
295 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
296 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
297 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
298 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
299 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
300 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
301 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
302 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
303 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
304 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
305 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
306 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
307 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
308 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK');
310 InitTextures();
312 itemTree := TDynAABBTreeItem.Create();
313 freeIds := binHeapNewIntLess();
314 end;
317 procedure g_Items_FreeData();
318 begin
319 e_WriteLog('Releasing items data...', MSG_NOTIFY);
321 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
322 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
323 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
324 g_Sound_Delete('SOUND_ITEM_GETITEM');
326 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
327 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
328 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
329 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
330 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
331 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
332 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
333 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
334 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
335 g_Frames_DeleteByName('FRAMES_FLAG_RED');
336 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
337 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
338 g_Texture_Delete('ITEM_MEDKIT_SMALL');
339 g_Texture_Delete('ITEM_MEDKIT_LARGE');
340 g_Texture_Delete('ITEM_WEAPON_SAW');
341 g_Texture_Delete('ITEM_WEAPON_PISTOL');
342 g_Texture_Delete('ITEM_WEAPON_KASTET');
343 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
344 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
345 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
346 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
347 g_Texture_Delete('ITEM_WEAPON_PLASMA');
348 g_Texture_Delete('ITEM_WEAPON_BFG');
349 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
350 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
351 g_Texture_Delete('ITEM_AMMO_BULLETS');
352 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
353 g_Texture_Delete('ITEM_AMMO_SHELLS');
354 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
355 g_Texture_Delete('ITEM_AMMO_ROCKET');
356 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
357 g_Texture_Delete('ITEM_AMMO_CELL');
358 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
359 g_Texture_Delete('ITEM_AMMO_FUELCAN');
360 g_Texture_Delete('ITEM_AMMO_BACKPACK');
361 g_Texture_Delete('ITEM_KEY_RED');
362 g_Texture_Delete('ITEM_KEY_GREEN');
363 g_Texture_Delete('ITEM_KEY_BLUE');
364 g_Texture_Delete('ITEM_OXYGEN');
365 g_Texture_Delete('ITEM_SUIT');
366 g_Texture_Delete('ITEM_WEAPON_KASTET');
367 g_Texture_Delete('ITEM_MEDKIT_BLACK');
368 g_Texture_Delete('ITEM_JETPACK');
370 itemTree.Free();
371 freeIds.Free();
372 end;
375 procedure releaseItem (idx: Integer);
376 var
377 it: PItem;
378 begin
379 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
380 it := @ggItems[idx];
381 if (it.treeNode = -1) then raise Exception.Create('releaseItem: trying to release unallocated item');
382 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
383 itemTree.removeObject(it.treeNode);
384 it.treeNode := -1;
385 if (it.Animation <> nil) then
386 begin
387 it.Animation.Free();
388 it.Animation := nil;
389 end;
390 it.Live := False;
391 it.SpawnTrigger := -1;
392 it.ItemType := ITEM_NONE;
393 freeIds.insert(it.arrIdx);
394 end;
397 function allocItem (): DWORD;
398 var
399 i, olen: Integer;
400 it: PItem;
401 begin
402 if (freeIds.count = 0) then
403 begin
404 // no free slots
405 olen := Length(ggItems);
406 SetLength(ggItems, olen+64);
407 for i := olen to High(ggItems) do
408 begin
409 it := @ggItems[i];
410 it.treeNode := -1;
411 it.arrIdx := i;
412 it.ItemType := ITEM_NONE;
413 it.Animation := nil;
414 it.Live := false;
415 it.SpawnTrigger := -1;
416 it.Respawnable := false;
417 freeIds.insert(i);
418 end;
419 end;
421 result := freeIds.front;
422 freeIds.popFront();
424 if (result > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
425 if (ggItems[result].arrIdx <> result) then raise Exception.Create('allocItem: arrIdx inconsistency');
426 end;
429 // it will be slow if the slot is free (we have to rebuild the heap)
430 function wantItemSlot (slot: Integer): Integer;
431 var
432 i, olen: Integer;
433 it: PItem;
434 rebuildFreeList: Boolean = true;
435 begin
436 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
437 // do we need to grow item storate?
438 olen := Length(ggItems);
439 if (slot >= olen) then
440 begin
441 // need more spice!
442 SetLength(ggItems, slot+64);
443 // add free slots to free list
444 for i := olen to High(ggItems) do
445 begin
446 it := @ggItems[i];
447 it.treeNode := -1;
448 it.arrIdx := i;
449 it.ItemType := ITEM_NONE;
450 it.Animation := nil;
451 it.Live := false;
452 it.SpawnTrigger := -1;
453 it.Respawnable := false;
454 if (i <> slot) then freeIds.insert(i);
455 end;
456 rebuildFreeList := false;
457 end;
459 it := @ggItems[slot];
460 if (it.treeNode = -1) then
461 begin
462 // this is unused slot; get it, and rebuild id list
463 if rebuildFreeList then
464 begin
465 freeIds.clear();
466 for i := 0 to High(ggItems) do
467 begin
468 if (i <> slot) and (ggItems[i].treeNode = -1) then freeIds.insert(i);
469 end;
470 end;
471 end
472 else
473 begin
474 // it will be readded
475 itemTree.removeObject(it.treeNode);
476 it.treeNode := -1;
477 end;
479 result := slot;
480 end;
483 // ////////////////////////////////////////////////////////////////////////// //
484 procedure g_Items_Init ();
485 var
486 a, b: Integer;
487 begin
488 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
489 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
490 gMaxDist := Trunc(Hypot(a, b));
491 end;
494 procedure g_Items_Free ();
495 var
496 i: Integer;
497 begin
498 if (ggItems <> nil) then
499 begin
500 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
501 ggItems := nil;
502 end;
503 if (itemTree <> nil) then itemTree.reset();
504 freeIds.clear();
505 end;
508 function g_Items_Create (X, Y: Integer; ItemType: Byte;
509 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
510 var
511 find_id: DWORD;
512 ID: DWORD;
513 it: PItem;
514 begin
515 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
517 {$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
519 it := @ggItems[find_id];
521 it.ItemType := ItemType;
522 it.Respawnable := Respawnable;
523 if g_Game_IsServer and (ITEM_RESPAWNTIME = 0) then it.Respawnable := False;
524 it.InitX := X;
525 it.InitY := Y;
526 it.RespawnTime := 0;
527 it.Fall := Fall;
528 it.Live := True;
529 it.QuietRespawn := False;
531 if (it.treeNode <> -1) then raise Exception.Create('g_Items_Create: trying to reuse already allocated item');
532 if (it.arrIdx <> find_id) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
533 //it.treeNode := -1;
534 //it.arrIdx := find_id;
536 g_Obj_Init(@it.Obj);
537 it.Obj.X := X;
538 it.Obj.Y := Y;
539 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
540 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
542 it.Animation := nil;
543 it.SpawnTrigger := -1;
545 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
546 if AdjCoord then
547 begin
548 with it^ do
549 begin
550 Obj.X := X - (Obj.Rect.Width div 2);
551 Obj.Y := Y - Obj.Rect.Height;
552 InitX := Obj.X;
553 InitY := Obj.Y;
554 end;
555 end;
557 // Óñòàíîâêà àíèìàöèè
558 case it.ItemType of
559 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
560 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
561 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
562 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
563 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
564 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
565 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
566 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
567 end;
569 it.positionChanged();
571 result := find_id;
572 end;
575 procedure g_Items_Update ();
576 var
577 i, j, k: Integer;
578 ID: DWord;
579 Anim: TAnimation;
580 m: Word;
581 r, nxt: Boolean;
582 begin
583 if (ggItems = nil) then exit;
585 for i := 0 to High(ggItems) do
586 begin
587 if (ggItems[i].ItemType = ITEM_NONE) then continue;
589 with ggItems[i] do
590 begin
591 nxt := False;
593 if Live then
594 begin
595 if Fall then
596 begin
597 m := g_Obj_Move(@Obj, True, True);
598 positionChanged(); // this updates spatial accelerators
600 // Ñîïðîòèâëåíèå âîçäóõà
601 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
603 // Åñëè âûïàë çà êàðòó
604 if WordBool(m and MOVE_FALLOUT) then
605 begin
606 if SpawnTrigger = -1 then
607 begin
608 g_Items_Pick(i);
609 end
610 else
611 begin
612 g_Items_Remove(i);
613 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
614 end;
615 continue;
616 end;
617 end;
619 // Åñëè èãðîêè ïîáëèçîñòè
620 if (gPlayers <> nil) then
621 begin
622 j := Random(Length(gPlayers))-1;
624 for k := 0 to High(gPlayers) do
625 begin
626 Inc(j);
627 if j > High(gPlayers) then j := 0;
629 if (gPlayers[j] <> nil) and gPlayers[j].Live and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
630 begin
631 if g_Game_IsClient then continue;
633 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
635 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
638 Doom 2D: Original:
639 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
640 +2. I_MEGA,I_INVL,I_SUPER
641 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
643 g_Item_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
645 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
646 if r then
647 begin
648 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
649 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
650 nxt := True;
651 break;
652 end;
653 end;
654 end;
655 end;
657 if nxt then continue;
658 end;
660 if Respawnable and g_Game_IsServer then
661 begin
662 DecMin(RespawnTime, 0);
663 if (RespawnTime = 0) and (not Live) then
664 begin
665 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
667 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
668 begin
669 Anim := TAnimation.Create(ID, False, 4);
670 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
671 Anim.Free();
672 end;
674 Obj.X := InitX;
675 Obj.Y := InitY;
676 Obj.Vel.X := 0;
677 Obj.Vel.Y := 0;
678 Obj.Accel.X := 0;
679 Obj.Accel.Y := 0;
680 positionChanged(); // this updates spatial accelerators
682 Live := true;
684 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
685 QuietRespawn := false;
686 end;
687 end;
689 if (Animation <> nil) then Animation.Update();
690 end;
691 end;
692 end;
695 procedure g_Items_Draw ();
696 var
697 i: Integer;
698 begin
699 if (ggItems = nil) then exit;
701 for i := 0 to High(ggItems) do
702 begin
703 if not ggItems[i].Live then continue;
705 with ggItems[i] do
706 begin
707 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
708 begin
709 if (Animation = nil) then
710 begin
711 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
712 end
713 else
714 begin
715 Animation.Draw(Obj.X, Obj.Y, M_NONE);
716 end;
718 if g_debug_Frames then
719 begin
720 e_DrawQuad(Obj.X+Obj.Rect.X,
721 Obj.Y+Obj.Rect.Y,
722 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
723 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
724 0, 255, 0);
725 end;
726 end;
727 end;
728 end;
729 end;
732 procedure g_Items_Pick (ID: DWORD);
733 begin
734 ggItems[ID].Live := false;
735 ggItems[ID].RespawnTime := ITEM_RESPAWNTIME;
736 end;
739 procedure g_Items_Remove (ID: DWORD);
740 var
741 it: PItem;
742 trig: Integer;
743 x, y: Integer;
744 begin
745 if not g_ItemValidId(ID) then raise Exception.Create('g_Items_Remove: invalid item id');
747 it := @ggItems[ID];
748 if (it.arrIdx <> ID) then raise Exception.Create('g_Items_Remove: arrIdx desync');
750 itemTree.getNodeXY(it.treeNode, x, y);
751 {$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}
753 trig := it.SpawnTrigger;
755 releaseItem(ID);
757 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
758 end;
761 procedure g_Items_SaveState (var Mem: TBinMemoryWriter);
762 var
763 count, i: Integer;
764 sig: DWORD;
765 begin
766 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
767 count := 0;
768 if (ggItems <> nil) then
769 begin
770 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) then Inc(count);
771 end;
773 Mem := TBinMemoryWriter.Create((count+1) * 60);
775 // Êîëè÷åñòâî ïðåäìåòîâ
776 Mem.WriteInt(count);
778 if (count = 0) then exit;
780 for i := 0 to High(ggItems) do
781 begin
782 if (ggItems[i].ItemType <> ITEM_NONE) then
783 begin
784 // Ñèãíàòóðà ïðåäìåòà
785 sig := ITEM_SIGNATURE; // 'ITEM'
786 Mem.WriteDWORD(sig);
787 // Òèï ïðåäìåòà
788 Mem.WriteByte(ggItems[i].ItemType);
789 // Åñòü ëè ðåñïàóí
790 Mem.WriteBoolean(ggItems[i].Respawnable);
791 // Êîîðäèíàòû ðåñïóíà
792 Mem.WriteInt(ggItems[i].InitX);
793 Mem.WriteInt(ggItems[i].InitY);
794 // Âðåìÿ äî ðåñïàóíà
795 Mem.WriteWord(ggItems[i].RespawnTime);
796 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
797 Mem.WriteBoolean(ggItems[i].Live);
798 // Ìîæåò ëè îí ïàäàòü
799 Mem.WriteBoolean(ggItems[i].Fall);
800 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
801 Mem.WriteInt(ggItems[i].SpawnTrigger);
802 // Îáúåêò ïðåäìåòà
803 Obj_SaveState(@ggItems[i].Obj, Mem);
804 end;
805 end;
806 end;
809 procedure g_Items_LoadState (var Mem: TBinMemoryReader);
810 var
811 count, i, a: Integer;
812 sig: DWORD;
813 b: Byte;
814 begin
815 if (Mem = nil) then exit;
817 g_Items_Free();
819 // Êîëè÷åñòâî ïðåäìåòîâ
820 Mem.ReadInt(count);
822 if (count = 0) then Exit;
824 for a := 0 to count-1 do
825 begin
826 // Ñèãíàòóðà ïðåäìåòà
827 Mem.ReadDWORD(sig);
828 if (sig <> ITEM_SIGNATURE) then raise EBinSizeError.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
829 // Òèï ïðåäìåòà
830 Mem.ReadByte(b);
831 // Ñîçäàåì ïðåäìåò
832 i := g_Items_Create(0, 0, b, False, False);
833 // Åñòü ëè ðåñïàóí
834 Mem.ReadBoolean(ggItems[i].Respawnable);
835 // Êîîðäèíàòû ðåñïóíà
836 Mem.ReadInt(ggItems[i].InitX);
837 Mem.ReadInt(ggItems[i].InitY);
838 // Âðåìÿ äî ðåñïàóíà
839 Mem.ReadWord(ggItems[i].RespawnTime);
840 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
841 Mem.ReadBoolean(ggItems[i].Live);
842 // Ìîæåò ëè îí ïàäàòü
843 Mem.ReadBoolean(ggItems[i].Fall);
844 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
845 Mem.ReadInt(ggItems[i].SpawnTrigger);
846 // Îáúåêò ïðåäìåòà
847 Obj_LoadState(@ggItems[i].Obj, Mem);
848 end;
849 end;
852 procedure g_Items_RestartRound ();
853 var
854 i: Integer;
855 it: PItem;
856 begin
857 for i := 0 to High(ggItems) do
858 begin
859 it := @ggItems[i];
860 if it.Respawnable then
861 begin
862 it.QuietRespawn := True;
863 it.RespawnTime := 0;
864 end
865 else
866 begin
867 g_Items_Remove(i);
868 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
869 end;
870 end;
871 end;
874 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
875 var
876 idx: Integer;
877 begin
878 result := false;
879 if (ggItems = nil) or not assigned(cb) then exit;
881 if backwards then
882 begin
883 for idx := High(ggItems) downto 0 do
884 begin
885 if ggItems[idx].Live then
886 begin
887 result := cb(@ggItems[idx]);
888 if result then exit;
889 end;
890 end;
891 end
892 else
893 begin
894 for idx := 0 to High(ggItems) 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 end;
906 // ////////////////////////////////////////////////////////////////////////// //
907 procedure g_Item_EmitPickupSound (idx: Integer);
908 var
909 it: PItem;
910 begin
911 if not g_ItemValidId(idx) then exit;
912 it := @ggItems[idx];
913 g_Item_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
914 end;
916 procedure g_Item_EmitPickupSoundAt (idx, x, y: Integer);
917 var
918 it: PItem;
919 begin
920 if not g_ItemValidId(idx) then exit;
922 it := @ggItems[idx];
923 if gSoundEffectsDF then
924 begin
925 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
926 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
927 begin
928 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
929 end
930 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
931 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
932 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
933 ITEM_AMMO_BACKPACK] then
934 begin
935 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
936 end
937 else
938 begin
939 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
940 end;
941 end
942 else
943 begin
944 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
945 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
946 begin
947 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
948 end
949 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
950 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
951 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
952 begin
953 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
954 end
955 else
956 begin
957 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
958 end;
959 end;
960 end;
962 end.