DEADSOFTWARE

1ee3fec1b60dd6f7490401081245f37770940e60
[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
44 NeedSend: Boolean;
46 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
47 procedure getMapBox (out x, y, w, h: Integer); inline;
48 procedure moveBy (dx, dy: Integer); inline;
50 property used: Boolean read slotIsUsed;
51 property myid: Integer read arrIdx;
52 end;
54 procedure g_Items_LoadData();
55 procedure g_Items_FreeData();
56 procedure g_Items_Init();
57 procedure g_Items_Free();
58 function g_Items_Create(X, Y: Integer; ItemType: Byte;
59 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
60 procedure g_Items_SetDrop (ID: DWORD);
61 procedure g_Items_PreUpdate();
62 procedure g_Items_Update();
63 procedure g_Items_Pick(ID: DWORD);
64 procedure g_Items_Remove(ID: DWORD);
65 procedure g_Items_SaveState (st: TStream);
66 procedure g_Items_LoadState (st: TStream);
68 procedure g_Items_RestartRound ();
70 function g_Items_ValidId (idx: Integer): Boolean; inline;
71 function g_Items_ByIdx (idx: Integer): PItem;
72 function g_Items_ObjByIdx (idx: Integer): PObj;
74 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
75 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
77 procedure g_Items_AddDynLights();
80 type
81 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
83 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
84 function g_Items_NextAlive (startIdx: Integer): PItem;
86 var
87 gMaxDist: Integer = 1; // for sounds
89 var (* private state *)
90 ggItems: Array of TItem = nil;
92 implementation
94 uses
95 Math,
96 g_basic, g_sound, g_gfx, g_map, r_textures, r_animations,
97 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
98 e_log, g_options,
99 g_grid, binheap, idpool, utils, xstreams;
101 // ////////////////////////////////////////////////////////////////////////// //
102 var
103 freeIds: TIdPool = nil;
106 // ////////////////////////////////////////////////////////////////////////// //
107 function g_Items_ValidId (idx: Integer): Boolean; inline;
108 begin
109 result := false;
110 if (idx < 0) or (idx > High(ggItems)) then exit;
111 if not ggItems[idx].slotIsUsed then exit;
112 result := true;
113 end;
116 function g_Items_ByIdx (idx: Integer): PItem;
117 begin
118 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
119 result := @ggItems[idx];
120 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
121 end;
124 function g_Items_ObjByIdx (idx: Integer): PObj;
125 begin
126 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
127 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
128 result := @ggItems[idx].Obj;
129 end;
132 // ////////////////////////////////////////////////////////////////////////// //
133 procedure TItem.positionChanged ();
134 begin
135 NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
136 end;
138 procedure TItem.getMapBox (out x, y, w, h: Integer); inline;
139 begin
140 x := Obj.X+Obj.Rect.X;
141 y := Obj.Y+Obj.Rect.Y;
142 w := Obj.Rect.Width;
143 h := Obj.Rect.Height;
144 end;
146 procedure TItem.moveBy (dx, dy: Integer); inline;
147 begin
148 if (dx <> 0) or (dy <> 0) then
149 begin
150 Obj.X += dx;
151 Obj.Y += dy;
152 positionChanged();
153 end;
154 end;
156 // ////////////////////////////////////////////////////////////////////////// //
157 const
158 ITEM_SIGNATURE = $4D455449; // 'ITEM'
160 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
161 (((14), (15)), // MEDKIT_SMALL
162 ((28), (19)), // MEDKIT_LARGE
163 ((28), (19)), // MEDKIT_BLACK
164 ((31), (16)), // ARMOR_GREEN
165 ((31), (16)), // ARMOR_BLUE
166 ((25), (25)), // SPHERE_BLUE
167 ((25), (25)), // SPHERE_WHITE
168 ((24), (47)), // SUIT
169 ((14), (27)), // OXYGEN
170 ((25), (25)), // INVUL
171 ((62), (24)), // WEAPON_SAW
172 ((63), (12)), // WEAPON_SHOTGUN1
173 ((54), (13)), // WEAPON_SHOTGUN2
174 ((54), (16)), // WEAPON_CHAINGUN
175 ((62), (16)), // WEAPON_ROCKETLAUNCHER
176 ((54), (16)), // WEAPON_PLASMA
177 ((61), (36)), // WEAPON_BFG
178 ((54), (16)), // WEAPON_SUPERPULEMET
179 (( 9), (11)), // AMMO_BULLETS
180 ((28), (16)), // AMMO_BULLETS_BOX
181 ((15), ( 7)), // AMMO_SHELLS
182 ((32), (12)), // AMMO_SHELLS_BOX
183 ((12), (27)), // AMMO_ROCKET
184 ((54), (21)), // AMMO_ROCKET_BOX
185 ((15), (12)), // AMMO_CELL
186 ((32), (21)), // AMMO_CELL_BIG
187 ((22), (29)), // AMMO_BACKPACK
188 ((16), (16)), // KEY_RED
189 ((16), (16)), // KEY_GREEN
190 ((16), (16)), // KEY_BLUE
191 (( 1), ( 1)), // WEAPON_KASTET
192 ((43), (16)), // WEAPON_PISTOL
193 ((14), (18)), // BOTTLE
194 ((16), (15)), // HELMET
195 ((32), (24)), // JETPACK
196 ((25), (25)), // INVIS
197 ((53), (20)), // WEAPON_FLAMETHROWER
198 ((13), (20))); // AMMO_FUELCAN
200 procedure g_Items_LoadData();
201 begin
202 e_WriteLog('Loading items data...', TMsgType.Notify);
204 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
205 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
206 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
207 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
209 freeIds := TIdPool.Create();
210 end;
213 procedure g_Items_FreeData();
214 begin
215 e_WriteLog('Releasing items data...', TMsgType.Notify);
217 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
218 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
219 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
220 g_Sound_Delete('SOUND_ITEM_GETITEM');
222 freeIds.Free();
223 freeIds := nil;
224 end;
227 procedure releaseItem (idx: Integer);
228 var
229 it: PItem;
230 begin
231 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
232 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
233 it := @ggItems[idx];
234 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
235 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
236 it.slotIsUsed := false;
237 if (it.Animation <> nil) then
238 begin
239 it.Animation.Free();
240 it.Animation := nil;
241 end;
242 it.alive := False;
243 it.SpawnTrigger := -1;
244 it.ItemType := ITEM_NONE;
245 it.NeedSend := false;
246 freeIds.release(LongWord(idx));
247 end;
250 procedure growItemArrayTo (newsz: Integer);
251 var
252 i, olen: Integer;
253 it: PItem;
254 begin
255 if (newsz < Length(ggItems)) then exit;
256 // no free slots
257 olen := Length(ggItems);
258 SetLength(ggItems, newsz);
259 for i := olen to High(ggItems) do
260 begin
261 it := @ggItems[i];
262 it.slotIsUsed := false;
263 it.arrIdx := i;
264 it.ItemType := ITEM_NONE;
265 it.Animation := nil;
266 it.alive := false;
267 it.SpawnTrigger := -1;
268 it.Respawnable := false;
269 it.NeedSend := false;
270 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
271 end;
272 end;
275 function allocItem (): DWORD;
276 begin
277 result := freeIds.alloc();
278 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
279 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
280 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
281 end;
284 // it will be slow if the slot is free (we have to rebuild the heap)
285 function wantItemSlot (slot: Integer): Integer;
286 var
287 olen: Integer;
288 it: PItem;
289 begin
290 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
291 // do we need to grow item storate?
292 olen := Length(ggItems);
293 if (slot >= olen) then growItemArrayTo(slot+64);
295 it := @ggItems[slot];
296 if not it.slotIsUsed then
297 begin
298 freeIds.alloc(LongWord(slot));
299 end
300 else
301 begin
302 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
303 end;
304 it.slotIsUsed := false;
306 result := slot;
307 end;
310 // ////////////////////////////////////////////////////////////////////////// //
311 procedure g_Items_Init ();
312 var
313 a, b: Integer;
314 begin
315 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
316 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
317 gMaxDist := Trunc(Hypot(a, b));
318 end;
321 procedure g_Items_Free ();
322 var
323 i: Integer;
324 begin
325 if (ggItems <> nil) then
326 begin
327 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
328 ggItems := nil;
329 end;
330 freeIds.clear();
331 end;
334 function g_Items_Create (X, Y: Integer; ItemType: Byte;
335 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
336 var
337 find_id: DWORD;
338 it: PItem;
339 begin
340 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
342 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
344 it := @ggItems[find_id];
346 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
347 //it.arrIdx := find_id;
348 it.slotIsUsed := true;
350 it.ItemType := ItemType;
351 it.Respawnable := Respawnable;
352 it.InitX := X;
353 it.InitY := Y;
354 it.RespawnTime := 0;
355 it.Fall := Fall;
356 it.alive := True;
357 it.QuietRespawn := False;
358 it.dropped := false;
359 it.NeedSend := false;
361 g_Obj_Init(@it.Obj);
362 it.Obj.X := X;
363 it.Obj.Y := Y;
364 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
365 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
367 it.Animation := nil;
368 it.SpawnTrigger := -1;
370 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
371 if AdjCoord then
372 begin
373 with it^ do
374 begin
375 Obj.X := X - (Obj.Rect.Width div 2);
376 Obj.Y := Y - Obj.Rect.Height;
377 InitX := Obj.X;
378 InitY := Obj.Y;
379 end;
380 end;
382 it.Obj.oldX := it.Obj.X;
383 it.Obj.oldY := it.Obj.Y;
385 // Óñòàíîâêà àíèìàöèè
386 case it.ItemType of
387 ITEM_ARMOR_GREEN: it.Animation := TAnimationState.Create(True, 20, 3);
388 ITEM_ARMOR_BLUE: it.Animation := TAnimationState.Create(True, 20, 3);
389 ITEM_JETPACK: it.Animation := TAnimationState.Create(True, 15, 3);
390 ITEM_SPHERE_BLUE: it.Animation := TAnimationState.Create(True, 15, 4);
391 ITEM_SPHERE_WHITE: it.Animation := TAnimationState.Create(True, 20, 4);
392 ITEM_INVUL: it.Animation := TAnimationState.Create(True, 20, 4);
393 ITEM_INVIS: it.Animation := TAnimationState.Create(True, 20, 4);
394 ITEM_BOTTLE: it.Animation := TAnimationState.Create(True, 20, 4);
395 ITEM_HELMET: it.Animation := TAnimationState.Create(True, 20, 4);
396 end;
398 it.positionChanged();
400 result := find_id;
401 end;
403 procedure g_Items_PreUpdate ();
404 var
405 i: Integer;
406 begin
407 if (ggItems = nil) then Exit;
408 for i := 0 to High(ggItems) do
409 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
410 begin
411 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
412 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
413 end;
414 end;
416 procedure g_Items_Update ();
417 var
418 i, j, k: Integer;
419 ID: DWord;
420 Anim: TAnimation;
421 m, ItemRespawnTime: Word;
422 r, nxt: Boolean;
423 begin
424 if (ggItems = nil) then exit;
426 // respawn items in 15 seconds regardless of settings during warmup
427 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
429 for i := 0 to High(ggItems) do
430 begin
431 if (ggItems[i].ItemType = ITEM_NONE) then continue;
432 if not ggItems[i].slotIsUsed then continue; // just in case
434 with ggItems[i] do
435 begin
436 nxt := False;
438 if alive then
439 begin
440 if Fall then
441 begin
442 m := g_Obj_Move(@Obj, True, True);
443 positionChanged(); // this updates spatial accelerators
445 // Ñîïðîòèâëåíèå âîçäóõà
446 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
448 // Åñëè âûïàë çà êàðòó
449 if WordBool(m and MOVE_FALLOUT) then
450 begin
451 if SpawnTrigger = -1 then
452 begin
453 g_Items_Pick(i);
454 end
455 else
456 begin
457 g_Items_Remove(i);
458 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
459 end;
460 continue;
461 end;
462 end;
464 // Åñëè èãðîêè ïîáëèçîñòè
465 if (gPlayers <> nil) then
466 begin
467 j := Random(Length(gPlayers))-1;
469 for k := 0 to High(gPlayers) do
470 begin
471 Inc(j);
472 if j > High(gPlayers) then j := 0;
474 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
475 begin
476 if g_Game_IsClient then continue;
478 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
480 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
483 Doom 2D: Original:
484 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
485 +2. I_MEGA,I_INVL,I_SUPER
486 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
488 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
490 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
491 if r then
492 begin
493 if not (Respawnable and (ItemRespawnTime > 0)) then
494 g_Items_Remove(i)
495 else
496 g_Items_Pick(i);
497 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
498 nxt := True;
499 break;
500 end;
501 end;
502 end;
503 end;
505 if nxt then continue;
506 end;
508 if Respawnable and g_Game_IsServer then
509 begin
510 DecMin(RespawnTime, 0);
511 if (RespawnTime = 0) and (not alive) then
512 begin
513 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
515 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
516 begin
517 Anim := TAnimation.Create(ID, False, 4);
518 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
519 Anim.Free();
520 end;
522 Obj.oldX := InitX;
523 Obj.oldY := InitY;
524 Obj.X := InitX;
525 Obj.Y := InitY;
526 Obj.Vel.X := 0;
527 Obj.Vel.Y := 0;
528 Obj.Accel.X := 0;
529 Obj.Accel.Y := 0;
530 positionChanged(); // this updates spatial accelerators
532 alive := true;
534 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
535 QuietRespawn := false;
536 end;
537 end;
539 if (Animation <> nil) then Animation.Update();
540 end;
541 end;
542 end;
544 procedure g_Items_SetDrop (ID: DWORD);
545 begin
546 if (ID < Length(ggItems)) then
547 begin
548 ggItems[ID].dropped := true;
549 end;
550 end;
553 procedure g_Items_Pick (ID: DWORD);
554 begin
555 if (ID < Length(ggItems)) then
556 begin
557 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
558 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
559 ggItems[ID].alive := false;
560 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
561 end;
562 end;
565 procedure g_Items_Remove (ID: DWORD);
566 var
567 it: PItem;
568 trig: Integer;
569 begin
570 if not g_Items_ValidId(ID) then
571 begin
572 //writeln('g_Items_Remove: invalid item id: ', ID);
573 raise Exception.Create('g_Items_Remove: invalid item id');
574 //exit;
575 end;
577 it := @ggItems[ID];
578 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
580 it.Obj.oldX := it.Obj.X;
581 it.Obj.oldY := it.Obj.Y;
582 trig := it.SpawnTrigger;
584 releaseItem(ID);
586 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
587 end;
590 procedure g_Items_SaveState (st: TStream);
591 var
592 count, i: Integer;
593 tt: Byte;
594 begin
595 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
596 count := 0;
597 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
599 // Êîëè÷åñòâî ïðåäìåòîâ
600 utils.writeInt(st, LongInt(count));
601 if (count = 0) then exit;
603 for i := 0 to High(ggItems) do
604 begin
605 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
606 begin
607 // Ñèãíàòóðà ïðåäìåòà
608 utils.writeSign(st, 'ITEM');
609 utils.writeInt(st, Byte(0));
610 // Òèï ïðåäìåòà
611 tt := ggItems[i].ItemType;
612 if ggItems[i].dropped then tt := tt or $80;
613 utils.writeInt(st, Byte(tt));
614 // Åñòü ëè ðåñïàóí
615 utils.writeBool(st, ggItems[i].Respawnable);
616 // Êîîðäèíàòû ðåñïóíà
617 utils.writeInt(st, LongInt(ggItems[i].InitX));
618 utils.writeInt(st, LongInt(ggItems[i].InitY));
619 // Âðåìÿ äî ðåñïàóíà
620 utils.writeInt(st, Word(ggItems[i].RespawnTime));
621 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
622 utils.writeBool(st, ggItems[i].alive);
623 // Ìîæåò ëè îí ïàäàòü
624 utils.writeBool(st, ggItems[i].Fall);
625 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
626 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
627 // Îáúåêò ïðåäìåòà
628 Obj_SaveState(st, @ggItems[i].Obj);
629 end;
630 end;
631 end;
634 procedure g_Items_LoadState (st: TStream);
635 var
636 count, i, a: Integer;
637 b: Byte;
638 begin
639 assert(st <> nil);
641 g_Items_Free();
643 // Êîëè÷åñòâî ïðåäìåòîâ
644 count := utils.readLongInt(st);
645 if (count = 0) then exit;
646 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
648 for a := 0 to count-1 do
649 begin
650 // Ñèãíàòóðà ïðåäìåòà
651 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
652 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
653 // Òèï ïðåäìåòà
654 b := utils.readByte(st); // bit7=1: monster drop
655 // Ñîçäàåì ïðåäìåò
656 i := g_Items_Create(0, 0, b and $7F, False, False);
657 if ((b and $80) <> 0) then g_Items_SetDrop(i);
658 // Åñòü ëè ðåñïàóí
659 ggItems[i].Respawnable := utils.readBool(st);
660 // Êîîðäèíàòû ðåñïóíà
661 ggItems[i].InitX := utils.readLongInt(st);
662 ggItems[i].InitY := utils.readLongInt(st);
663 // Âðåìÿ äî ðåñïàóíà
664 ggItems[i].RespawnTime := utils.readWord(st);
665 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
666 ggItems[i].alive := utils.readBool(st);
667 // Ìîæåò ëè îí ïàäàòü
668 ggItems[i].Fall := utils.readBool(st);
669 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
670 ggItems[i].SpawnTrigger := utils.readLongInt(st);
671 // Îáúåêò ïðåäìåòà
672 Obj_LoadState(@ggItems[i].Obj, st);
673 end;
674 end;
677 procedure g_Items_RestartRound ();
678 var
679 i: Integer;
680 it: PItem;
681 begin
682 for i := 0 to High(ggItems) do
683 begin
684 it := @ggItems[i];
685 it.Obj.oldX := it.Obj.X;
686 it.Obj.oldY := it.Obj.Y;
687 if not it.slotIsUsed then continue;
688 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
689 begin
690 it.QuietRespawn := True;
691 it.RespawnTime := 0;
692 end
693 else
694 begin
695 g_Items_Remove(i);
696 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
697 end;
698 end;
699 end;
702 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
703 var
704 idx: Integer;
705 begin
706 result := false;
707 if (ggItems = nil) or not assigned(cb) then exit;
709 if backwards then
710 begin
711 for idx := High(ggItems) downto 0 do
712 begin
713 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
714 begin
715 result := cb(@ggItems[idx]);
716 if result then exit;
717 end;
718 end;
719 end
720 else
721 begin
722 for idx := 0 to High(ggItems) do
723 begin
724 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
725 begin
726 result := cb(@ggItems[idx]);
727 if result then exit;
728 end;
729 end;
730 end;
731 end;
733 function g_Items_NextAlive (startIdx: Integer): PItem;
734 var
735 idx: Integer;
736 begin
737 result := nil;
738 if (ggItems = nil) or (startIdx >= High(ggItems)) then exit;
739 for idx := startIdx + 1 to High(ggItems) do
740 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
741 begin
742 result := @ggItems[idx];
743 exit;
744 end;
745 end;
747 // ////////////////////////////////////////////////////////////////////////// //
748 procedure g_Items_EmitPickupSound (idx: Integer);
749 var
750 it: PItem;
751 begin
752 if not g_Items_ValidId(idx) then exit;
753 it := @ggItems[idx];
754 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
755 end;
757 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
758 var
759 it: PItem;
760 begin
761 if not g_Items_ValidId(idx) then exit;
763 it := @ggItems[idx];
764 if gSoundEffectsDF then
765 begin
766 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
767 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
768 begin
769 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
770 end
771 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
772 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
773 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
774 ITEM_AMMO_BACKPACK] then
775 begin
776 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
777 end
778 else
779 begin
780 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
781 end;
782 end
783 else
784 begin
785 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
786 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
787 begin
788 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
789 end
790 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
791 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
792 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
793 begin
794 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
795 end
796 else
797 begin
798 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
799 end;
800 end;
801 end;
804 procedure g_Items_AddDynLights();
805 var
806 f: Integer;
807 it: PItem;
808 begin
809 for f := 0 to High(ggItems) do
810 begin
811 it := @ggItems[f];
812 if not it.alive then continue;
813 case it.ItemType of
814 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);
815 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);
816 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);
817 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);
818 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);
819 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);
820 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);
821 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);
822 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);
823 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);
824 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);
825 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);
826 end;
827 end;
828 end;
831 end.