DEADSOFTWARE

field namefix: `FLive` -> `FAlive`; `live` -> `alive`
[d2df-sdl.git] / src / game / g_items.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_items;
19 interface
21 uses
22 g_textures, g_phys, g_saveload, BinEditor, MAPDEF;
24 Type
25 PItem = ^TItem;
26 TItem = record
27 private
28 //treeNode: Integer;
29 slotIsUsed: Boolean;
30 arrIdx: Integer; // in ggItems
32 public
33 ItemType: Byte;
34 Respawnable: Boolean;
35 InitX, InitY: Integer;
36 RespawnTime: Word;
37 alive: Boolean;
38 Fall: Boolean;
39 QuietRespawn: Boolean;
40 SpawnTrigger: Integer;
41 Obj: TObj;
42 Animation: TAnimation;
44 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
46 property myid: Integer read arrIdx;
47 end;
49 procedure g_Items_LoadData();
50 procedure g_Items_FreeData();
51 procedure g_Items_Init();
52 procedure g_Items_Free();
53 function g_Items_Create(X, Y: Integer; ItemType: Byte;
54 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
55 procedure g_Items_Update();
56 procedure g_Items_Draw();
57 procedure g_Items_Pick(ID: DWORD);
58 procedure g_Items_Remove(ID: DWORD);
59 procedure g_Items_SaveState(var Mem: TBinMemoryWriter);
60 procedure g_Items_LoadState(var Mem: TBinMemoryReader);
62 procedure g_Items_RestartRound ();
64 function g_Items_ValidId (idx: Integer): Boolean; inline;
65 function g_Items_ByIdx (idx: Integer): PItem;
66 function g_Items_ObjByIdx (idx: Integer): PObj;
68 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
69 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
71 procedure g_Items_AddDynLights();
74 type
75 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
77 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
80 var
81 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
82 gMaxDist: Integer = 1; // for sounds
83 ITEM_RESPAWNTIME: Integer = 60 * 36;
85 implementation
87 uses
88 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
89 Math, g_game, g_triggers, g_console, SysUtils, g_player, g_net, g_netmsg,
90 e_log,
91 g_grid, binheap, idpool;
94 var
95 ggItems: Array of TItem = nil;
98 // ////////////////////////////////////////////////////////////////////////// //
99 var
100 freeIds: TIdPool = nil;
103 // ////////////////////////////////////////////////////////////////////////// //
104 function g_Items_ValidId (idx: Integer): Boolean; inline;
105 begin
106 result := false;
107 if (idx < 0) or (idx > High(ggItems)) then exit;
108 if not ggItems[idx].slotIsUsed then exit;
109 result := true;
110 end;
113 function g_Items_ByIdx (idx: Integer): PItem;
114 begin
115 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
116 result := @ggItems[idx];
117 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
118 end;
121 function g_Items_ObjByIdx (idx: Integer): PObj;
122 begin
123 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
124 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
125 result := @ggItems[idx].Obj;
126 end;
129 // ////////////////////////////////////////////////////////////////////////// //
130 procedure TItem.positionChanged ();
131 begin
132 end;
135 // ////////////////////////////////////////////////////////////////////////// //
136 const
137 ITEM_SIGNATURE = $4D455449; // 'ITEM'
139 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
140 (((14), (15)), // MEDKIT_SMALL
141 ((28), (19)), // MEDKIT_LARGE
142 ((28), (19)), // MEDKIT_BLACK
143 ((31), (16)), // ARMOR_GREEN
144 ((31), (16)), // ARMOR_BLUE
145 ((25), (25)), // SPHERE_BLUE
146 ((25), (25)), // SPHERE_WHITE
147 ((24), (47)), // SUIT
148 ((14), (27)), // OXYGEN
149 ((25), (25)), // INVUL
150 ((62), (24)), // WEAPON_SAW
151 ((63), (12)), // WEAPON_SHOTGUN1
152 ((54), (13)), // WEAPON_SHOTGUN2
153 ((54), (16)), // WEAPON_CHAINGUN
154 ((62), (16)), // WEAPON_ROCKETLAUNCHER
155 ((54), (16)), // WEAPON_PLASMA
156 ((61), (36)), // WEAPON_BFG
157 ((54), (16)), // WEAPON_SUPERPULEMET
158 (( 9), (11)), // AMMO_BULLETS
159 ((28), (16)), // AMMO_BULLETS_BOX
160 ((15), ( 7)), // AMMO_SHELLS
161 ((32), (12)), // AMMO_SHELLS_BOX
162 ((12), (27)), // AMMO_ROCKET
163 ((54), (21)), // AMMO_ROCKET_BOX
164 ((15), (12)), // AMMO_CELL
165 ((32), (21)), // AMMO_CELL_BIG
166 ((22), (29)), // AMMO_BACKPACK
167 ((16), (16)), // KEY_RED
168 ((16), (16)), // KEY_GREEN
169 ((16), (16)), // KEY_BLUE
170 (( 1), ( 1)), // WEAPON_KASTET
171 ((43), (16)), // WEAPON_PISTOL
172 ((14), (18)), // BOTTLE
173 ((16), (15)), // HELMET
174 ((32), (24)), // JETPACK
175 ((25), (25)), // INVIS
176 ((53), (20)), // WEAPON_FLAMETHROWER
177 ((13), (20))); // AMMO_FUELCAN
179 procedure InitTextures();
180 begin
181 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
182 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
183 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
184 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
185 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
186 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
187 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
188 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
189 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
190 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
191 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
192 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
193 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
194 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
195 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
196 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
197 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
198 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
199 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
200 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
201 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
202 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
203 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
204 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
205 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
206 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
207 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
208 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
209 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
210 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID[ITEM_JETPACK]);
211 end;
213 procedure g_Items_LoadData();
214 begin
215 e_WriteLog('Loading items data...', MSG_NOTIFY);
217 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
218 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
219 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
220 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
222 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
223 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
224 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
225 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
226 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
229 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
230 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
231 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
232 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
233 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
234 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
235 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
236 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
237 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
238 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
239 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
240 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
241 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
242 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
243 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
244 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
245 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
246 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
247 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
248 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
249 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
250 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
251 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
252 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
253 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
254 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
255 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
256 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
257 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
258 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
259 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
260 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
261 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
262 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
263 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
264 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK');
266 InitTextures();
268 freeIds := TIdPool.Create();
269 end;
272 procedure g_Items_FreeData();
273 begin
274 e_WriteLog('Releasing items data...', MSG_NOTIFY);
276 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
277 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
278 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
279 g_Sound_Delete('SOUND_ITEM_GETITEM');
281 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
282 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
283 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
284 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
285 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
286 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
287 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
288 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
289 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
290 g_Frames_DeleteByName('FRAMES_FLAG_RED');
291 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
292 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
293 g_Texture_Delete('ITEM_MEDKIT_SMALL');
294 g_Texture_Delete('ITEM_MEDKIT_LARGE');
295 g_Texture_Delete('ITEM_WEAPON_SAW');
296 g_Texture_Delete('ITEM_WEAPON_PISTOL');
297 g_Texture_Delete('ITEM_WEAPON_KASTET');
298 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
299 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
300 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
301 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
302 g_Texture_Delete('ITEM_WEAPON_PLASMA');
303 g_Texture_Delete('ITEM_WEAPON_BFG');
304 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
305 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
306 g_Texture_Delete('ITEM_AMMO_BULLETS');
307 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
308 g_Texture_Delete('ITEM_AMMO_SHELLS');
309 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
310 g_Texture_Delete('ITEM_AMMO_ROCKET');
311 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
312 g_Texture_Delete('ITEM_AMMO_CELL');
313 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
314 g_Texture_Delete('ITEM_AMMO_FUELCAN');
315 g_Texture_Delete('ITEM_AMMO_BACKPACK');
316 g_Texture_Delete('ITEM_KEY_RED');
317 g_Texture_Delete('ITEM_KEY_GREEN');
318 g_Texture_Delete('ITEM_KEY_BLUE');
319 g_Texture_Delete('ITEM_OXYGEN');
320 g_Texture_Delete('ITEM_SUIT');
321 g_Texture_Delete('ITEM_WEAPON_KASTET');
322 g_Texture_Delete('ITEM_MEDKIT_BLACK');
323 g_Texture_Delete('ITEM_JETPACK');
325 freeIds.Free();
326 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 if g_Game_IsServer and (ITEM_RESPAWNTIME = 0) then it.Respawnable := False;
455 it.InitX := X;
456 it.InitY := Y;
457 it.RespawnTime := 0;
458 it.Fall := Fall;
459 it.alive := True;
460 it.QuietRespawn := 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 // Óñòàíîâêà àíèìàöèè
484 case it.ItemType of
485 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
486 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
487 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
488 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
489 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
490 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
491 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
492 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
493 end;
495 it.positionChanged();
497 result := find_id;
498 end;
501 procedure g_Items_Update ();
502 var
503 i, j, k: Integer;
504 ID: DWord;
505 Anim: TAnimation;
506 m: Word;
507 r, nxt: Boolean;
508 begin
509 if (ggItems = nil) then exit;
511 for i := 0 to High(ggItems) do
512 begin
513 if (ggItems[i].ItemType = ITEM_NONE) then continue;
515 with ggItems[i] do
516 begin
517 nxt := False;
519 if alive then
520 begin
521 if Fall then
522 begin
523 m := g_Obj_Move(@Obj, True, True);
524 positionChanged(); // this updates spatial accelerators
526 // Ñîïðîòèâëåíèå âîçäóõà
527 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
529 // Åñëè âûïàë çà êàðòó
530 if WordBool(m and MOVE_FALLOUT) then
531 begin
532 if SpawnTrigger = -1 then
533 begin
534 g_Items_Pick(i);
535 end
536 else
537 begin
538 g_Items_Remove(i);
539 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
540 end;
541 continue;
542 end;
543 end;
545 // Åñëè èãðîêè ïîáëèçîñòè
546 if (gPlayers <> nil) then
547 begin
548 j := Random(Length(gPlayers))-1;
550 for k := 0 to High(gPlayers) do
551 begin
552 Inc(j);
553 if j > High(gPlayers) then j := 0;
555 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
556 begin
557 if g_Game_IsClient then continue;
559 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
561 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
564 Doom 2D: Original:
565 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
566 +2. I_MEGA,I_INVL,I_SUPER
567 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
569 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
571 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
572 if r then
573 begin
574 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
575 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
576 nxt := True;
577 break;
578 end;
579 end;
580 end;
581 end;
583 if nxt then continue;
584 end;
586 if Respawnable and g_Game_IsServer then
587 begin
588 DecMin(RespawnTime, 0);
589 if (RespawnTime = 0) and (not alive) then
590 begin
591 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
593 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
594 begin
595 Anim := TAnimation.Create(ID, False, 4);
596 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
597 Anim.Free();
598 end;
600 Obj.X := InitX;
601 Obj.Y := InitY;
602 Obj.Vel.X := 0;
603 Obj.Vel.Y := 0;
604 Obj.Accel.X := 0;
605 Obj.Accel.Y := 0;
606 positionChanged(); // this updates spatial accelerators
608 alive := true;
610 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
611 QuietRespawn := false;
612 end;
613 end;
615 if (Animation <> nil) then Animation.Update();
616 end;
617 end;
618 end;
621 procedure g_Items_Draw ();
622 var
623 i: Integer;
624 begin
625 if (ggItems = nil) then exit;
627 for i := 0 to High(ggItems) do
628 begin
629 if not ggItems[i].alive then continue;
631 with ggItems[i] do
632 begin
633 if (g_dbg_scale <> 1.0) or g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
634 begin
635 if (Animation = nil) then
636 begin
637 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
638 end
639 else
640 begin
641 Animation.Draw(Obj.X, Obj.Y, M_NONE);
642 end;
644 if g_debug_Frames then
645 begin
646 e_DrawQuad(Obj.X+Obj.Rect.X,
647 Obj.Y+Obj.Rect.Y,
648 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
649 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
650 0, 255, 0);
651 end;
652 end;
653 end;
654 end;
655 end;
658 procedure g_Items_Pick (ID: DWORD);
659 begin
660 ggItems[ID].alive := false;
661 ggItems[ID].RespawnTime := ITEM_RESPAWNTIME;
662 end;
665 procedure g_Items_Remove (ID: DWORD);
666 var
667 it: PItem;
668 trig: Integer;
669 begin
670 if not g_Items_ValidId(ID) then raise Exception.Create('g_Items_Remove: invalid item id');
672 it := @ggItems[ID];
673 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
675 trig := it.SpawnTrigger;
677 releaseItem(ID);
679 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
680 end;
683 procedure g_Items_SaveState (var Mem: TBinMemoryWriter);
684 var
685 count, i: Integer;
686 sig: DWORD;
687 begin
688 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
689 count := 0;
690 if (ggItems <> nil) then
691 begin
692 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) then Inc(count);
693 end;
695 Mem := TBinMemoryWriter.Create((count+1) * 60);
697 // Êîëè÷åñòâî ïðåäìåòîâ
698 Mem.WriteInt(count);
700 if (count = 0) then exit;
702 for i := 0 to High(ggItems) do
703 begin
704 if (ggItems[i].ItemType <> ITEM_NONE) then
705 begin
706 // Ñèãíàòóðà ïðåäìåòà
707 sig := ITEM_SIGNATURE; // 'ITEM'
708 Mem.WriteDWORD(sig);
709 // Òèï ïðåäìåòà
710 Mem.WriteByte(ggItems[i].ItemType);
711 // Åñòü ëè ðåñïàóí
712 Mem.WriteBoolean(ggItems[i].Respawnable);
713 // Êîîðäèíàòû ðåñïóíà
714 Mem.WriteInt(ggItems[i].InitX);
715 Mem.WriteInt(ggItems[i].InitY);
716 // Âðåìÿ äî ðåñïàóíà
717 Mem.WriteWord(ggItems[i].RespawnTime);
718 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
719 Mem.WriteBoolean(ggItems[i].alive);
720 // Ìîæåò ëè îí ïàäàòü
721 Mem.WriteBoolean(ggItems[i].Fall);
722 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
723 Mem.WriteInt(ggItems[i].SpawnTrigger);
724 // Îáúåêò ïðåäìåòà
725 Obj_SaveState(@ggItems[i].Obj, Mem);
726 end;
727 end;
728 end;
731 procedure g_Items_LoadState (var Mem: TBinMemoryReader);
732 var
733 count, i, a: Integer;
734 sig: DWORD;
735 b: Byte;
736 begin
737 if (Mem = nil) then exit;
739 g_Items_Free();
741 // Êîëè÷åñòâî ïðåäìåòîâ
742 Mem.ReadInt(count);
744 if (count = 0) then Exit;
746 for a := 0 to count-1 do
747 begin
748 // Ñèãíàòóðà ïðåäìåòà
749 Mem.ReadDWORD(sig);
750 if (sig <> ITEM_SIGNATURE) then raise EBinSizeError.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
751 // Òèï ïðåäìåòà
752 Mem.ReadByte(b);
753 // Ñîçäàåì ïðåäìåò
754 i := g_Items_Create(0, 0, b, False, False);
755 // Åñòü ëè ðåñïàóí
756 Mem.ReadBoolean(ggItems[i].Respawnable);
757 // Êîîðäèíàòû ðåñïóíà
758 Mem.ReadInt(ggItems[i].InitX);
759 Mem.ReadInt(ggItems[i].InitY);
760 // Âðåìÿ äî ðåñïàóíà
761 Mem.ReadWord(ggItems[i].RespawnTime);
762 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
763 Mem.ReadBoolean(ggItems[i].alive);
764 // Ìîæåò ëè îí ïàäàòü
765 Mem.ReadBoolean(ggItems[i].Fall);
766 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
767 Mem.ReadInt(ggItems[i].SpawnTrigger);
768 // Îáúåêò ïðåäìåòà
769 Obj_LoadState(@ggItems[i].Obj, Mem);
770 end;
771 end;
774 procedure g_Items_RestartRound ();
775 var
776 i: Integer;
777 it: PItem;
778 begin
779 for i := 0 to High(ggItems) do
780 begin
781 it := @ggItems[i];
782 if it.Respawnable then
783 begin
784 it.QuietRespawn := True;
785 it.RespawnTime := 0;
786 end
787 else
788 begin
789 g_Items_Remove(i);
790 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
791 end;
792 end;
793 end;
796 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
797 var
798 idx: Integer;
799 begin
800 result := false;
801 if (ggItems = nil) or not assigned(cb) then exit;
803 if backwards then
804 begin
805 for idx := High(ggItems) downto 0 do
806 begin
807 if ggItems[idx].alive then
808 begin
809 result := cb(@ggItems[idx]);
810 if result then exit;
811 end;
812 end;
813 end
814 else
815 begin
816 for idx := 0 to High(ggItems) do
817 begin
818 if ggItems[idx].alive then
819 begin
820 result := cb(@ggItems[idx]);
821 if result then exit;
822 end;
823 end;
824 end;
825 end;
828 // ////////////////////////////////////////////////////////////////////////// //
829 procedure g_Items_EmitPickupSound (idx: Integer);
830 var
831 it: PItem;
832 begin
833 if not g_Items_ValidId(idx) then exit;
834 it := @ggItems[idx];
835 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
836 end;
838 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
839 var
840 it: PItem;
841 begin
842 if not g_Items_ValidId(idx) then exit;
844 it := @ggItems[idx];
845 if gSoundEffectsDF then
846 begin
847 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
848 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
849 begin
850 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
851 end
852 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
853 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
854 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
855 ITEM_AMMO_BACKPACK] then
856 begin
857 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
858 end
859 else
860 begin
861 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
862 end;
863 end
864 else
865 begin
866 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
867 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
868 begin
869 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
870 end
871 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
872 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
873 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
874 begin
875 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
876 end
877 else
878 begin
879 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
880 end;
881 end;
882 end;
885 procedure g_Items_AddDynLights();
886 var
887 f: Integer;
888 it: PItem;
889 begin
890 for f := 0 to High(ggItems) do
891 begin
892 it := @ggItems[f];
893 if not it.alive then continue;
894 case it.ItemType of
895 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);
896 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);
897 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);
898 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);
899 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);
900 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);
901 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);
902 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);
903 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);
904 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);
905 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);
906 end;
907 end;
908 end;
911 end.