author | unc0rr |
Thu, 22 Jan 2009 17:53:39 +0000 | |
changeset 1738 | 00e8dadce69a |
parent 1537 | d34ef0fb003d |
child 1753 | 2ccba26f1aa4 |
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; |
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
|
25 |
TDirtyTag = packed array[0..31, 0..63] 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 |
|
358 | 121 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
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; |
248 |
if (_y < 0) or (_y > 1023) then exit; |
|
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); |
282 |
while (xr < 2047) 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; |
308 |
while r.y < 1024 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; |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
311 |
while r.x < 2048 do |
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); |
4 | 321 |
for x:= 0 to 2047 do |
1182
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
322 |
begin |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
323 |
yd:= 1023; |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
324 |
repeat |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
325 |
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
|
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 |
|
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
329 |
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
|
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 |
|
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
336 |
if (yd < 1023) and ((yd - yu) >= 16) then |
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 |
360 | 378 |
pa.ar[i].x:= 2047 - pa.ar[i].x; |
358 | 379 |
for i:= 0 to pred(FillPointsCount) do |
380 |
FillPoints^[i].x:= 2047 - FillPoints^[i].x; |
|
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 |
|
360 | 387 |
pa.ar[i].y:= 1023 - pa.ar[i].y; |
358 | 388 |
for i:= 0 to pred(FillPointsCount) do |
389 |
FillPoints^[i].y:= 1023 - FillPoints^[i].y; |
|
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 |
|
443 |
radz[i]:= Min(Max(x - cEdge, 0), Max(2048 - cEdge - x, 0)); |
|
444 |
radz[i]:= Min(radz[i], Min(Max(y - cEdge, 0), Max(1024 - cEdge - y, 0))); |
|
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 |
|
456 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
|
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 |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
476 |
for y:= 0 to 1023 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
477 |
for x:= 0 to 2047 do |
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; |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
515 |
for y:= 0 to 1023 do |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
516 |
begin |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
517 |
for x:= 0 to 2047 do |
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; |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
519 |
p:= @(p^[Surface^.pitch div 4]); |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
520 |
end; |
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
521 |
|
1182
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
522 |
if SDL_MustLock(Surface) then |
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
523 |
SDL_UnlockSurface(Surface) |
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
524 |
end; |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
525 |
|
23 | 526 |
procedure GenLandSurface; |
527 |
var tmpsurf: PSDL_Surface; |
|
528 |
begin |
|
53 | 529 |
WriteLnToConsole('Generating land...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
530 |
|
161 | 531 |
GenBlank(EdgeTemplates[SelectTemplate]); |
22 | 532 |
|
4 | 533 |
AddProgress; |
754 | 534 |
|
758 | 535 |
tmpsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, 2048, 1024, 32, RMask, GMask, BMask, 0); |
754 | 536 |
|
67 | 537 |
TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true); |
4 | 538 |
ColorizeLand(tmpsurf); |
1190
73ec31d8bb6f
Enable back rendering objects that are put on top of land texture
unc0rr
parents:
1183
diff
changeset
|
539 |
AddOnLandObjects(tmpsurf); |
754 | 540 |
|
1182
e2e13aa055c1
Step 3: Maps are rendered correctly, but without objects yet
unc0rr
parents:
1181
diff
changeset
|
541 |
LandSurface2LandPixels(tmpsurf); |
70 | 542 |
SDL_FreeSurface(tmpsurf); |
24 | 543 |
|
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
544 |
AddProgress; |
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 |
AddObjects; |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
547 |
|
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
548 |
UpdateLandTexture(0, 1023); |
70 | 549 |
AddProgress |
4 | 550 |
end; |
551 |
||
552 |
procedure MakeFortsMap; |
|
547 | 553 |
var tmpsurf: PSDL_Surface; |
4 | 554 |
begin |
53 | 555 |
WriteLnToConsole('Generating forts land...'); |
547 | 556 |
|
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
557 |
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
|
558 |
BlitImageAndGenerateCollisionInfo(0, 0, 1024, tmpsurf); |
4 | 559 |
SDL_FreeSurface(tmpsurf); |
547 | 560 |
|
1537 | 561 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + ClansArray[1]^.Teams[0]^.FortName + 'R', true, true, true); |
1183
540cea859395
Step 4: repair girder rendering (girder is 32bit now)
unc0rr
parents:
1182
diff
changeset
|
562 |
BlitImageAndGenerateCollisionInfo(1024, 0, 1024, tmpsurf); |
4 | 563 |
SDL_FreeSurface(tmpsurf); |
754 | 564 |
|
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
565 |
UpdateLandTexture(0, 1023) |
4 | 566 |
end; |
567 |
||
53 | 568 |
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
|
569 |
var tmpsurf: PSDL_Surface; |
53 | 570 |
begin |
571 |
WriteLnToConsole('Loading land from file...'); |
|
572 |
AddProgress; |
|
1181 | 573 |
tmpsurf:= LoadImage(Pathz[ptMapCurrent] + '/map', true, true, true); |
574 |
TryDo((tmpsurf^.w = 2048) and (tmpsurf^.h = 1024), 'Map dimensions should be 2048x1024!', true); |
|
53 | 575 |
|
1181 | 576 |
TryDo(tmpsurf^.format^.BytesPerPixel = 4, 'Map should be 32bit', true); |
351 | 577 |
|
1183
540cea859395
Step 4: repair girder rendering (girder is 32bit now)
unc0rr
parents:
1182
diff
changeset
|
578 |
BlitImageAndGenerateCollisionInfo(0, 0, 2048, tmpsurf); |
1181 | 579 |
SDL_FreeSurface(tmpsurf); |
754 | 580 |
|
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
581 |
UpdateLandTexture(0, 1023) |
53 | 582 |
end; |
583 |
||
37 | 584 |
procedure GenMap; |
585 |
begin |
|
1085
0b82870073b5
Load flakes information from theme.cfg when playing painted map
unc0rr
parents:
1066
diff
changeset
|
586 |
LoadThemeConfig; |
0b82870073b5
Load flakes information from theme.cfg when playing painted map
unc0rr
parents:
1066
diff
changeset
|
587 |
|
53 | 588 |
if (GameFlags and gfForts) = 0 then |
589 |
if Pathz[ptMapCurrent] <> '' then LoadMap |
|
590 |
else GenLandSurface |
|
37 | 591 |
else MakeFortsMap; |
592 |
AddProgress; |
|
593 |
{$IFDEF DEBUGFILE}LogLandDigest{$ENDIF} |
|
594 |
end; |
|
595 |
||
566 | 596 |
function GenPreview: TPreview; |
371 | 597 |
var x, y, xx, yy, t, bit: LongInt; |
566 | 598 |
Preview: TPreview; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
599 |
begin |
160 | 600 |
WriteLnToConsole('Generating preview...'); |
161 | 601 |
GenBlank(EdgeTemplates[SelectTemplate]); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
602 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
603 |
for y:= 0 to 127 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
604 |
for x:= 0 to 31 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
605 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
606 |
Preview[y, x]:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
607 |
for bit:= 0 to 7 do |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
608 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
609 |
t:= 0; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
610 |
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
|
611 |
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
|
612 |
if Land[yy, xx] <> 0 then inc(t); |
351 | 613 |
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
|
614 |
end |
566 | 615 |
end; |
616 |
GenPreview:= Preview |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
617 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
618 |
|
767
697728ffe39f
Introduce UpdateLandTexture function to update just parts of surface
unc0rr
parents:
766
diff
changeset
|
619 |
procedure UpdateLandTexture(Y, Height: LongInt); |
766 | 620 |
begin |
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
621 |
if (Height <= 0) then exit; |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
622 |
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
|
623 |
TryDo(Y + Height < 1024, 'UpdateLandTexture: wrong Height parameter', true); |
768 | 624 |
|
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
625 |
if LandTexture = nil then |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
626 |
LandTexture:= NewTexture(2048, 1024, @LandPixels) |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
627 |
else |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
628 |
begin |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
629 |
glBindTexture(GL_TEXTURE_2D, LandTexture^.id); |
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1085
diff
changeset
|
630 |
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
|
631 |
end |
766 | 632 |
end; |
633 |
||
51 | 634 |
initialization |
635 |
||
4 | 636 |
end. |