author | unc0rr |
Sun, 15 Jan 2006 14:14:12 +0000 | |
changeset 49 | 3afe33c1cf06 |
parent 47 | 8daf1ee0b9a3 |
child 57 | e1a77ae57065 |
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; |
|
76 |
procedure ApplyAmmoChanges(Hedgehog: PHedgehog); |
|
77 |
procedure SwitchHedgehog; |
|
78 |
procedure InitTeams; |
|
79 |
procedure OnUsedAmmo(Ammo: PHHAmmo); |
|
80 |
function TeamSize(p: PTeam): Longword; |
|
47 | 81 |
procedure RecountTeamHealth(team: PTeam); |
4 | 82 |
|
83 |
implementation |
|
84 |
uses uMisc, uStore, uWorld, uIO, uAIActions; |
|
47 | 85 |
const MaxTeamHealth: integer = 0; |
4 | 86 |
|
87 |
procedure FreeTeamsList; forward; |
|
88 |
||
89 |
procedure SwitchHedgehog; |
|
90 |
var tteam: PTeam; |
|
91 |
th: integer; |
|
92 |
begin |
|
93 |
FreeActionsList; |
|
94 |
TargetPoint.X:= NoPointX; |
|
95 |
if CurrentTeam = nil then OutError('nil Team', true); |
|
96 |
tteam:= CurrentTeam; |
|
97 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
98 |
if Gear <> nil then Gear.Message:= 0; |
|
99 |
||
100 |
repeat |
|
101 |
CurrentTeam:= CurrentTeam.Next; |
|
102 |
if CurrentTeam = nil then CurrentTeam:= TeamsList; |
|
103 |
th:= CurrentTeam.CurrHedgehog; |
|
104 |
repeat |
|
105 |
CurrentTeam.CurrHedgehog:= Succ(CurrentTeam.CurrHedgehog) mod cMaxHHIndex; |
|
106 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam.CurrHedgehog = th) |
|
107 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam = tteam); |
|
108 |
||
109 |
if (CurrentTeam = tteam) then |
|
110 |
begin |
|
111 |
if GameType = gmtDemo then |
|
112 |
begin |
|
113 |
SendIPC('q'); |
|
114 |
GameState:= gsExit; |
|
115 |
exit |
|
116 |
end else OutError('There''s only one team on map!', true); |
|
117 |
end; |
|
118 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
119 |
begin |
|
120 |
AttacksNum:= 0; |
|
121 |
with Gear^ do |
|
122 |
begin |
|
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
123 |
State:= gstHHDriven; |
4 | 124 |
Active:= true |
125 |
end; |
|
126 |
FollowGear:= Gear |
|
127 |
end; |
|
128 |
ResetKbd; |
|
129 |
cWindSpeed:= (GetRandom * 2 - 1) * cMaxWindSpeed; |
|
6 | 130 |
AddGear(0, 0, gtActionTimer, gtsSmoothWindCh).Tag:= round(72 * cWindSpeed / cMaxWindSpeed); |
4 | 131 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
132 |
ApplyAmmoChanges(@CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]); |
|
133 |
TurnTimeLeft:= cHedgehogTurnTime |
|
134 |
end; |
|
135 |
||
136 |
procedure SetFirstTurnHedgehog; |
|
137 |
var i: integer; |
|
138 |
begin |
|
139 |
if CurrentTeam=nil then OutError('nil Team (SetFirstTurnHedgehog)', true); |
|
140 |
i:= 0; |
|
141 |
while (i<cMaxHHIndex)and(CurrentTeam.Hedgehogs[i].Gear=nil) do inc(i); |
|
142 |
if CurrentTeam.Hedgehogs[i].Gear = nil then OutError(errmsgIncorrectUse + ' (sfth)', true); |
|
143 |
CurrentTeam.CurrHedgehog:= i; |
|
144 |
end; |
|
145 |
||
146 |
function AddTeam: PTeam; |
|
147 |
begin |
|
17 | 148 |
New(Result); |
149 |
TryDo(Result <> nil, 'AddTean: Result = nil', true); |
|
4 | 150 |
FillChar(Result^, sizeof(TTeam), 0); |
151 |
Result.AttackBar:= 1; |
|
152 |
if TeamsList = nil then TeamsList:= Result |
|
153 |
else begin |
|
154 |
Result.Next:= TeamsList; |
|
155 |
TeamsList:= Result |
|
156 |
end; |
|
157 |
CurrentTeam:= Result |
|
158 |
end; |
|
159 |
||
160 |
procedure FreeTeamsList; |
|
161 |
var t, tt: PTeam; |
|
162 |
begin |
|
163 |
tt:= TeamsList; |
|
164 |
TeamsList:= nil; |
|
165 |
while tt<>nil do |
|
166 |
begin |
|
167 |
t:= tt; |
|
168 |
tt:= tt.Next; |
|
169 |
Dispose(t) |
|
170 |
end; |
|
171 |
end; |
|
172 |
||
10 | 173 |
procedure FillAmmoGroup(Ammo: PHHAmmo); |
174 |
var mi: array[0..cMaxSlotIndex] of byte; |
|
175 |
a: TAmmoType; |
|
176 |
begin |
|
177 |
FillChar(mi, sizeof(mi), 0); |
|
178 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
|
179 |
begin |
|
180 |
TryDo(mi[Ammoz[a].Slot] <= cMaxSlotAmmoIndex, 'Ammo slot overflow', true); |
|
181 |
Ammo^[Ammoz[a].Slot, mi[Ammoz[a].Slot]]:= Ammoz[a].Ammo; |
|
182 |
inc(mi[Ammoz[a].Slot]) |
|
183 |
end; |
|
184 |
end; |
|
185 |
||
47 | 186 |
procedure RecountAllTeamsHealth; |
4 | 187 |
var p: PTeam; |
188 |
begin |
|
189 |
p:= TeamsList; |
|
190 |
while p <> nil do |
|
191 |
begin |
|
47 | 192 |
RecountTeamHealth(p); |
193 |
p:= p.Next |
|
194 |
end |
|
195 |
end; |
|
196 |
||
197 |
procedure InitTeams; |
|
198 |
var p: PTeam; |
|
199 |
i: integer; |
|
200 |
th: integer; |
|
201 |
begin |
|
202 |
p:= TeamsList; |
|
203 |
while p <> nil do |
|
204 |
begin |
|
205 |
th:= 0; |
|
10 | 206 |
FillAmmoGroup(@p.Ammos[0]); |
4 | 207 |
for i:= 0 to cMaxHHIndex do |
208 |
if p.Hedgehogs[i].Gear <> nil then |
|
209 |
begin |
|
210 |
p.Hedgehogs[i].Gear.Health:= 100; |
|
47 | 211 |
inc(th, 100); |
10 | 212 |
p.Hedgehogs[i].Ammo:= @p.Ammos[0] // 0 means all hedgehogs |
213 |
// will have common set of ammo |
|
4 | 214 |
end; |
47 | 215 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 216 |
p:= p.Next |
217 |
end; |
|
218 |
SetFirstTurnHedgehog; |
|
47 | 219 |
RecountAllTeamsHealth |
4 | 220 |
end; |
221 |
||
222 |
procedure ApplyAmmoChanges(Hedgehog: PHedgehog); |
|
223 |
var s: shortstring; |
|
224 |
begin |
|
225 |
with Hedgehog^ do |
|
47 | 226 |
begin |
4 | 227 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
228 |
begin |
|
229 |
CurAmmo:= 0; |
|
10 | 230 |
while (CurAmmo <= cMaxSlotAmmoIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo) |
4 | 231 |
end; |
232 |
||
233 |
with Ammo[CurSlot, CurAmmo] do |
|
234 |
begin |
|
235 |
s:= Ammoz[AmmoType].Name; |
|
236 |
if Count <> AMMO_INFINITE then |
|
237 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
238 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
239 |
s:= s + ', ' + inttostr(Timer div 1000) + ' sec'; |
|
240 |
AddCaption(s, Team.Color, capgrpAmmoinfo); |
|
241 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
242 |
then begin |
|
243 |
Gear.State:= Gear.State or gstHHChooseTarget; |
|
244 |
isCursorVisible:= true |
|
245 |
end else begin |
|
246 |
Gear.State:= Gear.State and not gstHHChooseTarget; |
|
247 |
AdjustMPoint; |
|
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 |
4 | 263 |
if (Ammo[slot, ami].Count = 0) |
264 |
and (Ammo[slot, ami + 1].Count > 0) then b:= true |
|
265 |
else inc(ami); |
|
266 |
if b then // есть пустое место |
|
267 |
begin |
|
268 |
Ammo[slot, ami]:= Ammo[slot, ami + 1] |
|
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 |
||
4 | 315 |
initialization |
316 |
||
317 |
finalization |
|
318 |
||
319 |
FreeTeamsList |
|
320 |
||
321 |
end. |