author | unc0rr |
Sun, 17 Dec 2006 20:50:49 +0000 | |
changeset 304 | 8096e69e839e |
parent 295 | 8834f3cb620e |
child 306 | 7b61834edcf6 |
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 |
* |
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
19 |
unit uTeams; |
|
20 |
interface |
|
288 | 21 |
uses SDLh, uConsts, uKeys, uGears, uRandom, uAmmos; |
4 | 22 |
{$INCLUDE options.inc} |
23 |
type PHedgehog = ^THedgehog; |
|
24 |
PTeam = ^TTeam; |
|
25 |
THedgehog = record |
|
74 | 26 |
Name: string[MAXNAMELEN]; |
4 | 27 |
Gear: PGear; |
95 | 28 |
NameTag, HealthTag: PSDL_Surface; |
4 | 29 |
Ammo: PHHAmmo; |
288 | 30 |
AmmoStore: Longword; |
4 | 31 |
CurSlot, CurAmmo: LongWord; |
32 |
AltSlot, AltAmmo: LongWord; |
|
33 |
Team: PTeam; |
|
34 |
AttacksNum: Longword; |
|
35 |
visStepPos: LongWord; |
|
5 | 36 |
BotLevel : LongWord; // 0 - Human player |
4 | 37 |
end; |
38 |
TTeam = record |
|
39 |
Next: PTeam; |
|
189 | 40 |
Color, AdjColor: Longword; |
74 | 41 |
TeamName: string[MAXNAMELEN]; |
4 | 42 |
ExtDriven: boolean; |
167 | 43 |
Binds: TBinds; |
4 | 44 |
Hedgehogs: array[0..cMaxHHIndex] of THedgehog; |
45 |
CurrHedgehog: integer; |
|
95 | 46 |
NameTag: PSDL_Surface; |
198 | 47 |
CrosshairSurf: PSDL_Surface; |
47 | 48 |
GraveRect, HealthRect: TSDL_Rect; |
4 | 49 |
GraveName: string; |
50 |
FortName: string; |
|
47 | 51 |
TeamHealth: integer; |
146 | 52 |
TeamHealthBarWidth: integer; |
49 | 53 |
DrawHealthY: integer; |
4 | 54 |
AttackBar: LongWord; |
55 |
end; |
|
56 |
||
57 |
var CurrentTeam: PTeam = nil; |
|
58 |
TeamsList: PTeam = nil; |
|
304 | 59 |
CurMinAngle, CurMaxAngle: Longword; |
4 | 60 |
|
61 |
function AddTeam: PTeam; |
|
70 | 62 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 63 |
procedure SwitchHedgehog; |
64 |
procedure InitTeams; |
|
65 |
function TeamSize(p: PTeam): Longword; |
|
47 | 66 |
procedure RecountTeamHealth(team: PTeam); |
72 | 67 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
68 |
function CheckForWin: boolean; |
165 | 69 |
procedure SetWeapon(weap: TAmmoType); |
4 | 70 |
|
71 |
implementation |
|
196 | 72 |
uses uMisc, uWorld, uAI, uLocale, uConsole; |
47 | 73 |
const MaxTeamHealth: integer = 0; |
4 | 74 |
|
75 |
procedure FreeTeamsList; forward; |
|
76 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
77 |
function CheckForWin: boolean; |
83 | 78 |
var team, AliveTeam: PTeam; |
79 |
AliveCount: Longword; |
|
80 |
begin |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
81 |
Result:= false; |
83 | 82 |
AliveCount:= 0; |
83 |
AliveTeam:= nil; |
|
84 |
team:= TeamsList; |
|
85 |
while team <> nil do |
|
86 |
begin |
|
87 |
if team.TeamHealth > 0 then |
|
88 |
begin |
|
89 |
inc(AliveCount); |
|
90 |
AliveTeam:= team |
|
91 |
end; |
|
92 |
team:= team.Next |
|
93 |
end; |
|
94 |
||
95 |
if AliveCount >= 2 then exit; |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
96 |
Result:= true; |
83 | 97 |
|
98 |
TurnTimeLeft:= 0; |
|
99 |
if AliveCount = 0 then |
|
100 |
begin // draw |
|
101 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
102 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
103 |
end else // win |
|
104 |
begin |
|
105 |
AddCaption(Format(trmsg[sidWinner], AliveTeam.TeamName), $FFFFFF, capgrpGameState); |
|
106 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
107 |
end; |
|
108 |
end; |
|
109 |
||
4 | 110 |
procedure SwitchHedgehog; |
111 |
var tteam: PTeam; |
|
112 |
th: integer; |
|
113 |
begin |
|
114 |
FreeActionsList; |
|
115 |
TargetPoint.X:= NoPointX; |
|
89 | 116 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
4 | 117 |
tteam:= CurrentTeam; |
118 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
294 | 119 |
if Gear <> nil then |
120 |
begin |
|
121 |
Gear.Message:= 0; |
|
122 |
Gear.Z:= cHHZ |
|
123 |
end; |
|
4 | 124 |
|
125 |
repeat |
|
126 |
CurrentTeam:= CurrentTeam.Next; |
|
127 |
if CurrentTeam = nil then CurrentTeam:= TeamsList; |
|
128 |
th:= CurrentTeam.CurrHedgehog; |
|
129 |
repeat |
|
83 | 130 |
CurrentTeam.CurrHedgehog:= Succ(CurrentTeam.CurrHedgehog) mod (cMaxHHIndex + 1); |
4 | 131 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam.CurrHedgehog = th) |
132 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam = tteam); |
|
133 |
||
83 | 134 |
TryDo(CurrentTeam <> tteam, 'Switch hedgehog: only one team?!', true); |
135 |
||
4 | 136 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
137 |
begin |
|
138 |
AttacksNum:= 0; |
|
139 |
with Gear^ do |
|
140 |
begin |
|
294 | 141 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
142 |
State:= gstHHDriven; |
4 | 143 |
Active:= true |
144 |
end; |
|
294 | 145 |
RemoveGearFromList(Gear); |
146 |
InsertGearToList(Gear); |
|
4 | 147 |
FollowGear:= Gear |
148 |
end; |
|
149 |
ResetKbd; |
|
150 |
cWindSpeed:= (GetRandom * 2 - 1) * cMaxWindSpeed; |
|
83 | 151 |
AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1).Tag:= round(72 * cWindSpeed / cMaxWindSpeed); |
4 | 152 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
70 | 153 |
ApplyAmmoChanges(CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]); |
167 | 154 |
if CurrentTeam.ExtDriven then SetDefaultBinds |
155 |
else SetBinds(CurrentTeam.Binds); |
|
176 | 156 |
bShowFinger:= true; |
4 | 157 |
TurnTimeLeft:= cHedgehogTurnTime |
158 |
end; |
|
159 |
||
160 |
function AddTeam: PTeam; |
|
161 |
begin |
|
17 | 162 |
New(Result); |
80 | 163 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 164 |
FillChar(Result^, sizeof(TTeam), 0); |
74 | 165 |
Result.AttackBar:= 2; |
83 | 166 |
Result.CurrHedgehog:= cMaxHHIndex; |
4 | 167 |
if TeamsList = nil then TeamsList:= Result |
168 |
else begin |
|
169 |
Result.Next:= TeamsList; |
|
170 |
TeamsList:= Result |
|
171 |
end; |
|
172 |
CurrentTeam:= Result |
|
173 |
end; |
|
174 |
||
175 |
procedure FreeTeamsList; |
|
176 |
var t, tt: PTeam; |
|
177 |
begin |
|
178 |
tt:= TeamsList; |
|
179 |
TeamsList:= nil; |
|
180 |
while tt<>nil do |
|
181 |
begin |
|
182 |
t:= tt; |
|
183 |
tt:= tt.Next; |
|
184 |
Dispose(t) |
|
185 |
end; |
|
186 |
end; |
|
187 |
||
47 | 188 |
procedure RecountAllTeamsHealth; |
4 | 189 |
var p: PTeam; |
190 |
begin |
|
191 |
p:= TeamsList; |
|
192 |
while p <> nil do |
|
193 |
begin |
|
47 | 194 |
RecountTeamHealth(p); |
195 |
p:= p.Next |
|
196 |
end |
|
197 |
end; |
|
198 |
||
199 |
procedure InitTeams; |
|
200 |
var p: PTeam; |
|
201 |
i: integer; |
|
202 |
th: integer; |
|
203 |
begin |
|
204 |
p:= TeamsList; |
|
205 |
while p <> nil do |
|
206 |
begin |
|
207 |
th:= 0; |
|
4 | 208 |
for i:= 0 to cMaxHHIndex do |
209 |
if p.Hedgehogs[i].Gear <> nil then |
|
210 |
begin |
|
211 |
p.Hedgehogs[i].Gear.Health:= 100; |
|
47 | 212 |
inc(th, 100); |
4 | 213 |
end; |
47 | 214 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 215 |
p:= p.Next |
216 |
end; |
|
47 | 217 |
RecountAllTeamsHealth |
4 | 218 |
end; |
219 |
||
70 | 220 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 221 |
var s: shortstring; |
222 |
begin |
|
162 | 223 |
TargetPoint.X:= NoPointX; |
224 |
||
70 | 225 |
with Hedgehog do |
47 | 226 |
begin |
4 | 227 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
228 |
begin |
|
229 |
CurAmmo:= 0; |
|
288 | 230 |
CurSlot:= 0; |
231 |
while (CurSlot <= cMaxSlotIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
|
4 | 232 |
end; |
233 |
||
234 |
with Ammo[CurSlot, CurAmmo] do |
|
235 |
begin |
|
304 | 236 |
CurMinAngle:= Ammoz[AmmoType].minAngle; |
237 |
if Ammoz[AmmoType].maxAngle <> 0 then CurMaxAngle:= Ammoz[AmmoType].maxAngle |
|
238 |
else CurMaxAngle:= cMaxAngle; |
|
239 |
with Hedgehog.Gear^ do |
|
240 |
begin |
|
241 |
if Angle < CurMinAngle then Angle:= CurMinAngle; |
|
242 |
if Angle > CurMaxAngle then Angle:= CurMaxAngle; |
|
243 |
end; |
|
244 |
||
80 | 245 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 246 |
if Count <> AMMO_INFINITE then |
247 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
248 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 249 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
4 | 250 |
AddCaption(s, Team.Color, capgrpAmmoinfo); |
251 |
if (Propz and ammoprop_NeedTarget) <> 0 |
|
252 |
then begin |
|
253 |
Gear.State:= Gear.State or gstHHChooseTarget; |
|
254 |
isCursorVisible:= true |
|
255 |
end else begin |
|
256 |
Gear.State:= Gear.State and not gstHHChooseTarget; |
|
257 |
isCursorVisible:= false |
|
13 | 258 |
end; |
259 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 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 |
||
47 | 272 |
procedure RecountTeamHealth(team: PTeam); |
273 |
var i: integer; |
|
274 |
begin |
|
275 |
with team^ do |
|
276 |
begin |
|
146 | 277 |
TeamHealthBarWidth:= 0; |
47 | 278 |
for i:= 0 to cMaxHHIndex do |
279 |
if Hedgehogs[i].Gear <> nil then |
|
146 | 280 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear.Health); |
281 |
TeamHealth:= TeamHealthBarWidth; |
|
282 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 283 |
begin |
146 | 284 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 285 |
RecountAllTeamsHealth; |
146 | 286 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 287 |
end; |
105 | 288 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
49 | 289 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
47 | 290 |
end; |
291 |
||
72 | 292 |
procedure RestoreTeamsFromSave; |
293 |
var p: PTeam; |
|
294 |
begin |
|
295 |
p:= TeamsList; |
|
296 |
while p <> nil do |
|
297 |
begin |
|
298 |
p.ExtDriven:= false; |
|
299 |
p:= p.Next |
|
300 |
end; |
|
301 |
end; |
|
302 |
||
165 | 303 |
procedure SetWeapon(weap: TAmmoType); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
304 |
var t: integer; |
165 | 305 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
306 |
t:= cMaxSlotAmmoIndex; |
165 | 307 |
with CurrentTeam^ do |
308 |
with Hedgehogs[CurrHedgehog] do |
|
255 | 309 |
while (Ammo[CurSlot, CurAmmo].AmmoType <> weap) and (t >= 0) do |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
310 |
begin |
165 | 311 |
ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot)); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
312 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
313 |
end |
165 | 314 |
end; |
315 |
||
4 | 316 |
initialization |
317 |
||
318 |
finalization |
|
319 |
||
320 |
FreeTeamsList |
|
321 |
||
322 |
end. |