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