author | unc0rr |
Sun, 10 Dec 2006 15:55:53 +0000 | |
changeset 295 | 8834f3cb620e |
parent 294 | 92a7ccd67bb9 |
child 304 | 8096e69e839e |
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; |
|
59 |
||
60 |
function AddTeam: PTeam; |
|
70 | 61 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 62 |
procedure SwitchHedgehog; |
63 |
procedure InitTeams; |
|
64 |
function TeamSize(p: PTeam): Longword; |
|
47 | 65 |
procedure RecountTeamHealth(team: PTeam); |
72 | 66 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
67 |
function CheckForWin: boolean; |
165 | 68 |
procedure SetWeapon(weap: TAmmoType); |
4 | 69 |
|
70 |
implementation |
|
196 | 71 |
uses uMisc, uWorld, uAI, uLocale, uConsole; |
47 | 72 |
const MaxTeamHealth: integer = 0; |
4 | 73 |
|
74 |
procedure FreeTeamsList; forward; |
|
75 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
76 |
function CheckForWin: boolean; |
83 | 77 |
var team, AliveTeam: PTeam; |
78 |
AliveCount: Longword; |
|
79 |
begin |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
80 |
Result:= false; |
83 | 81 |
AliveCount:= 0; |
82 |
AliveTeam:= nil; |
|
83 |
team:= TeamsList; |
|
84 |
while team <> nil do |
|
85 |
begin |
|
86 |
if team.TeamHealth > 0 then |
|
87 |
begin |
|
88 |
inc(AliveCount); |
|
89 |
AliveTeam:= team |
|
90 |
end; |
|
91 |
team:= team.Next |
|
92 |
end; |
|
93 |
||
94 |
if AliveCount >= 2 then exit; |
|
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
95 |
Result:= true; |
83 | 96 |
|
97 |
TurnTimeLeft:= 0; |
|
98 |
if AliveCount = 0 then |
|
99 |
begin // draw |
|
100 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
101 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
102 |
end else // win |
|
103 |
begin |
|
104 |
AddCaption(Format(trmsg[sidWinner], AliveTeam.TeamName), $FFFFFF, capgrpGameState); |
|
105 |
AddGear(0, 0, gtATFinishGame, 0, 0, 0, 2000) |
|
106 |
end; |
|
107 |
end; |
|
108 |
||
4 | 109 |
procedure SwitchHedgehog; |
110 |
var tteam: PTeam; |
|
111 |
th: integer; |
|
112 |
begin |
|
113 |
FreeActionsList; |
|
114 |
TargetPoint.X:= NoPointX; |
|
89 | 115 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
4 | 116 |
tteam:= CurrentTeam; |
117 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
294 | 118 |
if Gear <> nil then |
119 |
begin |
|
120 |
Gear.Message:= 0; |
|
121 |
Gear.Z:= cHHZ |
|
122 |
end; |
|
4 | 123 |
|
124 |
repeat |
|
125 |
CurrentTeam:= CurrentTeam.Next; |
|
126 |
if CurrentTeam = nil then CurrentTeam:= TeamsList; |
|
127 |
th:= CurrentTeam.CurrHedgehog; |
|
128 |
repeat |
|
83 | 129 |
CurrentTeam.CurrHedgehog:= Succ(CurrentTeam.CurrHedgehog) mod (cMaxHHIndex + 1); |
4 | 130 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam.CurrHedgehog = th) |
131 |
until (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear <> nil) or (CurrentTeam = tteam); |
|
132 |
||
83 | 133 |
TryDo(CurrentTeam <> tteam, 'Switch hedgehog: only one team?!', true); |
134 |
||
4 | 135 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
136 |
begin |
|
137 |
AttacksNum:= 0; |
|
138 |
with Gear^ do |
|
139 |
begin |
|
294 | 140 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
141 |
State:= gstHHDriven; |
4 | 142 |
Active:= true |
143 |
end; |
|
294 | 144 |
RemoveGearFromList(Gear); |
145 |
InsertGearToList(Gear); |
|
4 | 146 |
FollowGear:= Gear |
147 |
end; |
|
148 |
ResetKbd; |
|
149 |
cWindSpeed:= (GetRandom * 2 - 1) * cMaxWindSpeed; |
|
83 | 150 |
AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1).Tag:= round(72 * cWindSpeed / cMaxWindSpeed); |
4 | 151 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
70 | 152 |
ApplyAmmoChanges(CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]); |
167 | 153 |
if CurrentTeam.ExtDriven then SetDefaultBinds |
154 |
else SetBinds(CurrentTeam.Binds); |
|
176 | 155 |
bShowFinger:= true; |
4 | 156 |
TurnTimeLeft:= cHedgehogTurnTime |
157 |
end; |
|
158 |
||
159 |
function AddTeam: PTeam; |
|
160 |
begin |
|
17 | 161 |
New(Result); |
80 | 162 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 163 |
FillChar(Result^, sizeof(TTeam), 0); |
74 | 164 |
Result.AttackBar:= 2; |
83 | 165 |
Result.CurrHedgehog:= cMaxHHIndex; |
4 | 166 |
if TeamsList = nil then TeamsList:= Result |
167 |
else begin |
|
168 |
Result.Next:= TeamsList; |
|
169 |
TeamsList:= Result |
|
170 |
end; |
|
171 |
CurrentTeam:= Result |
|
172 |
end; |
|
173 |
||
174 |
procedure FreeTeamsList; |
|
175 |
var t, tt: PTeam; |
|
176 |
begin |
|
177 |
tt:= TeamsList; |
|
178 |
TeamsList:= nil; |
|
179 |
while tt<>nil do |
|
180 |
begin |
|
181 |
t:= tt; |
|
182 |
tt:= tt.Next; |
|
183 |
Dispose(t) |
|
184 |
end; |
|
185 |
end; |
|
186 |
||
47 | 187 |
procedure RecountAllTeamsHealth; |
4 | 188 |
var p: PTeam; |
189 |
begin |
|
190 |
p:= TeamsList; |
|
191 |
while p <> nil do |
|
192 |
begin |
|
47 | 193 |
RecountTeamHealth(p); |
194 |
p:= p.Next |
|
195 |
end |
|
196 |
end; |
|
197 |
||
198 |
procedure InitTeams; |
|
199 |
var p: PTeam; |
|
200 |
i: integer; |
|
201 |
th: integer; |
|
202 |
begin |
|
203 |
p:= TeamsList; |
|
204 |
while p <> nil do |
|
205 |
begin |
|
206 |
th:= 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); |
4 | 212 |
end; |
47 | 213 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 214 |
p:= p.Next |
215 |
end; |
|
47 | 216 |
RecountAllTeamsHealth |
4 | 217 |
end; |
218 |
||
70 | 219 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 220 |
var s: shortstring; |
221 |
begin |
|
162 | 222 |
TargetPoint.X:= NoPointX; |
223 |
||
70 | 224 |
with Hedgehog do |
47 | 225 |
begin |
4 | 226 |
if Ammo[CurSlot, CurAmmo].Count = 0 then |
227 |
begin |
|
228 |
CurAmmo:= 0; |
|
288 | 229 |
CurSlot:= 0; |
230 |
while (CurSlot <= cMaxSlotIndex) and (Ammo[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
|
4 | 231 |
end; |
232 |
||
233 |
with Ammo[CurSlot, CurAmmo] do |
|
234 |
begin |
|
80 | 235 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 236 |
if Count <> AMMO_INFINITE then |
237 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
238 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 239 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
4 | 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 |
function TeamSize(p: PTeam): Longword; |
|
255 |
var i: Longword; |
|
256 |
begin |
|
257 |
Result:= 0; |
|
258 |
for i:= 0 to cMaxHHIndex do |
|
259 |
if p.Hedgehogs[i].Gear <> nil then inc(Result) |
|
260 |
end; |
|
261 |
||
47 | 262 |
procedure RecountTeamHealth(team: PTeam); |
263 |
var i: integer; |
|
264 |
begin |
|
265 |
with team^ do |
|
266 |
begin |
|
146 | 267 |
TeamHealthBarWidth:= 0; |
47 | 268 |
for i:= 0 to cMaxHHIndex do |
269 |
if Hedgehogs[i].Gear <> nil then |
|
146 | 270 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear.Health); |
271 |
TeamHealth:= TeamHealthBarWidth; |
|
272 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 273 |
begin |
146 | 274 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 275 |
RecountAllTeamsHealth; |
146 | 276 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 277 |
end; |
105 | 278 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
49 | 279 |
AddGear(0, 0, gtTeamHealthSorter, 0) |
47 | 280 |
end; |
281 |
||
72 | 282 |
procedure RestoreTeamsFromSave; |
283 |
var p: PTeam; |
|
284 |
begin |
|
285 |
p:= TeamsList; |
|
286 |
while p <> nil do |
|
287 |
begin |
|
288 |
p.ExtDriven:= false; |
|
289 |
p:= p.Next |
|
290 |
end; |
|
291 |
end; |
|
292 |
||
165 | 293 |
procedure SetWeapon(weap: TAmmoType); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
294 |
var t: integer; |
165 | 295 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
296 |
t:= cMaxSlotAmmoIndex; |
165 | 297 |
with CurrentTeam^ do |
298 |
with Hedgehogs[CurrHedgehog] do |
|
255 | 299 |
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
|
300 |
begin |
165 | 301 |
ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot)); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
302 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
303 |
end |
165 | 304 |
end; |
305 |
||
4 | 306 |
initialization |
307 |
||
308 |
finalization |
|
309 |
||
310 |
FreeTeamsList |
|
311 |
||
312 |
end. |