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