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