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