author | unc0rr |
Tue, 20 Jun 2006 21:18:49 +0000 | |
changeset 66 | 9643d75baf1e |
parent 64 | 9df467527ae5 |
child 70 | 82d93eeecebe |
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 |
|
64 | 84 |
uses uMisc, uStore, uWorld, uIO, uAI; |
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 |
isCursorVisible:= false |
|
13 | 248 |
end; |
249 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 250 |
end |
251 |
end |
|
252 |
end; |
|
253 |
||
254 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer); |
|
255 |
var ami: integer; |
|
256 |
b: boolean; |
|
257 |
begin |
|
258 |
repeat |
|
259 |
b:= false; |
|
260 |
ami:= 0; |
|
10 | 261 |
while (not b) and (ami < cMaxSlotAmmoIndex) do |
4 | 262 |
if (Ammo[slot, ami].Count = 0) |
263 |
and (Ammo[slot, ami + 1].Count > 0) then b:= true |
|
264 |
else inc(ami); |
|
265 |
if b then // есть пустое место |
|
266 |
begin |
|
267 |
Ammo[slot, ami]:= Ammo[slot, ami + 1] |
|
268 |
end |
|
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 |
||
288 |
function TeamSize(p: PTeam): Longword; |
|
289 |
var i: Longword; |
|
290 |
begin |
|
291 |
Result:= 0; |
|
292 |
for i:= 0 to cMaxHHIndex do |
|
293 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
294 |
end; |
|
295 |
||
47 | 296 |
procedure RecountTeamHealth(team: PTeam); |
297 |
var i: integer; |
|
298 |
begin |
|
299 |
with team^ do |
|
300 |
begin |
|
301 |
TeamHealth:= 0; |
|
302 |
for i:= 0 to cMaxHHIndex do |
|
303 |
if Hedgehogs[i].Gear <> nil then |
|
304 |
inc(TeamHealth, Hedgehogs[i].Gear.Health); |
|
305 |
if TeamHealth > MaxTeamHealth then |
|
306 |
begin |
|
307 |
MaxTeamHealth:= TeamHealth; |
|
308 |
RecountAllTeamsHealth; |
|
309 |
end else TeamHealth:= (TeamHealth * cTeamHealthWidth) div MaxTeamHealth |
|
49 | 310 |
end; |
311 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
|
47 | 312 |
end; |
313 |
||
4 | 314 |
initialization |
315 |
||
316 |
finalization |
|
317 |
||
318 |
FreeTeamsList |
|
319 |
||
320 |
end. |