author | unc0rr |
Tue, 03 Jul 2007 14:47:33 +0000 | |
changeset 548 | ac1e32b538fd |
parent 547 | b81a055f2d06 |
child 549 | 4278a80140a8 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2004-2007 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 |
|
534 | 21 |
uses SDLh, uConsts, uKeys, uGears, uRandom, uFloat; |
4 | 22 |
{$INCLUDE options.inc} |
534 | 23 |
|
24 |
type PHHAmmo = ^THHAmmo; |
|
25 |
THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo; |
|
26 |
||
4 | 27 |
type PHedgehog = ^THedgehog; |
28 |
PTeam = ^TTeam; |
|
29 |
THedgehog = record |
|
74 | 30 |
Name: string[MAXNAMELEN]; |
4 | 31 |
Gear: PGear; |
95 | 32 |
NameTag, HealthTag: PSDL_Surface; |
4 | 33 |
Ammo: PHHAmmo; |
288 | 34 |
AmmoStore: Longword; |
4 | 35 |
CurSlot, CurAmmo: LongWord; |
36 |
AltSlot, AltAmmo: LongWord; |
|
37 |
Team: PTeam; |
|
38 |
AttacksNum: Longword; |
|
39 |
visStepPos: LongWord; |
|
5 | 40 |
BotLevel : LongWord; // 0 - Human player |
307 | 41 |
DamageGiven: Longword; |
42 |
MaxStepDamage: Longword; |
|
4 | 43 |
end; |
44 |
TTeam = record |
|
189 | 45 |
Color, AdjColor: Longword; |
74 | 46 |
TeamName: string[MAXNAMELEN]; |
4 | 47 |
ExtDriven: boolean; |
167 | 48 |
Binds: TBinds; |
4 | 49 |
Hedgehogs: array[0..cMaxHHIndex] of THedgehog; |
371 | 50 |
CurrHedgehog: LongInt; |
95 | 51 |
NameTag: PSDL_Surface; |
198 | 52 |
CrosshairSurf: PSDL_Surface; |
47 | 53 |
GraveRect, HealthRect: TSDL_Rect; |
4 | 54 |
GraveName: string; |
55 |
FortName: string; |
|
371 | 56 |
TeamHealth: LongInt; |
57 |
TeamHealthBarWidth: LongInt; |
|
58 |
DrawHealthY: LongInt; |
|
4 | 59 |
AttackBar: LongWord; |
312 | 60 |
HedgehogsNumber: byte; |
4 | 61 |
end; |
62 |
||
63 |
var CurrentTeam: PTeam = nil; |
|
547 | 64 |
TeamsArray: array[0..Pred(cMaxTeams)] of PTeam; |
65 |
TeamsCount: Longword = 0; |
|
304 | 66 |
CurMinAngle, CurMaxAngle: Longword; |
4 | 67 |
|
68 |
function AddTeam: PTeam; |
|
70 | 69 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 70 |
procedure SwitchHedgehog; |
71 |
procedure InitTeams; |
|
72 |
function TeamSize(p: PTeam): Longword; |
|
47 | 73 |
procedure RecountTeamHealth(team: PTeam); |
72 | 74 |
procedure RestoreTeamsFromSave; |
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
75 |
function CheckForWin: boolean; |
165 | 76 |
procedure SetWeapon(weap: TAmmoType); |
307 | 77 |
procedure SendStats; |
4 | 78 |
|
79 |
implementation |
|
534 | 80 |
uses uMisc, uWorld, uAI, uLocale, uConsole, uAmmos; |
371 | 81 |
const MaxTeamHealth: LongInt = 0; |
4 | 82 |
|
83 |
procedure FreeTeamsList; forward; |
|
84 |
||
92
0c359a7a2356
- Fix win message to appear only after all hedgehogs death
unc0rr
parents:
89
diff
changeset
|
85 |
function CheckForWin: boolean; |
83 | 86 |
var team, AliveTeam: PTeam; |
306 | 87 |
s: shortstring; |
548 | 88 |
t, AliveCount: LongInt; |
83 | 89 |
begin |
548 | 90 |
AliveCount:= 0; |
91 |
for t:= 0 to Pred(TeamsCount) do |
|
92 |
if TeamsArray[t]^.TeamHealth > 0 then |
|
93 |
begin |
|
94 |
inc(AliveCount); |
|
95 |
AliveTeam:= TeamsArray[t] |
|
96 |
end; |
|
97 |
||
98 |
if AliveCount >= 2 then exit(false); |
|
351 | 99 |
CheckForWin:= true; |
83 | 100 |
|
101 |
TurnTimeLeft:= 0; |
|
548 | 102 |
if AliveCount = 0 then |
83 | 103 |
begin // draw |
104 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
306 | 105 |
SendStat(siGameResult, trmsg[sidDraw]); |
498 | 106 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 107 |
end else // win |
108 |
begin |
|
547 | 109 |
s:= Format(trmsg[sidWinner], TeamsArray[0]^.TeamName); |
306 | 110 |
AddCaption(s, $FFFFFF, capgrpGameState); |
111 |
SendStat(siGameResult, s); |
|
498 | 112 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 113 |
end; |
307 | 114 |
SendStats |
83 | 115 |
end; |
116 |
||
4 | 117 |
procedure SwitchHedgehog; |
547 | 118 |
var th: LongInt; |
119 |
t: LongWord; |
|
351 | 120 |
g: PGear; |
4 | 121 |
begin |
122 |
FreeActionsList; |
|
123 |
TargetPoint.X:= NoPointX; |
|
89 | 124 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
547 | 125 |
|
351 | 126 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
294 | 127 |
if Gear <> nil then |
128 |
begin |
|
540 | 129 |
AttacksNum:= 0; |
351 | 130 |
Gear^.Message:= 0; |
534 | 131 |
Gear^.Z:= cHHZ; |
132 |
RemoveGearFromList(Gear); |
|
133 |
InsertGearToList(Gear) |
|
294 | 134 |
end; |
4 | 135 |
|
547 | 136 |
t:= 0; |
137 |
while CurrentTeam <> TeamsArray[t] do inc(t); |
|
138 |
CurrentTeam:= TeamsArray[(t + 1) mod TeamsCount]; |
|
139 |
||
140 |
th:= CurrentTeam^.CurrHedgehog; |
|
4 | 141 |
repeat |
547 | 142 |
CurrentTeam^.CurrHedgehog:= Succ(CurrentTeam^.CurrHedgehog) mod (cMaxHHIndex + 1); |
143 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (CurrentTeam^.CurrHedgehog = th); |
|
83 | 144 |
|
351 | 145 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
4 | 146 |
begin |
147 |
with Gear^ do |
|
148 |
begin |
|
294 | 149 |
Z:= cCurrHHZ; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
17
diff
changeset
|
150 |
State:= gstHHDriven; |
4 | 151 |
Active:= true |
152 |
end; |
|
294 | 153 |
RemoveGearFromList(Gear); |
154 |
InsertGearToList(Gear); |
|
4 | 155 |
FollowGear:= Gear |
156 |
end; |
|
157 |
ResetKbd; |
|
351 | 158 |
|
159 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
498 | 160 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, _0, _0, 1); |
351 | 161 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
4 | 162 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
351 | 163 |
ApplyAmmoChanges(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
164 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
|
165 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 166 |
bShowFinger:= true; |
4 | 167 |
TurnTimeLeft:= cHedgehogTurnTime |
168 |
end; |
|
169 |
||
170 |
function AddTeam: PTeam; |
|
351 | 171 |
var Result: PTeam; |
4 | 172 |
begin |
547 | 173 |
TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true); |
17 | 174 |
New(Result); |
80 | 175 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 176 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 177 |
Result^.AttackBar:= 2; |
178 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
547 | 179 |
|
180 |
TeamsArray[TeamsCount]:= Result; |
|
181 |
inc(TeamsCount); |
|
182 |
||
351 | 183 |
CurrentTeam:= Result; |
184 |
AddTeam:= Result |
|
4 | 185 |
end; |
186 |
||
187 |
procedure FreeTeamsList; |
|
547 | 188 |
var t: LongInt; |
4 | 189 |
begin |
547 | 190 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
191 |
TeamsCount:= 0 |
|
4 | 192 |
end; |
193 |
||
47 | 194 |
procedure RecountAllTeamsHealth; |
547 | 195 |
var t: LongInt; |
196 |
begin |
|
197 |
for t:= 0 to Pred(TeamsCount) do |
|
198 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 199 |
end; |
200 |
||
201 |
procedure InitTeams; |
|
547 | 202 |
var i, t: LongInt; |
371 | 203 |
th: LongInt; |
47 | 204 |
begin |
547 | 205 |
for t:= 0 to Pred(TeamsCount) do |
206 |
with TeamsArray[t]^ do |
|
47 | 207 |
begin |
208 |
th:= 0; |
|
4 | 209 |
for i:= 0 to cMaxHHIndex do |
547 | 210 |
if Hedgehogs[i].Gear <> nil then |
211 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 212 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 213 |
end; |
47 | 214 |
RecountAllTeamsHealth |
4 | 215 |
end; |
216 |
||
70 | 217 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 218 |
var s: shortstring; |
219 |
begin |
|
162 | 220 |
TargetPoint.X:= NoPointX; |
221 |
||
70 | 222 |
with Hedgehog do |
47 | 223 |
begin |
351 | 224 |
if Ammo^[CurSlot, CurAmmo].Count = 0 then |
4 | 225 |
begin |
226 |
CurAmmo:= 0; |
|
288 | 227 |
CurSlot:= 0; |
351 | 228 |
while (CurSlot <= cMaxSlotIndex) and (Ammo^[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
4 | 229 |
end; |
230 |
||
351 | 231 |
with Ammo^[CurSlot, CurAmmo] do |
4 | 232 |
begin |
304 | 233 |
CurMinAngle:= Ammoz[AmmoType].minAngle; |
234 |
if Ammoz[AmmoType].maxAngle <> 0 then CurMaxAngle:= Ammoz[AmmoType].maxAngle |
|
235 |
else CurMaxAngle:= cMaxAngle; |
|
236 |
with Hedgehog.Gear^ do |
|
237 |
begin |
|
238 |
if Angle < CurMinAngle then Angle:= CurMinAngle; |
|
239 |
if Angle > CurMaxAngle then Angle:= CurMaxAngle; |
|
240 |
end; |
|
351 | 241 |
|
80 | 242 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 243 |
if Count <> AMMO_INFINITE then |
244 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
245 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 246 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
351 | 247 |
AddCaption(s, Team^.Color, capgrpAmmoinfo); |
4 | 248 |
if (Propz and ammoprop_NeedTarget) <> 0 |
249 |
then begin |
|
351 | 250 |
Gear^.State:= Gear^.State or gstHHChooseTarget; |
4 | 251 |
isCursorVisible:= true |
252 |
end else begin |
|
351 | 253 |
Gear^.State:= Gear^.State and not gstHHChooseTarget; |
4 | 254 |
isCursorVisible:= false |
13 | 255 |
end; |
256 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 257 |
end |
258 |
end |
|
259 |
end; |
|
260 |
||
261 |
function TeamSize(p: PTeam): Longword; |
|
351 | 262 |
var i, Result: Longword; |
4 | 263 |
begin |
264 |
Result:= 0; |
|
265 |
for i:= 0 to cMaxHHIndex do |
|
351 | 266 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
267 |
TeamSize:= Result |
|
4 | 268 |
end; |
269 |
||
47 | 270 |
procedure RecountTeamHealth(team: PTeam); |
371 | 271 |
var i: LongInt; |
47 | 272 |
begin |
273 |
with team^ do |
|
274 |
begin |
|
146 | 275 |
TeamHealthBarWidth:= 0; |
47 | 276 |
for i:= 0 to cMaxHHIndex do |
277 |
if Hedgehogs[i].Gear <> nil then |
|
351 | 278 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
146 | 279 |
TeamHealth:= TeamHealthBarWidth; |
280 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 281 |
begin |
146 | 282 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 283 |
RecountAllTeamsHealth; |
146 | 284 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 285 |
end; |
105 | 286 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
498 | 287 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 288 |
end; |
289 |
||
72 | 290 |
procedure RestoreTeamsFromSave; |
547 | 291 |
var t: LongInt; |
72 | 292 |
begin |
547 | 293 |
for t:= 0 to Pred(TeamsCount) do |
294 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 295 |
end; |
296 |
||
165 | 297 |
procedure SetWeapon(weap: TAmmoType); |
371 | 298 |
var t: LongInt; |
165 | 299 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
300 |
t:= cMaxSlotAmmoIndex; |
165 | 301 |
with CurrentTeam^ do |
302 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 303 |
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
|
304 |
begin |
351 | 305 |
ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot), true); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
306 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
307 |
end |
165 | 308 |
end; |
309 |
||
307 | 310 |
procedure SendStats; |
547 | 311 |
var i, t: LongInt; |
307 | 312 |
msd: Longword; msdhh: PHedgehog; |
313 |
begin |
|
314 |
msd:= 0; msdhh:= nil; |
|
547 | 315 |
for t:= 0 to Pred(TeamsCount) do |
316 |
with TeamsArray[t]^ do |
|
307 | 317 |
begin |
318 |
for i:= 0 to cMaxHHIndex do |
|
547 | 319 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 320 |
begin |
547 | 321 |
msdhh:= @Hedgehogs[i]; |
322 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 323 |
end; |
324 |
end; |
|
351 | 325 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
326 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 327 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
328 |
end; |
|
329 |
||
4 | 330 |
initialization |
331 |
||
332 |
finalization |
|
333 |
||
334 |
FreeTeamsList |
|
335 |
||
336 |
end. |