DEADSOFTWARE

fixed bug in grid update; one more mode for Holmes
[d2df-sdl.git] / src / game / g_items.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_items;
19 interface
21 uses
22 g_textures, g_phys, g_saveload, BinEditor, MAPDEF;
24 Type
25 PItem = ^TItem;
26 TItem = record
27 private
28 //treeNode: Integer;
29 slotIsUsed: Boolean;
30 arrIdx: Integer; // in ggItems
32 public
33 ItemType: Byte;
34 Respawnable: Boolean;
35 InitX, InitY: Integer;
36 RespawnTime: Word;
37 Live: Boolean;
38 Fall: Boolean;
39 QuietRespawn: Boolean;
40 SpawnTrigger: Integer;
41 Obj: TObj;
42 Animation: TAnimation;
44 procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
46 property myid: Integer read arrIdx;
47 end;
49 procedure g_Items_LoadData();
50 procedure g_Items_FreeData();
51 procedure g_Items_Init();
52 procedure g_Items_Free();
53 function g_Items_Create(X, Y: Integer; ItemType: Byte;
54 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
55 procedure g_Items_Update();
56 procedure g_Items_Draw();
57 procedure g_Items_Pick(ID: DWORD);
58 procedure g_Items_Remove(ID: DWORD);
59 procedure g_Items_SaveState(var Mem: TBinMemoryWriter);
60 procedure g_Items_LoadState(var Mem: TBinMemoryReader);
62 procedure g_Items_RestartRound ();
64 function g_Items_ValidId (idx: Integer): Boolean; inline;
65 function g_Items_ByIdx (idx: Integer): PItem;
66 function g_Items_ObjByIdx (idx: Integer): PObj;
68 procedure g_Items_EmitPickupSound (idx: Integer); // at item position
69 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
72 type
73 TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
75 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
78 var
79 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
80 gMaxDist: Integer = 1;
81 ITEM_RESPAWNTIME: Integer = 60 * 36;
83 implementation
85 uses
86 g_basic, e_graphics, g_sound, g_main, g_gfx, g_map,
87 Math, g_game, g_triggers, g_console, SysUtils, g_player, g_net, g_netmsg,
88 e_log,
89 g_grid, binheap;
92 var
93 ggItems: Array of TItem = nil;
96 // ////////////////////////////////////////////////////////////////////////// //
97 var
98 freeIds: TBinaryHeapInt = nil; // free item ids
101 // ////////////////////////////////////////////////////////////////////////// //
102 function g_Items_ValidId (idx: Integer): Boolean; inline;
103 begin
104 result := false;
105 if (idx < 0) or (idx > High(ggItems)) then exit;
106 if not ggItems[idx].slotIsUsed then exit;
107 result := true;
108 end;
111 function g_Items_ByIdx (idx: Integer): PItem;
112 begin
113 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
114 result := @ggItems[idx];
115 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
116 end;
119 function g_Items_ObjByIdx (idx: Integer): PObj;
120 begin
121 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
122 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
123 result := @ggItems[idx].Obj;
124 end;
127 // ////////////////////////////////////////////////////////////////////////// //
128 procedure TItem.positionChanged ();
129 begin
130 end;
133 // ////////////////////////////////////////////////////////////////////////// //
134 const
135 ITEM_SIGNATURE = $4D455449; // 'ITEM'
137 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
138 (((14), (15)), // MEDKIT_SMALL
139 ((28), (19)), // MEDKIT_LARGE
140 ((28), (19)), // MEDKIT_BLACK
141 ((31), (16)), // ARMOR_GREEN
142 ((31), (16)), // ARMOR_BLUE
143 ((25), (25)), // SPHERE_BLUE
144 ((25), (25)), // SPHERE_WHITE
145 ((24), (47)), // SUIT
146 ((14), (27)), // OXYGEN
147 ((25), (25)), // INVUL
148 ((62), (24)), // WEAPON_SAW
149 ((63), (12)), // WEAPON_SHOTGUN1
150 ((54), (13)), // WEAPON_SHOTGUN2
151 ((54), (16)), // WEAPON_CHAINGUN
152 ((62), (16)), // WEAPON_ROCKETLAUNCHER
153 ((54), (16)), // WEAPON_PLASMA
154 ((61), (36)), // WEAPON_BFG
155 ((54), (16)), // WEAPON_SUPERPULEMET
156 (( 9), (11)), // AMMO_BULLETS
157 ((28), (16)), // AMMO_BULLETS_BOX
158 ((15), ( 7)), // AMMO_SHELLS
159 ((32), (12)), // AMMO_SHELLS_BOX
160 ((12), (27)), // AMMO_ROCKET
161 ((54), (21)), // AMMO_ROCKET_BOX
162 ((15), (12)), // AMMO_CELL
163 ((32), (21)), // AMMO_CELL_BIG
164 ((22), (29)), // AMMO_BACKPACK
165 ((16), (16)), // KEY_RED
166 ((16), (16)), // KEY_GREEN
167 ((16), (16)), // KEY_BLUE
168 (( 1), ( 1)), // WEAPON_KASTET
169 ((43), (16)), // WEAPON_PISTOL
170 ((14), (18)), // BOTTLE
171 ((16), (15)), // HELMET
172 ((32), (24)), // JETPACK
173 ((25), (25)), // INVIS
174 ((53), (20)), // WEAPON_FLAMETHROWER
175 ((13), (20))); // AMMO_FUELCAN
177 procedure InitTextures();
178 begin
179 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
180 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
181 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
182 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
183 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
184 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
185 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
186 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
187 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
188 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
189 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
190 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
191 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
192 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
193 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
194 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
195 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
196 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
197 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
198 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
199 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
200 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
201 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
202 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
203 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
204 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
205 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
206 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
207 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
208 g_Texture_Get('ITEM_JETPACK', gItemsTexturesID[ITEM_JETPACK]);
209 end;
211 procedure g_Items_LoadData();
212 begin
213 e_WriteLog('Loading items data...', MSG_NOTIFY);
215 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
216 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
217 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
218 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
220 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
221 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
222 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
223 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
224 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
225 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
226 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
227 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
228 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
229 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
230 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
231 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
232 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
233 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
234 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
235 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
236 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
237 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
238 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
239 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
240 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
241 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
242 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
243 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
244 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
245 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
246 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
247 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
248 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
249 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
250 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
251 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
252 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
253 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
254 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
255 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
256 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
257 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
258 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
259 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
260 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
261 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
262 g_Texture_CreateWADEx('ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK');
264 InitTextures();
266 freeIds := binHeapNewIntLess();
267 end;
270 procedure g_Items_FreeData();
271 begin
272 e_WriteLog('Releasing items data...', MSG_NOTIFY);
274 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
275 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
276 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
277 g_Sound_Delete('SOUND_ITEM_GETITEM');
279 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
280 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
281 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
282 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
283 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
284 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
285 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
286 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
287 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
288 g_Frames_DeleteByName('FRAMES_FLAG_RED');
289 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
290 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
291 g_Texture_Delete('ITEM_MEDKIT_SMALL');
292 g_Texture_Delete('ITEM_MEDKIT_LARGE');
293 g_Texture_Delete('ITEM_WEAPON_SAW');
294 g_Texture_Delete('ITEM_WEAPON_PISTOL');
295 g_Texture_Delete('ITEM_WEAPON_KASTET');
296 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
297 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
298 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
299 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
300 g_Texture_Delete('ITEM_WEAPON_PLASMA');
301 g_Texture_Delete('ITEM_WEAPON_BFG');
302 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
303 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
304 g_Texture_Delete('ITEM_AMMO_BULLETS');
305 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
306 g_Texture_Delete('ITEM_AMMO_SHELLS');
307 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
308 g_Texture_Delete('ITEM_AMMO_ROCKET');
309 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
310 g_Texture_Delete('ITEM_AMMO_CELL');
311 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
312 g_Texture_Delete('ITEM_AMMO_FUELCAN');
313 g_Texture_Delete('ITEM_AMMO_BACKPACK');
314 g_Texture_Delete('ITEM_KEY_RED');
315 g_Texture_Delete('ITEM_KEY_GREEN');
316 g_Texture_Delete('ITEM_KEY_BLUE');
317 g_Texture_Delete('ITEM_OXYGEN');
318 g_Texture_Delete('ITEM_SUIT');
319 g_Texture_Delete('ITEM_WEAPON_KASTET');
320 g_Texture_Delete('ITEM_MEDKIT_BLACK');
321 g_Texture_Delete('ITEM_JETPACK');
323 freeIds.Free();
324 end;
327 procedure releaseItem (idx: Integer);
328 var
329 it: PItem;
330 begin
331 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
332 it := @ggItems[idx];
333 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item');
334 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
335 it.slotIsUsed := false;
336 if (it.Animation <> nil) then
337 begin
338 it.Animation.Free();
339 it.Animation := nil;
340 end;
341 it.Live := False;
342 it.SpawnTrigger := -1;
343 it.ItemType := ITEM_NONE;
344 freeIds.insert(it.arrIdx);
345 end;
348 function allocItem (): DWORD;
349 var
350 i, olen: Integer;
351 it: PItem;
352 begin
353 if (freeIds.count = 0) then
354 begin
355 // no free slots
356 olen := Length(ggItems);
357 SetLength(ggItems, olen+64);
358 for i := olen to High(ggItems) do
359 begin
360 it := @ggItems[i];
361 it.slotIsUsed := false;
362 it.arrIdx := i;
363 it.ItemType := ITEM_NONE;
364 it.Animation := nil;
365 it.Live := false;
366 it.SpawnTrigger := -1;
367 it.Respawnable := false;
368 freeIds.insert(i);
369 end;
370 end;
372 result := freeIds.front;
373 freeIds.popFront();
375 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
376 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
377 end;
380 // it will be slow if the slot is free (we have to rebuild the heap)
381 function wantItemSlot (slot: Integer): Integer;
382 var
383 i, olen: Integer;
384 it: PItem;
385 rebuildFreeList: Boolean = true;
386 begin
387 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
388 // do we need to grow item storate?
389 olen := Length(ggItems);
390 if (slot >= olen) then
391 begin
392 // need more spice!
393 SetLength(ggItems, slot+64);
394 // add free slots to free list
395 for i := olen to High(ggItems) do
396 begin
397 it := @ggItems[i];
398 it.slotIsUsed := false;
399 it.arrIdx := i;
400 it.ItemType := ITEM_NONE;
401 it.Animation := nil;
402 it.Live := false;
403 it.SpawnTrigger := -1;
404 it.Respawnable := false;
405 if (i <> slot) then freeIds.insert(i);
406 end;
407 rebuildFreeList := false;
408 end;
410 it := @ggItems[slot];
411 if not it.slotIsUsed then
412 begin
413 // this is unused slot; get it, and rebuild id list
414 if rebuildFreeList then
415 begin
416 freeIds.clear();
417 for i := 0 to High(ggItems) do
418 begin
419 if (i <> slot) and (not it.slotIsUsed) then freeIds.insert(i);
420 end;
421 end;
422 end
423 else
424 begin
425 // it will be readded
426 it.slotIsUsed := false;
427 end;
429 result := slot;
430 end;
433 // ////////////////////////////////////////////////////////////////////////// //
434 procedure g_Items_Init ();
435 var
436 a, b: Integer;
437 begin
438 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
439 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
440 gMaxDist := Trunc(Hypot(a, b));
441 end;
444 procedure g_Items_Free ();
445 var
446 i: Integer;
447 begin
448 if (ggItems <> nil) then
449 begin
450 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
451 ggItems := nil;
452 end;
453 freeIds.clear();
454 end;
457 function g_Items_Create (X, Y: Integer; ItemType: Byte;
458 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
459 var
460 find_id: DWORD;
461 ID: DWORD;
462 it: PItem;
463 begin
464 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
466 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
468 it := @ggItems[find_id];
470 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
471 //it.arrIdx := find_id;
472 it.slotIsUsed := true;
474 it.ItemType := ItemType;
475 it.Respawnable := Respawnable;
476 if g_Game_IsServer and (ITEM_RESPAWNTIME = 0) then it.Respawnable := False;
477 it.InitX := X;
478 it.InitY := Y;
479 it.RespawnTime := 0;
480 it.Fall := Fall;
481 it.Live := True;
482 it.QuietRespawn := False;
484 g_Obj_Init(@it.Obj);
485 it.Obj.X := X;
486 it.Obj.Y := Y;
487 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
488 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
490 it.Animation := nil;
491 it.SpawnTrigger := -1;
493 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
494 if AdjCoord then
495 begin
496 with it^ do
497 begin
498 Obj.X := X - (Obj.Rect.Width div 2);
499 Obj.Y := Y - Obj.Rect.Height;
500 InitX := Obj.X;
501 InitY := Obj.Y;
502 end;
503 end;
505 // Óñòàíîâêà àíèìàöèè
506 case it.ItemType of
507 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
508 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
509 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
510 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
511 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
512 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
513 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
514 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
515 end;
517 it.positionChanged();
519 result := find_id;
520 end;
523 procedure g_Items_Update ();
524 var
525 i, j, k: Integer;
526 ID: DWord;
527 Anim: TAnimation;
528 m: Word;
529 r, nxt: Boolean;
530 begin
531 if (ggItems = nil) then exit;
533 for i := 0 to High(ggItems) do
534 begin
535 if (ggItems[i].ItemType = ITEM_NONE) then continue;
537 with ggItems[i] do
538 begin
539 nxt := False;
541 if Live then
542 begin
543 if Fall then
544 begin
545 m := g_Obj_Move(@Obj, True, True);
546 positionChanged(); // this updates spatial accelerators
548 // Ñîïðîòèâëåíèå âîçäóõà
549 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
551 // Åñëè âûïàë çà êàðòó
552 if WordBool(m and MOVE_FALLOUT) then
553 begin
554 if SpawnTrigger = -1 then
555 begin
556 g_Items_Pick(i);
557 end
558 else
559 begin
560 g_Items_Remove(i);
561 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
562 end;
563 continue;
564 end;
565 end;
567 // Åñëè èãðîêè ïîáëèçîñòè
568 if (gPlayers <> nil) then
569 begin
570 j := Random(Length(gPlayers))-1;
572 for k := 0 to High(gPlayers) do
573 begin
574 Inc(j);
575 if j > High(gPlayers) then j := 0;
577 if (gPlayers[j] <> nil) and gPlayers[j].Live and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
578 begin
579 if g_Game_IsClient then continue;
581 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
583 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
586 Doom 2D: Original:
587 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
588 +2. I_MEGA,I_INVL,I_SUPER
589 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
591 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
593 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
594 if r then
595 begin
596 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
597 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
598 nxt := True;
599 break;
600 end;
601 end;
602 end;
603 end;
605 if nxt then continue;
606 end;
608 if Respawnable and g_Game_IsServer then
609 begin
610 DecMin(RespawnTime, 0);
611 if (RespawnTime = 0) and (not Live) then
612 begin
613 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
615 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
616 begin
617 Anim := TAnimation.Create(ID, False, 4);
618 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
619 Anim.Free();
620 end;
622 Obj.X := InitX;
623 Obj.Y := InitY;
624 Obj.Vel.X := 0;
625 Obj.Vel.Y := 0;
626 Obj.Accel.X := 0;
627 Obj.Accel.Y := 0;
628 positionChanged(); // this updates spatial accelerators
630 Live := true;
632 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
633 QuietRespawn := false;
634 end;
635 end;
637 if (Animation <> nil) then Animation.Update();
638 end;
639 end;
640 end;
643 procedure g_Items_Draw ();
644 var
645 i: Integer;
646 begin
647 if (ggItems = nil) then exit;
649 for i := 0 to High(ggItems) do
650 begin
651 if not ggItems[i].Live then continue;
653 with ggItems[i] do
654 begin
655 if g_Collide(Obj.X, Obj.Y, Obj.Rect.Width, Obj.Rect.Height, sX, sY, sWidth, sHeight) then
656 begin
657 if (Animation = nil) then
658 begin
659 e_Draw(gItemsTexturesID[ItemType], Obj.X, Obj.Y, 0, true, false);
660 end
661 else
662 begin
663 Animation.Draw(Obj.X, Obj.Y, M_NONE);
664 end;
666 if g_debug_Frames then
667 begin
668 e_DrawQuad(Obj.X+Obj.Rect.X,
669 Obj.Y+Obj.Rect.Y,
670 Obj.X+Obj.Rect.X+Obj.Rect.Width-1,
671 Obj.Y+Obj.Rect.Y+Obj.Rect.Height-1,
672 0, 255, 0);
673 end;
674 end;
675 end;
676 end;
677 end;
680 procedure g_Items_Pick (ID: DWORD);
681 begin
682 ggItems[ID].Live := false;
683 ggItems[ID].RespawnTime := ITEM_RESPAWNTIME;
684 end;
687 procedure g_Items_Remove (ID: DWORD);
688 var
689 it: PItem;
690 trig: Integer;
691 begin
692 if not g_Items_ValidId(ID) then raise Exception.Create('g_Items_Remove: invalid item id');
694 it := @ggItems[ID];
695 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
697 trig := it.SpawnTrigger;
699 releaseItem(ID);
701 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
702 end;
705 procedure g_Items_SaveState (var Mem: TBinMemoryWriter);
706 var
707 count, i: Integer;
708 sig: DWORD;
709 begin
710 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
711 count := 0;
712 if (ggItems <> nil) then
713 begin
714 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) then Inc(count);
715 end;
717 Mem := TBinMemoryWriter.Create((count+1) * 60);
719 // Êîëè÷åñòâî ïðåäìåòîâ
720 Mem.WriteInt(count);
722 if (count = 0) then exit;
724 for i := 0 to High(ggItems) do
725 begin
726 if (ggItems[i].ItemType <> ITEM_NONE) then
727 begin
728 // Ñèãíàòóðà ïðåäìåòà
729 sig := ITEM_SIGNATURE; // 'ITEM'
730 Mem.WriteDWORD(sig);
731 // Òèï ïðåäìåòà
732 Mem.WriteByte(ggItems[i].ItemType);
733 // Åñòü ëè ðåñïàóí
734 Mem.WriteBoolean(ggItems[i].Respawnable);
735 // Êîîðäèíàòû ðåñïóíà
736 Mem.WriteInt(ggItems[i].InitX);
737 Mem.WriteInt(ggItems[i].InitY);
738 // Âðåìÿ äî ðåñïàóíà
739 Mem.WriteWord(ggItems[i].RespawnTime);
740 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
741 Mem.WriteBoolean(ggItems[i].Live);
742 // Ìîæåò ëè îí ïàäàòü
743 Mem.WriteBoolean(ggItems[i].Fall);
744 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
745 Mem.WriteInt(ggItems[i].SpawnTrigger);
746 // Îáúåêò ïðåäìåòà
747 Obj_SaveState(@ggItems[i].Obj, Mem);
748 end;
749 end;
750 end;
753 procedure g_Items_LoadState (var Mem: TBinMemoryReader);
754 var
755 count, i, a: Integer;
756 sig: DWORD;
757 b: Byte;
758 begin
759 if (Mem = nil) then exit;
761 g_Items_Free();
763 // Êîëè÷åñòâî ïðåäìåòîâ
764 Mem.ReadInt(count);
766 if (count = 0) then Exit;
768 for a := 0 to count-1 do
769 begin
770 // Ñèãíàòóðà ïðåäìåòà
771 Mem.ReadDWORD(sig);
772 if (sig <> ITEM_SIGNATURE) then raise EBinSizeError.Create('g_Items_LoadState: Wrong Item Signature'); // 'ITEM'
773 // Òèï ïðåäìåòà
774 Mem.ReadByte(b);
775 // Ñîçäàåì ïðåäìåò
776 i := g_Items_Create(0, 0, b, False, False);
777 // Åñòü ëè ðåñïàóí
778 Mem.ReadBoolean(ggItems[i].Respawnable);
779 // Êîîðäèíàòû ðåñïóíà
780 Mem.ReadInt(ggItems[i].InitX);
781 Mem.ReadInt(ggItems[i].InitY);
782 // Âðåìÿ äî ðåñïàóíà
783 Mem.ReadWord(ggItems[i].RespawnTime);
784 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
785 Mem.ReadBoolean(ggItems[i].Live);
786 // Ìîæåò ëè îí ïàäàòü
787 Mem.ReadBoolean(ggItems[i].Fall);
788 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
789 Mem.ReadInt(ggItems[i].SpawnTrigger);
790 // Îáúåêò ïðåäìåòà
791 Obj_LoadState(@ggItems[i].Obj, Mem);
792 end;
793 end;
796 procedure g_Items_RestartRound ();
797 var
798 i: Integer;
799 it: PItem;
800 begin
801 for i := 0 to High(ggItems) do
802 begin
803 it := @ggItems[i];
804 if it.Respawnable then
805 begin
806 it.QuietRespawn := True;
807 it.RespawnTime := 0;
808 end
809 else
810 begin
811 g_Items_Remove(i);
812 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
813 end;
814 end;
815 end;
818 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
819 var
820 idx: Integer;
821 begin
822 result := false;
823 if (ggItems = nil) or not assigned(cb) then exit;
825 if backwards then
826 begin
827 for idx := High(ggItems) downto 0 do
828 begin
829 if ggItems[idx].Live then
830 begin
831 result := cb(@ggItems[idx]);
832 if result then exit;
833 end;
834 end;
835 end
836 else
837 begin
838 for idx := 0 to High(ggItems) do
839 begin
840 if ggItems[idx].Live then
841 begin
842 result := cb(@ggItems[idx]);
843 if result then exit;
844 end;
845 end;
846 end;
847 end;
850 // ////////////////////////////////////////////////////////////////////////// //
851 procedure g_Items_EmitPickupSound (idx: Integer);
852 var
853 it: PItem;
854 begin
855 if not g_Items_ValidId(idx) then exit;
856 it := @ggItems[idx];
857 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
858 end;
860 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
861 var
862 it: PItem;
863 begin
864 if not g_Items_ValidId(idx) then exit;
866 it := @ggItems[idx];
867 if gSoundEffectsDF then
868 begin
869 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
870 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
871 begin
872 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
873 end
874 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
875 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
876 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
877 ITEM_AMMO_BACKPACK] then
878 begin
879 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
880 end
881 else
882 begin
883 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
884 end;
885 end
886 else
887 begin
888 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
889 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
890 begin
891 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
892 end
893 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
894 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
895 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
896 begin
897 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
898 end
899 else
900 begin
901 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
902 end;
903 end;
904 end;
906 end.