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