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