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