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