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