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 |
|
70
|
39 |
procedure AddObjects(InSurface, Surface: PSDL_Surface);
|
24
|
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;
|
70
|
46 |
MAXTHEMEOBJECTS = 32;
|
24
|
47 |
|
70
|
48 |
type PRectArray = ^TRectsArray;
|
|
49 |
TRectsArray = array[0..MaxRects] of TSDL_Rect;
|
|
50 |
TThemeObject = record
|
24
|
51 |
Surf: PSDL_Surface;
|
|
52 |
inland: TSDL_Rect;
|
70
|
53 |
outland: array[0..Pred(MAXOBJECTRECTS)] of TSDL_Rect;
|
24
|
54 |
rectcnt: Longword;
|
|
55 |
Width, Height: Longword;
|
56
|
56 |
Maxcnt: Longword;
|
24
|
57 |
end;
|
70
|
58 |
TThemeObjects = record
|
|
59 |
Count: integer;
|
|
60 |
objs: array[0..Pred(MAXTHEMEOBJECTS)] of TThemeObject;
|
|
61 |
end;
|
|
62 |
TSprayObject = record
|
|
63 |
Surf: PSDL_Surface;
|
|
64 |
Width, Height: Longword;
|
|
65 |
Maxcnt: Longword;
|
|
66 |
end;
|
|
67 |
TSprayObjects = record
|
|
68 |
Count: integer;
|
|
69 |
objs: array[0..Pred(MAXTHEMEOBJECTS)] of TSprayObject
|
|
70 |
end;
|
24
|
71 |
|
|
72 |
var Rects: PRectArray;
|
|
73 |
RectCount: Longword;
|
|
74 |
|
|
75 |
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
|
107
|
76 |
var p: PByteArray;
|
24
|
77 |
x, y: Longword;
|
|
78 |
bpp: integer;
|
|
79 |
r: TSDL_Rect;
|
|
80 |
begin
|
|
81 |
r.x:= cpX;
|
|
82 |
r.y:= cpY;
|
|
83 |
SDL_UpperBlit(Image, nil, Surface, @r);
|
|
84 |
WriteToConsole('Generating collision info... ');
|
|
85 |
|
|
86 |
if SDL_MustLock(Image) then
|
|
87 |
SDLTry(SDL_LockSurface(Image) >= 0, true);
|
|
88 |
|
|
89 |
bpp:= Image.format.BytesPerPixel;
|
|
90 |
WriteToConsole('('+inttostr(bpp)+') ');
|
107
|
91 |
p:= Image.pixels;
|
24
|
92 |
case bpp of
|
|
93 |
1: OutError('We don''t work with 8 bit surfaces', true);
|
|
94 |
2: for y:= 0 to Pred(Image.h) do
|
|
95 |
begin
|
|
96 |
for x:= 0 to Pred(Image.w) do
|
107
|
97 |
if PWord(@p[x * 2])^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
98 |
p:= @p[Image.pitch];
|
24
|
99 |
end;
|
|
100 |
3: for y:= 0 to Pred(Image.h) do
|
|
101 |
begin
|
|
102 |
for x:= 0 to Pred(Image.w) do
|
107
|
103 |
if (p[x * 3 + 0] <> 0)
|
|
104 |
or (p[x * 3 + 1] <> 0)
|
|
105 |
or (p[x * 3 + 2] <> 0) then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
106 |
p:= @p[Image.pitch];
|
24
|
107 |
end;
|
|
108 |
4: for y:= 0 to Pred(Image.h) do
|
|
109 |
begin
|
|
110 |
for x:= 0 to Pred(Image.w) do
|
107
|
111 |
if PLongword(@p[x * 4])^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
112 |
p:= @p[Image.pitch];
|
24
|
113 |
end;
|
|
114 |
end;
|
|
115 |
if SDL_MustLock(Image) then
|
|
116 |
SDL_UnlockSurface(Image);
|
|
117 |
WriteLnToConsole(msgOK)
|
|
118 |
end;
|
|
119 |
|
|
120 |
procedure AddRect(x1, y1, w1, h1: integer);
|
|
121 |
begin
|
|
122 |
with Rects[RectCount] do
|
|
123 |
begin
|
|
124 |
x:= x1;
|
|
125 |
y:= y1;
|
|
126 |
w:= w1;
|
|
127 |
h:= h1
|
|
128 |
end;
|
|
129 |
inc(RectCount);
|
|
130 |
TryDo(RectCount < MaxRects, 'AddRect: overflow', true)
|
|
131 |
end;
|
|
132 |
|
|
133 |
procedure InitRects;
|
|
134 |
begin
|
|
135 |
RectCount:= 0;
|
|
136 |
New(Rects)
|
|
137 |
end;
|
|
138 |
|
|
139 |
procedure FreeRects;
|
|
140 |
begin
|
|
141 |
Dispose(rects)
|
|
142 |
end;
|
|
143 |
|
|
144 |
function CheckIntersect(x1, y1, w1, h1: integer): boolean;
|
|
145 |
var i: Longword;
|
|
146 |
begin
|
|
147 |
Result:= false;
|
|
148 |
i:= 0;
|
|
149 |
if RectCount > 0 then
|
|
150 |
repeat
|
|
151 |
with Rects[i] do
|
|
152 |
Result:= (x < x1 + w1) and (x1 < x + w) and
|
|
153 |
(y < y1 + h1) and (y1 < y + h);
|
|
154 |
inc(i)
|
|
155 |
until (i = RectCount) or (Result)
|
|
156 |
end;
|
|
157 |
|
27
|
158 |
function AddGirder(gX: integer; Surface: PSDL_Surface): boolean;
|
24
|
159 |
var tmpsurf: PSDL_Surface;
|
|
160 |
x1, x2, y, k, i: integer;
|
|
161 |
r, rr: TSDL_Rect;
|
|
162 |
|
|
163 |
function CountNonZeroz(x, y: integer): Longword;
|
|
164 |
var i: integer;
|
|
165 |
begin
|
|
166 |
Result:= 0;
|
|
167 |
for i:= y to y + 15 do
|
|
168 |
if Land[i, x] <> 0 then inc(Result)
|
|
169 |
end;
|
|
170 |
|
|
171 |
begin
|
27
|
172 |
y:= 150;
|
24
|
173 |
repeat
|
|
174 |
inc(y, 24);
|
27
|
175 |
x1:= gX;
|
|
176 |
x2:= gX;
|
24
|
177 |
while (x1 > 100) and (CountNonZeroz(x1, y) = 0) do dec(x1, 2);
|
|
178 |
i:= x1 - 12;
|
|
179 |
repeat
|
|
180 |
k:= CountNonZeroz(x1, y);
|
|
181 |
dec(x1, 2)
|
|
182 |
until (x1 < 100) or (k = 0) or (k = 16) or (x1 < i);
|
|
183 |
inc(x1, 2);
|
|
184 |
if k = 16 then
|
|
185 |
begin
|
|
186 |
while (x2 < 1900) and (CountNonZeroz(x2, y) = 0) do inc(x2, 2);
|
|
187 |
i:= x2 + 12;
|
|
188 |
repeat
|
|
189 |
k:= CountNonZeroz(x2, y);
|
|
190 |
inc(x2, 2)
|
|
191 |
until (x2 > 1900) or (k = 0) or (k = 16) or (x2 > i);
|
|
192 |
if (x2 < 1900) and (k = 16) and (x2 - x1 > 250)
|
30
|
193 |
and not CheckIntersect(x1 - 32, y - 64, x2 - x1 + 64, 144) then break;
|
24
|
194 |
end;
|
|
195 |
x1:= 0;
|
|
196 |
until y > 900;
|
|
197 |
if x1 > 0 then
|
|
198 |
begin
|
27
|
199 |
Result:= true;
|
74
|
200 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/Girder', false);
|
24
|
201 |
rr.x:= x1;
|
|
202 |
rr.y:= y;
|
|
203 |
while rr.x + 100 < x2 do
|
|
204 |
begin
|
|
205 |
SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
|
|
206 |
inc(rr.x, 100);
|
|
207 |
end;
|
|
208 |
r.x:= 0;
|
|
209 |
r.y:= 0;
|
|
210 |
r.w:= x2 - rr.x;
|
|
211 |
r.h:= 16;
|
|
212 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
|
|
213 |
SDL_FreeSurface(tmpsurf);
|
30
|
214 |
AddRect(x1 - 8, y - 32, x2 - x1 + 16, 80);
|
24
|
215 |
for k:= y to y + 15 do
|
|
216 |
for i:= x1 to x2 do Land[k, i]:= $FFFFFF
|
27
|
217 |
end else Result:= false
|
24
|
218 |
end;
|
|
219 |
|
|
220 |
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean;
|
|
221 |
var i: Longword;
|
|
222 |
begin
|
|
223 |
Result:= true;
|
|
224 |
inc(rect.x, dX);
|
|
225 |
inc(rect.y, dY);
|
|
226 |
i:= 0;
|
|
227 |
{$WARNINGS OFF}
|
|
228 |
while (i <= rect.w) and Result do
|
|
229 |
begin
|
|
230 |
Result:= (Land[rect.y, rect.x + i] = Color) and (Land[rect.y + rect.h, rect.x + i] = Color);
|
|
231 |
inc(i)
|
|
232 |
end;
|
|
233 |
i:= 0;
|
|
234 |
while (i <= rect.h) and Result do
|
|
235 |
begin
|
27
|
236 |
Result:= (Land[rect.y + i, rect.x] = Color) and (Land[rect.y + i, rect.x + rect.w] = Color);
|
24
|
237 |
inc(i)
|
|
238 |
end;
|
|
239 |
{$WARNINGS ON}
|
|
240 |
end;
|
|
241 |
|
|
242 |
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean;
|
|
243 |
var i: Longword;
|
|
244 |
begin
|
|
245 |
with Obj do
|
|
246 |
if CheckLand(inland, x, y, $FFFFFF) then
|
|
247 |
begin
|
|
248 |
Result:= true;
|
|
249 |
i:= 1;
|
|
250 |
while Result and (i <= rectcnt) do
|
|
251 |
begin
|
|
252 |
Result:= CheckLand(outland[i], x, y, 0);
|
|
253 |
inc(i)
|
|
254 |
end;
|
|
255 |
if Result then
|
|
256 |
Result:= not CheckIntersect(x, y, Width, Height)
|
|
257 |
end else
|
|
258 |
Result:= false
|
|
259 |
end;
|
|
260 |
|
70
|
261 |
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean; overload;
|
24
|
262 |
const MaxPointsIndex = 2047;
|
|
263 |
var x, y: Longword;
|
|
264 |
ar: array[0..MaxPointsIndex] of TPoint;
|
|
265 |
cnt, i: Longword;
|
|
266 |
begin
|
|
267 |
cnt:= 0;
|
|
268 |
with Obj do
|
|
269 |
begin
|
56
|
270 |
if Maxcnt = 0 then
|
|
271 |
begin
|
|
272 |
Result:= false;
|
|
273 |
exit
|
|
274 |
end;
|
24
|
275 |
x:= 0;
|
|
276 |
repeat
|
|
277 |
y:= 0;
|
|
278 |
repeat
|
|
279 |
if CheckCanPlace(x, y, Obj) then
|
|
280 |
begin
|
|
281 |
ar[cnt].x:= x;
|
|
282 |
ar[cnt].y:= y;
|
|
283 |
inc(cnt);
|
|
284 |
if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
|
|
285 |
begin
|
|
286 |
y:= 5000;
|
|
287 |
x:= 5000;
|
|
288 |
end
|
|
289 |
end;
|
27
|
290 |
inc(y, 3);
|
24
|
291 |
until y > 1023 - Height;
|
27
|
292 |
inc(x, getrandom(6) + 3)
|
24
|
293 |
until x > 2047 - Width;
|
|
294 |
Result:= cnt <> 0;
|
|
295 |
if Result then
|
|
296 |
begin
|
|
297 |
i:= getrandom(cnt);
|
|
298 |
BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface);
|
|
299 |
AddRect(ar[i].x, ar[i].y, Width, Height);
|
56
|
300 |
dec(Maxcnt)
|
|
301 |
end else Maxcnt:= 0
|
24
|
302 |
end
|
|
303 |
end;
|
|
304 |
|
70
|
305 |
function TryPut(var Obj: TSprayObject; Surface: PSDL_Surface): boolean; overload;
|
|
306 |
const MaxPointsIndex = 8095;
|
|
307 |
var x, y: Longword;
|
|
308 |
ar: array[0..MaxPointsIndex] of TPoint;
|
|
309 |
cnt, i: Longword;
|
|
310 |
r: TSDL_Rect;
|
|
311 |
begin
|
|
312 |
cnt:= 0;
|
|
313 |
with Obj do
|
|
314 |
begin
|
|
315 |
if Maxcnt = 0 then
|
|
316 |
begin
|
|
317 |
Result:= false;
|
|
318 |
exit
|
|
319 |
end;
|
|
320 |
x:= 0;
|
|
321 |
r.x:= 0;
|
|
322 |
r.y:= 0;
|
|
323 |
r.w:= Width;
|
|
324 |
r.h:= Height + 16;
|
|
325 |
repeat
|
|
326 |
y:= 8;
|
|
327 |
repeat
|
|
328 |
if CheckLand(r, x, y - 8, $FFFFFF)
|
|
329 |
and not CheckIntersect(x, y, Width, Height) then
|
|
330 |
begin
|
|
331 |
ar[cnt].x:= x;
|
|
332 |
ar[cnt].y:= y;
|
|
333 |
inc(cnt);
|
|
334 |
if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
|
|
335 |
begin
|
|
336 |
y:= 5000;
|
|
337 |
x:= 5000;
|
|
338 |
end
|
|
339 |
end;
|
|
340 |
inc(y, 12);
|
|
341 |
until y > 1023 - Height - 8;
|
|
342 |
inc(x, getrandom(12) + 12)
|
|
343 |
until x > 2047 - Width;
|
|
344 |
Result:= cnt <> 0;
|
|
345 |
if Result then
|
|
346 |
begin
|
|
347 |
i:= getrandom(cnt);
|
|
348 |
r.x:= ar[i].X;
|
|
349 |
r.y:= ar[i].Y;
|
|
350 |
r.w:= Width;
|
|
351 |
r.h:= Height;
|
|
352 |
SDL_UpperBlit(Obj.Surf, nil, Surface, @r);
|
|
353 |
AddRect(ar[i].x - 32, ar[i].y - 32, Width + 64, Height + 64);
|
|
354 |
dec(Maxcnt)
|
|
355 |
end else Maxcnt:= 0
|
|
356 |
end
|
|
357 |
end;
|
|
358 |
|
|
359 |
procedure ReadThemeInfo(var ThemeObjects: TThemeObjects; var SprayObjects: TSprayObjects);
|
|
360 |
var s: string;
|
|
361 |
f: textfile;
|
|
362 |
i, ii: integer;
|
24
|
363 |
begin
|
80
|
364 |
s:= Pathz[ptCurrTheme] + '/' + cThemeCFGFilename;
|
70
|
365 |
WriteLnToConsole('Reading objects info...');
|
24
|
366 |
AssignFile(f, s);
|
|
367 |
{$I-}
|
|
368 |
Reset(f);
|
|
369 |
Readln(f, s); // skip color
|
70
|
370 |
Readln(f, ThemeObjects.Count);
|
|
371 |
for i:= 0 to Pred(ThemeObjects.Count) do
|
24
|
372 |
begin
|
|
373 |
Readln(f, s); // filename
|
70
|
374 |
with ThemeObjects.objs[i] do
|
24
|
375 |
begin
|
80
|
376 |
Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + s, false);
|
24
|
377 |
Read(f, Width, Height);
|
|
378 |
with inland do Read(f, x, y, w, h);
|
|
379 |
Read(f, rectcnt);
|
|
380 |
for ii:= 1 to rectcnt do
|
|
381 |
with outland[ii] do Read(f, x, y, w, h);
|
70
|
382 |
Maxcnt:= 3;
|
24
|
383 |
ReadLn(f)
|
|
384 |
end;
|
|
385 |
end;
|
70
|
386 |
|
|
387 |
Readln(f, SprayObjects.Count);
|
|
388 |
for i:= 0 to Pred(SprayObjects.Count) do
|
|
389 |
begin
|
|
390 |
Readln(f, s); // filename
|
|
391 |
with SprayObjects.objs[i] do
|
|
392 |
begin
|
80
|
393 |
Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + s, false);
|
70
|
394 |
Width:= Surf.w;
|
|
395 |
Height:= Surf.h;
|
|
396 |
ReadLn(f, Maxcnt)
|
|
397 |
end;
|
|
398 |
end;
|
24
|
399 |
Closefile(f);
|
|
400 |
{$I+}
|
70
|
401 |
TryDo(IOResult = 0, 'Bad data or cannot access file ' + cThemeCFGFilename, true)
|
|
402 |
end;
|
24
|
403 |
|
70
|
404 |
procedure AddThemeObjects(Surface: PSDL_Surface; var ThemeObjects: TThemeObjects; MaxCount: integer);
|
|
405 |
var i, ii, t: integer;
|
|
406 |
b: boolean;
|
|
407 |
begin
|
|
408 |
if ThemeObjects.Count = 0 then exit;
|
|
409 |
WriteLnToConsole('Adding theme objects...');
|
24
|
410 |
i:= 1;
|
|
411 |
repeat
|
70
|
412 |
t:= getrandom(ThemeObjects.Count);
|
24
|
413 |
ii:= t;
|
|
414 |
repeat
|
|
415 |
inc(ii);
|
70
|
416 |
if ii = ThemeObjects.Count then ii:= 0;
|
|
417 |
b:= TryPut(ThemeObjects.objs[ii], Surface)
|
24
|
418 |
until b or (ii = t);
|
70
|
419 |
inc(i)
|
|
420 |
until (i > MaxCount) or not b;
|
24
|
421 |
end;
|
|
422 |
|
70
|
423 |
procedure AddSprayObjects(Surface: PSDL_Surface; var SprayObjects: TSprayObjects; MaxCount: Longword);
|
|
424 |
var i: Longword;
|
|
425 |
ii, t: integer;
|
|
426 |
b: boolean;
|
|
427 |
begin
|
|
428 |
if SprayObjects.Count = 0 then exit;
|
|
429 |
WriteLnToConsole('Adding spray objects...');
|
|
430 |
i:= 1;
|
|
431 |
repeat
|
|
432 |
t:= getrandom(SprayObjects.Count);
|
|
433 |
ii:= t;
|
|
434 |
repeat
|
|
435 |
inc(ii);
|
|
436 |
if ii = SprayObjects.Count then ii:= 0;
|
|
437 |
b:= TryPut(SprayObjects.objs[ii], Surface)
|
|
438 |
until b or (ii = t);
|
|
439 |
inc(i)
|
|
440 |
until (i > MaxCount) or not b;
|
|
441 |
end;
|
|
442 |
|
|
443 |
procedure AddObjects(InSurface, Surface: PSDL_Surface);
|
|
444 |
var ThemeObjects: TThemeObjects;
|
|
445 |
SprayObjects: TSprayObjects;
|
24
|
446 |
begin
|
|
447 |
InitRects;
|
70
|
448 |
AddGirder(256, Surface);
|
27
|
449 |
AddGirder(512, Surface);
|
70
|
450 |
AddGirder(768, Surface);
|
27
|
451 |
AddGirder(1024, Surface);
|
70
|
452 |
AddGirder(1280, Surface);
|
27
|
453 |
AddGirder(1536, Surface);
|
70
|
454 |
AddGirder(1792, Surface);
|
|
455 |
ReadThemeInfo(ThemeObjects, SprayObjects);
|
|
456 |
AddThemeObjects(Surface, ThemeObjects, 8);
|
|
457 |
AddProgress;
|
|
458 |
SDL_UpperBlit(InSurface, nil, Surface, nil);
|
|
459 |
AddSprayObjects(Surface, SprayObjects, 10);
|
24
|
460 |
FreeRects
|
|
461 |
end;
|
|
462 |
|
|
463 |
end.
|