author | unc0rr |
Sun, 18 Feb 2007 14:43:35 +0000 | |
changeset 451 | 756616c38dd6 |
parent 431 | 79ac59673df3 |
child 495 | 62c1c2b4414c |
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; |
|
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; |
|
430 | 166 |
tsq, tcb, t, r1, r2, r3: 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; |
|
430 | 197 |
r1:= (1 - 3*t + 3*tsq - tcb); |
198 |
r2:= ( 3*t - 6*tsq + 3*tcb); |
|
199 |
r3:= ( 3*tsq - 3*tcb); |
|
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); |
371 | 215 |
var x, y, 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 |
|
351 | 302 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', false, true, true); |
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 |
||
316 |
tmpsurf:= SDL_CreateRGBSurfaceFrom(@Land, 2048, 1024, 32, 2048*4, $FF0000, $FF00, $FF, 0); |
|
317 |
SDLTry(tmpsurf <> nil, true); |
|
351 | 318 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, SDL_MapRGB(tmpsurf^.format, $FF, $FF, $FF)); |
22 | 319 |
SDL_UpperBlit(tmpsurf, nil, Surface, nil); |
320 |
SDL_FreeSurface(tmpsurf) |
|
4 | 321 |
end; |
322 |
||
323 |
procedure AddBorder(Surface: PSDL_Surface); |
|
324 |
var tmpsurf: PSDL_Surface; |
|
325 |
r, rr: TSDL_Rect; |
|
371 | 326 |
x, yd, yu: LongInt; |
4 | 327 |
begin |
351 | 328 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', false, true, true); |
4 | 329 |
for x:= 0 to 2047 do |
330 |
begin |
|
331 |
yd:= 1023; |
|
332 |
repeat |
|
333 |
while (yd > 0 ) and (Land[yd, x] = 0) do dec(yd); |
|
334 |
if (yd < 0) then yd:= 0; |
|
335 |
while (yd < 1024) and (Land[yd, x] <> 0) do inc(yd); |
|
336 |
dec(yd); |
|
337 |
yu:= yd; |
|
338 |
while (yu > 0 ) and (Land[yu, x] <> 0) do dec(yu); |
|
339 |
while (yu < yd ) and (Land[yu, x] = 0) do inc(yu); |
|
340 |
if (yd < 1023) and ((yd - yu) >= 16) then |
|
341 |
begin |
|
342 |
rr.x:= x; |
|
343 |
rr.y:= yd - 15; |
|
351 | 344 |
r.x:= x mod tmpsurf^.w; |
4 | 345 |
r.y:= 16; |
346 |
r.w:= 1; |
|
347 |
r.h:= 16; |
|
348 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
349 |
end; |
|
350 |
if (yu > 0) then |
|
351 |
begin |
|
352 |
rr.x:= x; |
|
353 |
rr.y:= yu; |
|
351 | 354 |
r.x:= x mod tmpsurf^.w; |
4 | 355 |
r.y:= 0; |
356 |
r.w:= 1; |
|
357 |
r.h:= min(16, yd - yu + 1); |
|
358 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
359 |
end; |
|
360 |
yd:= yu - 1; |
|
361 |
until yd < 0; |
|
362 |
end; |
|
363 |
end; |
|
364 |
||
358 | 365 |
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr); |
371 | 366 |
var i: LongInt; |
22 | 367 |
begin |
23 | 368 |
with Template do |
369 |
begin |
|
358 | 370 |
pa.Count:= BasePointsCount; |
371 |
for i:= 0 to pred(pa.Count) do |
|
23 | 372 |
begin |
371 | 373 |
pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w)); |
374 |
pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) |
|
23 | 375 |
end; |
358 | 376 |
|
377 |
if canMirror then |
|
360 | 378 |
if getrandom(2) = 0 then |
358 | 379 |
begin |
380 |
for i:= 0 to pred(BasePointsCount) do |
|
365 | 381 |
if pa.ar[i].x <> NTPX then |
360 | 382 |
pa.ar[i].x:= 2047 - pa.ar[i].x; |
358 | 383 |
for i:= 0 to pred(FillPointsCount) do |
384 |
FillPoints^[i].x:= 2047 - FillPoints^[i].x; |
|
385 |
end; |
|
22 | 386 |
|
358 | 387 |
if canFlip then |
360 | 388 |
if getrandom(2) = 0 then |
358 | 389 |
begin |
390 |
for i:= 0 to pred(BasePointsCount) do |
|
360 | 391 |
pa.ar[i].y:= 1023 - pa.ar[i].y; |
358 | 392 |
for i:= 0 to pred(FillPointsCount) do |
393 |
FillPoints^[i].y:= 1023 - FillPoints^[i].y; |
|
394 |
end; |
|
395 |
end |
|
4 | 396 |
end; |
67 | 397 |
|
429 | 398 |
procedure RandomizePoints(var pa: TPixAr); |
364 | 399 |
const cEdge = 55; |
365 | 400 |
cMinDist = 0; |
371 | 401 |
var radz: array[0..Pred(cMaxEdgePoints)] of LongInt; |
402 |
i, k, dist: LongInt; |
|
364 | 403 |
begin |
404 |
radz[0]:= 0; |
|
405 |
for i:= 0 to Pred(pa.Count) do |
|
406 |
with pa.ar[i] do |
|
365 | 407 |
if x <> NTPX then |
408 |
begin |
|
409 |
radz[i]:= Min(Max(x - cEdge, 0), Max(2048 - cEdge - x, 0)); |
|
410 |
radz[i]:= Min(radz[i], Min(Max(y - cEdge, 0), Max(1024 - cEdge - y, 0))); |
|
411 |
if radz[i] > 0 then |
|
412 |
for k:= 0 to Pred(i) do |
|
364 | 413 |
begin |
429 | 414 |
dist:= Max(abs(x - pa.ar[k].x), abs(y - pa.ar[k].y)); |
365 | 415 |
radz[k]:= Max(0, Min((dist - cMinDist) div 2, radz[k])); |
416 |
radz[i]:= Max(0, Min(dist - radz[k] - cMinDist, radz[i])) |
|
417 |
end |
|
418 |
end; |
|
364 | 419 |
|
420 |
for i:= 0 to Pred(pa.Count) do |
|
421 |
with pa.ar[i] do |
|
422 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
|
423 |
begin |
|
371 | 424 |
x:= x + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3; |
425 |
y:= y + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3 |
|
364 | 426 |
end |
67 | 427 |
end; |
428 |
||
364 | 429 |
|
23 | 430 |
procedure GenBlank(var Template: TEdgeTemplate); |
4 | 431 |
var pa: TPixAr; |
23 | 432 |
i: Longword; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
433 |
y, x: Longword; |
4 | 434 |
begin |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
435 |
for y:= 0 to 1023 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
436 |
for x:= 0 to 2047 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
437 |
Land[y, x]:= COLOR_LAND; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
438 |
|
358 | 439 |
SetPoints(Template, pa); |
429 | 440 |
for i:= 1 to Template.BezierizeCount do |
441 |
begin |
|
431 | 442 |
BezierizeEdge(pa, _0_5); |
429 | 443 |
RandomizePoints(pa) |
444 |
end; |
|
445 |
for i:= 1 to Template.RandPassesCount do RandomizePoints(pa); |
|
365 | 446 |
BezierizeEdge(pa, _0_1); |
27 | 447 |
|
365 | 448 |
DrawEdge(pa, 0); |
27 | 449 |
|
358 | 450 |
with Template do |
23 | 451 |
for i:= 0 to pred(FillPointsCount) do |
452 |
with FillPoints^[i] do |
|
89 | 453 |
FillLand(x, y); |
454 |
||
365 | 455 |
DrawEdge(pa, COLOR_LAND) |
23 | 456 |
end; |
457 |
||
371 | 458 |
function SelectTemplate: LongInt; |
161 | 459 |
begin |
351 | 460 |
SelectTemplate:= getrandom(Succ(High(EdgeTemplates))) |
161 | 461 |
end; |
462 |
||
23 | 463 |
procedure GenLandSurface; |
464 |
var tmpsurf: PSDL_Surface; |
|
465 |
begin |
|
53 | 466 |
WriteLnToConsole('Generating land...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
467 |
|
161 | 468 |
GenBlank(EdgeTemplates[SelectTemplate]); |
22 | 469 |
|
4 | 470 |
AddProgress; |
471 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
472 |
tmpsurf:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 473 |
TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true); |
4 | 474 |
ColorizeLand(tmpsurf); |
475 |
AddProgress; |
|
476 |
AddBorder(tmpsurf); |
|
477 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
478 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
67 | 479 |
TryDo(LandSurface <> nil, 'Error creating land surface', true); |
4 | 480 |
SDL_FillRect(LandSurface, nil, 0); |
27 | 481 |
AddProgress; |
24 | 482 |
|
70 | 483 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0); |
484 |
AddObjects(tmpsurf, LandSurface); |
|
485 |
SDL_FreeSurface(tmpsurf); |
|
24 | 486 |
|
70 | 487 |
AddProgress |
4 | 488 |
end; |
489 |
||
490 |
procedure MakeFortsMap; |
|
491 |
var p: PTeam; |
|
492 |
tmpsurf: PSDL_Surface; |
|
493 |
begin |
|
53 | 494 |
WriteLnToConsole('Generating forts land...'); |
4 | 495 |
p:= TeamsList; |
496 |
TryDo(p <> nil, 'No teams on map!', true); |
|
497 |
with PixelFormat^ do |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
498 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, AMask); |
37 | 499 |
SDL_FillRect(LandSurface, nil, 0); |
351 | 500 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'L', false, true, true); |
4 | 501 |
BlitImageAndGenerateCollisionInfo(0, 0, tmpsurf, LandSurface); |
502 |
SDL_FreeSurface(tmpsurf); |
|
351 | 503 |
p:= p^.Next; |
4 | 504 |
TryDo(p <> nil, 'Only one team on map!', true); |
351 | 505 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p^.FortName + 'R', false, true, true); |
4 | 506 |
BlitImageAndGenerateCollisionInfo(1024, 0, tmpsurf, LandSurface); |
507 |
SDL_FreeSurface(tmpsurf); |
|
351 | 508 |
p:= p^.Next; |
4 | 509 |
TryDo(p = nil, 'More than 2 teams on map in forts mode!', true); |
510 |
end; |
|
511 |
||
53 | 512 |
procedure LoadMap; |
107 | 513 |
var x, y: Longword; |
514 |
p: PByteArray; |
|
53 | 515 |
begin |
516 |
WriteLnToConsole('Loading land from file...'); |
|
517 |
AddProgress; |
|
351 | 518 |
LandSurface:= LoadImage(Pathz[ptMapCurrent] + '/map', false, true, true); |
519 |
TryDo((LandSurface^.w = 2048) and (LandSurface^.h = 1024), 'Map dimensions should be 2048x1024!', true); |
|
53 | 520 |
|
521 |
if SDL_MustLock(LandSurface) then |
|
522 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true); |
|
523 |
||
351 | 524 |
p:= LandSurface^.pixels; |
525 |
case LandSurface^.format^.BytesPerPixel of |
|
53 | 526 |
1: OutError('We don''t work with 8 bit surfaces', true); |
527 |
2: for y:= 0 to 1023 do |
|
528 |
begin |
|
529 |
for x:= 0 to 2047 do |
|
351 | 530 |
if PWord(@(p^[x * 2]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
531 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 532 |
end; |
533 |
3: for y:= 0 to 1023 do |
|
534 |
begin |
|
535 |
for x:= 0 to 2047 do |
|
351 | 536 |
if (p^[x * 3 + 0] <> 0) |
537 |
or (p^[x * 3 + 1] <> 0) |
|
538 |
or (p^[x * 3 + 2] <> 0) then Land[y, x]:= COLOR_LAND; |
|
539 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 540 |
end; |
541 |
4: for y:= 0 to 1023 do |
|
542 |
begin |
|
543 |
for x:= 0 to 2047 do |
|
351 | 544 |
if PLongword(@(p^[x * 4]))^ <> 0 then Land[y, x]:= COLOR_LAND; |
545 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 546 |
end; |
547 |
end; |
|
351 | 548 |
|
53 | 549 |
if SDL_MustLock(LandSurface) then |
550 |
SDL_UnlockSurface(LandSurface); |
|
551 |
end; |
|
552 |
||
37 | 553 |
procedure GenMap; |
554 |
begin |
|
53 | 555 |
if (GameFlags and gfForts) = 0 then |
556 |
if Pathz[ptMapCurrent] <> '' then LoadMap |
|
557 |
else GenLandSurface |
|
37 | 558 |
else MakeFortsMap; |
559 |
AddProgress; |
|
560 |
{$IFDEF DEBUGFILE}LogLandDigest{$ENDIF} |
|
561 |
end; |
|
562 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
563 |
procedure GenPreview; |
371 | 564 |
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
|
565 |
begin |
160 | 566 |
WriteLnToConsole('Generating preview...'); |
161 | 567 |
GenBlank(EdgeTemplates[SelectTemplate]); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
568 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
569 |
for y:= 0 to 127 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
570 |
for x:= 0 to 31 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
571 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
572 |
Preview[y, x]:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
573 |
for bit:= 0 to 7 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 |
t:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
576 |
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
|
577 |
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
|
578 |
if Land[yy, xx] <> 0 then inc(t); |
351 | 579 |
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
|
580 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
581 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
582 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
583 |
|
51 | 584 |
initialization |
585 |
||
4 | 586 |
end. |