DEADSOFTWARE

render: move textures loaders to 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: TAnimation;
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 gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
88 gMaxDist: Integer = 1; // for sounds
90 var (* private state *)
91 ggItems: Array of TItem = nil;
93 implementation
95 uses
96 Math,
97 g_basic, g_sound, g_gfx, g_map, r_textures, r_animations,
98 g_game, g_triggers, g_console, g_player, g_net, g_netmsg,
99 e_log, g_options,
100 g_grid, binheap, idpool, utils, xstreams;
102 // ////////////////////////////////////////////////////////////////////////// //
103 var
104 freeIds: TIdPool = nil;
107 // ////////////////////////////////////////////////////////////////////////// //
108 function g_Items_ValidId (idx: Integer): Boolean; inline;
109 begin
110 result := false;
111 if (idx < 0) or (idx > High(ggItems)) then exit;
112 if not ggItems[idx].slotIsUsed then exit;
113 result := true;
114 end;
117 function g_Items_ByIdx (idx: Integer): PItem;
118 begin
119 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
120 result := @ggItems[idx];
121 if not result.slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
122 end;
125 function g_Items_ObjByIdx (idx: Integer): PObj;
126 begin
127 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('g_ItemObjByIdx: invalid index');
128 if not ggItems[idx].slotIsUsed then raise Exception.Create('g_ItemObjByIdx: requested inexistent item');
129 result := @ggItems[idx].Obj;
130 end;
133 // ////////////////////////////////////////////////////////////////////////// //
134 procedure TItem.positionChanged ();
135 begin
136 NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
137 end;
139 procedure TItem.getMapBox (out x, y, w, h: Integer); inline;
140 begin
141 x := Obj.X+Obj.Rect.X;
142 y := Obj.Y+Obj.Rect.Y;
143 w := Obj.Rect.Width;
144 h := Obj.Rect.Height;
145 end;
147 procedure TItem.moveBy (dx, dy: Integer); inline;
148 begin
149 if (dx <> 0) or (dy <> 0) then
150 begin
151 Obj.X += dx;
152 Obj.Y += dy;
153 positionChanged();
154 end;
155 end;
157 // ////////////////////////////////////////////////////////////////////////// //
158 const
159 ITEM_SIGNATURE = $4D455449; // 'ITEM'
161 ITEMSIZE: Array [ITEM_MEDKIT_SMALL..ITEM_MAX] of Array [0..1] of Byte =
162 (((14), (15)), // MEDKIT_SMALL
163 ((28), (19)), // MEDKIT_LARGE
164 ((28), (19)), // MEDKIT_BLACK
165 ((31), (16)), // ARMOR_GREEN
166 ((31), (16)), // ARMOR_BLUE
167 ((25), (25)), // SPHERE_BLUE
168 ((25), (25)), // SPHERE_WHITE
169 ((24), (47)), // SUIT
170 ((14), (27)), // OXYGEN
171 ((25), (25)), // INVUL
172 ((62), (24)), // WEAPON_SAW
173 ((63), (12)), // WEAPON_SHOTGUN1
174 ((54), (13)), // WEAPON_SHOTGUN2
175 ((54), (16)), // WEAPON_CHAINGUN
176 ((62), (16)), // WEAPON_ROCKETLAUNCHER
177 ((54), (16)), // WEAPON_PLASMA
178 ((61), (36)), // WEAPON_BFG
179 ((54), (16)), // WEAPON_SUPERPULEMET
180 (( 9), (11)), // AMMO_BULLETS
181 ((28), (16)), // AMMO_BULLETS_BOX
182 ((15), ( 7)), // AMMO_SHELLS
183 ((32), (12)), // AMMO_SHELLS_BOX
184 ((12), (27)), // AMMO_ROCKET
185 ((54), (21)), // AMMO_ROCKET_BOX
186 ((15), (12)), // AMMO_CELL
187 ((32), (21)), // AMMO_CELL_BIG
188 ((22), (29)), // AMMO_BACKPACK
189 ((16), (16)), // KEY_RED
190 ((16), (16)), // KEY_GREEN
191 ((16), (16)), // KEY_BLUE
192 (( 1), ( 1)), // WEAPON_KASTET
193 ((43), (16)), // WEAPON_PISTOL
194 ((14), (18)), // BOTTLE
195 ((16), (15)), // HELMET
196 ((32), (24)), // JETPACK
197 ((25), (25)), // INVIS
198 ((53), (20)), // WEAPON_FLAMETHROWER
199 ((13), (20))); // AMMO_FUELCAN
201 procedure InitTextures();
202 begin
203 g_Texture_Get('ITEM_MEDKIT_SMALL', gItemsTexturesID[ITEM_MEDKIT_SMALL]);
204 g_Texture_Get('ITEM_MEDKIT_LARGE', gItemsTexturesID[ITEM_MEDKIT_LARGE]);
205 g_Texture_Get('ITEM_MEDKIT_BLACK', gItemsTexturesID[ITEM_MEDKIT_BLACK]);
206 g_Texture_Get('ITEM_SUIT', gItemsTexturesID[ITEM_SUIT]);
207 g_Texture_Get('ITEM_OXYGEN', gItemsTexturesID[ITEM_OXYGEN]);
208 g_Texture_Get('ITEM_WEAPON_SAW', gItemsTexturesID[ITEM_WEAPON_SAW]);
209 g_Texture_Get('ITEM_WEAPON_SHOTGUN1', gItemsTexturesID[ITEM_WEAPON_SHOTGUN1]);
210 g_Texture_Get('ITEM_WEAPON_SHOTGUN2', gItemsTexturesID[ITEM_WEAPON_SHOTGUN2]);
211 g_Texture_Get('ITEM_WEAPON_CHAINGUN', gItemsTexturesID[ITEM_WEAPON_CHAINGUN]);
212 g_Texture_Get('ITEM_WEAPON_ROCKETLAUNCHER', gItemsTexturesID[ITEM_WEAPON_ROCKETLAUNCHER]);
213 g_Texture_Get('ITEM_WEAPON_PLASMA', gItemsTexturesID[ITEM_WEAPON_PLASMA]);
214 g_Texture_Get('ITEM_WEAPON_BFG', gItemsTexturesID[ITEM_WEAPON_BFG]);
215 g_Texture_Get('ITEM_WEAPON_SUPERPULEMET', gItemsTexturesID[ITEM_WEAPON_SUPERPULEMET]);
216 g_Texture_Get('ITEM_WEAPON_FLAMETHROWER', gItemsTexturesID[ITEM_WEAPON_FLAMETHROWER]);
217 g_Texture_Get('ITEM_AMMO_BULLETS', gItemsTexturesID[ITEM_AMMO_BULLETS]);
218 g_Texture_Get('ITEM_AMMO_BULLETS_BOX', gItemsTexturesID[ITEM_AMMO_BULLETS_BOX]);
219 g_Texture_Get('ITEM_AMMO_SHELLS', gItemsTexturesID[ITEM_AMMO_SHELLS]);
220 g_Texture_Get('ITEM_AMMO_SHELLS_BOX', gItemsTexturesID[ITEM_AMMO_SHELLS_BOX]);
221 g_Texture_Get('ITEM_AMMO_ROCKET', gItemsTexturesID[ITEM_AMMO_ROCKET]);
222 g_Texture_Get('ITEM_AMMO_ROCKET_BOX', gItemsTexturesID[ITEM_AMMO_ROCKET_BOX]);
223 g_Texture_Get('ITEM_AMMO_CELL', gItemsTexturesID[ITEM_AMMO_CELL]);
224 g_Texture_Get('ITEM_AMMO_CELL_BIG', gItemsTexturesID[ITEM_AMMO_CELL_BIG]);
225 g_Texture_Get('ITEM_AMMO_FUELCAN', gItemsTexturesID[ITEM_AMMO_FUELCAN]);
226 g_Texture_Get('ITEM_AMMO_BACKPACK', gItemsTexturesID[ITEM_AMMO_BACKPACK]);
227 g_Texture_Get('ITEM_KEY_RED', gItemsTexturesID[ITEM_KEY_RED]);
228 g_Texture_Get('ITEM_KEY_GREEN', gItemsTexturesID[ITEM_KEY_GREEN]);
229 g_Texture_Get('ITEM_KEY_BLUE', gItemsTexturesID[ITEM_KEY_BLUE]);
230 g_Texture_Get('ITEM_WEAPON_KASTET', gItemsTexturesID[ITEM_WEAPON_KASTET]);
231 g_Texture_Get('ITEM_WEAPON_PISTOL', gItemsTexturesID[ITEM_WEAPON_PISTOL]);
232 end;
234 procedure g_Items_LoadData();
235 begin
236 e_WriteLog('Loading items data...', TMsgType.Notify);
238 g_Sound_CreateWADEx('SOUND_ITEM_RESPAWNITEM', GameWAD+':SOUNDS\RESPAWNITEM');
239 g_Sound_CreateWADEx('SOUND_ITEM_GETRULEZ', GameWAD+':SOUNDS\GETRULEZ');
240 g_Sound_CreateWADEx('SOUND_ITEM_GETWEAPON', GameWAD+':SOUNDS\GETWEAPON');
241 g_Sound_CreateWADEx('SOUND_ITEM_GETITEM', GameWAD+':SOUNDS\GETITEM');
243 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BLUESPHERE', GameWAD+':TEXTURES\SBLUE', 32, 32, 4, True);
244 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_WHITESPHERE', GameWAD+':TEXTURES\SWHITE', 32, 32, 4, True);
245 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORGREEN', GameWAD+':TEXTURES\ARMORGREEN', 32, 16, 3, True);
246 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_ARMORBLUE', GameWAD+':TEXTURES\ARMORBLUE', 32, 16, 3, True);
247 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_JETPACK', GameWAD+':TEXTURES\JETPACK', 32, 32, 3, True);
248 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVUL', GameWAD+':TEXTURES\INVUL', 32, 32, 4, True);
249 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_INVIS', GameWAD+':TEXTURES\INVIS', 32, 32, 4, True);
250 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_RESPAWN', GameWAD+':TEXTURES\ITEMRESPAWN', 32, 32, 5, True);
251 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_BOTTLE', GameWAD+':TEXTURES\BOTTLE', 16, 32, 4, True);
252 g_Frames_CreateWAD(nil, 'FRAMES_ITEM_HELMET', GameWAD+':TEXTURES\HELMET', 16, 16, 4, True);
253 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_RED', GameWAD+':TEXTURES\FLAGRED', 64, 64, 5, False);
254 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_BLUE', GameWAD+':TEXTURES\FLAGBLUE', 64, 64, 5, False);
255 g_Frames_CreateWAD(nil, 'FRAMES_FLAG_DOM', GameWAD+':TEXTURES\FLAGDOM', 64, 64, 5, False);
256 g_Texture_CreateWADEx('ITEM_MEDKIT_SMALL', GameWAD+':TEXTURES\MED1');
257 g_Texture_CreateWADEx('ITEM_MEDKIT_LARGE', GameWAD+':TEXTURES\MED2');
258 g_Texture_CreateWADEx('ITEM_WEAPON_SAW', GameWAD+':TEXTURES\SAW');
259 g_Texture_CreateWADEx('ITEM_WEAPON_PISTOL', GameWAD+':TEXTURES\PISTOL');
260 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
261 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN1', GameWAD+':TEXTURES\SHOTGUN1');
262 g_Texture_CreateWADEx('ITEM_WEAPON_SHOTGUN2', GameWAD+':TEXTURES\SHOTGUN2');
263 g_Texture_CreateWADEx('ITEM_WEAPON_CHAINGUN', GameWAD+':TEXTURES\MGUN');
264 g_Texture_CreateWADEx('ITEM_WEAPON_ROCKETLAUNCHER', GameWAD+':TEXTURES\RLAUNCHER');
265 g_Texture_CreateWADEx('ITEM_WEAPON_PLASMA', GameWAD+':TEXTURES\PGUN');
266 g_Texture_CreateWADEx('ITEM_WEAPON_BFG', GameWAD+':TEXTURES\BFG');
267 g_Texture_CreateWADEx('ITEM_WEAPON_SUPERPULEMET', GameWAD+':TEXTURES\SPULEMET');
268 g_Texture_CreateWADEx('ITEM_WEAPON_FLAMETHROWER', GameWAD+':TEXTURES\FLAMETHROWER');
269 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS', GameWAD+':TEXTURES\CLIP');
270 g_Texture_CreateWADEx('ITEM_AMMO_BULLETS_BOX', GameWAD+':TEXTURES\AMMO');
271 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS', GameWAD+':TEXTURES\SHELL1');
272 g_Texture_CreateWADEx('ITEM_AMMO_SHELLS_BOX', GameWAD+':TEXTURES\SHELL2');
273 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET', GameWAD+':TEXTURES\ROCKET');
274 g_Texture_CreateWADEx('ITEM_AMMO_ROCKET_BOX', GameWAD+':TEXTURES\ROCKETS');
275 g_Texture_CreateWADEx('ITEM_AMMO_CELL', GameWAD+':TEXTURES\CELL');
276 g_Texture_CreateWADEx('ITEM_AMMO_CELL_BIG', GameWAD+':TEXTURES\CELL2');
277 g_Texture_CreateWADEx('ITEM_AMMO_FUELCAN', GameWAD+':TEXTURES\FUELCAN');
278 g_Texture_CreateWADEx('ITEM_AMMO_BACKPACK', GameWAD+':TEXTURES\BPACK');
279 g_Texture_CreateWADEx('ITEM_KEY_RED', GameWAD+':TEXTURES\KEYR');
280 g_Texture_CreateWADEx('ITEM_KEY_GREEN', GameWAD+':TEXTURES\KEYG');
281 g_Texture_CreateWADEx('ITEM_KEY_BLUE', GameWAD+':TEXTURES\KEYB');
282 g_Texture_CreateWADEx('ITEM_OXYGEN', GameWAD+':TEXTURES\OXYGEN');
283 g_Texture_CreateWADEx('ITEM_SUIT', GameWAD+':TEXTURES\SUIT');
284 g_Texture_CreateWADEx('ITEM_WEAPON_KASTET', GameWAD+':TEXTURES\KASTET');
285 g_Texture_CreateWADEx('ITEM_MEDKIT_BLACK', GameWAD+':TEXTURES\BMED');
287 InitTextures();
289 freeIds := TIdPool.Create();
290 end;
293 procedure g_Items_FreeData();
294 begin
295 e_WriteLog('Releasing items data...', TMsgType.Notify);
297 g_Sound_Delete('SOUND_ITEM_RESPAWNITEM');
298 g_Sound_Delete('SOUND_ITEM_GETRULEZ');
299 g_Sound_Delete('SOUND_ITEM_GETWEAPON');
300 g_Sound_Delete('SOUND_ITEM_GETITEM');
302 g_Frames_DeleteByName('FRAMES_ITEM_BLUESPHERE');
303 g_Frames_DeleteByName('FRAMES_ITEM_WHITESPHERE');
304 g_Frames_DeleteByName('FRAMES_ITEM_ARMORGREEN');
305 g_Frames_DeleteByName('FRAMES_ITEM_ARMORBLUE');
306 g_Frames_DeleteByName('FRAMES_ITEM_JETPACK');
307 g_Frames_DeleteByName('FRAMES_ITEM_INVUL');
308 g_Frames_DeleteByName('FRAMES_ITEM_INVIS');
309 g_Frames_DeleteByName('FRAMES_ITEM_RESPAWN');
310 g_Frames_DeleteByName('FRAMES_ITEM_BOTTLE');
311 g_Frames_DeleteByName('FRAMES_ITEM_HELMET');
312 g_Frames_DeleteByName('FRAMES_FLAG_RED');
313 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
314 g_Frames_DeleteByName('FRAMES_FLAG_DOM');
315 g_Texture_Delete('ITEM_MEDKIT_SMALL');
316 g_Texture_Delete('ITEM_MEDKIT_LARGE');
317 g_Texture_Delete('ITEM_WEAPON_SAW');
318 g_Texture_Delete('ITEM_WEAPON_PISTOL');
319 g_Texture_Delete('ITEM_WEAPON_KASTET');
320 g_Texture_Delete('ITEM_WEAPON_SHOTGUN1');
321 g_Texture_Delete('ITEM_WEAPON_SHOTGUN2');
322 g_Texture_Delete('ITEM_WEAPON_CHAINGUN');
323 g_Texture_Delete('ITEM_WEAPON_ROCKETLAUNCHER');
324 g_Texture_Delete('ITEM_WEAPON_PLASMA');
325 g_Texture_Delete('ITEM_WEAPON_BFG');
326 g_Texture_Delete('ITEM_WEAPON_SUPERPULEMET');
327 g_Texture_Delete('ITEM_WEAPON_FLAMETHROWER');
328 g_Texture_Delete('ITEM_AMMO_BULLETS');
329 g_Texture_Delete('ITEM_AMMO_BULLETS_BOX');
330 g_Texture_Delete('ITEM_AMMO_SHELLS');
331 g_Texture_Delete('ITEM_AMMO_SHELLS_BOX');
332 g_Texture_Delete('ITEM_AMMO_ROCKET');
333 g_Texture_Delete('ITEM_AMMO_ROCKET_BOX');
334 g_Texture_Delete('ITEM_AMMO_CELL');
335 g_Texture_Delete('ITEM_AMMO_CELL_BIG');
336 g_Texture_Delete('ITEM_AMMO_FUELCAN');
337 g_Texture_Delete('ITEM_AMMO_BACKPACK');
338 g_Texture_Delete('ITEM_KEY_RED');
339 g_Texture_Delete('ITEM_KEY_GREEN');
340 g_Texture_Delete('ITEM_KEY_BLUE');
341 g_Texture_Delete('ITEM_OXYGEN');
342 g_Texture_Delete('ITEM_SUIT');
343 g_Texture_Delete('ITEM_WEAPON_KASTET');
344 g_Texture_Delete('ITEM_MEDKIT_BLACK');
346 freeIds.Free();
347 freeIds := nil;
348 end;
351 procedure releaseItem (idx: Integer);
352 var
353 it: PItem;
354 begin
355 if (idx < 0) or (idx > High(ggItems)) then raise Exception.Create('releaseItem: invalid item id');
356 if not freeIds.hasAlloced[LongWord(idx)] then raise Exception.Create('releaseItem: trying to release unallocated item (0)');
357 it := @ggItems[idx];
358 if not it.slotIsUsed then raise Exception.Create('releaseItem: trying to release unallocated item (1)');
359 if (it.arrIdx <> idx) then raise Exception.Create('releaseItem: arrIdx inconsistency');
360 it.slotIsUsed := false;
361 if (it.Animation <> nil) then
362 begin
363 it.Animation.Free();
364 it.Animation := nil;
365 end;
366 it.alive := False;
367 it.SpawnTrigger := -1;
368 it.ItemType := ITEM_NONE;
369 it.NeedSend := false;
370 freeIds.release(LongWord(idx));
371 end;
374 procedure growItemArrayTo (newsz: Integer);
375 var
376 i, olen: Integer;
377 it: PItem;
378 begin
379 if (newsz < Length(ggItems)) then exit;
380 // no free slots
381 olen := Length(ggItems);
382 SetLength(ggItems, newsz);
383 for i := olen to High(ggItems) do
384 begin
385 it := @ggItems[i];
386 it.slotIsUsed := false;
387 it.arrIdx := i;
388 it.ItemType := ITEM_NONE;
389 it.Animation := nil;
390 it.alive := false;
391 it.SpawnTrigger := -1;
392 it.Respawnable := false;
393 it.NeedSend := false;
394 //if not freeIds.hasFree[LongWord(i)] then raise Exception.Create('internal error in item idx manager');
395 end;
396 end;
399 function allocItem (): DWORD;
400 begin
401 result := freeIds.alloc();
402 if (result >= Length(ggItems)) then growItemArrayTo(Integer(result)+64);
403 if (Integer(result) > High(ggItems)) then raise Exception.Create('allocItem: freeid list corrupted');
404 if (ggItems[result].arrIdx <> Integer(result)) then raise Exception.Create('allocItem: arrIdx inconsistency');
405 end;
408 // it will be slow if the slot is free (we have to rebuild the heap)
409 function wantItemSlot (slot: Integer): Integer;
410 var
411 olen: Integer;
412 it: PItem;
413 begin
414 if (slot < 0) or (slot > $0fffffff) then raise Exception.Create('wantItemSlot: bad item slot request');
415 // do we need to grow item storate?
416 olen := Length(ggItems);
417 if (slot >= olen) then growItemArrayTo(slot+64);
419 it := @ggItems[slot];
420 if not it.slotIsUsed then
421 begin
422 freeIds.alloc(LongWord(slot));
423 end
424 else
425 begin
426 if not freeIds.hasAlloced[slot] then raise Exception.Create('wantItemSlot: internal error in item idx manager');
427 end;
428 it.slotIsUsed := false;
430 result := slot;
431 end;
434 // ////////////////////////////////////////////////////////////////////////// //
435 procedure g_Items_Init ();
436 var
437 a, b: Integer;
438 begin
439 if gMapInfo.Height > gPlayerScreenSize.Y then a := gMapInfo.Height-gPlayerScreenSize.Y else a := gMapInfo.Height;
440 if gMapInfo.Width > gPlayerScreenSize.X then b := gMapInfo.Width-gPlayerScreenSize.X else b := gMapInfo.Width;
441 gMaxDist := Trunc(Hypot(a, b));
442 end;
445 procedure g_Items_Free ();
446 var
447 i: Integer;
448 begin
449 if (ggItems <> nil) then
450 begin
451 for i := 0 to High(ggItems) do ggItems[i].Animation.Free();
452 ggItems := nil;
453 end;
454 freeIds.clear();
455 end;
458 function g_Items_Create (X, Y: Integer; ItemType: Byte;
459 Fall, Respawnable: Boolean; AdjCoord: Boolean = False; ForcedID: Integer = -1): DWORD;
460 var
461 find_id: DWORD;
462 ID: DWORD;
463 it: PItem;
464 begin
465 if ForcedID < 0 then find_id := allocItem() else find_id := wantItemSlot(ForcedID);
467 //{$IF DEFINED(D2F_DEBUG)}e_WriteLog(Format('allocated item #%d', [Integer(find_id)]), MSG_NOTIFY);{$ENDIF}
469 it := @ggItems[find_id];
471 if (it.arrIdx <> Integer(find_id)) then raise Exception.Create('g_Items_Create: arrIdx inconsistency');
472 //it.arrIdx := find_id;
473 it.slotIsUsed := true;
475 it.ItemType := ItemType;
476 it.Respawnable := Respawnable;
477 it.InitX := X;
478 it.InitY := Y;
479 it.RespawnTime := 0;
480 it.Fall := Fall;
481 it.alive := True;
482 it.QuietRespawn := False;
483 it.dropped := false;
484 it.NeedSend := false;
486 g_Obj_Init(@it.Obj);
487 it.Obj.X := X;
488 it.Obj.Y := Y;
489 it.Obj.Rect.Width := ITEMSIZE[ItemType][0];
490 it.Obj.Rect.Height := ITEMSIZE[ItemType][1];
492 it.Animation := nil;
493 it.SpawnTrigger := -1;
495 // Êîîðäèíàòû îòíîñèòåëüíî öåíòðà íèæíåãî ðåáðà
496 if AdjCoord then
497 begin
498 with it^ do
499 begin
500 Obj.X := X - (Obj.Rect.Width div 2);
501 Obj.Y := Y - Obj.Rect.Height;
502 InitX := Obj.X;
503 InitY := Obj.Y;
504 end;
505 end;
507 it.Obj.oldX := it.Obj.X;
508 it.Obj.oldY := it.Obj.Y;
510 // Óñòàíîâêà àíèìàöèè
511 case it.ItemType of
512 ITEM_ARMOR_GREEN: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORGREEN') then it.Animation := TAnimation.Create(ID, True, 20);
513 ITEM_ARMOR_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_ARMORBLUE') then it.Animation := TAnimation.Create(ID, True, 20);
514 ITEM_JETPACK: if g_Frames_Get(ID, 'FRAMES_ITEM_JETPACK') then it.Animation := TAnimation.Create(ID, True, 15);
515 ITEM_SPHERE_BLUE: if g_Frames_Get(ID, 'FRAMES_ITEM_BLUESPHERE') then it.Animation := TAnimation.Create(ID, True, 15);
516 ITEM_SPHERE_WHITE: if g_Frames_Get(ID, 'FRAMES_ITEM_WHITESPHERE') then it.Animation := TAnimation.Create(ID, True, 20);
517 ITEM_INVUL: if g_Frames_Get(ID, 'FRAMES_ITEM_INVUL') then it.Animation := TAnimation.Create(ID, True, 20);
518 ITEM_INVIS: if g_Frames_Get(ID, 'FRAMES_ITEM_INVIS') then it.Animation := TAnimation.Create(ID, True, 20);
519 ITEM_BOTTLE: if g_Frames_Get(ID, 'FRAMES_ITEM_BOTTLE') then it.Animation := TAnimation.Create(ID, True, 20);
520 ITEM_HELMET: if g_Frames_Get(ID, 'FRAMES_ITEM_HELMET') then it.Animation := TAnimation.Create(ID, True, 20);
521 end;
523 it.positionChanged();
525 result := find_id;
526 end;
528 procedure g_Items_PreUpdate ();
529 var
530 i: Integer;
531 begin
532 if (ggItems = nil) then Exit;
533 for i := 0 to High(ggItems) do
534 if (ggItems[i].ItemType <> ITEM_NONE) and ggItems[i].slotIsUsed then
535 begin
536 ggItems[i].Obj.oldX := ggItems[i].Obj.X;
537 ggItems[i].Obj.oldY := ggItems[i].Obj.Y;
538 end;
539 end;
541 procedure g_Items_Update ();
542 var
543 i, j, k: Integer;
544 ID: DWord;
545 Anim: TAnimation;
546 m, ItemRespawnTime: Word;
547 r, nxt: Boolean;
548 begin
549 if (ggItems = nil) then exit;
551 // respawn items in 15 seconds regardless of settings during warmup
552 ItemRespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15);
554 for i := 0 to High(ggItems) do
555 begin
556 if (ggItems[i].ItemType = ITEM_NONE) then continue;
557 if not ggItems[i].slotIsUsed then continue; // just in case
559 with ggItems[i] do
560 begin
561 nxt := False;
563 if alive then
564 begin
565 if Fall then
566 begin
567 m := g_Obj_Move(@Obj, True, True);
568 positionChanged(); // this updates spatial accelerators
570 // Ñîïðîòèâëåíèå âîçäóõà
571 if gTime mod (GAME_TICK*2) = 0 then Obj.Vel.X := z_dec(Obj.Vel.X, 1);
573 // Åñëè âûïàë çà êàðòó
574 if WordBool(m and MOVE_FALLOUT) then
575 begin
576 if SpawnTrigger = -1 then
577 begin
578 g_Items_Pick(i);
579 end
580 else
581 begin
582 g_Items_Remove(i);
583 if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
584 end;
585 continue;
586 end;
587 end;
589 // Åñëè èãðîêè ïîáëèçîñòè
590 if (gPlayers <> nil) then
591 begin
592 j := Random(Length(gPlayers))-1;
594 for k := 0 to High(gPlayers) do
595 begin
596 Inc(j);
597 if j > High(gPlayers) then j := 0;
599 if (gPlayers[j] <> nil) and gPlayers[j].alive and g_Obj_Collide(@gPlayers[j].Obj, @Obj) then
600 begin
601 if g_Game_IsClient then continue;
603 if not gPlayers[j].PickItem(ItemType, Respawnable, r) then continue;
605 if g_Game_IsNet then MH_SEND_PlayerStats(gPlayers[j].UID);
608 Doom 2D: Original:
609 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
610 +2. I_MEGA,I_INVL,I_SUPER
611 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
613 g_Items_EmitPickupSoundAt(i, gPlayers[j].Obj.X, gPlayers[j].Obj.Y);
615 // Íàäî óáðàòü ñ êàðòû, åñëè ýòî íå êëþ÷, êîòîðûì íóæíî ïîäåëèòüñÿ ñ äðóãèì èãðîêîì
616 if r then
617 begin
618 if not (Respawnable and (ItemRespawnTime > 0)) then
619 g_Items_Remove(i)
620 else
621 g_Items_Pick(i);
622 if g_Game_IsNet then MH_SEND_ItemDestroy(False, i);
623 nxt := True;
624 break;
625 end;
626 end;
627 end;
628 end;
630 if nxt then continue;
631 end;
633 if Respawnable and g_Game_IsServer then
634 begin
635 DecMin(RespawnTime, 0);
636 if (RespawnTime = 0) and (not alive) then
637 begin
638 if not QuietRespawn then g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', InitX, InitY);
640 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
641 begin
642 Anim := TAnimation.Create(ID, False, 4);
643 g_GFX_OnceAnim(InitX+(Obj.Rect.Width div 2)-16, InitY+(Obj.Rect.Height div 2)-16, Anim);
644 Anim.Free();
645 end;
647 Obj.oldX := InitX;
648 Obj.oldY := InitY;
649 Obj.X := InitX;
650 Obj.Y := InitY;
651 Obj.Vel.X := 0;
652 Obj.Vel.Y := 0;
653 Obj.Accel.X := 0;
654 Obj.Accel.Y := 0;
655 positionChanged(); // this updates spatial accelerators
657 alive := true;
659 if g_Game_IsNet then MH_SEND_ItemSpawn(QuietRespawn, i);
660 QuietRespawn := false;
661 end;
662 end;
664 if (Animation <> nil) then Animation.Update();
665 end;
666 end;
667 end;
669 procedure g_Items_SetDrop (ID: DWORD);
670 begin
671 if (ID < Length(ggItems)) then
672 begin
673 ggItems[ID].dropped := true;
674 end;
675 end;
678 procedure g_Items_Pick (ID: DWORD);
679 begin
680 if (ID < Length(ggItems)) then
681 begin
682 ggItems[ID].Obj.oldX := ggItems[ID].Obj.X;
683 ggItems[ID].Obj.oldY := ggItems[ID].Obj.Y;
684 ggItems[ID].alive := false;
685 ggItems[ID].RespawnTime := IfThen(gLMSRespawn = LMS_RESPAWN_NONE, gGameSettings.ItemRespawnTime, 15) * 36;
686 end;
687 end;
690 procedure g_Items_Remove (ID: DWORD);
691 var
692 it: PItem;
693 trig: Integer;
694 begin
695 if not g_Items_ValidId(ID) then
696 begin
697 //writeln('g_Items_Remove: invalid item id: ', ID);
698 raise Exception.Create('g_Items_Remove: invalid item id');
699 //exit;
700 end;
702 it := @ggItems[ID];
703 if (it.arrIdx <> Integer(ID)) then raise Exception.Create('g_Items_Remove: arrIdx desync');
705 it.Obj.oldX := it.Obj.X;
706 it.Obj.oldY := it.Obj.Y;
707 trig := it.SpawnTrigger;
709 releaseItem(ID);
711 if (trig > -1) then g_Triggers_DecreaseSpawner(trig);
712 end;
715 procedure g_Items_SaveState (st: TStream);
716 var
717 count, i: Integer;
718 tt: Byte;
719 begin
720 // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ïðåäìåòîâ
721 count := 0;
722 for i := 0 to High(ggItems) do if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then Inc(count);
724 // Êîëè÷åñòâî ïðåäìåòîâ
725 utils.writeInt(st, LongInt(count));
726 if (count = 0) then exit;
728 for i := 0 to High(ggItems) do
729 begin
730 if (ggItems[i].ItemType <> ITEM_NONE) and (ggItems[i].slotIsUsed) then
731 begin
732 // Ñèãíàòóðà ïðåäìåòà
733 utils.writeSign(st, 'ITEM');
734 utils.writeInt(st, Byte(0));
735 // Òèï ïðåäìåòà
736 tt := ggItems[i].ItemType;
737 if ggItems[i].dropped then tt := tt or $80;
738 utils.writeInt(st, Byte(tt));
739 // Åñòü ëè ðåñïàóí
740 utils.writeBool(st, ggItems[i].Respawnable);
741 // Êîîðäèíàòû ðåñïóíà
742 utils.writeInt(st, LongInt(ggItems[i].InitX));
743 utils.writeInt(st, LongInt(ggItems[i].InitY));
744 // Âðåìÿ äî ðåñïàóíà
745 utils.writeInt(st, Word(ggItems[i].RespawnTime));
746 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
747 utils.writeBool(st, ggItems[i].alive);
748 // Ìîæåò ëè îí ïàäàòü
749 utils.writeBool(st, ggItems[i].Fall);
750 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
751 utils.writeInt(st, LongInt(ggItems[i].SpawnTrigger));
752 // Îáúåêò ïðåäìåòà
753 Obj_SaveState(st, @ggItems[i].Obj);
754 end;
755 end;
756 end;
759 procedure g_Items_LoadState (st: TStream);
760 var
761 count, i, a: Integer;
762 b: Byte;
763 begin
764 assert(st <> nil);
766 g_Items_Free();
768 // Êîëè÷åñòâî ïðåäìåòîâ
769 count := utils.readLongInt(st);
770 if (count = 0) then exit;
771 if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid number of items');
773 for a := 0 to count-1 do
774 begin
775 // Ñèãíàòóðà ïðåäìåòà
776 if not utils.checkSign(st, 'ITEM') then raise XStreamError.Create('invalid item signature');
777 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid item version');
778 // Òèï ïðåäìåòà
779 b := utils.readByte(st); // bit7=1: monster drop
780 // Ñîçäàåì ïðåäìåò
781 i := g_Items_Create(0, 0, b and $7F, False, False);
782 if ((b and $80) <> 0) then g_Items_SetDrop(i);
783 // Åñòü ëè ðåñïàóí
784 ggItems[i].Respawnable := utils.readBool(st);
785 // Êîîðäèíàòû ðåñïóíà
786 ggItems[i].InitX := utils.readLongInt(st);
787 ggItems[i].InitY := utils.readLongInt(st);
788 // Âðåìÿ äî ðåñïàóíà
789 ggItems[i].RespawnTime := utils.readWord(st);
790 // Ñóùåñòâóåò ëè ýòîò ïðåäìåò
791 ggItems[i].alive := utils.readBool(st);
792 // Ìîæåò ëè îí ïàäàòü
793 ggItems[i].Fall := utils.readBool(st);
794 // Èíäåêñ òðèããåðà, ñîçäàâøåãî ïðåäìåò
795 ggItems[i].SpawnTrigger := utils.readLongInt(st);
796 // Îáúåêò ïðåäìåòà
797 Obj_LoadState(@ggItems[i].Obj, st);
798 end;
799 end;
802 procedure g_Items_RestartRound ();
803 var
804 i: Integer;
805 it: PItem;
806 begin
807 for i := 0 to High(ggItems) do
808 begin
809 it := @ggItems[i];
810 it.Obj.oldX := it.Obj.X;
811 it.Obj.oldY := it.Obj.Y;
812 if not it.slotIsUsed then continue;
813 if it.Respawnable and (it.ItemType <> ITEM_NONE) then
814 begin
815 it.QuietRespawn := True;
816 it.RespawnTime := 0;
817 end
818 else
819 begin
820 g_Items_Remove(i);
821 if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
822 end;
823 end;
824 end;
827 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
828 var
829 idx: Integer;
830 begin
831 result := false;
832 if (ggItems = nil) or not assigned(cb) then exit;
834 if backwards then
835 begin
836 for idx := High(ggItems) downto 0 do
837 begin
838 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
839 begin
840 result := cb(@ggItems[idx]);
841 if result then exit;
842 end;
843 end;
844 end
845 else
846 begin
847 for idx := 0 to High(ggItems) do
848 begin
849 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
850 begin
851 result := cb(@ggItems[idx]);
852 if result then exit;
853 end;
854 end;
855 end;
856 end;
858 function g_Items_NextAlive (startIdx: Integer): PItem;
859 var
860 idx: Integer;
861 begin
862 result := nil;
863 if (ggItems = nil) or (startIdx >= High(ggItems)) then exit;
864 for idx := startIdx + 1 to High(ggItems) do
865 if ggItems[idx].alive and ggItems[idx].slotIsUsed then
866 begin
867 result := @ggItems[idx];
868 exit;
869 end;
870 end;
872 // ////////////////////////////////////////////////////////////////////////// //
873 procedure g_Items_EmitPickupSound (idx: Integer);
874 var
875 it: PItem;
876 begin
877 if not g_Items_ValidId(idx) then exit;
878 it := @ggItems[idx];
879 g_Items_EmitPickupSoundAt(idx, it.Obj.X, it.Obj.Y);
880 end;
882 procedure g_Items_EmitPickupSoundAt (idx, x, y: Integer);
883 var
884 it: PItem;
885 begin
886 if not g_Items_ValidId(idx) then exit;
888 it := @ggItems[idx];
889 if gSoundEffectsDF then
890 begin
891 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
892 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
893 begin
894 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
895 end
896 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
897 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
898 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER,
899 ITEM_AMMO_BACKPACK] then
900 begin
901 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
902 end
903 else
904 begin
905 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
906 end;
907 end
908 else
909 begin
910 if it.ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
911 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
912 begin
913 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', x, y);
914 end
915 else if it.ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
916 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
917 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_WEAPON_FLAMETHROWER] then
918 begin
919 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON', x, y);
920 end
921 else
922 begin
923 g_Sound_PlayExAt('SOUND_ITEM_GETITEM', x, y);
924 end;
925 end;
926 end;
929 procedure g_Items_AddDynLights();
930 var
931 f: Integer;
932 it: PItem;
933 begin
934 for f := 0 to High(ggItems) do
935 begin
936 it := @ggItems[f];
937 if not it.alive then continue;
938 case it.ItemType of
939 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);
940 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);
941 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);
942 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);
943 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);
944 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);
945 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);
946 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);
947 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);
948 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);
949 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);
950 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);
951 end;
952 end;
953 end;
956 end.