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