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