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