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