DEADSOFTWARE

8d476efe76e6fa3fc1c27f98644e2def961704b4
[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 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 (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 implementation
89 uses
90 Math,
91 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
92 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
93 e_log,
94 g_grid, binheap, idpool, utils, xstreams;
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 end;
215 procedure g_Items_LoadData();
216 begin
217 e_WriteLog('Loading items data...', TMsgType.Notify);
219 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
220 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
221 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
222 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
224 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
225 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
226 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK', 32, 32, 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');
268 InitTextures();
270 freeIds := TIdPool.Create();
271 end;
274 procedure g_Items_FreeData();
275 begin
276 e_WriteLog('Releasing items data...', TMsgType.Notify);
278 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
279 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
280 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
281 g_Sound_Delete('SOUND_ITEM_GETITEM');
283 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
284 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
285 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
286 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
287 g_Frames_DeleteByName('FRAMES_ITEM_JETPACK');
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');
327 freeIds.Free();
328 freeIds := nil;
329 end;
332 procedure releaseItem (idx: Integer);
333 var
334 it: PItem;
335 begin
336 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
337 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
338 it := @ggItems[idx];
339 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
340 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
341 it.slotIsUsed := false;
342 if (it.Animation <> nil) then
343 begin
344 it.Animation.Free();
345 it.Animation := nil;
346 end;
347 it.alive := False;
348 it.SpawnTrigger := -1;
349 it.ItemType := ITEM_NONE;
350 freeIds.release(LongWord(idx));
351 end;
354 procedure growItemArrayTo (newsz: Integer);
355 var
356 i, olen: Integer;
357 it: PItem;
358 begin
359 if (newsz < Length(ggItems)) then exit;
360 // no free slots
361 olen := Length(ggItems);
362 SetLength(ggItems, newsz);
363 for i := olen to High(ggItems) do
364 begin
365 it := @ggItems[i];
366 it.slotIsUsed := false;
367 it.arrIdx := i;
368 it.ItemType := ITEM_NONE;
369 it.Animation := nil;
370 it.alive := false;
371 it.SpawnTrigger := -1;
372 it.Respawnable := false;
373 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
374 end;
375 end;
378 function allocItem (): DWORD;
379 begin
380 result := freeIds.alloc();
381 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
382 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
383 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
384 end;
387 // it will be slow if the slot is free (we have to rebuild the heap)
388 function wantItemSlot (slot: Integer): Integer;
389 var
390 olen: Integer;
391 it: PItem;
392 begin
393 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
394 // do we need to grow item storate?
395 olen := Length(ggItems);
396 if (slot >= olen) then growItemArrayTo(slot+64);
398 it := @ggItems[slot];
399 if not it.slotIsUsed then
400 begin
401 freeIds.alloc(LongWord(slot));
402 end
403 else
404 begin
405 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
406 end;
407 it.slotIsUsed := false;
409 result := slot;
410 end;
413 // ////////////////////////////////////////////////////////////////////////// //
414 procedure g_Items_Init ();
415 var
416 a, b: Integer;
417 begin
418 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
419 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
420 gMaxDist := Trunc(Hypot(a, b));
421 end;
424 procedure g_Items_Free ();
425 var
426 i: Integer;
427 begin
428 if (ggItems <> nil) then
429 begin
430 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
431 ggItems := nil;
432 end;
433 freeIds.clear();
434 end;
437 function g_Items_Create (X, Y: Integer; ItemType: Byte;
438 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
439 var
440 find_id: DWORD;
441 ID: DWORD;
442 it: PItem;
443 begin
444 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
446 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
448 it := @ggItems[find_id];
450 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
451 //it.arrIdx := find_id;
452 it.slotIsUsed := true;
454 it.ItemType := ItemType;
455 it.Respawnable := Respawnable;
456 it.InitX := X;
457 it.InitY := Y;
458 it.RespawnTime := 0;
459 it.Fall := Fall;
460 it.alive := True;
461 it.QuietRespawn := False;
462 it.dropped := false;
464 g_Obj_Init(@it.Obj);
465 it.Obj.X := X;
466 it.Obj.Y := Y;
467 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
468 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
470 it.Animation := nil;
471 it.SpawnTrigger := -1;
473 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
474 if AdjCoord then
475 begin
476 with it^ do
477 begin
478 Obj.X := X - (Obj.Rect.Width div 2);
479 Obj.Y := Y - Obj.Rect.Height;
480 InitX := Obj.X;
481 InitY := Obj.Y;
482 end;
483 end;
485 // Óñòàíîâêà àíèìàöèè
486 case it.ItemType of
487 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
488 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
489 ITEM_JETPACK: if g_Frames_Get(ID, 'FRAMES_ITEM_JETPACK') then it.Animation := TAnimation.Create(ID, True, 15);
490 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
491 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
492 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
493 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
494 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
495 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
496 end;
498 it.positionChanged();
500 result := find_id;
501 end;
504 procedure g_Items_Update ();
505 var
506 i, j, k: Integer;
507 ID: DWord;
508 Anim: TAnimation;
509 m, ItemRespawnTime: Word;
510 r, nxt: Boolean;
511 begin
512 if (ggItems = nil) then exit;
514 // respawn items in 15 seconds regardless of settings during warmup
515 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
517 for i := 0 to High(ggItems) do
518 begin
519 if (ggItems[i].ItemType = ITEM_NONE) then continue;
520 if not ggItems[i].slotIsUsed then continue; // just in case
522 with ggItems[i] do
523 begin
524 nxt := False;
526 if alive then
527 begin
528 if Fall then
529 begin
530 m := g_Obj_Move(@Obj, True, True);
531 positionChanged(); // this updates spatial accelerators
533 // Ñîïðîòèâëåíèå âîçäóõà
534 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
536 // Åñëè âûïàë çà êàðòó
537 if WordBool(m and MOVE_FALLOUT) then
538 begin
539 if SpawnTrigger = -1 then
540 begin
541 g_Items_Pick(i);
542 end
543 else
544 begin
545 g_Items_Remove(i);
546 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
547 end;
548 continue;
549 end;
550 end;
552 // Åñëè èãðîêè ïîáëèçîñòè
553 if (gPlayers <> nil) then
554 begin
555 j := Random(Length(gPlayers))-1;
557 for k := 0 to High(gPlayers) do
558 begin
559 Inc(j);
560 if j > High(gPlayers) then j := 0;
562 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
563 begin
564 if g_Game_IsClient then continue;
566 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
568 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
571 Doom 2D: Original:
572 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
573 +2. I_MEGA,I_INVL,I_SUPER
574 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
576 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
578 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
579 if r then
580 begin
581 if not (Respawnable and (ItemRespawnTime > 0)) then
582 g_Items_Remove(i)
583 else
584 g_Items_Pick(i);
585 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
586 nxt := True;
587 break;
588 end;
589 end;
590 end;
591 end;
593 if nxt then continue;
594 end;
596 if Respawnable and g_Game_IsServer then
597 begin
598 DecMin(RespawnTime, 0);
599 if (RespawnTime = 0) and (not alive) then
600 begin
601 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
603 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
604 begin
605 Anim := TAnimation.Create(ID, False, 4);
606 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
607 Anim.Free();
608 end;
610 Obj.X := InitX;
611 Obj.Y := InitY;
612 Obj.Vel.X := 0;
613 Obj.Vel.Y := 0;
614 Obj.Accel.X := 0;
615 Obj.Accel.Y := 0;
616 positionChanged(); // this updates spatial accelerators
618 alive := true;
620 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
621 QuietRespawn := false;
622 end;
623 end;
625 if (Animation <> nil) then Animation.Update();
626 end;
627 end;
628 end;
631 procedure itemsDrawInternal (dropflag: Boolean);
632 var
633 i: Integer;
634 it: PItem;
635 begin
636 if (ggItems = nil) then exit;
638 for i := 0 to High(ggItems) do
639 begin
640 it := @ggItems[i];
641 if (not it.slotIsUsed) or (it.ItemType = ITEM_NONE) then continue; // just in case
642 if not it.alive then continue;
643 if (it.dropped <> dropflag) then continue;
645 with it^ do
646 begin
647 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
648 begin
649 if (Animation = nil) then
650 begin
651 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
652 end
653 else
654 begin
655 Animation.Draw(Obj.X, Obj.Y, TMirrorType.None);
656 end;
658 if g_debug_Frames then
659 begin
660 e_DrawQuad(Obj.X+Obj.Rect.X,
661 Obj.Y+Obj.Rect.Y,
662 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
663 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
664 0, 255, 0);
665 end;
666 end;
667 end;
668 end;
669 end;
672 procedure g_Items_Draw ();
673 begin
674 itemsDrawInternal(false);
675 end;
677 procedure g_Items_DrawDrop ();
678 begin
679 itemsDrawInternal(true);
680 end;
683 procedure g_Items_SetDrop (ID: DWORD);
684 begin
685 if (ID < Length(ggItems)) then
686 begin
687 ggItems[ID].dropped := true;
688 end;
689 end;
692 procedure g_Items_Pick (ID: DWORD);
693 begin
694 if (ID < Length(ggItems)) then
695 begin
696 ggItems[ID].alive := false;
697 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
698 end;
699 end;
702 procedure g_Items_Remove (ID: DWORD);
703 var
704 it: PItem;
705 trig: Integer;
706 begin
707 if not g_Items_ValidId(ID) then
708 begin
709 //writeln('g_Items_Remove: invalid item id: ', ID);
710 raise Exception.Create('g_Items_Remove: invalid item id');
711 //exit;
712 end;
714 it := @ggItems[ID];
715 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
717 trig := it.SpawnTrigger;
719 releaseItem(ID);
721 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
722 end;
725 procedure g_Items_SaveState (st: TStream);
726 var
727 count, i: Integer;
728 tt: Byte;
729 begin
730 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
731 count := 0;
732 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
734 // Êîëè÷åñòâî ïðåäìåòîâ
735 utils.writeInt(st, LongInt(count));
736 if (count = 0) then exit;
738 for i := 0 to High(ggItems) do
739 begin
740 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
741 begin
742 // Ñèãíàòóðà ïðåäìåòà
743 utils.writeSign(st, 'ITEM');
744 utils.writeInt(st, Byte(0));
745 // Òèï ïðåäìåòà
746 tt := ggItems[i].ItemType;
747 if ggItems[i].dropped then tt := tt or $80;
748 utils.writeInt(st, Byte(tt));
749 // Åñòü ëè ðåñïàóí
750 utils.writeBool(st, ggItems[i].Respawnable);
751 // Êîîðäèíàòû ðåñïóíà
752 utils.writeInt(st, LongInt(ggItems[i].InitX));
753 utils.writeInt(st, LongInt(ggItems[i].InitY));
754 // Âðåìÿ äî ðåñïàóíà
755 utils.writeInt(st, Word(ggItems[i].RespawnTime));
756 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
757 utils.writeBool(st, ggItems[i].alive);
758 // Ìîæåò ëè îí ïàäàòü
759 utils.writeBool(st, ggItems[i].Fall);
760 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
761 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
762 // Îáúåêò ïðåäìåòà
763 Obj_SaveState(st, @ggItems[i].Obj);
764 end;
765 end;
766 end;
769 procedure g_Items_LoadState (st: TStream);
770 var
771 count, i, a: Integer;
772 b: Byte;
773 begin
774 assert(st <> nil);
776 g_Items_Free();
778 // Êîëè÷åñòâî ïðåäìåòîâ
779 count := utils.readLongInt(st);
780 if (count = 0) then exit;
781 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
783 for a := 0 to count-1 do
784 begin
785 // Ñèãíàòóðà ïðåäìåòà
786 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
787 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
788 // Òèï ïðåäìåòà
789 b := utils.readByte(st); // bit7=1: monster drop
790 // Ñîçäàåì ïðåäìåò
791 i := g_Items_Create(0, 0, b and $7F, False, False);
792 if ((b and $80) <> 0) then g_Items_SetDrop(i);
793 // Åñòü ëè ðåñïàóí
794 ggItems[i].Respawnable := utils.readBool(st);
795 // Êîîðäèíàòû ðåñïóíà
796 ggItems[i].InitX := utils.readLongInt(st);
797 ggItems[i].InitY := utils.readLongInt(st);
798 // Âðåìÿ äî ðåñïàóíà
799 ggItems[i].RespawnTime := utils.readWord(st);
800 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
801 ggItems[i].alive := utils.readBool(st);
802 // Ìîæåò ëè îí ïàäàòü
803 ggItems[i].Fall := utils.readBool(st);
804 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
805 ggItems[i].SpawnTrigger := utils.readLongInt(st);
806 // Îáúåêò ïðåäìåòà
807 Obj_LoadState(@ggItems[i].Obj, st);
808 end;
809 end;
812 procedure g_Items_RestartRound ();
813 var
814 i: Integer;
815 it: PItem;
816 begin
817 for i := 0 to High(ggItems) do
818 begin
819 it := @ggItems[i];
820 if not it.slotIsUsed then continue;
821 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
822 begin
823 it.QuietRespawn := True;
824 it.RespawnTime := 0;
825 end
826 else
827 begin
828 g_Items_Remove(i);
829 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
830 end;
831 end;
832 end;
835 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
836 var
837 idx: Integer;
838 begin
839 result := false;
840 if (ggItems = nil) or not assigned(cb) then exit;
842 if backwards then
843 begin
844 for idx := High(ggItems) downto 0 do
845 begin
846 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
847 begin
848 result := cb(@ggItems[idx]);
849 if result then exit;
850 end;
851 end;
852 end
853 else
854 begin
855 for idx := 0 to High(ggItems) do
856 begin
857 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
858 begin
859 result := cb(@ggItems[idx]);
860 if result then exit;
861 end;
862 end;
863 end;
864 end;
867 // ////////////////////////////////////////////////////////////////////////// //
868 procedure g_Items_EmitPickupSound (idx: Integer);
869 var
870 it: PItem;
871 begin
872 if not g_Items_ValidId(idx) then exit;
873 it := @ggItems[idx];
874 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
875 end;
877 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
878 var
879 it: PItem;
880 begin
881 if not g_Items_ValidId(idx) then exit;
883 it := @ggItems[idx];
884 if gSoundEffectsDF then
885 begin
886 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
887 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
888 begin
889 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
890 end
891 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
892 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
893 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
894 ITEM_AMMO_BACKPACK] then
895 begin
896 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
897 end
898 else
899 begin
900 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
901 end;
902 end
903 else
904 begin
905 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
906 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
907 begin
908 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
909 end
910 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
911 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
912 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
913 begin
914 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
915 end
916 else
917 begin
918 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
919 end;
920 end;
921 end;
924 procedure g_Items_AddDynLights();
925 var
926 f: Integer;
927 it: PItem;
928 begin
929 for f := 0 to High(ggItems) do
930 begin
931 it := @ggItems[f];
932 if not it.alive then continue;
933 case it.ItemType of
934 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);
935 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);
936 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);
937 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);
938 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);
939 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);
940 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);
941 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);
942 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);
943 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);
944 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);
945 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);
946 end;
947 end;
948 end;
951 end.