DEADSOFTWARE

02c8b5fc86dc50e62410e335f7fa0eb61aafea48
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_items;
18 interface
20 uses
21 SysUtils, Classes,
22 MAPDEF, g_textures, g_phys, g_saveload;
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 used: Boolean read slotIsUsed;
48 property myid: Integer read arrIdx;
49 end;
51 procedure g_Items_LoadData();
52 procedure g_Items_FreeData();
53 procedure g_Items_Init();
54 procedure g_Items_Free();
55 function g_Items_Create(X, Y: Integer; ItemType: Byte;
56 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
57 procedure g_Items_SetDrop (ID: DWORD);
58 procedure g_Items_PreUpdate();
59 procedure g_Items_Update();
60 procedure g_Items_Pick(ID: DWORD);
61 procedure g_Items_Remove(ID: DWORD);
62 procedure g_Items_SaveState (st: TStream);
63 procedure g_Items_LoadState (st: TStream);
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
87 var (* private state *)
88 ggItems: Array of TItem = nil;
90 implementation
92 uses
93 Math,
94 g_basic, g_sound, g_main, g_gfx, g_map,
95 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
96 e_log,
97 g_grid, binheap, idpool, utils, xstreams;
99 // ////////////////////////////////////////////////////////////////////////// //
100 var
101 freeIds: TIdPool = nil;
104 // ////////////////////////////////////////////////////////////////////////// //
105 function g_Items_ValidId (idx: Integer): Boolean; inline;
106 begin
107 result := false;
108 if (idx < 0) or (idx > High(ggItems)) then exit;
109 if not ggItems[idx].slotIsUsed then exit;
110 result := true;
111 end;
114 function g_Items_ByIdx (idx: Integer): PItem;
115 begin
116 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
117 result := @ggItems[idx];
118 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
119 end;
122 function g_Items_ObjByIdx (idx: Integer): PObj;
123 begin
124 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
125 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
126 result := @ggItems[idx].Obj;
127 end;
130 // ////////////////////////////////////////////////////////////////////////// //
131 procedure TItem.positionChanged ();
132 begin
133 end;
136 // ////////////////////////////////////////////////////////////////////////// //
137 const
138 ITEM_SIGNATURE = $4D455449; // 'ITEM'
140 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
141 (((14), (15)), // MEDKIT_SMALL
142 ((28), (19)), // MEDKIT_LARGE
143 ((28), (19)), // MEDKIT_BLACK
144 ((31), (16)), // ARMOR_GREEN
145 ((31), (16)), // ARMOR_BLUE
146 ((25), (25)), // SPHERE_BLUE
147 ((25), (25)), // SPHERE_WHITE
148 ((24), (47)), // SUIT
149 ((14), (27)), // OXYGEN
150 ((25), (25)), // INVUL
151 ((62), (24)), // WEAPON_SAW
152 ((63), (12)), // WEAPON_SHOTGUN1
153 ((54), (13)), // WEAPON_SHOTGUN2
154 ((54), (16)), // WEAPON_CHAINGUN
155 ((62), (16)), // WEAPON_ROCKETLAUNCHER
156 ((54), (16)), // WEAPON_PLASMA
157 ((61), (36)), // WEAPON_BFG
158 ((54), (16)), // WEAPON_SUPERPULEMET
159 (( 9), (11)), // AMMO_BULLETS
160 ((28), (16)), // AMMO_BULLETS_BOX
161 ((15), ( 7)), // AMMO_SHELLS
162 ((32), (12)), // AMMO_SHELLS_BOX
163 ((12), (27)), // AMMO_ROCKET
164 ((54), (21)), // AMMO_ROCKET_BOX
165 ((15), (12)), // AMMO_CELL
166 ((32), (21)), // AMMO_CELL_BIG
167 ((22), (29)), // AMMO_BACKPACK
168 ((16), (16)), // KEY_RED
169 ((16), (16)), // KEY_GREEN
170 ((16), (16)), // KEY_BLUE
171 (( 1), ( 1)), // WEAPON_KASTET
172 ((43), (16)), // WEAPON_PISTOL
173 ((14), (18)), // BOTTLE
174 ((16), (15)), // HELMET
175 ((32), (24)), // JETPACK
176 ((25), (25)), // INVIS
177 ((53), (20)), // WEAPON_FLAMETHROWER
178 ((13), (20))); // AMMO_FUELCAN
180 procedure InitTextures();
181 begin
182 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
183 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
184 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
185 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
186 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
187 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
188 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
189 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
190 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
191 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
192 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
193 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
194 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
195 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
196 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
197 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
198 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
199 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
200 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
201 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
202 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
203 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
204 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
205 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
206 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
207 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
208 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
209 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
210 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
211 end;
213 procedure g_Items_LoadData();
214 begin
215 e_WriteLog('Loading items data...', TMsgType.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_JETPACK', GameWAD+':TEXTURES\JETPACK', 32, 32, 3, True);
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
229 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
230 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
231 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
232 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
233 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
234 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
235 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
236 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
237 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
238 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
239 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
240 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
241 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
242 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
243 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
244 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
245 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
246 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
247 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
248 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
249 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
250 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
251 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
252 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
253 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
254 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
255 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
256 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
257 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
258 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
259 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
260 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
261 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
262 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
263 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
264 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
266 InitTextures();
268 freeIds := TIdPool.Create();
269 end;
272 procedure g_Items_FreeData();
273 begin
274 e_WriteLog('Releasing items data...', TMsgType.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_JETPACK');
286 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
287 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
288 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
289 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
290 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
291 g_Frames_DeleteByName('FRAMES_FLAG_RED');
292 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
293 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
294 g_Texture_Delete('ITEM_MEDKIT_SMALL');
295 g_Texture_Delete('ITEM_MEDKIT_LARGE');
296 g_Texture_Delete('ITEM_WEAPON_SAW');
297 g_Texture_Delete('ITEM_WEAPON_PISTOL');
298 g_Texture_Delete('ITEM_WEAPON_KASTET');
299 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
300 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
301 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
302 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
303 g_Texture_Delete('ITEM_WEAPON_PLASMA');
304 g_Texture_Delete('ITEM_WEAPON_BFG');
305 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
306 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
307 g_Texture_Delete('ITEM_AMMO_BULLETS');
308 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
309 g_Texture_Delete('ITEM_AMMO_SHELLS');
310 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
311 g_Texture_Delete('ITEM_AMMO_ROCKET');
312 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
313 g_Texture_Delete('ITEM_AMMO_CELL');
314 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
315 g_Texture_Delete('ITEM_AMMO_FUELCAN');
316 g_Texture_Delete('ITEM_AMMO_BACKPACK');
317 g_Texture_Delete('ITEM_KEY_RED');
318 g_Texture_Delete('ITEM_KEY_GREEN');
319 g_Texture_Delete('ITEM_KEY_BLUE');
320 g_Texture_Delete('ITEM_OXYGEN');
321 g_Texture_Delete('ITEM_SUIT');
322 g_Texture_Delete('ITEM_WEAPON_KASTET');
323 g_Texture_Delete('ITEM_MEDKIT_BLACK');
325 freeIds.Free();
326 freeIds := nil;
327 end;
330 procedure releaseItem (idx: Integer);
331 var
332 it: PItem;
333 begin
334 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
335 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
336 it := @ggItems[idx];
337 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
338 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
339 it.slotIsUsed := false;
340 if (it.Animation <> nil) then
341 begin
342 it.Animation.Free();
343 it.Animation := nil;
344 end;
345 it.alive := False;
346 it.SpawnTrigger := -1;
347 it.ItemType := ITEM_NONE;
348 freeIds.release(LongWord(idx));
349 end;
352 procedure growItemArrayTo (newsz: Integer);
353 var
354 i, olen: Integer;
355 it: PItem;
356 begin
357 if (newsz < Length(ggItems)) then exit;
358 // no free slots
359 olen := Length(ggItems);
360 SetLength(ggItems, newsz);
361 for i := olen to High(ggItems) do
362 begin
363 it := @ggItems[i];
364 it.slotIsUsed := false;
365 it.arrIdx := i;
366 it.ItemType := ITEM_NONE;
367 it.Animation := nil;
368 it.alive := false;
369 it.SpawnTrigger := -1;
370 it.Respawnable := false;
371 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
372 end;
373 end;
376 function allocItem (): DWORD;
377 begin
378 result := freeIds.alloc();
379 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
380 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
381 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
382 end;
385 // it will be slow if the slot is free (we have to rebuild the heap)
386 function wantItemSlot (slot: Integer): Integer;
387 var
388 olen: Integer;
389 it: PItem;
390 begin
391 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
392 // do we need to grow item storate?
393 olen := Length(ggItems);
394 if (slot >= olen) then growItemArrayTo(slot+64);
396 it := @ggItems[slot];
397 if not it.slotIsUsed then
398 begin
399 freeIds.alloc(LongWord(slot));
400 end
401 else
402 begin
403 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
404 end;
405 it.slotIsUsed := false;
407 result := slot;
408 end;
411 // ////////////////////////////////////////////////////////////////////////// //
412 procedure g_Items_Init ();
413 var
414 a, b: Integer;
415 begin
416 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
417 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
418 gMaxDist := Trunc(Hypot(a, b));
419 end;
422 procedure g_Items_Free ();
423 var
424 i: Integer;
425 begin
426 if (ggItems <> nil) then
427 begin
428 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
429 ggItems := nil;
430 end;
431 freeIds.clear();
432 end;
435 function g_Items_Create (X, Y: Integer; ItemType: Byte;
436 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
437 var
438 find_id: DWORD;
439 ID: DWORD;
440 it: PItem;
441 begin
442 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
444 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
446 it := @ggItems[find_id];
448 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
449 //it.arrIdx := find_id;
450 it.slotIsUsed := true;
452 it.ItemType := ItemType;
453 it.Respawnable := Respawnable;
454 it.InitX := X;
455 it.InitY := Y;
456 it.RespawnTime := 0;
457 it.Fall := Fall;
458 it.alive := True;
459 it.QuietRespawn := False;
460 it.dropped := false;
462 g_Obj_Init(@it.Obj);
463 it.Obj.X := X;
464 it.Obj.Y := Y;
465 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
466 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
468 it.Animation := nil;
469 it.SpawnTrigger := -1;
471 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
472 if AdjCoord then
473 begin
474 with it^ do
475 begin
476 Obj.X := X - (Obj.Rect.Width div 2);
477 Obj.Y := Y - Obj.Rect.Height;
478 InitX := Obj.X;
479 InitY := Obj.Y;
480 end;
481 end;
483 it.Obj.oldX := it.Obj.X;
484 it.Obj.oldY := it.Obj.Y;
486 // Óñòàíîâêà àíèìàöèè
487 case it.ItemType of
488 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
489 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
490 ITEM_JETPACK: if g_Frames_Get(ID, 'FRAMES_ITEM_JETPACK') then it.Animation := TAnimation.Create(ID, True, 15);
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;
504 procedure g_Items_PreUpdate ();
505 var
506 i: Integer;
507 begin
508 if (ggItems = nil) then Exit;
509 for i := 0 to High(ggItems) do
510 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
511 begin
512 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
513 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
514 end;
515 end;
517 procedure g_Items_Update ();
518 var
519 i, j, k: Integer;
520 ID: DWord;
521 Anim: TAnimation;
522 m, ItemRespawnTime: Word;
523 r, nxt: Boolean;
524 begin
525 if (ggItems = nil) then exit;
527 // respawn items in 15 seconds regardless of settings during warmup
528 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
530 for i := 0 to High(ggItems) do
531 begin
532 if (ggItems[i].ItemType = ITEM_NONE) then continue;
533 if not ggItems[i].slotIsUsed then continue; // just in case
535 with ggItems[i] do
536 begin
537 nxt := False;
539 if alive then
540 begin
541 if Fall then
542 begin
543 m := g_Obj_Move(@Obj, True, True);
544 positionChanged(); // this updates spatial accelerators
546 // Ñîïðîòèâëåíèå âîçäóõà
547 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
549 // Åñëè âûïàë çà êàðòó
550 if WordBool(m and MOVE_FALLOUT) then
551 begin
552 if SpawnTrigger = -1 then
553 begin
554 g_Items_Pick(i);
555 end
556 else
557 begin
558 g_Items_Remove(i);
559 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
560 end;
561 continue;
562 end;
563 end;
565 // Åñëè èãðîêè ïîáëèçîñòè
566 if (gPlayers <> nil) then
567 begin
568 j := Random(Length(gPlayers))-1;
570 for k := 0 to High(gPlayers) do
571 begin
572 Inc(j);
573 if j > High(gPlayers) then j := 0;
575 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
576 begin
577 if g_Game_IsClient then continue;
579 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
581 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
584 Doom 2D: Original:
585 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
586 +2. I_MEGA,I_INVL,I_SUPER
587 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
589 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
591 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
592 if r then
593 begin
594 if not (Respawnable and (ItemRespawnTime > 0)) then
595 g_Items_Remove(i)
596 else
597 g_Items_Pick(i);
598 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
599 nxt := True;
600 break;
601 end;
602 end;
603 end;
604 end;
606 if nxt then continue;
607 end;
609 if Respawnable and g_Game_IsServer then
610 begin
611 DecMin(RespawnTime, 0);
612 if (RespawnTime = 0) and (not alive) then
613 begin
614 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
616 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
617 begin
618 Anim := TAnimation.Create(ID, False, 4);
619 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
620 Anim.Free();
621 end;
623 Obj.oldX := InitX;
624 Obj.oldY := InitY;
625 Obj.X := InitX;
626 Obj.Y := InitY;
627 Obj.Vel.X := 0;
628 Obj.Vel.Y := 0;
629 Obj.Accel.X := 0;
630 Obj.Accel.Y := 0;
631 positionChanged(); // this updates spatial accelerators
633 alive := true;
635 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
636 QuietRespawn := false;
637 end;
638 end;
640 if (Animation <> nil) then Animation.Update();
641 end;
642 end;
643 end;
645 procedure g_Items_SetDrop (ID: DWORD);
646 begin
647 if (ID < Length(ggItems)) then
648 begin
649 ggItems[ID].dropped := true;
650 end;
651 end;
654 procedure g_Items_Pick (ID: DWORD);
655 begin
656 if (ID < Length(ggItems)) then
657 begin
658 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
659 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
660 ggItems[ID].alive := false;
661 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
662 end;
663 end;
666 procedure g_Items_Remove (ID: DWORD);
667 var
668 it: PItem;
669 trig: Integer;
670 begin
671 if not g_Items_ValidId(ID) then
672 begin
673 //writeln('g_Items_Remove: invalid item id: ', ID);
674 raise Exception.Create('g_Items_Remove: invalid item id');
675 //exit;
676 end;
678 it := @ggItems[ID];
679 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
681 it.Obj.oldX := it.Obj.X;
682 it.Obj.oldY := it.Obj.Y;
683 trig := it.SpawnTrigger;
685 releaseItem(ID);
687 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
688 end;
691 procedure g_Items_SaveState (st: TStream);
692 var
693 count, i: Integer;
694 tt: Byte;
695 begin
696 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
697 count := 0;
698 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
700 // Êîëè÷åñòâî ïðåäìåòîâ
701 utils.writeInt(st, LongInt(count));
702 if (count = 0) then exit;
704 for i := 0 to High(ggItems) do
705 begin
706 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
707 begin
708 // Ñèãíàòóðà ïðåäìåòà
709 utils.writeSign(st, 'ITEM');
710 utils.writeInt(st, Byte(0));
711 // Òèï ïðåäìåòà
712 tt := ggItems[i].ItemType;
713 if ggItems[i].dropped then tt := tt or $80;
714 utils.writeInt(st, Byte(tt));
715 // Åñòü ëè ðåñïàóí
716 utils.writeBool(st, ggItems[i].Respawnable);
717 // Êîîðäèíàòû ðåñïóíà
718 utils.writeInt(st, LongInt(ggItems[i].InitX));
719 utils.writeInt(st, LongInt(ggItems[i].InitY));
720 // Âðåìÿ äî ðåñïàóíà
721 utils.writeInt(st, Word(ggItems[i].RespawnTime));
722 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
723 utils.writeBool(st, ggItems[i].alive);
724 // Ìîæåò ëè îí ïàäàòü
725 utils.writeBool(st, ggItems[i].Fall);
726 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
727 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
728 // Îáúåêò ïðåäìåòà
729 Obj_SaveState(st, @ggItems[i].Obj);
730 end;
731 end;
732 end;
735 procedure g_Items_LoadState (st: TStream);
736 var
737 count, i, a: Integer;
738 b: Byte;
739 begin
740 assert(st <> nil);
742 g_Items_Free();
744 // Êîëè÷åñòâî ïðåäìåòîâ
745 count := utils.readLongInt(st);
746 if (count = 0) then exit;
747 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
749 for a := 0 to count-1 do
750 begin
751 // Ñèãíàòóðà ïðåäìåòà
752 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
753 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
754 // Òèï ïðåäìåòà
755 b := utils.readByte(st); // bit7=1: monster drop
756 // Ñîçäàåì ïðåäìåò
757 i := g_Items_Create(0, 0, b and $7F, False, False);
758 if ((b and $80) <> 0) then g_Items_SetDrop(i);
759 // Åñòü ëè ðåñïàóí
760 ggItems[i].Respawnable := utils.readBool(st);
761 // Êîîðäèíàòû ðåñïóíà
762 ggItems[i].InitX := utils.readLongInt(st);
763 ggItems[i].InitY := utils.readLongInt(st);
764 // Âðåìÿ äî ðåñïàóíà
765 ggItems[i].RespawnTime := utils.readWord(st);
766 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
767 ggItems[i].alive := utils.readBool(st);
768 // Ìîæåò ëè îí ïàäàòü
769 ggItems[i].Fall := utils.readBool(st);
770 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
771 ggItems[i].SpawnTrigger := utils.readLongInt(st);
772 // Îáúåêò ïðåäìåòà
773 Obj_LoadState(@ggItems[i].Obj, st);
774 end;
775 end;
778 procedure g_Items_RestartRound ();
779 var
780 i: Integer;
781 it: PItem;
782 begin
783 for i := 0 to High(ggItems) do
784 begin
785 it := @ggItems[i];
786 it.Obj.oldX := it.Obj.X;
787 it.Obj.oldY := it.Obj.Y;
788 if not it.slotIsUsed then continue;
789 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
790 begin
791 it.QuietRespawn := True;
792 it.RespawnTime := 0;
793 end
794 else
795 begin
796 g_Items_Remove(i);
797 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
798 end;
799 end;
800 end;
803 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
804 var
805 idx: Integer;
806 begin
807 result := false;
808 if (ggItems = nil) or not assigned(cb) then exit;
810 if backwards then
811 begin
812 for idx := High(ggItems) downto 0 do
813 begin
814 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
815 begin
816 result := cb(@ggItems[idx]);
817 if result then exit;
818 end;
819 end;
820 end
821 else
822 begin
823 for idx := 0 to High(ggItems) do
824 begin
825 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
826 begin
827 result := cb(@ggItems[idx]);
828 if result then exit;
829 end;
830 end;
831 end;
832 end;
835 // ////////////////////////////////////////////////////////////////////////// //
836 procedure g_Items_EmitPickupSound (idx: Integer);
837 var
838 it: PItem;
839 begin
840 if not g_Items_ValidId(idx) then exit;
841 it := @ggItems[idx];
842 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
843 end;
845 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
846 var
847 it: PItem;
848 begin
849 if not g_Items_ValidId(idx) then exit;
851 it := @ggItems[idx];
852 if gSoundEffectsDF then
853 begin
854 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
855 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
856 begin
857 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
858 end
859 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
860 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
861 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
862 ITEM_AMMO_BACKPACK] then
863 begin
864 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
865 end
866 else
867 begin
868 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
869 end;
870 end
871 else
872 begin
873 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
874 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
875 begin
876 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
877 end
878 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
879 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
880 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
881 begin
882 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
883 end
884 else
885 begin
886 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
887 end;
888 end;
889 end;
892 procedure g_Items_AddDynLights();
893 var
894 f: Integer;
895 it: PItem;
896 begin
897 for f := 0 to High(ggItems) do
898 begin
899 it := @ggItems[f];
900 if not it.alive then continue;
901 case it.ItemType of
902 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);
903 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);
904 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);
905 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);
906 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);
907 ITEM_JETPACK: 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);
908 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);
909 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);
910 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);
911 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);
912 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);
913 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);
914 end;
915 end;
916 end;
919 end.