author | unc0rr |
Thu, 10 Aug 2006 21:52:14 +0000 | |
changeset 105 | e7cb9bb4a9de |
parent 95 | 1ef5e2c41115 |
child 146 | 458f4f58c1b6 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
47 | 3 |
* Copyright (c) 2004, 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*) |
|
33 |
||
34 |
unit uTeams; |
|
35 |
interface |
|
36 |
uses SDLh, uConsts, uKeys, uGears, uRandom; |
|
37 |
{$INCLUDE options.inc} |
|
38 |
type PHedgehog = ^THedgehog; |
|
39 |
PTeam = ^TTeam; |
|
40 |
PHHAmmo = ^THHAmmo; |
|
41 |
THedgehog = record |
|
74 | 42 |
Name: string[MAXNAMELEN]; |
4 | 43 |
Gear: PGear; |
95 | 44 |
NameTag, HealthTag: PSDL_Surface; |
4 | 45 |
Ammo: PHHAmmo; |
46 |
CurSlot, CurAmmo: LongWord; |
|
47 |
AltSlot, AltAmmo: LongWord; |
|
48 |
Team: PTeam; |
|
49 |
AttacksNum: Longword; |
|
50 |
visStepPos: LongWord; |
|
5 | 51 |
BotLevel : LongWord; // 0 - Human player |
4 | 52 |
end; |
10 | 53 |
THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo; |
4 | 54 |
TTeam = record |
55 |
Next: PTeam; |
|
56 |
Color: Cardinal; |
|
74 | 57 |
TeamName: string[MAXNAMELEN]; |
4 | 58 |
ExtDriven: boolean; |
59 |
Aliases: array[0..cKeyMaxIndex] of shortstring; |
|
60 |
Hedgehogs: array[0..cMaxHHIndex] of THedgehog; |
|
61 |
Ammos: array[0..cMaxHHIndex] of THHAmmo; |
|
62 |
CurrHedgehog: integer; |
|
95 | 63 |
NameTag: PSDL_Surface; |
64 |
CrossHairRect, |
|
47 | 65 |
GraveRect, HealthRect: TSDL_Rect; |
4 | 66 |
GraveName: string; |
67 |
FortName: string; |
|
47 | 68 |
TeamHealth: integer; |
49 | 69 |
DrawHealthY: integer; |
4 | 70 |
AttackBar: LongWord; |
71 |
end; |
|
72 |
||
73 |
var CurrentTeam: PTeam = nil; |
|
74 |
TeamsList: PTeam = nil; |
|
75 |
||
76 |
function AddTeam: PTeam; |
|
70 | 77 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 78 |
procedure SwitchHedgehog; |
79 |
procedure InitTeams; |
|
80 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
75 | 81 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
4 | 82 |
function TeamSize(p: PTeam): Longword; |
47 | 83 |
procedure RecountTeamHealth(team: PTeam); |
72 | 84 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
85 |
function CheckForWin: boolean; |
4 | 86 |
|
87 |
implementation |
|
80 | 88 |
uses uMisc, uStore, uWorld, uIO, uAI, uLocale; |
47 | 89 |
const MaxTeamHealth: integer = 0; |
4 | 90 |
|
91 |
procedure FreeTeamsList; forward; |
|
92 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
93 |
function CheckForWin: boolean; |
83 | 94 |
var team, AliveTeam: PTeam; |
95 |
AliveCount: Longword; |
|
96 |
begin |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
97 |
Result:= false; |
83 | 98 |
AliveCount:= 0; |
99 |
AliveTeam:= nil; |
|
100 |
team:= TeamsList; |
|
101 |
while team <> nil do |
|
102 |
begin |
|
103 |
if team.TeamHealth > 0 then |
|
104 |
begin |
|
105 |
inc(AliveCount); |
|
106 |
AliveTeam:= team |
|
107 |
end; |
|
108 |
team:= team.Next |
|
109 |
end; |
|
110 |
||
111 |
if AliveCount >= 2 then exit; |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
112 |
Result:= true; |
83 | 113 |
|
114 |
TurnTimeLeft:= 0; |
|
115 |
if AliveCount = 0 then |
|
116 |
begin // draw |
|
117 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
118 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
119 |
end else // win |
|
120 |
begin |
|
121 |
AddCaption(Format(trmsg[sidWinner], AliveTeam.TeamName), $FFFFFF, capgrpGameState); |
|
122 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
123 |
end; |
|
124 |
end; |
|
125 |
||
4 | 126 |
procedure SwitchHedgehog; |
127 |
var tteam: PTeam; |
|
128 |
th: integer; |
|
129 |
begin |
|
130 |
FreeActionsList; |
|
131 |
TargetPoint.X:= NoPointX; |
|
89 | 132 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
4 | 133 |
tteam:= CurrentTeam; |
134 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
135 |
if Gear <> nil then Gear.Message:= 0; |
|
136 |
||
137 |
repeat |
|
138 |
CurrentTeam:= CurrentTeam.Next; |
|
139 |
if CurrentTeam = nil then CurrentTeam:= TeamsList; |
|
140 |
th:= CurrentTeam.CurrHedgehog; |
|
141 |
repeat |
|
83 | 142 |
CurrentTeam.CurrHedgehog:= Succ(CurrentTeam.CurrHedgehog) mod (cMaxHHIndex + 1); |
4 | 143 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam.CurrHedgehog = th) |
144 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam = tteam); |
|
145 |
||
83 | 146 |
TryDo(CurrentTeam <> tteam, 'Switch hedgehog: only one team?!', true); |
147 |
||
4 | 148 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
149 |
begin |
|
150 |
AttacksNum:= 0; |
|
151 |
with Gear^ do |
|
152 |
begin |
|
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
153 |
State:= gstHHDriven; |
4 | 154 |
Active:= true |
155 |
end; |
|
156 |
FollowGear:= Gear |
|
157 |
end; |
|
158 |
ResetKbd; |
|
159 |
cWindSpeed:= (GetRandom * 2 - 1) * cMaxWindSpeed; |
|
83 | 160 |
AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1).Tag:= round(72 * cWindSpeed / cMaxWindSpeed); |
4 | 161 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
70 | 162 |
ApplyAmmoChanges(CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]); |
4 | 163 |
TurnTimeLeft:= cHedgehogTurnTime |
164 |
end; |
|
165 |
||
166 |
function AddTeam: PTeam; |
|
167 |
begin |
|
17 | 168 |
New(Result); |
80 | 169 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 170 |
FillChar(Result^, sizeof(TTeam), 0); |
74 | 171 |
Result.AttackBar:= 2; |
83 | 172 |
Result.CurrHedgehog:= cMaxHHIndex; |
4 | 173 |
if TeamsList = nil then TeamsList:= Result |
174 |
else begin |
|
175 |
Result.Next:= TeamsList; |
|
176 |
TeamsList:= Result |
|
177 |
end; |
|
178 |
CurrentTeam:= Result |
|
179 |
end; |
|
180 |
||
181 |
procedure FreeTeamsList; |
|
182 |
var t, tt: PTeam; |
|
183 |
begin |
|
184 |
tt:= TeamsList; |
|
185 |
TeamsList:= nil; |
|
186 |
while tt<>nil do |
|
187 |
begin |
|
188 |
t:= tt; |
|
189 |
tt:= tt.Next; |
|
190 |
Dispose(t) |
|
191 |
end; |
|
192 |
end; |
|
193 |
||
10 | 194 |
procedure FillAmmoGroup(Ammo: PHHAmmo); |
195 |
var mi: array[0..cMaxSlotIndex] of byte; |
|
196 |
a: TAmmoType; |
|
197 |
begin |
|
198 |
FillChar(mi, sizeof(mi), 0); |
|
199 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
|
200 |
begin |
|
201 |
TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true); |
|
202 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo; |
|
203 |
inc(mi[Ammoz[a].Slot]) |
|
204 |
end; |
|
205 |
end; |
|
206 |
||
47 | 207 |
procedure RecountAllTeamsHealth; |
4 | 208 |
var p: PTeam; |
209 |
begin |
|
210 |
p:= TeamsList; |
|
211 |
while p <> nil do |
|
212 |
begin |
|
47 | 213 |
RecountTeamHealth(p); |
214 |
p:= p.Next |
|
215 |
end |
|
216 |
end; |
|
217 |
||
218 |
procedure InitTeams; |
|
219 |
var p: PTeam; |
|
220 |
i: integer; |
|
221 |
th: integer; |
|
222 |
begin |
|
223 |
p:= TeamsList; |
|
224 |
while p <> nil do |
|
225 |
begin |
|
226 |
th:= 0; |
|
10 | 227 |
FillAmmoGroup(@p.Ammos[0]); |
4 | 228 |
for i:= 0 to cMaxHHIndex do |
229 |
if p.Hedgehogs[i].Gear <> nil then |
|
230 |
begin |
|
231 |
p.Hedgehogs[i].Gear.Health:= 100; |
|
47 | 232 |
inc(th, 100); |
10 | 233 |
p.Hedgehogs[i].Ammo:= @p.Ammos[0] // 0 means all hedgehogs |
234 |
// will have common set of ammo |
|
4 | 235 |
end; |
47 | 236 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 237 |
p:= p.Next |
238 |
end; |
|
47 | 239 |
RecountAllTeamsHealth |
4 | 240 |
end; |
241 |
||
70 | 242 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 243 |
var s: shortstring; |
244 |
begin |
|
70 | 245 |
with Hedgehog do |
47 | 246 |
begin |
4 | 247 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
248 |
begin |
|
249 |
CurAmmo:= 0; |
|
10 | 250 |
while (CurAmmo <= cMaxSlotAmmoIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo) |
4 | 251 |
end; |
252 |
||
253 |
with Ammo[CurSlot, CurAmmo] do |
|
254 |
begin |
|
80 | 255 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 256 |
if Count <> AMMO_INFINITE then |
257 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
258 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 259 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
4 | 260 |
AddCaption(s, Team.Color, capgrpAmmoinfo); |
261 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
262 |
then begin |
|
263 |
Gear.State:= Gear.State or gstHHChooseTarget; |
|
264 |
isCursorVisible:= true |
|
265 |
end else begin |
|
266 |
Gear.State:= Gear.State and not gstHHChooseTarget; |
|
267 |
isCursorVisible:= false |
|
13 | 268 |
end; |
269 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 270 |
end |
271 |
end |
|
272 |
end; |
|
273 |
||
274 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer); |
|
275 |
var ami: integer; |
|
276 |
b: boolean; |
|
277 |
begin |
|
278 |
repeat |
|
279 |
b:= false; |
|
280 |
ami:= 0; |
|
10 | 281 |
while (not b) and (ami < cMaxSlotAmmoIndex) do |
70 | 282 |
if (Ammo[Slot, ami].Count = 0) |
283 |
and (Ammo[Slot, ami + 1].Count > 0) then b:= true |
|
4 | 284 |
else inc(ami); |
79 | 285 |
if b then // there's a free item in ammo stack |
70 | 286 |
Ammo[Slot, ami]:= Ammo[Slot, ami + 1] |
4 | 287 |
until not b; |
288 |
end; |
|
289 |
||
290 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
291 |
var s, a: Longword; |
|
292 |
begin |
|
293 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
294 |
begin |
|
295 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end |
|
296 |
else begin s:= AltSlot; a:= AltAmmo end; |
|
297 |
with Ammo[s, a] do |
|
298 |
if Count <> AMMO_INFINITE then |
|
299 |
begin |
|
300 |
dec(Count); |
|
301 |
if Count = 0 then PackAmmo(Ammo, CurSlot) |
|
302 |
end |
|
303 |
end |
|
304 |
end; |
|
305 |
||
75 | 306 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
307 |
var slot, ami: integer; |
|
308 |
begin |
|
309 |
Slot:= Ammoz[Ammo].Slot; |
|
310 |
ami:= 0; |
|
311 |
Result:= false; |
|
312 |
while (not Result) and (ami <= cMaxSlotAmmoIndex) do |
|
313 |
begin |
|
314 |
with Hedgehog.Ammo[Slot, ami] do |
|
315 |
if (AmmoType = Ammo) and (Count > 0) then Result:= true; |
|
316 |
inc(ami) |
|
317 |
end |
|
318 |
end; |
|
319 |
||
4 | 320 |
function TeamSize(p: PTeam): Longword; |
321 |
var i: Longword; |
|
322 |
begin |
|
323 |
Result:= 0; |
|
324 |
for i:= 0 to cMaxHHIndex do |
|
325 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
326 |
end; |
|
327 |
||
47 | 328 |
procedure RecountTeamHealth(team: PTeam); |
329 |
var i: integer; |
|
330 |
begin |
|
331 |
with team^ do |
|
332 |
begin |
|
333 |
TeamHealth:= 0; |
|
334 |
for i:= 0 to cMaxHHIndex do |
|
335 |
if Hedgehogs[i].Gear <> nil then |
|
336 |
inc(TeamHealth, Hedgehogs[i].Gear.Health); |
|
337 |
if TeamHealth > MaxTeamHealth then |
|
338 |
begin |
|
339 |
MaxTeamHealth:= TeamHealth; |
|
340 |
RecountAllTeamsHealth; |
|
341 |
end else TeamHealth:= (TeamHealth * cTeamHealthWidth) div MaxTeamHealth |
|
49 | 342 |
end; |
105 | 343 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
49 | 344 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
47 | 345 |
end; |
346 |
||
72 | 347 |
procedure RestoreTeamsFromSave; |
348 |
var p: PTeam; |
|
349 |
begin |
|
350 |
p:= TeamsList; |
|
351 |
while p <> nil do |
|
352 |
begin |
|
353 |
p.ExtDriven:= false; |
|
354 |
p:= p.Next |
|
355 |
end; |
|
356 |
end; |
|
357 |
||
4 | 358 |
initialization |
359 |
||
360 |
finalization |
|
361 |
||
362 |
FreeTeamsList |
|
363 |
||
364 |
end. |