author | unc0rr |
Sun, 12 Apr 2009 12:50:43 +0000 | |
changeset 1964 | dc9ea05c9d2f |
parent 1922 | 88cdabb51995 |
child 1966 | 31e449e1d9dd |
permissions | -rw-r--r-- |
393 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2006-2008 Andrey Korotaev <unC0Rr@gmail.com> |
393 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
288 | 19 |
unit uAmmos; |
20 |
interface |
|
534 | 21 |
uses uConsts, uTeams; |
288 | 22 |
{$INCLUDE options.inc} |
23 |
||
24 |
procedure AddAmmoStore(s: shortstring); |
|
25 |
procedure AssignStores; |
|
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
26 |
procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType); |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
27 |
function HHHasAmmo(var Hedgehog: THedgehog; Ammo: TAmmoType): boolean; |
371 | 28 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt); |
534 | 29 |
procedure OnUsedAmmo(var Hedgehog: THedgehog); |
1964 | 30 |
procedure ApplyAngleBounds(var Hedgehog: THedgehog; AmmoType: TAmmoType); |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
31 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
1922 | 32 |
procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog); |
783 | 33 |
procedure SetWeapon(weap: TAmmoType); |
1784 | 34 |
procedure DisableSomeWeapons; |
288 | 35 |
|
36 |
implementation |
|
783 | 37 |
uses uMisc, uGears, uWorld, uLocale, uConsole; |
295 | 38 |
type TAmmoCounts = array[TAmmoType] of Longword; |
288 | 39 |
var StoresList: array[0..Pred(cMaxHHs)] of PHHAmmo; |
40 |
StoreCnt: Longword = 0; |
|
41 |
||
295 | 42 |
procedure FillAmmoStore(Ammo: PHHAmmo; var cnts: TAmmoCounts); |
288 | 43 |
var mi: array[0..cMaxSlotIndex] of byte; |
44 |
a: TAmmoType; |
|
295 | 45 |
begin |
46 |
FillChar(mi, sizeof(mi), 0); |
|
47 |
FillChar(Ammo^, sizeof(Ammo^), 0); |
|
48 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
|
49 |
if cnts[a] > 0 then |
|
50 |
begin |
|
51 |
TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true); |
|
351 | 52 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo; |
53 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]].Count:= cnts[a]; |
|
295 | 54 |
inc(mi[Ammoz[a].Slot]) |
55 |
end |
|
56 |
end; |
|
57 |
||
58 |
procedure AddAmmoStore(s: shortstring); |
|
59 |
var cnt: Longword; |
|
60 |
a: TAmmoType; |
|
61 |
ammos: TAmmoCounts; |
|
288 | 62 |
begin |
63 |
TryDo(byte(s[0]) = byte(ord(High(TAmmoType)) + 1), 'Invalid ammo scheme (incompatible frontend)', true); |
|
64 |
||
65 |
inc(StoreCnt); |
|
66 |
TryDo(StoreCnt <= cMaxHHs, 'Ammo stores overflow', true); |
|
67 |
||
68 |
new(StoresList[Pred(StoreCnt)]); |
|
69 |
||
70 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
|
71 |
begin |
|
72 |
cnt:= byte(s[ord(a) + 1]) - byte('0'); |
|
1865
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1850
diff
changeset
|
73 |
if cnt = 9 then |
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1850
diff
changeset
|
74 |
begin |
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1850
diff
changeset
|
75 |
cnt:= AMMO_INFINITE; |
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1850
diff
changeset
|
76 |
Ammoz[a].Probability:= 0 |
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1850
diff
changeset
|
77 |
end; |
1895 | 78 |
if ((a = amLowGravity) and ((GameFlags and gfLowGravity) <> 0)) or |
79 |
((a = amInvulnerable) and ((GameFlags and gfInvulnerable) <> 0)) or |
|
80 |
((a = amLaserSight) and ((GameFlags and gfLaserSight) <> 0)) then |
|
81 |
begin |
|
82 |
cnt:= 0; |
|
83 |
Ammoz[a].Probability:= 0 |
|
84 |
end; |
|
295 | 85 |
ammos[a]:= cnt |
288 | 86 |
end; |
295 | 87 |
|
88 |
FillAmmoStore(StoresList[Pred(StoreCnt)], ammos) |
|
288 | 89 |
end; |
90 |
||
91 |
function GetAmmoByNum(num: Longword): PHHAmmo; |
|
92 |
begin |
|
93 |
TryDo(num < StoreCnt, 'Invalid store number', true); |
|
351 | 94 |
exit(StoresList[num]) |
288 | 95 |
end; |
96 |
||
97 |
procedure AssignStores; |
|
547 | 98 |
var t: LongInt; |
288 | 99 |
i: Longword; |
100 |
begin |
|
547 | 101 |
for t:= 0 to Pred(TeamsCount) do |
102 |
with TeamsArray[t]^ do |
|
288 | 103 |
begin |
104 |
for i:= 0 to cMaxHHIndex do |
|
547 | 105 |
if Hedgehogs[i].Gear <> nil then |
106 |
Hedgehogs[i].Ammo:= GetAmmoByNum(Hedgehogs[i].AmmoStore); |
|
288 | 107 |
end |
108 |
end; |
|
109 |
||
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
110 |
procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType); |
295 | 111 |
var ammos: TAmmoCounts; |
371 | 112 |
slot, ami: LongInt; |
295 | 113 |
hhammo: PHHAmmo; |
114 |
begin |
|
115 |
FillChar(ammos, sizeof(ammos), 0); |
|
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
116 |
hhammo:= Hedgehog.Ammo; |
295 | 117 |
|
118 |
for slot:= 0 to cMaxSlotIndex do |
|
119 |
for ami:= 0 to cMaxSlotAmmoIndex do |
|
351 | 120 |
if hhammo^[slot, ami].Count > 0 then |
121 |
ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami].Count; |
|
295 | 122 |
|
941
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
123 |
if ammos[ammo] <> AMMO_INFINITE then |
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
124 |
begin |
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
125 |
inc(ammos[ammo], Ammoz[ammo].NumberInCase); |
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
126 |
if ammos[ammo] > AMMO_INFINITE then ammos[ammo]:= AMMO_INFINITE |
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
127 |
end; |
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
128 |
|
295 | 129 |
FillAmmoStore(hhammo, ammos) |
130 |
end; |
|
131 |
||
371 | 132 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt); |
133 |
var ami: LongInt; |
|
295 | 134 |
b: boolean; |
135 |
begin |
|
136 |
repeat |
|
137 |
b:= false; |
|
138 |
ami:= 0; |
|
139 |
while (not b) and (ami < cMaxSlotAmmoIndex) do |
|
351 | 140 |
if (Ammo^[Slot, ami].Count = 0) |
141 |
and (Ammo^[Slot, ami + 1].Count > 0) then b:= true |
|
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
142 |
else inc(ami); |
295 | 143 |
if b then // there's a free item in ammo stack |
144 |
begin |
|
351 | 145 |
Ammo^[Slot, ami]:= Ammo^[Slot, ami + 1]; |
146 |
Ammo^[Slot, ami + 1].Count:= 0 |
|
295 | 147 |
end; |
148 |
until not b; |
|
149 |
end; |
|
150 |
||
534 | 151 |
procedure OnUsedAmmo(var Hedgehog: THedgehog); |
295 | 152 |
begin |
534 | 153 |
with Hedgehog do |
971
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
154 |
begin |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
155 |
with Ammo^[CurSlot, CurAmmo] do |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
156 |
if Count <> AMMO_INFINITE then |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
157 |
begin |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
158 |
dec(Count); |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
159 |
if Count = 0 then |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
160 |
begin |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
161 |
PackAmmo(Ammo, CurSlot); |
1922 | 162 |
SwitchNotHeldAmmo(Hedgehog) |
971
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
163 |
end |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
164 |
end |
d2c49b730771
Fix a bug with automatic weapon switching when attacking from rope
unc0rr
parents:
941
diff
changeset
|
165 |
end |
295 | 166 |
end; |
167 |
||
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
168 |
function HHHasAmmo(var Hedgehog: THedgehog; Ammo: TAmmoType): boolean; |
371 | 169 |
var slot, ami: LongInt; |
295 | 170 |
begin |
171 |
Slot:= Ammoz[Ammo].Slot; |
|
172 |
ami:= 0; |
|
351 | 173 |
while (ami <= cMaxSlotAmmoIndex) do |
295 | 174 |
begin |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
175 |
with Hedgehog.Ammo^[Slot, ami] do |
941
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
176 |
if (AmmoType = Ammo) then |
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
177 |
exit((Count > 0) and (Hedgehog.Team^.Clan^.TurnNumber > Ammoz[AmmoType].SkipTurns)); |
295 | 178 |
inc(ami) |
351 | 179 |
end; |
180 |
HHHasAmmo:= false |
|
295 | 181 |
end; |
182 |
||
1964 | 183 |
procedure ApplyAngleBounds(var Hedgehog: THedgehog; AmmoType: TAmmoType); |
1922 | 184 |
begin |
185 |
with Hedgehog do |
|
1964 | 186 |
begin |
187 |
CurMinAngle:= Ammoz[AmmoType].minAngle; |
|
188 |
if Ammoz[AmmoType].maxAngle <> 0 then |
|
189 |
CurMaxAngle:= Ammoz[AmmoType].maxAngle |
|
190 |
else |
|
191 |
CurMaxAngle:= cMaxAngle; |
|
192 |
||
193 |
with Hedgehog.Gear^ do |
|
1922 | 194 |
begin |
1964 | 195 |
if Angle < CurMinAngle then Angle:= CurMinAngle; |
196 |
if Angle > CurMaxAngle then Angle:= CurMaxAngle; |
|
1922 | 197 |
end |
1964 | 198 |
end |
1922 | 199 |
end; |
200 |
||
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
201 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
202 |
var s: shortstring; |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
203 |
begin |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
204 |
TargetPoint.X:= NoPointX; |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
205 |
|
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
206 |
with Hedgehog do |
1922 | 207 |
begin |
208 |
if (Ammo^[CurSlot, CurAmmo].Count = 0) then |
|
209 |
begin |
|
210 |
CurAmmo:= 0; |
|
211 |
CurSlot:= 0; |
|
212 |
while (CurSlot <= cMaxSlotIndex) and (Ammo^[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
|
213 |
end; |
|
214 |
||
1964 | 215 |
ApplyAngleBounds(Hedgehog, Ammo^[CurSlot, CurAmmo].AmmoType); |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
216 |
|
1922 | 217 |
with Ammo^[CurSlot, CurAmmo] do |
218 |
begin |
|
219 |
s:= trammo[Ammoz[AmmoType].NameId]; |
|
220 |
if Count <> AMMO_INFINITE then |
|
221 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
222 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
223 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
|
224 |
AddCaption(s, Team^.Clan^.Color, capgrpAmmoinfo); |
|
225 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
226 |
then begin |
|
227 |
Gear^.State:= Gear^.State or gstHHChooseTarget; |
|
228 |
isCursorVisible:= true |
|
229 |
end else begin |
|
230 |
Gear^.State:= Gear^.State and not gstHHChooseTarget; |
|
231 |
isCursorVisible:= false |
|
232 |
end; |
|
233 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
234 |
end |
|
235 |
end |
|
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
236 |
end; |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
237 |
|
1922 | 238 |
procedure SwitchNotHeldAmmo(var Hedgehog: THedgehog); |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
239 |
begin |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
240 |
with Hedgehog do |
1909 | 241 |
if ((Ammo^[CurSlot, CurAmmo].Propz and ammoprop_DontHold) <> 0) or |
1915 | 242 |
(Ammoz[Ammo^[CurSlot, CurAmmo].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0) then |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
243 |
begin |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
244 |
CurAmmo:= 0; |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
245 |
CurSlot:= 0; |
1909 | 246 |
while (CurSlot <= cMaxSlotIndex) and |
247 |
(Ammo^[CurSlot, CurAmmo].Count = 0) and |
|
1915 | 248 |
(Ammoz[Ammo^[CurSlot, CurAmmo].AmmoType].SkipTurns - CurrentTeam^.Clan^.TurnNumber >= 0) |
1909 | 249 |
do inc(CurSlot) |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
250 |
end |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
251 |
end; |
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
547
diff
changeset
|
252 |
|
783 | 253 |
procedure SetWeapon(weap: TAmmoType); |
254 |
begin |
|
1850 | 255 |
ParseCommand('/setweap ' + char(weap), true) |
783 | 256 |
end; |
257 |
||
1784 | 258 |
procedure DisableSomeWeapons; |
259 |
var i, slot, a: Longword; |
|
260 |
t: TAmmoType; |
|
261 |
begin |
|
262 |
for i:= 0 to Pred(StoreCnt) do |
|
263 |
for slot:= 0 to cMaxSlotIndex do |
|
264 |
begin |
|
265 |
for a:= 0 to cMaxSlotAmmoIndex do |
|
266 |
with StoresList[i]^[slot, a] do |
|
267 |
if (Propz and ammoprop_NotBorder) <> 0 then Count:= 0; |
|
268 |
||
269 |
PackAmmo(StoresList[i], slot) |
|
270 |
end; |
|
271 |
||
272 |
for t:= Low(TAmmoType) to High(TAmmoType) do |
|
273 |
if (Ammoz[t].Ammo.Propz and ammoprop_NotBorder) <> 0 then Ammoz[t].Probability:= 0 |
|
274 |
end; |
|
275 |
||
288 | 276 |
end. |