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