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