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