author | unc0rr |
Tue, 23 Jan 2007 16:59:05 +0000 | |
changeset 358 | 236bbd12d4d9 |
parent 351 | 29bc9c36ad5f |
child 359 | 59fbfc65fbda |
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; |
|
104 |
||
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; |
174 |
end; |
|
175 |
end; |
|
176 |
||
4 | 177 |
procedure FillLand(x, y: integer); |
178 |
var Stack: record |
|
179 |
Count: Longword; |
|
180 |
points: array[0..8192] of record |
|
181 |
xl, xr, y, dir: integer; |
|
182 |
end |
|
183 |
end; |
|
184 |
||
185 |
procedure Push(_xl, _xr, _y, _dir: integer); |
|
186 |
begin |
|
75 | 187 |
TryDo(Stack.Count <= 8192, 'FillLand: stack overflow', true); |
4 | 188 |
_y:= _y + _dir; |
189 |
if (_y < 0) or (_y > 1023) then exit; |
|
190 |
with Stack.points[Stack.Count] do |
|
191 |
begin |
|
192 |
xl:= _xl; |
|
193 |
xr:= _xr; |
|
194 |
y:= _y; |
|
195 |
dir:= _dir |
|
196 |
end; |
|
75 | 197 |
inc(Stack.Count) |
4 | 198 |
end; |
199 |
||
351 | 200 |
procedure Pop(var _xl, _xr, _y, _dir: integer); |
4 | 201 |
begin |
202 |
dec(Stack.Count); |
|
203 |
with Stack.points[Stack.Count] do |
|
204 |
begin |
|
205 |
_xl:= xl; |
|
206 |
_xr:= xr; |
|
207 |
_y:= y; |
|
208 |
_dir:= dir |
|
209 |
end |
|
210 |
end; |
|
211 |
||
212 |
var xl, xr, dir: integer; |
|
351 | 213 |
begin |
4 | 214 |
Stack.Count:= 0; |
215 |
xl:= x - 1; |
|
216 |
xr:= x; |
|
23 | 217 |
Push(xl, xr, y, -1); |
218 |
Push(xl, xr, y, 1); |
|
4 | 219 |
while Stack.Count > 0 do |
220 |
begin |
|
221 |
Pop(xl, xr, y, dir); |
|
51 | 222 |
while (xl > 0) and (Land[y, xl] <> 0) do dec(xl); |
223 |
while (xr < 2047) and (Land[y, xr] <> 0) do inc(xr); |
|
4 | 224 |
while (xl < xr) do |
225 |
begin |
|
51 | 226 |
while (xl <= xr) and (Land[y, xl] = 0) do inc(xl); |
4 | 227 |
x:= xl; |
51 | 228 |
while (xl <= xr) and (Land[y, xl] <> 0) do |
4 | 229 |
begin |
51 | 230 |
Land[y, xl]:= 0; |
4 | 231 |
inc(xl) |
232 |
end; |
|
22 | 233 |
if x < xl then |
234 |
begin |
|
235 |
Push(x, Pred(xl), y, dir); |
|
236 |
Push(x, Pred(xl), y,-dir); |
|
237 |
end; |
|
4 | 238 |
end; |
239 |
end; |
|
240 |
end; |
|
241 |
||
242 |
procedure ColorizeLand(Surface: PSDL_Surface); |
|
243 |
var tmpsurf: PSDL_Surface; |
|
244 |
r: TSDL_Rect; |
|
245 |
begin |
|
351 | 246 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', false, true, true); |
4 | 247 |
r.y:= 0; |
248 |
while r.y < 1024 do |
|
249 |
begin |
|
250 |
r.x:= 0; |
|
251 |
while r.x < 2048 do |
|
252 |
begin |
|
253 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
|
351 | 254 |
inc(r.x, tmpsurf^.w) |
4 | 255 |
end; |
351 | 256 |
inc(r.y, tmpsurf^.h) |
4 | 257 |
end; |
258 |
SDL_FreeSurface(tmpsurf); |
|
259 |
||
260 |
tmpsurf:= SDL_CreateRGBSurfaceFrom(@Land, 2048, 1024, 32, 2048*4, $FF0000, $FF00, $FF, 0); |
|
261 |
SDLTry(tmpsurf <> nil, true); |
|
351 | 262 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, SDL_MapRGB(tmpsurf^.format, $FF, $FF, $FF)); |
22 | 263 |
SDL_UpperBlit(tmpsurf, nil, Surface, nil); |
264 |
SDL_FreeSurface(tmpsurf) |
|
4 | 265 |
end; |
266 |
||
267 |
procedure AddBorder(Surface: PSDL_Surface); |
|
268 |
var tmpsurf: PSDL_Surface; |
|
269 |
r, rr: TSDL_Rect; |
|
270 |
x, yd, yu: integer; |
|
271 |
begin |
|
351 | 272 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', false, true, true); |
4 | 273 |
for x:= 0 to 2047 do |
274 |
begin |
|
275 |
yd:= 1023; |
|
276 |
repeat |
|
277 |
while (yd > 0 ) and (Land[yd, x] = 0) do dec(yd); |
|
278 |
if (yd < 0) then yd:= 0; |
|
279 |
while (yd < 1024) and (Land[yd, x] <> 0) do inc(yd); |
|
280 |
dec(yd); |
|
281 |
yu:= yd; |
|
282 |
while (yu > 0 ) and (Land[yu, x] <> 0) do dec(yu); |
|
283 |
while (yu < yd ) and (Land[yu, x] = 0) do inc(yu); |
|
284 |
if (yd < 1023) and ((yd - yu) >= 16) then |
|
285 |
begin |
|
286 |
rr.x:= x; |
|
287 |
rr.y:= yd - 15; |
|
351 | 288 |
r.x:= x mod tmpsurf^.w; |
4 | 289 |
r.y:= 16; |
290 |
r.w:= 1; |
|
291 |
r.h:= 16; |
|
292 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
293 |
end; |
|
294 |
if (yu > 0) then |
|
295 |
begin |
|
296 |
rr.x:= x; |
|
297 |
rr.y:= yu; |
|
351 | 298 |
r.x:= x mod tmpsurf^.w; |
4 | 299 |
r.y:= 0; |
300 |
r.w:= 1; |
|
301 |
r.h:= min(16, yd - yu + 1); |
|
302 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
303 |
end; |
|
304 |
yd:= yu - 1; |
|
305 |
until yd < 0; |
|
306 |
end; |
|
307 |
end; |
|
308 |
||
358 | 309 |
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr); |
310 |
var i: integer; |
|
22 | 311 |
begin |
23 | 312 |
with Template do |
313 |
begin |
|
358 | 314 |
pa.Count:= BasePointsCount; |
315 |
for i:= 0 to pred(pa.Count) do |
|
23 | 316 |
begin |
358 | 317 |
pa.ar[i].x:= BasePoints^[i].x + integer(GetRandom(BasePoints^[i].w)); |
318 |
pa.ar[i].y:= BasePoints^[i].y + integer(GetRandom(BasePoints^[i].h)) |
|
23 | 319 |
end; |
358 | 320 |
|
321 |
if canMirror then |
|
322 |
if getrandom(16) < 8 then |
|
323 |
begin |
|
324 |
for i:= 0 to pred(BasePointsCount) do |
|
325 |
BasePoints^[i].x:= 2047 - BasePoints^[i].x; |
|
326 |
for i:= 0 to pred(FillPointsCount) do |
|
327 |
FillPoints^[i].x:= 2047 - FillPoints^[i].x; |
|
328 |
end; |
|
22 | 329 |
|
358 | 330 |
if canFlip then |
331 |
if getrandom(16) < 8 then |
|
332 |
begin |
|
333 |
for i:= 0 to pred(BasePointsCount) do |
|
334 |
BasePoints^[i].y:= 1023 - BasePoints^[i].y; |
|
335 |
for i:= 0 to pred(FillPointsCount) do |
|
336 |
FillPoints^[i].y:= 1023 - FillPoints^[i].y; |
|
337 |
end; |
|
338 |
end |
|
4 | 339 |
end; |
340 |
||
67 | 341 |
procedure NormalizePoints(var pa: TPixAr); |
342 |
const brd = 32; |
|
351 | 343 |
var isUP: boolean; // HACK: transform for Y should be exact as one for X |
67 | 344 |
Left, Right, Top, Bottom, |
345 |
OWidth, Width, OHeight, Height, |
|
346 |
OLeft: integer; |
|
347 |
i: integer; |
|
348 |
begin |
|
349 |
TryDo((pa.ar[0].y < 0) or (pa.ar[0].y > 1023), 'Bad land generated', true); |
|
160 | 350 |
TryDo((pa.ar[Pred(pa.Count)].y < 0) or (pa.ar[Pred(pa.Count)].y > 1023), 'Bad land generated', true); |
67 | 351 |
isUP:= pa.ar[0].y > 0; |
352 |
Left:= 1023; |
|
353 |
Right:= Left; |
|
354 |
Top:= pa.ar[0].y; |
|
355 |
Bottom:= Top; |
|
356 |
||
357 |
for i:= 1 to Pred(pa.Count) do |
|
358 |
with pa.ar[i] do |
|
359 |
begin |
|
360 |
if (y and $FFFFFC00) = 0 then |
|
361 |
if x < Left then Left:= x else |
|
362 |
if x > Right then Right:= x; |
|
363 |
if y < Top then Top:= y else |
|
364 |
if y > Bottom then Bottom:= y |
|
365 |
end; |
|
366 |
||
367 |
if (Left < brd) or (Right > 2047 - brd) then |
|
368 |
begin |
|
369 |
OLeft:= Left; |
|
370 |
OWidth:= Right - OLeft; |
|
371 |
if Left < brd then Left:= brd; |
|
372 |
if Right > 2047 - brd then Right:= 2047 - brd; |
|
373 |
Width:= Right - Left; |
|
374 |
for i:= 0 to Pred(pa.Count) do |
|
375 |
with pa.ar[i] do |
|
376 |
x:= round((x - OLeft) * Width div OWidth + Left) |
|
377 |
end; |
|
378 |
||
379 |
if isUp then // FIXME: remove hack |
|
380 |
if Top < brd then |
|
381 |
begin |
|
382 |
OHeight:= 1023 - Top; |
|
383 |
Height:= 1023 - brd; |
|
384 |
for i:= 0 to Pred(pa.Count) do |
|
385 |
with pa.ar[i] do |
|
386 |
y:= round((y - 1023) * Height div OHeight + 1023) |
|
387 |
end; |
|
388 |
end; |
|
389 |
||
23 | 390 |
procedure GenBlank(var Template: TEdgeTemplate); |
4 | 391 |
var pa: TPixAr; |
23 | 392 |
i: Longword; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
393 |
y, x: Longword; |
4 | 394 |
begin |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
395 |
for y:= 0 to 1023 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
396 |
for x:= 0 to 2047 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
397 |
Land[y, x]:= COLOR_LAND; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
398 |
|
358 | 399 |
SetPoints(Template, pa); |
400 |
NormalizePoints(pa); |
|
27 | 401 |
|
358 | 402 |
DrawBezierEdge(pa, 0); |
27 | 403 |
|
358 | 404 |
with Template do |
23 | 405 |
for i:= 0 to pred(FillPointsCount) do |
406 |
with FillPoints^[i] do |
|
89 | 407 |
FillLand(x, y); |
408 |
||
358 | 409 |
DrawBezierEdge(pa, COLOR_LAND) |
23 | 410 |
end; |
411 |
||
161 | 412 |
function SelectTemplate: integer; |
413 |
begin |
|
351 | 414 |
SelectTemplate:= getrandom(Succ(High(EdgeTemplates))) |
161 | 415 |
end; |
416 |
||
23 | 417 |
procedure GenLandSurface; |
418 |
var tmpsurf: PSDL_Surface; |
|
419 |
begin |
|
53 | 420 |
WriteLnToConsole('Generating land...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
421 |
|
161 | 422 |
GenBlank(EdgeTemplates[SelectTemplate]); |
22 | 423 |
|
4 | 424 |
AddProgress; |
425 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
426 |
tmpsurf:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 427 |
TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true); |
4 | 428 |
ColorizeLand(tmpsurf); |
429 |
AddProgress; |
|
430 |
AddBorder(tmpsurf); |
|
431 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
432 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 433 |
TryDo(LandSurface <> nil, 'Error creating land surface', true); |
4 | 434 |
SDL_FillRect(LandSurface, nil, 0); |
27 | 435 |
AddProgress; |
24 | 436 |
|
70 | 437 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0); |
438 |
AddObjects(tmpsurf, LandSurface); |
|
439 |
SDL_FreeSurface(tmpsurf); |
|
24 | 440 |
|
70 | 441 |
AddProgress |
4 | 442 |
end; |
443 |
||
444 |
procedure MakeFortsMap; |
|
445 |
var p: PTeam; |
|
446 |
tmpsurf: PSDL_Surface; |
|
447 |
begin |
|
53 | 448 |
WriteLnToConsole('Generating forts land...'); |
4 | 449 |
p:= TeamsList; |
450 |
TryDo(p <> nil, 'No teams on map!', true); |
|
451 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
452 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
37 | 453 |
SDL_FillRect(LandSurface, nil, 0); |
351 | 454 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'L', false, true, true); |
4 | 455 |
BlitImageAndGenerateCollisionInfo(0, 0, tmpsurf, LandSurface); |
456 |
SDL_FreeSurface(tmpsurf); |
|
351 | 457 |
p:= p^.Next; |
4 | 458 |
TryDo(p <> nil, 'Only one team on map!', true); |
351 | 459 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'R', false, true, true); |
4 | 460 |
BlitImageAndGenerateCollisionInfo(1024, 0, tmpsurf, LandSurface); |
461 |
SDL_FreeSurface(tmpsurf); |
|
351 | 462 |
p:= p^.Next; |
4 | 463 |
TryDo(p = nil, 'More than 2 teams on map in forts mode!', true); |
464 |
end; |
|
465 |
||
53 | 466 |
procedure LoadMap; |
107 | 467 |
var x, y: Longword; |
468 |
p: PByteArray; |
|
53 | 469 |
begin |
470 |
WriteLnToConsole('Loading land from file...'); |
|
471 |
AddProgress; |
|
351 | 472 |
LandSurface:= LoadImage(Pathz[ptMapCurrent] + '/map', false, true, true); |
473 |
TryDo((LandSurface^.w = 2048) and (LandSurface^.h = 1024), 'Map dimensions should be 2048x1024!', true); |
|
53 | 474 |
|
475 |
if SDL_MustLock(LandSurface) then |
|
476 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true); |
|
477 |
||
351 | 478 |
p:= LandSurface^.pixels; |
479 |
case LandSurface^.format^.BytesPerPixel of |
|
53 | 480 |
1: OutError('We don''t work with 8 bit surfaces', true); |
481 |
2: for y:= 0 to 1023 do |
|
482 |
begin |
|
483 |
for x:= 0 to 2047 do |
|
351 | 484 |
if PWord(@(p^[x * 2]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
485 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 486 |
end; |
487 |
3: for y:= 0 to 1023 do |
|
488 |
begin |
|
489 |
for x:= 0 to 2047 do |
|
351 | 490 |
if (p^[x * 3 + 0] <> 0) |
491 |
or (p^[x * 3 + 1] <> 0) |
|
492 |
or (p^[x * 3 + 2] <> 0) then Land[y, x]:= COLOR_LAND; |
|
493 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 494 |
end; |
495 |
4: for y:= 0 to 1023 do |
|
496 |
begin |
|
497 |
for x:= 0 to 2047 do |
|
351 | 498 |
if PLongword(@(p^[x * 4]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
499 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 500 |
end; |
501 |
end; |
|
351 | 502 |
|
53 | 503 |
if SDL_MustLock(LandSurface) then |
504 |
SDL_UnlockSurface(LandSurface); |
|
505 |
end; |
|
506 |
||
37 | 507 |
procedure GenMap; |
508 |
begin |
|
53 | 509 |
if (GameFlags and gfForts) = 0 then |
510 |
if Pathz[ptMapCurrent] <> '' then LoadMap |
|
511 |
else GenLandSurface |
|
37 | 512 |
else MakeFortsMap; |
513 |
AddProgress; |
|
514 |
{$IFDEF DEBUGFILE}LogLandDigest{$ENDIF} |
|
515 |
end; |
|
516 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
517 |
procedure GenPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
518 |
var x, y, xx, yy, t, bit: integer; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
519 |
begin |
160 | 520 |
WriteLnToConsole('Generating preview...'); |
161 | 521 |
GenBlank(EdgeTemplates[SelectTemplate]); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
522 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
523 |
for y:= 0 to 127 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
524 |
for x:= 0 to 31 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
525 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
526 |
Preview[y, x]:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
527 |
for bit:= 0 to 7 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
528 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
529 |
t:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
530 |
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
|
531 |
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
|
532 |
if Land[yy, xx] <> 0 then inc(t); |
351 | 533 |
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
|
534 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
535 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
536 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
537 |
|
51 | 538 |
initialization |
539 |
||
4 | 540 |
end. |