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;
|
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
|
|
143 |
try
|
|
144 |
New(Result);
|
|
145 |
except Result:= nil; OutError(errmsgDynamicVar, true) end;
|
|
146 |
FillChar(Result^, sizeof(TTeam), 0);
|
|
147 |
Result.AttackBar:= 1;
|
|
148 |
if TeamsList = nil then TeamsList:= Result
|
|
149 |
else begin
|
|
150 |
Result.Next:= TeamsList;
|
|
151 |
TeamsList:= Result
|
|
152 |
end;
|
|
153 |
CurrentTeam:= Result
|
|
154 |
end;
|
|
155 |
|
|
156 |
procedure FreeTeamsList;
|
|
157 |
var t, tt: PTeam;
|
|
158 |
begin
|
|
159 |
tt:= TeamsList;
|
|
160 |
TeamsList:= nil;
|
|
161 |
while tt<>nil do
|
|
162 |
begin
|
|
163 |
t:= tt;
|
|
164 |
tt:= tt.Next;
|
|
165 |
try
|
|
166 |
Dispose(t)
|
|
167 |
except OutError(errmsgDynamicVar) end;
|
|
168 |
end;
|
|
169 |
end;
|
|
170 |
|
|
171 |
procedure InitTeams;
|
|
172 |
var p: PTeam;
|
|
173 |
i: integer;
|
|
174 |
begin
|
|
175 |
p:= TeamsList;
|
|
176 |
while p <> nil do
|
|
177 |
begin
|
|
178 |
for i:= 0 to cMaxHHIndex do
|
|
179 |
if p.Hedgehogs[i].Gear <> nil then
|
|
180 |
begin
|
|
181 |
p.Ammos[i][0, 0]:= Ammoz[amGrenade].Ammo;
|
|
182 |
p.Ammos[i][0, 1]:= Ammoz[amUFO].Ammo;
|
|
183 |
p.Ammos[i][1, 0]:= Ammoz[amBazooka].Ammo;
|
|
184 |
p.Ammos[i][2, 0]:= Ammoz[amShotgun].Ammo;
|
|
185 |
p.Ammos[i][3, 0]:= Ammoz[amPickHammer].Ammo;
|
|
186 |
p.Ammos[i][3, 1]:= Ammoz[amRope].Ammo;
|
|
187 |
p.Ammos[i][4, 0]:= Ammoz[amSkip].Ammo;
|
|
188 |
p.Hedgehogs[i].Gear.Health:= 100;
|
|
189 |
p.Hedgehogs[i].Ammo:= @p.Ammos[0]
|
|
190 |
{0 - общее на всех оружие, i - у каждого своё
|
|
191 |
можно группировать ёжиков, чтобы у каждой группы было своё оружие}
|
|
192 |
end;
|
|
193 |
p:= p.Next
|
|
194 |
end;
|
|
195 |
SetFirstTurnHedgehog;
|
|
196 |
end;
|
|
197 |
|
|
198 |
procedure ApplyAmmoChanges(Hedgehog: PHedgehog);
|
|
199 |
var s: shortstring;
|
|
200 |
begin
|
|
201 |
with Hedgehog^ do
|
|
202 |
begin
|
|
203 |
if Ammo[CurSlot, CurAmmo].Count = 0 then
|
|
204 |
begin
|
|
205 |
CurAmmo:= 0;
|
|
206 |
while (CurAmmo <= cMaxSlotAmmo) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurAmmo)
|
|
207 |
end;
|
|
208 |
|
|
209 |
with Ammo[CurSlot, CurAmmo] do
|
|
210 |
begin
|
|
211 |
s:= Ammoz[AmmoType].Name;
|
|
212 |
if Count <> AMMO_INFINITE then
|
|
213 |
s:= s + ' (' + IntToStr(Count) + ')';
|
|
214 |
if (Propz and ammoprop_Timerable) <> 0 then
|
|
215 |
s:= s + ', ' + inttostr(Timer div 1000) + ' sec';
|
|
216 |
AddCaption(s, Team.Color, capgrpAmmoinfo);
|
|
217 |
if (Propz and ammoprop_NeedTarget) <> 0
|
|
218 |
then begin
|
|
219 |
Gear.State:= Gear.State or gstHHChooseTarget;
|
|
220 |
isCursorVisible:= true
|
|
221 |
end else begin
|
|
222 |
Gear.State:= Gear.State and not gstHHChooseTarget;
|
|
223 |
AdjustMPoint;
|
|
224 |
isCursorVisible:= false
|
|
225 |
end
|
|
226 |
end
|
|
227 |
end
|
|
228 |
end;
|
|
229 |
|
|
230 |
procedure PackAmmo(Ammo: PHHAmmo; Slot: integer);
|
|
231 |
var ami: integer;
|
|
232 |
b: boolean;
|
|
233 |
begin
|
|
234 |
repeat
|
|
235 |
b:= false;
|
|
236 |
ami:= 0;
|
|
237 |
while (not b) and (ami < cMaxSlotAmmo) do
|
|
238 |
if (Ammo[slot, ami].Count = 0)
|
|
239 |
and (Ammo[slot, ami + 1].Count > 0) then b:= true
|
|
240 |
else inc(ami);
|
|
241 |
if b then // есть пустое место
|
|
242 |
begin
|
|
243 |
Ammo[slot, ami]:= Ammo[slot, ami + 1]
|
|
244 |
end
|
|
245 |
until not b;
|
|
246 |
end;
|
|
247 |
|
|
248 |
procedure OnUsedAmmo(Ammo: PHHAmmo);
|
|
249 |
var s, a: Longword;
|
|
250 |
begin
|
|
251 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
|
|
252 |
begin
|
|
253 |
if CurAmmoGear = nil then begin s:= CurSlot; a:= CurAmmo end
|
|
254 |
else begin s:= AltSlot; a:= AltAmmo end;
|
|
255 |
with Ammo[s, a] do
|
|
256 |
if Count <> AMMO_INFINITE then
|
|
257 |
begin
|
|
258 |
dec(Count);
|
|
259 |
if Count = 0 then PackAmmo(Ammo, CurSlot)
|
|
260 |
end
|
|
261 |
end
|
|
262 |
end;
|
|
263 |
|
|
264 |
function TeamSize(p: PTeam): Longword;
|
|
265 |
var i: Longword;
|
|
266 |
begin
|
|
267 |
Result:= 0;
|
|
268 |
for i:= 0 to cMaxHHIndex do
|
|
269 |
if p.Hedgehogs[i].Gear <> nil then inc(Result)
|
|
270 |
end;
|
|
271 |
|
|
272 |
initialization
|
|
273 |
|
|
274 |
finalization
|
|
275 |
|
|
276 |
FreeTeamsList
|
|
277 |
|
|
278 |
end.
|