author | unc0rr |
Tue, 11 Jul 2006 21:09:06 +0000 | |
changeset 76 | 4e50c2bdeefc |
parent 75 | d2b737858ff7 |
child 79 | 29b477319854 |
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); |
267 |
if b then // есть пустое место |
|
268 |
begin |
|
70 | 269 |
Ammo[Slot, ami]:= Ammo[Slot, ami + 1] |
4 | 270 |
end |
271 |
until not b; |
|
272 |
end; |
|
273 |
||
274 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
275 |
var s, a: Longword; |
|
276 |
begin |
|
277 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
278 |
begin |
|
279 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end |
|
280 |
else begin s:= AltSlot; a:= AltAmmo end; |
|
281 |
with Ammo[s, a] do |
|
282 |
if Count <> AMMO_INFINITE then |
|
283 |
begin |
|
284 |
dec(Count); |
|
285 |
if Count = 0 then PackAmmo(Ammo, CurSlot) |
|
286 |
end |
|
287 |
end |
|
288 |
end; |
|
289 |
||
75 | 290 |
function HHHasAmmo(Hedgehog: PHedgehog; Ammo: TAmmoType): boolean; |
291 |
var slot, ami: integer; |
|
292 |
begin |
|
293 |
Slot:= Ammoz[Ammo].Slot; |
|
294 |
ami:= 0; |
|
295 |
Result:= false; |
|
296 |
while (not Result) and (ami <= cMaxSlotAmmoIndex) do |
|
297 |
begin |
|
298 |
with Hedgehog.Ammo[Slot, ami] do |
|
299 |
if (AmmoType = Ammo) and (Count > 0) then Result:= true; |
|
300 |
inc(ami) |
|
301 |
end |
|
302 |
end; |
|
303 |
||
4 | 304 |
function TeamSize(p: PTeam): Longword; |
305 |
var i: Longword; |
|
306 |
begin |
|
307 |
Result:= 0; |
|
308 |
for i:= 0 to cMaxHHIndex do |
|
309 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
310 |
end; |
|
311 |
||
47 | 312 |
procedure RecountTeamHealth(team: PTeam); |
313 |
var i: integer; |
|
314 |
begin |
|
315 |
with team^ do |
|
316 |
begin |
|
317 |
TeamHealth:= 0; |
|
318 |
for i:= 0 to cMaxHHIndex do |
|
319 |
if Hedgehogs[i].Gear <> nil then |
|
320 |
inc(TeamHealth, Hedgehogs[i].Gear.Health); |
|
321 |
if TeamHealth > MaxTeamHealth then |
|
322 |
begin |
|
323 |
MaxTeamHealth:= TeamHealth; |
|
324 |
RecountAllTeamsHealth; |
|
325 |
end else TeamHealth:= (TeamHealth * cTeamHealthWidth) div MaxTeamHealth |
|
49 | 326 |
end; |
327 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
|
47 | 328 |
end; |
329 |
||
72 | 330 |
procedure RestoreTeamsFromSave; |
331 |
var p: PTeam; |
|
332 |
begin |
|
333 |
p:= TeamsList; |
|
334 |
while p <> nil do |
|
335 |
begin |
|
336 |
p.ExtDriven:= false; |
|
337 |
p:= p.Next |
|
338 |
end; |
|
339 |
end; |
|
340 |
||
4 | 341 |
initialization |
342 |
||
343 |
finalization |
|
344 |
||
345 |
FreeTeamsList |
|
346 |
||
347 |
end. |