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