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