author | unc0rr |
Tue, 05 Feb 2008 17:53:38 +0000 | |
changeset 783 | 4f59db6f147d |
parent 764 | 7513452b1d51 |
child 800 | e95f16b7ec80 |
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 |
|
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); |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
225 |
Color:= TeamColor |
549 | 226 |
end |
227 |
end else |
|
228 |
begin |
|
229 |
Result^.Clan:= ClansArray[c]; |
|
230 |
end; |
|
231 |
||
232 |
with Result^.Clan^ do |
|
233 |
begin |
|
234 |
Teams[TeamsNumber]:= Result; |
|
235 |
inc(TeamsNumber) |
|
236 |
end; |
|
237 |
||
351 | 238 |
CurrentTeam:= Result; |
239 |
AddTeam:= Result |
|
4 | 240 |
end; |
241 |
||
242 |
procedure FreeTeamsList; |
|
547 | 243 |
var t: LongInt; |
4 | 244 |
begin |
547 | 245 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
246 |
TeamsCount:= 0 |
|
4 | 247 |
end; |
248 |
||
47 | 249 |
procedure RecountAllTeamsHealth; |
547 | 250 |
var t: LongInt; |
251 |
begin |
|
252 |
for t:= 0 to Pred(TeamsCount) do |
|
253 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 254 |
end; |
255 |
||
256 |
procedure InitTeams; |
|
547 | 257 |
var i, t: LongInt; |
371 | 258 |
th: LongInt; |
47 | 259 |
begin |
547 | 260 |
for t:= 0 to Pred(TeamsCount) do |
261 |
with TeamsArray[t]^ do |
|
47 | 262 |
begin |
263 |
th:= 0; |
|
4 | 264 |
for i:= 0 to cMaxHHIndex do |
547 | 265 |
if Hedgehogs[i].Gear <> nil then |
266 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 267 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 268 |
end; |
47 | 269 |
RecountAllTeamsHealth |
4 | 270 |
end; |
271 |
||
272 |
function TeamSize(p: PTeam): Longword; |
|
351 | 273 |
var i, Result: Longword; |
4 | 274 |
begin |
275 |
Result:= 0; |
|
276 |
for i:= 0 to cMaxHHIndex do |
|
351 | 277 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
278 |
TeamSize:= Result |
|
4 | 279 |
end; |
280 |
||
549 | 281 |
procedure RecountClanHealth(clan: PClan); |
282 |
var i: LongInt; |
|
283 |
begin |
|
284 |
with clan^ do |
|
285 |
begin |
|
286 |
ClanHealth:= 0; |
|
287 |
for i:= 0 to Pred(TeamsNumber) do |
|
288 |
inc(ClanHealth, Teams[i]^.TeamHealth) |
|
289 |
end |
|
290 |
end; |
|
291 |
||
47 | 292 |
procedure RecountTeamHealth(team: PTeam); |
371 | 293 |
var i: LongInt; |
47 | 294 |
begin |
295 |
with team^ do |
|
296 |
begin |
|
557 | 297 |
NewTeamHealthBarWidth:= 0; |
47 | 298 |
for i:= 0 to cMaxHHIndex do |
299 |
if Hedgehogs[i].Gear <> nil then |
|
557 | 300 |
inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
301 |
TeamHealth:= NewTeamHealthBarWidth; |
|
302 |
if NewTeamHealthBarWidth > MaxTeamHealth then |
|
47 | 303 |
begin |
557 | 304 |
MaxTeamHealth:= NewTeamHealthBarWidth; |
47 | 305 |
RecountAllTeamsHealth; |
557 | 306 |
end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 307 |
end; |
549 | 308 |
|
309 |
RecountClanHealth(team^.Clan); |
|
310 |
||
498 | 311 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 312 |
end; |
313 |
||
72 | 314 |
procedure RestoreTeamsFromSave; |
547 | 315 |
var t: LongInt; |
72 | 316 |
begin |
547 | 317 |
for t:= 0 to Pred(TeamsCount) do |
318 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 319 |
end; |
320 |
||
307 | 321 |
procedure SendStats; |
547 | 322 |
var i, t: LongInt; |
307 | 323 |
msd: Longword; msdhh: PHedgehog; |
324 |
begin |
|
325 |
msd:= 0; msdhh:= nil; |
|
547 | 326 |
for t:= 0 to Pred(TeamsCount) do |
327 |
with TeamsArray[t]^ do |
|
307 | 328 |
begin |
329 |
for i:= 0 to cMaxHHIndex do |
|
547 | 330 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 331 |
begin |
547 | 332 |
msdhh:= @Hedgehogs[i]; |
333 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 334 |
end; |
335 |
end; |
|
351 | 336 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
337 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 338 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
339 |
end; |
|
340 |
||
4 | 341 |
initialization |
342 |
||
343 |
finalization |
|
344 |
||
345 |
FreeTeamsList |
|
346 |
||
347 |
end. |