DEADSOFTWARE

item list fixes
[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 slotIsUsed: Boolean;
30 arrIdx: Integer; // in ggItems
31 nextFree: Integer; // in item array
33 public
34 ItemType: Byte;
35 Respawnable: Boolean;
36 InitX, InitY: Integer;
37 RespawnTime: Word;
38 Live: Boolean;
39 Fall: Boolean;
40 QuietRespawn: Boolean;
41 SpawnTrigger: Integer;
42 Obj: TObj;
43 Animation: TAnimation;
45 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
47 property myid: Integer read arrIdx;
48 end;
50 procedure g_Items_LoadData();
51 procedure g_Items_FreeData();
52 procedure g_Items_Init();
53 procedure g_Items_Free();
54 function g_Items_Create(X, Y: Integer; ItemType: Byte;
55 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
56 procedure g_Items_Update();
57 procedure g_Items_Draw();
58 procedure g_Items_Pick(ID: DWORD);
59 procedure g_Items_Remove(ID: DWORD);
60 procedure g_Items_SaveState(var Mem: TBinMemoryWriter);
61 procedure g_Items_LoadState(var Mem: TBinMemoryReader);
63 procedure g_Items_RestartRound ();
65 function g_Items_ValidId (idx: Integer): Boolean; inline;
66 function g_Items_ByIdx (idx: Integer): PItem;
67 function g_Items_ObjByIdx (idx: Integer): PObj;
69 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
70 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
73 type
74 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
76 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
79 var
80 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
81 gMaxDist: Integer = 1; // for sounds
82 ITEM_RESPAWNTIME: Integer = 60 * 36;
84 implementation
86 uses
87 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
88 Math, g_game, g_triggers, g_console, SysUtils, g_player, g_net, g_netmsg,
89 e_log,
90 g_grid, binheap;
93 var
94 ggItems: Array of TItem = nil;
97 // ////////////////////////////////////////////////////////////////////////// //
98 var
99 //freeIds: TBinaryHeapInt = nil; // free item ids
100 freeListHead: Integer = -1;
103 // ////////////////////////////////////////////////////////////////////////// //
104 function g_Items_ValidId (idx: Integer): Boolean; inline;
105 begin
106 result := false;
107 if (idx < 0) or (idx > High(ggItems)) then exit;
108 if not ggItems[idx].slotIsUsed then exit;
109 result := true;
110 end;
113 function g_Items_ByIdx (idx: Integer): PItem;
114 begin
115 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
116 result := @ggItems[idx];
117 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
118 end;
121 function g_Items_ObjByIdx (idx: Integer): PObj;
122 begin
123 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
124 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
125 result := @ggItems[idx].Obj;
126 end;
129 // ////////////////////////////////////////////////////////////////////////// //
130 procedure TItem.positionChanged ();
131 begin
132 end;
135 // ////////////////////////////////////////////////////////////////////////// //
136 const
137 ITEM_SIGNATURE = $4D455449; // 'ITEM'
139 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
140 (((14), (15)), // MEDKIT_SMALL
141 ((28), (19)), // MEDKIT_LARGE
142 ((28), (19)), // MEDKIT_BLACK
143 ((31), (16)), // ARMOR_GREEN
144 ((31), (16)), // ARMOR_BLUE
145 ((25), (25)), // SPHERE_BLUE
146 ((25), (25)), // SPHERE_WHITE
147 ((24), (47)), // SUIT
148 ((14), (27)), // OXYGEN
149 ((25), (25)), // INVUL
150 ((62), (24)), // WEAPON_SAW
151 ((63), (12)), // WEAPON_SHOTGUN1
152 ((54), (13)), // WEAPON_SHOTGUN2
153 ((54), (16)), // WEAPON_CHAINGUN
154 ((62), (16)), // WEAPON_ROCKETLAUNCHER
155 ((54), (16)), // WEAPON_PLASMA
156 ((61), (36)), // WEAPON_BFG
157 ((54), (16)), // WEAPON_SUPERPULEMET
158 (( 9), (11)), // AMMO_BULLETS
159 ((28), (16)), // AMMO_BULLETS_BOX
160 ((15), ( 7)), // AMMO_SHELLS
161 ((32), (12)), // AMMO_SHELLS_BOX
162 ((12), (27)), // AMMO_ROCKET
163 ((54), (21)), // AMMO_ROCKET_BOX
164 ((15), (12)), // AMMO_CELL
165 ((32), (21)), // AMMO_CELL_BIG
166 ((22), (29)), // AMMO_BACKPACK
167 ((16), (16)), // KEY_RED
168 ((16), (16)), // KEY_GREEN
169 ((16), (16)), // KEY_BLUE
170 (( 1), ( 1)), // WEAPON_KASTET
171 ((43), (16)), // WEAPON_PISTOL
172 ((14), (18)), // BOTTLE
173 ((16), (15)), // HELMET
174 ((32), (24)), // JETPACK
175 ((25), (25)), // INVIS
176 ((53), (20)), // WEAPON_FLAMETHROWER
177 ((13), (20))); // AMMO_FUELCAN
179 procedure InitTextures();
180 begin
181 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
182 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
183 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
184 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
185 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
186 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
187 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
188 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
189 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
190 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
191 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
192 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
193 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
194 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
195 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
196 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
197 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
198 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
199 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
200 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
201 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
202 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
203 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
204 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
205 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
206 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
207 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
208 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
209 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
210 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID[ITEM_JETPACK]);
211 end;
213 procedure g_Items_LoadData();
214 begin
215 e_WriteLog('Loading items data...', MSG_NOTIFY);
217 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
218 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
219 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
220 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
222 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
223 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
224 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
225 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
226 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
229 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
230 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
231 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
232 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
233 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
234 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
235 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
236 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
237 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
238 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
239 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
240 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
241 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
242 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
243 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
244 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
245 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
246 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
247 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
248 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
249 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
250 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
251 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
252 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
253 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
254 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
255 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
256 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
257 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
258 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
259 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
260 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
261 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
262 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
263 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
264 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK');
266 InitTextures();
268 //freeIds := binHeapNewIntLess();
269 end;
272 procedure g_Items_FreeData();
273 begin
274 e_WriteLog('Releasing items data...', MSG_NOTIFY);
276 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
277 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
278 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
279 g_Sound_Delete('SOUND_ITEM_GETITEM');
281 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
282 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
283 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
284 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
285 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
286 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
287 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
288 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
289 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
290 g_Frames_DeleteByName('FRAMES_FLAG_RED');
291 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
292 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
293 g_Texture_Delete('ITEM_MEDKIT_SMALL');
294 g_Texture_Delete('ITEM_MEDKIT_LARGE');
295 g_Texture_Delete('ITEM_WEAPON_SAW');
296 g_Texture_Delete('ITEM_WEAPON_PISTOL');
297 g_Texture_Delete('ITEM_WEAPON_KASTET');
298 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
299 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
300 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
301 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
302 g_Texture_Delete('ITEM_WEAPON_PLASMA');
303 g_Texture_Delete('ITEM_WEAPON_BFG');
304 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
305 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
306 g_Texture_Delete('ITEM_AMMO_BULLETS');
307 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
308 g_Texture_Delete('ITEM_AMMO_SHELLS');
309 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
310 g_Texture_Delete('ITEM_AMMO_ROCKET');
311 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
312 g_Texture_Delete('ITEM_AMMO_CELL');
313 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
314 g_Texture_Delete('ITEM_AMMO_FUELCAN');
315 g_Texture_Delete('ITEM_AMMO_BACKPACK');
316 g_Texture_Delete('ITEM_KEY_RED');
317 g_Texture_Delete('ITEM_KEY_GREEN');
318 g_Texture_Delete('ITEM_KEY_BLUE');
319 g_Texture_Delete('ITEM_OXYGEN');
320 g_Texture_Delete('ITEM_SUIT');
321 g_Texture_Delete('ITEM_WEAPON_KASTET');
322 g_Texture_Delete('ITEM_MEDKIT_BLACK');
323 g_Texture_Delete('ITEM_JETPACK');
325 //freeIds.Free();
326 end;
329 procedure releaseItem (idx: Integer);
330 var
331 it: PItem;
332 begin
333 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
334 it := @ggItems[idx];
335 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item');
336 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
337 it.slotIsUsed := false;
338 if (it.Animation <> nil) then
339 begin
340 it.Animation.Free();
341 it.Animation := nil;
342 end;
343 it.Live := False;
344 it.SpawnTrigger := -1;
345 it.ItemType := ITEM_NONE;
346 //freeIds.insert(it.arrIdx);
347 it.nextFree := freeListHead;
348 freeListHead := idx;
349 end;
352 procedure rebuildFreeList (reservedIdx: Integer=-1);
353 var
354 i, lfi: Integer;
355 it: PItem;
356 begin
357 // rebuild free list
358 freeListHead := -1;
359 lfi := -1;
360 for i := 0 to High(ggItems) do
361 begin
362 it := @ggItems[i];
363 if (i <> reservedIdx) and (not it.slotIsUsed) then
364 begin
365 if (lfi = -1) then freeListHead := i else ggItems[lfi].nextFree := lfi;
366 lfi := i;
367 end;
368 end;
369 if (lfi <> -1) then ggItems[lfi].nextFree := -1;
370 end;
373 procedure growItemArrayTo (newsz: Integer; rebuildList: Boolean=true);
374 var
375 i, olen: Integer;
376 it: PItem;
377 begin
378 if (newsz < Length(ggItems)) then exit;
379 // no free slots
380 olen := Length(ggItems);
381 SetLength(ggItems, newsz);
382 for i := olen to High(ggItems) do
383 begin
384 it := @ggItems[i];
385 it.slotIsUsed := false;
386 it.arrIdx := i;
387 it.nextFree := i+1;
388 it.ItemType := ITEM_NONE;
389 it.Animation := nil;
390 it.Live := false;
391 it.SpawnTrigger := -1;
392 it.Respawnable := false;
393 //freeIds.insert(i);
394 end;
395 if rebuildList then rebuildFreeList();
396 end;
399 function allocItem (): DWORD;
400 begin
401 if (freeListHead = -1) then
402 begin
403 growItemArrayTo(Length(ggItems)+64);
404 if (freeListHead = -1) then raise Exception.Create('internal error in item manager');
405 end;
407 result := DWORD(freeListHead);
408 freeListHead := ggItems[freeListHead].nextFree;
410 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
411 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
412 end;
415 // it will be slow if the slot is free (we have to rebuild the heap)
416 function wantItemSlot (slot: Integer): Integer;
417 var
418 olen: Integer;
419 it: PItem;
420 rebuildList: Boolean = false;
421 begin
422 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
423 // do we need to grow item storate?
424 olen := Length(ggItems);
425 if (slot >= olen) then
426 begin
427 growItemArrayTo(slot+64, false);
428 rebuildList := true;
429 end;
431 it := @ggItems[slot];
432 if rebuildList or (not it.slotIsUsed) then
433 begin
434 rebuildFreeList(slot);
435 end;
436 it.slotIsUsed := false;
438 result := slot;
439 end;
442 // ////////////////////////////////////////////////////////////////////////// //
443 procedure g_Items_Init ();
444 var
445 a, b: Integer;
446 begin
447 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
448 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
449 gMaxDist := Trunc(Hypot(a, b));
450 end;
453 procedure g_Items_Free ();
454 var
455 i: Integer;
456 begin
457 if (ggItems <> nil) then
458 begin
459 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
460 ggItems := nil;
461 end;
462 //freeIds.clear();
463 freeListHead := -1;
464 end;
467 function g_Items_Create (X, Y: Integer; ItemType: Byte;
468 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
469 var
470 find_id: DWORD;
471 ID: DWORD;
472 it: PItem;
473 begin
474 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
476 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
478 it := @ggItems[find_id];
480 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
481 //it.arrIdx := find_id;
482 it.slotIsUsed := true;
484 it.ItemType := ItemType;
485 it.Respawnable := Respawnable;
486 if g_Game_IsServer and (ITEM_RESPAWNTIME = 0) then it.Respawnable := False;
487 it.InitX := X;
488 it.InitY := Y;
489 it.RespawnTime := 0;
490 it.Fall := Fall;
491 it.Live := True;
492 it.QuietRespawn := False;
494 g_Obj_Init(@it.Obj);
495 it.Obj.X := X;
496 it.Obj.Y := Y;
497 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
498 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
500 it.Animation := nil;
501 it.SpawnTrigger := -1;
503 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
504 if AdjCoord then
505 begin
506 with it^ do
507 begin
508 Obj.X := X - (Obj.Rect.Width div 2);
509 Obj.Y := Y - Obj.Rect.Height;
510 InitX := Obj.X;
511 InitY := Obj.Y;
512 end;
513 end;
515 // Óñòàíîâêà àíèìàöèè
516 case it.ItemType of
517 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
518 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
519 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
520 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
521 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
522 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
523 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
524 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
525 end;
527 it.positionChanged();
529 result := find_id;
530 end;
533 procedure g_Items_Update ();
534 var
535 i, j, k: Integer;
536 ID: DWord;
537 Anim: TAnimation;
538 m: Word;
539 r, nxt: Boolean;
540 begin
541 if (ggItems = nil) then exit;
543 for i := 0 to High(ggItems) do
544 begin
545 if (ggItems[i].ItemType = ITEM_NONE) then continue;
547 with ggItems[i] do
548 begin
549 nxt := False;
551 if Live then
552 begin
553 if Fall then
554 begin
555 m := g_Obj_Move(@Obj, True, True);
556 positionChanged(); // this updates spatial accelerators
558 // Ñîïðîòèâëåíèå âîçäóõà
559 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
561 // Åñëè âûïàë çà êàðòó
562 if WordBool(m and MOVE_FALLOUT) then
563 begin
564 if SpawnTrigger = -1 then
565 begin
566 g_Items_Pick(i);
567 end
568 else
569 begin
570 g_Items_Remove(i);
571 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
572 end;
573 continue;
574 end;
575 end;
577 // Åñëè èãðîêè ïîáëèçîñòè
578 if (gPlayers <> nil) then
579 begin
580 j := Random(Length(gPlayers))-1;
582 for k := 0 to High(gPlayers) do
583 begin
584 Inc(j);
585 if j > High(gPlayers) then j := 0;
587 if (gPlayers[j] <> nil) and gPlayers[j].Live and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
588 begin
589 if g_Game_IsClient then continue;
591 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
593 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
596 Doom 2D: Original:
597 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
598 +2. I_MEGA,I_INVL,I_SUPER
599 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
601 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
603 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
604 if r then
605 begin
606 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
607 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
608 nxt := True;
609 break;
610 end;
611 end;
612 end;
613 end;
615 if nxt then continue;
616 end;
618 if Respawnable and g_Game_IsServer then
619 begin
620 DecMin(RespawnTime, 0);
621 if (RespawnTime = 0) and (not Live) then
622 begin
623 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
625 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
626 begin
627 Anim := TAnimation.Create(ID, False, 4);
628 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
629 Anim.Free();
630 end;
632 Obj.X := InitX;
633 Obj.Y := InitY;
634 Obj.Vel.X := 0;
635 Obj.Vel.Y := 0;
636 Obj.Accel.X := 0;
637 Obj.Accel.Y := 0;
638 positionChanged(); // this updates spatial accelerators
640 Live := true;
642 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
643 QuietRespawn := false;
644 end;
645 end;
647 if (Animation <> nil) then Animation.Update();
648 end;
649 end;
650 end;
653 procedure g_Items_Draw ();
654 var
655 i: Integer;
656 begin
657 if (ggItems = nil) then exit;
659 for i := 0 to High(ggItems) do
660 begin
661 if not ggItems[i].Live then continue;
663 with ggItems[i] do
664 begin
665 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
666 begin
667 if (Animation = nil) then
668 begin
669 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
670 end
671 else
672 begin
673 Animation.Draw(Obj.X, Obj.Y, M_NONE);
674 end;
676 if g_debug_Frames then
677 begin
678 e_DrawQuad(Obj.X+Obj.Rect.X,
679 Obj.Y+Obj.Rect.Y,
680 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
681 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
682 0, 255, 0);
683 end;
684 end;
685 end;
686 end;
687 end;
690 procedure g_Items_Pick (ID: DWORD);
691 begin
692 ggItems[ID].Live := false;
693 ggItems[ID].RespawnTime := ITEM_RESPAWNTIME;
694 end;
697 procedure g_Items_Remove (ID: DWORD);
698 var
699 it: PItem;
700 trig: Integer;
701 begin
702 if not g_Items_ValidId(ID) then raise Exception.Create('g_Items_Remove: invalid item id');
704 it := @ggItems[ID];
705 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
707 trig := it.SpawnTrigger;
709 releaseItem(ID);
711 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
712 end;
715 procedure g_Items_SaveState (var Mem: TBinMemoryWriter);
716 var
717 count, i: Integer;
718 sig: DWORD;
719 begin
720 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
721 count := 0;
722 if (ggItems <> nil) then
723 begin
724 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) then Inc(count);
725 end;
727 Mem := TBinMemoryWriter.Create((count+1) * 60);
729 // Êîëè÷åñòâî ïðåäìåòîâ
730 Mem.WriteInt(count);
732 if (count = 0) then exit;
734 for i := 0 to High(ggItems) do
735 begin
736 if (ggItems[i].ItemType <> ITEM_NONE) then
737 begin
738 // Ñèãíàòóðà ïðåäìåòà
739 sig := ITEM_SIGNATURE; // 'ITEM'
740 Mem.WriteDWORD(sig);
741 // Òèï ïðåäìåòà
742 Mem.WriteByte(ggItems[i].ItemType);
743 // Åñòü ëè ðåñïàóí
744 Mem.WriteBoolean(ggItems[i].Respawnable);
745 // Êîîðäèíàòû ðåñïóíà
746 Mem.WriteInt(ggItems[i].InitX);
747 Mem.WriteInt(ggItems[i].InitY);
748 // Âðåìÿ äî ðåñïàóíà
749 Mem.WriteWord(ggItems[i].RespawnTime);
750 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
751 Mem.WriteBoolean(ggItems[i].Live);
752 // Ìîæåò ëè îí ïàäàòü
753 Mem.WriteBoolean(ggItems[i].Fall);
754 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
755 Mem.WriteInt(ggItems[i].SpawnTrigger);
756 // Îáúåêò ïðåäìåòà
757 Obj_SaveState(@ggItems[i].Obj, Mem);
758 end;
759 end;
760 end;
763 procedure g_Items_LoadState (var Mem: TBinMemoryReader);
764 var
765 count, i, a: Integer;
766 sig: DWORD;
767 b: Byte;
768 begin
769 if (Mem = nil) then exit;
771 g_Items_Free();
773 // Êîëè÷åñòâî ïðåäìåòîâ
774 Mem.ReadInt(count);
776 if (count = 0) then Exit;
778 for a := 0 to count-1 do
779 begin
780 // Ñèãíàòóðà ïðåäìåòà
781 Mem.ReadDWORD(sig);
782 if (sig <> ITEM_SIGNATURE) then raise EBinSizeError.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
783 // Òèï ïðåäìåòà
784 Mem.ReadByte(b);
785 // Ñîçäàåì ïðåäìåò
786 i := g_Items_Create(0, 0, b, False, False);
787 // Åñòü ëè ðåñïàóí
788 Mem.ReadBoolean(ggItems[i].Respawnable);
789 // Êîîðäèíàòû ðåñïóíà
790 Mem.ReadInt(ggItems[i].InitX);
791 Mem.ReadInt(ggItems[i].InitY);
792 // Âðåìÿ äî ðåñïàóíà
793 Mem.ReadWord(ggItems[i].RespawnTime);
794 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
795 Mem.ReadBoolean(ggItems[i].Live);
796 // Ìîæåò ëè îí ïàäàòü
797 Mem.ReadBoolean(ggItems[i].Fall);
798 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
799 Mem.ReadInt(ggItems[i].SpawnTrigger);
800 // Îáúåêò ïðåäìåòà
801 Obj_LoadState(@ggItems[i].Obj, Mem);
802 end;
803 end;
806 procedure g_Items_RestartRound ();
807 var
808 i: Integer;
809 it: PItem;
810 begin
811 for i := 0 to High(ggItems) do
812 begin
813 it := @ggItems[i];
814 if it.Respawnable then
815 begin
816 it.QuietRespawn := True;
817 it.RespawnTime := 0;
818 end
819 else
820 begin
821 g_Items_Remove(i);
822 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
823 end;
824 end;
825 end;
828 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
829 var
830 idx: Integer;
831 begin
832 result := false;
833 if (ggItems = nil) or not assigned(cb) then exit;
835 if backwards then
836 begin
837 for idx := High(ggItems) downto 0 do
838 begin
839 if ggItems[idx].Live then
840 begin
841 result := cb(@ggItems[idx]);
842 if result then exit;
843 end;
844 end;
845 end
846 else
847 begin
848 for idx := 0 to High(ggItems) do
849 begin
850 if ggItems[idx].Live then
851 begin
852 result := cb(@ggItems[idx]);
853 if result then exit;
854 end;
855 end;
856 end;
857 end;
860 // ////////////////////////////////////////////////////////////////////////// //
861 procedure g_Items_EmitPickupSound (idx: Integer);
862 var
863 it: PItem;
864 begin
865 if not g_Items_ValidId(idx) then exit;
866 it := @ggItems[idx];
867 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
868 end;
870 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
871 var
872 it: PItem;
873 begin
874 if not g_Items_ValidId(idx) then exit;
876 it := @ggItems[idx];
877 if gSoundEffectsDF then
878 begin
879 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
880 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
881 begin
882 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
883 end
884 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
885 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
886 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
887 ITEM_AMMO_BACKPACK] then
888 begin
889 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
890 end
891 else
892 begin
893 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
894 end;
895 end
896 else
897 begin
898 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
899 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
900 begin
901 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
902 end
903 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
904 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
905 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
906 begin
907 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
908 end
909 else
910 begin
911 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
912 end;
913 end;
914 end;
916 end.