author | displacer |
Thu, 08 Feb 2007 20:54:14 +0000 | |
changeset 419 | fdeed9718e6b |
parent 393 | db01cc79f278 |
child 498 | 9c8b385dc9a1 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2004-2007 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 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 |
|
4 | 8 |
* |
183 | 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. |
|
4 | 13 |
* |
183 | 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 |
|
4 | 17 |
*) |
18 |
||
19 |
unit uTeams; |
|
20 |
interface |
|
351 | 21 |
uses SDLh, uConsts, uKeys, uGears, uRandom, uAmmos, uFloat; |
4 | 22 |
{$INCLUDE options.inc} |
23 |
type PHedgehog = ^THedgehog; |
|
24 |
PTeam = ^TTeam; |
|
25 |
THedgehog = record |
|
74 | 26 |
Name: string[MAXNAMELEN]; |
4 | 27 |
Gear: PGear; |
95 | 28 |
NameTag, HealthTag: PSDL_Surface; |
4 | 29 |
Ammo: PHHAmmo; |
288 | 30 |
AmmoStore: Longword; |
4 | 31 |
CurSlot, CurAmmo: LongWord; |
32 |
AltSlot, AltAmmo: LongWord; |
|
33 |
Team: PTeam; |
|
34 |
AttacksNum: Longword; |
|
35 |
visStepPos: LongWord; |
|
5 | 36 |
BotLevel : LongWord; // 0 - Human player |
307 | 37 |
DamageGiven: Longword; |
38 |
MaxStepDamage: Longword; |
|
4 | 39 |
end; |
40 |
TTeam = record |
|
41 |
Next: PTeam; |
|
189 | 42 |
Color, AdjColor: Longword; |
74 | 43 |
TeamName: string[MAXNAMELEN]; |
4 | 44 |
ExtDriven: boolean; |
167 | 45 |
Binds: TBinds; |
4 | 46 |
Hedgehogs: array[0..cMaxHHIndex] of THedgehog; |
371 | 47 |
CurrHedgehog: LongInt; |
95 | 48 |
NameTag: PSDL_Surface; |
198 | 49 |
CrosshairSurf: PSDL_Surface; |
47 | 50 |
GraveRect, HealthRect: TSDL_Rect; |
4 | 51 |
GraveName: string; |
52 |
FortName: string; |
|
371 | 53 |
TeamHealth: LongInt; |
54 |
TeamHealthBarWidth: LongInt; |
|
55 |
DrawHealthY: LongInt; |
|
4 | 56 |
AttackBar: LongWord; |
312 | 57 |
HedgehogsNumber: byte; |
4 | 58 |
end; |
59 |
||
60 |
var CurrentTeam: PTeam = nil; |
|
61 |
TeamsList: PTeam = nil; |
|
304 | 62 |
CurMinAngle, CurMaxAngle: Longword; |
4 | 63 |
|
64 |
function AddTeam: PTeam; |
|
70 | 65 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 66 |
procedure SwitchHedgehog; |
67 |
procedure InitTeams; |
|
68 |
function TeamSize(p: PTeam): Longword; |
|
47 | 69 |
procedure RecountTeamHealth(team: PTeam); |
72 | 70 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
71 |
function CheckForWin: boolean; |
165 | 72 |
procedure SetWeapon(weap: TAmmoType); |
307 | 73 |
procedure SendStats; |
4 | 74 |
|
75 |
implementation |
|
196 | 76 |
uses uMisc, uWorld, uAI, uLocale, uConsole; |
371 | 77 |
const MaxTeamHealth: LongInt = 0; |
4 | 78 |
|
79 |
procedure FreeTeamsList; forward; |
|
80 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
81 |
function CheckForWin: boolean; |
83 | 82 |
var team, AliveTeam: PTeam; |
83 |
AliveCount: Longword; |
|
306 | 84 |
s: shortstring; |
83 | 85 |
begin |
86 |
AliveCount:= 0; |
|
87 |
AliveTeam:= nil; |
|
88 |
team:= TeamsList; |
|
89 |
while team <> nil do |
|
90 |
begin |
|
351 | 91 |
if team^.TeamHealth > 0 then |
83 | 92 |
begin |
93 |
inc(AliveCount); |
|
94 |
AliveTeam:= team |
|
95 |
end; |
|
351 | 96 |
team:= team^.Next |
83 | 97 |
end; |
98 |
||
351 | 99 |
if AliveCount >= 2 then exit(false); |
100 |
CheckForWin:= true; |
|
83 | 101 |
|
102 |
TurnTimeLeft:= 0; |
|
103 |
if AliveCount = 0 then |
|
104 |
begin // draw |
|
105 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
306 | 106 |
SendStat(siGameResult, trmsg[sidDraw]); |
83 | 107 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
108 |
end else // win |
|
109 |
begin |
|
351 | 110 |
s:= Format(trmsg[sidWinner], AliveTeam^.TeamName); |
306 | 111 |
AddCaption(s, $FFFFFF, capgrpGameState); |
112 |
SendStat(siGameResult, s); |
|
83 | 113 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
114 |
end; |
|
307 | 115 |
SendStats |
83 | 116 |
end; |
117 |
||
4 | 118 |
procedure SwitchHedgehog; |
119 |
var tteam: PTeam; |
|
371 | 120 |
th: LongInt; |
351 | 121 |
g: PGear; |
4 | 122 |
begin |
123 |
FreeActionsList; |
|
124 |
TargetPoint.X:= NoPointX; |
|
89 | 125 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
4 | 126 |
tteam:= CurrentTeam; |
351 | 127 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
294 | 128 |
if Gear <> nil then |
129 |
begin |
|
351 | 130 |
Gear^.Message:= 0; |
131 |
Gear^.Z:= cHHZ |
|
294 | 132 |
end; |
4 | 133 |
|
134 |
repeat |
|
351 | 135 |
CurrentTeam:= CurrentTeam^.Next; |
4 | 136 |
if CurrentTeam = nil then CurrentTeam:= TeamsList; |
351 | 137 |
th:= CurrentTeam^.CurrHedgehog; |
4 | 138 |
repeat |
351 | 139 |
CurrentTeam^.CurrHedgehog:= Succ(CurrentTeam^.CurrHedgehog) mod (cMaxHHIndex + 1); |
140 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (CurrentTeam^.CurrHedgehog = th) |
|
141 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (CurrentTeam = tteam); |
|
4 | 142 |
|
83 | 143 |
TryDo(CurrentTeam <> tteam, 'Switch hedgehog: only one team?!', true); |
144 |
||
351 | 145 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
4 | 146 |
begin |
147 |
AttacksNum:= 0; |
|
148 |
with Gear^ do |
|
149 |
begin |
|
294 | 150 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
151 |
State:= gstHHDriven; |
4 | 152 |
Active:= true |
153 |
end; |
|
294 | 154 |
RemoveGearFromList(Gear); |
155 |
InsertGearToList(Gear); |
|
4 | 156 |
FollowGear:= Gear |
157 |
end; |
|
158 |
ResetKbd; |
|
351 | 159 |
|
160 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
161 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1); |
|
162 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
|
4 | 163 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
351 | 164 |
ApplyAmmoChanges(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
165 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
|
166 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 167 |
bShowFinger:= true; |
4 | 168 |
TurnTimeLeft:= cHedgehogTurnTime |
169 |
end; |
|
170 |
||
171 |
function AddTeam: PTeam; |
|
351 | 172 |
var Result: PTeam; |
4 | 173 |
begin |
17 | 174 |
New(Result); |
80 | 175 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 176 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 177 |
Result^.AttackBar:= 2; |
178 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
4 | 179 |
if TeamsList = nil then TeamsList:= Result |
180 |
else begin |
|
351 | 181 |
Result^.Next:= TeamsList; |
4 | 182 |
TeamsList:= Result |
183 |
end; |
|
351 | 184 |
CurrentTeam:= Result; |
185 |
AddTeam:= Result |
|
4 | 186 |
end; |
187 |
||
188 |
procedure FreeTeamsList; |
|
189 |
var t, tt: PTeam; |
|
190 |
begin |
|
191 |
tt:= TeamsList; |
|
192 |
TeamsList:= nil; |
|
351 | 193 |
while tt <> nil do |
4 | 194 |
begin |
195 |
t:= tt; |
|
351 | 196 |
tt:= tt^.Next; |
4 | 197 |
Dispose(t) |
198 |
end; |
|
199 |
end; |
|
200 |
||
47 | 201 |
procedure RecountAllTeamsHealth; |
4 | 202 |
var p: PTeam; |
203 |
begin |
|
204 |
p:= TeamsList; |
|
205 |
while p <> nil do |
|
206 |
begin |
|
47 | 207 |
RecountTeamHealth(p); |
351 | 208 |
p:= p^.Next |
47 | 209 |
end |
210 |
end; |
|
211 |
||
212 |
procedure InitTeams; |
|
213 |
var p: PTeam; |
|
371 | 214 |
i: LongInt; |
215 |
th: LongInt; |
|
47 | 216 |
begin |
217 |
p:= TeamsList; |
|
218 |
while p <> nil do |
|
219 |
begin |
|
220 |
th:= 0; |
|
4 | 221 |
for i:= 0 to cMaxHHIndex do |
351 | 222 |
if p^.Hedgehogs[i].Gear <> nil then |
223 |
inc(th, p^.Hedgehogs[i].Gear^.Health); |
|
47 | 224 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
351 | 225 |
p:= p^.Next |
4 | 226 |
end; |
47 | 227 |
RecountAllTeamsHealth |
4 | 228 |
end; |
229 |
||
70 | 230 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 231 |
var s: shortstring; |
232 |
begin |
|
162 | 233 |
TargetPoint.X:= NoPointX; |
234 |
||
70 | 235 |
with Hedgehog do |
47 | 236 |
begin |
351 | 237 |
if Ammo^[CurSlot, CurAmmo].Count = 0 then |
4 | 238 |
begin |
239 |
CurAmmo:= 0; |
|
288 | 240 |
CurSlot:= 0; |
351 | 241 |
while (CurSlot <= cMaxSlotIndex) and (Ammo^[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
4 | 242 |
end; |
243 |
||
351 | 244 |
with Ammo^[CurSlot, CurAmmo] do |
4 | 245 |
begin |
304 | 246 |
CurMinAngle:= Ammoz[AmmoType].minAngle; |
247 |
if Ammoz[AmmoType].maxAngle <> 0 then CurMaxAngle:= Ammoz[AmmoType].maxAngle |
|
248 |
else CurMaxAngle:= cMaxAngle; |
|
249 |
with Hedgehog.Gear^ do |
|
250 |
begin |
|
251 |
if Angle < CurMinAngle then Angle:= CurMinAngle; |
|
252 |
if Angle > CurMaxAngle then Angle:= CurMaxAngle; |
|
253 |
end; |
|
351 | 254 |
|
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]; |
351 | 260 |
AddCaption(s, Team^.Color, capgrpAmmoinfo); |
4 | 261 |
if (Propz and ammoprop_NeedTarget) <> 0 |
262 |
then begin |
|
351 | 263 |
Gear^.State:= Gear^.State or gstHHChooseTarget; |
4 | 264 |
isCursorVisible:= true |
265 |
end else begin |
|
351 | 266 |
Gear^.State:= Gear^.State and not gstHHChooseTarget; |
4 | 267 |
isCursorVisible:= false |
13 | 268 |
end; |
269 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 270 |
end |
271 |
end |
|
272 |
end; |
|
273 |
||
274 |
function TeamSize(p: PTeam): Longword; |
|
351 | 275 |
var i, Result: Longword; |
4 | 276 |
begin |
277 |
Result:= 0; |
|
278 |
for i:= 0 to cMaxHHIndex do |
|
351 | 279 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
280 |
TeamSize:= Result |
|
4 | 281 |
end; |
282 |
||
47 | 283 |
procedure RecountTeamHealth(team: PTeam); |
371 | 284 |
var i: LongInt; |
47 | 285 |
begin |
286 |
with team^ do |
|
287 |
begin |
|
146 | 288 |
TeamHealthBarWidth:= 0; |
47 | 289 |
for i:= 0 to cMaxHHIndex do |
290 |
if Hedgehogs[i].Gear <> nil then |
|
351 | 291 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
146 | 292 |
TeamHealth:= TeamHealthBarWidth; |
293 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 294 |
begin |
146 | 295 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 296 |
RecountAllTeamsHealth; |
146 | 297 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 298 |
end; |
105 | 299 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
351 | 300 |
AddGear(0, 0, gtTeamHealthSorter, 0, 0, 0, 0) |
47 | 301 |
end; |
302 |
||
72 | 303 |
procedure RestoreTeamsFromSave; |
304 |
var p: PTeam; |
|
305 |
begin |
|
306 |
p:= TeamsList; |
|
307 |
while p <> nil do |
|
308 |
begin |
|
351 | 309 |
p^.ExtDriven:= false; |
310 |
p:= p^.Next |
|
72 | 311 |
end; |
312 |
end; |
|
313 |
||
165 | 314 |
procedure SetWeapon(weap: TAmmoType); |
371 | 315 |
var t: LongInt; |
165 | 316 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
317 |
t:= cMaxSlotAmmoIndex; |
165 | 318 |
with CurrentTeam^ do |
319 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 320 |
while (Ammo^[CurSlot, CurAmmo].AmmoType <> weap) and (t >= 0) do |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
321 |
begin |
351 | 322 |
ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot), true); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
323 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
324 |
end |
165 | 325 |
end; |
326 |
||
307 | 327 |
procedure SendStats; |
328 |
var p: PTeam; |
|
371 | 329 |
i: LongInt; |
307 | 330 |
msd: Longword; msdhh: PHedgehog; |
331 |
begin |
|
332 |
msd:= 0; msdhh:= nil; |
|
333 |
p:= TeamsList; |
|
334 |
while p <> nil do |
|
335 |
begin |
|
336 |
for i:= 0 to cMaxHHIndex do |
|
351 | 337 |
if p^.Hedgehogs[i].MaxStepDamage > msd then |
307 | 338 |
begin |
351 | 339 |
msdhh:= @(p^.Hedgehogs[i]); |
340 |
msd:= p^.Hedgehogs[i].MaxStepDamage |
|
307 | 341 |
end; |
351 | 342 |
p:= p^.Next |
307 | 343 |
end; |
351 | 344 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
345 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 346 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
347 |
end; |
|
348 |
||
4 | 349 |
initialization |
350 |
||
351 |
finalization |
|
352 |
||
353 |
FreeTeamsList |
|
354 |
||
355 |
end. |