DEADSOFTWARE

Game: Assign gsGameFlags where it's missing
[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_PreUpdate();
58 procedure g_Items_Update();
59 procedure g_Items_Draw();
60 procedure g_Items_DrawDrop();
61 procedure g_Items_Pick(ID: DWORD);
62 procedure g_Items_Remove(ID: DWORD);
63 procedure g_Items_SaveState (st: TStream);
64 procedure g_Items_LoadState (st: TStream);
66 procedure g_Items_RestartRound ();
68 function g_Items_ValidId (idx: Integer): Boolean; inline;
69 function g_Items_ByIdx (idx: Integer): PItem;
70 function g_Items_ObjByIdx (idx: Integer): PObj;
72 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
73 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
75 procedure g_Items_AddDynLights();
78 type
79 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
81 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
84 var
85 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
86 gMaxDist: Integer = 1; // for sounds
88 implementation
90 uses
91 Math,
92 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
93 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
94 e_log,
95 g_grid, binheap, idpool, utils, xstreams;
98 var
99 ggItems: Array of TItem = nil;
102 // ////////////////////////////////////////////////////////////////////////// //
103 var
104 freeIds: TIdPool = nil;
107 // ////////////////////////////////////////////////////////////////////////// //
108 function g_Items_ValidId (idx: Integer): Boolean; inline;
109 begin
110 result := false;
111 if (idx < 0) or (idx > High(ggItems)) then exit;
112 if not ggItems[idx].slotIsUsed then exit;
113 result := true;
114 end;
117 function g_Items_ByIdx (idx: Integer): PItem;
118 begin
119 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
120 result := @ggItems[idx];
121 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
122 end;
125 function g_Items_ObjByIdx (idx: Integer): PObj;
126 begin
127 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
128 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
129 result := @ggItems[idx].Obj;
130 end;
133 // ////////////////////////////////////////////////////////////////////////// //
134 procedure TItem.positionChanged ();
135 begin
136 end;
139 // ////////////////////////////////////////////////////////////////////////// //
140 const
141 ITEM_SIGNATURE = $4D455449; // 'ITEM'
143 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
144 (((14), (15)), // MEDKIT_SMALL
145 ((28), (19)), // MEDKIT_LARGE
146 ((28), (19)), // MEDKIT_BLACK
147 ((31), (16)), // ARMOR_GREEN
148 ((31), (16)), // ARMOR_BLUE
149 ((25), (25)), // SPHERE_BLUE
150 ((25), (25)), // SPHERE_WHITE
151 ((24), (47)), // SUIT
152 ((14), (27)), // OXYGEN
153 ((25), (25)), // INVUL
154 ((62), (24)), // WEAPON_SAW
155 ((63), (12)), // WEAPON_SHOTGUN1
156 ((54), (13)), // WEAPON_SHOTGUN2
157 ((54), (16)), // WEAPON_CHAINGUN
158 ((62), (16)), // WEAPON_ROCKETLAUNCHER
159 ((54), (16)), // WEAPON_PLASMA
160 ((61), (36)), // WEAPON_BFG
161 ((54), (16)), // WEAPON_SUPERPULEMET
162 (( 9), (11)), // AMMO_BULLETS
163 ((28), (16)), // AMMO_BULLETS_BOX
164 ((15), ( 7)), // AMMO_SHELLS
165 ((32), (12)), // AMMO_SHELLS_BOX
166 ((12), (27)), // AMMO_ROCKET
167 ((54), (21)), // AMMO_ROCKET_BOX
168 ((15), (12)), // AMMO_CELL
169 ((32), (21)), // AMMO_CELL_BIG
170 ((22), (29)), // AMMO_BACKPACK
171 ((16), (16)), // KEY_RED
172 ((16), (16)), // KEY_GREEN
173 ((16), (16)), // KEY_BLUE
174 (( 1), ( 1)), // WEAPON_KASTET
175 ((43), (16)), // WEAPON_PISTOL
176 ((14), (18)), // BOTTLE
177 ((16), (15)), // HELMET
178 ((32), (24)), // JETPACK
179 ((25), (25)), // INVIS
180 ((53), (20)), // WEAPON_FLAMETHROWER
181 ((13), (20))); // AMMO_FUELCAN
183 procedure InitTextures();
184 begin
185 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
186 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
187 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
188 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
189 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
190 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
191 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
192 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
193 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
194 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
195 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
196 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
197 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
198 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
199 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
200 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
201 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
202 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
203 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
204 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
205 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
206 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
207 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
208 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
209 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
210 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
211 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
212 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
213 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
214 end;
216 procedure g_Items_LoadData();
217 begin
218 e_WriteLog('Loading items data...', TMsgType.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_JETPACK', GameWAD+':TEXTURES\JETPACK', 32, 32, 3, True);
230 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
231 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
232 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
233 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
234 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
235 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
236 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
237 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
238 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
239 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
240 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
241 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
242 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
243 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
244 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
245 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
246 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
247 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
248 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
249 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
250 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
251 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
252 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
253 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
254 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
255 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
256 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
257 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
258 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
259 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
260 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
261 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
262 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
263 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
264 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
265 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
266 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
267 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
269 InitTextures();
271 freeIds := TIdPool.Create();
272 end;
275 procedure g_Items_FreeData();
276 begin
277 e_WriteLog('Releasing items data...', TMsgType.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_JETPACK');
289 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
290 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
291 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
292 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
293 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
294 g_Frames_DeleteByName('FRAMES_FLAG_RED');
295 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
296 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
297 g_Texture_Delete('ITEM_MEDKIT_SMALL');
298 g_Texture_Delete('ITEM_MEDKIT_LARGE');
299 g_Texture_Delete('ITEM_WEAPON_SAW');
300 g_Texture_Delete('ITEM_WEAPON_PISTOL');
301 g_Texture_Delete('ITEM_WEAPON_KASTET');
302 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
303 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
304 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
305 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
306 g_Texture_Delete('ITEM_WEAPON_PLASMA');
307 g_Texture_Delete('ITEM_WEAPON_BFG');
308 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
309 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
310 g_Texture_Delete('ITEM_AMMO_BULLETS');
311 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
312 g_Texture_Delete('ITEM_AMMO_SHELLS');
313 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
314 g_Texture_Delete('ITEM_AMMO_ROCKET');
315 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
316 g_Texture_Delete('ITEM_AMMO_CELL');
317 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
318 g_Texture_Delete('ITEM_AMMO_FUELCAN');
319 g_Texture_Delete('ITEM_AMMO_BACKPACK');
320 g_Texture_Delete('ITEM_KEY_RED');
321 g_Texture_Delete('ITEM_KEY_GREEN');
322 g_Texture_Delete('ITEM_KEY_BLUE');
323 g_Texture_Delete('ITEM_OXYGEN');
324 g_Texture_Delete('ITEM_SUIT');
325 g_Texture_Delete('ITEM_WEAPON_KASTET');
326 g_Texture_Delete('ITEM_MEDKIT_BLACK');
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 it.InitX := X;
458 it.InitY := Y;
459 it.RespawnTime := 0;
460 it.Fall := Fall;
461 it.alive := True;
462 it.QuietRespawn := False;
463 it.dropped := false;
465 g_Obj_Init(@it.Obj);
466 it.Obj.oldX := X;
467 it.Obj.oldY := Y;
468 it.Obj.X := X;
469 it.Obj.Y := Y;
470 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
471 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
473 it.Animation := nil;
474 it.SpawnTrigger := -1;
476 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
477 if AdjCoord then
478 begin
479 with it^ do
480 begin
481 Obj.X := X - (Obj.Rect.Width div 2);
482 Obj.Y := Y - Obj.Rect.Height;
483 InitX := Obj.X;
484 InitY := Obj.Y;
485 end;
486 end;
488 // Óñòàíîâêà àíèìàöèè
489 case it.ItemType of
490 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
491 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
492 ITEM_JETPACK: if g_Frames_Get(ID, 'FRAMES_ITEM_JETPACK') then it.Animation := TAnimation.Create(ID, True, 15);
493 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
494 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
495 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
496 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
497 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
498 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
499 end;
501 it.positionChanged();
503 result := find_id;
504 end;
506 procedure g_Items_PreUpdate ();
507 var
508 i: Integer;
509 begin
510 if (ggItems = nil) then Exit;
511 for i := 0 to High(ggItems) do
512 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
513 begin
514 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
515 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
516 end;
517 end;
519 procedure g_Items_Update ();
520 var
521 i, j, k: Integer;
522 ID: DWord;
523 Anim: TAnimation;
524 m, ItemRespawnTime: Word;
525 r, nxt: Boolean;
526 begin
527 if (ggItems = nil) then exit;
529 // respawn items in 15 seconds regardless of settings during warmup
530 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
532 for i := 0 to High(ggItems) do
533 begin
534 if (ggItems[i].ItemType = ITEM_NONE) then continue;
535 if not ggItems[i].slotIsUsed then continue; // just in case
537 with ggItems[i] do
538 begin
539 nxt := False;
541 if alive then
542 begin
543 if Fall then
544 begin
545 m := g_Obj_Move(@Obj, True, True);
546 positionChanged(); // this updates spatial accelerators
548 // Ñîïðîòèâëåíèå âîçäóõà
549 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
551 // Åñëè âûïàë çà êàðòó
552 if WordBool(m and MOVE_FALLOUT) then
553 begin
554 if SpawnTrigger = -1 then
555 begin
556 g_Items_Pick(i);
557 end
558 else
559 begin
560 g_Items_Remove(i);
561 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
562 end;
563 continue;
564 end;
565 end;
567 // Åñëè èãðîêè ïîáëèçîñòè
568 if (gPlayers <> nil) then
569 begin
570 j := Random(Length(gPlayers))-1;
572 for k := 0 to High(gPlayers) do
573 begin
574 Inc(j);
575 if j > High(gPlayers) then j := 0;
577 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
578 begin
579 if g_Game_IsClient then continue;
581 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
583 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
586 Doom 2D: Original:
587 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
588 +2. I_MEGA,I_INVL,I_SUPER
589 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
591 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
593 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
594 if r then
595 begin
596 if not (Respawnable and (ItemRespawnTime > 0)) then
597 g_Items_Remove(i)
598 else
599 g_Items_Pick(i);
600 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
601 nxt := True;
602 break;
603 end;
604 end;
605 end;
606 end;
608 if nxt then continue;
609 end;
611 if Respawnable and g_Game_IsServer then
612 begin
613 DecMin(RespawnTime, 0);
614 if (RespawnTime = 0) and (not alive) then
615 begin
616 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
618 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
619 begin
620 Anim := TAnimation.Create(ID, False, 4);
621 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
622 Anim.Free();
623 end;
625 Obj.oldX := InitX;
626 Obj.oldY := InitY;
627 Obj.X := InitX;
628 Obj.Y := InitY;
629 Obj.Vel.X := 0;
630 Obj.Vel.Y := 0;
631 Obj.Accel.X := 0;
632 Obj.Accel.Y := 0;
633 positionChanged(); // this updates spatial accelerators
635 alive := true;
637 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
638 QuietRespawn := false;
639 end;
640 end;
642 if (Animation <> nil) then Animation.Update();
643 end;
644 end;
645 end;
648 procedure itemsDrawInternal (dropflag: Boolean);
649 var
650 i, fX, fY: Integer;
651 it: PItem;
652 begin
653 if (ggItems = nil) then exit;
655 for i := 0 to High(ggItems) do
656 begin
657 it := @ggItems[i];
658 if (not it.slotIsUsed) or (it.ItemType = ITEM_NONE) then continue; // just in case
659 if not it.alive then continue;
660 if (it.dropped <> dropflag) then continue;
662 with it^ do
663 begin
664 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
665 begin
666 Obj.lerp(gLerpFactor, fX, fY);
667 if (Animation = nil) then
668 begin
669 e_Draw(gItemsTexturesID[ItemType], fX, fY, 0, true, false);
670 end
671 else
672 begin
673 Animation.Draw(fX, fY, TMirrorType.None);
674 end;
676 if g_debug_Frames then
677 begin
678 e_DrawQuad(Obj.X+Obj.Rect.X,
679 Obj.Y+Obj.Rect.Y,
680 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
681 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
682 0, 255, 0);
683 end;
684 end;
685 end;
686 end;
687 end;
690 procedure g_Items_Draw ();
691 begin
692 itemsDrawInternal(false);
693 end;
695 procedure g_Items_DrawDrop ();
696 begin
697 itemsDrawInternal(true);
698 end;
701 procedure g_Items_SetDrop (ID: DWORD);
702 begin
703 if (ID < Length(ggItems)) then
704 begin
705 ggItems[ID].dropped := true;
706 end;
707 end;
710 procedure g_Items_Pick (ID: DWORD);
711 begin
712 if (ID < Length(ggItems)) then
713 begin
714 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
715 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
716 ggItems[ID].alive := false;
717 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
718 end;
719 end;
722 procedure g_Items_Remove (ID: DWORD);
723 var
724 it: PItem;
725 trig: Integer;
726 begin
727 if not g_Items_ValidId(ID) then
728 begin
729 //writeln('g_Items_Remove: invalid item id: ', ID);
730 raise Exception.Create('g_Items_Remove: invalid item id');
731 //exit;
732 end;
734 it := @ggItems[ID];
735 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
737 it.Obj.oldX := it.Obj.X;
738 it.Obj.oldY := it.Obj.Y;
739 trig := it.SpawnTrigger;
741 releaseItem(ID);
743 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
744 end;
747 procedure g_Items_SaveState (st: TStream);
748 var
749 count, i: Integer;
750 tt: Byte;
751 begin
752 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
753 count := 0;
754 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
756 // Êîëè÷åñòâî ïðåäìåòîâ
757 utils.writeInt(st, LongInt(count));
758 if (count = 0) then exit;
760 for i := 0 to High(ggItems) do
761 begin
762 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
763 begin
764 // Ñèãíàòóðà ïðåäìåòà
765 utils.writeSign(st, 'ITEM');
766 utils.writeInt(st, Byte(0));
767 // Òèï ïðåäìåòà
768 tt := ggItems[i].ItemType;
769 if ggItems[i].dropped then tt := tt or $80;
770 utils.writeInt(st, Byte(tt));
771 // Åñòü ëè ðåñïàóí
772 utils.writeBool(st, ggItems[i].Respawnable);
773 // Êîîðäèíàòû ðåñïóíà
774 utils.writeInt(st, LongInt(ggItems[i].InitX));
775 utils.writeInt(st, LongInt(ggItems[i].InitY));
776 // Âðåìÿ äî ðåñïàóíà
777 utils.writeInt(st, Word(ggItems[i].RespawnTime));
778 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
779 utils.writeBool(st, ggItems[i].alive);
780 // Ìîæåò ëè îí ïàäàòü
781 utils.writeBool(st, ggItems[i].Fall);
782 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
783 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
784 // Îáúåêò ïðåäìåòà
785 Obj_SaveState(st, @ggItems[i].Obj);
786 end;
787 end;
788 end;
791 procedure g_Items_LoadState (st: TStream);
792 var
793 count, i, a: Integer;
794 b: Byte;
795 begin
796 assert(st <> nil);
798 g_Items_Free();
800 // Êîëè÷åñòâî ïðåäìåòîâ
801 count := utils.readLongInt(st);
802 if (count = 0) then exit;
803 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
805 for a := 0 to count-1 do
806 begin
807 // Ñèãíàòóðà ïðåäìåòà
808 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
809 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
810 // Òèï ïðåäìåòà
811 b := utils.readByte(st); // bit7=1: monster drop
812 // Ñîçäàåì ïðåäìåò
813 i := g_Items_Create(0, 0, b and $7F, False, False);
814 if ((b and $80) <> 0) then g_Items_SetDrop(i);
815 // Åñòü ëè ðåñïàóí
816 ggItems[i].Respawnable := utils.readBool(st);
817 // Êîîðäèíàòû ðåñïóíà
818 ggItems[i].InitX := utils.readLongInt(st);
819 ggItems[i].InitY := utils.readLongInt(st);
820 // Âðåìÿ äî ðåñïàóíà
821 ggItems[i].RespawnTime := utils.readWord(st);
822 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
823 ggItems[i].alive := utils.readBool(st);
824 // Ìîæåò ëè îí ïàäàòü
825 ggItems[i].Fall := utils.readBool(st);
826 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
827 ggItems[i].SpawnTrigger := utils.readLongInt(st);
828 // Îáúåêò ïðåäìåòà
829 Obj_LoadState(@ggItems[i].Obj, st);
830 end;
831 end;
834 procedure g_Items_RestartRound ();
835 var
836 i: Integer;
837 it: PItem;
838 begin
839 for i := 0 to High(ggItems) do
840 begin
841 it := @ggItems[i];
842 it.Obj.oldX := it.Obj.X;
843 it.Obj.oldY := it.Obj.Y;
844 if not it.slotIsUsed then continue;
845 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
846 begin
847 it.QuietRespawn := True;
848 it.RespawnTime := 0;
849 end
850 else
851 begin
852 g_Items_Remove(i);
853 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
854 end;
855 end;
856 end;
859 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
860 var
861 idx: Integer;
862 begin
863 result := false;
864 if (ggItems = nil) or not assigned(cb) then exit;
866 if backwards then
867 begin
868 for idx := High(ggItems) downto 0 do
869 begin
870 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
871 begin
872 result := cb(@ggItems[idx]);
873 if result then exit;
874 end;
875 end;
876 end
877 else
878 begin
879 for idx := 0 to High(ggItems) do
880 begin
881 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
882 begin
883 result := cb(@ggItems[idx]);
884 if result then exit;
885 end;
886 end;
887 end;
888 end;
891 // ////////////////////////////////////////////////////////////////////////// //
892 procedure g_Items_EmitPickupSound (idx: Integer);
893 var
894 it: PItem;
895 begin
896 if not g_Items_ValidId(idx) then exit;
897 it := @ggItems[idx];
898 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
899 end;
901 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
902 var
903 it: PItem;
904 begin
905 if not g_Items_ValidId(idx) then exit;
907 it := @ggItems[idx];
908 if gSoundEffectsDF then
909 begin
910 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
911 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
912 begin
913 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
914 end
915 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
916 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
917 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
918 ITEM_AMMO_BACKPACK] then
919 begin
920 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
921 end
922 else
923 begin
924 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
925 end;
926 end
927 else
928 begin
929 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
930 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
931 begin
932 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
933 end
934 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
935 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
936 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
937 begin
938 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
939 end
940 else
941 begin
942 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
943 end;
944 end;
945 end;
948 procedure g_Items_AddDynLights();
949 var
950 f: Integer;
951 it: PItem;
952 begin
953 for f := 0 to High(ggItems) do
954 begin
955 it := @ggItems[f];
956 if not it.alive then continue;
957 case it.ItemType of
958 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);
959 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);
960 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);
961 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);
962 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);
963 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);
964 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);
965 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);
966 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);
967 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);
968 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);
969 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);
970 end;
971 end;
972 end;
975 end.