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