author | unc0rr |
Mon, 02 Jul 2007 21:33:21 +0000 | |
changeset 547 | b81a055f2d06 |
parent 540 | b06c5aace2fa |
child 548 | ac1e32b538fd |
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; |
83 | 88 |
begin |
547 | 89 |
if TeamsCount >= 2 then exit(false); |
351 | 90 |
CheckForWin:= true; |
83 | 91 |
|
92 |
TurnTimeLeft:= 0; |
|
547 | 93 |
if TeamsCount = 0 then |
83 | 94 |
begin // draw |
95 |
AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState); |
|
306 | 96 |
SendStat(siGameResult, trmsg[sidDraw]); |
498 | 97 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 98 |
end else // win |
99 |
begin |
|
547 | 100 |
s:= Format(trmsg[sidWinner], TeamsArray[0]^.TeamName); |
306 | 101 |
AddCaption(s, $FFFFFF, capgrpGameState); |
102 |
SendStat(siGameResult, s); |
|
498 | 103 |
AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000) |
83 | 104 |
end; |
307 | 105 |
SendStats |
83 | 106 |
end; |
107 |
||
4 | 108 |
procedure SwitchHedgehog; |
547 | 109 |
var th: LongInt; |
110 |
t: LongWord; |
|
351 | 111 |
g: PGear; |
4 | 112 |
begin |
113 |
FreeActionsList; |
|
114 |
TargetPoint.X:= NoPointX; |
|
89 | 115 |
TryDo(CurrentTeam <> nil, 'nil Team', true); |
547 | 116 |
|
351 | 117 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
294 | 118 |
if Gear <> nil then |
119 |
begin |
|
540 | 120 |
AttacksNum:= 0; |
351 | 121 |
Gear^.Message:= 0; |
534 | 122 |
Gear^.Z:= cHHZ; |
123 |
RemoveGearFromList(Gear); |
|
124 |
InsertGearToList(Gear) |
|
294 | 125 |
end; |
4 | 126 |
|
547 | 127 |
t:= 0; |
128 |
while CurrentTeam <> TeamsArray[t] do inc(t); |
|
129 |
CurrentTeam:= TeamsArray[(t + 1) mod TeamsCount]; |
|
130 |
||
131 |
th:= CurrentTeam^.CurrHedgehog; |
|
4 | 132 |
repeat |
547 | 133 |
CurrentTeam^.CurrHedgehog:= Succ(CurrentTeam^.CurrHedgehog) mod (cMaxHHIndex + 1); |
134 |
until (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].Gear <> nil) or (CurrentTeam^.CurrHedgehog = th); |
|
83 | 135 |
|
351 | 136 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
4 | 137 |
begin |
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; |
|
351 | 149 |
|
150 |
cWindSpeed:= rndSign(GetRandom * cMaxWindSpeed); |
|
498 | 151 |
g:= AddGear(0, 0, gtATSmoothWindCh, 0, _0, _0, 1); |
351 | 152 |
g^.Tag:= hwRound(cWindSpeed * 72 / cMaxWindSpeed); |
4 | 153 |
{$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF} |
351 | 154 |
ApplyAmmoChanges(CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog]); |
155 |
if CurrentTeam^.ExtDriven then SetDefaultBinds |
|
156 |
else SetBinds(CurrentTeam^.Binds); |
|
176 | 157 |
bShowFinger:= true; |
4 | 158 |
TurnTimeLeft:= cHedgehogTurnTime |
159 |
end; |
|
160 |
||
161 |
function AddTeam: PTeam; |
|
351 | 162 |
var Result: PTeam; |
4 | 163 |
begin |
547 | 164 |
TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true); |
17 | 165 |
New(Result); |
80 | 166 |
TryDo(Result <> nil, 'AddTeam: Result = nil', true); |
4 | 167 |
FillChar(Result^, sizeof(TTeam), 0); |
351 | 168 |
Result^.AttackBar:= 2; |
169 |
Result^.CurrHedgehog:= cMaxHHIndex; |
|
547 | 170 |
|
171 |
TeamsArray[TeamsCount]:= Result; |
|
172 |
inc(TeamsCount); |
|
173 |
||
351 | 174 |
CurrentTeam:= Result; |
175 |
AddTeam:= Result |
|
4 | 176 |
end; |
177 |
||
178 |
procedure FreeTeamsList; |
|
547 | 179 |
var t: LongInt; |
4 | 180 |
begin |
547 | 181 |
for t:= 0 to Pred(TeamsCount) do Dispose(TeamsArray[t]); |
182 |
TeamsCount:= 0 |
|
4 | 183 |
end; |
184 |
||
47 | 185 |
procedure RecountAllTeamsHealth; |
547 | 186 |
var t: LongInt; |
187 |
begin |
|
188 |
for t:= 0 to Pred(TeamsCount) do |
|
189 |
RecountTeamHealth(TeamsArray[t]) |
|
47 | 190 |
end; |
191 |
||
192 |
procedure InitTeams; |
|
547 | 193 |
var i, t: LongInt; |
371 | 194 |
th: LongInt; |
47 | 195 |
begin |
547 | 196 |
for t:= 0 to Pred(TeamsCount) do |
197 |
with TeamsArray[t]^ do |
|
47 | 198 |
begin |
199 |
th:= 0; |
|
4 | 200 |
for i:= 0 to cMaxHHIndex do |
547 | 201 |
if Hedgehogs[i].Gear <> nil then |
202 |
inc(th, Hedgehogs[i].Gear^.Health); |
|
47 | 203 |
if th > MaxTeamHealth then MaxTeamHealth:= th; |
4 | 204 |
end; |
47 | 205 |
RecountAllTeamsHealth |
4 | 206 |
end; |
207 |
||
70 | 208 |
procedure ApplyAmmoChanges(var Hedgehog: THedgehog); |
4 | 209 |
var s: shortstring; |
210 |
begin |
|
162 | 211 |
TargetPoint.X:= NoPointX; |
212 |
||
70 | 213 |
with Hedgehog do |
47 | 214 |
begin |
351 | 215 |
if Ammo^[CurSlot, CurAmmo].Count = 0 then |
4 | 216 |
begin |
217 |
CurAmmo:= 0; |
|
288 | 218 |
CurSlot:= 0; |
351 | 219 |
while (CurSlot <= cMaxSlotIndex) and (Ammo^[CurSlot, CurAmmo].Count = 0) do inc(CurSlot) |
4 | 220 |
end; |
221 |
||
351 | 222 |
with Ammo^[CurSlot, CurAmmo] do |
4 | 223 |
begin |
304 | 224 |
CurMinAngle:= Ammoz[AmmoType].minAngle; |
225 |
if Ammoz[AmmoType].maxAngle <> 0 then CurMaxAngle:= Ammoz[AmmoType].maxAngle |
|
226 |
else CurMaxAngle:= cMaxAngle; |
|
227 |
with Hedgehog.Gear^ do |
|
228 |
begin |
|
229 |
if Angle < CurMinAngle then Angle:= CurMinAngle; |
|
230 |
if Angle > CurMaxAngle then Angle:= CurMaxAngle; |
|
231 |
end; |
|
351 | 232 |
|
80 | 233 |
s:= trammo[Ammoz[AmmoType].NameId]; |
4 | 234 |
if Count <> AMMO_INFINITE then |
235 |
s:= s + ' (' + IntToStr(Count) + ')'; |
|
236 |
if (Propz and ammoprop_Timerable) <> 0 then |
|
83 | 237 |
s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds]; |
351 | 238 |
AddCaption(s, Team^.Color, capgrpAmmoinfo); |
4 | 239 |
if (Propz and ammoprop_NeedTarget) <> 0 |
240 |
then begin |
|
351 | 241 |
Gear^.State:= Gear^.State or gstHHChooseTarget; |
4 | 242 |
isCursorVisible:= true |
243 |
end else begin |
|
351 | 244 |
Gear^.State:= Gear^.State and not gstHHChooseTarget; |
4 | 245 |
isCursorVisible:= false |
13 | 246 |
end; |
247 |
ShowCrosshair:= (Propz and ammoprop_NoCrosshair) = 0 |
|
4 | 248 |
end |
249 |
end |
|
250 |
end; |
|
251 |
||
252 |
function TeamSize(p: PTeam): Longword; |
|
351 | 253 |
var i, Result: Longword; |
4 | 254 |
begin |
255 |
Result:= 0; |
|
256 |
for i:= 0 to cMaxHHIndex do |
|
351 | 257 |
if p^.Hedgehogs[i].Gear <> nil then inc(Result); |
258 |
TeamSize:= Result |
|
4 | 259 |
end; |
260 |
||
47 | 261 |
procedure RecountTeamHealth(team: PTeam); |
371 | 262 |
var i: LongInt; |
47 | 263 |
begin |
264 |
with team^ do |
|
265 |
begin |
|
146 | 266 |
TeamHealthBarWidth:= 0; |
47 | 267 |
for i:= 0 to cMaxHHIndex do |
268 |
if Hedgehogs[i].Gear <> nil then |
|
351 | 269 |
inc(TeamHealthBarWidth, Hedgehogs[i].Gear^.Health); |
146 | 270 |
TeamHealth:= TeamHealthBarWidth; |
271 |
if TeamHealthBarWidth > MaxTeamHealth then |
|
47 | 272 |
begin |
146 | 273 |
MaxTeamHealth:= TeamHealthBarWidth; |
47 | 274 |
RecountAllTeamsHealth; |
146 | 275 |
end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth |
49 | 276 |
end; |
105 | 277 |
// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously |
498 | 278 |
AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0) |
47 | 279 |
end; |
280 |
||
72 | 281 |
procedure RestoreTeamsFromSave; |
547 | 282 |
var t: LongInt; |
72 | 283 |
begin |
547 | 284 |
for t:= 0 to Pred(TeamsCount) do |
285 |
TeamsArray[t]^.ExtDriven:= false |
|
72 | 286 |
end; |
287 |
||
165 | 288 |
procedure SetWeapon(weap: TAmmoType); |
371 | 289 |
var t: LongInt; |
165 | 290 |
begin |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
291 |
t:= cMaxSlotAmmoIndex; |
165 | 292 |
with CurrentTeam^ do |
293 |
with Hedgehogs[CurrHedgehog] do |
|
351 | 294 |
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
|
295 |
begin |
351 | 296 |
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
|
297 |
dec(t) |
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
198
diff
changeset
|
298 |
end |
165 | 299 |
end; |
300 |
||
307 | 301 |
procedure SendStats; |
547 | 302 |
var i, t: LongInt; |
307 | 303 |
msd: Longword; msdhh: PHedgehog; |
304 |
begin |
|
305 |
msd:= 0; msdhh:= nil; |
|
547 | 306 |
for t:= 0 to Pred(TeamsCount) do |
307 |
with TeamsArray[t]^ do |
|
307 | 308 |
begin |
309 |
for i:= 0 to cMaxHHIndex do |
|
547 | 310 |
if Hedgehogs[i].MaxStepDamage > msd then |
307 | 311 |
begin |
547 | 312 |
msdhh:= @Hedgehogs[i]; |
313 |
msd:= Hedgehogs[i].MaxStepDamage |
|
307 | 314 |
end; |
315 |
end; |
|
351 | 316 |
if msdhh <> nil then SendStat(siMaxStepDamage, inttostr(msdhh^.MaxStepDamage) + ' ' + |
317 |
msdhh^.Name + ' (' + msdhh^.Team^.TeamName + ')'); |
|
307 | 318 |
if KilledHHs > 0 then SendStat(siKilledHHs, inttostr(KilledHHs)); |
319 |
end; |
|
320 |
||
4 | 321 |
initialization |
322 |
||
323 |
finalization |
|
324 |
||
325 |
FreeTeamsList |
|
326 |
||
327 |
end. |