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