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