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