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;
|
|
53 |
THHAmmo = array[0..cMaxSlot, 0..cMaxSlotAmmo] of TAmmo;
|
|
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
|
|
118 |
State:= State or gstHHDriven;
|
|
119 |
Active:= true
|
|
120 |
end;
|
|
121 |
FollowGear:= Gear
|
|
122 |
end;
|
|
123 |
ResetKbd;
|
|
124 |
cWindSpeed:= (GetRandom * 2 - 1) * cMaxWindSpeed;
|
|
125 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF}
|
|
126 |
ApplyAmmoChanges(@CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]);
|
|
127 |
TurnTimeLeft:= cHedgehogTurnTime
|
|
128 |
end;
|
|
129 |
|
|
130 |
procedure SetFirstTurnHedgehog;
|
|
131 |
var i: integer;
|
|
132 |
begin
|
|
133 |
if CurrentTeam=nil then OutError('nil Team (SetFirstTurnHedgehog)', true);
|
|
134 |
i:= 0;
|
|
135 |
while (i<cMaxHHIndex)and(CurrentTeam.Hedgehogs[i].Gear=nil) do inc(i);
|
|
136 |
if CurrentTeam.Hedgehogs[i].Gear = nil then OutError(errmsgIncorrectUse + ' (sfth)', true);
|
|
137 |
CurrentTeam.CurrHedgehog:= i;
|
|
138 |
end;
|
|
139 |
|
|
140 |
function AddTeam: PTeam;
|
|
141 |
begin
|
|
142 |
try
|
|
143 |
New(Result);
|
|
144 |
except Result:= nil; OutError(errmsgDynamicVar, true) end;
|
|
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 |
try
|
|
165 |
Dispose(t)
|
|
166 |
except OutError(errmsgDynamicVar) end;
|
|
167 |
end;
|
|
168 |
end;
|
|
169 |
|
|
170 |
procedure InitTeams;
|
|
171 |
var p: PTeam;
|
|
172 |
i: integer;
|
|
173 |
begin
|
|
174 |
p:= TeamsList;
|
|
175 |
while p <> nil do
|
|
176 |
begin
|
|
177 |
for i:= 0 to cMaxHHIndex do
|
|
178 |
if p.Hedgehogs[i].Gear <> nil then
|
|
179 |
begin
|
|
180 |
p.Ammos[i][0, 0]:= Ammoz[amGrenade].Ammo;
|
|
181 |
p.Ammos[i][0, 1]:= Ammoz[amUFO].Ammo;
|
|
182 |
p.Ammos[i][1, 0]:= Ammoz[amBazooka].Ammo;
|
|
183 |
p.Ammos[i][2, 0]:= Ammoz[amShotgun].Ammo;
|
|
184 |
p.Ammos[i][3, 0]:= Ammoz[amPickHammer].Ammo;
|
|
185 |
p.Ammos[i][3, 1]:= Ammoz[amRope].Ammo;
|
|
186 |
p.Ammos[i][4, 0]:= Ammoz[amSkip].Ammo;
|
|
187 |
p.Hedgehogs[i].Gear.Health:= 100;
|
|
188 |
p.Hedgehogs[i].Ammo:= @p.Ammos[0]
|
|
189 |
{0 - общее на всех оружие, i - у каждого своё
|
|
190 |
можно группировать ёжиков, чтобы у каждой группы было своё оружие}
|
|
191 |
end;
|
|
192 |
p:= p.Next
|
|
193 |
end;
|
|
194 |
SetFirstTurnHedgehog;
|
|
195 |
end;
|
|
196 |
|
|
197 |
procedure ApplyAmmoChanges(Hedgehog: PHedgehog);
|
|
198 |
var s: shortstring;
|
|
199 |
begin
|
|
200 |
with Hedgehog^ do
|
|
201 |
begin
|
|
202 |
if Ammo[CurSlot, CurAmmo].Count = 0 then
|
|
203 |
begin
|
|
204 |
CurAmmo:= 0;
|
|
205 |
while (CurAmmo <= cMaxSlotAmmo) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo)
|
|
206 |
end;
|
|
207 |
|
|
208 |
with Ammo[CurSlot, CurAmmo] do
|
|
209 |
begin
|
|
210 |
s:= Ammoz[AmmoType].Name;
|
|
211 |
if Count <> AMMO_INFINITE then
|
|
212 |
s:= s + ' (' + IntToStr(Count) + ')';
|
|
213 |
if (Propz and ammoprop_Timerable) <> 0 then
|
|
214 |
s:= s + ', ' + inttostr(Timer div 1000) + ' sec';
|
|
215 |
AddCaption(s, Team.Color, capgrpAmmoinfo);
|
|
216 |
if (Propz and ammoprop_NeedTarget) <> 0
|
|
217 |
then begin
|
|
218 |
Gear.State:= Gear.State or gstHHChooseTarget;
|
|
219 |
isCursorVisible:= true
|
|
220 |
end else begin
|
|
221 |
Gear.State:= Gear.State and not gstHHChooseTarget;
|
|
222 |
AdjustMPoint;
|
|
223 |
isCursorVisible:= false
|
|
224 |
end
|
|
225 |
end
|
|
226 |
end
|
|
227 |
end;
|
|
228 |
|
|
229 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer);
|
|
230 |
var ami: integer;
|
|
231 |
b: boolean;
|
|
232 |
begin
|
|
233 |
repeat
|
|
234 |
b:= false;
|
|
235 |
ami:= 0;
|
|
236 |
while (not b) and (ami < cMaxSlotAmmo) do
|
|
237 |
if (Ammo[slot, ami].Count = 0)
|
|
238 |
and (Ammo[slot, ami + 1].Count > 0) then b:= true
|
|
239 |
else inc(ami);
|
|
240 |
if b then // есть пустое место
|
|
241 |
begin
|
|
242 |
Ammo[slot, ami]:= Ammo[slot, ami + 1]
|
|
243 |
end
|
|
244 |
until not b;
|
|
245 |
end;
|
|
246 |
|
|
247 |
procedure OnUsedAmmo(Ammo: PHHAmmo);
|
|
248 |
var s, a: Longword;
|
|
249 |
begin
|
|
250 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
|
|
251 |
begin
|
|
252 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end
|
|
253 |
else begin s:= AltSlot; a:= AltAmmo end;
|
|
254 |
with Ammo[s, a] do
|
|
255 |
if Count <> AMMO_INFINITE then
|
|
256 |
begin
|
|
257 |
dec(Count);
|
|
258 |
if Count = 0 then PackAmmo(Ammo, CurSlot)
|
|
259 |
end
|
|
260 |
end
|
|
261 |
end;
|
|
262 |
|
|
263 |
function TeamSize(p: PTeam): Longword;
|
|
264 |
var i: Longword;
|
|
265 |
begin
|
|
266 |
Result:= 0;
|
|
267 |
for i:= 0 to cMaxHHIndex do
|
|
268 |
if p.Hedgehogs[i].Gear <> nil then inc(Result)
|
|
269 |
end;
|
|
270 |
|
|
271 |
initialization
|
|
272 |
|
|
273 |
finalization
|
|
274 |
|
|
275 |
FreeTeamsList
|
|
276 |
|
|
277 |
end.
|