author | unc0rr |
Sun, 08 Jul 2007 17:16:46 +0000 | |
changeset 553 | 5478386d935f |
parent 552 | 2167020d3bc0 |
child 557 | a7d49d5e8257 |
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; |
4 | 80 |
procedure SwitchHedgehog; |
81 |
procedure InitTeams; |
|
82 |
function TeamSize(p: PTeam): Longword; |
|
47 | 83 |
procedure RecountTeamHealth(team: PTeam); |
72 | 84 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
85 |
function CheckForWin: boolean; |
165 | 86 |
procedure SetWeapon(weap: TAmmoType); |
307 | 87 |
procedure SendStats; |
4 | 88 |
|
89 |
implementation |
|
534 | 90 |
uses uMisc, uWorld, uAI, uLocale, uConsole, uAmmos; |
371 | 91 |
const MaxTeamHealth: LongInt = 0; |
4 | 92 |
|
93 |
procedure FreeTeamsList; forward; |
|
94 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
95 |
function CheckForWin: boolean; |
549 | 96 |
var team: PTeam; |
97 |
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; |
547 | 135 |
t: LongWord; |
351 | 136 |
g: PGear; |
552 | 137 |
PrevHH, PrevTeam: LongWord; |
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; |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
552
diff
changeset
|
149 |
SwitchNotHoldedAmmo(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
534 | 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 |
||
271 |
function TeamSize(p: PTeam): Longword; |
|
351 | 272 |
var i, Result: Longword; |
4 | 273 |
begin |
274 |
Result:= 0; |
|
275 |
for i:= 0 to cMaxHHIndex do |
|
351 | 276 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
277 |
TeamSize:= Result |
|
4 | 278 |
end; |
279 |
||
549 | 280 |
procedure RecountClanHealth(clan: PClan); |
281 |
var i: LongInt; |
|
282 |
begin |
|
283 |
with clan^ do |
|
284 |
begin |
|
285 |
ClanHealth:= 0; |
|
286 |
for i:= 0 to Pred(TeamsNumber) do |
|
287 |
inc(ClanHealth, Teams[i]^.TeamHealth) |
|
288 |
end |
|
289 |
end; |
|
290 |
||
47 | 291 |
procedure RecountTeamHealth(team: PTeam); |
371 | 292 |
var i: LongInt; |
47 | 293 |
begin |
294 |
with team^ do |
|
295 |
begin |
|
146 | 296 |
TeamHealthBarWidth:= 0; |
47 | 297 |
for i:= 0 to cMaxHHIndex do |
298 |
if Hedgehogs[i].Gear <> nil then |
|
351 | 299 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
146 | 300 |
TeamHealth:= TeamHealthBarWidth; |
301 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 302 |
begin |
146 | 303 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 304 |
RecountAllTeamsHealth; |
146 | 305 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 306 |
end; |
549 | 307 |
|
308 |
RecountClanHealth(team^.Clan); |
|
309 |
||
105 | 310 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
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. |