author | unc0rr |
Thu, 01 May 2008 15:55:03 +0000 | |
changeset 898 | 344ba7dba23d |
parent 883 | 07a568ba44e0 |
child 955 | 474afaab0365 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
883 | 3 |
* Copyright (c) 2005-2008 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 |
|
755 | 21 |
uses SDLh, uLandTemplates, uFloat, GL, uConsts; |
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; |
|
768 | 28 |
LandPixels: TLandArray; |
766 | 29 |
LandTexture: PTexture = nil; |
4 | 30 |
|
37 | 31 |
procedure GenMap; |
766 | 32 |
function GenPreview: TPreview; |
367 | 33 |
procedure CheckLandDigest(s: shortstring); |
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
34 |
procedure UpdateLandTexture(Y, Height: LongInt); |
4 | 35 |
|
36 |
implementation |
|
755 | 37 |
uses uConsole, uStore, uMisc, uRandom, uTeams, uLandObjects, uSHA, uIO; |
4 | 38 |
|
39 |
type TPixAr = record |
|
40 |
Count: Longword; |
|
22 | 41 |
ar: array[0..Pred(cMaxEdgePoints)] of TPoint; |
4 | 42 |
end; |
43 |
||
37 | 44 |
procedure LogLandDigest; |
316 | 45 |
var ctx: TSHA1Context; |
46 |
dig: TSHA1Digest; |
|
47 |
s: shortstring; |
|
37 | 48 |
begin |
316 | 49 |
SHA1Init(ctx); |
50 |
SHA1Update(ctx, @Land, sizeof(Land)); |
|
51 |
dig:= SHA1Final(ctx); |
|
367 | 52 |
s:='M{'+inttostr(dig[0])+':' |
316 | 53 |
+inttostr(dig[1])+':' |
54 |
+inttostr(dig[2])+':' |
|
55 |
+inttostr(dig[3])+':' |
|
56 |
+inttostr(dig[4])+'}'; |
|
699 | 57 |
CheckLandDigest(s); |
367 | 58 |
SendIPCRaw(@s[0], Length(s) + 1) |
59 |
end; |
|
60 |
||
61 |
procedure CheckLandDigest(s: shortstring); |
|
62 |
const digest: shortstring = ''; |
|
63 |
begin |
|
368 | 64 |
{$IFDEF DEBUGFILE} |
65 |
AddFileLog('CheckLandDigest: ' + s); |
|
66 |
{$ENDIF} |
|
367 | 67 |
if digest = '' then |
68 |
digest:= s |
|
69 |
else |
|
700 | 70 |
TryDo(s = digest, 'Different maps generated, sorry', true) |
37 | 71 |
end; |
72 |
||
371 | 73 |
procedure DrawLine(X1, Y1, X2, Y2: LongInt; Color: Longword); |
358 | 74 |
var |
371 | 75 |
eX, eY, dX, dY: LongInt; |
76 |
i, sX, sY, x, y, d: LongInt; |
|
358 | 77 |
begin |
78 |
eX:= 0; |
|
79 |
eY:= 0; |
|
80 |
dX:= X2 - X1; |
|
81 |
dY:= Y2 - Y1; |
|
82 |
||
83 |
if (dX > 0) then sX:= 1 |
|
84 |
else |
|
85 |
if (dX < 0) then |
|
86 |
begin |
|
87 |
sX:= -1; |
|
88 |
dX:= -dX |
|
89 |
end else sX:= dX; |
|
90 |
||
91 |
if (dY > 0) then sY:= 1 |
|
92 |
else |
|
93 |
if (dY < 0) then |
|
94 |
begin |
|
95 |
sY:= -1; |
|
96 |
dY:= -dY |
|
97 |
end else sY:= dY; |
|
98 |
||
99 |
if (dX > dY) then d:= dX |
|
100 |
else d:= dY; |
|
101 |
||
102 |
x:= X1; |
|
103 |
y:= Y1; |
|
104 |
||
105 |
for i:= 0 to d do |
|
106 |
begin |
|
107 |
inc(eX, dX); |
|
108 |
inc(eY, dY); |
|
109 |
if (eX > d) then |
|
110 |
begin |
|
111 |
dec(eX, d); |
|
112 |
inc(x, sX); |
|
113 |
end; |
|
114 |
if (eY > d) then |
|
115 |
begin |
|
116 |
dec(eY, d); |
|
117 |
inc(y, sY); |
|
118 |
end; |
|
364 | 119 |
|
358 | 120 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
121 |
Land[y, x]:= Color; |
|
122 |
end |
|
123 |
end; |
|
124 |
||
365 | 125 |
procedure DrawEdge(var pa: TPixAr; Color: Longword); |
371 | 126 |
var i: LongInt; |
4 | 127 |
begin |
365 | 128 |
i:= 0; |
4 | 129 |
with pa do |
371 | 130 |
while i < LongInt(Count) - 1 do |
365 | 131 |
if (ar[i + 1].X = NTPX) then inc(i, 2) |
132 |
else begin |
|
133 |
DrawLine(ar[i].x, ar[i].y, ar[i + 1].x, ar[i + 1].y, Color); |
|
134 |
inc(i) |
|
135 |
end |
|
22 | 136 |
end; |
137 |
||
365 | 138 |
procedure Vector(p1, p2, p3: TPoint; var Vx, Vy: hwFloat); |
139 |
var d1, d2, d: hwFloat; |
|
364 | 140 |
begin |
498 | 141 |
Vx:= int2hwFloat(p1.X - p3.X); |
142 |
Vy:= int2hwFloat(p1.Y - p3.Y); |
|
143 |
d:= DistanceI(p2.X - p1.X, p2.Y - p1.Y); |
|
144 |
d1:= DistanceI(p2.X - p3.X, p2.Y - p3.Y); |
|
365 | 145 |
d2:= Distance(Vx, Vy); |
146 |
if d1 < d then d:= d1; |
|
147 |
if d2 < d then d:= d2; |
|
148 |
d:= d * _1div3; |
|
149 |
if d2.QWordValue = 0 then |
|
150 |
begin |
|
498 | 151 |
Vx:= _0; |
152 |
Vy:= _0 |
|
365 | 153 |
end else |
154 |
begin |
|
498 | 155 |
d2:= _1 / d2; |
365 | 156 |
Vx:= Vx * d2; |
157 |
Vy:= Vy * d2; |
|
158 |
||
159 |
Vx:= Vx * d; |
|
160 |
Vy:= Vy * d |
|
161 |
end |
|
162 |
end; |
|
163 |
||
371 | 164 |
procedure AddLoopPoints(var pa, opa: TPixAr; StartI, EndI: LongInt; Delta: hwFloat); |
165 |
var i, pi, ni: LongInt; |
|
365 | 166 |
NVx, NVy, PVx, PVy: hwFloat; |
498 | 167 |
x1, x2, y1, y2: LongInt; |
168 |
tsq, tcb, t, r1, r2, r3, cx1, cx2, cy1, cy2: hwFloat; |
|
371 | 169 |
X, Y: LongInt; |
365 | 170 |
begin |
171 |
pi:= EndI; |
|
172 |
i:= StartI; |
|
173 |
ni:= Succ(StartI); |
|
174 |
Vector(opa.ar[pi], opa.ar[i], opa.ar[ni], NVx, NVy); |
|
175 |
repeat |
|
176 |
inc(pi); |
|
177 |
if pi > EndI then pi:= StartI; |
|
178 |
inc(i); |
|
179 |
if i > EndI then i:= StartI; |
|
180 |
inc(ni); |
|
181 |
if ni > EndI then ni:= StartI; |
|
182 |
PVx:= NVx; |
|
183 |
PVy:= NVy; |
|
184 |
Vector(opa.ar[pi], opa.ar[i], opa.ar[ni], NVx, NVy); |
|
185 |
||
186 |
x1:= opa.ar[pi].x; |
|
187 |
y1:= opa.ar[pi].y; |
|
188 |
x2:= opa.ar[i].x; |
|
189 |
y2:= opa.ar[i].y; |
|
498 | 190 |
cx1:= int2hwFloat(x1) - PVx; |
191 |
cy1:= int2hwFloat(y1) - PVy; |
|
192 |
cx2:= int2hwFloat(x2) + NVx; |
|
193 |
cy2:= int2hwFloat(y2) + NVy; |
|
194 |
t:= _0; |
|
364 | 195 |
while t.Round = 0 do |
196 |
begin |
|
197 |
tsq:= t * t; |
|
198 |
tcb:= tsq * t; |
|
498 | 199 |
r1:= (_1 - t*3 + tsq*3 - tcb); |
200 |
r2:= ( t*3 - tsq*6 + tcb*3); |
|
201 |
r3:= ( tsq*3 - tcb*3); |
|
430 | 202 |
X:= hwRound(r1 * x1 + r2 * cx1 + r3 * cx2 + tcb * x2); |
203 |
Y:= hwRound(r1 * y1 + r2 * cy1 + r3 * cy2 + tcb * y2); |
|
364 | 204 |
t:= t + Delta; |
205 |
pa.ar[pa.Count].x:= X; |
|
206 |
pa.ar[pa.Count].y:= Y; |
|
207 |
inc(pa.Count); |
|
208 |
TryDo(pa.Count <= cMaxEdgePoints, 'Edge points overflow', true) |
|
209 |
end; |
|
365 | 210 |
until i = StartI; |
211 |
pa.ar[pa.Count].x:= opa.ar[StartI].X; |
|
212 |
pa.ar[pa.Count].y:= opa.ar[StartI].Y; |
|
364 | 213 |
inc(pa.Count) |
214 |
end; |
|
215 |
||
365 | 216 |
procedure BezierizeEdge(var pa: TPixAr; Delta: hwFloat); |
495 | 217 |
var i, StartLoop: LongInt; |
365 | 218 |
opa: TPixAr; |
219 |
begin |
|
220 |
opa:= pa; |
|
221 |
pa.Count:= 0; |
|
222 |
i:= 0; |
|
223 |
StartLoop:= 0; |
|
371 | 224 |
while i < LongInt(opa.Count) do |
365 | 225 |
if (opa.ar[i + 1].X = NTPX) then |
226 |
begin |
|
227 |
AddLoopPoints(pa, opa, StartLoop, i, Delta); |
|
228 |
inc(i, 2); |
|
229 |
StartLoop:= i; |
|
230 |
pa.ar[pa.Count].X:= NTPX; |
|
231 |
inc(pa.Count); |
|
232 |
end else inc(i) |
|
233 |
end; |
|
234 |
||
371 | 235 |
procedure FillLand(x, y: LongInt); |
4 | 236 |
var Stack: record |
237 |
Count: Longword; |
|
238 |
points: array[0..8192] of record |
|
371 | 239 |
xl, xr, y, dir: LongInt; |
4 | 240 |
end |
241 |
end; |
|
242 |
||
371 | 243 |
procedure Push(_xl, _xr, _y, _dir: LongInt); |
4 | 244 |
begin |
75 | 245 |
TryDo(Stack.Count <= 8192, 'FillLand: stack overflow', true); |
4 | 246 |
_y:= _y + _dir; |
247 |
if (_y < 0) or (_y > 1023) then exit; |
|
248 |
with Stack.points[Stack.Count] do |
|
249 |
begin |
|
250 |
xl:= _xl; |
|
251 |
xr:= _xr; |
|
252 |
y:= _y; |
|
253 |
dir:= _dir |
|
254 |
end; |
|
75 | 255 |
inc(Stack.Count) |
4 | 256 |
end; |
257 |
||
371 | 258 |
procedure Pop(var _xl, _xr, _y, _dir: LongInt); |
4 | 259 |
begin |
260 |
dec(Stack.Count); |
|
261 |
with Stack.points[Stack.Count] do |
|
262 |
begin |
|
263 |
_xl:= xl; |
|
264 |
_xr:= xr; |
|
265 |
_y:= y; |
|
266 |
_dir:= dir |
|
267 |
end |
|
268 |
end; |
|
269 |
||
371 | 270 |
var xl, xr, dir: LongInt; |
351 | 271 |
begin |
4 | 272 |
Stack.Count:= 0; |
273 |
xl:= x - 1; |
|
274 |
xr:= x; |
|
23 | 275 |
Push(xl, xr, y, -1); |
276 |
Push(xl, xr, y, 1); |
|
4 | 277 |
while Stack.Count > 0 do |
278 |
begin |
|
279 |
Pop(xl, xr, y, dir); |
|
51 | 280 |
while (xl > 0) and (Land[y, xl] <> 0) do dec(xl); |
281 |
while (xr < 2047) and (Land[y, xr] <> 0) do inc(xr); |
|
4 | 282 |
while (xl < xr) do |
283 |
begin |
|
51 | 284 |
while (xl <= xr) and (Land[y, xl] = 0) do inc(xl); |
4 | 285 |
x:= xl; |
51 | 286 |
while (xl <= xr) and (Land[y, xl] <> 0) do |
4 | 287 |
begin |
51 | 288 |
Land[y, xl]:= 0; |
4 | 289 |
inc(xl) |
290 |
end; |
|
22 | 291 |
if x < xl then |
292 |
begin |
|
293 |
Push(x, Pred(xl), y, dir); |
|
294 |
Push(x, Pred(xl), y,-dir); |
|
295 |
end; |
|
4 | 296 |
end; |
297 |
end; |
|
298 |
end; |
|
299 |
||
300 |
procedure ColorizeLand(Surface: PSDL_Surface); |
|
301 |
var tmpsurf: PSDL_Surface; |
|
302 |
r: TSDL_Rect; |
|
303 |
begin |
|
567 | 304 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', false, true, false); |
4 | 305 |
r.y:= 0; |
306 |
while r.y < 1024 do |
|
307 |
begin |
|
308 |
r.x:= 0; |
|
309 |
while r.x < 2048 do |
|
310 |
begin |
|
311 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
|
351 | 312 |
inc(r.x, tmpsurf^.w) |
4 | 313 |
end; |
351 | 314 |
inc(r.y, tmpsurf^.h) |
4 | 315 |
end; |
316 |
SDL_FreeSurface(tmpsurf); |
|
317 |
||
760 | 318 |
|
758 | 319 |
tmpsurf:= SDL_CreateRGBSurfaceFrom(@Land, 2048, 1024, 32, 2048*4, RMask, GMask, BMask, 0); |
4 | 320 |
SDLTry(tmpsurf <> nil, true); |
760 | 321 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, SDL_MapRGB(tmpsurf^.format, $FF, $FF, $FF)); |
22 | 322 |
SDL_UpperBlit(tmpsurf, nil, Surface, nil); |
323 |
SDL_FreeSurface(tmpsurf) |
|
4 | 324 |
end; |
325 |
||
326 |
procedure AddBorder(Surface: PSDL_Surface); |
|
327 |
var tmpsurf: PSDL_Surface; |
|
328 |
r, rr: TSDL_Rect; |
|
371 | 329 |
x, yd, yu: LongInt; |
4 | 330 |
begin |
351 | 331 |
tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', false, true, true); |
4 | 332 |
for x:= 0 to 2047 do |
333 |
begin |
|
334 |
yd:= 1023; |
|
335 |
repeat |
|
336 |
while (yd > 0 ) and (Land[yd, x] = 0) do dec(yd); |
|
337 |
if (yd < 0) then yd:= 0; |
|
338 |
while (yd < 1024) and (Land[yd, x] <> 0) do inc(yd); |
|
339 |
dec(yd); |
|
340 |
yu:= yd; |
|
341 |
while (yu > 0 ) and (Land[yu, x] <> 0) do dec(yu); |
|
342 |
while (yu < yd ) and (Land[yu, x] = 0) do inc(yu); |
|
343 |
if (yd < 1023) and ((yd - yu) >= 16) then |
|
344 |
begin |
|
345 |
rr.x:= x; |
|
346 |
rr.y:= yd - 15; |
|
351 | 347 |
r.x:= x mod tmpsurf^.w; |
4 | 348 |
r.y:= 16; |
349 |
r.w:= 1; |
|
350 |
r.h:= 16; |
|
351 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
352 |
end; |
|
353 |
if (yu > 0) then |
|
354 |
begin |
|
355 |
rr.x:= x; |
|
356 |
rr.y:= yu; |
|
351 | 357 |
r.x:= x mod tmpsurf^.w; |
4 | 358 |
r.y:= 0; |
359 |
r.w:= 1; |
|
360 |
r.h:= min(16, yd - yu + 1); |
|
361 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
362 |
end; |
|
363 |
yd:= yu - 1; |
|
364 |
until yd < 0; |
|
365 |
end; |
|
366 |
end; |
|
367 |
||
358 | 368 |
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr); |
371 | 369 |
var i: LongInt; |
22 | 370 |
begin |
23 | 371 |
with Template do |
372 |
begin |
|
358 | 373 |
pa.Count:= BasePointsCount; |
374 |
for i:= 0 to pred(pa.Count) do |
|
23 | 375 |
begin |
371 | 376 |
pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w)); |
377 |
pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) |
|
23 | 378 |
end; |
358 | 379 |
|
380 |
if canMirror then |
|
360 | 381 |
if getrandom(2) = 0 then |
358 | 382 |
begin |
383 |
for i:= 0 to pred(BasePointsCount) do |
|
365 | 384 |
if pa.ar[i].x <> NTPX then |
360 | 385 |
pa.ar[i].x:= 2047 - pa.ar[i].x; |
358 | 386 |
for i:= 0 to pred(FillPointsCount) do |
387 |
FillPoints^[i].x:= 2047 - FillPoints^[i].x; |
|
388 |
end; |
|
22 | 389 |
|
358 | 390 |
if canFlip then |
360 | 391 |
if getrandom(2) = 0 then |
358 | 392 |
begin |
393 |
for i:= 0 to pred(BasePointsCount) do |
|
360 | 394 |
pa.ar[i].y:= 1023 - pa.ar[i].y; |
358 | 395 |
for i:= 0 to pred(FillPointsCount) do |
396 |
FillPoints^[i].y:= 1023 - FillPoints^[i].y; |
|
397 |
end; |
|
398 |
end |
|
4 | 399 |
end; |
67 | 400 |
|
561 | 401 |
function CheckIntersect(V1, V2, V3, V4: TPoint): boolean; |
402 |
var c1, c2, dm: LongInt; |
|
403 |
begin |
|
404 |
dm:= (V4.y - V3.y) * (V2.x - V1.x) - (V4.x - V3.x) * (V2.y - V1.y); |
|
405 |
c1:= (V4.x - V3.x) * (V1.y - V3.y) - (V4.y - V3.y) * (V1.x - V3.x); |
|
406 |
if dm = 0 then exit(false); |
|
407 |
||
408 |
c2:= (V2.x - V3.x) * (V1.y - V3.y) - (V2.y - V3.y) * (V1.x - V3.x); |
|
409 |
if dm > 0 then |
|
410 |
begin |
|
411 |
if (c1 < 0) or (c1 > dm) then exit(false); |
|
412 |
if (c2 < 0) or (c2 > dm) then exit(false) |
|
413 |
end else |
|
414 |
begin |
|
415 |
if (c1 > 0) or (c1 < dm) then exit(false); |
|
416 |
if (c2 > 0) or (c2 < dm) then exit(false) |
|
417 |
end; |
|
418 |
||
419 |
//AddFileLog('1 (' + inttostr(V1.x) + ',' + inttostr(V1.y) + ')x(' + inttostr(V2.x) + ',' + inttostr(V2.y) + ')'); |
|
420 |
//AddFileLog('2 (' + inttostr(V3.x) + ',' + inttostr(V3.y) + ')x(' + inttostr(V4.x) + ',' + inttostr(V4.y) + ')'); |
|
421 |
CheckIntersect:= true |
|
422 |
end; |
|
423 |
||
424 |
function CheckSelfIntersect(var pa: TPixAr; ind: Longword): boolean; |
|
425 |
var i: Longword; |
|
426 |
begin |
|
427 |
if (ind <= 0) or (ind >= Pred(pa.Count)) then exit(false); |
|
428 |
for i:= 1 to pa.Count - 3 do |
|
429 |
if (i <= ind - 1) or (i >= ind + 2) then |
|
430 |
begin |
|
431 |
if (i <> ind - 1) and |
|
432 |
CheckIntersect(pa.ar[ind], pa.ar[ind - 1], pa.ar[i], pa.ar[i - 1]) then exit(true); |
|
433 |
if (i <> ind + 2) and |
|
434 |
CheckIntersect(pa.ar[ind], pa.ar[ind + 1], pa.ar[i], pa.ar[i - 1]) then exit(true); |
|
435 |
end; |
|
436 |
CheckSelfIntersect:= false |
|
437 |
end; |
|
438 |
||
429 | 439 |
procedure RandomizePoints(var pa: TPixAr); |
364 | 440 |
const cEdge = 55; |
561 | 441 |
cMinDist = 8; |
371 | 442 |
var radz: array[0..Pred(cMaxEdgePoints)] of LongInt; |
561 | 443 |
i, k, dist, px, py: LongInt; |
364 | 444 |
begin |
445 |
radz[0]:= 0; |
|
446 |
for i:= 0 to Pred(pa.Count) do |
|
447 |
with pa.ar[i] do |
|
365 | 448 |
if x <> NTPX then |
449 |
begin |
|
450 |
radz[i]:= Min(Max(x - cEdge, 0), Max(2048 - cEdge - x, 0)); |
|
451 |
radz[i]:= Min(radz[i], Min(Max(y - cEdge, 0), Max(1024 - cEdge - y, 0))); |
|
452 |
if radz[i] > 0 then |
|
453 |
for k:= 0 to Pred(i) do |
|
364 | 454 |
begin |
429 | 455 |
dist:= Max(abs(x - pa.ar[k].x), abs(y - pa.ar[k].y)); |
365 | 456 |
radz[k]:= Max(0, Min((dist - cMinDist) div 2, radz[k])); |
457 |
radz[i]:= Max(0, Min(dist - radz[k] - cMinDist, radz[i])) |
|
458 |
end |
|
459 |
end; |
|
364 | 460 |
|
461 |
for i:= 0 to Pred(pa.Count) do |
|
462 |
with pa.ar[i] do |
|
463 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
|
464 |
begin |
|
561 | 465 |
px:= x; |
466 |
py:= y; |
|
371 | 467 |
x:= x + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3; |
561 | 468 |
y:= y + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3; |
469 |
if CheckSelfIntersect(pa, i) then |
|
470 |
begin |
|
471 |
x:= px; |
|
472 |
y:= py |
|
473 |
end; |
|
364 | 474 |
end |
67 | 475 |
end; |
476 |
||
364 | 477 |
|
23 | 478 |
procedure GenBlank(var Template: TEdgeTemplate); |
4 | 479 |
var pa: TPixAr; |
23 | 480 |
i: Longword; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
481 |
y, x: Longword; |
4 | 482 |
begin |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
483 |
for y:= 0 to 1023 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
484 |
for x:= 0 to 2047 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
485 |
Land[y, x]:= COLOR_LAND; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
486 |
|
358 | 487 |
SetPoints(Template, pa); |
429 | 488 |
for i:= 1 to Template.BezierizeCount do |
489 |
begin |
|
431 | 490 |
BezierizeEdge(pa, _0_5); |
561 | 491 |
RandomizePoints(pa); |
429 | 492 |
RandomizePoints(pa) |
493 |
end; |
|
494 |
for i:= 1 to Template.RandPassesCount do RandomizePoints(pa); |
|
365 | 495 |
BezierizeEdge(pa, _0_1); |
27 | 496 |
|
365 | 497 |
DrawEdge(pa, 0); |
27 | 498 |
|
358 | 499 |
with Template do |
23 | 500 |
for i:= 0 to pred(FillPointsCount) do |
501 |
with FillPoints^[i] do |
|
89 | 502 |
FillLand(x, y); |
503 |
||
365 | 504 |
DrawEdge(pa, COLOR_LAND) |
23 | 505 |
end; |
506 |
||
371 | 507 |
function SelectTemplate: LongInt; |
161 | 508 |
begin |
351 | 509 |
SelectTemplate:= getrandom(Succ(High(EdgeTemplates))) |
161 | 510 |
end; |
511 |
||
23 | 512 |
procedure GenLandSurface; |
513 |
var tmpsurf: PSDL_Surface; |
|
514 |
begin |
|
53 | 515 |
WriteLnToConsole('Generating land...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
516 |
|
161 | 517 |
GenBlank(EdgeTemplates[SelectTemplate]); |
22 | 518 |
|
4 | 519 |
AddProgress; |
754 | 520 |
|
758 | 521 |
tmpsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, 2048, 1024, 32, RMask, GMask, BMask, 0); |
754 | 522 |
|
67 | 523 |
TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true); |
4 | 524 |
ColorizeLand(tmpsurf); |
525 |
AddProgress; |
|
526 |
AddBorder(tmpsurf); |
|
754 | 527 |
|
528 |
LandSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, 2048, 1024, 32, RMask, GMask, BMask, AMask); |
|
529 |
||
67 | 530 |
TryDo(LandSurface <> nil, 'Error creating land surface', true); |
4 | 531 |
SDL_FillRect(LandSurface, nil, 0); |
27 | 532 |
AddProgress; |
70 | 533 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0); |
534 |
AddObjects(tmpsurf, LandSurface); |
|
535 |
SDL_FreeSurface(tmpsurf); |
|
24 | 536 |
|
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
537 |
UpdateLandTexture(0, 1024); |
70 | 538 |
AddProgress |
4 | 539 |
end; |
540 |
||
541 |
procedure MakeFortsMap; |
|
547 | 542 |
var tmpsurf: PSDL_Surface; |
4 | 543 |
begin |
53 | 544 |
WriteLnToConsole('Generating forts land...'); |
547 | 545 |
TryDo(TeamsCount = 2, 'More or less than 2 teams on map in forts mode!', true); |
546 |
||
754 | 547 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, 32, RMask, GMask, BMask, AMask); |
548 |
||
37 | 549 |
SDL_FillRect(LandSurface, nil, 0); |
547 | 550 |
|
551 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + TeamsArray[0]^.FortName + 'L', false, true, true); |
|
4 | 552 |
BlitImageAndGenerateCollisionInfo(0, 0, tmpsurf, LandSurface); |
553 |
SDL_FreeSurface(tmpsurf); |
|
547 | 554 |
|
555 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + TeamsArray[1]^.FortName + 'R', false, true, true); |
|
4 | 556 |
BlitImageAndGenerateCollisionInfo(1024, 0, tmpsurf, LandSurface); |
557 |
SDL_FreeSurface(tmpsurf); |
|
754 | 558 |
|
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
559 |
UpdateLandTexture(0, 1024) |
4 | 560 |
end; |
561 |
||
53 | 562 |
procedure LoadMap; |
107 | 563 |
var x, y: Longword; |
564 |
p: PByteArray; |
|
53 | 565 |
begin |
566 |
WriteLnToConsole('Loading land from file...'); |
|
567 |
AddProgress; |
|
351 | 568 |
LandSurface:= LoadImage(Pathz[ptMapCurrent] + '/map', false, true, true); |
569 |
TryDo((LandSurface^.w = 2048) and (LandSurface^.h = 1024), 'Map dimensions should be 2048x1024!', true); |
|
53 | 570 |
|
571 |
if SDL_MustLock(LandSurface) then |
|
572 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true); |
|
573 |
||
351 | 574 |
p:= LandSurface^.pixels; |
575 |
case LandSurface^.format^.BytesPerPixel of |
|
53 | 576 |
1: OutError('We don''t work with 8 bit surfaces', true); |
754 | 577 |
2: OutError('We don''t work with 16 bit surfaces', true); |
53 | 578 |
3: for y:= 0 to 1023 do |
579 |
begin |
|
580 |
for x:= 0 to 2047 do |
|
351 | 581 |
if (p^[x * 3 + 0] <> 0) |
582 |
or (p^[x * 3 + 1] <> 0) |
|
583 |
or (p^[x * 3 + 2] <> 0) then Land[y, x]:= COLOR_LAND; |
|
584 |
p:= @(p^[LandSurface^.pitch]); |
|
53 | 585 |
end; |
586 |
4: for y:= 0 to 1023 do |
|
587 |
begin |
|
588 |
for x:= 0 to 2047 do |
|
827
ee4fa9ff2a7b
Aware that not visible pixels are not ground, whatever their color under alpha-channel is
unc0rr
parents:
797
diff
changeset
|
589 |
if (PLongword(@(p^[x * 4]))^ and $FF000000) <> 0 then Land[y, x]:= COLOR_LAND; |
351 | 590 |
p:= @(p^[LandSurface^.pitch]); |
53 | 591 |
end; |
592 |
end; |
|
351 | 593 |
|
53 | 594 |
if SDL_MustLock(LandSurface) then |
595 |
SDL_UnlockSurface(LandSurface); |
|
754 | 596 |
|
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
597 |
UpdateLandTexture(0, 1024) |
53 | 598 |
end; |
599 |
||
37 | 600 |
procedure GenMap; |
601 |
begin |
|
53 | 602 |
if (GameFlags and gfForts) = 0 then |
603 |
if Pathz[ptMapCurrent] <> '' then LoadMap |
|
604 |
else GenLandSurface |
|
37 | 605 |
else MakeFortsMap; |
606 |
AddProgress; |
|
607 |
{$IFDEF DEBUGFILE}LogLandDigest{$ENDIF} |
|
608 |
end; |
|
609 |
||
566 | 610 |
function GenPreview: TPreview; |
371 | 611 |
var x, y, xx, yy, t, bit: LongInt; |
566 | 612 |
Preview: TPreview; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
613 |
begin |
160 | 614 |
WriteLnToConsole('Generating preview...'); |
161 | 615 |
GenBlank(EdgeTemplates[SelectTemplate]); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
616 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
617 |
for y:= 0 to 127 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
618 |
for x:= 0 to 31 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
619 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
620 |
Preview[y, x]:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
621 |
for bit:= 0 to 7 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
622 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
623 |
t:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
624 |
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
|
625 |
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
|
626 |
if Land[yy, xx] <> 0 then inc(t); |
351 | 627 |
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
|
628 |
end |
566 | 629 |
end; |
630 |
GenPreview:= Preview |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
631 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
632 |
|
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
633 |
procedure UpdateLandTexture(Y, Height: LongInt); |
766 | 634 |
begin |
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
635 |
if LandTexture <> nil then |
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
636 |
begin |
797
e7fa1a1b9791
Fix bug with handling explosions (wrong parameters passed to glTexSubImage2D) spotted by Tiyuri
unc0rr
parents:
769
diff
changeset
|
637 |
if (Height <= 0) then exit; |
e7fa1a1b9791
Fix bug with handling explosions (wrong parameters passed to glTexSubImage2D) spotted by Tiyuri
unc0rr
parents:
769
diff
changeset
|
638 |
TryDo((Y >= 0) and (Y < 1024), 'UpdateLandTexture: wrong Y parameter', true); |
e7fa1a1b9791
Fix bug with handling explosions (wrong parameters passed to glTexSubImage2D) spotted by Tiyuri
unc0rr
parents:
769
diff
changeset
|
639 |
TryDo(Y + Height < 1024, 'UpdateLandTexture: wrong Height parameter', true); |
768 | 640 |
glBindTexture(GL_TEXTURE_2D, LandTexture^.id); |
641 |
||
642 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, Y, 2048, Height, GL_RGBA, GL_UNSIGNED_BYTE, @LandPixels[Y, 0]); |
|
643 |
end else |
|
644 |
begin |
|
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
645 |
TryDo(LandSurface <> nil, 'Assert (LandSurface <> nil) failed', true); |
768 | 646 |
LandTexture:= Surface2Tex(LandSurface); |
647 |
||
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
648 |
if SDL_MustLock(LandSurface) then |
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
649 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true); |
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
650 |
|
768 | 651 |
Move(LandSurface^.pixels^, LandPixels, 2048 * 1024 * 4); |
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
652 |
|
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
653 |
if SDL_MustLock(LandSurface) then |
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
654 |
SDL_UnlockSurface(LandSurface); |
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
655 |
|
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
656 |
SDL_FreeSurface(LandSurface); |
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
657 |
LandSurface:= nil |
768 | 658 |
end; |
659 |
||
766 | 660 |
end; |
661 |
||
51 | 662 |
initialization |
663 |
||
4 | 664 |
end. |