author | unc0rr |
Thu, 13 Mar 2008 20:21:45 +0000 | |
changeset 812 | cbc392576990 |
parent 801 | 0323e5c7ee54 |
child 814 | 7fb4417b7bc1 |
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; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
54 |
CrosshairTex, |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
55 |
GraveTex, |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
56 |
HealthTex: PTexture; |
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 |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
67 |
Color: Longword; |
549 | 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; |
307 | 90 |
procedure SendStats; |
4 | 91 |
|
92 |
implementation |
|
800 | 93 |
uses uMisc, uWorld, uAI, uLocale, uConsole, uAmmos, uSound; |
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^); |
800 | 194 |
|
351 | 195 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
196 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 197 |
bShowFinger:= true; |
800 | 198 |
|
801 | 199 |
if (CurrentTeam^.ExtDriven or (CurrentHedgehog^.BotLevel > 0)) then |
200 |
PlaySound(sndIllGetYou, false) |
|
201 |
else |
|
202 |
PlaySound(sndYesSir, false); |
|
800 | 203 |
|
4 | 204 |
TurnTimeLeft:= cHedgehogTurnTime |
205 |
end; |
|
206 |
||
549 | 207 |
function AddTeam(TeamColor: Longword): PTeam; |
351 | 208 |
var Result: PTeam; |
549 | 209 |
c: LongInt; |
4 | 210 |
begin |
547 | 211 |
TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true); |
17 | 212 |
New(Result); |
80 | 213 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 214 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 215 |
Result^.AttackBar:= 2; |
216 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
547 | 217 |
|
218 |
TeamsArray[TeamsCount]:= Result; |
|
219 |
inc(TeamsCount); |
|
220 |
||
549 | 221 |
c:= Pred(ClansCount); |
222 |
while (c >= 0) and (ClansArray[c]^.Color <> TeamColor) do dec(c); |
|
223 |
if c < 0 then |
|
224 |
begin |
|
225 |
new(Result^.Clan); |
|
226 |
FillChar(Result^.Clan^, sizeof(TClan), 0); |
|
227 |
ClansArray[ClansCount]:= Result^.Clan; |
|
228 |
inc(ClansCount); |
|
229 |
with Result^.Clan^ do |
|
230 |
begin |
|
550 | 231 |
ClanIndex:= Pred(ClansCount); |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
232 |
Color:= TeamColor |
549 | 233 |
end |
234 |
end else |
|
235 |
begin |
|
236 |
Result^.Clan:= ClansArray[c]; |
|
237 |
end; |
|
238 |
||
239 |
with Result^.Clan^ do |
|
240 |
begin |
|
241 |
Teams[TeamsNumber]:= Result; |
|
242 |
inc(TeamsNumber) |
|
243 |
end; |
|
244 |
||
351 | 245 |
CurrentTeam:= Result; |
246 |
AddTeam:= Result |
|
4 | 247 |
end; |
248 |
||
249 |
procedure FreeTeamsList; |
|
547 | 250 |
var t: LongInt; |
4 | 251 |
begin |
547 | 252 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
253 |
TeamsCount:= 0 |
|
4 | 254 |
end; |
255 |
||
47 | 256 |
procedure RecountAllTeamsHealth; |
547 | 257 |
var t: LongInt; |
258 |
begin |
|
259 |
for t:= 0 to Pred(TeamsCount) do |
|
260 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 261 |
end; |
262 |
||
263 |
procedure InitTeams; |
|
547 | 264 |
var i, t: LongInt; |
371 | 265 |
th: LongInt; |
47 | 266 |
begin |
547 | 267 |
for t:= 0 to Pred(TeamsCount) do |
268 |
with TeamsArray[t]^ do |
|
47 | 269 |
begin |
270 |
th:= 0; |
|
4 | 271 |
for i:= 0 to cMaxHHIndex do |
547 | 272 |
if Hedgehogs[i].Gear <> nil then |
273 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 274 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 275 |
end; |
47 | 276 |
RecountAllTeamsHealth |
4 | 277 |
end; |
278 |
||
279 |
function TeamSize(p: PTeam): Longword; |
|
351 | 280 |
var i, Result: Longword; |
4 | 281 |
begin |
282 |
Result:= 0; |
|
283 |
for i:= 0 to cMaxHHIndex do |
|
351 | 284 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
285 |
TeamSize:= Result |
|
4 | 286 |
end; |
287 |
||
549 | 288 |
procedure RecountClanHealth(clan: PClan); |
289 |
var i: LongInt; |
|
290 |
begin |
|
291 |
with clan^ do |
|
292 |
begin |
|
293 |
ClanHealth:= 0; |
|
294 |
for i:= 0 to Pred(TeamsNumber) do |
|
295 |
inc(ClanHealth, Teams[i]^.TeamHealth) |
|
296 |
end |
|
297 |
end; |
|
298 |
||
47 | 299 |
procedure RecountTeamHealth(team: PTeam); |
371 | 300 |
var i: LongInt; |
47 | 301 |
begin |
302 |
with team^ do |
|
303 |
begin |
|
557 | 304 |
NewTeamHealthBarWidth:= 0; |
47 | 305 |
for i:= 0 to cMaxHHIndex do |
306 |
if Hedgehogs[i].Gear <> nil then |
|
557 | 307 |
inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
308 |
TeamHealth:= NewTeamHealthBarWidth; |
|
309 |
if NewTeamHealthBarWidth > MaxTeamHealth then |
|
47 | 310 |
begin |
557 | 311 |
MaxTeamHealth:= NewTeamHealthBarWidth; |
47 | 312 |
RecountAllTeamsHealth; |
557 | 313 |
end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 314 |
end; |
549 | 315 |
|
316 |
RecountClanHealth(team^.Clan); |
|
317 |
||
498 | 318 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 319 |
end; |
320 |
||
72 | 321 |
procedure RestoreTeamsFromSave; |
547 | 322 |
var t: LongInt; |
72 | 323 |
begin |
547 | 324 |
for t:= 0 to Pred(TeamsCount) do |
325 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 326 |
end; |
327 |
||
307 | 328 |
procedure SendStats; |
547 | 329 |
var i, t: LongInt; |
307 | 330 |
msd: Longword; msdhh: PHedgehog; |
331 |
begin |
|
332 |
msd:= 0; msdhh:= nil; |
|
547 | 333 |
for t:= 0 to Pred(TeamsCount) do |
334 |
with TeamsArray[t]^ do |
|
307 | 335 |
begin |
336 |
for i:= 0 to cMaxHHIndex do |
|
547 | 337 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 338 |
begin |
547 | 339 |
msdhh:= @Hedgehogs[i]; |
340 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 341 |
end; |
342 |
end; |
|
351 | 343 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
344 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 345 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
346 |
end; |
|
347 |
||
4 | 348 |
initialization |
349 |
||
350 |
finalization |
|
351 |
||
352 |
FreeTeamsList |
|
353 |
||
354 |
end. |