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