author | unc0rr |
Sun, 28 Jan 2007 16:40:04 +0000 | |
changeset 374 | 95169697cc38 |
parent 371 | 731ad6d27bd1 |
child 393 | db01cc79f278 |
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; |
367 | 32 |
procedure CheckLandDigest(s: shortstring); |
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); |
|
367 | 50 |
s:='M{'+inttostr(dig[0])+':' |
316 | 51 |
+inttostr(dig[1])+':' |
52 |
+inttostr(dig[2])+':' |
|
53 |
+inttostr(dig[3])+':' |
|
54 |
+inttostr(dig[4])+'}'; |
|
367 | 55 |
CheckLandDigest(s); |
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 |
|
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 |
365 | 139 |
Vx:= p1.X - p3.X; |
140 |
Vy:= p1.Y - p3.Y; |
|
141 |
d:= Distance(p2.X - p1.X, p2.Y - p1.Y); |
|
142 |
d1:= Distance(p2.X - p3.X, p2.Y - p3.Y); |
|
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 |
|
149 |
Vx:= 0; |
|
150 |
Vy:= 0 |
|
151 |
end else |
|
152 |
begin |
|
153 |
d2:= 1 / d2; |
|
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; |
165 |
x1, x2, y1, y2, cx1, cx2, cy1, cy2: hwFloat; |
|
166 |
tsq, tcb, t, r1, r2, r3, r4: 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; |
|
188 |
cx1:= x1 - PVx; |
|
189 |
cy1:= y1 - PVy; |
|
190 |
cx2:= x2 + NVx; |
|
191 |
cy2:= y2 + NVy; |
|
364 | 192 |
t:= 0; |
193 |
while t.Round = 0 do |
|
194 |
begin |
|
195 |
tsq:= t * t; |
|
196 |
tcb:= tsq * t; |
|
197 |
r1:= (1 - 3*t + 3*tsq - tcb) * x1; |
|
198 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cx1; |
|
199 |
r3:= ( 3*tsq - 3*tcb) * cx2; |
|
200 |
r4:= ( tcb) * x2; |
|
201 |
X:= hwRound(r1 + r2 + r3 + r4); |
|
202 |
r1:= (1 - 3*t + 3*tsq - tcb) * y1; |
|
203 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cy1; |
|
204 |
r3:= ( 3*tsq - 3*tcb) * cy2; |
|
205 |
r4:= ( tcb) * y2; |
|
206 |
Y:= hwRound(r1 + r2 + r3 + r4); |
|
207 |
t:= t + Delta; |
|
208 |
pa.ar[pa.Count].x:= X; |
|
209 |
pa.ar[pa.Count].y:= Y; |
|
210 |
inc(pa.Count); |
|
211 |
TryDo(pa.Count <= cMaxEdgePoints, 'Edge points overflow', true) |
|
212 |
end; |
|
365 | 213 |
until i = StartI; |
214 |
pa.ar[pa.Count].x:= opa.ar[StartI].X; |
|
215 |
pa.ar[pa.Count].y:= opa.ar[StartI].Y; |
|
364 | 216 |
inc(pa.Count) |
217 |
end; |
|
218 |
||
365 | 219 |
procedure BezierizeEdge(var pa: TPixAr; Delta: hwFloat); |
371 | 220 |
var x, y, i, StartLoop: LongInt; |
365 | 221 |
opa: TPixAr; |
222 |
begin |
|
223 |
opa:= pa; |
|
224 |
pa.Count:= 0; |
|
225 |
i:= 0; |
|
226 |
StartLoop:= 0; |
|
371 | 227 |
while i < LongInt(opa.Count) do |
365 | 228 |
if (opa.ar[i + 1].X = NTPX) then |
229 |
begin |
|
230 |
AddLoopPoints(pa, opa, StartLoop, i, Delta); |
|
231 |
inc(i, 2); |
|
232 |
StartLoop:= i; |
|
233 |
pa.ar[pa.Count].X:= NTPX; |
|
234 |
inc(pa.Count); |
|
235 |
end else inc(i) |
|
236 |
end; |
|
237 |
||
371 | 238 |
procedure FillLand(x, y: LongInt); |
4 | 239 |
var Stack: record |
240 |
Count: Longword; |
|
241 |
points: array[0..8192] of record |
|
371 | 242 |
xl, xr, y, dir: LongInt; |
4 | 243 |
end |
244 |
end; |
|
245 |
||
371 | 246 |
procedure Push(_xl, _xr, _y, _dir: LongInt); |
4 | 247 |
begin |
75 | 248 |
TryDo(Stack.Count <= 8192, 'FillLand: stack overflow', true); |
4 | 249 |
_y:= _y + _dir; |
250 |
if (_y < 0) or (_y > 1023) then exit; |
|
251 |
with Stack.points[Stack.Count] do |
|
252 |
begin |
|
253 |
xl:= _xl; |
|
254 |
xr:= _xr; |
|
255 |
y:= _y; |
|
256 |
dir:= _dir |
|
257 |
end; |
|
75 | 258 |
inc(Stack.Count) |
4 | 259 |
end; |
260 |
||
371 | 261 |
procedure Pop(var _xl, _xr, _y, _dir: LongInt); |
4 | 262 |
begin |
263 |
dec(Stack.Count); |
|
264 |
with Stack.points[Stack.Count] do |
|
265 |
begin |
|
266 |
_xl:= xl; |
|
267 |
_xr:= xr; |
|
268 |
_y:= y; |
|
269 |
_dir:= dir |
|
270 |
end |
|
271 |
end; |
|
272 |
||
371 | 273 |
var xl, xr, dir: LongInt; |
351 | 274 |
begin |
4 | 275 |
Stack.Count:= 0; |
276 |
xl:= x - 1; |
|
277 |
xr:= x; |
|
23 | 278 |
Push(xl, xr, y, -1); |
279 |
Push(xl, xr, y, 1); |
|
4 | 280 |
while Stack.Count > 0 do |
281 |
begin |
|
282 |
Pop(xl, xr, y, dir); |
|
51 | 283 |
while (xl > 0) and (Land[y, xl] <> 0) do dec(xl); |
284 |
while (xr < 2047) and (Land[y, xr] <> 0) do inc(xr); |
|
4 | 285 |
while (xl < xr) do |
286 |
begin |
|
51 | 287 |
while (xl <= xr) and (Land[y, xl] = 0) do inc(xl); |
4 | 288 |
x:= xl; |
51 | 289 |
while (xl <= xr) and (Land[y, xl] <> 0) do |
4 | 290 |
begin |
51 | 291 |
Land[y, xl]:= 0; |
4 | 292 |
inc(xl) |
293 |
end; |
|
22 | 294 |
if x < xl then |
295 |
begin |
|
296 |
Push(x, Pred(xl), y, dir); |
|
297 |
Push(x, Pred(xl), y,-dir); |
|
298 |
end; |
|
4 | 299 |
end; |
300 |
end; |
|
301 |
end; |
|
302 |
||
303 |
procedure ColorizeLand(Surface: PSDL_Surface); |
|
304 |
var tmpsurf: PSDL_Surface; |
|
305 |
r: TSDL_Rect; |
|
306 |
begin |
|
351 | 307 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', false, true, true); |
4 | 308 |
r.y:= 0; |
309 |
while r.y < 1024 do |
|
310 |
begin |
|
311 |
r.x:= 0; |
|
312 |
while r.x < 2048 do |
|
313 |
begin |
|
314 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
|
351 | 315 |
inc(r.x, tmpsurf^.w) |
4 | 316 |
end; |
351 | 317 |
inc(r.y, tmpsurf^.h) |
4 | 318 |
end; |
319 |
SDL_FreeSurface(tmpsurf); |
|
320 |
||
321 |
tmpsurf:= SDL_CreateRGBSurfaceFrom(@Land, 2048, 1024, 32, 2048*4, $FF0000, $FF00, $FF, 0); |
|
322 |
SDLTry(tmpsurf <> nil, true); |
|
351 | 323 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, SDL_MapRGB(tmpsurf^.format, $FF, $FF, $FF)); |
22 | 324 |
SDL_UpperBlit(tmpsurf, nil, Surface, nil); |
325 |
SDL_FreeSurface(tmpsurf) |
|
4 | 326 |
end; |
327 |
||
328 |
procedure AddBorder(Surface: PSDL_Surface); |
|
329 |
var tmpsurf: PSDL_Surface; |
|
330 |
r, rr: TSDL_Rect; |
|
371 | 331 |
x, yd, yu: LongInt; |
4 | 332 |
begin |
351 | 333 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', false, true, true); |
4 | 334 |
for x:= 0 to 2047 do |
335 |
begin |
|
336 |
yd:= 1023; |
|
337 |
repeat |
|
338 |
while (yd > 0 ) and (Land[yd, x] = 0) do dec(yd); |
|
339 |
if (yd < 0) then yd:= 0; |
|
340 |
while (yd < 1024) and (Land[yd, x] <> 0) do inc(yd); |
|
341 |
dec(yd); |
|
342 |
yu:= yd; |
|
343 |
while (yu > 0 ) and (Land[yu, x] <> 0) do dec(yu); |
|
344 |
while (yu < yd ) and (Land[yu, x] = 0) do inc(yu); |
|
345 |
if (yd < 1023) and ((yd - yu) >= 16) then |
|
346 |
begin |
|
347 |
rr.x:= x; |
|
348 |
rr.y:= yd - 15; |
|
351 | 349 |
r.x:= x mod tmpsurf^.w; |
4 | 350 |
r.y:= 16; |
351 |
r.w:= 1; |
|
352 |
r.h:= 16; |
|
353 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
354 |
end; |
|
355 |
if (yu > 0) then |
|
356 |
begin |
|
357 |
rr.x:= x; |
|
358 |
rr.y:= yu; |
|
351 | 359 |
r.x:= x mod tmpsurf^.w; |
4 | 360 |
r.y:= 0; |
361 |
r.w:= 1; |
|
362 |
r.h:= min(16, yd - yu + 1); |
|
363 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
364 |
end; |
|
365 |
yd:= yu - 1; |
|
366 |
until yd < 0; |
|
367 |
end; |
|
368 |
end; |
|
369 |
||
358 | 370 |
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr); |
371 | 371 |
var i: LongInt; |
22 | 372 |
begin |
23 | 373 |
with Template do |
374 |
begin |
|
358 | 375 |
pa.Count:= BasePointsCount; |
376 |
for i:= 0 to pred(pa.Count) do |
|
23 | 377 |
begin |
371 | 378 |
pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w)); |
379 |
pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) |
|
23 | 380 |
end; |
358 | 381 |
|
382 |
if canMirror then |
|
360 | 383 |
if getrandom(2) = 0 then |
358 | 384 |
begin |
385 |
for i:= 0 to pred(BasePointsCount) do |
|
365 | 386 |
if pa.ar[i].x <> NTPX then |
360 | 387 |
pa.ar[i].x:= 2047 - pa.ar[i].x; |
358 | 388 |
for i:= 0 to pred(FillPointsCount) do |
389 |
FillPoints^[i].x:= 2047 - FillPoints^[i].x; |
|
390 |
end; |
|
22 | 391 |
|
358 | 392 |
if canFlip then |
360 | 393 |
if getrandom(2) = 0 then |
358 | 394 |
begin |
395 |
for i:= 0 to pred(BasePointsCount) do |
|
360 | 396 |
pa.ar[i].y:= 1023 - pa.ar[i].y; |
358 | 397 |
for i:= 0 to pred(FillPointsCount) do |
398 |
FillPoints^[i].y:= 1023 - FillPoints^[i].y; |
|
399 |
end; |
|
400 |
end |
|
4 | 401 |
end; |
67 | 402 |
|
371 | 403 |
procedure RandomizePoints(var pa: TPixAr; MaxRad: LongInt); |
364 | 404 |
const cEdge = 55; |
365 | 405 |
cMinDist = 0; |
371 | 406 |
var radz: array[0..Pred(cMaxEdgePoints)] of LongInt; |
407 |
i, k, dist: LongInt; |
|
364 | 408 |
begin |
409 |
radz[0]:= 0; |
|
410 |
for i:= 0 to Pred(pa.Count) do |
|
411 |
with pa.ar[i] do |
|
365 | 412 |
if x <> NTPX then |
413 |
begin |
|
414 |
radz[i]:= Min(Max(x - cEdge, 0), Max(2048 - cEdge - x, 0)); |
|
415 |
radz[i]:= Min(radz[i], Min(Max(y - cEdge, 0), Max(1024 - cEdge - y, 0))); |
|
416 |
if radz[i] > 0 then |
|
417 |
for k:= 0 to Pred(i) do |
|
364 | 418 |
begin |
365 | 419 |
dist:= Min(Max(abs(x - pa.ar[k].x), abs(y - pa.ar[k].y)), MaxRad); |
420 |
radz[k]:= Max(0, Min((dist - cMinDist) div 2, radz[k])); |
|
421 |
radz[i]:= Max(0, Min(dist - radz[k] - cMinDist, radz[i])) |
|
422 |
end |
|
423 |
end; |
|
364 | 424 |
|
425 |
for i:= 0 to Pred(pa.Count) do |
|
426 |
with pa.ar[i] do |
|
427 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
|
428 |
begin |
|
371 | 429 |
x:= x + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3; |
430 |
y:= y + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3 |
|
364 | 431 |
end |
67 | 432 |
end; |
433 |
||
364 | 434 |
|
23 | 435 |
procedure GenBlank(var Template: TEdgeTemplate); |
4 | 436 |
var pa: TPixAr; |
23 | 437 |
i: Longword; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
438 |
y, x: Longword; |
4 | 439 |
begin |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
440 |
for y:= 0 to 1023 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
441 |
for x:= 0 to 2047 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
442 |
Land[y, x]:= COLOR_LAND; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
443 |
|
358 | 444 |
SetPoints(Template, pa); |
364 | 445 |
BezierizeEdge(pa, _1div3); |
365 | 446 |
for i:= 0 to Pred(Template.RandPassesCount) do RandomizePoints(pa, 1000); |
447 |
BezierizeEdge(pa, _1div3); |
|
448 |
RandomizePoints(pa, 1000); |
|
449 |
BezierizeEdge(pa, _0_1); |
|
27 | 450 |
|
365 | 451 |
DrawEdge(pa, 0); |
27 | 452 |
|
358 | 453 |
with Template do |
23 | 454 |
for i:= 0 to pred(FillPointsCount) do |
455 |
with FillPoints^[i] do |
|
89 | 456 |
FillLand(x, y); |
457 |
||
365 | 458 |
DrawEdge(pa, COLOR_LAND) |
23 | 459 |
end; |
460 |
||
371 | 461 |
function SelectTemplate: LongInt; |
161 | 462 |
begin |
351 | 463 |
SelectTemplate:= getrandom(Succ(High(EdgeTemplates))) |
161 | 464 |
end; |
465 |
||
23 | 466 |
procedure GenLandSurface; |
467 |
var tmpsurf: PSDL_Surface; |
|
468 |
begin |
|
53 | 469 |
WriteLnToConsole('Generating land...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
470 |
|
161 | 471 |
GenBlank(EdgeTemplates[SelectTemplate]); |
22 | 472 |
|
4 | 473 |
AddProgress; |
474 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
475 |
tmpsurf:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 476 |
TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true); |
4 | 477 |
ColorizeLand(tmpsurf); |
478 |
AddProgress; |
|
479 |
AddBorder(tmpsurf); |
|
480 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
481 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 482 |
TryDo(LandSurface <> nil, 'Error creating land surface', true); |
4 | 483 |
SDL_FillRect(LandSurface, nil, 0); |
27 | 484 |
AddProgress; |
24 | 485 |
|
70 | 486 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0); |
487 |
AddObjects(tmpsurf, LandSurface); |
|
488 |
SDL_FreeSurface(tmpsurf); |
|
24 | 489 |
|
70 | 490 |
AddProgress |
4 | 491 |
end; |
492 |
||
493 |
procedure MakeFortsMap; |
|
494 |
var p: PTeam; |
|
495 |
tmpsurf: PSDL_Surface; |
|
496 |
begin |
|
53 | 497 |
WriteLnToConsole('Generating forts land...'); |
4 | 498 |
p:= TeamsList; |
499 |
TryDo(p <> nil, 'No teams on map!', true); |
|
500 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
501 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
37 | 502 |
SDL_FillRect(LandSurface, nil, 0); |
351 | 503 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'L', false, true, true); |
4 | 504 |
BlitImageAndGenerateCollisionInfo(0, 0, tmpsurf, LandSurface); |
505 |
SDL_FreeSurface(tmpsurf); |
|
351 | 506 |
p:= p^.Next; |
4 | 507 |
TryDo(p <> nil, 'Only one team on map!', true); |
351 | 508 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'R', false, true, true); |
4 | 509 |
BlitImageAndGenerateCollisionInfo(1024, 0, tmpsurf, LandSurface); |
510 |
SDL_FreeSurface(tmpsurf); |
|
351 | 511 |
p:= p^.Next; |
4 | 512 |
TryDo(p = nil, 'More than 2 teams on map in forts mode!', true); |
513 |
end; |
|
514 |
||
53 | 515 |
procedure LoadMap; |
107 | 516 |
var x, y: Longword; |
517 |
p: PByteArray; |
|
53 | 518 |
begin |
519 |
WriteLnToConsole('Loading land from file...'); |
|
520 |
AddProgress; |
|
351 | 521 |
LandSurface:= LoadImage(Pathz[ptMapCurrent] + '/map', false, true, true); |
522 |
TryDo((LandSurface^.w = 2048) and (LandSurface^.h = 1024), 'Map dimensions should be 2048x1024!', true); |
|
53 | 523 |
|
524 |
if SDL_MustLock(LandSurface) then |
|
525 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true); |
|
526 |
||
351 | 527 |
p:= LandSurface^.pixels; |
528 |
case LandSurface^.format^.BytesPerPixel of |
|
53 | 529 |
1: OutError('We don''t work with 8 bit surfaces', true); |
530 |
2: for y:= 0 to 1023 do |
|
531 |
begin |
|
532 |
for x:= 0 to 2047 do |
|
351 | 533 |
if PWord(@(p^[x * 2]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
534 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 535 |
end; |
536 |
3: for y:= 0 to 1023 do |
|
537 |
begin |
|
538 |
for x:= 0 to 2047 do |
|
351 | 539 |
if (p^[x * 3 + 0] <> 0) |
540 |
or (p^[x * 3 + 1] <> 0) |
|
541 |
or (p^[x * 3 + 2] <> 0) then Land[y, x]:= COLOR_LAND; |
|
542 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 543 |
end; |
544 |
4: for y:= 0 to 1023 do |
|
545 |
begin |
|
546 |
for x:= 0 to 2047 do |
|
351 | 547 |
if PLongword(@(p^[x * 4]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
548 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 549 |
end; |
550 |
end; |
|
351 | 551 |
|
53 | 552 |
if SDL_MustLock(LandSurface) then |
553 |
SDL_UnlockSurface(LandSurface); |
|
554 |
end; |
|
555 |
||
37 | 556 |
procedure GenMap; |
557 |
begin |
|
53 | 558 |
if (GameFlags and gfForts) = 0 then |
559 |
if Pathz[ptMapCurrent] <> '' then LoadMap |
|
560 |
else GenLandSurface |
|
37 | 561 |
else MakeFortsMap; |
562 |
AddProgress; |
|
563 |
{$IFDEF DEBUGFILE}LogLandDigest{$ENDIF} |
|
564 |
end; |
|
565 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
566 |
procedure GenPreview; |
371 | 567 |
var x, y, xx, yy, t, bit: LongInt; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
568 |
begin |
160 | 569 |
WriteLnToConsole('Generating preview...'); |
161 | 570 |
GenBlank(EdgeTemplates[SelectTemplate]); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
571 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
572 |
for y:= 0 to 127 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
573 |
for x:= 0 to 31 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
574 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
575 |
Preview[y, x]:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
576 |
for bit:= 0 to 7 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
577 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
578 |
t:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
579 |
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
|
580 |
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
|
581 |
if Land[yy, xx] <> 0 then inc(t); |
351 | 582 |
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
|
583 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
584 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
585 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
586 |
|
51 | 587 |
initialization |
588 |
||
4 | 589 |
end. |