author | unc0rr |
Mon, 11 Jun 2007 15:52:32 +0000 | |
changeset 540 | b06c5aace2fa |
parent 534 | 92fb2b0d5117 |
child 547 | b81a055f2d06 |
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; |
|
29 |
THedgehog = record |
|
74 | 30 |
Name: string[MAXNAMELEN]; |
4 | 31 |
Gear: PGear; |
95 | 32 |
NameTag, HealthTag: PSDL_Surface; |
4 | 33 |
Ammo: PHHAmmo; |
288 | 34 |
AmmoStore: Longword; |
4 | 35 |
CurSlot, CurAmmo: LongWord; |
36 |
AltSlot, AltAmmo: LongWord; |
|
37 |
Team: PTeam; |
|
38 |
AttacksNum: Longword; |
|
39 |
visStepPos: LongWord; |
|
5 | 40 |
BotLevel : LongWord; // 0 - Human player |
307 | 41 |
DamageGiven: Longword; |
42 |
MaxStepDamage: Longword; |
|
4 | 43 |
end; |
44 |
TTeam = record |
|
45 |
Next: PTeam; |
|
189 | 46 |
Color, AdjColor: Longword; |
74 | 47 |
TeamName: string[MAXNAMELEN]; |
4 | 48 |
ExtDriven: boolean; |
167 | 49 |
Binds: TBinds; |
4 | 50 |
Hedgehogs: array[0..cMaxHHIndex] of THedgehog; |
371 | 51 |
CurrHedgehog: LongInt; |
95 | 52 |
NameTag: PSDL_Surface; |
198 | 53 |
CrosshairSurf: PSDL_Surface; |
47 | 54 |
GraveRect, HealthRect: TSDL_Rect; |
4 | 55 |
GraveName: string; |
56 |
FortName: string; |
|
371 | 57 |
TeamHealth: LongInt; |
58 |
TeamHealthBarWidth: LongInt; |
|
59 |
DrawHealthY: LongInt; |
|
4 | 60 |
AttackBar: LongWord; |
312 | 61 |
HedgehogsNumber: byte; |
4 | 62 |
end; |
63 |
||
64 |
var CurrentTeam: PTeam = nil; |
|
65 |
TeamsList: PTeam = nil; |
|
304 | 66 |
CurMinAngle, CurMaxAngle: Longword; |
4 | 67 |
|
68 |
function AddTeam: PTeam; |
|
70 | 69 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 70 |
procedure SwitchHedgehog; |
71 |
procedure InitTeams; |
|
72 |
function TeamSize(p: PTeam): Longword; |
|
47 | 73 |
procedure RecountTeamHealth(team: PTeam); |
72 | 74 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
75 |
function CheckForWin: boolean; |
165 | 76 |
procedure SetWeapon(weap: TAmmoType); |
307 | 77 |
procedure SendStats; |
4 | 78 |
|
79 |
implementation |
|
534 | 80 |
uses uMisc, uWorld, uAI, uLocale, uConsole, uAmmos; |
371 | 81 |
const MaxTeamHealth: LongInt = 0; |
4 | 82 |
|
83 |
procedure FreeTeamsList; forward; |
|
84 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
85 |
function CheckForWin: boolean; |
83 | 86 |
var team, AliveTeam: PTeam; |
87 |
AliveCount: Longword; |
|
306 | 88 |
s: shortstring; |
83 | 89 |
begin |
90 |
AliveCount:= 0; |
|
91 |
AliveTeam:= nil; |
|
92 |
team:= TeamsList; |
|
93 |
while team <> nil do |
|
94 |
begin |
|
351 | 95 |
if team^.TeamHealth > 0 then |
83 | 96 |
begin |
97 |
inc(AliveCount); |
|
98 |
AliveTeam:= team |
|
99 |
end; |
|
351 | 100 |
team:= team^.Next |
83 | 101 |
end; |
102 |
||
351 | 103 |
if AliveCount >= 2 then exit(false); |
104 |
CheckForWin:= true; |
|
83 | 105 |
|
106 |
TurnTimeLeft:= 0; |
|
107 |
if AliveCount = 0 then |
|
108 |
begin // draw |
|
109 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
306 | 110 |
SendStat(siGameResult, trmsg[sidDraw]); |
498 | 111 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 112 |
end else // win |
113 |
begin |
|
351 | 114 |
s:= Format(trmsg[sidWinner], AliveTeam^.TeamName); |
306 | 115 |
AddCaption(s, $FFFFFF, capgrpGameState); |
116 |
SendStat(siGameResult, s); |
|
498 | 117 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 118 |
end; |
307 | 119 |
SendStats |
83 | 120 |
end; |
121 |
||
4 | 122 |
procedure SwitchHedgehog; |
123 |
var tteam: PTeam; |
|
371 | 124 |
th: LongInt; |
351 | 125 |
g: PGear; |
4 | 126 |
begin |
127 |
FreeActionsList; |
|
128 |
TargetPoint.X:= NoPointX; |
|
89 | 129 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
4 | 130 |
tteam:= CurrentTeam; |
351 | 131 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
294 | 132 |
if Gear <> nil then |
133 |
begin |
|
540 | 134 |
AttacksNum:= 0; |
351 | 135 |
Gear^.Message:= 0; |
534 | 136 |
Gear^.Z:= cHHZ; |
137 |
RemoveGearFromList(Gear); |
|
138 |
InsertGearToList(Gear) |
|
294 | 139 |
end; |
4 | 140 |
|
141 |
repeat |
|
351 | 142 |
CurrentTeam:= CurrentTeam^.Next; |
4 | 143 |
if CurrentTeam = nil then CurrentTeam:= TeamsList; |
351 | 144 |
th:= CurrentTeam^.CurrHedgehog; |
4 | 145 |
repeat |
351 | 146 |
CurrentTeam^.CurrHedgehog:= Succ(CurrentTeam^.CurrHedgehog) mod (cMaxHHIndex + 1); |
147 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (CurrentTeam^.CurrHedgehog = th) |
|
148 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (CurrentTeam = tteam); |
|
4 | 149 |
|
83 | 150 |
TryDo(CurrentTeam <> tteam, 'Switch hedgehog: only one team?!', true); |
151 |
||
351 | 152 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
4 | 153 |
begin |
154 |
with Gear^ do |
|
155 |
begin |
|
294 | 156 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
157 |
State:= gstHHDriven; |
4 | 158 |
Active:= true |
159 |
end; |
|
294 | 160 |
RemoveGearFromList(Gear); |
161 |
InsertGearToList(Gear); |
|
4 | 162 |
FollowGear:= Gear |
163 |
end; |
|
164 |
ResetKbd; |
|
351 | 165 |
|
166 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
498 | 167 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, _0, _0, 1); |
351 | 168 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
4 | 169 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
351 | 170 |
ApplyAmmoChanges(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
171 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
|
172 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 173 |
bShowFinger:= true; |
4 | 174 |
TurnTimeLeft:= cHedgehogTurnTime |
175 |
end; |
|
176 |
||
177 |
function AddTeam: PTeam; |
|
351 | 178 |
var Result: PTeam; |
4 | 179 |
begin |
17 | 180 |
New(Result); |
80 | 181 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 182 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 183 |
Result^.AttackBar:= 2; |
184 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
4 | 185 |
if TeamsList = nil then TeamsList:= Result |
186 |
else begin |
|
351 | 187 |
Result^.Next:= TeamsList; |
4 | 188 |
TeamsList:= Result |
189 |
end; |
|
351 | 190 |
CurrentTeam:= Result; |
191 |
AddTeam:= Result |
|
4 | 192 |
end; |
193 |
||
194 |
procedure FreeTeamsList; |
|
195 |
var t, tt: PTeam; |
|
196 |
begin |
|
197 |
tt:= TeamsList; |
|
198 |
TeamsList:= nil; |
|
351 | 199 |
while tt <> nil do |
4 | 200 |
begin |
201 |
t:= tt; |
|
351 | 202 |
tt:= tt^.Next; |
4 | 203 |
Dispose(t) |
204 |
end; |
|
205 |
end; |
|
206 |
||
47 | 207 |
procedure RecountAllTeamsHealth; |
4 | 208 |
var p: PTeam; |
209 |
begin |
|
210 |
p:= TeamsList; |
|
211 |
while p <> nil do |
|
212 |
begin |
|
47 | 213 |
RecountTeamHealth(p); |
351 | 214 |
p:= p^.Next |
47 | 215 |
end |
216 |
end; |
|
217 |
||
218 |
procedure InitTeams; |
|
219 |
var p: PTeam; |
|
371 | 220 |
i: LongInt; |
221 |
th: LongInt; |
|
47 | 222 |
begin |
223 |
p:= TeamsList; |
|
224 |
while p <> nil do |
|
225 |
begin |
|
226 |
th:= 0; |
|
4 | 227 |
for i:= 0 to cMaxHHIndex do |
351 | 228 |
if p^.Hedgehogs[i].Gear <> nil then |
229 |
inc(th, p^.Hedgehogs[i].Gear^.Health); |
|
47 | 230 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
351 | 231 |
p:= p^.Next |
4 | 232 |
end; |
47 | 233 |
RecountAllTeamsHealth |
4 | 234 |
end; |
235 |
||
70 | 236 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 237 |
var s: shortstring; |
238 |
begin |
|
162 | 239 |
TargetPoint.X:= NoPointX; |
240 |
||
70 | 241 |
with Hedgehog do |
47 | 242 |
begin |
351 | 243 |
if Ammo^[CurSlot, CurAmmo].Count = 0 then |
4 | 244 |
begin |
245 |
CurAmmo:= 0; |
|
288 | 246 |
CurSlot:= 0; |
351 | 247 |
while (CurSlot <= cMaxSlotIndex) and (Ammo^[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
4 | 248 |
end; |
249 |
||
351 | 250 |
with Ammo^[CurSlot, CurAmmo] do |
4 | 251 |
begin |
304 | 252 |
CurMinAngle:= Ammoz[AmmoType].minAngle; |
253 |
if Ammoz[AmmoType].maxAngle <> 0 then CurMaxAngle:= Ammoz[AmmoType].maxAngle |
|
254 |
else CurMaxAngle:= cMaxAngle; |
|
255 |
with Hedgehog.Gear^ do |
|
256 |
begin |
|
257 |
if Angle < CurMinAngle then Angle:= CurMinAngle; |
|
258 |
if Angle > CurMaxAngle then Angle:= CurMaxAngle; |
|
259 |
end; |
|
351 | 260 |
|
80 | 261 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 262 |
if Count <> AMMO_INFINITE then |
263 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
264 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 265 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
351 | 266 |
AddCaption(s, Team^.Color, capgrpAmmoinfo); |
4 | 267 |
if (Propz and ammoprop_NeedTarget) <> 0 |
268 |
then begin |
|
351 | 269 |
Gear^.State:= Gear^.State or gstHHChooseTarget; |
4 | 270 |
isCursorVisible:= true |
271 |
end else begin |
|
351 | 272 |
Gear^.State:= Gear^.State and not gstHHChooseTarget; |
4 | 273 |
isCursorVisible:= false |
13 | 274 |
end; |
275 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 276 |
end |
277 |
end |
|
278 |
end; |
|
279 |
||
280 |
function TeamSize(p: PTeam): Longword; |
|
351 | 281 |
var i, Result: Longword; |
4 | 282 |
begin |
283 |
Result:= 0; |
|
284 |
for i:= 0 to cMaxHHIndex do |
|
351 | 285 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
286 |
TeamSize:= Result |
|
4 | 287 |
end; |
288 |
||
47 | 289 |
procedure RecountTeamHealth(team: PTeam); |
371 | 290 |
var i: LongInt; |
47 | 291 |
begin |
292 |
with team^ do |
|
293 |
begin |
|
146 | 294 |
TeamHealthBarWidth:= 0; |
47 | 295 |
for i:= 0 to cMaxHHIndex do |
296 |
if Hedgehogs[i].Gear <> nil then |
|
351 | 297 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
146 | 298 |
TeamHealth:= TeamHealthBarWidth; |
299 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 300 |
begin |
146 | 301 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 302 |
RecountAllTeamsHealth; |
146 | 303 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 304 |
end; |
105 | 305 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
498 | 306 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 307 |
end; |
308 |
||
72 | 309 |
procedure RestoreTeamsFromSave; |
310 |
var p: PTeam; |
|
311 |
begin |
|
312 |
p:= TeamsList; |
|
313 |
while p <> nil do |
|
314 |
begin |
|
351 | 315 |
p^.ExtDriven:= false; |
316 |
p:= p^.Next |
|
72 | 317 |
end; |
318 |
end; |
|
319 |
||
165 | 320 |
procedure SetWeapon(weap: TAmmoType); |
371 | 321 |
var t: LongInt; |
165 | 322 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
323 |
t:= cMaxSlotAmmoIndex; |
165 | 324 |
with CurrentTeam^ do |
325 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 326 |
while (Ammo^[CurSlot, CurAmmo].AmmoType <> weap) and (t >= 0) do |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
327 |
begin |
351 | 328 |
ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot), true); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
329 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
330 |
end |
165 | 331 |
end; |
332 |
||
307 | 333 |
procedure SendStats; |
334 |
var p: PTeam; |
|
371 | 335 |
i: LongInt; |
307 | 336 |
msd: Longword; msdhh: PHedgehog; |
337 |
begin |
|
338 |
msd:= 0; msdhh:= nil; |
|
339 |
p:= TeamsList; |
|
340 |
while p <> nil do |
|
341 |
begin |
|
342 |
for i:= 0 to cMaxHHIndex do |
|
351 | 343 |
if p^.Hedgehogs[i].MaxStepDamage > msd then |
307 | 344 |
begin |
351 | 345 |
msdhh:= @(p^.Hedgehogs[i]); |
346 |
msd:= p^.Hedgehogs[i].MaxStepDamage |
|
307 | 347 |
end; |
351 | 348 |
p:= p^.Next |
307 | 349 |
end; |
351 | 350 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
351 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 352 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
353 |
end; |
|
354 |
||
4 | 355 |
initialization |
356 |
||
357 |
finalization |
|
358 |
||
359 |
FreeTeamsList |
|
360 |
||
361 |
end. |