author | unc0rr |
Tue, 20 Jun 2006 21:20:06 +0000 | |
changeset 67 | 3101306251e5 |
parent 64 | 9df467527ae5 |
child 70 | 82d93eeecebe |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*) |
|
33 |
||
34 |
unit uLand; |
|
35 |
interface |
|
36 |
uses SDLh; |
|
37 |
{$include options.inc} |
|
38 |
type TLandArray = packed array[0..1023, 0..2047] of LongWord; |
|
39 |
||
40 |
var Land: TLandArray; |
|
41 |
LandSurface: PSDL_Surface; |
|
42 |
||
43 |
procedure AddHHPoint(_x, _y: integer); |
|
44 |
procedure GetHHPoint(out _x, _y: integer); |
|
45 |
procedure RandomizeHHPoints; |
|
37 | 46 |
procedure GenMap; |
4 | 47 |
|
48 |
implementation |
|
37 | 49 |
uses uConsole, uStore, uMisc, uConsts, uRandom, uTeams, uIO, uLandTemplates, uLandObjects, uSHA; |
4 | 50 |
|
51 |
type TPixAr = record |
|
52 |
Count: Longword; |
|
22 | 53 |
ar: array[0..Pred(cMaxEdgePoints)] of TPoint; |
4 | 54 |
end; |
55 |
||
56 |
var HHPoints: record |
|
57 |
First, Last: word; |
|
22 | 58 |
ar: array[1..Pred(cMaxSpawnPoints)] of TPoint |
51 | 59 |
end; |
4 | 60 |
|
37 | 61 |
procedure LogLandDigest; |
62 |
var ctx: TSHA1Context; |
|
63 |
dig: TSHA1Digest; |
|
64 |
begin |
|
65 |
SHA1Init(ctx); |
|
66 |
SHA1Update(ctx, @Land, sizeof(Land)); |
|
67 |
dig:= SHA1Final(ctx); |
|
57 | 68 |
{$IFDEF DEBUGFILE} |
37 | 69 |
AddFileLog('SHA1 Land digest: {'+inttostr(dig.LongWords[0])+':' |
70 |
+inttostr(dig.LongWords[1])+':'+inttostr(dig.LongWords[2])+':' |
|
71 |
+inttostr(dig.LongWords[3])+':'+inttostr(dig.LongWords[4])+'}'); |
|
57 | 72 |
{$ENDIF} |
37 | 73 |
end; |
74 |
||
22 | 75 |
procedure DrawBezierEdge(var pa: TPixAr); |
4 | 76 |
var x, y, i: integer; |
77 |
tx, ty, vx, vy, vlen, t: real; |
|
78 |
r1, r2, r3, r4: real; |
|
79 |
x1, y1, x2, y2, cx1, cy1, cx2, cy2, tsq, tcb: real; |
|
80 |
begin |
|
81 |
vx:= 0; |
|
82 |
vy:= 0; |
|
83 |
with pa do |
|
84 |
for i:= 0 to Count-2 do |
|
85 |
begin |
|
86 |
vlen:= sqrt(sqr(ar[i + 1].x - ar[i ].X) + sqr(ar[i + 1].y - ar[i ].y)); |
|
87 |
t:= sqrt(sqr(ar[i + 1].x - ar[i + 2].X) + sqr(ar[i + 1].y - ar[i + 2].y)); |
|
88 |
if t<vlen then vlen:= t; |
|
89 |
vlen:= vlen/3; |
|
90 |
tx:= ar[i+2].X - ar[i].X; |
|
91 |
ty:= ar[i+2].y - ar[i].y; |
|
92 |
t:= sqrt(sqr(tx)+sqr(ty)); |
|
93 |
if t = 0 then |
|
94 |
begin |
|
95 |
tx:= -tx * 100000; |
|
96 |
ty:= -ty * 100000; |
|
97 |
end else |
|
98 |
begin |
|
99 |
tx:= -tx/t; |
|
100 |
ty:= -ty/t; |
|
101 |
end; |
|
102 |
t:= 1.0*vlen; |
|
103 |
tx:= tx*t; |
|
104 |
ty:= ty*t; |
|
105 |
x1:= ar[i].x; |
|
106 |
y1:= ar[i].y; |
|
107 |
x2:= ar[i + 1].x; |
|
108 |
y2:= ar[i + 1].y; |
|
109 |
cx1:= ar[i].X + trunc(vx); |
|
110 |
cy1:= ar[i].y + trunc(vy); |
|
111 |
cx2:= ar[i+1].X + trunc(tx); |
|
112 |
cy2:= ar[i+1].y + trunc(ty); |
|
113 |
vx:= -tx; |
|
114 |
vy:= -ty; |
|
115 |
t:= 0; |
|
116 |
while t <= 1.0 do |
|
117 |
begin |
|
118 |
tsq:= sqr(t); |
|
119 |
tcb:= tsq * t; |
|
120 |
r1:= (1 - 3*t + 3*tsq - tcb) * x1; |
|
121 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cx1; |
|
122 |
r3:= ( 3*tsq - 3*tcb) * cx2; |
|
123 |
r4:= ( tcb) * x2; |
|
124 |
X:= round(r1 + r2 + r3 + r4); |
|
125 |
r1:= (1 - 3*t + 3*tsq - tcb) * y1; |
|
126 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cy1; |
|
127 |
r3:= ( 3*tsq - 3*tcb) * cy2; |
|
128 |
r4:= ( tcb) * y2; |
|
129 |
Y:= round(r1 + r2 + r3 + r4); |
|
130 |
t:= t + 0.001; |
|
131 |
if ((x and $FFFFF800) = 0) and ((y and $FFFFFC00) = 0) then |
|
51 | 132 |
Land[y, x]:= 0; |
4 | 133 |
end; |
134 |
end; |
|
135 |
end; |
|
136 |
||
22 | 137 |
procedure BezierizeEdge(var pa: TPixAr; Delta: real); |
138 |
var x, y, i: integer; |
|
139 |
tx, ty, vx, vy, vlen, t: real; |
|
140 |
r1, r2, r3, r4: real; |
|
141 |
x1, y1, x2, y2, cx1, cy1, cx2, cy2, tsq, tcb: real; |
|
142 |
opa: TPixAr; |
|
143 |
begin |
|
144 |
opa:= pa; |
|
145 |
pa.Count:= 0; |
|
146 |
vx:= 0; |
|
147 |
vy:= 0; |
|
148 |
with opa do |
|
149 |
for i:= 0 to Count-2 do |
|
150 |
begin |
|
151 |
vlen:= sqrt(sqr(ar[i + 1].x - ar[i ].X) + sqr(ar[i + 1].y - ar[i ].y)); |
|
152 |
t:= sqrt(sqr(ar[i + 1].x - ar[i + 2].X) + sqr(ar[i + 1].y - ar[i + 2].y)); |
|
153 |
if t<vlen then vlen:= t; |
|
154 |
vlen:= vlen/3; |
|
155 |
tx:= ar[i+2].X - ar[i].X; |
|
156 |
ty:= ar[i+2].y - ar[i].y; |
|
157 |
t:= sqrt(sqr(tx)+sqr(ty)); |
|
158 |
if t = 0 then |
|
159 |
begin |
|
160 |
tx:= -tx * 100000; |
|
161 |
ty:= -ty * 100000; |
|
162 |
end else |
|
163 |
begin |
|
164 |
tx:= -tx/t; |
|
165 |
ty:= -ty/t; |
|
166 |
end; |
|
167 |
t:= 1.0*vlen; |
|
168 |
tx:= tx*t; |
|
169 |
ty:= ty*t; |
|
170 |
x1:= ar[i].x; |
|
171 |
y1:= ar[i].y; |
|
172 |
x2:= ar[i + 1].x; |
|
173 |
y2:= ar[i + 1].y; |
|
174 |
cx1:= ar[i].X + trunc(vx); |
|
175 |
cy1:= ar[i].y + trunc(vy); |
|
176 |
cx2:= ar[i+1].X + trunc(tx); |
|
177 |
cy2:= ar[i+1].y + trunc(ty); |
|
178 |
vx:= -tx; |
|
179 |
vy:= -ty; |
|
180 |
t:= 0; |
|
181 |
while t <= 1.0 do |
|
182 |
begin |
|
183 |
tsq:= sqr(t); |
|
184 |
tcb:= tsq * t; |
|
185 |
r1:= (1 - 3*t + 3*tsq - tcb) * x1; |
|
186 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cx1; |
|
187 |
r3:= ( 3*tsq - 3*tcb) * cx2; |
|
188 |
r4:= ( tcb) * x2; |
|
189 |
X:= round(r1 + r2 + r3 + r4); |
|
190 |
r1:= (1 - 3*t + 3*tsq - tcb) * y1; |
|
191 |
r2:= ( 3*t - 6*tsq + 3*tcb) * cy1; |
|
192 |
r3:= ( 3*tsq - 3*tcb) * cy2; |
|
193 |
r4:= ( tcb) * y2; |
|
194 |
Y:= round(r1 + r2 + r3 + r4); |
|
195 |
t:= t + Delta; |
|
196 |
pa.ar[pa.Count].x:= X; |
|
197 |
pa.ar[pa.Count].y:= Y; |
|
198 |
inc(pa.Count); |
|
199 |
TryDo(pa.Count < cMaxEdgePoints, 'Edge points overflow', true) |
|
200 |
end; |
|
201 |
end; |
|
202 |
end; |
|
203 |
||
4 | 204 |
procedure FillLand(x, y: integer); |
205 |
var Stack: record |
|
206 |
Count: Longword; |
|
207 |
points: array[0..8192] of record |
|
208 |
xl, xr, y, dir: integer; |
|
209 |
end |
|
210 |
end; |
|
211 |
||
212 |
procedure Push(_xl, _xr, _y, _dir: integer); |
|
213 |
begin |
|
214 |
_y:= _y + _dir; |
|
215 |
if (_y < 0) or (_y > 1023) then exit; |
|
216 |
with Stack.points[Stack.Count] do |
|
217 |
begin |
|
218 |
xl:= _xl; |
|
219 |
xr:= _xr; |
|
220 |
y:= _y; |
|
221 |
dir:= _dir |
|
222 |
end; |
|
223 |
inc(Stack.Count); |
|
224 |
TryDo(Stack.Count < 8192, 'stack overflow', true) |
|
225 |
end; |
|
226 |
||
227 |
procedure Pop(out _xl, _xr, _y, _dir: integer); |
|
228 |
begin |
|
229 |
dec(Stack.Count); |
|
230 |
with Stack.points[Stack.Count] do |
|
231 |
begin |
|
232 |
_xl:= xl; |
|
233 |
_xr:= xr; |
|
234 |
_y:= y; |
|
235 |
_dir:= dir |
|
236 |
end |
|
237 |
end; |
|
238 |
||
239 |
var xl, xr, dir: integer; |
|
240 |
begin |
|
241 |
Stack.Count:= 0; |
|
242 |
xl:= x - 1; |
|
243 |
xr:= x; |
|
23 | 244 |
Push(xl, xr, y, -1); |
245 |
Push(xl, xr, y, 1); |
|
4 | 246 |
while Stack.Count > 0 do |
247 |
begin |
|
248 |
Pop(xl, xr, y, dir); |
|
51 | 249 |
while (xl > 0) and (Land[y, xl] <> 0) do dec(xl); |
250 |
while (xr < 2047) and (Land[y, xr] <> 0) do inc(xr); |
|
4 | 251 |
while (xl < xr) do |
252 |
begin |
|
51 | 253 |
while (xl <= xr) and (Land[y, xl] = 0) do inc(xl); |
4 | 254 |
x:= xl; |
51 | 255 |
while (xl <= xr) and (Land[y, xl] <> 0) do |
4 | 256 |
begin |
51 | 257 |
Land[y, xl]:= 0; |
4 | 258 |
inc(xl) |
259 |
end; |
|
22 | 260 |
if x < xl then |
261 |
begin |
|
262 |
Push(x, Pred(xl), y, dir); |
|
263 |
Push(x, Pred(xl), y,-dir); |
|
264 |
end; |
|
4 | 265 |
end; |
266 |
end; |
|
267 |
end; |
|
268 |
||
269 |
procedure ColorizeLand(Surface: PSDL_Surface); |
|
270 |
var tmpsurf: PSDL_Surface; |
|
271 |
r: TSDL_Rect; |
|
272 |
begin |
|
54 | 273 |
tmpsurf:= LoadImage(Pathz[ptThemeCurrent] + '/LandTex.png', false); |
4 | 274 |
r.y:= 0; |
275 |
while r.y < 1024 do |
|
276 |
begin |
|
277 |
r.x:= 0; |
|
278 |
while r.x < 2048 do |
|
279 |
begin |
|
280 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
|
281 |
inc(r.x, tmpsurf.w) |
|
282 |
end; |
|
283 |
inc(r.y, tmpsurf.h) |
|
284 |
end; |
|
285 |
SDL_FreeSurface(tmpsurf); |
|
286 |
||
287 |
tmpsurf:= SDL_CreateRGBSurfaceFrom(@Land, 2048, 1024, 32, 2048*4, $FF0000, $FF00, $FF, 0); |
|
288 |
SDLTry(tmpsurf <> nil, true); |
|
289 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, SDL_MapRGB(tmpsurf.format, $FF, $FF, $FF)); |
|
22 | 290 |
SDL_UpperBlit(tmpsurf, nil, Surface, nil); |
291 |
SDL_FreeSurface(tmpsurf) |
|
4 | 292 |
end; |
293 |
||
294 |
procedure AddBorder(Surface: PSDL_Surface); |
|
295 |
var tmpsurf: PSDL_Surface; |
|
296 |
r, rr: TSDL_Rect; |
|
297 |
x, yd, yu: integer; |
|
298 |
begin |
|
54 | 299 |
tmpsurf:= LoadImage(Pathz[ptThemeCurrent] + '/Border.png', false); |
4 | 300 |
for x:= 0 to 2047 do |
301 |
begin |
|
302 |
yd:= 1023; |
|
303 |
repeat |
|
304 |
while (yd > 0 ) and (Land[yd, x] = 0) do dec(yd); |
|
305 |
if (yd < 0) then yd:= 0; |
|
306 |
while (yd < 1024) and (Land[yd, x] <> 0) do inc(yd); |
|
307 |
dec(yd); |
|
308 |
yu:= yd; |
|
309 |
while (yu > 0 ) and (Land[yu, x] <> 0) do dec(yu); |
|
310 |
while (yu < yd ) and (Land[yu, x] = 0) do inc(yu); |
|
311 |
if (yd < 1023) and ((yd - yu) >= 16) then |
|
312 |
begin |
|
313 |
rr.x:= x; |
|
314 |
rr.y:= yd - 15; |
|
315 |
r.x:= x mod tmpsurf.w; |
|
316 |
r.y:= 16; |
|
317 |
r.w:= 1; |
|
318 |
r.h:= 16; |
|
319 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
320 |
end; |
|
321 |
if (yu > 0) then |
|
322 |
begin |
|
323 |
rr.x:= x; |
|
324 |
rr.y:= yu; |
|
325 |
r.x:= x mod tmpsurf.w; |
|
326 |
r.y:= 0; |
|
327 |
r.w:= 1; |
|
328 |
r.h:= min(16, yd - yu + 1); |
|
329 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
330 |
end; |
|
331 |
yd:= yu - 1; |
|
332 |
until yd < 0; |
|
333 |
end; |
|
334 |
end; |
|
335 |
||
336 |
procedure AddHHPoints; |
|
10 | 337 |
var x, y, t: integer; |
8
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
338 |
|
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
339 |
function CountNonZeroz(x, y: integer): integer; |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
340 |
var i: integer; |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
341 |
begin |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
342 |
Result:= 0; |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
343 |
if (y and $FFFFFC00) <> 0 then exit; |
23 | 344 |
for i:= max(x - 5, 0) to min(x + 5, 2043) do |
8
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
345 |
if Land[y, i] <> 0 then inc(Result) |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
346 |
end; |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
347 |
|
4 | 348 |
begin |
10 | 349 |
x:= 40; |
350 |
while x < 2010 do |
|
4 | 351 |
begin |
8
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
352 |
y:= -24; |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
353 |
while y < 1023 do |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
354 |
begin |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
355 |
repeat |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
356 |
inc(y, 2); |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
357 |
until (y > 1023) or (CountNonZeroz(x, y) = 0); |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
358 |
t:= 0; |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
359 |
repeat |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
360 |
inc(y, 2); |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
361 |
inc(t, 2) |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
362 |
until (y > 1023) or (CountNonZeroz(x, y) <> 0); |
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
363 |
if (t > 22) and (y < 1023) then AddHHPoint(x, y - 12); |
27 | 364 |
inc(y, 80) |
8
24048039955c
- Hedgehogs spawn points can be generated under the girder
unc0rr
parents:
4
diff
changeset
|
365 |
end; |
27 | 366 |
inc(x, 100) |
4 | 367 |
end; |
22 | 368 |
|
369 |
if HHPoints.Last < cMaxHHs then |
|
370 |
begin |
|
371 |
AddHHPoint(300, 800); |
|
372 |
AddHHPoint(400, 800); |
|
373 |
AddHHPoint(500, 800); |
|
374 |
AddHHPoint(600, 800); |
|
375 |
AddHHPoint(700, 800); |
|
376 |
AddHHPoint(800, 800); |
|
377 |
AddHHPoint(900, 800); |
|
378 |
AddHHPoint(1000, 800); |
|
379 |
AddHHPoint(1100, 800); |
|
380 |
AddHHPoint(1200, 800); |
|
381 |
AddHHPoint(1300, 800); |
|
382 |
AddHHPoint(1400, 800); |
|
383 |
end; |
|
384 |
end; |
|
385 |
||
23 | 386 |
procedure PointWave(var Template: TEdgeTemplate; var pa: TPixAr); |
27 | 387 |
const MAXPASSES = 32; |
388 |
var ar: array[0..MAXPASSES, 0..5] of real; |
|
22 | 389 |
i, k: integer; |
390 |
rx, ry, oy: real; |
|
23 | 391 |
PassesNum: Longword; |
22 | 392 |
begin |
23 | 393 |
with Template do |
394 |
begin |
|
395 |
PassesNum:= PassMin + getrandom(PassDelta); |
|
396 |
TryDo(PassesNum < MAXPASSES, 'Passes number too big', true); |
|
27 | 397 |
ar[0, 1]:= WaveFreqMin; |
398 |
ar[0, 4]:= WaveFreqMin; |
|
23 | 399 |
for i:= 1 to PassesNum do // initialize random parameters |
400 |
begin |
|
401 |
ar[i, 0]:= WaveAmplMin + getrandom * WaveAmplDelta; |
|
27 | 402 |
ar[i, 1]:= ar[i - 1, 1] + (getrandom * 0.7 + 0.3) * WaveFreqDelta; |
23 | 403 |
ar[i, 2]:= getrandom * pi * 2; |
404 |
ar[i, 3]:= WaveAmplMin + getrandom * WaveAmplDelta; |
|
27 | 405 |
ar[i, 4]:= ar[i - 1, 4] + (getrandom * 0.7 + 0.3) * WaveFreqDelta; |
23 | 406 |
ar[i, 5]:= getrandom * pi * 2; |
53 | 407 |
{$IFDEF DEBUGFILE} |
408 |
AddFileLog('Wave params ¹' + inttostr(i) + ':'); |
|
409 |
AddFileLog('X: ampl = ' + floattostr(ar[i, 0]) + '; freq = ' + floattostr(ar[i, 1]) + '; shift = ' + floattostr(ar[i, 2])); |
|
410 |
AddFileLog('Y: ampl = ' + floattostr(ar[i, 3]) + '; freq = ' + floattostr(ar[i, 4]) + '; shift = ' + floattostr(ar[i, 5])); |
|
411 |
{$ENDIF} |
|
23 | 412 |
end; |
413 |
end; |
|
22 | 414 |
|
415 |
for k:= 0 to Pred(pa.Count) do // apply transformation |
|
416 |
begin |
|
417 |
rx:= pa.ar[k].x; |
|
418 |
ry:= pa.ar[k].y; |
|
24 | 419 |
for i:= 1 to PassesNum do |
22 | 420 |
begin |
421 |
oy:= ry; |
|
422 |
ry:= ry + ar[i, 0] * sin(ar[i, 1] * rx + ar[i, 2]); |
|
423 |
rx:= rx + ar[i, 3] * sin(ar[i, 4] * oy + ar[i, 5]); |
|
424 |
end; |
|
24 | 425 |
pa.ar[k].x:= round(rx); |
426 |
pa.ar[k].y:= round(ry); |
|
427 |
end; |
|
4 | 428 |
end; |
429 |
||
67 | 430 |
procedure NormalizePoints(var pa: TPixAr); |
431 |
const brd = 32; |
|
432 |
var isUP: boolean; // HACK: transform for Y should be exact as one for X |
|
433 |
Left, Right, Top, Bottom, |
|
434 |
OWidth, Width, OHeight, Height, |
|
435 |
OLeft: integer; |
|
436 |
i: integer; |
|
437 |
begin |
|
438 |
TryDo((pa.ar[0].y < 0) or (pa.ar[0].y > 1023), 'Bad land generated', true); |
|
439 |
isUP:= pa.ar[0].y > 0; |
|
440 |
Left:= 1023; |
|
441 |
Right:= Left; |
|
442 |
Top:= pa.ar[0].y; |
|
443 |
Bottom:= Top; |
|
444 |
||
445 |
for i:= 1 to Pred(pa.Count) do |
|
446 |
with pa.ar[i] do |
|
447 |
begin |
|
448 |
if (y and $FFFFFC00) = 0 then |
|
449 |
if x < Left then Left:= x else |
|
450 |
if x > Right then Right:= x; |
|
451 |
if y < Top then Top:= y else |
|
452 |
if y > Bottom then Bottom:= y |
|
453 |
end; |
|
454 |
||
455 |
if (Left < brd) or (Right > 2047 - brd) then |
|
456 |
begin |
|
457 |
OLeft:= Left; |
|
458 |
OWidth:= Right - OLeft; |
|
459 |
if Left < brd then Left:= brd; |
|
460 |
if Right > 2047 - brd then Right:= 2047 - brd; |
|
461 |
Width:= Right - Left; |
|
462 |
for i:= 0 to Pred(pa.Count) do |
|
463 |
with pa.ar[i] do |
|
464 |
x:= round((x - OLeft) * Width div OWidth + Left) |
|
465 |
end; |
|
466 |
||
467 |
if isUp then // FIXME: remove hack |
|
468 |
if Top < brd then |
|
469 |
begin |
|
470 |
OHeight:= 1023 - Top; |
|
471 |
Height:= 1023 - brd; |
|
472 |
for i:= 0 to Pred(pa.Count) do |
|
473 |
with pa.ar[i] do |
|
474 |
y:= round((y - 1023) * Height div OHeight + 1023) |
|
475 |
end; |
|
476 |
end; |
|
477 |
||
23 | 478 |
procedure GenBlank(var Template: TEdgeTemplate); |
4 | 479 |
var pa: TPixAr; |
23 | 480 |
i: Longword; |
4 | 481 |
begin |
23 | 482 |
with Template do |
483 |
begin |
|
27 | 484 |
if canMirror then |
485 |
if getrandom(16) < 8 then |
|
486 |
begin |
|
487 |
for i:= 0 to pred(BasePointsCount) do |
|
488 |
BasePoints^[i].x:= 2047 - BasePoints^[i].x; |
|
489 |
for i:= 0 to pred(FillPointsCount) do |
|
490 |
FillPoints^[i].x:= 2047 - FillPoints^[i].x; |
|
491 |
end; |
|
492 |
||
493 |
if canFlip then |
|
494 |
if getrandom(16) < 8 then |
|
495 |
begin |
|
496 |
for i:= 0 to pred(BasePointsCount) do |
|
497 |
BasePoints^[i].y:= 1023 - BasePoints^[i].y; |
|
498 |
for i:= 0 to pred(FillPointsCount) do |
|
499 |
FillPoints^[i].y:= 1023 - FillPoints^[i].y; |
|
500 |
end; |
|
501 |
||
23 | 502 |
pa.Count:= BasePointsCount; |
503 |
for i:= 0 to pred(pa.Count) do |
|
504 |
pa.ar[i]:= BasePoints^[i]; |
|
505 |
||
506 |
for i:= 1 to BezPassCnt do |
|
507 |
BezierizeEdge(pa, 0.33333334); |
|
508 |
||
509 |
PointWave(Template, pa); |
|
67 | 510 |
NormalizePoints(pa); |
23 | 511 |
DrawBezierEdge(pa); |
512 |
||
513 |
for i:= 0 to pred(FillPointsCount) do |
|
514 |
with FillPoints^[i] do |
|
515 |
FillLand(x, y) |
|
516 |
end; |
|
517 |
end; |
|
518 |
||
519 |
procedure GenLandSurface; |
|
520 |
var tmpsurf: PSDL_Surface; |
|
51 | 521 |
i: Longword; |
23 | 522 |
begin |
53 | 523 |
WriteLnToConsole('Generating land...'); |
51 | 524 |
for i:= 0 to sizeof(Land) div 4 do |
64 | 525 |
PLongword(Longword(@Land) + i * 4)^:= COLOR_LAND; |
53 | 526 |
GenBlank(EdgeTemplates[getrandom(Succ(High(EdgeTemplates)))]); |
22 | 527 |
|
4 | 528 |
AddProgress; |
529 |
with PixelFormat^ do |
|
530 |
tmpsurf:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, 0); |
|
67 | 531 |
TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true); |
4 | 532 |
ColorizeLand(tmpsurf); |
533 |
AddProgress; |
|
534 |
AddBorder(tmpsurf); |
|
535 |
with PixelFormat^ do |
|
536 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, 0); |
|
67 | 537 |
TryDo(LandSurface <> nil, 'Error creating land surface', true); |
4 | 538 |
SDL_FillRect(LandSurface, nil, 0); |
27 | 539 |
AddProgress; |
24 | 540 |
|
541 |
AddObjects(LandSurface); |
|
542 |
||
4 | 543 |
SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0); |
544 |
SDL_UpperBlit(tmpsurf, nil, LandSurface, nil); |
|
545 |
SDL_FreeSurface(tmpsurf); |
|
546 |
AddProgress; |
|
547 |
AddHHPoints; |
|
548 |
RandomizeHHPoints; |
|
549 |
end; |
|
550 |
||
551 |
procedure MakeFortsMap; |
|
552 |
var p: PTeam; |
|
553 |
tmpsurf: PSDL_Surface; |
|
554 |
begin |
|
53 | 555 |
WriteLnToConsole('Generating forts land...'); |
4 | 556 |
p:= TeamsList; |
557 |
TryDo(p <> nil, 'No teams on map!', true); |
|
558 |
with PixelFormat^ do |
|
559 |
LandSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 2048, 1024, BitsPerPixel, RMask, GMask, BMask, 0); |
|
37 | 560 |
SDL_FillRect(LandSurface, nil, 0); |
53 | 561 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p.FortName + 'L.png', false); |
4 | 562 |
BlitImageAndGenerateCollisionInfo(0, 0, tmpsurf, LandSurface); |
563 |
SDL_FreeSurface(tmpsurf); |
|
564 |
LoadFortPoints(p.FortName, false, TeamSize(p)); |
|
565 |
p:= p.Next; |
|
566 |
TryDo(p <> nil, 'Only one team on map!', true); |
|
53 | 567 |
tmpsurf:= LoadImage(Pathz[ptForts] + '/' + p.FortName + 'R.png', false); |
4 | 568 |
BlitImageAndGenerateCollisionInfo(1024, 0, tmpsurf, LandSurface); |
569 |
SDL_FreeSurface(tmpsurf); |
|
570 |
LoadFortPoints(p.FortName, true, TeamSize(p)); |
|
571 |
p:= p.Next; |
|
572 |
TryDo(p = nil, 'More than 2 teams on map in forts mode!', true); |
|
573 |
end; |
|
574 |
||
53 | 575 |
procedure LoadMap; |
576 |
var p, x, y, i: Longword; |
|
577 |
begin |
|
578 |
WriteLnToConsole('Loading land from file...'); |
|
579 |
AddProgress; |
|
580 |
LandSurface:= LoadImage(Pathz[ptMapCurrent] + '/map.png', false); |
|
581 |
TryDo((LandSurface.w = 2048) and (LandSurface.h = 1024), 'Map dimensions should be 2048x1024!', true); |
|
582 |
||
583 |
if SDL_MustLock(LandSurface) then |
|
584 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true); |
|
585 |
||
586 |
p:= Longword(LandSurface.pixels); |
|
587 |
i:= Longword(@Land); |
|
588 |
case LandSurface.format.BytesPerPixel of |
|
589 |
1: OutError('We don''t work with 8 bit surfaces', true); |
|
590 |
2: for y:= 0 to 1023 do |
|
591 |
begin |
|
592 |
for x:= 0 to 2047 do |
|
64 | 593 |
if PWord(p + x * 2)^ <> 0 then PLongWord(i + x * 4)^:= COLOR_LAND; |
53 | 594 |
inc(i, 2048 * 4); |
595 |
inc(p, LandSurface.pitch); |
|
596 |
end; |
|
597 |
3: for y:= 0 to 1023 do |
|
598 |
begin |
|
599 |
for x:= 0 to 2047 do |
|
600 |
if (PByte(p + x * 3 + 0)^ <> 0) |
|
601 |
or (PByte(p + x * 3 + 1)^ <> 0) |
|
64 | 602 |
or (PByte(p + x * 3 + 2)^ <> 0) then PLongWord(i + x * 4)^:= COLOR_LAND; |
53 | 603 |
inc(i, 2048 * 4); |
604 |
inc(p, LandSurface.pitch); |
|
605 |
end; |
|
606 |
4: for y:= 0 to 1023 do |
|
607 |
begin |
|
608 |
for x:= 0 to 2047 do |
|
64 | 609 |
if PLongword(p + x * 4)^ <> 0 then PLongWord(i + x * 4)^:= COLOR_LAND; |
53 | 610 |
inc(i, 2048 * 4); |
611 |
inc(p, LandSurface.pitch); |
|
612 |
end; |
|
613 |
end; |
|
614 |
if SDL_MustLock(LandSurface) then |
|
615 |
SDL_UnlockSurface(LandSurface); |
|
616 |
||
617 |
AddHHPoints; |
|
618 |
RandomizeHHPoints; |
|
619 |
end; |
|
620 |
||
37 | 621 |
procedure GenMap; |
622 |
begin |
|
53 | 623 |
if (GameFlags and gfForts) = 0 then |
624 |
if Pathz[ptMapCurrent] <> '' then LoadMap |
|
625 |
else GenLandSurface |
|
37 | 626 |
else MakeFortsMap; |
627 |
AddProgress; |
|
628 |
{$IFDEF DEBUGFILE}LogLandDigest{$ENDIF} |
|
629 |
end; |
|
630 |
||
4 | 631 |
procedure AddHHPoint(_x, _y: integer); |
632 |
begin |
|
633 |
with HHPoints do |
|
634 |
begin |
|
635 |
inc(Last); |
|
22 | 636 |
TryDo(Last < cMaxSpawnPoints, 'HHs coords queue overflow', true); |
4 | 637 |
with ar[Last] do |
638 |
begin |
|
639 |
x:= _x; |
|
640 |
y:= _y |
|
641 |
end |
|
642 |
end |
|
643 |
end; |
|
644 |
||
645 |
procedure GetHHPoint(out _x, _y: integer); |
|
646 |
begin |
|
647 |
with HHPoints do |
|
648 |
begin |
|
649 |
TryDo(First <= Last, 'HHs coords queue underflow ' + inttostr(First), true); |
|
650 |
with ar[First] do |
|
651 |
begin |
|
652 |
_x:= x; |
|
653 |
_y:= y |
|
654 |
end; |
|
655 |
inc(First) |
|
656 |
end |
|
657 |
end; |
|
658 |
||
659 |
procedure RandomizeHHPoints; |
|
660 |
var i, t: integer; |
|
661 |
p: TPoint; |
|
662 |
begin |
|
663 |
with HHPoints do |
|
664 |
begin |
|
665 |
for i:= First to Last do |
|
666 |
begin |
|
667 |
t:= GetRandom(Last - First + 1) + First; |
|
668 |
if i <> t then |
|
669 |
begin |
|
670 |
p:= ar[i]; |
|
671 |
ar[i]:= ar[t]; |
|
672 |
ar[t]:= p |
|
673 |
end |
|
674 |
end |
|
675 |
end |
|
676 |
end; |
|
677 |
||
51 | 678 |
initialization |
679 |
||
680 |
HHPoints.First:= 1 |
|
681 |
||
4 | 682 |
end. |