author | displacer |
Thu, 16 Aug 2007 14:10:06 +0000 | |
changeset 571 | a6cea9fbbc59 |
parent 557 | a7d49d5e8257 |
child 576 | 35a1af355ada |
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; |
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; |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
552
diff
changeset
|
150 |
SwitchNotHoldedAmmo(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
534 | 151 |
RemoveGearFromList(Gear); |
152 |
InsertGearToList(Gear) |
|
294 | 153 |
end; |
4 | 154 |
|
550 | 155 |
c:= CurrentTeam^.Clan^.ClanIndex; |
4 | 156 |
repeat |
550 | 157 |
c:= Succ(c) mod ClansCount; |
158 |
with ClansArray[c]^ do |
|
159 |
repeat |
|
552 | 160 |
PrevTeam:= CurrTeam; |
550 | 161 |
CurrTeam:= Succ(CurrTeam) mod TeamsNumber; |
162 |
CurrentTeam:= Teams[CurrTeam]; |
|
163 |
with CurrentTeam^ do |
|
552 | 164 |
begin |
165 |
PrevHH:= CurrHedgehog; |
|
550 | 166 |
repeat |
552 | 167 |
CurrHedgehog:= Succ(CurrHedgehog) mod HedgehogsNumber; |
168 |
until (Hedgehogs[CurrHedgehog].Gear <> nil) or (CurrHedgehog = PrevHH) |
|
169 |
end |
|
170 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (PrevTeam = CurrTeam); |
|
550 | 171 |
until CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil; |
83 | 172 |
|
351 | 173 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
4 | 174 |
begin |
175 |
with Gear^ do |
|
176 |
begin |
|
294 | 177 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
178 |
State:= gstHHDriven; |
4 | 179 |
Active:= true |
180 |
end; |
|
294 | 181 |
RemoveGearFromList(Gear); |
182 |
InsertGearToList(Gear); |
|
4 | 183 |
FollowGear:= Gear |
184 |
end; |
|
185 |
ResetKbd; |
|
351 | 186 |
|
187 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
498 | 188 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, _0, _0, 1); |
351 | 189 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
4 | 190 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
351 | 191 |
ApplyAmmoChanges(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
192 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
|
193 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 194 |
bShowFinger:= true; |
4 | 195 |
TurnTimeLeft:= cHedgehogTurnTime |
196 |
end; |
|
197 |
||
549 | 198 |
function AddTeam(TeamColor: Longword): PTeam; |
351 | 199 |
var Result: PTeam; |
549 | 200 |
c: LongInt; |
4 | 201 |
begin |
547 | 202 |
TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true); |
17 | 203 |
New(Result); |
80 | 204 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 205 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 206 |
Result^.AttackBar:= 2; |
207 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
547 | 208 |
|
209 |
TeamsArray[TeamsCount]:= Result; |
|
210 |
inc(TeamsCount); |
|
211 |
||
549 | 212 |
c:= Pred(ClansCount); |
213 |
while (c >= 0) and (ClansArray[c]^.Color <> TeamColor) do dec(c); |
|
214 |
if c < 0 then |
|
215 |
begin |
|
216 |
new(Result^.Clan); |
|
217 |
FillChar(Result^.Clan^, sizeof(TClan), 0); |
|
218 |
ClansArray[ClansCount]:= Result^.Clan; |
|
219 |
inc(ClansCount); |
|
220 |
with Result^.Clan^ do |
|
221 |
begin |
|
550 | 222 |
ClanIndex:= Pred(ClansCount); |
549 | 223 |
Color:= TeamColor; |
224 |
AdjColor:= Color; |
|
225 |
AdjustColor(AdjColor); |
|
226 |
end |
|
227 |
end else |
|
228 |
begin |
|
229 |
Result^.Clan:= ClansArray[c]; |
|
230 |
end; |
|
231 |
||
232 |
with Result^.Clan^ do |
|
233 |
begin |
|
234 |
Teams[TeamsNumber]:= Result; |
|
235 |
inc(TeamsNumber) |
|
236 |
end; |
|
237 |
||
351 | 238 |
CurrentTeam:= Result; |
239 |
AddTeam:= Result |
|
4 | 240 |
end; |
241 |
||
242 |
procedure FreeTeamsList; |
|
547 | 243 |
var t: LongInt; |
4 | 244 |
begin |
547 | 245 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
246 |
TeamsCount:= 0 |
|
4 | 247 |
end; |
248 |
||
47 | 249 |
procedure RecountAllTeamsHealth; |
547 | 250 |
var t: LongInt; |
251 |
begin |
|
252 |
for t:= 0 to Pred(TeamsCount) do |
|
253 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 254 |
end; |
255 |
||
256 |
procedure InitTeams; |
|
547 | 257 |
var i, t: LongInt; |
371 | 258 |
th: LongInt; |
47 | 259 |
begin |
547 | 260 |
for t:= 0 to Pred(TeamsCount) do |
261 |
with TeamsArray[t]^ do |
|
47 | 262 |
begin |
263 |
th:= 0; |
|
4 | 264 |
for i:= 0 to cMaxHHIndex do |
547 | 265 |
if Hedgehogs[i].Gear <> nil then |
266 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 267 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 268 |
end; |
47 | 269 |
RecountAllTeamsHealth |
4 | 270 |
end; |
271 |
||
272 |
function TeamSize(p: PTeam): Longword; |
|
351 | 273 |
var i, Result: Longword; |
4 | 274 |
begin |
275 |
Result:= 0; |
|
276 |
for i:= 0 to cMaxHHIndex do |
|
351 | 277 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
278 |
TeamSize:= Result |
|
4 | 279 |
end; |
280 |
||
549 | 281 |
procedure RecountClanHealth(clan: PClan); |
282 |
var i: LongInt; |
|
283 |
begin |
|
284 |
with clan^ do |
|
285 |
begin |
|
286 |
ClanHealth:= 0; |
|
287 |
for i:= 0 to Pred(TeamsNumber) do |
|
288 |
inc(ClanHealth, Teams[i]^.TeamHealth) |
|
289 |
end |
|
290 |
end; |
|
291 |
||
47 | 292 |
procedure RecountTeamHealth(team: PTeam); |
371 | 293 |
var i: LongInt; |
47 | 294 |
begin |
295 |
with team^ do |
|
296 |
begin |
|
557 | 297 |
NewTeamHealthBarWidth:= 0; |
47 | 298 |
for i:= 0 to cMaxHHIndex do |
299 |
if Hedgehogs[i].Gear <> nil then |
|
557 | 300 |
inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
301 |
TeamHealth:= NewTeamHealthBarWidth; |
|
302 |
if NewTeamHealthBarWidth > MaxTeamHealth then |
|
47 | 303 |
begin |
557 | 304 |
MaxTeamHealth:= NewTeamHealthBarWidth; |
47 | 305 |
RecountAllTeamsHealth; |
557 | 306 |
end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 307 |
end; |
549 | 308 |
|
309 |
RecountClanHealth(team^.Clan); |
|
310 |
||
498 | 311 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 312 |
end; |
313 |
||
72 | 314 |
procedure RestoreTeamsFromSave; |
547 | 315 |
var t: LongInt; |
72 | 316 |
begin |
547 | 317 |
for t:= 0 to Pred(TeamsCount) do |
318 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 319 |
end; |
320 |
||
165 | 321 |
procedure SetWeapon(weap: TAmmoType); |
371 | 322 |
var t: LongInt; |
165 | 323 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
324 |
t:= cMaxSlotAmmoIndex; |
165 | 325 |
with CurrentTeam^ do |
326 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 327 |
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
|
328 |
begin |
351 | 329 |
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
|
330 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
331 |
end |
165 | 332 |
end; |
333 |
||
307 | 334 |
procedure SendStats; |
547 | 335 |
var i, t: LongInt; |
307 | 336 |
msd: Longword; msdhh: PHedgehog; |
337 |
begin |
|
338 |
msd:= 0; msdhh:= nil; |
|
547 | 339 |
for t:= 0 to Pred(TeamsCount) do |
340 |
with TeamsArray[t]^ do |
|
307 | 341 |
begin |
342 |
for i:= 0 to cMaxHHIndex do |
|
547 | 343 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 344 |
begin |
547 | 345 |
msdhh:= @Hedgehogs[i]; |
346 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 347 |
end; |
348 |
end; |
|
351 | 349 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
350 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 351 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
352 |
end; |
|
353 |
||
4 | 354 |
initialization |
355 |
||
356 |
finalization |
|
357 |
||
358 |
FreeTeamsList |
|
359 |
||
360 |
end. |