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