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