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