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