author | unc0rr |
Sun, 02 Dec 2007 17:29:20 +0000 | |
changeset 626 | a30171dbcd19 |
parent 602 | f7628ebfccde |
child 689 | f4ec46c48ed2 |
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; |
|
602 | 74 |
CurrentHedgehog: PHedgehog = nil; |
547 | 75 |
TeamsArray: array[0..Pred(cMaxTeams)] of PTeam; |
76 |
TeamsCount: Longword = 0; |
|
549 | 77 |
ClansArray: array[0..Pred(cMaxTeams)] of PClan; |
78 |
ClansCount: Longword = 0; |
|
304 | 79 |
CurMinAngle, CurMaxAngle: Longword; |
4 | 80 |
|
549 | 81 |
function AddTeam(TeamColor: Longword): PTeam; |
4 | 82 |
procedure SwitchHedgehog; |
83 |
procedure InitTeams; |
|
84 |
function TeamSize(p: PTeam): Longword; |
|
47 | 85 |
procedure RecountTeamHealth(team: PTeam); |
72 | 86 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
87 |
function CheckForWin: boolean; |
165 | 88 |
procedure SetWeapon(weap: TAmmoType); |
307 | 89 |
procedure SendStats; |
4 | 90 |
|
91 |
implementation |
|
534 | 92 |
uses uMisc, uWorld, uAI, uLocale, uConsole, uAmmos; |
371 | 93 |
const MaxTeamHealth: LongInt = 0; |
4 | 94 |
|
95 |
procedure FreeTeamsList; forward; |
|
96 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
97 |
function CheckForWin: boolean; |
576 | 98 |
var 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 |
||
601
78a68cc4d846
Special game mode allowing the only clan on map for training mode
unc0rr
parents:
576
diff
changeset
|
110 |
if (AliveCount > 1) or |
78a68cc4d846
Special game mode allowing the only clan on map for training mode
unc0rr
parents:
576
diff
changeset
|
111 |
((AliveCount = 1) and ((GameFlags and gfOneClanMode) <> 0)) then exit(false); |
351 | 112 |
CheckForWin:= true; |
83 | 113 |
|
114 |
TurnTimeLeft:= 0; |
|
548 | 115 |
if AliveCount = 0 then |
83 | 116 |
begin // draw |
117 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
306 | 118 |
SendStat(siGameResult, trmsg[sidDraw]); |
498 | 119 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 120 |
end else // win |
549 | 121 |
with AliveClan^ do |
122 |
begin |
|
123 |
if TeamsNumber = 1 then |
|
124 |
s:= Format(trmsg[sidWinner], Teams[0]^.TeamName) // team wins |
|
125 |
else |
|
126 |
s:= Format(trmsg[sidWinner], Teams[0]^.TeamName); // clan wins |
|
127 |
||
128 |
AddCaption(s, $FFFFFF, capgrpGameState); |
|
129 |
SendStat(siGameResult, s); |
|
130 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
|
131 |
end; |
|
307 | 132 |
SendStats |
83 | 133 |
end; |
134 |
||
4 | 135 |
procedure SwitchHedgehog; |
551 | 136 |
var c: 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 |
|
602 | 173 |
CurrentHedgehog:= @(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
174 |
with CurrentHedgehog^ do |
|
4 | 175 |
begin |
176 |
with Gear^ do |
|
177 |
begin |
|
294 | 178 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
179 |
State:= gstHHDriven; |
4 | 180 |
Active:= true |
181 |
end; |
|
294 | 182 |
RemoveGearFromList(Gear); |
183 |
InsertGearToList(Gear); |
|
4 | 184 |
FollowGear:= Gear |
185 |
end; |
|
186 |
ResetKbd; |
|
351 | 187 |
|
188 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
498 | 189 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, _0, _0, 1); |
351 | 190 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
4 | 191 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
602 | 192 |
ApplyAmmoChanges(CurrentHedgehog^); |
351 | 193 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
194 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 195 |
bShowFinger:= true; |
4 | 196 |
TurnTimeLeft:= cHedgehogTurnTime |
197 |
end; |
|
198 |
||
549 | 199 |
function AddTeam(TeamColor: Longword): PTeam; |
351 | 200 |
var Result: PTeam; |
549 | 201 |
c: LongInt; |
4 | 202 |
begin |
547 | 203 |
TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true); |
17 | 204 |
New(Result); |
80 | 205 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 206 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 207 |
Result^.AttackBar:= 2; |
208 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
547 | 209 |
|
210 |
TeamsArray[TeamsCount]:= Result; |
|
211 |
inc(TeamsCount); |
|
212 |
||
549 | 213 |
c:= Pred(ClansCount); |
214 |
while (c >= 0) and (ClansArray[c]^.Color <> TeamColor) do dec(c); |
|
215 |
if c < 0 then |
|
216 |
begin |
|
217 |
new(Result^.Clan); |
|
218 |
FillChar(Result^.Clan^, sizeof(TClan), 0); |
|
219 |
ClansArray[ClansCount]:= Result^.Clan; |
|
220 |
inc(ClansCount); |
|
221 |
with Result^.Clan^ do |
|
222 |
begin |
|
550 | 223 |
ClanIndex:= Pred(ClansCount); |
549 | 224 |
Color:= TeamColor; |
225 |
AdjColor:= Color; |
|
226 |
AdjustColor(AdjColor); |
|
227 |
end |
|
228 |
end else |
|
229 |
begin |
|
230 |
Result^.Clan:= ClansArray[c]; |
|
231 |
end; |
|
232 |
||
233 |
with Result^.Clan^ do |
|
234 |
begin |
|
235 |
Teams[TeamsNumber]:= Result; |
|
236 |
inc(TeamsNumber) |
|
237 |
end; |
|
238 |
||
351 | 239 |
CurrentTeam:= Result; |
240 |
AddTeam:= Result |
|
4 | 241 |
end; |
242 |
||
243 |
procedure FreeTeamsList; |
|
547 | 244 |
var t: LongInt; |
4 | 245 |
begin |
547 | 246 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
247 |
TeamsCount:= 0 |
|
4 | 248 |
end; |
249 |
||
47 | 250 |
procedure RecountAllTeamsHealth; |
547 | 251 |
var t: LongInt; |
252 |
begin |
|
253 |
for t:= 0 to Pred(TeamsCount) do |
|
254 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 255 |
end; |
256 |
||
257 |
procedure InitTeams; |
|
547 | 258 |
var i, t: LongInt; |
371 | 259 |
th: LongInt; |
47 | 260 |
begin |
547 | 261 |
for t:= 0 to Pred(TeamsCount) do |
262 |
with TeamsArray[t]^ do |
|
47 | 263 |
begin |
264 |
th:= 0; |
|
4 | 265 |
for i:= 0 to cMaxHHIndex do |
547 | 266 |
if Hedgehogs[i].Gear <> nil then |
267 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 268 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 269 |
end; |
47 | 270 |
RecountAllTeamsHealth |
4 | 271 |
end; |
272 |
||
273 |
function TeamSize(p: PTeam): Longword; |
|
351 | 274 |
var i, Result: Longword; |
4 | 275 |
begin |
276 |
Result:= 0; |
|
277 |
for i:= 0 to cMaxHHIndex do |
|
351 | 278 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
279 |
TeamSize:= Result |
|
4 | 280 |
end; |
281 |
||
549 | 282 |
procedure RecountClanHealth(clan: PClan); |
283 |
var i: LongInt; |
|
284 |
begin |
|
285 |
with clan^ do |
|
286 |
begin |
|
287 |
ClanHealth:= 0; |
|
288 |
for i:= 0 to Pred(TeamsNumber) do |
|
289 |
inc(ClanHealth, Teams[i]^.TeamHealth) |
|
290 |
end |
|
291 |
end; |
|
292 |
||
47 | 293 |
procedure RecountTeamHealth(team: PTeam); |
371 | 294 |
var i: LongInt; |
47 | 295 |
begin |
296 |
with team^ do |
|
297 |
begin |
|
557 | 298 |
NewTeamHealthBarWidth:= 0; |
47 | 299 |
for i:= 0 to cMaxHHIndex do |
300 |
if Hedgehogs[i].Gear <> nil then |
|
557 | 301 |
inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
302 |
TeamHealth:= NewTeamHealthBarWidth; |
|
303 |
if NewTeamHealthBarWidth > MaxTeamHealth then |
|
47 | 304 |
begin |
557 | 305 |
MaxTeamHealth:= NewTeamHealthBarWidth; |
47 | 306 |
RecountAllTeamsHealth; |
557 | 307 |
end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 308 |
end; |
549 | 309 |
|
310 |
RecountClanHealth(team^.Clan); |
|
311 |
||
498 | 312 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 313 |
end; |
314 |
||
72 | 315 |
procedure RestoreTeamsFromSave; |
547 | 316 |
var t: LongInt; |
72 | 317 |
begin |
547 | 318 |
for t:= 0 to Pred(TeamsCount) do |
319 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 320 |
end; |
321 |
||
165 | 322 |
procedure SetWeapon(weap: TAmmoType); |
371 | 323 |
var t: LongInt; |
165 | 324 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
325 |
t:= cMaxSlotAmmoIndex; |
165 | 326 |
with CurrentTeam^ do |
327 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 328 |
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
|
329 |
begin |
351 | 330 |
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
|
331 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
332 |
end |
165 | 333 |
end; |
334 |
||
307 | 335 |
procedure SendStats; |
547 | 336 |
var i, t: LongInt; |
307 | 337 |
msd: Longword; msdhh: PHedgehog; |
338 |
begin |
|
339 |
msd:= 0; msdhh:= nil; |
|
547 | 340 |
for t:= 0 to Pred(TeamsCount) do |
341 |
with TeamsArray[t]^ do |
|
307 | 342 |
begin |
343 |
for i:= 0 to cMaxHHIndex do |
|
547 | 344 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 345 |
begin |
547 | 346 |
msdhh:= @Hedgehogs[i]; |
347 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 348 |
end; |
349 |
end; |
|
351 | 350 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
351 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 352 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
353 |
end; |
|
354 |
||
4 | 355 |
initialization |
356 |
||
357 |
finalization |
|
358 |
||
359 |
FreeTeamsList |
|
360 |
||
361 |
end. |