author | unc0rr |
Thu, 20 Sep 2007 20:00:10 +0000 | |
changeset 601 | 78a68cc4d846 |
parent 576 | 35a1af355ada |
child 602 | f7628ebfccde |
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; |
576 | 97 |
var 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 |
||
601
78a68cc4d846
Special game mode allowing the only clan on map for training mode
unc0rr
parents:
576
diff
changeset
|
109 |
if (AliveCount > 1) or |
78a68cc4d846
Special game mode allowing the only clan on map for training mode
unc0rr
parents:
576
diff
changeset
|
110 |
((AliveCount = 1) and ((GameFlags and gfOneClanMode) <> 0)) 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; |
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 |
|
557 | 296 |
NewTeamHealthBarWidth:= 0; |
47 | 297 |
for i:= 0 to cMaxHHIndex do |
298 |
if Hedgehogs[i].Gear <> nil then |
|
557 | 299 |
inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
300 |
TeamHealth:= NewTeamHealthBarWidth; |
|
301 |
if NewTeamHealthBarWidth > MaxTeamHealth then |
|
47 | 302 |
begin |
557 | 303 |
MaxTeamHealth:= NewTeamHealthBarWidth; |
47 | 304 |
RecountAllTeamsHealth; |
557 | 305 |
end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 306 |
end; |
549 | 307 |
|
308 |
RecountClanHealth(team^.Clan); |
|
309 |
||
498 | 310 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 311 |
end; |
312 |
||
72 | 313 |
procedure RestoreTeamsFromSave; |
547 | 314 |
var t: LongInt; |
72 | 315 |
begin |
547 | 316 |
for t:= 0 to Pred(TeamsCount) do |
317 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 318 |
end; |
319 |
||
165 | 320 |
procedure SetWeapon(weap: TAmmoType); |
371 | 321 |
var t: LongInt; |
165 | 322 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
323 |
t:= cMaxSlotAmmoIndex; |
165 | 324 |
with CurrentTeam^ do |
325 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 326 |
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
|
327 |
begin |
351 | 328 |
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
|
329 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
330 |
end |
165 | 331 |
end; |
332 |
||
307 | 333 |
procedure SendStats; |
547 | 334 |
var i, t: LongInt; |
307 | 335 |
msd: Longword; msdhh: PHedgehog; |
336 |
begin |
|
337 |
msd:= 0; msdhh:= nil; |
|
547 | 338 |
for t:= 0 to Pred(TeamsCount) do |
339 |
with TeamsArray[t]^ do |
|
307 | 340 |
begin |
341 |
for i:= 0 to cMaxHHIndex do |
|
547 | 342 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 343 |
begin |
547 | 344 |
msdhh:= @Hedgehogs[i]; |
345 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 346 |
end; |
347 |
end; |
|
351 | 348 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
349 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 350 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
351 |
end; |
|
352 |
||
4 | 353 |
initialization |
354 |
||
355 |
finalization |
|
356 |
||
357 |
FreeTeamsList |
|
358 |
||
359 |
end. |