51
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005, 2006 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 |
|
24
|
34 |
unit uLandObjects;
|
|
35 |
interface
|
|
36 |
uses SDLh;
|
|
37 |
{$include options.inc}
|
|
38 |
|
|
39 |
procedure AddObjects(Surface: PSDL_Surface);
|
|
40 |
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
|
|
41 |
|
|
42 |
implementation
|
|
43 |
uses uLand, uStore, uConsts, uMisc, uConsole, uRandom;
|
27
|
44 |
const MaxRects = 256;
|
|
45 |
MAXOBJECTRECTS = 16;
|
24
|
46 |
type PRectArray = ^TRectsArray;
|
|
47 |
TRectsArray = array[0..MaxRects] of TSDL_rect;
|
|
48 |
|
|
49 |
type TThemeObject = record
|
|
50 |
Surf: PSDL_Surface;
|
|
51 |
inland: TSDL_Rect;
|
|
52 |
outland: array[1..MAXOBJECTRECTS] of TSDL_Rect;
|
|
53 |
rectcnt: Longword;
|
|
54 |
Width, Height: Longword;
|
56
|
55 |
Maxcnt: Longword;
|
24
|
56 |
end;
|
|
57 |
|
|
58 |
var Rects: PRectArray;
|
|
59 |
RectCount: Longword;
|
|
60 |
|
|
61 |
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
|
|
62 |
var i, p: LongWord;
|
|
63 |
x, y: Longword;
|
|
64 |
bpp: integer;
|
|
65 |
r: TSDL_Rect;
|
|
66 |
begin
|
|
67 |
r.x:= cpX;
|
|
68 |
r.y:= cpY;
|
|
69 |
SDL_UpperBlit(Image, nil, Surface, @r);
|
|
70 |
WriteToConsole('Generating collision info... ');
|
|
71 |
|
|
72 |
if SDL_MustLock(Image) then
|
|
73 |
SDLTry(SDL_LockSurface(Image) >= 0, true);
|
|
74 |
|
|
75 |
bpp:= Image.format.BytesPerPixel;
|
|
76 |
WriteToConsole('('+inttostr(bpp)+') ');
|
|
77 |
p:= LongWord(Image.pixels);
|
|
78 |
case bpp of
|
|
79 |
1: OutError('We don''t work with 8 bit surfaces', true);
|
|
80 |
2: for y:= 0 to Pred(Image.h) do
|
|
81 |
begin
|
|
82 |
i:= Longword(@Land[cpY + y, cpX]);
|
|
83 |
for x:= 0 to Pred(Image.w) do
|
|
84 |
if PWord(p + x * 2)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
|
|
85 |
inc(p, Image.pitch);
|
|
86 |
end;
|
|
87 |
3: for y:= 0 to Pred(Image.h) do
|
|
88 |
begin
|
|
89 |
i:= Longword(@Land[cpY + y, cpX]);
|
|
90 |
for x:= 0 to Pred(Image.w) do
|
|
91 |
if (PByte(p + x * 3 + 0)^ <> 0)
|
|
92 |
or (PByte(p + x * 3 + 1)^ <> 0)
|
|
93 |
or (PByte(p + x * 3 + 2)^ <> 0) then PLongWord(i + x * 4)^:= $FFFFFF;
|
|
94 |
inc(p, Image.pitch);
|
|
95 |
end;
|
|
96 |
4: for y:= 0 to Pred(Image.h) do
|
|
97 |
begin
|
|
98 |
i:= Longword(@Land[cpY + y, cpX]);
|
|
99 |
for x:= 0 to Pred(Image.w) do
|
|
100 |
if PLongword(p + x * 4)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
|
|
101 |
inc(p, Image.pitch);
|
|
102 |
end;
|
|
103 |
end;
|
|
104 |
if SDL_MustLock(Image) then
|
|
105 |
SDL_UnlockSurface(Image);
|
|
106 |
WriteLnToConsole(msgOK)
|
|
107 |
end;
|
|
108 |
|
|
109 |
procedure AddRect(x1, y1, w1, h1: integer);
|
|
110 |
begin
|
|
111 |
with Rects[RectCount] do
|
|
112 |
begin
|
|
113 |
x:= x1;
|
|
114 |
y:= y1;
|
|
115 |
w:= w1;
|
|
116 |
h:= h1
|
|
117 |
end;
|
|
118 |
inc(RectCount);
|
|
119 |
TryDo(RectCount < MaxRects, 'AddRect: overflow', true)
|
|
120 |
end;
|
|
121 |
|
|
122 |
procedure InitRects;
|
|
123 |
begin
|
|
124 |
RectCount:= 0;
|
|
125 |
New(Rects)
|
|
126 |
end;
|
|
127 |
|
|
128 |
procedure FreeRects;
|
|
129 |
begin
|
|
130 |
Dispose(rects)
|
|
131 |
end;
|
|
132 |
|
|
133 |
function CheckIntersect(x1, y1, w1, h1: integer): boolean;
|
|
134 |
var i: Longword;
|
|
135 |
begin
|
|
136 |
Result:= false;
|
|
137 |
i:= 0;
|
|
138 |
if RectCount > 0 then
|
|
139 |
repeat
|
|
140 |
with Rects[i] do
|
|
141 |
Result:= (x < x1 + w1) and (x1 < x + w) and
|
|
142 |
(y < y1 + h1) and (y1 < y + h);
|
|
143 |
inc(i)
|
|
144 |
until (i = RectCount) or (Result)
|
|
145 |
end;
|
|
146 |
|
27
|
147 |
function AddGirder(gX: integer; Surface: PSDL_Surface): boolean;
|
24
|
148 |
var tmpsurf: PSDL_Surface;
|
|
149 |
x1, x2, y, k, i: integer;
|
|
150 |
r, rr: TSDL_Rect;
|
|
151 |
|
|
152 |
function CountNonZeroz(x, y: integer): Longword;
|
|
153 |
var i: integer;
|
|
154 |
begin
|
|
155 |
Result:= 0;
|
|
156 |
for i:= y to y + 15 do
|
|
157 |
if Land[i, x] <> 0 then inc(Result)
|
|
158 |
end;
|
|
159 |
|
|
160 |
begin
|
27
|
161 |
y:= 150;
|
24
|
162 |
repeat
|
|
163 |
inc(y, 24);
|
27
|
164 |
x1:= gX;
|
|
165 |
x2:= gX;
|
24
|
166 |
while (x1 > 100) and (CountNonZeroz(x1, y) = 0) do dec(x1, 2);
|
|
167 |
i:= x1 - 12;
|
|
168 |
repeat
|
|
169 |
k:= CountNonZeroz(x1, y);
|
|
170 |
dec(x1, 2)
|
|
171 |
until (x1 < 100) or (k = 0) or (k = 16) or (x1 < i);
|
|
172 |
inc(x1, 2);
|
|
173 |
if k = 16 then
|
|
174 |
begin
|
|
175 |
while (x2 < 1900) and (CountNonZeroz(x2, y) = 0) do inc(x2, 2);
|
|
176 |
i:= x2 + 12;
|
|
177 |
repeat
|
|
178 |
k:= CountNonZeroz(x2, y);
|
|
179 |
inc(x2, 2)
|
|
180 |
until (x2 > 1900) or (k = 0) or (k = 16) or (x2 > i);
|
|
181 |
if (x2 < 1900) and (k = 16) and (x2 - x1 > 250)
|
30
|
182 |
and not CheckIntersect(x1 - 32, y - 64, x2 - x1 + 64, 144) then break;
|
24
|
183 |
end;
|
|
184 |
x1:= 0;
|
|
185 |
until y > 900;
|
|
186 |
if x1 > 0 then
|
|
187 |
begin
|
27
|
188 |
Result:= true;
|
54
|
189 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/Girder.png', false);
|
24
|
190 |
rr.x:= x1;
|
|
191 |
rr.y:= y;
|
|
192 |
while rr.x + 100 < x2 do
|
|
193 |
begin
|
|
194 |
SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
|
|
195 |
inc(rr.x, 100);
|
|
196 |
end;
|
|
197 |
r.x:= 0;
|
|
198 |
r.y:= 0;
|
|
199 |
r.w:= x2 - rr.x;
|
|
200 |
r.h:= 16;
|
|
201 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
|
|
202 |
SDL_FreeSurface(tmpsurf);
|
30
|
203 |
AddRect(x1 - 8, y - 32, x2 - x1 + 16, 80);
|
24
|
204 |
for k:= y to y + 15 do
|
|
205 |
for i:= x1 to x2 do Land[k, i]:= $FFFFFF
|
27
|
206 |
end else Result:= false
|
24
|
207 |
end;
|
|
208 |
|
|
209 |
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean;
|
|
210 |
var i: Longword;
|
|
211 |
begin
|
|
212 |
Result:= true;
|
|
213 |
inc(rect.x, dX);
|
|
214 |
inc(rect.y, dY);
|
|
215 |
i:= 0;
|
|
216 |
{$WARNINGS OFF}
|
|
217 |
while (i <= rect.w) and Result do
|
|
218 |
begin
|
|
219 |
Result:= (Land[rect.y, rect.x + i] = Color) and (Land[rect.y + rect.h, rect.x + i] = Color);
|
|
220 |
inc(i)
|
|
221 |
end;
|
|
222 |
i:= 0;
|
|
223 |
while (i <= rect.h) and Result do
|
|
224 |
begin
|
27
|
225 |
Result:= (Land[rect.y + i, rect.x] = Color) and (Land[rect.y + i, rect.x + rect.w] = Color);
|
24
|
226 |
inc(i)
|
|
227 |
end;
|
|
228 |
{$WARNINGS ON}
|
|
229 |
end;
|
|
230 |
|
|
231 |
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean;
|
|
232 |
var i: Longword;
|
|
233 |
begin
|
|
234 |
with Obj do
|
|
235 |
if CheckLand(inland, x, y, $FFFFFF) then
|
|
236 |
begin
|
|
237 |
Result:= true;
|
|
238 |
i:= 1;
|
|
239 |
while Result and (i <= rectcnt) do
|
|
240 |
begin
|
|
241 |
Result:= CheckLand(outland[i], x, y, 0);
|
|
242 |
inc(i)
|
|
243 |
end;
|
|
244 |
if Result then
|
|
245 |
Result:= not CheckIntersect(x, y, Width, Height)
|
|
246 |
end else
|
|
247 |
Result:= false
|
|
248 |
end;
|
|
249 |
|
|
250 |
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean;
|
|
251 |
const MaxPointsIndex = 2047;
|
|
252 |
var x, y: Longword;
|
|
253 |
ar: array[0..MaxPointsIndex] of TPoint;
|
|
254 |
cnt, i: Longword;
|
|
255 |
begin
|
|
256 |
cnt:= 0;
|
|
257 |
with Obj do
|
|
258 |
begin
|
56
|
259 |
if Maxcnt = 0 then
|
|
260 |
begin
|
|
261 |
Result:= false;
|
|
262 |
exit
|
|
263 |
end;
|
24
|
264 |
x:= 0;
|
|
265 |
repeat
|
|
266 |
y:= 0;
|
|
267 |
repeat
|
|
268 |
if CheckCanPlace(x, y, Obj) then
|
|
269 |
begin
|
|
270 |
ar[cnt].x:= x;
|
|
271 |
ar[cnt].y:= y;
|
|
272 |
inc(cnt);
|
|
273 |
if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
|
|
274 |
begin
|
|
275 |
y:= 5000;
|
|
276 |
x:= 5000;
|
|
277 |
end
|
|
278 |
end;
|
27
|
279 |
inc(y, 3);
|
24
|
280 |
until y > 1023 - Height;
|
27
|
281 |
inc(x, getrandom(6) + 3)
|
24
|
282 |
until x > 2047 - Width;
|
|
283 |
Result:= cnt <> 0;
|
|
284 |
if Result then
|
|
285 |
begin
|
|
286 |
i:= getrandom(cnt);
|
|
287 |
BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface);
|
|
288 |
AddRect(ar[i].x, ar[i].y, Width, Height);
|
56
|
289 |
dec(Maxcnt)
|
|
290 |
end else Maxcnt:= 0
|
24
|
291 |
end
|
|
292 |
end;
|
|
293 |
|
|
294 |
procedure AddThemeObjects(Surface: PSDL_Surface; MaxCount: Longword);
|
27
|
295 |
const MAXTHEMEOBJECTS = 32;
|
24
|
296 |
var f: textfile;
|
|
297 |
s: string;
|
|
298 |
ThemeObjects: array[1..MAXTHEMEOBJECTS] of TThemeObject;
|
|
299 |
i, ii, t, n: Longword;
|
|
300 |
b: boolean;
|
|
301 |
begin
|
54
|
302 |
s:= Pathz[ptThemeCurrent] + '/' + cThemeCFGFilename;
|
24
|
303 |
WriteLnToConsole('Adding objects...');
|
|
304 |
AssignFile(f, s);
|
|
305 |
{$I-}
|
|
306 |
Reset(f);
|
|
307 |
Readln(f, s); // skip color
|
|
308 |
Readln(f, n);
|
|
309 |
for i:= 1 to n do
|
|
310 |
begin
|
|
311 |
Readln(f, s); // filename
|
|
312 |
with ThemeObjects[i] do
|
|
313 |
begin
|
54
|
314 |
Surf:= LoadImage(Pathz[ptThemeCurrent] + '/' + s + '.png', false);
|
24
|
315 |
Read(f, Width, Height);
|
|
316 |
with inland do Read(f, x, y, w, h);
|
|
317 |
Read(f, rectcnt);
|
|
318 |
for ii:= 1 to rectcnt do
|
|
319 |
with outland[ii] do Read(f, x, y, w, h);
|
56
|
320 |
Maxcnt:= 2;
|
24
|
321 |
ReadLn(f)
|
|
322 |
end;
|
|
323 |
end;
|
|
324 |
Closefile(f);
|
|
325 |
{$I+}
|
54
|
326 |
TryDo(IOResult = 0, 'Bad data or cannot access file ' + cThemeCFGFilename, true);
|
24
|
327 |
|
|
328 |
// loaded objects, try to put on land
|
|
329 |
if n = 0 then exit;
|
|
330 |
i:= 1;
|
|
331 |
repeat
|
|
332 |
t:= getrandom(n) + 1;
|
|
333 |
ii:= t;
|
|
334 |
repeat
|
|
335 |
inc(ii);
|
|
336 |
if ii > n then ii:= 1;
|
|
337 |
b:= TryPut(ThemeObjects[ii], Surface)
|
|
338 |
until b or (ii = t);
|
|
339 |
inc(i)
|
|
340 |
until (i > MaxCount) or not b
|
|
341 |
end;
|
|
342 |
|
|
343 |
procedure AddObjects(Surface: PSDL_Surface);
|
|
344 |
begin
|
|
345 |
InitRects;
|
27
|
346 |
AddGirder(512, Surface);
|
|
347 |
AddGirder(1024, Surface);
|
|
348 |
AddGirder(1300, Surface);
|
|
349 |
AddGirder(1536, Surface);
|
|
350 |
AddThemeObjects(Surface, 8);
|
24
|
351 |
FreeRects
|
|
352 |
end;
|
|
353 |
|
|
354 |
end.
|