author | unc0rr |
Wed, 24 Jan 2007 22:05:05 +0000 | |
changeset 364 | 52cb4d6f84b7 |
parent 360 | ab6a94334d6d |
child 365 | a26cec847dd7 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
70 | 3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 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 |
|
4 | 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. |
|
4 | 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 |
|
4 | 17 |
*) |
18 |
||
19 |
unit uLand; |
|
20 |
interface |
|
351 | 21 |
uses SDLh, uLandTemplates, uFloat; |
4 | 22 |
{$include options.inc} |
23 |
type TLandArray = packed array[0..1023, 0..2047] of LongWord; |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
24 |
TPreview = packed array[0..127, 0..31] of byte; |
4 | 25 |
|
26 |
var Land: TLandArray; |
|
27 |
LandSurface: PSDL_Surface; |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
28 |
Preview: TPreview; |
4 | 29 |
|
37 | 30 |
procedure GenMap; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
31 |
procedure GenPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
32 |
|
4 | 33 |
|
34 |
implementation |
|
316 | 35 |
uses uConsole, uStore, uMisc, uConsts, uRandom, uTeams, uLandObjects, uSHA, uIO; |
4 | 36 |
|
37 |
type TPixAr = record |
|
38 |
Count: Longword; |
|
22 | 39 |
ar: array[0..Pred(cMaxEdgePoints)] of TPoint; |
4 | 40 |
end; |
41 |
||
37 | 42 |
procedure LogLandDigest; |
316 | 43 |
var ctx: TSHA1Context; |
44 |
dig: TSHA1Digest; |
|
45 |
s: shortstring; |
|
37 | 46 |
begin |
316 | 47 |
SHA1Init(ctx); |
48 |
SHA1Update(ctx, @Land, sizeof(Land)); |
|
49 |
dig:= SHA1Final(ctx); |
|
50 |
s:= '{'+inttostr(dig[0])+':' |
|
51 |
+inttostr(dig[1])+':' |
|
52 |
+inttostr(dig[2])+':' |
|
53 |
+inttostr(dig[3])+':' |
|
54 |
+inttostr(dig[4])+'}'; |
|
55 |
SendIPC('M' + s) |
|
37 | 56 |
end; |
57 |
||
358 | 58 |
procedure DrawLine(X1, Y1, X2, Y2: integer; Color: Longword); |
59 |
var |
|
60 |
eX, eY, dX, dY: integer; |
|
61 |
i, sX, sY, x, y, d: integer; |
|
62 |
begin |
|
63 |
eX:= 0; |
|
64 |
eY:= 0; |
|
65 |
dX:= X2 - X1; |
|
66 |
dY:= Y2 - Y1; |
|
67 |
||
68 |
if (dX > 0) then sX:= 1 |
|
69 |
else |
|
70 |
if (dX < 0) then |
|
71 |
begin |
|
72 |
sX:= -1; |
|
73 |
dX:= -dX |
|
74 |
end else sX:= dX; |
|
75 |
||
76 |
if (dY > 0) then sY:= 1 |
|
77 |
else |
|
78 |
if (dY < 0) then |
|
79 |
begin |
|
80 |
sY:= -1; |
|
81 |
dY:= -dY |
|
82 |
end else sY:= dY; |
|
83 |
||
84 |
if (dX > dY) then d:= dX |
|
85 |
else d:= dY; |
|
86 |
||
87 |
x:= X1; |
|
88 |
y:= Y1; |
|
89 |
||
90 |
for i:= 0 to d do |
|
91 |
begin |
|
92 |
inc(eX, dX); |
|
93 |
inc(eY, dY); |
|
94 |
if (eX > d) then |
|
95 |
begin |
|
96 |
dec(eX, d); |
|
97 |
inc(x, sX); |
|
98 |
end; |
|
99 |
if (eY > d) then |
|
100 |
begin |
|
101 |
dec(eY, d); |
|
102 |
inc(y, sY); |
|
103 |
end; |
|
364 | 104 |
|
358 | 105 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
106 |
Land[y, x]:= Color; |
|
107 |
end |
|
108 |
end; |
|
109 |
||
89 | 110 |
procedure DrawBezierEdge(var pa: TPixAr; Color: Longword); |
358 | 111 |
const dT: hwFloat = (isNegative: false; QWordValue: 85899346); |
112 |
var x, y, i, px, py: integer; |
|
351 | 113 |
tx, ty, vx, vy, vlen, t: hwFloat; |
114 |
r1, r2, r3, r4: hwFloat; |
|
115 |
x1, y1, x2, y2, cx1, cy1, cx2, cy2, tsq, tcb: hwFloat; |
|
4 | 116 |
begin |
117 |
vx:= 0; |
|
118 |
vy:= 0; |
|
119 |
with pa do |
|
120 |
for i:= 0 to Count-2 do |
|
121 |
begin |
|
351 | 122 |
vlen:= Distance(ar[i + 1].x - ar[i].X, ar[i + 1].y - ar[i].y); |
123 |
t:= Distance(ar[i + 1].x - ar[i + 2].X,ar[i + 1].y - ar[i + 2].y); |
|
4 | 124 |
if t<vlen then vlen:= t; |
351 | 125 |
vlen:= vlen * _1div3; |
4 | 126 |
tx:= ar[i+2].X - ar[i].X; |
127 |
ty:= ar[i+2].y - ar[i].y; |
|
351 | 128 |
t:= Distance(tx, ty); |
129 |
if t.QWordValue = 0 then |
|
4 | 130 |
begin |
351 | 131 |
tx:= -tx * 10000; |
132 |
ty:= -ty * 10000; |
|
4 | 133 |
end else |
134 |
begin |
|
351 | 135 |
t:= 1/t; |
136 |
tx:= -tx * t; |
|
137 |
ty:= -ty * t; |
|
4 | 138 |
end; |
351 | 139 |
t:= vlen; |
140 |
tx:= tx * t; |
|
141 |
ty:= ty * t; |
|
4 | 142 |
x1:= ar[i].x; |
143 |
y1:= ar[i].y; |
|
144 |
x2:= ar[i + 1].x; |
|
145 |
y2:= ar[i + 1].y; |
|
351 | 146 |
cx1:= ar[i].X + hwRound(vx); |
147 |
cy1:= ar[i].y + hwRound(vy); |
|
148 |
cx2:= ar[i+1].X + hwRound(tx); |
|
149 |
cy2:= ar[i+1].y + hwRound(ty); |
|
4 | 150 |
vx:= -tx; |
151 |
vy:= -ty; |
|
358 | 152 |
px:= hwRound(x1); |
153 |
py:= hwRound(y1); |
|
154 |
t:= dT; |
|
351 | 155 |
while t.Round = 0 do |
4 | 156 |
begin |
351 | 157 |
tsq:= t * t; |
4 | 158 |
tcb:= tsq * t; |
159 |
r1:= (1 - 3*t + 3*tsq - tcb) * x1; |
|
160 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cx1; |
|
161 |
r3:= ( 3*tsq - 3*tcb) * cx2; |
|
162 |
r4:= ( tcb) * x2; |
|
351 | 163 |
X:= hwRound(r1 + r2 + r3 + r4); |
4 | 164 |
r1:= (1 - 3*t + 3*tsq - tcb) * y1; |
165 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cy1; |
|
166 |
r3:= ( 3*tsq - 3*tcb) * cy2; |
|
167 |
r4:= ( tcb) * y2; |
|
351 | 168 |
Y:= hwRound(r1 + r2 + r3 + r4); |
358 | 169 |
t:= t + dT; |
170 |
DrawLine(px, py, x, y, Color); |
|
171 |
px:= x; |
|
172 |
py:= y |
|
22 | 173 |
end; |
359 | 174 |
DrawLine(px, py, hwRound(x2), hwRound(y2), Color) |
22 | 175 |
end; |
176 |
end; |
|
177 |
||
364 | 178 |
procedure BezierizeEdge(var pa: TPixAr; Delta: hwFloat); |
179 |
var x, y, i: integer; |
|
180 |
tx, ty, vx, vy, vlen, t: hwFloat; |
|
181 |
r1, r2, r3, r4: hwFloat; |
|
182 |
x1, y1, x2, y2, cx1, cy1, cx2, cy2, tsq, tcb: hwFloat; |
|
183 |
opa: TPixAr; |
|
184 |
begin |
|
185 |
opa:= pa; |
|
186 |
pa.Count:= 0; |
|
187 |
vx:= 0; |
|
188 |
vy:= 0; |
|
189 |
with opa do |
|
190 |
for i:= 0 to Count-2 do |
|
191 |
begin |
|
192 |
vlen:= Distance(ar[i + 1].x - ar[i].X, ar[i + 1].y - ar[i].y); |
|
193 |
t:= Distance(ar[i + 1].x - ar[i + 2].X,ar[i + 1].y - ar[i + 2].y); |
|
194 |
if t<vlen then vlen:= t; |
|
195 |
vlen:= vlen * _1div3; |
|
196 |
tx:= ar[i+2].X - ar[i].X; |
|
197 |
ty:= ar[i+2].y - ar[i].y; |
|
198 |
t:= Distance(tx, ty); |
|
199 |
if t.QWordValue = 0 then |
|
200 |
begin |
|
201 |
tx:= -tx * 100000; |
|
202 |
ty:= -ty * 100000; |
|
203 |
end else |
|
204 |
begin |
|
205 |
t:= 1/t; |
|
206 |
tx:= -tx * t; |
|
207 |
ty:= -ty * t; |
|
208 |
end; |
|
209 |
t:= vlen; |
|
210 |
tx:= tx*t; |
|
211 |
ty:= ty*t; |
|
212 |
x1:= ar[i].x; |
|
213 |
y1:= ar[i].y; |
|
214 |
x2:= ar[i + 1].x; |
|
215 |
y2:= ar[i + 1].y; |
|
216 |
cx1:= ar[i].X + hwRound(vx); |
|
217 |
cy1:= ar[i].y + hwRound(vy); |
|
218 |
cx2:= ar[i+1].X + hwRound(tx); |
|
219 |
cy2:= ar[i+1].y + hwRound(ty); |
|
220 |
vx:= -tx; |
|
221 |
vy:= -ty; |
|
222 |
t:= 0; |
|
223 |
while t.Round = 0 do |
|
224 |
begin |
|
225 |
tsq:= t * t; |
|
226 |
tcb:= tsq * t; |
|
227 |
r1:= (1 - 3*t + 3*tsq - tcb) * x1; |
|
228 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cx1; |
|
229 |
r3:= ( 3*tsq - 3*tcb) * cx2; |
|
230 |
r4:= ( tcb) * x2; |
|
231 |
X:= hwRound(r1 + r2 + r3 + r4); |
|
232 |
r1:= (1 - 3*t + 3*tsq - tcb) * y1; |
|
233 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cy1; |
|
234 |
r3:= ( 3*tsq - 3*tcb) * cy2; |
|
235 |
r4:= ( tcb) * y2; |
|
236 |
Y:= hwRound(r1 + r2 + r3 + r4); |
|
237 |
t:= t + Delta; |
|
238 |
pa.ar[pa.Count].x:= X; |
|
239 |
pa.ar[pa.Count].y:= Y; |
|
240 |
inc(pa.Count); |
|
241 |
TryDo(pa.Count <= cMaxEdgePoints, 'Edge points overflow', true) |
|
242 |
end; |
|
243 |
end; |
|
244 |
pa.ar[pa.Count].x:= opa.ar[Pred(opa.Count)].X; |
|
245 |
pa.ar[pa.Count].y:= opa.ar[Pred(opa.Count)].Y; |
|
246 |
inc(pa.Count) |
|
247 |
end; |
|
248 |
||
4 | 249 |
procedure FillLand(x, y: integer); |
250 |
var Stack: record |
|
251 |
Count: Longword; |
|
252 |
points: array[0..8192] of record |
|
253 |
xl, xr, y, dir: integer; |
|
254 |
end |
|
255 |
end; |
|
256 |
||
257 |
procedure Push(_xl, _xr, _y, _dir: integer); |
|
258 |
begin |
|
75 | 259 |
TryDo(Stack.Count <= 8192, 'FillLand: stack overflow', true); |
4 | 260 |
_y:= _y + _dir; |
261 |
if (_y < 0) or (_y > 1023) then exit; |
|
262 |
with Stack.points[Stack.Count] do |
|
263 |
begin |
|
264 |
xl:= _xl; |
|
265 |
xr:= _xr; |
|
266 |
y:= _y; |
|
267 |
dir:= _dir |
|
268 |
end; |
|
75 | 269 |
inc(Stack.Count) |
4 | 270 |
end; |
271 |
||
351 | 272 |
procedure Pop(var _xl, _xr, _y, _dir: integer); |
4 | 273 |
begin |
274 |
dec(Stack.Count); |
|
275 |
with Stack.points[Stack.Count] do |
|
276 |
begin |
|
277 |
_xl:= xl; |
|
278 |
_xr:= xr; |
|
279 |
_y:= y; |
|
280 |
_dir:= dir |
|
281 |
end |
|
282 |
end; |
|
283 |
||
284 |
var xl, xr, dir: integer; |
|
351 | 285 |
begin |
4 | 286 |
Stack.Count:= 0; |
287 |
xl:= x - 1; |
|
288 |
xr:= x; |
|
23 | 289 |
Push(xl, xr, y, -1); |
290 |
Push(xl, xr, y, 1); |
|
4 | 291 |
while Stack.Count > 0 do |
292 |
begin |
|
293 |
Pop(xl, xr, y, dir); |
|
51 | 294 |
while (xl > 0) and (Land[y, xl] <> 0) do dec(xl); |
295 |
while (xr < 2047) and (Land[y, xr] <> 0) do inc(xr); |
|
4 | 296 |
while (xl < xr) do |
297 |
begin |
|
51 | 298 |
while (xl <= xr) and (Land[y, xl] = 0) do inc(xl); |
4 | 299 |
x:= xl; |
51 | 300 |
while (xl <= xr) and (Land[y, xl] <> 0) do |
4 | 301 |
begin |
51 | 302 |
Land[y, xl]:= 0; |
4 | 303 |
inc(xl) |
304 |
end; |
|
22 | 305 |
if x < xl then |
306 |
begin |
|
307 |
Push(x, Pred(xl), y, dir); |
|
308 |
Push(x, Pred(xl), y,-dir); |
|
309 |
end; |
|
4 | 310 |
end; |
311 |
end; |
|
312 |
end; |
|
313 |
||
314 |
procedure ColorizeLand(Surface: PSDL_Surface); |
|
315 |
var tmpsurf: PSDL_Surface; |
|
316 |
r: TSDL_Rect; |
|
317 |
begin |
|
351 | 318 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', false, true, true); |
4 | 319 |
r.y:= 0; |
320 |
while r.y < 1024 do |
|
321 |
begin |
|
322 |
r.x:= 0; |
|
323 |
while r.x < 2048 do |
|
324 |
begin |
|
325 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
|
351 | 326 |
inc(r.x, tmpsurf^.w) |
4 | 327 |
end; |
351 | 328 |
inc(r.y, tmpsurf^.h) |
4 | 329 |
end; |
330 |
SDL_FreeSurface(tmpsurf); |
|
331 |
||
332 |
tmpsurf:= SDL_CreateRGBSurfaceFrom(@Land, 2048, 1024, 32, 2048*4, $FF0000, $FF00, $FF, 0); |
|
333 |
SDLTry(tmpsurf <> nil, true); |
|
351 | 334 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, SDL_MapRGB(tmpsurf^.format, $FF, $FF, $FF)); |
22 | 335 |
SDL_UpperBlit(tmpsurf, nil, Surface, nil); |
336 |
SDL_FreeSurface(tmpsurf) |
|
4 | 337 |
end; |
338 |
||
339 |
procedure AddBorder(Surface: PSDL_Surface); |
|
340 |
var tmpsurf: PSDL_Surface; |
|
341 |
r, rr: TSDL_Rect; |
|
342 |
x, yd, yu: integer; |
|
343 |
begin |
|
351 | 344 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', false, true, true); |
4 | 345 |
for x:= 0 to 2047 do |
346 |
begin |
|
347 |
yd:= 1023; |
|
348 |
repeat |
|
349 |
while (yd > 0 ) and (Land[yd, x] = 0) do dec(yd); |
|
350 |
if (yd < 0) then yd:= 0; |
|
351 |
while (yd < 1024) and (Land[yd, x] <> 0) do inc(yd); |
|
352 |
dec(yd); |
|
353 |
yu:= yd; |
|
354 |
while (yu > 0 ) and (Land[yu, x] <> 0) do dec(yu); |
|
355 |
while (yu < yd ) and (Land[yu, x] = 0) do inc(yu); |
|
356 |
if (yd < 1023) and ((yd - yu) >= 16) then |
|
357 |
begin |
|
358 |
rr.x:= x; |
|
359 |
rr.y:= yd - 15; |
|
351 | 360 |
r.x:= x mod tmpsurf^.w; |
4 | 361 |
r.y:= 16; |
362 |
r.w:= 1; |
|
363 |
r.h:= 16; |
|
364 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
365 |
end; |
|
366 |
if (yu > 0) then |
|
367 |
begin |
|
368 |
rr.x:= x; |
|
369 |
rr.y:= yu; |
|
351 | 370 |
r.x:= x mod tmpsurf^.w; |
4 | 371 |
r.y:= 0; |
372 |
r.w:= 1; |
|
373 |
r.h:= min(16, yd - yu + 1); |
|
374 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
375 |
end; |
|
376 |
yd:= yu - 1; |
|
377 |
until yd < 0; |
|
378 |
end; |
|
379 |
end; |
|
380 |
||
358 | 381 |
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr); |
382 |
var i: integer; |
|
22 | 383 |
begin |
23 | 384 |
with Template do |
385 |
begin |
|
358 | 386 |
pa.Count:= BasePointsCount; |
387 |
for i:= 0 to pred(pa.Count) do |
|
23 | 388 |
begin |
358 | 389 |
pa.ar[i].x:= BasePoints^[i].x + integer(GetRandom(BasePoints^[i].w)); |
390 |
pa.ar[i].y:= BasePoints^[i].y + integer(GetRandom(BasePoints^[i].h)) |
|
23 | 391 |
end; |
358 | 392 |
|
393 |
if canMirror then |
|
360 | 394 |
if getrandom(2) = 0 then |
358 | 395 |
begin |
396 |
for i:= 0 to pred(BasePointsCount) do |
|
360 | 397 |
pa.ar[i].x:= 2047 - pa.ar[i].x; |
358 | 398 |
for i:= 0 to pred(FillPointsCount) do |
399 |
FillPoints^[i].x:= 2047 - FillPoints^[i].x; |
|
400 |
end; |
|
22 | 401 |
|
358 | 402 |
if canFlip then |
360 | 403 |
if getrandom(2) = 0 then |
358 | 404 |
begin |
405 |
for i:= 0 to pred(BasePointsCount) do |
|
360 | 406 |
pa.ar[i].y:= 1023 - pa.ar[i].y; |
358 | 407 |
for i:= 0 to pred(FillPointsCount) do |
408 |
FillPoints^[i].y:= 1023 - FillPoints^[i].y; |
|
409 |
end; |
|
410 |
end |
|
4 | 411 |
end; |
364 | 412 |
(* |
67 | 413 |
procedure NormalizePoints(var pa: TPixAr); |
414 |
const brd = 32; |
|
351 | 415 |
var isUP: boolean; // HACK: transform for Y should be exact as one for X |
67 | 416 |
Left, Right, Top, Bottom, |
417 |
OWidth, Width, OHeight, Height, |
|
418 |
OLeft: integer; |
|
419 |
i: integer; |
|
420 |
begin |
|
421 |
TryDo((pa.ar[0].y < 0) or (pa.ar[0].y > 1023), 'Bad land generated', true); |
|
160 | 422 |
TryDo((pa.ar[Pred(pa.Count)].y < 0) or (pa.ar[Pred(pa.Count)].y > 1023), 'Bad land generated', true); |
67 | 423 |
isUP:= pa.ar[0].y > 0; |
424 |
Left:= 1023; |
|
425 |
Right:= Left; |
|
426 |
Top:= pa.ar[0].y; |
|
427 |
Bottom:= Top; |
|
428 |
||
429 |
for i:= 1 to Pred(pa.Count) do |
|
430 |
with pa.ar[i] do |
|
431 |
begin |
|
432 |
if (y and $FFFFFC00) = 0 then |
|
433 |
if x < Left then Left:= x else |
|
434 |
if x > Right then Right:= x; |
|
435 |
if y < Top then Top:= y else |
|
436 |
if y > Bottom then Bottom:= y |
|
437 |
end; |
|
438 |
||
439 |
if (Left < brd) or (Right > 2047 - brd) then |
|
440 |
begin |
|
441 |
OLeft:= Left; |
|
442 |
OWidth:= Right - OLeft; |
|
443 |
if Left < brd then Left:= brd; |
|
444 |
if Right > 2047 - brd then Right:= 2047 - brd; |
|
445 |
Width:= Right - Left; |
|
446 |
for i:= 0 to Pred(pa.Count) do |
|
447 |
with pa.ar[i] do |
|
448 |
x:= round((x - OLeft) * Width div OWidth + Left) |
|
449 |
end; |
|
450 |
||
451 |
if isUp then // FIXME: remove hack |
|
452 |
if Top < brd then |
|
453 |
begin |
|
454 |
OHeight:= 1023 - Top; |
|
455 |
Height:= 1023 - brd; |
|
456 |
for i:= 0 to Pred(pa.Count) do |
|
457 |
with pa.ar[i] do |
|
458 |
y:= round((y - 1023) * Height div OHeight + 1023) |
|
459 |
end; |
|
364 | 460 |
end;*) |
461 |
||
462 |
procedure RandomizePoints(var pa: TPixAr); |
|
463 |
const cEdge = 55; |
|
464 |
cMinDist = 14; |
|
465 |
var radz: array[0..Pred(cMaxEdgePoints)] of integer; |
|
466 |
i, k, dist: integer; |
|
467 |
begin |
|
468 |
radz[0]:= 0; |
|
469 |
for i:= 0 to Pred(pa.Count) do |
|
470 |
with pa.ar[i] do |
|
471 |
begin |
|
472 |
radz[i]:= Min(Max(x - cEdge, 0), Max(2048 - cEdge - x, 0)); |
|
473 |
radz[i]:= Min(radz[i], Min(Max(y - cEdge, 0), Max(1024 - cEdge - y, 0))); |
|
474 |
if radz[i] > 0 then |
|
475 |
for k:= 0 to Pred(i) do |
|
476 |
begin |
|
477 |
dist:= Min(Max(abs(x - pa.ar[k].x), abs(y - pa.ar[k].y)), 50); |
|
478 |
if radz[k] >= dist then |
|
479 |
begin |
|
480 |
radz[k]:= Max(0, dist - cMinDist * 2); |
|
481 |
radz[i]:= Min(dist - radz[k], radz[i]) |
|
482 |
end; |
|
483 |
radz[i]:= Min(radz[i], dist) |
|
484 |
end |
|
485 |
end; |
|
486 |
||
487 |
for i:= 0 to Pred(pa.Count) do |
|
488 |
with pa.ar[i] do |
|
489 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
|
490 |
begin |
|
491 |
x:= x + integer(GetRandom(radz[i] * 2 + 1)) - radz[i]; |
|
492 |
y:= y + integer(GetRandom(radz[i] * 2 + 1)) - radz[i] |
|
493 |
end |
|
67 | 494 |
end; |
495 |
||
364 | 496 |
|
23 | 497 |
procedure GenBlank(var Template: TEdgeTemplate); |
4 | 498 |
var pa: TPixAr; |
23 | 499 |
i: Longword; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
500 |
y, x: Longword; |
4 | 501 |
begin |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
502 |
for y:= 0 to 1023 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
503 |
for x:= 0 to 2047 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
504 |
Land[y, x]:= COLOR_LAND; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
505 |
|
358 | 506 |
SetPoints(Template, pa); |
364 | 507 |
BezierizeEdge(pa, _1div3); |
508 |
for i:= 0 to Pred(Template.RandPassesCount) do RandomizePoints(pa); |
|
509 |
//NormalizePoints(pa); |
|
27 | 510 |
|
358 | 511 |
DrawBezierEdge(pa, 0); |
27 | 512 |
|
358 | 513 |
with Template do |
23 | 514 |
for i:= 0 to pred(FillPointsCount) do |
515 |
with FillPoints^[i] do |
|
89 | 516 |
FillLand(x, y); |
517 |
||
358 | 518 |
DrawBezierEdge(pa, COLOR_LAND) |
23 | 519 |
end; |
520 |
||
161 | 521 |
function SelectTemplate: integer; |
522 |
begin |
|
351 | 523 |
SelectTemplate:= getrandom(Succ(High(EdgeTemplates))) |
161 | 524 |
end; |
525 |
||
23 | 526 |
procedure GenLandSurface; |
527 |
var tmpsurf: PSDL_Surface; |
|
528 |
begin |
|
53 | 529 |
WriteLnToConsole('Generating land...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
530 |
|
161 | 531 |
GenBlank(EdgeTemplates[SelectTemplate]); |
22 | 532 |
|
4 | 533 |
AddProgress; |
534 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
535 |
tmpsurf:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 536 |
TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true); |
4 | 537 |
ColorizeLand(tmpsurf); |
538 |
AddProgress; |
|
539 |
AddBorder(tmpsurf); |
|
540 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
541 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 542 |
TryDo(LandSurface <> nil, 'Error creating land surface', true); |
4 | 543 |
SDL_FillRect(LandSurface, nil, 0); |
27 | 544 |
AddProgress; |
24 | 545 |
|
70 | 546 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0); |
547 |
AddObjects(tmpsurf, LandSurface); |
|
548 |
SDL_FreeSurface(tmpsurf); |
|
24 | 549 |
|
70 | 550 |
AddProgress |
4 | 551 |
end; |
552 |
||
553 |
procedure MakeFortsMap; |
|
554 |
var p: PTeam; |
|
555 |
tmpsurf: PSDL_Surface; |
|
556 |
begin |
|
53 | 557 |
WriteLnToConsole('Generating forts land...'); |
4 | 558 |
p:= TeamsList; |
559 |
TryDo(p <> nil, 'No teams on map!', true); |
|
560 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
561 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
37 | 562 |
SDL_FillRect(LandSurface, nil, 0); |
351 | 563 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'L', false, true, true); |
4 | 564 |
BlitImageAndGenerateCollisionInfo(0, 0, tmpsurf, LandSurface); |
565 |
SDL_FreeSurface(tmpsurf); |
|
351 | 566 |
p:= p^.Next; |
4 | 567 |
TryDo(p <> nil, 'Only one team on map!', true); |
351 | 568 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'R', false, true, true); |
4 | 569 |
BlitImageAndGenerateCollisionInfo(1024, 0, tmpsurf, LandSurface); |
570 |
SDL_FreeSurface(tmpsurf); |
|
351 | 571 |
p:= p^.Next; |
4 | 572 |
TryDo(p = nil, 'More than 2 teams on map in forts mode!', true); |
573 |
end; |
|
574 |
||
53 | 575 |
procedure LoadMap; |
107 | 576 |
var x, y: Longword; |
577 |
p: PByteArray; |
|
53 | 578 |
begin |
579 |
WriteLnToConsole('Loading land from file...'); |
|
580 |
AddProgress; |
|
351 | 581 |
LandSurface:= LoadImage(Pathz[ptMapCurrent] + '/map', false, true, true); |
582 |
TryDo((LandSurface^.w = 2048) and (LandSurface^.h = 1024), 'Map dimensions should be 2048x1024!', true); |
|
53 | 583 |
|
584 |
if SDL_MustLock(LandSurface) then |
|
585 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true); |
|
586 |
||
351 | 587 |
p:= LandSurface^.pixels; |
588 |
case LandSurface^.format^.BytesPerPixel of |
|
53 | 589 |
1: OutError('We don''t work with 8 bit surfaces', true); |
590 |
2: for y:= 0 to 1023 do |
|
591 |
begin |
|
592 |
for x:= 0 to 2047 do |
|
351 | 593 |
if PWord(@(p^[x * 2]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
594 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 595 |
end; |
596 |
3: for y:= 0 to 1023 do |
|
597 |
begin |
|
598 |
for x:= 0 to 2047 do |
|
351 | 599 |
if (p^[x * 3 + 0] <> 0) |
600 |
or (p^[x * 3 + 1] <> 0) |
|
601 |
or (p^[x * 3 + 2] <> 0) then Land[y, x]:= COLOR_LAND; |
|
602 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 603 |
end; |
604 |
4: for y:= 0 to 1023 do |
|
605 |
begin |
|
606 |
for x:= 0 to 2047 do |
|
351 | 607 |
if PLongword(@(p^[x * 4]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
608 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 609 |
end; |
610 |
end; |
|
351 | 611 |
|
53 | 612 |
if SDL_MustLock(LandSurface) then |
613 |
SDL_UnlockSurface(LandSurface); |
|
614 |
end; |
|
615 |
||
37 | 616 |
procedure GenMap; |
617 |
begin |
|
53 | 618 |
if (GameFlags and gfForts) = 0 then |
619 |
if Pathz[ptMapCurrent] <> '' then LoadMap |
|
620 |
else GenLandSurface |
|
37 | 621 |
else MakeFortsMap; |
622 |
AddProgress; |
|
623 |
{$IFDEF DEBUGFILE}LogLandDigest{$ENDIF} |
|
624 |
end; |
|
625 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
626 |
procedure GenPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
627 |
var x, y, xx, yy, t, bit: integer; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
628 |
begin |
160 | 629 |
WriteLnToConsole('Generating preview...'); |
161 | 630 |
GenBlank(EdgeTemplates[SelectTemplate]); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
631 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
632 |
for y:= 0 to 127 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
633 |
for x:= 0 to 31 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
634 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
635 |
Preview[y, x]:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
636 |
for bit:= 0 to 7 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
637 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
638 |
t:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
639 |
for yy:= y * 8 to y * 8 + 7 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
640 |
for xx:= x * 64 + bit * 8 to x * 64 + bit * 8 + 7 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
641 |
if Land[yy, xx] <> 0 then inc(t); |
351 | 642 |
if t > 8 then Preview[y, x]:= Preview[y, x] or ($80 shr bit) |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
643 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
644 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
645 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
646 |
|
51 | 647 |
initialization |
648 |
||
4 | 649 |
end. |