DEADSOFTWARE

render: use TPlayerModel for corpse drawing
[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: TAnimationState;
43 dropped: Boolean; // dropped from the monster? drops should be rendered after corpses, so zombie corpse will not obscure ammo container, for example
45 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
47 property used: Boolean read slotIsUsed;
48 property myid: Integer read arrIdx;
49 end;
51 procedure g_Items_LoadData();
52 procedure g_Items_FreeData();
53 procedure g_Items_Init();
54 procedure g_Items_Free();
55 function g_Items_Create(X, Y: Integer; ItemType: Byte;
56 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
57 procedure g_Items_SetDrop (ID: DWORD);
58 procedure g_Items_PreUpdate();
59 procedure g_Items_Update();
60 procedure g_Items_Pick(ID: DWORD);
61 procedure g_Items_Remove(ID: DWORD);
62 procedure g_Items_SaveState (st: TStream);
63 procedure g_Items_LoadState (st: TStream);
65 procedure g_Items_RestartRound ();
67 function g_Items_ValidId (idx: Integer): Boolean; inline;
68 function g_Items_ByIdx (idx: Integer): PItem;
69 function g_Items_ObjByIdx (idx: Integer): PObj;
71 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
72 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
74 procedure g_Items_AddDynLights();
77 type
78 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
80 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
83 var
84 gMaxDist: Integer = 1; // for sounds
86 var (* private state *)
87 ggItems: Array of TItem = nil;
89 implementation
91 uses
92 Math,
93 g_basic, g_sound, g_gfx, g_map, r_animations, r_gfx,
94 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
95 e_log, g_options,
96 g_grid, binheap, idpool, utils, xstreams;
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 g_Items_LoadData();
180 begin
181 e_WriteLog('Loading items data...', TMsgType.Notify);
183 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
184 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
185 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
186 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
188 freeIds := TIdPool.Create();
189 end;
192 procedure g_Items_FreeData();
193 begin
194 e_WriteLog('Releasing items data...', TMsgType.Notify);
196 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
197 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
198 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
199 g_Sound_Delete('SOUND_ITEM_GETITEM');
201 freeIds.Free();
202 freeIds := nil;
203 end;
206 procedure releaseItem (idx: Integer);
207 var
208 it: PItem;
209 begin
210 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
211 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
212 it := @ggItems[idx];
213 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
214 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
215 it.slotIsUsed := false;
216 if (it.Animation <> nil) then
217 begin
218 it.Animation.Free();
219 it.Animation := nil;
220 end;
221 it.alive := False;
222 it.SpawnTrigger := -1;
223 it.ItemType := ITEM_NONE;
224 freeIds.release(LongWord(idx));
225 end;
228 procedure growItemArrayTo (newsz: Integer);
229 var
230 i, olen: Integer;
231 it: PItem;
232 begin
233 if (newsz < Length(ggItems)) then exit;
234 // no free slots
235 olen := Length(ggItems);
236 SetLength(ggItems, newsz);
237 for i := olen to High(ggItems) do
238 begin
239 it := @ggItems[i];
240 it.slotIsUsed := false;
241 it.arrIdx := i;
242 it.ItemType := ITEM_NONE;
243 it.Animation := nil;
244 it.alive := false;
245 it.SpawnTrigger := -1;
246 it.Respawnable := false;
247 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
248 end;
249 end;
252 function allocItem (): DWORD;
253 begin
254 result := freeIds.alloc();
255 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
256 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
257 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
258 end;
261 // it will be slow if the slot is free (we have to rebuild the heap)
262 function wantItemSlot (slot: Integer): Integer;
263 var
264 olen: Integer;
265 it: PItem;
266 begin
267 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
268 // do we need to grow item storate?
269 olen := Length(ggItems);
270 if (slot >= olen) then growItemArrayTo(slot+64);
272 it := @ggItems[slot];
273 if not it.slotIsUsed then
274 begin
275 freeIds.alloc(LongWord(slot));
276 end
277 else
278 begin
279 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
280 end;
281 it.slotIsUsed := false;
283 result := slot;
284 end;
287 // ////////////////////////////////////////////////////////////////////////// //
288 procedure g_Items_Init ();
289 var
290 a, b: Integer;
291 begin
292 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
293 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
294 gMaxDist := Trunc(Hypot(a, b));
295 end;
298 procedure g_Items_Free ();
299 var
300 i: Integer;
301 begin
302 if (ggItems <> nil) then
303 begin
304 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
305 ggItems := nil;
306 end;
307 freeIds.clear();
308 end;
311 function g_Items_Create (X, Y: Integer; ItemType: Byte;
312 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
313 var
314 find_id: DWORD;
315 it: PItem;
316 begin
317 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
319 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
321 it := @ggItems[find_id];
323 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
324 //it.arrIdx := find_id;
325 it.slotIsUsed := true;
327 it.ItemType := ItemType;
328 it.Respawnable := Respawnable;
329 it.InitX := X;
330 it.InitY := Y;
331 it.RespawnTime := 0;
332 it.Fall := Fall;
333 it.alive := True;
334 it.QuietRespawn := False;
335 it.dropped := false;
337 g_Obj_Init(@it.Obj);
338 it.Obj.X := X;
339 it.Obj.Y := Y;
340 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
341 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
343 it.Animation := nil;
344 it.SpawnTrigger := -1;
346 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
347 if AdjCoord then
348 begin
349 with it^ do
350 begin
351 Obj.X := X - (Obj.Rect.Width div 2);
352 Obj.Y := Y - Obj.Rect.Height;
353 InitX := Obj.X;
354 InitY := Obj.Y;
355 end;
356 end;
358 it.Obj.oldX := it.Obj.X;
359 it.Obj.oldY := it.Obj.Y;
361 // Óñòàíîâêà àíèìàöèè
362 case it.ItemType of
363 ITEM_ARMOR_GREEN: it.Animation := TAnimationState.Create(True, 20, 3);
364 ITEM_ARMOR_BLUE: it.Animation := TAnimationState.Create(True, 20, 3);
365 ITEM_JETPACK: it.Animation := TAnimationState.Create(True, 15, 3);
366 ITEM_SPHERE_BLUE: it.Animation := TAnimationState.Create(True, 15, 4);
367 ITEM_SPHERE_WHITE: it.Animation := TAnimationState.Create(True, 20, 4);
368 ITEM_INVUL: it.Animation := TAnimationState.Create(True, 20, 4);
369 ITEM_INVIS: it.Animation := TAnimationState.Create(True, 20, 4);
370 ITEM_BOTTLE: it.Animation := TAnimationState.Create(True, 20, 4);
371 ITEM_HELMET: it.Animation := TAnimationState.Create(True, 20, 4);
372 end;
374 it.positionChanged();
376 result := find_id;
377 end;
379 procedure g_Items_PreUpdate ();
380 var
381 i: Integer;
382 begin
383 if (ggItems = nil) then Exit;
384 for i := 0 to High(ggItems) do
385 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
386 begin
387 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
388 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
389 end;
390 end;
392 procedure g_Items_Update ();
393 var
394 i, j, k: Integer;
395 m, ItemRespawnTime: Word;
396 r, nxt: Boolean;
397 begin
398 if (ggItems = nil) then exit;
400 // respawn items in 15 seconds regardless of settings during warmup
401 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
403 for i := 0 to High(ggItems) do
404 begin
405 if (ggItems[i].ItemType = ITEM_NONE) then continue;
406 if not ggItems[i].slotIsUsed then continue; // just in case
408 with ggItems[i] do
409 begin
410 nxt := False;
412 if alive then
413 begin
414 if Fall then
415 begin
416 m := g_Obj_Move(@Obj, True, True);
417 positionChanged(); // this updates spatial accelerators
419 // Ñîïðîòèâëåíèå âîçäóõà
420 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
422 // Åñëè âûïàë çà êàðòó
423 if WordBool(m and MOVE_FALLOUT) then
424 begin
425 if SpawnTrigger = -1 then
426 begin
427 g_Items_Pick(i);
428 end
429 else
430 begin
431 g_Items_Remove(i);
432 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
433 end;
434 continue;
435 end;
436 end;
438 // Åñëè èãðîêè ïîáëèçîñòè
439 if (gPlayers <> nil) then
440 begin
441 j := Random(Length(gPlayers))-1;
443 for k := 0 to High(gPlayers) do
444 begin
445 Inc(j);
446 if j > High(gPlayers) then j := 0;
448 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
449 begin
450 if g_Game_IsClient then continue;
452 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
454 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
457 Doom 2D: Original:
458 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
459 +2. I_MEGA,I_INVL,I_SUPER
460 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
462 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
464 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
465 if r then
466 begin
467 if not (Respawnable and (ItemRespawnTime > 0)) then
468 g_Items_Remove(i)
469 else
470 g_Items_Pick(i);
471 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
472 nxt := True;
473 break;
474 end;
475 end;
476 end;
477 end;
479 if nxt then continue;
480 end;
482 if Respawnable and g_Game_IsServer then
483 begin
484 DecMin(RespawnTime, 0);
485 if (RespawnTime = 0) and (not alive) then
486 begin
487 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
488 r_GFX_OnceAnim(R_GFX_ITEM_RESPAWN, InitX + (Obj.Rect.Width div 2) - 16, InitY + (Obj.Rect.Height div 2) - 16);
489 Obj.oldX := InitX;
490 Obj.oldY := InitY;
491 Obj.X := InitX;
492 Obj.Y := InitY;
493 Obj.Vel.X := 0;
494 Obj.Vel.Y := 0;
495 Obj.Accel.X := 0;
496 Obj.Accel.Y := 0;
497 positionChanged(); // this updates spatial accelerators
499 alive := true;
501 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
502 QuietRespawn := false;
503 end;
504 end;
506 if (Animation <> nil) then Animation.Update();
507 end;
508 end;
509 end;
511 procedure g_Items_SetDrop (ID: DWORD);
512 begin
513 if (ID < Length(ggItems)) then
514 begin
515 ggItems[ID].dropped := true;
516 end;
517 end;
520 procedure g_Items_Pick (ID: DWORD);
521 begin
522 if (ID < Length(ggItems)) then
523 begin
524 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
525 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
526 ggItems[ID].alive := false;
527 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
528 end;
529 end;
532 procedure g_Items_Remove (ID: DWORD);
533 var
534 it: PItem;
535 trig: Integer;
536 begin
537 if not g_Items_ValidId(ID) then
538 begin
539 //writeln('g_Items_Remove: invalid item id: ', ID);
540 raise Exception.Create('g_Items_Remove: invalid item id');
541 //exit;
542 end;
544 it := @ggItems[ID];
545 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
547 it.Obj.oldX := it.Obj.X;
548 it.Obj.oldY := it.Obj.Y;
549 trig := it.SpawnTrigger;
551 releaseItem(ID);
553 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
554 end;
557 procedure g_Items_SaveState (st: TStream);
558 var
559 count, i: Integer;
560 tt: Byte;
561 begin
562 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
563 count := 0;
564 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
566 // Êîëè÷åñòâî ïðåäìåòîâ
567 utils.writeInt(st, LongInt(count));
568 if (count = 0) then exit;
570 for i := 0 to High(ggItems) do
571 begin
572 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
573 begin
574 // Ñèãíàòóðà ïðåäìåòà
575 utils.writeSign(st, 'ITEM');
576 utils.writeInt(st, Byte(0));
577 // Òèï ïðåäìåòà
578 tt := ggItems[i].ItemType;
579 if ggItems[i].dropped then tt := tt or $80;
580 utils.writeInt(st, Byte(tt));
581 // Åñòü ëè ðåñïàóí
582 utils.writeBool(st, ggItems[i].Respawnable);
583 // Êîîðäèíàòû ðåñïóíà
584 utils.writeInt(st, LongInt(ggItems[i].InitX));
585 utils.writeInt(st, LongInt(ggItems[i].InitY));
586 // Âðåìÿ äî ðåñïàóíà
587 utils.writeInt(st, Word(ggItems[i].RespawnTime));
588 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
589 utils.writeBool(st, ggItems[i].alive);
590 // Ìîæåò ëè îí ïàäàòü
591 utils.writeBool(st, ggItems[i].Fall);
592 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
593 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
594 // Îáúåêò ïðåäìåòà
595 Obj_SaveState(st, @ggItems[i].Obj);
596 end;
597 end;
598 end;
601 procedure g_Items_LoadState (st: TStream);
602 var
603 count, i, a: Integer;
604 b: Byte;
605 begin
606 assert(st <> nil);
608 g_Items_Free();
610 // Êîëè÷åñòâî ïðåäìåòîâ
611 count := utils.readLongInt(st);
612 if (count = 0) then exit;
613 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
615 for a := 0 to count-1 do
616 begin
617 // Ñèãíàòóðà ïðåäìåòà
618 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
619 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
620 // Òèï ïðåäìåòà
621 b := utils.readByte(st); // bit7=1: monster drop
622 // Ñîçäàåì ïðåäìåò
623 i := g_Items_Create(0, 0, b and $7F, False, False);
624 if ((b and $80) <> 0) then g_Items_SetDrop(i);
625 // Åñòü ëè ðåñïàóí
626 ggItems[i].Respawnable := utils.readBool(st);
627 // Êîîðäèíàòû ðåñïóíà
628 ggItems[i].InitX := utils.readLongInt(st);
629 ggItems[i].InitY := utils.readLongInt(st);
630 // Âðåìÿ äî ðåñïàóíà
631 ggItems[i].RespawnTime := utils.readWord(st);
632 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
633 ggItems[i].alive := utils.readBool(st);
634 // Ìîæåò ëè îí ïàäàòü
635 ggItems[i].Fall := utils.readBool(st);
636 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
637 ggItems[i].SpawnTrigger := utils.readLongInt(st);
638 // Îáúåêò ïðåäìåòà
639 Obj_LoadState(@ggItems[i].Obj, st);
640 end;
641 end;
644 procedure g_Items_RestartRound ();
645 var
646 i: Integer;
647 it: PItem;
648 begin
649 for i := 0 to High(ggItems) do
650 begin
651 it := @ggItems[i];
652 it.Obj.oldX := it.Obj.X;
653 it.Obj.oldY := it.Obj.Y;
654 if not it.slotIsUsed then continue;
655 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
656 begin
657 it.QuietRespawn := True;
658 it.RespawnTime := 0;
659 end
660 else
661 begin
662 g_Items_Remove(i);
663 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
664 end;
665 end;
666 end;
669 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
670 var
671 idx: Integer;
672 begin
673 result := false;
674 if (ggItems = nil) or not assigned(cb) then exit;
676 if backwards then
677 begin
678 for idx := High(ggItems) downto 0 do
679 begin
680 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
681 begin
682 result := cb(@ggItems[idx]);
683 if result then exit;
684 end;
685 end;
686 end
687 else
688 begin
689 for idx := 0 to High(ggItems) do
690 begin
691 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
692 begin
693 result := cb(@ggItems[idx]);
694 if result then exit;
695 end;
696 end;
697 end;
698 end;
701 // ////////////////////////////////////////////////////////////////////////// //
702 procedure g_Items_EmitPickupSound (idx: Integer);
703 var
704 it: PItem;
705 begin
706 if not g_Items_ValidId(idx) then exit;
707 it := @ggItems[idx];
708 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
709 end;
711 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
712 var
713 it: PItem;
714 begin
715 if not g_Items_ValidId(idx) then exit;
717 it := @ggItems[idx];
718 if gSoundEffectsDF then
719 begin
720 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
721 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
722 begin
723 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
724 end
725 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
726 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
727 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
728 ITEM_AMMO_BACKPACK] then
729 begin
730 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
731 end
732 else
733 begin
734 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
735 end;
736 end
737 else
738 begin
739 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
740 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
741 begin
742 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
743 end
744 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
745 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
746 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
747 begin
748 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
749 end
750 else
751 begin
752 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
753 end;
754 end;
755 end;
758 procedure g_Items_AddDynLights();
759 var
760 f: Integer;
761 it: PItem;
762 begin
763 for f := 0 to High(ggItems) do
764 begin
765 it := @ggItems[f];
766 if not it.alive then continue;
767 case it.ItemType of
768 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);
769 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);
770 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);
771 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);
772 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);
773 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);
774 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);
775 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);
776 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);
777 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);
778 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);
779 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);
780 end;
781 end;
782 end;
785 end.