author | unc0rr |
Sun, 27 Jul 2008 16:03:45 +0000 | |
changeset 1116 | dd6db8e09f9e |
parent 1066 | 1f1b3686a2b0 |
child 1170 | bd0f414e395a |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2004-2008 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 |
|
814
7fb4417b7bc1
Start implementing better statistics implementation (does nothing yet)
unc0rr
parents:
801
diff
changeset
|
21 |
uses SDLh, uConsts, uKeys, uGears, uRandom, uFloat, uStats; |
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 |
Team: PTeam; |
|
39 |
AttacksNum: Longword; |
|
40 |
visStepPos: LongWord; |
|
5 | 41 |
BotLevel : LongWord; // 0 - Human player |
814
7fb4417b7bc1
Start implementing better statistics implementation (does nothing yet)
unc0rr
parents:
801
diff
changeset
|
42 |
stats: TStatistics; |
4 | 43 |
end; |
44 |
TTeam = record |
|
549 | 45 |
Clan: PClan; |
74 | 46 |
TeamName: string[MAXNAMELEN]; |
4 | 47 |
ExtDriven: boolean; |
167 | 48 |
Binds: TBinds; |
4 | 49 |
Hedgehogs: array[0..cMaxHHIndex] of THedgehog; |
551 | 50 |
CurrHedgehog: LongWord; |
762 | 51 |
NameTagTex: PTexture; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
52 |
CrosshairTex, |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
53 |
GraveTex, |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
54 |
HealthTex: PTexture; |
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 |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
65 |
Color: Longword; |
549 | 66 |
Teams: array[0..Pred(cMaxTeams)] of PTeam; |
67 |
TeamsNumber: Longword; |
|
551 | 68 |
CurrTeam: LongWord; |
549 | 69 |
ClanHealth: LongInt; |
550 | 70 |
ClanIndex: LongInt; |
941
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
71 |
TurnNumber: LongWord; |
549 | 72 |
end; |
4 | 73 |
|
74 |
var CurrentTeam: PTeam = nil; |
|
602 | 75 |
CurrentHedgehog: PHedgehog = nil; |
547 | 76 |
TeamsArray: array[0..Pred(cMaxTeams)] of PTeam; |
77 |
TeamsCount: Longword = 0; |
|
549 | 78 |
ClansArray: array[0..Pred(cMaxTeams)] of PClan; |
79 |
ClansCount: Longword = 0; |
|
304 | 80 |
CurMinAngle, CurMaxAngle: Longword; |
4 | 81 |
|
1058
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
82 |
function AddTeam(TeamColor: Longword): PTeam; |
4 | 83 |
procedure SwitchHedgehog; |
1058
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
84 |
procedure AfterSwitchHedgehog; |
4 | 85 |
procedure InitTeams; |
86 |
function TeamSize(p: PTeam): Longword; |
|
47 | 87 |
procedure RecountTeamHealth(team: PTeam); |
72 | 88 |
procedure RestoreTeamsFromSave; |
1058
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
89 |
function CheckForWin: boolean; |
4 | 90 |
|
91 |
implementation |
|
800 | 92 |
uses uMisc, uWorld, uAI, uLocale, uConsole, uAmmos, uSound; |
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; |
1011 | 100 |
t, AliveCount, i, j: 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 |
1011 | 116 |
begin // draw |
117 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
118 |
SendStat(siGameResult, trmsg[sidDraw]); |
|
119 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 3000) |
|
120 |
end else // win |
|
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 |
|
549 | 127 |
|
1011 | 128 |
for j:= 0 to Pred(TeamsNumber) do |
129 |
with Teams[j]^ do |
|
130 |
for i:= 0 to cMaxHHIndex do |
|
131 |
with Hedgehogs[i] do |
|
132 |
if (Gear <> nil) then |
|
133 |
Gear^.State:= gstWinner; |
|
134 |
||
135 |
AddCaption(s, $FFFFFF, capgrpGameState); |
|
136 |
SendStat(siGameResult, s); |
|
137 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 3000) |
|
138 |
end; |
|
307 | 139 |
SendStats |
83 | 140 |
end; |
141 |
||
4 | 142 |
procedure SwitchHedgehog; |
551 | 143 |
var c: LongWord; |
552 | 144 |
PrevHH, PrevTeam: LongWord; |
4 | 145 |
begin |
146 |
FreeActionsList; |
|
147 |
TargetPoint.X:= NoPointX; |
|
89 | 148 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
547 | 149 |
|
944 | 150 |
with CurrentHedgehog^ do |
294 | 151 |
if Gear <> nil then |
152 |
begin |
|
540 | 153 |
AttacksNum:= 0; |
351 | 154 |
Gear^.Message:= 0; |
534 | 155 |
Gear^.Z:= cHHZ; |
156 |
RemoveGearFromList(Gear); |
|
157 |
InsertGearToList(Gear) |
|
294 | 158 |
end; |
4 | 159 |
|
550 | 160 |
c:= CurrentTeam^.Clan^.ClanIndex; |
4 | 161 |
repeat |
1058
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
162 |
inc(c); |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
163 |
if c = ClansCount then |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
164 |
begin |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
165 |
inc(TotalRounds); |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
166 |
c:= 0 |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
167 |
end; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
168 |
|
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
169 |
with ClansArray[c]^ do |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
170 |
repeat |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
171 |
PrevTeam:= CurrTeam; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
172 |
CurrTeam:= Succ(CurrTeam) mod TeamsNumber; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
173 |
CurrentTeam:= Teams[CurrTeam]; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
174 |
with CurrentTeam^ do |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
175 |
begin |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
176 |
PrevHH:= CurrHedgehog; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
177 |
repeat |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
178 |
CurrHedgehog:= Succ(CurrHedgehog) mod HedgehogsNumber; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
179 |
until (Hedgehogs[CurrHedgehog].Gear <> nil) or (CurrHedgehog = PrevHH) |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
180 |
end |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
181 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (PrevTeam = CurrTeam); |
550 | 182 |
until CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil; |
83 | 183 |
|
1058
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
184 |
CurrentHedgehog:= @(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]) |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
185 |
end; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
186 |
|
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
187 |
procedure AfterSwitchHedgehog; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
188 |
var g: PGear; |
c53c5c4e7b48
- Proper turns counting, split SwitchHedgehog into two functions
unc0rr
parents:
1011
diff
changeset
|
189 |
begin |
944 | 190 |
SwitchNotHoldedAmmo(CurrentHedgehog^); |
602 | 191 |
with CurrentHedgehog^ do |
4 | 192 |
begin |
193 |
with Gear^ do |
|
194 |
begin |
|
294 | 195 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
196 |
State:= gstHHDriven; |
4 | 197 |
Active:= true |
198 |
end; |
|
294 | 199 |
RemoveGearFromList(Gear); |
200 |
InsertGearToList(Gear); |
|
4 | 201 |
FollowGear:= Gear |
202 |
end; |
|
941
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
203 |
|
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
204 |
inc(CurrentTeam^.Clan^.TurnNumber); |
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
927
diff
changeset
|
205 |
|
4 | 206 |
ResetKbd; |
351 | 207 |
|
208 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
498 | 209 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, _0, _0, 1); |
351 | 210 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
4 | 211 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
602 | 212 |
ApplyAmmoChanges(CurrentHedgehog^); |
800 | 213 |
|
351 | 214 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
215 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 216 |
bShowFinger:= true; |
800 | 217 |
|
801 | 218 |
if (CurrentTeam^.ExtDriven or (CurrentHedgehog^.BotLevel > 0)) then |
219 |
PlaySound(sndIllGetYou, false) |
|
220 |
else |
|
221 |
PlaySound(sndYesSir, false); |
|
800 | 222 |
|
4 | 223 |
TurnTimeLeft:= cHedgehogTurnTime |
224 |
end; |
|
225 |
||
549 | 226 |
function AddTeam(TeamColor: Longword): PTeam; |
351 | 227 |
var Result: PTeam; |
549 | 228 |
c: LongInt; |
4 | 229 |
begin |
547 | 230 |
TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true); |
17 | 231 |
New(Result); |
80 | 232 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 233 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 234 |
Result^.AttackBar:= 2; |
235 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
547 | 236 |
|
237 |
TeamsArray[TeamsCount]:= Result; |
|
238 |
inc(TeamsCount); |
|
239 |
||
549 | 240 |
c:= Pred(ClansCount); |
241 |
while (c >= 0) and (ClansArray[c]^.Color <> TeamColor) do dec(c); |
|
242 |
if c < 0 then |
|
243 |
begin |
|
244 |
new(Result^.Clan); |
|
245 |
FillChar(Result^.Clan^, sizeof(TClan), 0); |
|
246 |
ClansArray[ClansCount]:= Result^.Clan; |
|
247 |
inc(ClansCount); |
|
248 |
with Result^.Clan^ do |
|
249 |
begin |
|
550 | 250 |
ClanIndex:= Pred(ClansCount); |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
251 |
Color:= TeamColor |
549 | 252 |
end |
253 |
end else |
|
254 |
begin |
|
255 |
Result^.Clan:= ClansArray[c]; |
|
256 |
end; |
|
257 |
||
258 |
with Result^.Clan^ do |
|
259 |
begin |
|
260 |
Teams[TeamsNumber]:= Result; |
|
261 |
inc(TeamsNumber) |
|
262 |
end; |
|
263 |
||
351 | 264 |
CurrentTeam:= Result; |
265 |
AddTeam:= Result |
|
4 | 266 |
end; |
267 |
||
268 |
procedure FreeTeamsList; |
|
547 | 269 |
var t: LongInt; |
4 | 270 |
begin |
547 | 271 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
272 |
TeamsCount:= 0 |
|
4 | 273 |
end; |
274 |
||
47 | 275 |
procedure RecountAllTeamsHealth; |
547 | 276 |
var t: LongInt; |
277 |
begin |
|
278 |
for t:= 0 to Pred(TeamsCount) do |
|
279 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 280 |
end; |
281 |
||
282 |
procedure InitTeams; |
|
547 | 283 |
var i, t: LongInt; |
371 | 284 |
th: LongInt; |
47 | 285 |
begin |
547 | 286 |
for t:= 0 to Pred(TeamsCount) do |
287 |
with TeamsArray[t]^ do |
|
47 | 288 |
begin |
289 |
th:= 0; |
|
4 | 290 |
for i:= 0 to cMaxHHIndex do |
547 | 291 |
if Hedgehogs[i].Gear <> nil then |
292 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 293 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 294 |
end; |
47 | 295 |
RecountAllTeamsHealth |
4 | 296 |
end; |
297 |
||
298 |
function TeamSize(p: PTeam): Longword; |
|
351 | 299 |
var i, Result: Longword; |
4 | 300 |
begin |
301 |
Result:= 0; |
|
302 |
for i:= 0 to cMaxHHIndex do |
|
351 | 303 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
304 |
TeamSize:= Result |
|
4 | 305 |
end; |
306 |
||
549 | 307 |
procedure RecountClanHealth(clan: PClan); |
308 |
var i: LongInt; |
|
309 |
begin |
|
310 |
with clan^ do |
|
311 |
begin |
|
312 |
ClanHealth:= 0; |
|
313 |
for i:= 0 to Pred(TeamsNumber) do |
|
314 |
inc(ClanHealth, Teams[i]^.TeamHealth) |
|
315 |
end |
|
316 |
end; |
|
317 |
||
47 | 318 |
procedure RecountTeamHealth(team: PTeam); |
371 | 319 |
var i: LongInt; |
47 | 320 |
begin |
321 |
with team^ do |
|
322 |
begin |
|
557 | 323 |
NewTeamHealthBarWidth:= 0; |
47 | 324 |
for i:= 0 to cMaxHHIndex do |
325 |
if Hedgehogs[i].Gear <> nil then |
|
557 | 326 |
inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
327 |
TeamHealth:= NewTeamHealthBarWidth; |
|
328 |
if NewTeamHealthBarWidth > MaxTeamHealth then |
|
47 | 329 |
begin |
557 | 330 |
MaxTeamHealth:= NewTeamHealthBarWidth; |
47 | 331 |
RecountAllTeamsHealth; |
557 | 332 |
end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 333 |
end; |
549 | 334 |
|
335 |
RecountClanHealth(team^.Clan); |
|
336 |
||
498 | 337 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 338 |
end; |
339 |
||
72 | 340 |
procedure RestoreTeamsFromSave; |
547 | 341 |
var t: LongInt; |
72 | 342 |
begin |
547 | 343 |
for t:= 0 to Pred(TeamsCount) do |
344 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 345 |
end; |
346 |
||
4 | 347 |
initialization |
348 |
||
349 |
finalization |
|
350 |
||
351 |
FreeTeamsList |
|
352 |
||
353 |
end. |