author | unc0rr |
Mon, 24 Jul 2006 14:03:51 +0000 | |
changeset 83 | 207c85fbef51 |
parent 81 | d74e0e914b50 |
child 89 | f9db56409a86 |
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; |
44 |
NameRect, HealthRect, HealthTagRect: TSDL_Rect; |
|
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; |
|
47 | 63 |
NameRect, CrossHairRect, |
64 |
GraveRect, HealthRect: TSDL_Rect; |
|
4 | 65 |
GraveName: string; |
66 |
FortName: string; |
|
47 | 67 |
TeamHealth: integer; |
49 | 68 |
DrawHealthY: integer; |
4 | 69 |
AttackBar: LongWord; |
70 |
end; |
|
71 |
||
72 |
var CurrentTeam: PTeam = nil; |
|
73 |
TeamsList: PTeam = nil; |
|
74 |
||
75 |
function AddTeam: PTeam; |
|
70 | 76 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 77 |
procedure SwitchHedgehog; |
78 |
procedure InitTeams; |
|
79 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
75 | 80 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
4 | 81 |
function TeamSize(p: PTeam): Longword; |
47 | 82 |
procedure RecountTeamHealth(team: PTeam); |
72 | 83 |
procedure RestoreTeamsFromSave; |
83 | 84 |
procedure CheckForWin; |
4 | 85 |
|
86 |
implementation |
|
80 | 87 |
uses uMisc, uStore, uWorld, uIO, uAI, uLocale; |
47 | 88 |
const MaxTeamHealth: integer = 0; |
4 | 89 |
|
90 |
procedure FreeTeamsList; forward; |
|
91 |
||
83 | 92 |
procedure CheckForWin; |
93 |
var team, AliveTeam: PTeam; |
|
94 |
AliveCount: Longword; |
|
95 |
begin |
|
96 |
AliveCount:= 0; |
|
97 |
AliveTeam:= nil; |
|
98 |
team:= TeamsList; |
|
99 |
while team <> nil do |
|
100 |
begin |
|
101 |
if team.TeamHealth > 0 then |
|
102 |
begin |
|
103 |
inc(AliveCount); |
|
104 |
AliveTeam:= team |
|
105 |
end; |
|
106 |
team:= team.Next |
|
107 |
end; |
|
108 |
||
109 |
if AliveCount >= 2 then exit; |
|
110 |
||
111 |
TurnTimeLeft:= 0; |
|
112 |
if AliveCount = 0 then |
|
113 |
begin // draw |
|
114 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
115 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
116 |
end else // win |
|
117 |
begin |
|
118 |
AddCaption(Format(trmsg[sidWinner], AliveTeam.TeamName), $FFFFFF, capgrpGameState); |
|
119 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
120 |
end; |
|
121 |
end; |
|
122 |
||
4 | 123 |
procedure SwitchHedgehog; |
124 |
var tteam: PTeam; |
|
125 |
th: integer; |
|
126 |
begin |
|
127 |
FreeActionsList; |
|
128 |
TargetPoint.X:= NoPointX; |
|
129 |
if CurrentTeam = nil then OutError('nil Team', true); |
|
130 |
tteam:= CurrentTeam; |
|
131 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
132 |
if Gear <> nil then Gear.Message:= 0; |
|
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 |
|
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
150 |
State:= gstHHDriven; |
4 | 151 |
Active:= true |
152 |
end; |
|
153 |
FollowGear:= Gear |
|
154 |
end; |
|
155 |
ResetKbd; |
|
156 |
cWindSpeed:= (GetRandom * 2 - 1) * cMaxWindSpeed; |
|
83 | 157 |
AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1).Tag:= round(72 * cWindSpeed / cMaxWindSpeed); |
4 | 158 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
70 | 159 |
ApplyAmmoChanges(CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]); |
4 | 160 |
TurnTimeLeft:= cHedgehogTurnTime |
161 |
end; |
|
162 |
||
163 |
function AddTeam: PTeam; |
|
164 |
begin |
|
17 | 165 |
New(Result); |
80 | 166 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 167 |
FillChar(Result^, sizeof(TTeam), 0); |
74 | 168 |
Result.AttackBar:= 2; |
83 | 169 |
Result.CurrHedgehog:= cMaxHHIndex; |
4 | 170 |
if TeamsList = nil then TeamsList:= Result |
171 |
else begin |
|
172 |
Result.Next:= TeamsList; |
|
173 |
TeamsList:= Result |
|
174 |
end; |
|
175 |
CurrentTeam:= Result |
|
176 |
end; |
|
177 |
||
178 |
procedure FreeTeamsList; |
|
179 |
var t, tt: PTeam; |
|
180 |
begin |
|
181 |
tt:= TeamsList; |
|
182 |
TeamsList:= nil; |
|
183 |
while tt<>nil do |
|
184 |
begin |
|
185 |
t:= tt; |
|
186 |
tt:= tt.Next; |
|
187 |
Dispose(t) |
|
188 |
end; |
|
189 |
end; |
|
190 |
||
10 | 191 |
procedure FillAmmoGroup(Ammo: PHHAmmo); |
192 |
var mi: array[0..cMaxSlotIndex] of byte; |
|
193 |
a: TAmmoType; |
|
194 |
begin |
|
195 |
FillChar(mi, sizeof(mi), 0); |
|
196 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
|
197 |
begin |
|
198 |
TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true); |
|
199 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo; |
|
200 |
inc(mi[Ammoz[a].Slot]) |
|
201 |
end; |
|
202 |
end; |
|
203 |
||
47 | 204 |
procedure RecountAllTeamsHealth; |
4 | 205 |
var p: PTeam; |
206 |
begin |
|
207 |
p:= TeamsList; |
|
208 |
while p <> nil do |
|
209 |
begin |
|
47 | 210 |
RecountTeamHealth(p); |
211 |
p:= p.Next |
|
212 |
end |
|
213 |
end; |
|
214 |
||
215 |
procedure InitTeams; |
|
216 |
var p: PTeam; |
|
217 |
i: integer; |
|
218 |
th: integer; |
|
219 |
begin |
|
220 |
p:= TeamsList; |
|
221 |
while p <> nil do |
|
222 |
begin |
|
223 |
th:= 0; |
|
10 | 224 |
FillAmmoGroup(@p.Ammos[0]); |
4 | 225 |
for i:= 0 to cMaxHHIndex do |
226 |
if p.Hedgehogs[i].Gear <> nil then |
|
227 |
begin |
|
228 |
p.Hedgehogs[i].Gear.Health:= 100; |
|
47 | 229 |
inc(th, 100); |
10 | 230 |
p.Hedgehogs[i].Ammo:= @p.Ammos[0] // 0 means all hedgehogs |
231 |
// will have common set of ammo |
|
4 | 232 |
end; |
47 | 233 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 234 |
p:= p.Next |
235 |
end; |
|
47 | 236 |
RecountAllTeamsHealth |
4 | 237 |
end; |
238 |
||
70 | 239 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 240 |
var s: shortstring; |
241 |
begin |
|
70 | 242 |
with Hedgehog do |
47 | 243 |
begin |
4 | 244 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
245 |
begin |
|
246 |
CurAmmo:= 0; |
|
10 | 247 |
while (CurAmmo <= cMaxSlotAmmoIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo) |
4 | 248 |
end; |
249 |
||
250 |
with Ammo[CurSlot, CurAmmo] do |
|
251 |
begin |
|
80 | 252 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 253 |
if Count <> AMMO_INFINITE then |
254 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
255 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 256 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
4 | 257 |
AddCaption(s, Team.Color, capgrpAmmoinfo); |
258 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
259 |
then begin |
|
260 |
Gear.State:= Gear.State or gstHHChooseTarget; |
|
261 |
isCursorVisible:= true |
|
262 |
end else begin |
|
263 |
Gear.State:= Gear.State and not gstHHChooseTarget; |
|
264 |
isCursorVisible:= false |
|
13 | 265 |
end; |
266 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 267 |
end |
268 |
end |
|
269 |
end; |
|
270 |
||
271 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer); |
|
272 |
var ami: integer; |
|
273 |
b: boolean; |
|
274 |
begin |
|
275 |
repeat |
|
276 |
b:= false; |
|
277 |
ami:= 0; |
|
10 | 278 |
while (not b) and (ami < cMaxSlotAmmoIndex) do |
70 | 279 |
if (Ammo[Slot, ami].Count = 0) |
280 |
and (Ammo[Slot, ami + 1].Count > 0) then b:= true |
|
4 | 281 |
else inc(ami); |
79 | 282 |
if b then // there's a free item in ammo stack |
70 | 283 |
Ammo[Slot, ami]:= Ammo[Slot, ami + 1] |
4 | 284 |
until not b; |
285 |
end; |
|
286 |
||
287 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
288 |
var s, a: Longword; |
|
289 |
begin |
|
290 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
291 |
begin |
|
292 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end |
|
293 |
else begin s:= AltSlot; a:= AltAmmo end; |
|
294 |
with Ammo[s, a] do |
|
295 |
if Count <> AMMO_INFINITE then |
|
296 |
begin |
|
297 |
dec(Count); |
|
298 |
if Count = 0 then PackAmmo(Ammo, CurSlot) |
|
299 |
end |
|
300 |
end |
|
301 |
end; |
|
302 |
||
75 | 303 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
304 |
var slot, ami: integer; |
|
305 |
begin |
|
306 |
Slot:= Ammoz[Ammo].Slot; |
|
307 |
ami:= 0; |
|
308 |
Result:= false; |
|
309 |
while (not Result) and (ami <= cMaxSlotAmmoIndex) do |
|
310 |
begin |
|
311 |
with Hedgehog.Ammo[Slot, ami] do |
|
312 |
if (AmmoType = Ammo) and (Count > 0) then Result:= true; |
|
313 |
inc(ami) |
|
314 |
end |
|
315 |
end; |
|
316 |
||
4 | 317 |
function TeamSize(p: PTeam): Longword; |
318 |
var i: Longword; |
|
319 |
begin |
|
320 |
Result:= 0; |
|
321 |
for i:= 0 to cMaxHHIndex do |
|
322 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
323 |
end; |
|
324 |
||
47 | 325 |
procedure RecountTeamHealth(team: PTeam); |
326 |
var i: integer; |
|
327 |
begin |
|
328 |
with team^ do |
|
329 |
begin |
|
330 |
TeamHealth:= 0; |
|
331 |
for i:= 0 to cMaxHHIndex do |
|
332 |
if Hedgehogs[i].Gear <> nil then |
|
333 |
inc(TeamHealth, Hedgehogs[i].Gear.Health); |
|
334 |
if TeamHealth > MaxTeamHealth then |
|
335 |
begin |
|
336 |
MaxTeamHealth:= TeamHealth; |
|
337 |
RecountAllTeamsHealth; |
|
338 |
end else TeamHealth:= (TeamHealth * cTeamHealthWidth) div MaxTeamHealth |
|
49 | 339 |
end; |
340 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
|
47 | 341 |
end; |
342 |
||
72 | 343 |
procedure RestoreTeamsFromSave; |
344 |
var p: PTeam; |
|
345 |
begin |
|
346 |
p:= TeamsList; |
|
347 |
while p <> nil do |
|
348 |
begin |
|
349 |
p.ExtDriven:= false; |
|
350 |
p:= p.Next |
|
351 |
end; |
|
352 |
end; |
|
353 |
||
4 | 354 |
initialization |
355 |
||
356 |
finalization |
|
357 |
||
358 |
FreeTeamsList |
|
359 |
||
360 |
end. |