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