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
|
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 |
|
|
163 |
if TurnTimeLeft <> 0 then
|
|
164 |
begin
|
|
165 |
i:= Succ(Pred(TurnTimeLeft) div 1000);
|
|
166 |
if i>99 then t:= 112
|
|
167 |
else if i>9 then t:= 96
|
|
168 |
else t:= 80;
|
|
169 |
DrawSprite(sprFrame, t, cScreenHeight - 48, 1, Surface);
|
|
170 |
while i > 0 do
|
|
171 |
begin
|
|
172 |
dec(t, 32);
|
|
173 |
DrawSprite(sprBigDigit, t, cScreenHeight - 48, i mod 10, Surface);
|
|
174 |
i:= i div 10
|
|
175 |
end;
|
|
176 |
DrawSprite(sprFrame, t - 4, cScreenHeight - 48, 0, Surface);
|
|
177 |
end;
|
|
178 |
if CurrentTeam <> nil then
|
|
179 |
case AttackBar of
|
|
180 |
1: begin
|
|
181 |
r:= StuffPoz[sPowerBar];
|
|
182 |
{$WARNINGS OFF}
|
|
183 |
r.w:= (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear.Power * 256) div cPowerDivisor;
|
|
184 |
{$WARNINGS ON}
|
|
185 |
DrawSpriteFromRect(r, cScreenWidth - 272, cScreenHeight - 48, 16, 0, Surface);
|
|
186 |
end;
|
74
|
187 |
2: with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
|
|
188 |
begin
|
|
189 |
tdx:= Sign(Gear.dX) * Sin(Gear.Angle*pi/cMaxAngle);
|
|
190 |
tdy:= - Cos(Gear.Angle*pi/cMaxAngle);
|
|
191 |
for i:= (Gear.Power * 24) div cPowerDivisor downto 0 do
|
75
|
192 |
DrawSprite(sprPower, round(Gear.X + WorldDx + tdx * (24 + i * 2)) - 16,
|
|
193 |
round(Gear.Y + WorldDy + tdy * (24 + i * 2)) - 12,
|
74
|
194 |
i, Surface)
|
|
195 |
end
|
4
|
196 |
end;
|
|
197 |
|
5
|
198 |
// Target
|
4
|
199 |
if TargetPoint.X <> NoPointX then DrawSprite(sprTargetP, TargetPoint.X + WorldDx - 16, TargetPoint.Y + WorldDy - 16, 0, Surface);
|
|
200 |
|
|
201 |
// Captions
|
|
202 |
i:= 0;
|
|
203 |
while (i < cMaxCaptions) do
|
|
204 |
begin
|
|
205 |
with Captions[i] do
|
|
206 |
if EndTime > 0 then DrawCaption(cScreenWidth div 2, 8 + i * 32 + cConsoleYAdd, r, Surface, true);
|
|
207 |
inc(i)
|
|
208 |
end;
|
|
209 |
while (Captions[0].EndTime > 0) and (Captions[0].EndTime <= RealTicks) do
|
|
210 |
begin
|
|
211 |
for i:= 1 to Pred(cMaxCaptions) do
|
|
212 |
Captions[Pred(i)]:= Captions[i];
|
|
213 |
Captions[Pred(cMaxCaptions)].EndTime:= 0
|
|
214 |
end;
|
|
215 |
|
47
|
216 |
// Teams Healths
|
|
217 |
team:= TeamsList;
|
|
218 |
while team <> nil do
|
|
219 |
begin
|
49
|
220 |
DrawFromStoreRect(cScreenWidth div 2 - team.NameRect.w - 3,
|
|
221 |
Team.DrawHealthY,
|
47
|
222 |
@team.NameRect, Surface);
|
|
223 |
r:= team.HealthRect;
|
|
224 |
r.w:= 3 + team.TeamHealth;
|
49
|
225 |
DrawFromStoreRect(cScreenWidth div 2,
|
|
226 |
Team.DrawHealthY,
|
47
|
227 |
@r, Surface);
|
|
228 |
inc(r.x, cTeamHealthWidth + 3);
|
|
229 |
r.w:= 2;
|
49
|
230 |
DrawFromStoreRect(cScreenWidth div 2 + team.TeamHealth + 3,
|
|
231 |
Team.DrawHealthY,
|
47
|
232 |
@r, Surface);
|
|
233 |
team:= team.Next
|
|
234 |
end;
|
|
235 |
|
5
|
236 |
// Lag alert
|
4
|
237 |
if isInLag then DrawSprite(sprLag, 32, 32 + cConsoleYAdd, (RealTicks shr 7) mod 7, Surface);
|
|
238 |
|
5
|
239 |
// Wind bar
|
|
240 |
DrawGear(sWindBar, cScreenWidth - 180, cScreenHeight - 30, Surface);
|
6
|
241 |
if WindBarWidth > 0 then
|
5
|
242 |
begin
|
|
243 |
with StuffPoz[sWindR] do
|
|
244 |
begin
|
6
|
245 |
{$WARNINGS OFF}
|
5
|
246 |
r.x:= x + 8 - (RealTicks shr 6) mod 8;
|
6
|
247 |
{$WARNINGS ON}
|
5
|
248 |
r.y:= y;
|
6
|
249 |
r.w:= WindBarWidth;
|
5
|
250 |
r.h:= 13;
|
|
251 |
end;
|
|
252 |
DrawSpriteFromRect(r, cScreenWidth - 103, cScreenHeight - 28, 13, 0, Surface);
|
|
253 |
end else
|
6
|
254 |
if WindBarWidth < 0 then
|
5
|
255 |
begin
|
|
256 |
with StuffPoz[sWindL] do
|
|
257 |
begin
|
6
|
258 |
{$WARNINGS OFF}
|
|
259 |
r.x:= x + (WindBarWidth + RealTicks shr 6) mod 8;
|
|
260 |
{$WARNINGS ON}
|
5
|
261 |
r.y:= y;
|
6
|
262 |
r.w:= - WindBarWidth;
|
5
|
263 |
r.h:= 13;
|
|
264 |
end;
|
6
|
265 |
DrawSpriteFromRect(r, cScreenWidth - 106 + WindBarWidth, cScreenHeight - 28, 13, 0, Surface);
|
5
|
266 |
end;
|
|
267 |
|
|
268 |
// Cursor
|
4
|
269 |
if isCursorVisible then DrawSprite(sprArrow, CursorPoint.X, CursorPoint.Y, (RealTicks shr 6) mod 8, Surface);
|
|
270 |
|
|
271 |
{$IFDEF COUNTTICKS}
|
|
272 |
DXOutText(10, 10, fnt16, inttostr(cntTicks), Surface);
|
|
273 |
{$ENDIF}
|
|
274 |
|
|
275 |
inc(Frames);
|
|
276 |
inc(CountTicks, Lag);
|
|
277 |
if CountTicks >= 1000 then
|
|
278 |
begin
|
|
279 |
FPS:= Frames;
|
|
280 |
Frames:= 0;
|
|
281 |
CountTicks:= 0;
|
|
282 |
end;
|
|
283 |
if cShowFPS then DXOutText(cScreenWidth - 50, 10, fnt16, inttostr(FPS) + ' fps', Surface)
|
|
284 |
end;
|
|
285 |
|
|
286 |
procedure AddCaption(s: shortstring; Color, Group: LongWord);
|
|
287 |
var i, t, m, k: LongWord;
|
|
288 |
begin
|
|
289 |
i:= 0;
|
|
290 |
while (i < cMaxCaptions) and (Captions[i].Group <> Group)do inc(i);
|
|
291 |
if i < cMaxCaptions then
|
|
292 |
begin
|
|
293 |
while (i < Pred(cMaxCaptions)) do
|
|
294 |
begin
|
|
295 |
Captions[i]:= Captions[Succ(i)];
|
|
296 |
inc(i)
|
|
297 |
end;
|
|
298 |
Captions[Pred(cMaxCaptions)].EndTime:= 0
|
|
299 |
end;
|
|
300 |
|
|
301 |
if Captions[Pred(cMaxCaptions)].EndTime > 0 then
|
|
302 |
begin
|
|
303 |
m:= Pred(cMaxCaptions);
|
|
304 |
for i:= 1 to m do
|
|
305 |
Captions[Pred(i)]:= Captions[i];
|
|
306 |
Captions[m].EndTime:= 0
|
|
307 |
end else
|
|
308 |
begin
|
|
309 |
m:= 0;
|
|
310 |
while (m < cMaxCaptions)and(Captions[m].EndTime > 0) do inc(m)
|
|
311 |
end;
|
|
312 |
|
|
313 |
k:= 0;
|
|
314 |
for i:= 0 to Pred(cMaxCaptions) do
|
|
315 |
for t:= 0 to Pred(cMaxCaptions) do
|
|
316 |
if (Captions[t].EndTime > 0)and(Captions[t].StorePos = k) then inc(k);
|
|
317 |
|
|
318 |
Captions[m].r:= RenderString(s, Color, k);
|
|
319 |
Captions[m].StorePos:= k;
|
|
320 |
Captions[m].Group:= Group;
|
|
321 |
Captions[m].EndTime:= RealTicks + 1200
|
|
322 |
end;
|
|
323 |
|
|
324 |
procedure MoveWorld;
|
|
325 |
const PrevSentPointTime: LongWord = 0;
|
|
326 |
var s: string[9];
|
|
327 |
begin
|
|
328 |
if not (CurrentTeam.ExtDriven and isCursorVisible) then SDL_GetMouseState(@CursorPoint.X, @CursorPoint.Y);
|
|
329 |
if (FollowGear <> nil) then
|
57
|
330 |
if abs(CursorPoint.X - prevPoint.X) + abs(CursorPoint.Y - prevpoint.Y) > 4 then
|
4
|
331 |
begin
|
|
332 |
FollowGear:= nil;
|
|
333 |
exit
|
|
334 |
end
|
|
335 |
else begin
|
71
|
336 |
CursorPoint.x:= (CursorPoint.x * 7 + (round(FollowGear.X + Sign(FollowGear.dX) * 100) + WorldDx)) div 8;
|
|
337 |
CursorPoint.y:= (CursorPoint.y * 7 + (round(FollowGear.Y) + WorldDy)) div 8
|
4
|
338 |
end;
|
|
339 |
|
|
340 |
if ((CursorPoint.X = prevPoint.X)and(CursorPoint.Y = prevpoint.Y)) then exit;
|
|
341 |
|
|
342 |
if isCursorVisible then
|
|
343 |
begin
|
|
344 |
if (not CurrentTeam.ExtDriven)and(GameTicks >= PrevSentPointTime + cSendCursorPosTime) then
|
|
345 |
begin
|
|
346 |
s[0]:= #9;
|
|
347 |
s[1]:= 'P';
|
|
348 |
PInteger(@s[2])^:= CursorPoint.X - WorldDx;
|
|
349 |
PInteger(@s[6])^:= CursorPoint.Y - WorldDy;
|
|
350 |
SendIPC(s);
|
|
351 |
PrevSentPointTime:= GameTicks
|
|
352 |
end;
|
|
353 |
end;
|
|
354 |
if isCursorVisible or (FollowGear <> nil) then
|
|
355 |
begin
|
|
356 |
if CursorPoint.X < cScreenEdgesDist then
|
|
357 |
begin
|
|
358 |
WorldDx:= WorldDx - CursorPoint.X + cScreenEdgesDist;
|
|
359 |
CursorPoint.X:= cScreenEdgesDist
|
|
360 |
end else
|
|
361 |
if CursorPoint.X > cScreenWidth - cScreenEdgesDist then
|
|
362 |
begin
|
|
363 |
WorldDx:= WorldDx - CursorPoint.X + cScreenWidth - cScreenEdgesDist;
|
|
364 |
CursorPoint.X:= cScreenWidth - cScreenEdgesDist
|
|
365 |
end;
|
|
366 |
if CursorPoint.Y < cScreenEdgesDist then
|
|
367 |
begin
|
|
368 |
WorldDy:= WorldDy - CursorPoint.Y + cScreenEdgesDist;
|
|
369 |
CursorPoint.Y:= cScreenEdgesDist
|
|
370 |
end else
|
|
371 |
if CursorPoint.Y > cScreenHeight - cScreenEdgesDist then
|
|
372 |
begin
|
|
373 |
WorldDy:= WorldDy - CursorPoint.Y + cScreenHeight - cScreenEdgesDist;
|
|
374 |
CursorPoint.Y:= cScreenHeight - cScreenEdgesDist
|
|
375 |
end;
|
|
376 |
end else
|
|
377 |
begin
|
70
|
378 |
WorldDx:= WorldDx - CursorPoint.X + prevPoint.X;
|
|
379 |
WorldDy:= WorldDy - CursorPoint.Y + prevPoint.Y;
|
4
|
380 |
CursorPoint.X:= (cScreenWidth shr 1);
|
|
381 |
CursorPoint.Y:= (cScreenHeight shr 1);
|
|
382 |
end;
|
|
383 |
SDL_WarpMouse(CursorPoint.X, CursorPoint.Y);
|
|
384 |
prevPoint:= CursorPoint;
|
75
|
385 |
if WorldDy < cScreenHeight - cWaterLine - cVisibleWater then WorldDy:= cScreenHeight - cWaterLine - cVisibleWater;
|
4
|
386 |
if WorldDy > 2048 then WorldDy:= 2048;
|
|
387 |
if WorldDx < -2048 then WorldDx:= -2048;
|
|
388 |
if WorldDx > cScreenWidth then WorldDx:= cScreenWidth;
|
|
389 |
end;
|
|
390 |
|
|
391 |
initialization
|
|
392 |
FillChar(Captions, sizeof(Captions), 0)
|
|
393 |
|
|
394 |
end.
|