author | unc0rr |
Sun, 26 Nov 2006 20:33:46 +0000 | |
changeset 279 | 5df0ab96b60a |
parent 184 | f97a7a3dc8f6 |
child 351 | 29bc9c36ad5f |
permissions | -rw-r--r-- |
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 |
||
74 |
bpp:= Image.format.BytesPerPixel; |
|
75 |
WriteToConsole('('+inttostr(bpp)+') '); |
|
76 |
p:= Image.pixels; |
|
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 |
|
82 |
if PWord(@p[x * 2])^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND; |
|
83 |
p:= @p[Image.pitch]; |
|
84 |
end; |
|
85 |
3: for y:= 0 to Pred(Image.h) do |
|
86 |
begin |
|
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]; |
|
92 |
end; |
|
93 |
4: for y:= 0 to Pred(Image.h) do |
|
94 |
begin |
|
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]; |
|
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 |
||
143 |
function AddGirder(gX: integer; Surface: PSDL_Surface): boolean; |
|
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 |
|
157 |
y:= 150; |
|
158 |
repeat |
|
159 |
inc(y, 24); |
|
160 |
x1:= gX; |
|
161 |
x2:= gX; |
|
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) |
|
178 |
and not CheckIntersect(x1 - 32, y - 64, x2 - x1 + 64, 144) then break; |
|
179 |
end; |
|
180 |
x1:= 0; |
|
181 |
until y > 900; |
|
182 |
if x1 > 0 then |
|
183 |
begin |
|
184 |
Result:= true; |
|
185 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/Girder', false); |
|
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); |
|
199 |
AddRect(x1 - 8, y - 32, x2 - x1 + 16, 80); |
|
200 |
for k:= y to y + 15 do |
|
201 |
for i:= x1 to x2 do Land[k, i]:= $FFFFFF |
|
202 |
end else Result:= false |
|
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 |
|
221 |
Result:= (Land[rect.y + i, rect.x] = Color) and (Land[rect.y + i, rect.x + rect.w] = Color); |
|
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 |
||
246 |
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean; overload; |
|
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 |
|
255 |
if Maxcnt = 0 then |
|
256 |
begin |
|
257 |
Result:= false; |
|
258 |
exit |
|
259 |
end; |
|
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; |
|
275 |
inc(y, 3); |
|
276 |
until y > 1023 - Height; |
|
277 |
inc(x, getrandom(6) + 3) |
|
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); |
|
285 |
dec(Maxcnt) |
|
286 |
end else Maxcnt:= 0 |
|
287 |
end |
|
288 |
end; |
|
289 |
||
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; |
|
348 |
begin |
|
349 |
s:= Pathz[ptCurrTheme] + '/' + cThemeCFGFilename; |
|
350 |
WriteLnToConsole('Reading objects info...'); |
|
351 |
AssignFile(f, s); |
|
352 |
{$I-} |
|
353 |
Reset(f); |
|
354 |
Readln(f, s); // skip color |
|
355 |
Readln(f, ThemeObjects.Count); |
|
356 |
for i:= 0 to Pred(ThemeObjects.Count) do |
|
357 |
begin |
|
358 |
Readln(f, s); // filename |
|
359 |
with ThemeObjects.objs[i] do |
|
360 |
begin |
|
361 |
Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + s, false); |
|
279
5df0ab96b60a
Less complex theme.cfg format ;) Information about image dimensions is obtained from image
unc0rr
parents:
184
diff
changeset
|
362 |
Width:= Surf.w; |
5df0ab96b60a
Less complex theme.cfg format ;) Information about image dimensions is obtained from image
unc0rr
parents:
184
diff
changeset
|
363 |
Height:= Surf.h; |
184 | 364 |
with inland do Read(f, x, y, w, h); |
365 |
Read(f, rectcnt); |
|
366 |
for ii:= 1 to rectcnt do |
|
367 |
with outland[ii] do Read(f, x, y, w, h); |
|
368 |
Maxcnt:= 3; |
|
369 |
ReadLn(f) |
|
370 |
end; |
|
371 |
end; |
|
372 |
||
373 |
Readln(f, SprayObjects.Count); |
|
374 |
for i:= 0 to Pred(SprayObjects.Count) do |
|
375 |
begin |
|
376 |
Readln(f, s); // filename |
|
377 |
with SprayObjects.objs[i] do |
|
378 |
begin |
|
379 |
Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + s, false); |
|
380 |
Width:= Surf.w; |
|
381 |
Height:= Surf.h; |
|
382 |
ReadLn(f, Maxcnt) |
|
383 |
end; |
|
384 |
end; |
|
385 |
Closefile(f); |
|
386 |
{$I+} |
|
387 |
TryDo(IOResult = 0, 'Bad data or cannot access file ' + cThemeCFGFilename, true) |
|
388 |
end; |
|
389 |
||
390 |
procedure AddThemeObjects(Surface: PSDL_Surface; var ThemeObjects: TThemeObjects; MaxCount: integer); |
|
391 |
var i, ii, t: integer; |
|
392 |
b: boolean; |
|
393 |
begin |
|
394 |
if ThemeObjects.Count = 0 then exit; |
|
395 |
WriteLnToConsole('Adding theme objects...'); |
|
396 |
i:= 1; |
|
397 |
repeat |
|
398 |
t:= getrandom(ThemeObjects.Count); |
|
399 |
ii:= t; |
|
400 |
repeat |
|
401 |
inc(ii); |
|
402 |
if ii = ThemeObjects.Count then ii:= 0; |
|
403 |
b:= TryPut(ThemeObjects.objs[ii], Surface) |
|
404 |
until b or (ii = t); |
|
405 |
inc(i) |
|
406 |
until (i > MaxCount) or not b; |
|
407 |
end; |
|
408 |
||
409 |
procedure AddSprayObjects(Surface: PSDL_Surface; var SprayObjects: TSprayObjects; MaxCount: Longword); |
|
410 |
var i: Longword; |
|
411 |
ii, t: integer; |
|
412 |
b: boolean; |
|
413 |
begin |
|
414 |
if SprayObjects.Count = 0 then exit; |
|
415 |
WriteLnToConsole('Adding spray objects...'); |
|
416 |
i:= 1; |
|
417 |
repeat |
|
418 |
t:= getrandom(SprayObjects.Count); |
|
419 |
ii:= t; |
|
420 |
repeat |
|
421 |
inc(ii); |
|
422 |
if ii = SprayObjects.Count then ii:= 0; |
|
423 |
b:= TryPut(SprayObjects.objs[ii], Surface) |
|
424 |
until b or (ii = t); |
|
425 |
inc(i) |
|
426 |
until (i > MaxCount) or not b; |
|
427 |
end; |
|
428 |
||
429 |
procedure AddObjects(InSurface, Surface: PSDL_Surface); |
|
430 |
var ThemeObjects: TThemeObjects; |
|
431 |
SprayObjects: TSprayObjects; |
|
432 |
begin |
|
433 |
InitRects; |
|
434 |
AddGirder(256, Surface); |
|
435 |
AddGirder(512, Surface); |
|
436 |
AddGirder(768, Surface); |
|
437 |
AddGirder(1024, Surface); |
|
438 |
AddGirder(1280, Surface); |
|
439 |
AddGirder(1536, Surface); |
|
440 |
AddGirder(1792, Surface); |
|
441 |
ReadThemeInfo(ThemeObjects, SprayObjects); |
|
442 |
AddThemeObjects(Surface, ThemeObjects, 8); |
|
443 |
AddProgress; |
|
444 |
SDL_UpperBlit(InSurface, nil, Surface, nil); |
|
445 |
AddSprayObjects(Surface, SprayObjects, 10); |
|
446 |
FreeRects |
|
447 |
end; |
|
448 |
||
449 |
end. |