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