288
|
1 |
unit uAmmos;
|
|
2 |
interface
|
|
3 |
uses uConsts;
|
|
4 |
{$INCLUDE options.inc}
|
|
5 |
type PHHAmmo = ^THHAmmo;
|
|
6 |
THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo;
|
|
7 |
|
|
8 |
procedure AddAmmoStore(s: shortstring);
|
|
9 |
procedure AssignStores;
|
295
|
10 |
procedure AddAmmo(Hedgehog: pointer; ammo: TAmmoType);
|
|
11 |
function HHHasAmmo(Hedgehog: pointer; Ammo: TAmmoType): boolean;
|
|
12 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer);
|
|
13 |
procedure OnUsedAmmo(Ammo: PHHAmmo);
|
288
|
14 |
|
|
15 |
implementation
|
295
|
16 |
uses uMisc, uTeams, uGears;
|
|
17 |
type TAmmoCounts = array[TAmmoType] of Longword;
|
288
|
18 |
var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo;
|
|
19 |
StoreCnt: Longword = 0;
|
|
20 |
|
295
|
21 |
procedure FillAmmoStore(Ammo: PHHAmmo; var cnts: TAmmoCounts);
|
288
|
22 |
var mi: array[0..cMaxSlotIndex] of byte;
|
|
23 |
a: TAmmoType;
|
295
|
24 |
begin
|
|
25 |
FillChar(mi, sizeof(mi), 0);
|
|
26 |
FillChar(Ammo^, sizeof(Ammo^), 0);
|
|
27 |
for a:= Low(TAmmoType) to High(TAmmoType) do
|
|
28 |
if cnts[a] > 0 then
|
|
29 |
begin
|
|
30 |
TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true);
|
351
|
31 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo;
|
|
32 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]].Count:= cnts[a];
|
295
|
33 |
inc(mi[Ammoz[a].Slot])
|
|
34 |
end
|
|
35 |
end;
|
|
36 |
|
|
37 |
procedure AddAmmoStore(s: shortstring);
|
|
38 |
var cnt: Longword;
|
|
39 |
a: TAmmoType;
|
|
40 |
ammos: TAmmoCounts;
|
288
|
41 |
begin
|
|
42 |
TryDo(byte(s[0]) = byte(ord(High(TAmmoType)) + 1), 'Invalid ammo scheme (incompatible frontend)', true);
|
|
43 |
|
|
44 |
inc(StoreCnt);
|
|
45 |
TryDo(StoreCnt <= cMaxHHs, 'Ammo stores overflow', true);
|
|
46 |
|
|
47 |
new(StoresList[Pred(StoreCnt)]);
|
|
48 |
|
|
49 |
for a:= Low(TAmmoType) to High(TAmmoType) do
|
|
50 |
begin
|
|
51 |
cnt:= byte(s[ord(a) + 1]) - byte('0');
|
295
|
52 |
if cnt = 9 then cnt:= AMMO_INFINITE;
|
|
53 |
ammos[a]:= cnt
|
288
|
54 |
end;
|
295
|
55 |
|
|
56 |
FillAmmoStore(StoresList[Pred(StoreCnt)], ammos)
|
288
|
57 |
end;
|
|
58 |
|
|
59 |
function GetAmmoByNum(num: Longword): PHHAmmo;
|
|
60 |
begin
|
|
61 |
TryDo(num < StoreCnt, 'Invalid store number', true);
|
351
|
62 |
exit(StoresList[num])
|
288
|
63 |
end;
|
|
64 |
|
|
65 |
procedure AssignStores;
|
|
66 |
var tteam: PTeam;
|
|
67 |
i: Longword;
|
|
68 |
begin
|
|
69 |
tteam:= TeamsList;
|
|
70 |
while tteam <> nil do
|
|
71 |
begin
|
|
72 |
for i:= 0 to cMaxHHIndex do
|
351
|
73 |
if tteam^.Hedgehogs[i].Gear <> nil then
|
|
74 |
tteam^.Hedgehogs[i].Ammo:= GetAmmoByNum(tteam^.Hedgehogs[i].AmmoStore);
|
|
75 |
tteam:= tteam^.Next
|
288
|
76 |
end
|
|
77 |
end;
|
|
78 |
|
295
|
79 |
procedure AddAmmo(Hedgehog: pointer; ammo: TAmmoType);
|
|
80 |
var ammos: TAmmoCounts;
|
|
81 |
slot, ami: integer;
|
|
82 |
hhammo: PHHAmmo;
|
|
83 |
begin
|
|
84 |
FillChar(ammos, sizeof(ammos), 0);
|
351
|
85 |
hhammo:= PHedgehog(Hedgehog)^.Ammo;
|
295
|
86 |
|
|
87 |
for slot:= 0 to cMaxSlotIndex do
|
|
88 |
for ami:= 0 to cMaxSlotAmmoIndex do
|
351
|
89 |
if hhammo^[slot, ami].Count > 0 then
|
|
90 |
ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami].Count;
|
295
|
91 |
|
|
92 |
if ammos[ammo] <> AMMO_INFINITE then inc(ammos[ammo]);
|
|
93 |
FillAmmoStore(hhammo, ammos)
|
|
94 |
end;
|
|
95 |
|
|
96 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer);
|
|
97 |
var ami: integer;
|
|
98 |
b: boolean;
|
|
99 |
begin
|
|
100 |
repeat
|
|
101 |
b:= false;
|
|
102 |
ami:= 0;
|
|
103 |
while (not b) and (ami < cMaxSlotAmmoIndex) do
|
351
|
104 |
if (Ammo^[Slot, ami].Count = 0)
|
|
105 |
and (Ammo^[Slot, ami + 1].Count > 0) then b:= true
|
295
|
106 |
else inc(ami);
|
|
107 |
if b then // there's a free item in ammo stack
|
|
108 |
begin
|
351
|
109 |
Ammo^[Slot, ami]:= Ammo^[Slot, ami + 1];
|
|
110 |
Ammo^[Slot, ami + 1].Count:= 0
|
295
|
111 |
end;
|
|
112 |
until not b;
|
|
113 |
end;
|
|
114 |
|
|
115 |
procedure OnUsedAmmo(Ammo: PHHAmmo);
|
|
116 |
var s, a: Longword;
|
|
117 |
begin
|
351
|
118 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do
|
295
|
119 |
begin
|
|
120 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end
|
|
121 |
else begin s:= AltSlot; a:= AltAmmo end;
|
351
|
122 |
with Ammo^[s, a] do
|
295
|
123 |
if Count <> AMMO_INFINITE then
|
|
124 |
begin
|
|
125 |
dec(Count);
|
|
126 |
if Count = 0 then PackAmmo(Ammo, CurSlot)
|
|
127 |
end
|
|
128 |
end
|
|
129 |
end;
|
|
130 |
|
|
131 |
function HHHasAmmo(Hedgehog: pointer; Ammo: TAmmoType): boolean;
|
|
132 |
var slot, ami: integer;
|
|
133 |
begin
|
|
134 |
Slot:= Ammoz[Ammo].Slot;
|
|
135 |
ami:= 0;
|
351
|
136 |
while (ami <= cMaxSlotAmmoIndex) do
|
295
|
137 |
begin
|
351
|
138 |
with PHedgehog(Hedgehog)^.Ammo^[Slot, ami] do
|
|
139 |
if (AmmoType = Ammo) and (Count > 0) then exit(true);
|
295
|
140 |
inc(ami)
|
351
|
141 |
end;
|
|
142 |
HHHasAmmo:= false
|
295
|
143 |
end;
|
|
144 |
|
288
|
145 |
end.
|