author | unc0rr |
Thu, 21 Sep 2006 17:57:35 +0000 | |
changeset 157 | 8e3e11bb0a5f |
parent 146 | 458f4f58c1b6 |
child 162 | 4822f6face35 |
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 |
|
70 | 246 |
with Hedgehog do |
47 | 247 |
begin |
4 | 248 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
249 |
begin |
|
250 |
CurAmmo:= 0; |
|
10 | 251 |
while (CurAmmo <= cMaxSlotAmmoIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo) |
4 | 252 |
end; |
253 |
||
254 |
with Ammo[CurSlot, CurAmmo] do |
|
255 |
begin |
|
80 | 256 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 257 |
if Count <> AMMO_INFINITE then |
258 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
259 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 260 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
4 | 261 |
AddCaption(s, Team.Color, capgrpAmmoinfo); |
262 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
263 |
then begin |
|
264 |
Gear.State:= Gear.State or gstHHChooseTarget; |
|
265 |
isCursorVisible:= true |
|
266 |
end else begin |
|
267 |
Gear.State:= Gear.State and not gstHHChooseTarget; |
|
268 |
isCursorVisible:= false |
|
13 | 269 |
end; |
270 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 271 |
end |
272 |
end |
|
273 |
end; |
|
274 |
||
275 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer); |
|
276 |
var ami: integer; |
|
277 |
b: boolean; |
|
278 |
begin |
|
279 |
repeat |
|
280 |
b:= false; |
|
281 |
ami:= 0; |
|
10 | 282 |
while (not b) and (ami < cMaxSlotAmmoIndex) do |
70 | 283 |
if (Ammo[Slot, ami].Count = 0) |
284 |
and (Ammo[Slot, ami + 1].Count > 0) then b:= true |
|
4 | 285 |
else inc(ami); |
79 | 286 |
if b then // there's a free item in ammo stack |
70 | 287 |
Ammo[Slot, ami]:= Ammo[Slot, ami + 1] |
4 | 288 |
until not b; |
289 |
end; |
|
290 |
||
291 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
292 |
var s, a: Longword; |
|
293 |
begin |
|
294 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
295 |
begin |
|
296 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end |
|
297 |
else begin s:= AltSlot; a:= AltAmmo end; |
|
298 |
with Ammo[s, a] do |
|
299 |
if Count <> AMMO_INFINITE then |
|
300 |
begin |
|
301 |
dec(Count); |
|
302 |
if Count = 0 then PackAmmo(Ammo, CurSlot) |
|
303 |
end |
|
304 |
end |
|
305 |
end; |
|
306 |
||
75 | 307 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
308 |
var slot, ami: integer; |
|
309 |
begin |
|
310 |
Slot:= Ammoz[Ammo].Slot; |
|
311 |
ami:= 0; |
|
312 |
Result:= false; |
|
313 |
while (not Result) and (ami <= cMaxSlotAmmoIndex) do |
|
314 |
begin |
|
315 |
with Hedgehog.Ammo[Slot, ami] do |
|
316 |
if (AmmoType = Ammo) and (Count > 0) then Result:= true; |
|
317 |
inc(ami) |
|
318 |
end |
|
319 |
end; |
|
320 |
||
4 | 321 |
function TeamSize(p: PTeam): Longword; |
322 |
var i: Longword; |
|
323 |
begin |
|
324 |
Result:= 0; |
|
325 |
for i:= 0 to cMaxHHIndex do |
|
326 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
327 |
end; |
|
328 |
||
47 | 329 |
procedure RecountTeamHealth(team: PTeam); |
330 |
var i: integer; |
|
331 |
begin |
|
332 |
with team^ do |
|
333 |
begin |
|
146 | 334 |
TeamHealthBarWidth:= 0; |
47 | 335 |
for i:= 0 to cMaxHHIndex do |
336 |
if Hedgehogs[i].Gear <> nil then |
|
146 | 337 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear.Health); |
338 |
TeamHealth:= TeamHealthBarWidth; |
|
339 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 340 |
begin |
146 | 341 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 342 |
RecountAllTeamsHealth; |
146 | 343 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 344 |
end; |
105 | 345 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
49 | 346 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
47 | 347 |
end; |
348 |
||
72 | 349 |
procedure RestoreTeamsFromSave; |
350 |
var p: PTeam; |
|
351 |
begin |
|
352 |
p:= TeamsList; |
|
353 |
while p <> nil do |
|
354 |
begin |
|
355 |
p.ExtDriven:= false; |
|
356 |
p:= p.Next |
|
357 |
end; |
|
358 |
end; |
|
359 |
||
4 | 360 |
initialization |
361 |
||
362 |
finalization |
|
363 |
||
364 |
FreeTeamsList |
|
365 |
||
366 |
end. |