DEADSOFTWARE

render monster drop after monsters, so monster corpses will not obscure ammo clips...
[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
32 public
33 ItemType: Byte;
34 Respawnable: Boolean;
35 InitX, InitY: Integer;
36 RespawnTime: Word;
37 alive: Boolean;
38 Fall: Boolean;
39 QuietRespawn: Boolean;
40 SpawnTrigger: Integer;
41 Obj: TObj;
42 Animation: TAnimation;
43 dropped: Boolean; // dropped from the monster? drops should be rendered after corpses, so zombie corpse will not obscure ammo container, for example
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_SetDrop (ID: DWORD);
57 procedure g_Items_Update();
58 procedure g_Items_Draw();
59 procedure g_Items_DrawDrop();
60 procedure g_Items_Pick(ID: DWORD);
61 procedure g_Items_Remove(ID: DWORD);
62 procedure g_Items_SaveState(var Mem: TBinMemoryWriter);
63 procedure g_Items_LoadState(var Mem: TBinMemoryReader);
65 procedure g_Items_RestartRound ();
67 function g_Items_ValidId (idx: Integer): Boolean; inline;
68 function g_Items_ByIdx (idx: Integer): PItem;
69 function g_Items_ObjByIdx (idx: Integer): PObj;
71 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
72 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
74 procedure g_Items_AddDynLights();
77 type
78 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
80 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
83 var
84 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
85 gMaxDist: Integer = 1; // for sounds
86 ITEM_RESPAWNTIME: Integer = 60 * 36;
88 implementation
90 uses
91 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
92 Math, g_game, g_triggers, g_console, SysUtils, g_player, g_net, g_netmsg,
93 e_log,
94 g_grid, binheap, idpool;
97 var
98 ggItems: Array of TItem = nil;
101 // ////////////////////////////////////////////////////////////////////////// //
102 var
103 freeIds: TIdPool = nil;
106 // ////////////////////////////////////////////////////////////////////////// //
107 function g_Items_ValidId (idx: Integer): Boolean; inline;
108 begin
109 result := false;
110 if (idx < 0) or (idx > High(ggItems)) then exit;
111 if not ggItems[idx].slotIsUsed then exit;
112 result := true;
113 end;
116 function g_Items_ByIdx (idx: Integer): PItem;
117 begin
118 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
119 result := @ggItems[idx];
120 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
121 end;
124 function g_Items_ObjByIdx (idx: Integer): PObj;
125 begin
126 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
127 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
128 result := @ggItems[idx].Obj;
129 end;
132 // ////////////////////////////////////////////////////////////////////////// //
133 procedure TItem.positionChanged ();
134 begin
135 end;
138 // ////////////////////////////////////////////////////////////////////////// //
139 const
140 ITEM_SIGNATURE = $4D455449; // 'ITEM'
142 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
143 (((14), (15)), // MEDKIT_SMALL
144 ((28), (19)), // MEDKIT_LARGE
145 ((28), (19)), // MEDKIT_BLACK
146 ((31), (16)), // ARMOR_GREEN
147 ((31), (16)), // ARMOR_BLUE
148 ((25), (25)), // SPHERE_BLUE
149 ((25), (25)), // SPHERE_WHITE
150 ((24), (47)), // SUIT
151 ((14), (27)), // OXYGEN
152 ((25), (25)), // INVUL
153 ((62), (24)), // WEAPON_SAW
154 ((63), (12)), // WEAPON_SHOTGUN1
155 ((54), (13)), // WEAPON_SHOTGUN2
156 ((54), (16)), // WEAPON_CHAINGUN
157 ((62), (16)), // WEAPON_ROCKETLAUNCHER
158 ((54), (16)), // WEAPON_PLASMA
159 ((61), (36)), // WEAPON_BFG
160 ((54), (16)), // WEAPON_SUPERPULEMET
161 (( 9), (11)), // AMMO_BULLETS
162 ((28), (16)), // AMMO_BULLETS_BOX
163 ((15), ( 7)), // AMMO_SHELLS
164 ((32), (12)), // AMMO_SHELLS_BOX
165 ((12), (27)), // AMMO_ROCKET
166 ((54), (21)), // AMMO_ROCKET_BOX
167 ((15), (12)), // AMMO_CELL
168 ((32), (21)), // AMMO_CELL_BIG
169 ((22), (29)), // AMMO_BACKPACK
170 ((16), (16)), // KEY_RED
171 ((16), (16)), // KEY_GREEN
172 ((16), (16)), // KEY_BLUE
173 (( 1), ( 1)), // WEAPON_KASTET
174 ((43), (16)), // WEAPON_PISTOL
175 ((14), (18)), // BOTTLE
176 ((16), (15)), // HELMET
177 ((32), (24)), // JETPACK
178 ((25), (25)), // INVIS
179 ((53), (20)), // WEAPON_FLAMETHROWER
180 ((13), (20))); // AMMO_FUELCAN
182 procedure InitTextures();
183 begin
184 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
185 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
186 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
187 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
188 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
189 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
190 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
191 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
192 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
193 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
194 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
195 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
196 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
197 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
198 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
199 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
200 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
201 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
202 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
203 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
204 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
205 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
206 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
207 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
208 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
209 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
210 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
211 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
212 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
213 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID[ITEM_JETPACK]);
214 end;
216 procedure g_Items_LoadData();
217 begin
218 e_WriteLog('Loading items data...', MSG_NOTIFY);
220 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
221 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
222 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
223 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
225 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
226 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
229 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
230 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
231 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
232 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
233 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
234 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
235 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
236 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
237 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
238 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
239 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
240 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
241 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
242 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
243 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
244 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
245 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
246 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
247 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
248 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
249 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
250 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
251 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
252 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
253 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
254 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
255 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
256 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
257 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
258 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
259 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
260 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
261 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
262 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
263 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
264 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
265 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
266 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
267 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK');
269 InitTextures();
271 freeIds := TIdPool.Create();
272 end;
275 procedure g_Items_FreeData();
276 begin
277 e_WriteLog('Releasing items data...', MSG_NOTIFY);
279 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
280 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
281 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
282 g_Sound_Delete('SOUND_ITEM_GETITEM');
284 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
285 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
286 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
287 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
288 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
289 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
290 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
291 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
292 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
293 g_Frames_DeleteByName('FRAMES_FLAG_RED');
294 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
295 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
296 g_Texture_Delete('ITEM_MEDKIT_SMALL');
297 g_Texture_Delete('ITEM_MEDKIT_LARGE');
298 g_Texture_Delete('ITEM_WEAPON_SAW');
299 g_Texture_Delete('ITEM_WEAPON_PISTOL');
300 g_Texture_Delete('ITEM_WEAPON_KASTET');
301 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
302 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
303 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
304 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
305 g_Texture_Delete('ITEM_WEAPON_PLASMA');
306 g_Texture_Delete('ITEM_WEAPON_BFG');
307 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
308 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
309 g_Texture_Delete('ITEM_AMMO_BULLETS');
310 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
311 g_Texture_Delete('ITEM_AMMO_SHELLS');
312 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
313 g_Texture_Delete('ITEM_AMMO_ROCKET');
314 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
315 g_Texture_Delete('ITEM_AMMO_CELL');
316 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
317 g_Texture_Delete('ITEM_AMMO_FUELCAN');
318 g_Texture_Delete('ITEM_AMMO_BACKPACK');
319 g_Texture_Delete('ITEM_KEY_RED');
320 g_Texture_Delete('ITEM_KEY_GREEN');
321 g_Texture_Delete('ITEM_KEY_BLUE');
322 g_Texture_Delete('ITEM_OXYGEN');
323 g_Texture_Delete('ITEM_SUIT');
324 g_Texture_Delete('ITEM_WEAPON_KASTET');
325 g_Texture_Delete('ITEM_MEDKIT_BLACK');
326 g_Texture_Delete('ITEM_JETPACK');
328 freeIds.Free();
329 freeIds := nil;
330 end;
333 procedure releaseItem (idx: Integer);
334 var
335 it: PItem;
336 begin
337 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
338 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
339 it := @ggItems[idx];
340 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
341 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
342 it.slotIsUsed := false;
343 if (it.Animation <> nil) then
344 begin
345 it.Animation.Free();
346 it.Animation := nil;
347 end;
348 it.alive := False;
349 it.SpawnTrigger := -1;
350 it.ItemType := ITEM_NONE;
351 freeIds.release(LongWord(idx));
352 end;
355 procedure growItemArrayTo (newsz: Integer);
356 var
357 i, olen: Integer;
358 it: PItem;
359 begin
360 if (newsz < Length(ggItems)) then exit;
361 // no free slots
362 olen := Length(ggItems);
363 SetLength(ggItems, newsz);
364 for i := olen to High(ggItems) do
365 begin
366 it := @ggItems[i];
367 it.slotIsUsed := false;
368 it.arrIdx := i;
369 it.ItemType := ITEM_NONE;
370 it.Animation := nil;
371 it.alive := false;
372 it.SpawnTrigger := -1;
373 it.Respawnable := false;
374 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
375 end;
376 end;
379 function allocItem (): DWORD;
380 begin
381 result := freeIds.alloc();
382 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
383 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
384 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
385 end;
388 // it will be slow if the slot is free (we have to rebuild the heap)
389 function wantItemSlot (slot: Integer): Integer;
390 var
391 olen: Integer;
392 it: PItem;
393 begin
394 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
395 // do we need to grow item storate?
396 olen := Length(ggItems);
397 if (slot >= olen) then growItemArrayTo(slot+64);
399 it := @ggItems[slot];
400 if not it.slotIsUsed then
401 begin
402 freeIds.alloc(LongWord(slot));
403 end
404 else
405 begin
406 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
407 end;
408 it.slotIsUsed := false;
410 result := slot;
411 end;
414 // ////////////////////////////////////////////////////////////////////////// //
415 procedure g_Items_Init ();
416 var
417 a, b: Integer;
418 begin
419 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
420 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
421 gMaxDist := Trunc(Hypot(a, b));
422 end;
425 procedure g_Items_Free ();
426 var
427 i: Integer;
428 begin
429 if (ggItems <> nil) then
430 begin
431 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
432 ggItems := nil;
433 end;
434 freeIds.clear();
435 end;
438 function g_Items_Create (X, Y: Integer; ItemType: Byte;
439 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
440 var
441 find_id: DWORD;
442 ID: DWORD;
443 it: PItem;
444 begin
445 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
447 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
449 it := @ggItems[find_id];
451 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
452 //it.arrIdx := find_id;
453 it.slotIsUsed := true;
455 it.ItemType := ItemType;
456 it.Respawnable := Respawnable;
457 if g_Game_IsServer and (ITEM_RESPAWNTIME = 0) then it.Respawnable := False;
458 it.InitX := X;
459 it.InitY := Y;
460 it.RespawnTime := 0;
461 it.Fall := Fall;
462 it.alive := True;
463 it.QuietRespawn := False;
464 it.dropped := false;
466 g_Obj_Init(@it.Obj);
467 it.Obj.X := X;
468 it.Obj.Y := Y;
469 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
470 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
472 it.Animation := nil;
473 it.SpawnTrigger := -1;
475 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
476 if AdjCoord then
477 begin
478 with it^ do
479 begin
480 Obj.X := X - (Obj.Rect.Width div 2);
481 Obj.Y := Y - Obj.Rect.Height;
482 InitX := Obj.X;
483 InitY := Obj.Y;
484 end;
485 end;
487 // Óñòàíîâêà àíèìàöèè
488 case it.ItemType of
489 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
490 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
491 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
492 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
493 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
494 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
495 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
496 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
497 end;
499 it.positionChanged();
501 result := find_id;
502 end;
505 procedure g_Items_Update ();
506 var
507 i, j, k: Integer;
508 ID: DWord;
509 Anim: TAnimation;
510 m: Word;
511 r, nxt: Boolean;
512 begin
513 if (ggItems = nil) then exit;
515 for i := 0 to High(ggItems) do
516 begin
517 if (ggItems[i].ItemType = ITEM_NONE) then continue;
519 with ggItems[i] do
520 begin
521 nxt := False;
523 if alive then
524 begin
525 if Fall then
526 begin
527 m := g_Obj_Move(@Obj, True, True);
528 positionChanged(); // this updates spatial accelerators
530 // Ñîïðîòèâëåíèå âîçäóõà
531 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
533 // Åñëè âûïàë çà êàðòó
534 if WordBool(m and MOVE_FALLOUT) then
535 begin
536 if SpawnTrigger = -1 then
537 begin
538 g_Items_Pick(i);
539 end
540 else
541 begin
542 g_Items_Remove(i);
543 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
544 end;
545 continue;
546 end;
547 end;
549 // Åñëè èãðîêè ïîáëèçîñòè
550 if (gPlayers <> nil) then
551 begin
552 j := Random(Length(gPlayers))-1;
554 for k := 0 to High(gPlayers) do
555 begin
556 Inc(j);
557 if j > High(gPlayers) then j := 0;
559 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
560 begin
561 if g_Game_IsClient then continue;
563 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
565 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
568 Doom 2D: Original:
569 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
570 +2. I_MEGA,I_INVL,I_SUPER
571 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
573 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
575 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
576 if r then
577 begin
578 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
579 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
580 nxt := True;
581 break;
582 end;
583 end;
584 end;
585 end;
587 if nxt then continue;
588 end;
590 if Respawnable and g_Game_IsServer then
591 begin
592 DecMin(RespawnTime, 0);
593 if (RespawnTime = 0) and (not alive) then
594 begin
595 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
597 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
598 begin
599 Anim := TAnimation.Create(ID, False, 4);
600 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
601 Anim.Free();
602 end;
604 Obj.X := InitX;
605 Obj.Y := InitY;
606 Obj.Vel.X := 0;
607 Obj.Vel.Y := 0;
608 Obj.Accel.X := 0;
609 Obj.Accel.Y := 0;
610 positionChanged(); // this updates spatial accelerators
612 alive := true;
614 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
615 QuietRespawn := false;
616 end;
617 end;
619 if (Animation <> nil) then Animation.Update();
620 end;
621 end;
622 end;
625 procedure itemsDrawInternal (dropflag: Boolean);
626 var
627 i: Integer;
628 it: PItem;
629 begin
630 if (ggItems = nil) then exit;
632 for i := 0 to High(ggItems) do
633 begin
634 it := @ggItems[i];
635 if not it.alive then continue;
636 if (it.dropped <> dropflag) then continue;
638 with it^ do
639 begin
640 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
641 begin
642 if (Animation = nil) then
643 begin
644 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
645 end
646 else
647 begin
648 Animation.Draw(Obj.X, Obj.Y, M_NONE);
649 end;
651 if g_debug_Frames then
652 begin
653 e_DrawQuad(Obj.X+Obj.Rect.X,
654 Obj.Y+Obj.Rect.Y,
655 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
656 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
657 0, 255, 0);
658 end;
659 end;
660 end;
661 end;
662 end;
665 procedure g_Items_Draw ();
666 begin
667 itemsDrawInternal(false);
668 end;
670 procedure g_Items_DrawDrop ();
671 begin
672 itemsDrawInternal(true);
673 end;
676 procedure g_Items_SetDrop (ID: DWORD);
677 begin
678 if (ID < Length(ggItems)) then
679 begin
680 ggItems[ID].dropped := true;
681 end;
682 end;
685 procedure g_Items_Pick (ID: DWORD);
686 begin
687 if (ID < Length(ggItems)) then
688 begin
689 ggItems[ID].alive := false;
690 ggItems[ID].RespawnTime := ITEM_RESPAWNTIME;
691 end;
692 end;
695 procedure g_Items_Remove (ID: DWORD);
696 var
697 it: PItem;
698 trig: Integer;
699 begin
700 if not g_Items_ValidId(ID) then raise Exception.Create('g_Items_Remove: invalid item id');
702 it := @ggItems[ID];
703 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
705 trig := it.SpawnTrigger;
707 releaseItem(ID);
709 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
710 end;
713 procedure g_Items_SaveState (var Mem: TBinMemoryWriter);
714 var
715 count, i: Integer;
716 sig: DWORD;
717 tt: Byte;
718 begin
719 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
720 count := 0;
721 if (ggItems <> nil) then
722 begin
723 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) then Inc(count);
724 end;
726 Mem := TBinMemoryWriter.Create((count+1) * 60);
728 // Êîëè÷åñòâî ïðåäìåòîâ
729 Mem.WriteInt(count);
731 if (count = 0) then exit;
733 for i := 0 to High(ggItems) do
734 begin
735 if (ggItems[i].ItemType <> ITEM_NONE) then
736 begin
737 // Ñèãíàòóðà ïðåäìåòà
738 sig := ITEM_SIGNATURE; // 'ITEM'
739 Mem.WriteDWORD(sig);
740 // Òèï ïðåäìåòà
741 tt := ggItems[i].ItemType;
742 if ggItems[i].dropped then tt := tt or $80;
743 Mem.WriteByte(tt);
744 // Åñòü ëè ðåñïàóí
745 Mem.WriteBoolean(ggItems[i].Respawnable);
746 // Êîîðäèíàòû ðåñïóíà
747 Mem.WriteInt(ggItems[i].InitX);
748 Mem.WriteInt(ggItems[i].InitY);
749 // Âðåìÿ äî ðåñïàóíà
750 Mem.WriteWord(ggItems[i].RespawnTime);
751 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
752 Mem.WriteBoolean(ggItems[i].alive);
753 // Ìîæåò ëè îí ïàäàòü
754 Mem.WriteBoolean(ggItems[i].Fall);
755 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
756 Mem.WriteInt(ggItems[i].SpawnTrigger);
757 // Îáúåêò ïðåäìåòà
758 Obj_SaveState(@ggItems[i].Obj, Mem);
759 end;
760 end;
761 end;
764 procedure g_Items_LoadState (var Mem: TBinMemoryReader);
765 var
766 count, i, a: Integer;
767 sig: DWORD;
768 b: Byte;
769 begin
770 if (Mem = nil) then exit;
772 g_Items_Free();
774 // Êîëè÷åñòâî ïðåäìåòîâ
775 Mem.ReadInt(count);
777 if (count = 0) then Exit;
779 for a := 0 to count-1 do
780 begin
781 // Ñèãíàòóðà ïðåäìåòà
782 Mem.ReadDWORD(sig);
783 if (sig <> ITEM_SIGNATURE) then raise EBinSizeError.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
784 // Òèï ïðåäìåòà
785 Mem.ReadByte(b); // bit7=1: monster drop
786 // Ñîçäàåì ïðåäìåò
787 i := g_Items_Create(0, 0, b and $7F, False, False);
788 if ((b and $80) <> 0) then g_Items_SetDrop(i);
789 // Åñòü ëè ðåñïàóí
790 Mem.ReadBoolean(ggItems[i].Respawnable);
791 // Êîîðäèíàòû ðåñïóíà
792 Mem.ReadInt(ggItems[i].InitX);
793 Mem.ReadInt(ggItems[i].InitY);
794 // Âðåìÿ äî ðåñïàóíà
795 Mem.ReadWord(ggItems[i].RespawnTime);
796 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
797 Mem.ReadBoolean(ggItems[i].alive);
798 // Ìîæåò ëè îí ïàäàòü
799 Mem.ReadBoolean(ggItems[i].Fall);
800 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
801 Mem.ReadInt(ggItems[i].SpawnTrigger);
802 // Îáúåêò ïðåäìåòà
803 Obj_LoadState(@ggItems[i].Obj, Mem);
804 end;
805 end;
808 procedure g_Items_RestartRound ();
809 var
810 i: Integer;
811 it: PItem;
812 begin
813 for i := 0 to High(ggItems) do
814 begin
815 it := @ggItems[i];
816 if it.Respawnable then
817 begin
818 it.QuietRespawn := True;
819 it.RespawnTime := 0;
820 end
821 else
822 begin
823 g_Items_Remove(i);
824 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
825 end;
826 end;
827 end;
830 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
831 var
832 idx: Integer;
833 begin
834 result := false;
835 if (ggItems = nil) or not assigned(cb) then exit;
837 if backwards then
838 begin
839 for idx := High(ggItems) downto 0 do
840 begin
841 if ggItems[idx].alive then
842 begin
843 result := cb(@ggItems[idx]);
844 if result then exit;
845 end;
846 end;
847 end
848 else
849 begin
850 for idx := 0 to High(ggItems) do
851 begin
852 if ggItems[idx].alive then
853 begin
854 result := cb(@ggItems[idx]);
855 if result then exit;
856 end;
857 end;
858 end;
859 end;
862 // ////////////////////////////////////////////////////////////////////////// //
863 procedure g_Items_EmitPickupSound (idx: Integer);
864 var
865 it: PItem;
866 begin
867 if not g_Items_ValidId(idx) then exit;
868 it := @ggItems[idx];
869 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
870 end;
872 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
873 var
874 it: PItem;
875 begin
876 if not g_Items_ValidId(idx) then exit;
878 it := @ggItems[idx];
879 if gSoundEffectsDF then
880 begin
881 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
882 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
883 begin
884 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
885 end
886 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
887 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
888 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
889 ITEM_AMMO_BACKPACK] then
890 begin
891 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
892 end
893 else
894 begin
895 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
896 end;
897 end
898 else
899 begin
900 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
901 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
902 begin
903 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
904 end
905 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
906 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
907 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
908 begin
909 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
910 end
911 else
912 begin
913 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
914 end;
915 end;
916 end;
919 procedure g_Items_AddDynLights();
920 var
921 f: Integer;
922 it: PItem;
923 begin
924 for f := 0 to High(ggItems) do
925 begin
926 it := @ggItems[f];
927 if not it.alive then continue;
928 case it.ItemType of
929 ITEM_KEY_RED: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 24, 1.0, 0.0, 0.0, 0.6);
930 ITEM_KEY_GREEN: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 24, 0.0, 1.0, 0.0, 0.6);
931 ITEM_KEY_BLUE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 24, 0.0, 0.0, 1.0, 0.6);
932 ITEM_ARMOR_GREEN: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 42, 0.0, 1.0, 0.0, 0.6);
933 ITEM_ARMOR_BLUE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 42, 0.0, 0.0, 1.0, 0.6);
934 ITEM_SPHERE_BLUE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 0.0, 1.0, 0.0, 0.6);
935 ITEM_SPHERE_WHITE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 1.0, 1.0, 1.0, 0.6);
936 ITEM_INVUL: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 1.0, 0.0, 0.0, 0.6);
937 ITEM_INVIS: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 32, 1.0, 1.0, 0.0, 0.6);
938 ITEM_BOTTLE: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 16, 0.0, 0.0, 0.8, 0.6);
939 ITEM_HELMET: g_AddDynLight(it.Obj.X+(it.Obj.Rect.Width div 2), it.Obj.Y+(it.Obj.Rect.Height div 2), 16, 0.0, 0.8, 0.0, 0.6);
940 end;
941 end;
942 end;
945 end.