DEADSOFTWARE

render: use only r_render to access 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_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,
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 m, ItemRespawnTime: Word;
420 r, nxt: Boolean;
421 begin
422 if (ggItems = nil) then exit;
424 // respawn items in 15 seconds regardless of settings during warmup
425 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
427 for i := 0 to High(ggItems) do
428 begin
429 if (ggItems[i].ItemType = ITEM_NONE) then continue;
430 if not ggItems[i].slotIsUsed then continue; // just in case
432 with ggItems[i] do
433 begin
434 nxt := False;
436 if alive then
437 begin
438 if Fall then
439 begin
440 m := g_Obj_Move(@Obj, True, True);
441 positionChanged(); // this updates spatial accelerators
443 // Ñîïðîòèâëåíèå âîçäóõà
444 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
446 // Åñëè âûïàë çà êàðòó
447 if WordBool(m and MOVE_FALLOUT) then
448 begin
449 if SpawnTrigger = -1 then
450 begin
451 g_Items_Pick(i);
452 end
453 else
454 begin
455 g_Items_Remove(i);
456 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
457 end;
458 continue;
459 end;
460 end;
462 // Åñëè èãðîêè ïîáëèçîñòè
463 if (gPlayers <> nil) then
464 begin
465 j := Random(Length(gPlayers))-1;
467 for k := 0 to High(gPlayers) do
468 begin
469 Inc(j);
470 if j > High(gPlayers) then j := 0;
472 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
473 begin
474 if g_Game_IsClient then continue;
476 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
478 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
481 Doom 2D: Original:
482 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
483 +2. I_MEGA,I_INVL,I_SUPER
484 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
486 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
488 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
489 if r then
490 begin
491 if not (Respawnable and (ItemRespawnTime > 0)) then
492 g_Items_Remove(i)
493 else
494 g_Items_Pick(i);
495 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
496 nxt := True;
497 break;
498 end;
499 end;
500 end;
501 end;
503 if nxt then continue;
504 end;
506 if Respawnable and g_Game_IsServer then
507 begin
508 DecMin(RespawnTime, 0);
509 if (RespawnTime = 0) and (not alive) then
510 begin
511 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
512 g_GFX_QueueEffect(R_GFX_ITEM_RESPAWN, InitX + (Obj.Rect.Width div 2) - 16, InitY + (Obj.Rect.Height div 2) - 16);
513 Obj.oldX := InitX;
514 Obj.oldY := InitY;
515 Obj.X := InitX;
516 Obj.Y := InitY;
517 Obj.Vel.X := 0;
518 Obj.Vel.Y := 0;
519 Obj.Accel.X := 0;
520 Obj.Accel.Y := 0;
521 positionChanged(); // this updates spatial accelerators
523 alive := true;
525 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
526 QuietRespawn := false;
527 end;
528 end;
530 if (Animation <> nil) then Animation.Update();
531 end;
532 end;
533 end;
535 procedure g_Items_SetDrop (ID: DWORD);
536 begin
537 if (ID < Length(ggItems)) then
538 begin
539 ggItems[ID].dropped := true;
540 end;
541 end;
544 procedure g_Items_Pick (ID: DWORD);
545 begin
546 if (ID < Length(ggItems)) then
547 begin
548 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
549 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
550 ggItems[ID].alive := false;
551 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
552 end;
553 end;
556 procedure g_Items_Remove (ID: DWORD);
557 var
558 it: PItem;
559 trig: Integer;
560 begin
561 if not g_Items_ValidId(ID) then
562 begin
563 //writeln('g_Items_Remove: invalid item id: ', ID);
564 raise Exception.Create('g_Items_Remove: invalid item id');
565 //exit;
566 end;
568 it := @ggItems[ID];
569 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
571 it.Obj.oldX := it.Obj.X;
572 it.Obj.oldY := it.Obj.Y;
573 trig := it.SpawnTrigger;
575 releaseItem(ID);
577 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
578 end;
581 procedure g_Items_SaveState (st: TStream);
582 var
583 count, i: Integer;
584 tt: Byte;
585 begin
586 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
587 count := 0;
588 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
590 // Êîëè÷åñòâî ïðåäìåòîâ
591 utils.writeInt(st, LongInt(count));
592 if (count = 0) then exit;
594 for i := 0 to High(ggItems) do
595 begin
596 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
597 begin
598 // Ñèãíàòóðà ïðåäìåòà
599 utils.writeSign(st, 'ITEM');
600 utils.writeInt(st, Byte(0));
601 // Òèï ïðåäìåòà
602 tt := ggItems[i].ItemType;
603 if ggItems[i].dropped then tt := tt or $80;
604 utils.writeInt(st, Byte(tt));
605 // Åñòü ëè ðåñïàóí
606 utils.writeBool(st, ggItems[i].Respawnable);
607 // Êîîðäèíàòû ðåñïóíà
608 utils.writeInt(st, LongInt(ggItems[i].InitX));
609 utils.writeInt(st, LongInt(ggItems[i].InitY));
610 // Âðåìÿ äî ðåñïàóíà
611 utils.writeInt(st, Word(ggItems[i].RespawnTime));
612 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
613 utils.writeBool(st, ggItems[i].alive);
614 // Ìîæåò ëè îí ïàäàòü
615 utils.writeBool(st, ggItems[i].Fall);
616 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
617 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
618 // Îáúåêò ïðåäìåòà
619 Obj_SaveState(st, @ggItems[i].Obj);
620 end;
621 end;
622 end;
625 procedure g_Items_LoadState (st: TStream);
626 var
627 count, i, a: Integer;
628 b: Byte;
629 begin
630 assert(st <> nil);
632 g_Items_Free();
634 // Êîëè÷åñòâî ïðåäìåòîâ
635 count := utils.readLongInt(st);
636 if (count = 0) then exit;
637 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
639 for a := 0 to count-1 do
640 begin
641 // Ñèãíàòóðà ïðåäìåòà
642 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
643 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
644 // Òèï ïðåäìåòà
645 b := utils.readByte(st); // bit7=1: monster drop
646 // Ñîçäàåì ïðåäìåò
647 i := g_Items_Create(0, 0, b and $7F, False, False);
648 if ((b and $80) <> 0) then g_Items_SetDrop(i);
649 // Åñòü ëè ðåñïàóí
650 ggItems[i].Respawnable := utils.readBool(st);
651 // Êîîðäèíàòû ðåñïóíà
652 ggItems[i].InitX := utils.readLongInt(st);
653 ggItems[i].InitY := utils.readLongInt(st);
654 // Âðåìÿ äî ðåñïàóíà
655 ggItems[i].RespawnTime := utils.readWord(st);
656 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
657 ggItems[i].alive := utils.readBool(st);
658 // Ìîæåò ëè îí ïàäàòü
659 ggItems[i].Fall := utils.readBool(st);
660 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
661 ggItems[i].SpawnTrigger := utils.readLongInt(st);
662 // Îáúåêò ïðåäìåòà
663 Obj_LoadState(@ggItems[i].Obj, st);
664 end;
665 end;
668 procedure g_Items_RestartRound ();
669 var
670 i: Integer;
671 it: PItem;
672 begin
673 for i := 0 to High(ggItems) do
674 begin
675 it := @ggItems[i];
676 it.Obj.oldX := it.Obj.X;
677 it.Obj.oldY := it.Obj.Y;
678 if not it.slotIsUsed then continue;
679 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
680 begin
681 it.QuietRespawn := True;
682 it.RespawnTime := 0;
683 end
684 else
685 begin
686 g_Items_Remove(i);
687 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
688 end;
689 end;
690 end;
693 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
694 var
695 idx: Integer;
696 begin
697 result := false;
698 if (ggItems = nil) or not assigned(cb) then exit;
700 if backwards then
701 begin
702 for idx := High(ggItems) downto 0 do
703 begin
704 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
705 begin
706 result := cb(@ggItems[idx]);
707 if result then exit;
708 end;
709 end;
710 end
711 else
712 begin
713 for idx := 0 to High(ggItems) do
714 begin
715 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
716 begin
717 result := cb(@ggItems[idx]);
718 if result then exit;
719 end;
720 end;
721 end;
722 end;
724 function g_Items_NextAlive (startIdx: Integer): PItem;
725 var
726 idx: Integer;
727 begin
728 result := nil;
729 if (ggItems = nil) or (startIdx >= High(ggItems)) then exit;
730 for idx := startIdx + 1 to High(ggItems) do
731 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
732 begin
733 result := @ggItems[idx];
734 exit;
735 end;
736 end;
738 // ////////////////////////////////////////////////////////////////////////// //
739 procedure g_Items_EmitPickupSound (idx: Integer);
740 var
741 it: PItem;
742 begin
743 if not g_Items_ValidId(idx) then exit;
744 it := @ggItems[idx];
745 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
746 end;
748 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
749 var
750 it: PItem;
751 begin
752 if not g_Items_ValidId(idx) then exit;
754 it := @ggItems[idx];
755 if gSoundEffectsDF then
756 begin
757 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
758 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
759 begin
760 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
761 end
762 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
763 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
764 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
765 ITEM_AMMO_BACKPACK] 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 else
775 begin
776 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
777 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
778 begin
779 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
780 end
781 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
782 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
783 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
784 begin
785 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
786 end
787 else
788 begin
789 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
790 end;
791 end;
792 end;
795 procedure g_Items_AddDynLights();
796 var
797 f: Integer;
798 it: PItem;
799 begin
800 for f := 0 to High(ggItems) do
801 begin
802 it := @ggItems[f];
803 if not it.alive then continue;
804 case it.ItemType of
805 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);
806 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);
807 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);
808 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);
809 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);
810 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);
811 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);
812 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);
813 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);
814 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);
815 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);
816 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);
817 end;
818 end;
819 end;
822 end.