author | unc0rr |
Wed, 27 Sep 2006 20:52:22 +0000 | |
changeset 175 | d226d976d836 |
parent 167 | 805fa9a27e9e |
child 176 | 533d03041dcd |
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; |
167 | 59 |
Binds: TBinds; |
4 | 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]); |
167 | 165 |
if CurrentTeam.ExtDriven then SetDefaultBinds |
166 |
else SetBinds(CurrentTeam.Binds); |
|
4 | 167 |
TurnTimeLeft:= cHedgehogTurnTime |
168 |
end; |
|
169 |
||
170 |
function AddTeam: PTeam; |
|
171 |
begin |
|
17 | 172 |
New(Result); |
80 | 173 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 174 |
FillChar(Result^, sizeof(TTeam), 0); |
74 | 175 |
Result.AttackBar:= 2; |
83 | 176 |
Result.CurrHedgehog:= cMaxHHIndex; |
4 | 177 |
if TeamsList = nil then TeamsList:= Result |
178 |
else begin |
|
179 |
Result.Next:= TeamsList; |
|
180 |
TeamsList:= Result |
|
181 |
end; |
|
182 |
CurrentTeam:= Result |
|
183 |
end; |
|
184 |
||
185 |
procedure FreeTeamsList; |
|
186 |
var t, tt: PTeam; |
|
187 |
begin |
|
188 |
tt:= TeamsList; |
|
189 |
TeamsList:= nil; |
|
190 |
while tt<>nil do |
|
191 |
begin |
|
192 |
t:= tt; |
|
193 |
tt:= tt.Next; |
|
194 |
Dispose(t) |
|
195 |
end; |
|
196 |
end; |
|
197 |
||
10 | 198 |
procedure FillAmmoGroup(Ammo: PHHAmmo); |
199 |
var mi: array[0..cMaxSlotIndex] of byte; |
|
200 |
a: TAmmoType; |
|
201 |
begin |
|
202 |
FillChar(mi, sizeof(mi), 0); |
|
203 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
|
204 |
begin |
|
205 |
TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true); |
|
206 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo; |
|
207 |
inc(mi[Ammoz[a].Slot]) |
|
208 |
end; |
|
209 |
end; |
|
210 |
||
47 | 211 |
procedure RecountAllTeamsHealth; |
4 | 212 |
var p: PTeam; |
213 |
begin |
|
214 |
p:= TeamsList; |
|
215 |
while p <> nil do |
|
216 |
begin |
|
47 | 217 |
RecountTeamHealth(p); |
218 |
p:= p.Next |
|
219 |
end |
|
220 |
end; |
|
221 |
||
222 |
procedure InitTeams; |
|
223 |
var p: PTeam; |
|
224 |
i: integer; |
|
225 |
th: integer; |
|
226 |
begin |
|
227 |
p:= TeamsList; |
|
228 |
while p <> nil do |
|
229 |
begin |
|
230 |
th:= 0; |
|
10 | 231 |
FillAmmoGroup(@p.Ammos[0]); |
4 | 232 |
for i:= 0 to cMaxHHIndex do |
233 |
if p.Hedgehogs[i].Gear <> nil then |
|
234 |
begin |
|
235 |
p.Hedgehogs[i].Gear.Health:= 100; |
|
47 | 236 |
inc(th, 100); |
10 | 237 |
p.Hedgehogs[i].Ammo:= @p.Ammos[0] // 0 means all hedgehogs |
238 |
// will have common set of ammo |
|
4 | 239 |
end; |
47 | 240 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 241 |
p:= p.Next |
242 |
end; |
|
47 | 243 |
RecountAllTeamsHealth |
4 | 244 |
end; |
245 |
||
70 | 246 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 247 |
var s: shortstring; |
248 |
begin |
|
162 | 249 |
TargetPoint.X:= NoPointX; |
250 |
||
70 | 251 |
with Hedgehog do |
47 | 252 |
begin |
4 | 253 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
254 |
begin |
|
255 |
CurAmmo:= 0; |
|
10 | 256 |
while (CurAmmo <= cMaxSlotAmmoIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo) |
4 | 257 |
end; |
258 |
||
259 |
with Ammo[CurSlot, CurAmmo] do |
|
260 |
begin |
|
80 | 261 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 262 |
if Count <> AMMO_INFINITE then |
263 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
264 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 265 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
4 | 266 |
AddCaption(s, Team.Color, capgrpAmmoinfo); |
267 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
268 |
then begin |
|
269 |
Gear.State:= Gear.State or gstHHChooseTarget; |
|
270 |
isCursorVisible:= true |
|
271 |
end else begin |
|
272 |
Gear.State:= Gear.State and not gstHHChooseTarget; |
|
273 |
isCursorVisible:= false |
|
13 | 274 |
end; |
275 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 276 |
end |
277 |
end |
|
278 |
end; |
|
279 |
||
280 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer); |
|
281 |
var ami: integer; |
|
282 |
b: boolean; |
|
283 |
begin |
|
284 |
repeat |
|
285 |
b:= false; |
|
286 |
ami:= 0; |
|
10 | 287 |
while (not b) and (ami < cMaxSlotAmmoIndex) do |
70 | 288 |
if (Ammo[Slot, ami].Count = 0) |
289 |
and (Ammo[Slot, ami + 1].Count > 0) then b:= true |
|
4 | 290 |
else inc(ami); |
79 | 291 |
if b then // there's a free item in ammo stack |
70 | 292 |
Ammo[Slot, ami]:= Ammo[Slot, ami + 1] |
4 | 293 |
until not b; |
294 |
end; |
|
295 |
||
296 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
297 |
var s, a: Longword; |
|
298 |
begin |
|
299 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
300 |
begin |
|
301 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end |
|
302 |
else begin s:= AltSlot; a:= AltAmmo end; |
|
303 |
with Ammo[s, a] do |
|
304 |
if Count <> AMMO_INFINITE then |
|
305 |
begin |
|
306 |
dec(Count); |
|
307 |
if Count = 0 then PackAmmo(Ammo, CurSlot) |
|
308 |
end |
|
309 |
end |
|
310 |
end; |
|
311 |
||
75 | 312 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
313 |
var slot, ami: integer; |
|
314 |
begin |
|
315 |
Slot:= Ammoz[Ammo].Slot; |
|
316 |
ami:= 0; |
|
317 |
Result:= false; |
|
318 |
while (not Result) and (ami <= cMaxSlotAmmoIndex) do |
|
319 |
begin |
|
320 |
with Hedgehog.Ammo[Slot, ami] do |
|
321 |
if (AmmoType = Ammo) and (Count > 0) then Result:= true; |
|
322 |
inc(ami) |
|
323 |
end |
|
324 |
end; |
|
325 |
||
4 | 326 |
function TeamSize(p: PTeam): Longword; |
327 |
var i: Longword; |
|
328 |
begin |
|
329 |
Result:= 0; |
|
330 |
for i:= 0 to cMaxHHIndex do |
|
331 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
332 |
end; |
|
333 |
||
47 | 334 |
procedure RecountTeamHealth(team: PTeam); |
335 |
var i: integer; |
|
336 |
begin |
|
337 |
with team^ do |
|
338 |
begin |
|
146 | 339 |
TeamHealthBarWidth:= 0; |
47 | 340 |
for i:= 0 to cMaxHHIndex do |
341 |
if Hedgehogs[i].Gear <> nil then |
|
146 | 342 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear.Health); |
343 |
TeamHealth:= TeamHealthBarWidth; |
|
344 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 345 |
begin |
146 | 346 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 347 |
RecountAllTeamsHealth; |
146 | 348 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 349 |
end; |
105 | 350 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
49 | 351 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
47 | 352 |
end; |
353 |
||
72 | 354 |
procedure RestoreTeamsFromSave; |
355 |
var p: PTeam; |
|
356 |
begin |
|
357 |
p:= TeamsList; |
|
358 |
while p <> nil do |
|
359 |
begin |
|
360 |
p.ExtDriven:= false; |
|
361 |
p:= p.Next |
|
362 |
end; |
|
363 |
end; |
|
364 |
||
165 | 365 |
procedure SetWeapon(weap: TAmmoType); |
366 |
begin |
|
367 |
with CurrentTeam^ do |
|
368 |
with Hedgehogs[CurrHedgehog] do |
|
369 |
while Ammo[CurSlot, CurAmmo].AmmoType <> weap do |
|
370 |
ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot)); |
|
371 |
end; |
|
372 |
||
4 | 373 |
initialization |
374 |
||
375 |
finalization |
|
376 |
||
377 |
FreeTeamsList |
|
378 |
||
379 |
end. |