4
+ − 1
(*
+ − 2
* Hedgewars, a worms-like game
47
+ − 3
* Copyright (c) 2004, 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
4
+ − 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 uWorld;
+ − 35
interface
+ − 36
uses SDLh, uGears;
+ − 37
{$INCLUDE options.inc}
+ − 38
const WorldDx: integer = -512;
+ − 39
WorldDy: integer = -256;
+ − 40
+ − 41
procedure InitWorld;
+ − 42
procedure DrawWorld(Lag: integer; Surface: PSDL_Surface);
+ − 43
procedure AddCaption(s: shortstring; Color, Group: LongWord);
+ − 44
procedure MoveWorld;
+ − 45
+ − 46
{$IFDEF COUNTTICKS}
+ − 47
var cntTicks: LongWord;
+ − 48
{$ENDIF}
+ − 49
var FollowGear: PGear = nil;
6
+ − 50
WindBarWidth: integer = 0;
4
+ − 51
+ − 52
implementation
+ − 53
uses uStore, uMisc, uConsts, uTeams, uIO;
+ − 54
const RealTicks: Longword = 0;
+ − 55
Frames: Longword = 0;
+ − 56
FPS: Longword = 0;
+ − 57
CountTicks: Longword = 0;
+ − 58
prevPoint: TPoint = (X: 0; Y: 0);
+ − 59
+ − 60
type TCaptionStr = record
+ − 61
r: TSDL_Rect;
+ − 62
StorePos,
+ − 63
Group,
+ − 64
EndTime: LongWord;
+ − 65
end;
+ − 66
+ − 67
var cWaterSprCount: integer;
+ − 68
Captions: array[0..Pred(cMaxCaptions)] of TCaptionStr;
+ − 69
+ − 70
procedure InitWorld;
+ − 71
begin
+ − 72
cLandYShift:= cWaterLine + 64;
74
+ − 73
cWaterSprCount:= 1 + cScreenWidth div (SpritesData[sprWater].Width);
+ − 74
cScreenEdgesDist:= Min(cScreenWidth div 4, cScreenHeight div 4);
+ − 75
SDL_WarpMouse(cScreenWidth div 2, cScreenHeight div 2);
+ − 76
prevPoint.X:= cScreenWidth div 2;
+ − 77
prevPoint.Y:= cScreenHeight div 2;
+ − 78
WorldDx:= - 1024 + cScreenWidth div 2;
+ − 79
WorldDy:= - 512 + cScreenHeight div 2
4
+ − 80
end;
+ − 81
+ − 82
procedure DrawWorld(Lag: integer; Surface: PSDL_Surface);
+ − 83
var i, t: integer;
+ − 84
r: TSDL_Rect;
+ − 85
team: PTeam;
74
+ − 86
tdx, tdy: real;
4
+ − 87
begin
5
+ − 88
// Sky
4
+ − 89
inc(RealTicks, Lag);
+ − 90
r.h:= WorldDy;
+ − 91
if r.h > 0 then
+ − 92
begin
+ − 93
if r.h > cScreenHeight then r.h:= cScreenHeight;
+ − 94
r.x:= 0;
+ − 95
r.y:= 0;
+ − 96
r.w:= cScreenWidth;
+ − 97
SDL_FillRect(Surface, @r, cSkyColor)
+ − 98
end;
5
+ − 99
// background
4
+ − 100
for i:= 0 to (cScreenWidth shr 6) do
+ − 101
DrawGear(sSky, i*64, WorldDy, Surface);
+ − 102
5
+ − 103
for i:= -1 to 3 do
4
+ − 104
DrawGear(sHorizont, i * 512 + (((WorldDx * 3) div 5) and $1FF), cWaterLine - 256 + WorldDy, Surface);
+ − 105
5
+ − 106
// Waves
4
+ − 107
{$WARNINGS OFF}
56
+ − 108
for i:= -1 to cWaterSprCount do DrawSprite(sprWater, i * 256 + ((WorldDx + (RealTicks shr 6) ) and $FF), cWaterLine + WorldDy - 64, 0, Surface);
+ − 109
for i:= -1 to cWaterSprCount do DrawSprite(sprWater, i * 256 + ((WorldDx - (RealTicks shr 6) + 192) and $FF), cWaterLine + WorldDy - 48, 0, Surface);
4
+ − 110
{$WARNINGS ON}
+ − 111
+ − 112
DrawLand(WorldDx, WorldDy, Surface);
5
+ − 113
// Water
4
+ − 114
r.y:= WorldDy + cWaterLine + 32;
+ − 115
if r.y < cScreenHeight then
+ − 116
begin
23
+ − 117
if r.y < 0 then r.y:= 0;
4
+ − 118
r.h:= cScreenHeight - r.y;
+ − 119
r.x:= 0;
+ − 120
r.w:= cScreenWidth;
+ − 121
SDL_FillRect(Surface, @r, cWaterColor)
+ − 122
end;
+ − 123
+ − 124
DrawGears(Surface);
+ − 125
+ − 126
team:= TeamsList;
+ − 127
while team<>nil do
+ − 128
begin
+ − 129
for i:= 0 to 7 do
+ − 130
with team.Hedgehogs[i] do
+ − 131
if Gear<>nil then
+ − 132
if Gear.State = 0 then
5
+ − 133
begin
4
+ − 134
DrawCaption( round(Gear.X) + WorldDx,
53
+ − 135
round(Gear.Y) - cHHRadius - 30 + WorldDy,
4
+ − 136
HealthRect, Surface, true);
+ − 137
DrawCaption( round(Gear.X) + WorldDx,
53
+ − 138
round(Gear.Y) - cHHRadius - 54 + WorldDy,
4
+ − 139
NameRect, Surface);
+ − 140
// DrawCaption( round(Gear.X) + WorldDx,
53
+ − 141
// round(Gear.Y) - Gear.Radius - 60 + WorldDy,
4
+ − 142
// Team.NameRect, Surface);
5
+ − 143
end else // Current hedgehog
4
+ − 144
begin
69
+ − 145
if (Gear.State and (gstMoving or gstDrowning or gstFalling)) = 0 then
4
+ − 146
if (Gear.State and gstHHThinking) <> 0 then
53
+ − 147
DrawGear(sQuestion, Round(Gear.X) - 10 + WorldDx, Round(Gear.Y) - cHHRadius - 34 + WorldDy, Surface)
4
+ − 148
else
69
+ − 149
if ShowCrosshair and ((Gear.State and gstAttacked) = 0) then
4
+ − 150
DrawCaption(Round(Gear.X + Sign(Gear.dX) * Sin(Gear.Angle*pi/cMaxAngle)*60) + WorldDx,
39
+ − 151
Round(Gear.Y - Cos(Gear.Angle*pi/cMaxAngle)*60) + WorldDy - 4,
4
+ − 152
Team.CrossHairRect, Surface)
+ − 153
end;
+ − 154
team:= team.Next
+ − 155
end;
+ − 156
5
+ − 157
// Waves
4
+ − 158
{$WARNINGS OFF}
56
+ − 159
for i:= -1 to cWaterSprCount do DrawSprite(sprWater, i * 256 + ((WorldDx + (RealTicks shr 6) + 64) and $FF), cWaterLine + WorldDy - 32, 0, Surface);
+ − 160
for i:= -1 to cWaterSprCount do DrawSprite(sprWater, i * 256 + ((WorldDx - (RealTicks shr 6) + 128) and $FF), cWaterLine + WorldDy - 16, 0, Surface);
+ − 161
for i:= -1 to cWaterSprCount do DrawSprite(sprWater, i * 256 + ((WorldDx + (RealTicks shr 6) ) and $FF), cWaterLine + WorldDy , 0, Surface);
4
+ − 162
{$WARNINGS ON}
+ − 163
+ − 164
if TurnTimeLeft <> 0 then
+ − 165
begin
+ − 166
i:= Succ(Pred(TurnTimeLeft) div 1000);
+ − 167
if i>99 then t:= 112
+ − 168
else if i>9 then t:= 96
+ − 169
else t:= 80;
+ − 170
DrawSprite(sprFrame, t, cScreenHeight - 48, 1, Surface);
+ − 171
while i > 0 do
+ − 172
begin
+ − 173
dec(t, 32);
+ − 174
DrawSprite(sprBigDigit, t, cScreenHeight - 48, i mod 10, Surface);
+ − 175
i:= i div 10
+ − 176
end;
+ − 177
DrawSprite(sprFrame, t - 4, cScreenHeight - 48, 0, Surface);
+ − 178
end;
+ − 179
if CurrentTeam <> nil then
+ − 180
case AttackBar of
+ − 181
1: begin
+ − 182
r:= StuffPoz[sPowerBar];
+ − 183
{$WARNINGS OFF}
+ − 184
r.w:= (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear.Power * 256) div cPowerDivisor;
+ − 185
{$WARNINGS ON}
+ − 186
DrawSpriteFromRect(r, cScreenWidth - 272, cScreenHeight - 48, 16, 0, Surface);
+ − 187
end;
74
+ − 188
2: with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
+ − 189
begin
+ − 190
tdx:= Sign(Gear.dX) * Sin(Gear.Angle*pi/cMaxAngle);
+ − 191
tdy:= - Cos(Gear.Angle*pi/cMaxAngle);
+ − 192
for i:= (Gear.Power * 24) div cPowerDivisor downto 0 do
+ − 193
DrawSprite(sprPower, round(Gear.X + WorldDx + tdx * (32 + i * 2)) - 16,
+ − 194
round(Gear.Y + WorldDy + tdy * (32 + i * 2)) - 12,
+ − 195
i, Surface)
+ − 196
end
4
+ − 197
end;
+ − 198
5
+ − 199
// Target
4
+ − 200
if TargetPoint.X <> NoPointX then DrawSprite(sprTargetP, TargetPoint.X + WorldDx - 16, TargetPoint.Y + WorldDy - 16, 0, Surface);
+ − 201
+ − 202
// Captions
+ − 203
i:= 0;
+ − 204
while (i < cMaxCaptions) do
+ − 205
begin
+ − 206
with Captions[i] do
+ − 207
if EndTime > 0 then DrawCaption(cScreenWidth div 2, 8 + i * 32 + cConsoleYAdd, r, Surface, true);
+ − 208
inc(i)
+ − 209
end;
+ − 210
while (Captions[0].EndTime > 0) and (Captions[0].EndTime <= RealTicks) do
+ − 211
begin
+ − 212
for i:= 1 to Pred(cMaxCaptions) do
+ − 213
Captions[Pred(i)]:= Captions[i];
+ − 214
Captions[Pred(cMaxCaptions)].EndTime:= 0
+ − 215
end;
+ − 216
47
+ − 217
// Teams Healths
+ − 218
team:= TeamsList;
+ − 219
while team <> nil do
+ − 220
begin
49
+ − 221
DrawFromStoreRect(cScreenWidth div 2 - team.NameRect.w - 3,
+ − 222
Team.DrawHealthY,
47
+ − 223
@team.NameRect, Surface);
+ − 224
r:= team.HealthRect;
+ − 225
r.w:= 3 + team.TeamHealth;
49
+ − 226
DrawFromStoreRect(cScreenWidth div 2,
+ − 227
Team.DrawHealthY,
47
+ − 228
@r, Surface);
+ − 229
inc(r.x, cTeamHealthWidth + 3);
+ − 230
r.w:= 2;
49
+ − 231
DrawFromStoreRect(cScreenWidth div 2 + team.TeamHealth + 3,
+ − 232
Team.DrawHealthY,
47
+ − 233
@r, Surface);
+ − 234
team:= team.Next
+ − 235
end;
+ − 236
5
+ − 237
// Lag alert
4
+ − 238
if isInLag then DrawSprite(sprLag, 32, 32 + cConsoleYAdd, (RealTicks shr 7) mod 7, Surface);
+ − 239
5
+ − 240
// Wind bar
+ − 241
DrawGear(sWindBar, cScreenWidth - 180, cScreenHeight - 30, Surface);
6
+ − 242
if WindBarWidth > 0 then
5
+ − 243
begin
+ − 244
with StuffPoz[sWindR] do
+ − 245
begin
6
+ − 246
{$WARNINGS OFF}
5
+ − 247
r.x:= x + 8 - (RealTicks shr 6) mod 8;
6
+ − 248
{$WARNINGS ON}
5
+ − 249
r.y:= y;
6
+ − 250
r.w:= WindBarWidth;
5
+ − 251
r.h:= 13;
+ − 252
end;
+ − 253
DrawSpriteFromRect(r, cScreenWidth - 103, cScreenHeight - 28, 13, 0, Surface);
+ − 254
end else
6
+ − 255
if WindBarWidth < 0 then
5
+ − 256
begin
+ − 257
with StuffPoz[sWindL] do
+ − 258
begin
6
+ − 259
{$WARNINGS OFF}
+ − 260
r.x:= x + (WindBarWidth + RealTicks shr 6) mod 8;
+ − 261
{$WARNINGS ON}
5
+ − 262
r.y:= y;
6
+ − 263
r.w:= - WindBarWidth;
5
+ − 264
r.h:= 13;
+ − 265
end;
6
+ − 266
DrawSpriteFromRect(r, cScreenWidth - 106 + WindBarWidth, cScreenHeight - 28, 13, 0, Surface);
5
+ − 267
end;
+ − 268
+ − 269
// Cursor
4
+ − 270
if isCursorVisible then DrawSprite(sprArrow, CursorPoint.X, CursorPoint.Y, (RealTicks shr 6) mod 8, Surface);
+ − 271
+ − 272
{$IFDEF COUNTTICKS}
+ − 273
DXOutText(10, 10, fnt16, inttostr(cntTicks), Surface);
+ − 274
{$ENDIF}
+ − 275
+ − 276
inc(Frames);
+ − 277
inc(CountTicks, Lag);
+ − 278
if CountTicks >= 1000 then
+ − 279
begin
+ − 280
FPS:= Frames;
+ − 281
Frames:= 0;
+ − 282
CountTicks:= 0;
+ − 283
end;
+ − 284
if cShowFPS then DXOutText(cScreenWidth - 50, 10, fnt16, inttostr(FPS) + ' fps', Surface)
+ − 285
end;
+ − 286
+ − 287
procedure AddCaption(s: shortstring; Color, Group: LongWord);
+ − 288
var i, t, m, k: LongWord;
+ − 289
begin
+ − 290
i:= 0;
+ − 291
while (i < cMaxCaptions) and (Captions[i].Group <> Group)do inc(i);
+ − 292
if i < cMaxCaptions then
+ − 293
begin
+ − 294
while (i < Pred(cMaxCaptions)) do
+ − 295
begin
+ − 296
Captions[i]:= Captions[Succ(i)];
+ − 297
inc(i)
+ − 298
end;
+ − 299
Captions[Pred(cMaxCaptions)].EndTime:= 0
+ − 300
end;
+ − 301
+ − 302
if Captions[Pred(cMaxCaptions)].EndTime > 0 then
+ − 303
begin
+ − 304
m:= Pred(cMaxCaptions);
+ − 305
for i:= 1 to m do
+ − 306
Captions[Pred(i)]:= Captions[i];
+ − 307
Captions[m].EndTime:= 0
+ − 308
end else
+ − 309
begin
+ − 310
m:= 0;
+ − 311
while (m < cMaxCaptions)and(Captions[m].EndTime > 0) do inc(m)
+ − 312
end;
+ − 313
+ − 314
k:= 0;
+ − 315
for i:= 0 to Pred(cMaxCaptions) do
+ − 316
for t:= 0 to Pred(cMaxCaptions) do
+ − 317
if (Captions[t].EndTime > 0)and(Captions[t].StorePos = k) then inc(k);
+ − 318
+ − 319
Captions[m].r:= RenderString(s, Color, k);
+ − 320
Captions[m].StorePos:= k;
+ − 321
Captions[m].Group:= Group;
+ − 322
Captions[m].EndTime:= RealTicks + 1200
+ − 323
end;
+ − 324
+ − 325
procedure MoveWorld;
+ − 326
const PrevSentPointTime: LongWord = 0;
+ − 327
var s: string[9];
+ − 328
begin
+ − 329
if not (CurrentTeam.ExtDriven and isCursorVisible) then SDL_GetMouseState(@CursorPoint.X, @CursorPoint.Y);
+ − 330
if (FollowGear <> nil) then
57
+ − 331
if abs(CursorPoint.X - prevPoint.X) + abs(CursorPoint.Y - prevpoint.Y) > 4 then
4
+ − 332
begin
+ − 333
FollowGear:= nil;
+ − 334
exit
+ − 335
end
+ − 336
else begin
71
+ − 337
CursorPoint.x:= (CursorPoint.x * 7 + (round(FollowGear.X + Sign(FollowGear.dX) * 100) + WorldDx)) div 8;
+ − 338
CursorPoint.y:= (CursorPoint.y * 7 + (round(FollowGear.Y) + WorldDy)) div 8
4
+ − 339
end;
+ − 340
+ − 341
if ((CursorPoint.X = prevPoint.X)and(CursorPoint.Y = prevpoint.Y)) then exit;
+ − 342
+ − 343
if isCursorVisible then
+ − 344
begin
+ − 345
if (not CurrentTeam.ExtDriven)and(GameTicks >= PrevSentPointTime + cSendCursorPosTime) then
+ − 346
begin
+ − 347
s[0]:= #9;
+ − 348
s[1]:= 'P';
+ − 349
PInteger(@s[2])^:= CursorPoint.X - WorldDx;
+ − 350
PInteger(@s[6])^:= CursorPoint.Y - WorldDy;
+ − 351
SendIPC(s);
+ − 352
PrevSentPointTime:= GameTicks
+ − 353
end;
+ − 354
end;
+ − 355
if isCursorVisible or (FollowGear <> nil) then
+ − 356
begin
+ − 357
if CursorPoint.X < cScreenEdgesDist then
+ − 358
begin
+ − 359
WorldDx:= WorldDx - CursorPoint.X + cScreenEdgesDist;
+ − 360
CursorPoint.X:= cScreenEdgesDist
+ − 361
end else
+ − 362
if CursorPoint.X > cScreenWidth - cScreenEdgesDist then
+ − 363
begin
+ − 364
WorldDx:= WorldDx - CursorPoint.X + cScreenWidth - cScreenEdgesDist;
+ − 365
CursorPoint.X:= cScreenWidth - cScreenEdgesDist
+ − 366
end;
+ − 367
if CursorPoint.Y < cScreenEdgesDist then
+ − 368
begin
+ − 369
WorldDy:= WorldDy - CursorPoint.Y + cScreenEdgesDist;
+ − 370
CursorPoint.Y:= cScreenEdgesDist
+ − 371
end else
+ − 372
if CursorPoint.Y > cScreenHeight - cScreenEdgesDist then
+ − 373
begin
+ − 374
WorldDy:= WorldDy - CursorPoint.Y + cScreenHeight - cScreenEdgesDist;
+ − 375
CursorPoint.Y:= cScreenHeight - cScreenEdgesDist
+ − 376
end;
+ − 377
end else
+ − 378
begin
70
+ − 379
WorldDx:= WorldDx - CursorPoint.X + prevPoint.X;
+ − 380
WorldDy:= WorldDy - CursorPoint.Y + prevPoint.Y;
4
+ − 381
CursorPoint.X:= (cScreenWidth shr 1);
+ − 382
CursorPoint.Y:= (cScreenHeight shr 1);
+ − 383
end;
+ − 384
SDL_WarpMouse(CursorPoint.X, CursorPoint.Y);
+ − 385
prevPoint:= CursorPoint;
+ − 386
if WorldDy < cScreenHeight - cLandYShift - cVisibleWater then WorldDy:= cScreenHeight - cLandYShift - cVisibleWater;
+ − 387
if WorldDy > 2048 then WorldDy:= 2048;
+ − 388
if WorldDx < -2048 then WorldDx:= -2048;
+ − 389
if WorldDx > cScreenWidth then WorldDx:= cScreenWidth;
+ − 390
end;
+ − 391
+ − 392
initialization
+ − 393
FillChar(Captions, sizeof(Captions), 0)
+ − 394
+ − 395
end.