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