author | unc0rr |
Mon, 20 Aug 2007 18:52:12 +0000 | |
changeset 582 | cbbf7646461e |
parent 576 | 35a1af355ada |
child 601 | 78a68cc4d846 |
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; |
557 | 58 |
TeamHealthBarWidth, |
59 |
NewTeamHealthBarWidth: LongInt; |
|
371 | 60 |
DrawHealthY: LongInt; |
4 | 61 |
AttackBar: LongWord; |
550 | 62 |
HedgehogsNumber: Longword; |
4 | 63 |
end; |
549 | 64 |
TClan = record |
65 |
Color, AdjColor: Longword; |
|
66 |
Teams: array[0..Pred(cMaxTeams)] of PTeam; |
|
67 |
TeamsNumber: Longword; |
|
551 | 68 |
CurrTeam: LongWord; |
549 | 69 |
ClanHealth: LongInt; |
550 | 70 |
ClanIndex: LongInt; |
549 | 71 |
end; |
4 | 72 |
|
73 |
var CurrentTeam: PTeam = nil; |
|
547 | 74 |
TeamsArray: array[0..Pred(cMaxTeams)] of PTeam; |
75 |
TeamsCount: Longword = 0; |
|
549 | 76 |
ClansArray: array[0..Pred(cMaxTeams)] of PClan; |
77 |
ClansCount: Longword = 0; |
|
304 | 78 |
CurMinAngle, CurMaxAngle: Longword; |
4 | 79 |
|
549 | 80 |
function AddTeam(TeamColor: Longword): PTeam; |
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; |
576 | 97 |
var AliveClan: PClan; |
306 | 98 |
s: shortstring; |
548 | 99 |
t, AliveCount: LongInt; |
83 | 100 |
begin |
548 | 101 |
AliveCount:= 0; |
549 | 102 |
for t:= 0 to Pred(ClansCount) do |
103 |
if ClansArray[t]^.ClanHealth > 0 then |
|
548 | 104 |
begin |
105 |
inc(AliveCount); |
|
549 | 106 |
AliveClan:= ClansArray[t] |
548 | 107 |
end; |
108 |
||
109 |
if AliveCount >= 2 then exit(false); |
|
351 | 110 |
CheckForWin:= true; |
83 | 111 |
|
112 |
TurnTimeLeft:= 0; |
|
548 | 113 |
if AliveCount = 0 then |
83 | 114 |
begin // draw |
115 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
306 | 116 |
SendStat(siGameResult, trmsg[sidDraw]); |
498 | 117 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 118 |
end else // win |
549 | 119 |
with AliveClan^ do |
120 |
begin |
|
121 |
if TeamsNumber = 1 then |
|
122 |
s:= Format(trmsg[sidWinner], Teams[0]^.TeamName) // team wins |
|
123 |
else |
|
124 |
s:= Format(trmsg[sidWinner], Teams[0]^.TeamName); // clan wins |
|
125 |
||
126 |
AddCaption(s, $FFFFFF, capgrpGameState); |
|
127 |
SendStat(siGameResult, s); |
|
128 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
|
129 |
end; |
|
307 | 130 |
SendStats |
83 | 131 |
end; |
132 |
||
4 | 133 |
procedure SwitchHedgehog; |
551 | 134 |
var c: LongWord; |
351 | 135 |
g: PGear; |
552 | 136 |
PrevHH, PrevTeam: LongWord; |
4 | 137 |
begin |
138 |
FreeActionsList; |
|
139 |
TargetPoint.X:= NoPointX; |
|
89 | 140 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
547 | 141 |
|
351 | 142 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
294 | 143 |
if Gear <> nil then |
144 |
begin |
|
540 | 145 |
AttacksNum:= 0; |
351 | 146 |
Gear^.Message:= 0; |
534 | 147 |
Gear^.Z:= cHHZ; |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
552
diff
changeset
|
148 |
SwitchNotHoldedAmmo(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
534 | 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 |
|
552 | 158 |
PrevTeam:= CurrTeam; |
550 | 159 |
CurrTeam:= Succ(CurrTeam) mod TeamsNumber; |
160 |
CurrentTeam:= Teams[CurrTeam]; |
|
161 |
with CurrentTeam^ do |
|
552 | 162 |
begin |
163 |
PrevHH:= CurrHedgehog; |
|
550 | 164 |
repeat |
552 | 165 |
CurrHedgehog:= Succ(CurrHedgehog) mod HedgehogsNumber; |
166 |
until (Hedgehogs[CurrHedgehog].Gear <> nil) or (CurrHedgehog = PrevHH) |
|
167 |
end |
|
168 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (PrevTeam = CurrTeam); |
|
550 | 169 |
until CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil; |
83 | 170 |
|
351 | 171 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
4 | 172 |
begin |
173 |
with Gear^ do |
|
174 |
begin |
|
294 | 175 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
176 |
State:= gstHHDriven; |
4 | 177 |
Active:= true |
178 |
end; |
|
294 | 179 |
RemoveGearFromList(Gear); |
180 |
InsertGearToList(Gear); |
|
4 | 181 |
FollowGear:= Gear |
182 |
end; |
|
183 |
ResetKbd; |
|
351 | 184 |
|
185 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
498 | 186 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, _0, _0, 1); |
351 | 187 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
4 | 188 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
351 | 189 |
ApplyAmmoChanges(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
190 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
|
191 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 192 |
bShowFinger:= true; |
4 | 193 |
TurnTimeLeft:= cHedgehogTurnTime |
194 |
end; |
|
195 |
||
549 | 196 |
function AddTeam(TeamColor: Longword): PTeam; |
351 | 197 |
var Result: PTeam; |
549 | 198 |
c: LongInt; |
4 | 199 |
begin |
547 | 200 |
TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true); |
17 | 201 |
New(Result); |
80 | 202 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 203 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 204 |
Result^.AttackBar:= 2; |
205 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
547 | 206 |
|
207 |
TeamsArray[TeamsCount]:= Result; |
|
208 |
inc(TeamsCount); |
|
209 |
||
549 | 210 |
c:= Pred(ClansCount); |
211 |
while (c >= 0) and (ClansArray[c]^.Color <> TeamColor) do dec(c); |
|
212 |
if c < 0 then |
|
213 |
begin |
|
214 |
new(Result^.Clan); |
|
215 |
FillChar(Result^.Clan^, sizeof(TClan), 0); |
|
216 |
ClansArray[ClansCount]:= Result^.Clan; |
|
217 |
inc(ClansCount); |
|
218 |
with Result^.Clan^ do |
|
219 |
begin |
|
550 | 220 |
ClanIndex:= Pred(ClansCount); |
549 | 221 |
Color:= TeamColor; |
222 |
AdjColor:= Color; |
|
223 |
AdjustColor(AdjColor); |
|
224 |
end |
|
225 |
end else |
|
226 |
begin |
|
227 |
Result^.Clan:= ClansArray[c]; |
|
228 |
end; |
|
229 |
||
230 |
with Result^.Clan^ do |
|
231 |
begin |
|
232 |
Teams[TeamsNumber]:= Result; |
|
233 |
inc(TeamsNumber) |
|
234 |
end; |
|
235 |
||
351 | 236 |
CurrentTeam:= Result; |
237 |
AddTeam:= Result |
|
4 | 238 |
end; |
239 |
||
240 |
procedure FreeTeamsList; |
|
547 | 241 |
var t: LongInt; |
4 | 242 |
begin |
547 | 243 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
244 |
TeamsCount:= 0 |
|
4 | 245 |
end; |
246 |
||
47 | 247 |
procedure RecountAllTeamsHealth; |
547 | 248 |
var t: LongInt; |
249 |
begin |
|
250 |
for t:= 0 to Pred(TeamsCount) do |
|
251 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 252 |
end; |
253 |
||
254 |
procedure InitTeams; |
|
547 | 255 |
var i, t: LongInt; |
371 | 256 |
th: LongInt; |
47 | 257 |
begin |
547 | 258 |
for t:= 0 to Pred(TeamsCount) do |
259 |
with TeamsArray[t]^ do |
|
47 | 260 |
begin |
261 |
th:= 0; |
|
4 | 262 |
for i:= 0 to cMaxHHIndex do |
547 | 263 |
if Hedgehogs[i].Gear <> nil then |
264 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 265 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 266 |
end; |
47 | 267 |
RecountAllTeamsHealth |
4 | 268 |
end; |
269 |
||
270 |
function TeamSize(p: PTeam): Longword; |
|
351 | 271 |
var i, Result: Longword; |
4 | 272 |
begin |
273 |
Result:= 0; |
|
274 |
for i:= 0 to cMaxHHIndex do |
|
351 | 275 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
276 |
TeamSize:= Result |
|
4 | 277 |
end; |
278 |
||
549 | 279 |
procedure RecountClanHealth(clan: PClan); |
280 |
var i: LongInt; |
|
281 |
begin |
|
282 |
with clan^ do |
|
283 |
begin |
|
284 |
ClanHealth:= 0; |
|
285 |
for i:= 0 to Pred(TeamsNumber) do |
|
286 |
inc(ClanHealth, Teams[i]^.TeamHealth) |
|
287 |
end |
|
288 |
end; |
|
289 |
||
47 | 290 |
procedure RecountTeamHealth(team: PTeam); |
371 | 291 |
var i: LongInt; |
47 | 292 |
begin |
293 |
with team^ do |
|
294 |
begin |
|
557 | 295 |
NewTeamHealthBarWidth:= 0; |
47 | 296 |
for i:= 0 to cMaxHHIndex do |
297 |
if Hedgehogs[i].Gear <> nil then |
|
557 | 298 |
inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
299 |
TeamHealth:= NewTeamHealthBarWidth; |
|
300 |
if NewTeamHealthBarWidth > MaxTeamHealth then |
|
47 | 301 |
begin |
557 | 302 |
MaxTeamHealth:= NewTeamHealthBarWidth; |
47 | 303 |
RecountAllTeamsHealth; |
557 | 304 |
end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 305 |
end; |
549 | 306 |
|
307 |
RecountClanHealth(team^.Clan); |
|
308 |
||
498 | 309 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 310 |
end; |
311 |
||
72 | 312 |
procedure RestoreTeamsFromSave; |
547 | 313 |
var t: LongInt; |
72 | 314 |
begin |
547 | 315 |
for t:= 0 to Pred(TeamsCount) do |
316 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 317 |
end; |
318 |
||
165 | 319 |
procedure SetWeapon(weap: TAmmoType); |
371 | 320 |
var t: LongInt; |
165 | 321 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
322 |
t:= cMaxSlotAmmoIndex; |
165 | 323 |
with CurrentTeam^ do |
324 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 325 |
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
|
326 |
begin |
351 | 327 |
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
|
328 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
329 |
end |
165 | 330 |
end; |
331 |
||
307 | 332 |
procedure SendStats; |
547 | 333 |
var i, t: LongInt; |
307 | 334 |
msd: Longword; msdhh: PHedgehog; |
335 |
begin |
|
336 |
msd:= 0; msdhh:= nil; |
|
547 | 337 |
for t:= 0 to Pred(TeamsCount) do |
338 |
with TeamsArray[t]^ do |
|
307 | 339 |
begin |
340 |
for i:= 0 to cMaxHHIndex do |
|
547 | 341 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 342 |
begin |
547 | 343 |
msdhh:= @Hedgehogs[i]; |
344 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 345 |
end; |
346 |
end; |
|
351 | 347 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
348 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 349 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
350 |
end; |
|
351 |
||
4 | 352 |
initialization |
353 |
||
354 |
finalization |
|
355 |
||
356 |
FreeTeamsList |
|
357 |
||
358 |
end. |