author | unc0rr |
Sun, 24 Sep 2006 18:29:14 +0000 | |
changeset 162 | 4822f6face35 |
parent 146 | 458f4f58c1b6 |
child 165 | 9b9144948668 |
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 |
* |
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*) |
|
33 |
||
34 |
unit uTeams; |
|
35 |
interface |
|
36 |
uses SDLh, uConsts, uKeys, uGears, uRandom; |
|
37 |
{$INCLUDE options.inc} |
|
38 |
type PHedgehog = ^THedgehog; |
|
39 |
PTeam = ^TTeam; |
|
40 |
PHHAmmo = ^THHAmmo; |
|
41 |
THedgehog = record |
|
74 | 42 |
Name: string[MAXNAMELEN]; |
4 | 43 |
Gear: PGear; |
95 | 44 |
NameTag, HealthTag: PSDL_Surface; |
4 | 45 |
Ammo: PHHAmmo; |
46 |
CurSlot, CurAmmo: LongWord; |
|
47 |
AltSlot, AltAmmo: LongWord; |
|
48 |
Team: PTeam; |
|
49 |
AttacksNum: Longword; |
|
50 |
visStepPos: LongWord; |
|
5 | 51 |
BotLevel : LongWord; // 0 - Human player |
4 | 52 |
end; |
10 | 53 |
THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo; |
4 | 54 |
TTeam = record |
55 |
Next: PTeam; |
|
56 |
Color: Cardinal; |
|
74 | 57 |
TeamName: string[MAXNAMELEN]; |
4 | 58 |
ExtDriven: boolean; |
59 |
Aliases: array[0..cKeyMaxIndex] of shortstring; |
|
60 |
Hedgehogs: array[0..cMaxHHIndex] of THedgehog; |
|
61 |
Ammos: array[0..cMaxHHIndex] of THHAmmo; |
|
62 |
CurrHedgehog: integer; |
|
95 | 63 |
NameTag: PSDL_Surface; |
64 |
CrossHairRect, |
|
47 | 65 |
GraveRect, HealthRect: TSDL_Rect; |
4 | 66 |
GraveName: string; |
67 |
FortName: string; |
|
47 | 68 |
TeamHealth: integer; |
146 | 69 |
TeamHealthBarWidth: integer; |
49 | 70 |
DrawHealthY: integer; |
4 | 71 |
AttackBar: LongWord; |
72 |
end; |
|
73 |
||
74 |
var CurrentTeam: PTeam = nil; |
|
75 |
TeamsList: PTeam = nil; |
|
76 |
||
77 |
function AddTeam: PTeam; |
|
70 | 78 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 79 |
procedure SwitchHedgehog; |
80 |
procedure InitTeams; |
|
81 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
75 | 82 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
4 | 83 |
function TeamSize(p: PTeam): Longword; |
47 | 84 |
procedure RecountTeamHealth(team: PTeam); |
72 | 85 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
86 |
function CheckForWin: boolean; |
4 | 87 |
|
88 |
implementation |
|
80 | 89 |
uses uMisc, uStore, uWorld, uIO, uAI, uLocale; |
47 | 90 |
const MaxTeamHealth: integer = 0; |
4 | 91 |
|
92 |
procedure FreeTeamsList; forward; |
|
93 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
94 |
function CheckForWin: boolean; |
83 | 95 |
var team, AliveTeam: PTeam; |
96 |
AliveCount: Longword; |
|
97 |
begin |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
98 |
Result:= false; |
83 | 99 |
AliveCount:= 0; |
100 |
AliveTeam:= nil; |
|
101 |
team:= TeamsList; |
|
102 |
while team <> nil do |
|
103 |
begin |
|
104 |
if team.TeamHealth > 0 then |
|
105 |
begin |
|
106 |
inc(AliveCount); |
|
107 |
AliveTeam:= team |
|
108 |
end; |
|
109 |
team:= team.Next |
|
110 |
end; |
|
111 |
||
112 |
if AliveCount >= 2 then exit; |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
113 |
Result:= true; |
83 | 114 |
|
115 |
TurnTimeLeft:= 0; |
|
116 |
if AliveCount = 0 then |
|
117 |
begin // draw |
|
118 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
119 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
120 |
end else // win |
|
121 |
begin |
|
122 |
AddCaption(Format(trmsg[sidWinner], AliveTeam.TeamName), $FFFFFF, capgrpGameState); |
|
123 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
124 |
end; |
|
125 |
end; |
|
126 |
||
4 | 127 |
procedure SwitchHedgehog; |
128 |
var tteam: PTeam; |
|
129 |
th: integer; |
|
130 |
begin |
|
131 |
FreeActionsList; |
|
132 |
TargetPoint.X:= NoPointX; |
|
89 | 133 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
4 | 134 |
tteam:= CurrentTeam; |
135 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
136 |
if Gear <> nil then Gear.Message:= 0; |
|
137 |
||
138 |
repeat |
|
139 |
CurrentTeam:= CurrentTeam.Next; |
|
140 |
if CurrentTeam = nil then CurrentTeam:= TeamsList; |
|
141 |
th:= CurrentTeam.CurrHedgehog; |
|
142 |
repeat |
|
83 | 143 |
CurrentTeam.CurrHedgehog:= Succ(CurrentTeam.CurrHedgehog) mod (cMaxHHIndex + 1); |
4 | 144 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam.CurrHedgehog = th) |
145 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam = tteam); |
|
146 |
||
83 | 147 |
TryDo(CurrentTeam <> tteam, 'Switch hedgehog: only one team?!', true); |
148 |
||
4 | 149 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
150 |
begin |
|
151 |
AttacksNum:= 0; |
|
152 |
with Gear^ do |
|
153 |
begin |
|
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
154 |
State:= gstHHDriven; |
4 | 155 |
Active:= true |
156 |
end; |
|
157 |
FollowGear:= Gear |
|
158 |
end; |
|
159 |
ResetKbd; |
|
160 |
cWindSpeed:= (GetRandom * 2 - 1) * cMaxWindSpeed; |
|
83 | 161 |
AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1).Tag:= round(72 * cWindSpeed / cMaxWindSpeed); |
4 | 162 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
70 | 163 |
ApplyAmmoChanges(CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]); |
4 | 164 |
TurnTimeLeft:= cHedgehogTurnTime |
165 |
end; |
|
166 |
||
167 |
function AddTeam: PTeam; |
|
168 |
begin |
|
17 | 169 |
New(Result); |
80 | 170 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 171 |
FillChar(Result^, sizeof(TTeam), 0); |
74 | 172 |
Result.AttackBar:= 2; |
83 | 173 |
Result.CurrHedgehog:= cMaxHHIndex; |
4 | 174 |
if TeamsList = nil then TeamsList:= Result |
175 |
else begin |
|
176 |
Result.Next:= TeamsList; |
|
177 |
TeamsList:= Result |
|
178 |
end; |
|
179 |
CurrentTeam:= Result |
|
180 |
end; |
|
181 |
||
182 |
procedure FreeTeamsList; |
|
183 |
var t, tt: PTeam; |
|
184 |
begin |
|
185 |
tt:= TeamsList; |
|
186 |
TeamsList:= nil; |
|
187 |
while tt<>nil do |
|
188 |
begin |
|
189 |
t:= tt; |
|
190 |
tt:= tt.Next; |
|
191 |
Dispose(t) |
|
192 |
end; |
|
193 |
end; |
|
194 |
||
10 | 195 |
procedure FillAmmoGroup(Ammo: PHHAmmo); |
196 |
var mi: array[0..cMaxSlotIndex] of byte; |
|
197 |
a: TAmmoType; |
|
198 |
begin |
|
199 |
FillChar(mi, sizeof(mi), 0); |
|
200 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
|
201 |
begin |
|
202 |
TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true); |
|
203 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo; |
|
204 |
inc(mi[Ammoz[a].Slot]) |
|
205 |
end; |
|
206 |
end; |
|
207 |
||
47 | 208 |
procedure RecountAllTeamsHealth; |
4 | 209 |
var p: PTeam; |
210 |
begin |
|
211 |
p:= TeamsList; |
|
212 |
while p <> nil do |
|
213 |
begin |
|
47 | 214 |
RecountTeamHealth(p); |
215 |
p:= p.Next |
|
216 |
end |
|
217 |
end; |
|
218 |
||
219 |
procedure InitTeams; |
|
220 |
var p: PTeam; |
|
221 |
i: integer; |
|
222 |
th: integer; |
|
223 |
begin |
|
224 |
p:= TeamsList; |
|
225 |
while p <> nil do |
|
226 |
begin |
|
227 |
th:= 0; |
|
10 | 228 |
FillAmmoGroup(@p.Ammos[0]); |
4 | 229 |
for i:= 0 to cMaxHHIndex do |
230 |
if p.Hedgehogs[i].Gear <> nil then |
|
231 |
begin |
|
232 |
p.Hedgehogs[i].Gear.Health:= 100; |
|
47 | 233 |
inc(th, 100); |
10 | 234 |
p.Hedgehogs[i].Ammo:= @p.Ammos[0] // 0 means all hedgehogs |
235 |
// will have common set of ammo |
|
4 | 236 |
end; |
47 | 237 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 238 |
p:= p.Next |
239 |
end; |
|
47 | 240 |
RecountAllTeamsHealth |
4 | 241 |
end; |
242 |
||
70 | 243 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 244 |
var s: shortstring; |
245 |
begin |
|
162 | 246 |
TargetPoint.X:= NoPointX; |
247 |
||
70 | 248 |
with Hedgehog do |
47 | 249 |
begin |
4 | 250 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
251 |
begin |
|
252 |
CurAmmo:= 0; |
|
10 | 253 |
while (CurAmmo <= cMaxSlotAmmoIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo) |
4 | 254 |
end; |
255 |
||
256 |
with Ammo[CurSlot, CurAmmo] do |
|
257 |
begin |
|
80 | 258 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 259 |
if Count <> AMMO_INFINITE then |
260 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
261 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 262 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
4 | 263 |
AddCaption(s, Team.Color, capgrpAmmoinfo); |
264 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
265 |
then begin |
|
266 |
Gear.State:= Gear.State or gstHHChooseTarget; |
|
267 |
isCursorVisible:= true |
|
268 |
end else begin |
|
269 |
Gear.State:= Gear.State and not gstHHChooseTarget; |
|
270 |
isCursorVisible:= false |
|
13 | 271 |
end; |
272 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 273 |
end |
274 |
end |
|
275 |
end; |
|
276 |
||
277 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer); |
|
278 |
var ami: integer; |
|
279 |
b: boolean; |
|
280 |
begin |
|
281 |
repeat |
|
282 |
b:= false; |
|
283 |
ami:= 0; |
|
10 | 284 |
while (not b) and (ami < cMaxSlotAmmoIndex) do |
70 | 285 |
if (Ammo[Slot, ami].Count = 0) |
286 |
and (Ammo[Slot, ami + 1].Count > 0) then b:= true |
|
4 | 287 |
else inc(ami); |
79 | 288 |
if b then // there's a free item in ammo stack |
70 | 289 |
Ammo[Slot, ami]:= Ammo[Slot, ami + 1] |
4 | 290 |
until not b; |
291 |
end; |
|
292 |
||
293 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
294 |
var s, a: Longword; |
|
295 |
begin |
|
296 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
297 |
begin |
|
298 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end |
|
299 |
else begin s:= AltSlot; a:= AltAmmo end; |
|
300 |
with Ammo[s, a] do |
|
301 |
if Count <> AMMO_INFINITE then |
|
302 |
begin |
|
303 |
dec(Count); |
|
304 |
if Count = 0 then PackAmmo(Ammo, CurSlot) |
|
305 |
end |
|
306 |
end |
|
307 |
end; |
|
308 |
||
75 | 309 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
310 |
var slot, ami: integer; |
|
311 |
begin |
|
312 |
Slot:= Ammoz[Ammo].Slot; |
|
313 |
ami:= 0; |
|
314 |
Result:= false; |
|
315 |
while (not Result) and (ami <= cMaxSlotAmmoIndex) do |
|
316 |
begin |
|
317 |
with Hedgehog.Ammo[Slot, ami] do |
|
318 |
if (AmmoType = Ammo) and (Count > 0) then Result:= true; |
|
319 |
inc(ami) |
|
320 |
end |
|
321 |
end; |
|
322 |
||
4 | 323 |
function TeamSize(p: PTeam): Longword; |
324 |
var i: Longword; |
|
325 |
begin |
|
326 |
Result:= 0; |
|
327 |
for i:= 0 to cMaxHHIndex do |
|
328 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
329 |
end; |
|
330 |
||
47 | 331 |
procedure RecountTeamHealth(team: PTeam); |
332 |
var i: integer; |
|
333 |
begin |
|
334 |
with team^ do |
|
335 |
begin |
|
146 | 336 |
TeamHealthBarWidth:= 0; |
47 | 337 |
for i:= 0 to cMaxHHIndex do |
338 |
if Hedgehogs[i].Gear <> nil then |
|
146 | 339 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear.Health); |
340 |
TeamHealth:= TeamHealthBarWidth; |
|
341 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 342 |
begin |
146 | 343 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 344 |
RecountAllTeamsHealth; |
146 | 345 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 346 |
end; |
105 | 347 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
49 | 348 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
47 | 349 |
end; |
350 |
||
72 | 351 |
procedure RestoreTeamsFromSave; |
352 |
var p: PTeam; |
|
353 |
begin |
|
354 |
p:= TeamsList; |
|
355 |
while p <> nil do |
|
356 |
begin |
|
357 |
p.ExtDriven:= false; |
|
358 |
p:= p.Next |
|
359 |
end; |
|
360 |
end; |
|
361 |
||
4 | 362 |
initialization |
363 |
||
364 |
finalization |
|
365 |
||
366 |
FreeTeamsList |
|
367 |
||
368 |
end. |