DEADSOFTWARE

render: move item animations into render
[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_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 dropped: Boolean; // dropped from the monster? drops should be rendered after corpses, so zombie corpse will not obscure ammo container, for example
43 NeedSend: Boolean;
45 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
46 procedure getMapBox (out x, y, w, h: Integer); inline;
47 procedure moveBy (dx, dy: Integer); inline;
49 property used: Boolean read slotIsUsed;
50 property myid: Integer read arrIdx;
51 end;
53 procedure g_Items_LoadData();
54 procedure g_Items_FreeData();
55 procedure g_Items_Init();
56 procedure g_Items_Free();
57 function g_Items_Create(X, Y: Integer; ItemType: Byte;
58 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
59 procedure g_Items_SetDrop (ID: DWORD);
60 procedure g_Items_PreUpdate();
61 procedure g_Items_Update();
62 procedure g_Items_Pick(ID: DWORD);
63 procedure g_Items_Remove(ID: DWORD);
64 procedure g_Items_SaveState (st: TStream);
65 procedure g_Items_LoadState (st: TStream);
67 procedure g_Items_RestartRound ();
69 function g_Items_ValidId (idx: Integer): Boolean; inline;
70 function g_Items_ByIdx (idx: Integer): PItem;
71 function g_Items_ObjByIdx (idx: Integer): PObj;
73 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
74 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
76 procedure g_Items_AddDynLights();
79 type
80 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
82 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
83 function g_Items_NextAlive (startIdx: Integer): PItem;
85 var
86 gMaxDist: Integer = 1; // for sounds
88 var (* private state *)
89 ggItems: Array of TItem = nil;
91 implementation
93 uses
94 {$IFDEF ENABLE_GFX}
95 g_gfx,
96 {$ENDIF}
97 Math,
98 g_basic, g_sound, g_map,
99 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
100 e_log, g_options,
101 g_grid, binheap, idpool, utils, xstreams
104 // ////////////////////////////////////////////////////////////////////////// //
105 var
106 freeIds: TIdPool = nil;
109 // ////////////////////////////////////////////////////////////////////////// //
110 function g_Items_ValidId (idx: Integer): Boolean; inline;
111 begin
112 result := false;
113 if (idx < 0) or (idx > High(ggItems)) then exit;
114 if not ggItems[idx].slotIsUsed then exit;
115 result := true;
116 end;
119 function g_Items_ByIdx (idx: Integer): PItem;
120 begin
121 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
122 result := @ggItems[idx];
123 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
124 end;
127 function g_Items_ObjByIdx (idx: Integer): PObj;
128 begin
129 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
130 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
131 result := @ggItems[idx].Obj;
132 end;
135 // ////////////////////////////////////////////////////////////////////////// //
136 procedure TItem.positionChanged ();
137 begin
138 NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
139 end;
141 procedure TItem.getMapBox (out x, y, w, h: Integer); inline;
142 begin
143 x := Obj.X+Obj.Rect.X;
144 y := Obj.Y+Obj.Rect.Y;
145 w := Obj.Rect.Width;
146 h := Obj.Rect.Height;
147 end;
149 procedure TItem.moveBy (dx, dy: Integer); inline;
150 begin
151 if (dx <> 0) or (dy <> 0) then
152 begin
153 Obj.X += dx;
154 Obj.Y += dy;
155 positionChanged();
156 end;
157 end;
159 // ////////////////////////////////////////////////////////////////////////// //
160 const
161 ITEM_SIGNATURE = $4D455449; // 'ITEM'
163 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
164 (((14), (15)), // MEDKIT_SMALL
165 ((28), (19)), // MEDKIT_LARGE
166 ((28), (19)), // MEDKIT_BLACK
167 ((31), (16)), // ARMOR_GREEN
168 ((31), (16)), // ARMOR_BLUE
169 ((25), (25)), // SPHERE_BLUE
170 ((25), (25)), // SPHERE_WHITE
171 ((24), (47)), // SUIT
172 ((14), (27)), // OXYGEN
173 ((25), (25)), // INVUL
174 ((62), (24)), // WEAPON_SAW
175 ((63), (12)), // WEAPON_SHOTGUN1
176 ((54), (13)), // WEAPON_SHOTGUN2
177 ((54), (16)), // WEAPON_CHAINGUN
178 ((62), (16)), // WEAPON_ROCKETLAUNCHER
179 ((54), (16)), // WEAPON_PLASMA
180 ((61), (36)), // WEAPON_BFG
181 ((54), (16)), // WEAPON_SUPERPULEMET
182 (( 9), (11)), // AMMO_BULLETS
183 ((28), (16)), // AMMO_BULLETS_BOX
184 ((15), ( 7)), // AMMO_SHELLS
185 ((32), (12)), // AMMO_SHELLS_BOX
186 ((12), (27)), // AMMO_ROCKET
187 ((54), (21)), // AMMO_ROCKET_BOX
188 ((15), (12)), // AMMO_CELL
189 ((32), (21)), // AMMO_CELL_BIG
190 ((22), (29)), // AMMO_BACKPACK
191 ((16), (16)), // KEY_RED
192 ((16), (16)), // KEY_GREEN
193 ((16), (16)), // KEY_BLUE
194 (( 1), ( 1)), // WEAPON_KASTET
195 ((43), (16)), // WEAPON_PISTOL
196 ((14), (18)), // BOTTLE
197 ((16), (15)), // HELMET
198 ((32), (24)), // JETPACK
199 ((25), (25)), // INVIS
200 ((53), (20)), // WEAPON_FLAMETHROWER
201 ((13), (20))); // AMMO_FUELCAN
203 procedure g_Items_LoadData();
204 begin
205 e_WriteLog('Loading items data...', TMsgType.Notify);
207 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
208 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
209 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
210 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
212 freeIds := TIdPool.Create();
213 end;
216 procedure g_Items_FreeData();
217 begin
218 e_WriteLog('Releasing items data...', TMsgType.Notify);
220 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
221 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
222 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
223 g_Sound_Delete('SOUND_ITEM_GETITEM');
225 freeIds.Free();
226 freeIds := nil;
227 end;
230 procedure releaseItem (idx: Integer);
231 var
232 it: PItem;
233 begin
234 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
235 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
236 it := @ggItems[idx];
237 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
238 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
239 it.slotIsUsed := false;
240 it.alive := False;
241 it.SpawnTrigger := -1;
242 it.ItemType := ITEM_NONE;
243 it.NeedSend := false;
244 freeIds.release(LongWord(idx));
245 end;
248 procedure growItemArrayTo (newsz: Integer);
249 var
250 i, olen: Integer;
251 it: PItem;
252 begin
253 if (newsz < Length(ggItems)) then exit;
254 // no free slots
255 olen := Length(ggItems);
256 SetLength(ggItems, newsz);
257 for i := olen to High(ggItems) do
258 begin
259 it := @ggItems[i];
260 it.slotIsUsed := false;
261 it.arrIdx := i;
262 it.ItemType := ITEM_NONE;
263 it.alive := false;
264 it.SpawnTrigger := -1;
265 it.Respawnable := false;
266 it.NeedSend := false;
267 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
268 end;
269 end;
272 function allocItem (): DWORD;
273 begin
274 result := freeIds.alloc();
275 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
276 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
277 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
278 end;
281 // it will be slow if the slot is free (we have to rebuild the heap)
282 function wantItemSlot (slot: Integer): Integer;
283 var
284 olen: Integer;
285 it: PItem;
286 begin
287 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
288 // do we need to grow item storate?
289 olen := Length(ggItems);
290 if (slot >= olen) then growItemArrayTo(slot+64);
292 it := @ggItems[slot];
293 if not it.slotIsUsed then
294 begin
295 freeIds.alloc(LongWord(slot));
296 end
297 else
298 begin
299 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
300 end;
301 it.slotIsUsed := false;
303 result := slot;
304 end;
307 // ////////////////////////////////////////////////////////////////////////// //
308 procedure g_Items_Init ();
309 var
310 a, b: Integer;
311 begin
312 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
313 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
314 gMaxDist := Trunc(Hypot(a, b));
315 end;
318 procedure g_Items_Free ();
319 begin
320 if (ggItems <> nil) then
321 ggItems := nil;
322 freeIds.clear();
323 end;
326 function g_Items_Create (X, Y: Integer; ItemType: Byte;
327 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
328 var
329 find_id: DWORD;
330 it: PItem;
331 begin
332 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
334 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
336 it := @ggItems[find_id];
338 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
339 //it.arrIdx := find_id;
340 it.slotIsUsed := true;
342 it.ItemType := ItemType;
343 it.Respawnable := Respawnable;
344 it.InitX := X;
345 it.InitY := Y;
346 it.RespawnTime := 0;
347 it.Fall := Fall;
348 it.alive := True;
349 it.QuietRespawn := False;
350 it.dropped := false;
351 it.NeedSend := false;
353 g_Obj_Init(@it.Obj);
354 it.Obj.X := X;
355 it.Obj.Y := Y;
356 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
357 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
359 it.SpawnTrigger := -1;
361 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
362 if AdjCoord then
363 begin
364 with it^ do
365 begin
366 Obj.X := X - (Obj.Rect.Width div 2);
367 Obj.Y := Y - Obj.Rect.Height;
368 InitX := Obj.X;
369 InitY := Obj.Y;
370 end;
371 end;
373 it.Obj.oldX := it.Obj.X;
374 it.Obj.oldY := it.Obj.Y;
376 it.positionChanged();
378 result := find_id;
379 end;
381 procedure g_Items_PreUpdate ();
382 var
383 i: Integer;
384 begin
385 if (ggItems = nil) then Exit;
386 for i := 0 to High(ggItems) do
387 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
388 begin
389 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
390 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
391 end;
392 end;
394 procedure g_Items_Update ();
395 var
396 i, j, k: Integer;
397 m, ItemRespawnTime: Word;
398 r, nxt: Boolean;
399 begin
400 if (ggItems = nil) then exit;
402 // respawn items in 15 seconds regardless of settings during warmup
403 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
405 for i := 0 to High(ggItems) do
406 begin
407 if (ggItems[i].ItemType = ITEM_NONE) then continue;
408 if not ggItems[i].slotIsUsed then continue; // just in case
410 with ggItems[i] do
411 begin
412 nxt := False;
414 if alive then
415 begin
416 if Fall then
417 begin
418 m := g_Obj_Move(@Obj, True, True);
419 positionChanged(); // this updates spatial accelerators
421 // Ñîïðîòèâëåíèå âîçäóõà
422 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
424 // Åñëè âûïàë çà êàðòó
425 if WordBool(m and MOVE_FALLOUT) then
426 begin
427 if SpawnTrigger = -1 then
428 begin
429 g_Items_Pick(i);
430 end
431 else
432 begin
433 g_Items_Remove(i);
434 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
435 end;
436 continue;
437 end;
438 end;
440 // Åñëè èãðîêè ïîáëèçîñòè
441 if (gPlayers <> nil) then
442 begin
443 j := Random(Length(gPlayers))-1;
445 for k := 0 to High(gPlayers) do
446 begin
447 Inc(j);
448 if j > High(gPlayers) then j := 0;
450 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
451 begin
452 if g_Game_IsClient then continue;
454 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
456 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
459 Doom 2D: Original:
460 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
461 +2. I_MEGA,I_INVL,I_SUPER
462 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
464 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
466 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
467 if r then
468 begin
469 if not (Respawnable and (ItemRespawnTime > 0)) then
470 g_Items_Remove(i)
471 else
472 g_Items_Pick(i);
473 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
474 nxt := True;
475 break;
476 end;
477 end;
478 end;
479 end;
481 if nxt then continue;
482 end;
484 if Respawnable and g_Game_IsServer then
485 begin
486 DecMin(RespawnTime, 0);
487 if (RespawnTime = 0) and (not alive) then
488 begin
489 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
490 {$IFDEF ENABLE_GFX}
491 g_GFX_QueueEffect(
492 R_GFX_ITEM_RESPAWN,
493 InitX + (Obj.Rect.Width div 2) - 16,
494 InitY + (Obj.Rect.Height div 2) - 16
495 );
496 {$ENDIF}
497 Obj.oldX := InitX;
498 Obj.oldY := InitY;
499 Obj.X := InitX;
500 Obj.Y := InitY;
501 Obj.Vel.X := 0;
502 Obj.Vel.Y := 0;
503 Obj.Accel.X := 0;
504 Obj.Accel.Y := 0;
505 positionChanged(); // this updates spatial accelerators
507 alive := true;
509 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
510 QuietRespawn := false;
511 end;
512 end;
513 end;
514 end;
515 end;
517 procedure g_Items_SetDrop (ID: DWORD);
518 begin
519 if (ID < Length(ggItems)) then
520 begin
521 ggItems[ID].dropped := true;
522 end;
523 end;
526 procedure g_Items_Pick (ID: DWORD);
527 begin
528 if (ID < Length(ggItems)) then
529 begin
530 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
531 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
532 ggItems[ID].alive := false;
533 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
534 end;
535 end;
538 procedure g_Items_Remove (ID: DWORD);
539 var
540 it: PItem;
541 trig: Integer;
542 begin
543 if not g_Items_ValidId(ID) then
544 begin
545 //writeln('g_Items_Remove: invalid item id: ', ID);
546 raise Exception.Create('g_Items_Remove: invalid item id');
547 //exit;
548 end;
550 it := @ggItems[ID];
551 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
553 it.Obj.oldX := it.Obj.X;
554 it.Obj.oldY := it.Obj.Y;
555 trig := it.SpawnTrigger;
557 releaseItem(ID);
559 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
560 end;
563 procedure g_Items_SaveState (st: TStream);
564 var
565 count, i: Integer;
566 tt: Byte;
567 begin
568 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
569 count := 0;
570 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
572 // Êîëè÷åñòâî ïðåäìåòîâ
573 utils.writeInt(st, LongInt(count));
574 if (count = 0) then exit;
576 for i := 0 to High(ggItems) do
577 begin
578 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
579 begin
580 // Ñèãíàòóðà ïðåäìåòà
581 utils.writeSign(st, 'ITEM');
582 utils.writeInt(st, Byte(0));
583 // Òèï ïðåäìåòà
584 tt := ggItems[i].ItemType;
585 if ggItems[i].dropped then tt := tt or $80;
586 utils.writeInt(st, Byte(tt));
587 // Åñòü ëè ðåñïàóí
588 utils.writeBool(st, ggItems[i].Respawnable);
589 // Êîîðäèíàòû ðåñïóíà
590 utils.writeInt(st, LongInt(ggItems[i].InitX));
591 utils.writeInt(st, LongInt(ggItems[i].InitY));
592 // Âðåìÿ äî ðåñïàóíà
593 utils.writeInt(st, Word(ggItems[i].RespawnTime));
594 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
595 utils.writeBool(st, ggItems[i].alive);
596 // Ìîæåò ëè îí ïàäàòü
597 utils.writeBool(st, ggItems[i].Fall);
598 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
599 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
600 // Îáúåêò ïðåäìåòà
601 Obj_SaveState(st, @ggItems[i].Obj);
602 end;
603 end;
604 end;
607 procedure g_Items_LoadState (st: TStream);
608 var
609 count, i, a: Integer;
610 b: Byte;
611 begin
612 assert(st <> nil);
614 g_Items_Free();
616 // Êîëè÷åñòâî ïðåäìåòîâ
617 count := utils.readLongInt(st);
618 if (count = 0) then exit;
619 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
621 for a := 0 to count-1 do
622 begin
623 // Ñèãíàòóðà ïðåäìåòà
624 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
625 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
626 // Òèï ïðåäìåòà
627 b := utils.readByte(st); // bit7=1: monster drop
628 // Ñîçäàåì ïðåäìåò
629 i := g_Items_Create(0, 0, b and $7F, False, False);
630 if ((b and $80) <> 0) then g_Items_SetDrop(i);
631 // Åñòü ëè ðåñïàóí
632 ggItems[i].Respawnable := utils.readBool(st);
633 // Êîîðäèíàòû ðåñïóíà
634 ggItems[i].InitX := utils.readLongInt(st);
635 ggItems[i].InitY := utils.readLongInt(st);
636 // Âðåìÿ äî ðåñïàóíà
637 ggItems[i].RespawnTime := utils.readWord(st);
638 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
639 ggItems[i].alive := utils.readBool(st);
640 // Ìîæåò ëè îí ïàäàòü
641 ggItems[i].Fall := utils.readBool(st);
642 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
643 ggItems[i].SpawnTrigger := utils.readLongInt(st);
644 // Îáúåêò ïðåäìåòà
645 Obj_LoadState(@ggItems[i].Obj, st);
646 end;
647 end;
650 procedure g_Items_RestartRound ();
651 var
652 i: Integer;
653 it: PItem;
654 begin
655 for i := 0 to High(ggItems) do
656 begin
657 it := @ggItems[i];
658 it.Obj.oldX := it.Obj.X;
659 it.Obj.oldY := it.Obj.Y;
660 if not it.slotIsUsed then continue;
661 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
662 begin
663 it.QuietRespawn := True;
664 it.RespawnTime := 0;
665 end
666 else
667 begin
668 g_Items_Remove(i);
669 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
670 end;
671 end;
672 end;
675 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
676 var
677 idx: Integer;
678 begin
679 result := false;
680 if (ggItems = nil) or not assigned(cb) then exit;
682 if backwards then
683 begin
684 for idx := High(ggItems) downto 0 do
685 begin
686 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
687 begin
688 result := cb(@ggItems[idx]);
689 if result then exit;
690 end;
691 end;
692 end
693 else
694 begin
695 for idx := 0 to High(ggItems) do
696 begin
697 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
698 begin
699 result := cb(@ggItems[idx]);
700 if result then exit;
701 end;
702 end;
703 end;
704 end;
706 function g_Items_NextAlive (startIdx: Integer): PItem;
707 var
708 idx: Integer;
709 begin
710 result := nil;
711 if (ggItems = nil) or (startIdx >= High(ggItems)) then exit;
712 for idx := startIdx + 1 to High(ggItems) do
713 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
714 begin
715 result := @ggItems[idx];
716 exit;
717 end;
718 end;
720 // ////////////////////////////////////////////////////////////////////////// //
721 procedure g_Items_EmitPickupSound (idx: Integer);
722 var
723 it: PItem;
724 begin
725 if not g_Items_ValidId(idx) then exit;
726 it := @ggItems[idx];
727 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
728 end;
730 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
731 var
732 it: PItem;
733 begin
734 if not g_Items_ValidId(idx) then exit;
736 it := @ggItems[idx];
737 if gSoundEffectsDF then
738 begin
739 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
740 ITEM_INVIS, ITEM_MEDKIT_BLACK, 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,
747 ITEM_AMMO_BACKPACK] then
748 begin
749 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
750 end
751 else
752 begin
753 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
754 end;
755 end
756 else
757 begin
758 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
759 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
760 begin
761 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
762 end
763 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
764 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
765 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
766 begin
767 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
768 end
769 else
770 begin
771 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
772 end;
773 end;
774 end;
777 procedure g_Items_AddDynLights();
778 var
779 f: Integer;
780 it: PItem;
781 begin
782 for f := 0 to High(ggItems) do
783 begin
784 it := @ggItems[f];
785 if not it.alive then continue;
786 case it.ItemType of
787 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);
788 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);
789 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);
790 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);
791 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);
792 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);
793 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);
794 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);
795 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);
796 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);
797 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);
798 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);
799 end;
800 end;
801 end;
804 end.