author | unc0rr |
Sun, 21 Jan 2007 19:51:02 +0000 | |
changeset 351 | 29bc9c36ad5f |
parent 312 | c36d0b34ac3d |
child 364 | 52cb4d6f84b7 |
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 |
* |
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; |
47 |
CurrHedgehog: integer; |
|
95 | 48 |
NameTag: PSDL_Surface; |
198 | 49 |
CrosshairSurf: PSDL_Surface; |
47 | 50 |
GraveRect, HealthRect: TSDL_Rect; |
4 | 51 |
GraveName: string; |
52 |
FortName: string; |
|
47 | 53 |
TeamHealth: integer; |
146 | 54 |
TeamHealthBarWidth: integer; |
49 | 55 |
DrawHealthY: integer; |
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; |
47 | 77 |
const MaxTeamHealth: integer = 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; |
|
120 |
th: integer; |
|
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 |
addfilelog('SwitchHedgehog 06'); |
|
162 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1); |
|
163 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
|
164 |
addfilelog('SwitchHedgehog 07'); |
|
4 | 165 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
351 | 166 |
ApplyAmmoChanges(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
167 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
|
168 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 169 |
bShowFinger:= true; |
4 | 170 |
TurnTimeLeft:= cHedgehogTurnTime |
171 |
end; |
|
172 |
||
173 |
function AddTeam: PTeam; |
|
351 | 174 |
var Result: PTeam; |
4 | 175 |
begin |
17 | 176 |
New(Result); |
80 | 177 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 178 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 179 |
Result^.AttackBar:= 2; |
180 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
4 | 181 |
if TeamsList = nil then TeamsList:= Result |
182 |
else begin |
|
351 | 183 |
Result^.Next:= TeamsList; |
4 | 184 |
TeamsList:= Result |
185 |
end; |
|
351 | 186 |
CurrentTeam:= Result; |
187 |
AddTeam:= Result |
|
4 | 188 |
end; |
189 |
||
190 |
procedure FreeTeamsList; |
|
191 |
var t, tt: PTeam; |
|
192 |
begin |
|
193 |
tt:= TeamsList; |
|
194 |
TeamsList:= nil; |
|
351 | 195 |
while tt <> nil do |
4 | 196 |
begin |
197 |
t:= tt; |
|
351 | 198 |
tt:= tt^.Next; |
4 | 199 |
Dispose(t) |
200 |
end; |
|
201 |
end; |
|
202 |
||
47 | 203 |
procedure RecountAllTeamsHealth; |
4 | 204 |
var p: PTeam; |
205 |
begin |
|
206 |
p:= TeamsList; |
|
207 |
while p <> nil do |
|
208 |
begin |
|
47 | 209 |
RecountTeamHealth(p); |
351 | 210 |
p:= p^.Next |
47 | 211 |
end |
212 |
end; |
|
213 |
||
214 |
procedure InitTeams; |
|
215 |
var p: PTeam; |
|
216 |
i: integer; |
|
217 |
th: integer; |
|
218 |
begin |
|
219 |
p:= TeamsList; |
|
220 |
while p <> nil do |
|
221 |
begin |
|
222 |
th:= 0; |
|
4 | 223 |
for i:= 0 to cMaxHHIndex do |
351 | 224 |
if p^.Hedgehogs[i].Gear <> nil then |
225 |
inc(th, p^.Hedgehogs[i].Gear^.Health); |
|
47 | 226 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
351 | 227 |
p:= p^.Next |
4 | 228 |
end; |
47 | 229 |
RecountAllTeamsHealth |
4 | 230 |
end; |
231 |
||
70 | 232 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 233 |
var s: shortstring; |
234 |
begin |
|
162 | 235 |
TargetPoint.X:= NoPointX; |
236 |
||
70 | 237 |
with Hedgehog do |
47 | 238 |
begin |
351 | 239 |
if Ammo^[CurSlot, CurAmmo].Count = 0 then |
4 | 240 |
begin |
241 |
CurAmmo:= 0; |
|
288 | 242 |
CurSlot:= 0; |
351 | 243 |
while (CurSlot <= cMaxSlotIndex) and (Ammo^[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
4 | 244 |
end; |
245 |
||
351 | 246 |
with Ammo^[CurSlot, CurAmmo] do |
4 | 247 |
begin |
304 | 248 |
CurMinAngle:= Ammoz[AmmoType].minAngle; |
249 |
if Ammoz[AmmoType].maxAngle <> 0 then CurMaxAngle:= Ammoz[AmmoType].maxAngle |
|
250 |
else CurMaxAngle:= cMaxAngle; |
|
251 |
with Hedgehog.Gear^ do |
|
252 |
begin |
|
253 |
if Angle < CurMinAngle then Angle:= CurMinAngle; |
|
254 |
if Angle > CurMaxAngle then Angle:= CurMaxAngle; |
|
255 |
end; |
|
351 | 256 |
|
80 | 257 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 258 |
if Count <> AMMO_INFINITE then |
259 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
260 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 261 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
351 | 262 |
AddCaption(s, Team^.Color, capgrpAmmoinfo); |
4 | 263 |
if (Propz and ammoprop_NeedTarget) <> 0 |
264 |
then begin |
|
351 | 265 |
Gear^.State:= Gear^.State or gstHHChooseTarget; |
4 | 266 |
isCursorVisible:= true |
267 |
end else begin |
|
351 | 268 |
Gear^.State:= Gear^.State and not gstHHChooseTarget; |
4 | 269 |
isCursorVisible:= false |
13 | 270 |
end; |
271 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 272 |
end |
273 |
end |
|
274 |
end; |
|
275 |
||
276 |
function TeamSize(p: PTeam): Longword; |
|
351 | 277 |
var i, Result: Longword; |
4 | 278 |
begin |
279 |
Result:= 0; |
|
280 |
for i:= 0 to cMaxHHIndex do |
|
351 | 281 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
282 |
TeamSize:= Result |
|
4 | 283 |
end; |
284 |
||
47 | 285 |
procedure RecountTeamHealth(team: PTeam); |
286 |
var i: integer; |
|
287 |
begin |
|
288 |
with team^ do |
|
289 |
begin |
|
146 | 290 |
TeamHealthBarWidth:= 0; |
47 | 291 |
for i:= 0 to cMaxHHIndex do |
292 |
if Hedgehogs[i].Gear <> nil then |
|
351 | 293 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
146 | 294 |
TeamHealth:= TeamHealthBarWidth; |
295 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 296 |
begin |
146 | 297 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 298 |
RecountAllTeamsHealth; |
146 | 299 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 300 |
end; |
105 | 301 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
351 | 302 |
AddGear(0, 0, gtTeamHealthSorter, 0, 0, 0, 0) |
47 | 303 |
end; |
304 |
||
72 | 305 |
procedure RestoreTeamsFromSave; |
306 |
var p: PTeam; |
|
307 |
begin |
|
308 |
p:= TeamsList; |
|
309 |
while p <> nil do |
|
310 |
begin |
|
351 | 311 |
p^.ExtDriven:= false; |
312 |
p:= p^.Next |
|
72 | 313 |
end; |
314 |
end; |
|
315 |
||
165 | 316 |
procedure SetWeapon(weap: TAmmoType); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
317 |
var t: integer; |
165 | 318 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
319 |
t:= cMaxSlotAmmoIndex; |
165 | 320 |
with CurrentTeam^ do |
321 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 322 |
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
|
323 |
begin |
351 | 324 |
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
|
325 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
326 |
end |
165 | 327 |
end; |
328 |
||
307 | 329 |
procedure SendStats; |
330 |
var p: PTeam; |
|
331 |
i: integer; |
|
332 |
msd: Longword; msdhh: PHedgehog; |
|
333 |
begin |
|
334 |
msd:= 0; msdhh:= nil; |
|
335 |
p:= TeamsList; |
|
336 |
while p <> nil do |
|
337 |
begin |
|
338 |
for i:= 0 to cMaxHHIndex do |
|
351 | 339 |
if p^.Hedgehogs[i].MaxStepDamage > msd then |
307 | 340 |
begin |
351 | 341 |
msdhh:= @(p^.Hedgehogs[i]); |
342 |
msd:= p^.Hedgehogs[i].MaxStepDamage |
|
307 | 343 |
end; |
351 | 344 |
p:= p^.Next |
307 | 345 |
end; |
351 | 346 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
347 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 348 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
349 |
end; |
|
350 |
||
4 | 351 |
initialization |
352 |
||
353 |
finalization |
|
354 |
||
355 |
FreeTeamsList |
|
356 |
||
357 |
end. |