author | Wuzzy <Wuzzy2@mail.ru> |
Tue, 06 Aug 2019 23:33:08 +0200 | |
changeset 15324 | b761efebe3c4 |
parent 14307 | 7a7c090f96f6 |
child 15929 | 128ace913837 |
permissions | -rw-r--r-- |
10198 | 1 |
unit uLandGenTemplateBased; |
2 |
interface |
|
3 |
||
10387 | 4 |
uses uLandTemplates, uLandOutline; |
10198 | 5 |
|
6 |
procedure GenTemplated(var Template: TEdgeTemplate); |
|
10387 | 7 |
procedure DivideEdges(fillPointsCount: LongWord; var pa: TPixAr); |
10198 | 8 |
|
10499 | 9 |
var minDistance, dabDiv: LongInt; // different details size |
10477
b219c5a2317f
Fiddling with slider, unbreak maze. Next to mess around w/ perlin params.
nemo
parents:
10472
diff
changeset
|
10 |
|
10198 | 11 |
implementation |
11537
de40095f3327
- ifdef to avoid compiler warning
antonc27 <antonc27@mail.ru>
parents:
11536
diff
changeset
|
12 |
uses {$IFDEF IPHONEOS}uTypes, {$ENDIF} uVariables, uConsts, uFloat, uLandUtils, uRandom, SDLh, math; |
10198 | 13 |
|
14 |
||
15 |
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray); |
|
16 |
var i: LongInt; |
|
17 |
begin |
|
18 |
with Template do |
|
19 |
begin |
|
20 |
pa.Count:= BasePointsCount; |
|
10564
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10562
diff
changeset
|
21 |
for i:= 0 to pred(LongInt(pa.Count)) do |
10198 | 22 |
begin |
23 |
pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w)); |
|
24 |
if pa.ar[i].x <> NTPX then |
|
10199 | 25 |
pa.ar[i].x:= pa.ar[i].x + ((LAND_WIDTH - Template.TemplateWidth) div 2); |
10198 | 26 |
pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) + LAND_HEIGHT - LongInt(Template.TemplateHeight) |
27 |
end; |
|
28 |
||
29 |
if canMirror then |
|
30 |
if getrandom(2) = 0 then |
|
31 |
begin |
|
32 |
for i:= 0 to pred(BasePointsCount) do |
|
33 |
if pa.ar[i].x <> NTPX then |
|
34 |
pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x; |
|
35 |
for i:= 0 to pred(FillPointsCount) do |
|
36 |
fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x; |
|
37 |
end; |
|
38 |
||
39 |
(* Experiment in making this option more useful |
|
40 |
if ((not isNegative) and (cTemplateFilter = 4)) or |
|
41 |
(canFlip and (getrandom(2) = 0)) then |
|
42 |
begin |
|
43 |
for i:= 0 to pred(BasePointsCount) do |
|
44 |
begin |
|
45 |
pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y + (LAND_HEIGHT - TemplateHeight) * 2; |
|
46 |
if pa.ar[i].y > LAND_HEIGHT - 1 then |
|
47 |
pa.ar[i].y:= LAND_HEIGHT - 1; |
|
48 |
end; |
|
49 |
for i:= 0 to pred(FillPointsCount) do |
|
50 |
begin |
|
51 |
FillPoints^[i].y:= LAND_HEIGHT - 1 - FillPoints^[i].y + (LAND_HEIGHT - TemplateHeight) * 2; |
|
52 |
if FillPoints^[i].y > LAND_HEIGHT - 1 then |
|
53 |
FillPoints^[i].y:= LAND_HEIGHT - 1; |
|
54 |
end; |
|
55 |
end; |
|
56 |
end |
|
57 |
*) |
|
58 |
// template recycling. Pull these off the floor a bit |
|
59 |
if (not isNegative) and (cTemplateFilter = 4) then |
|
60 |
begin |
|
61 |
for i:= 0 to pred(BasePointsCount) do |
|
62 |
begin |
|
63 |
dec(pa.ar[i].y, 100); |
|
64 |
if pa.ar[i].y < 0 then |
|
65 |
pa.ar[i].y:= 0; |
|
66 |
end; |
|
67 |
for i:= 0 to pred(FillPointsCount) do |
|
68 |
begin |
|
69 |
dec(fps^[i].y, 100); |
|
70 |
if fps^[i].y < 0 then |
|
71 |
fps^[i].y:= 0; |
|
72 |
end; |
|
73 |
end; |
|
74 |
||
75 |
if (canFlip and (getrandom(2) = 0)) then |
|
76 |
begin |
|
77 |
for i:= 0 to pred(BasePointsCount) do |
|
78 |
pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y; |
|
79 |
for i:= 0 to pred(FillPointsCount) do |
|
80 |
fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y; |
|
81 |
end; |
|
82 |
end |
|
83 |
end; |
|
10199 | 84 |
|
10209 | 85 |
procedure FindPoint(si: LongInt; fillPointsCount: LongWord; var newPoint: TPoint; var pa: TPixAr); |
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
86 |
const mapBorderMargin = 40; |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
87 |
var p1, p2, p4, fp, mp: TPoint; |
10226 | 88 |
i, t1, t2, iy, ix, aqpb: LongInt; |
89 |
a, b, p, q: LongInt; |
|
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
90 |
dab, d, distL, distR: LongInt; |
10199 | 91 |
begin |
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
92 |
// [p1, p2] is the segment we're trying to divide |
10199 | 93 |
p1:= pa.ar[si]; |
94 |
p2:= pa.ar[si + 1]; |
|
95 |
||
10206 | 96 |
if p2.x = NTPX then |
97 |
// it is segment from last to first point, so need to find first point |
|
98 |
begin |
|
99 |
i:= si - 2; |
|
100 |
while (i >= 0) and (pa.ar[i].x <> NTPX) do |
|
101 |
dec(i); |
|
102 |
p2:= pa.ar[i + 1] |
|
103 |
end; |
|
104 |
||
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
105 |
// perpendicular vector |
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
106 |
a:= p2.y - p1.y; |
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
107 |
b:= p1.x - p2.x; |
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
108 |
dab:= DistanceI(a, b).Round; |
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
109 |
|
10206 | 110 |
// its middle point |
111 |
mp.x:= (p1.x + p2.x) div 2; |
|
112 |
mp.y:= (p1.y + p2.y) div 2; |
|
113 |
||
114 |
// don't process too short segments or those which are too close to map borders |
|
115 |
if (p1.x = NTPX) |
|
10510 | 116 |
or (dab < minDistance * 3) |
14303
6015b74eea55
overall, using LongInt for leftX/rightX results in fewer casts, since most comparisons are against ints.
nemo
parents:
14121
diff
changeset
|
117 |
or (mp.x < leftX + mapBorderMargin) |
6015b74eea55
overall, using LongInt for leftX/rightX results in fewer casts, since most comparisons are against ints.
nemo
parents:
14121
diff
changeset
|
118 |
or (mp.x > rightX - mapBorderMargin) |
14307 | 119 |
or (mp.y < topY + mapBorderMargin) |
10209 | 120 |
or (mp.y > LongInt(LAND_HEIGHT) - mapBorderMargin) |
10206 | 121 |
then |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
122 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
123 |
newPoint:= p1; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
124 |
exit; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
125 |
end; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
126 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
127 |
// find distances to map borders |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
128 |
if a <> 0 then |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
129 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
130 |
// left border |
14303
6015b74eea55
overall, using LongInt for leftX/rightX results in fewer casts, since most comparisons are against ints.
nemo
parents:
14121
diff
changeset
|
131 |
iy:= (leftX + mapBorderMargin - mp.x) * b div a + mp.y; |
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
132 |
d:= DistanceI(mp.x - leftX - mapBorderMargin, mp.y - iy).Round; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
133 |
t1:= a * (mp.x - mapBorderMargin) + b * (mp.y - iy); |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
134 |
if t1 > 0 then distL:= d else distR:= d; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
135 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
136 |
// right border |
14303
6015b74eea55
overall, using LongInt for leftX/rightX results in fewer casts, since most comparisons are against ints.
nemo
parents:
14121
diff
changeset
|
137 |
iy:= (rightX - mapBorderMargin - mp.x) * b div a + mp.y; |
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
138 |
d:= DistanceI(mp.x - rightX + mapBorderMargin, mp.y - iy).Round; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
139 |
if t1 > 0 then distR:= d else distL:= d; |
10478
12662c21e0e9
Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents:
10477
diff
changeset
|
140 |
end else |
12662c21e0e9
Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents:
10477
diff
changeset
|
141 |
begin |
12662c21e0e9
Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents:
10477
diff
changeset
|
142 |
distL:= LAND_WIDTH + LAND_HEIGHT; |
12662c21e0e9
Fix use of uninitialized variables (fixes straight lines in maze gen aswell)
unc0rr
parents:
10477
diff
changeset
|
143 |
distR:= distL; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
144 |
end; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
145 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
146 |
if b <> 0 then |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
147 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
148 |
// top border |
14307 | 149 |
ix:= (topY + mapBorderMargin - mp.y) * a div b + mp.x; |
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
150 |
d:= DistanceI(mp.y - topY - mapBorderMargin, mp.x - ix).Round; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
151 |
t2:= b * (mp.y - mapBorderMargin) + a * (mp.x - ix); |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
152 |
if t2 > 0 then distL:= min(d, distL) else distR:= min(d, distR); |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
153 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
154 |
// bottom border |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
155 |
ix:= (LAND_HEIGHT - mapBorderMargin - mp.y) * a div b + mp.x; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
156 |
d:= DistanceI(mp.y - LAND_HEIGHT + mapBorderMargin, mp.x - ix).Round; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
157 |
if t2 > 0 then distR:= min(d, distR) else distL:= min(d, distL); |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
158 |
end; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
159 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
160 |
// now go through all other segments |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
161 |
fp:= pa.ar[0]; |
10564
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10562
diff
changeset
|
162 |
for i:= 0 to LongInt(pa.Count) - 2 do |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
163 |
if pa.ar[i].x = NTPX then |
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
164 |
fp:= pa.ar[i + 1] |
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
165 |
else if (i <> si) then |
10199 | 166 |
begin |
14121 | 167 |
p4:= pa.ar[i + 1]; |
168 |
if p4.x = NTPX then |
|
169 |
p4:= fp; |
|
10206 | 170 |
|
10199 | 171 |
// check if it intersects |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
172 |
t1:= (mp.x - pa.ar[i].x) * b - a * (mp.y - pa.ar[i].y); |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
173 |
t2:= (mp.x - p4.x) * b - a * (mp.y - p4.y); |
10199 | 174 |
|
175 |
if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows |
|
176 |
begin |
|
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
177 |
p:= p4.x - pa.ar[i].x; |
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
178 |
q:= p4.y - pa.ar[i].y; |
10199 | 179 |
aqpb:= a * q - p * b; |
180 |
||
181 |
if (aqpb <> 0) then |
|
182 |
begin |
|
183 |
// (ix; iy) is intersection point |
|
10494 | 184 |
iy:= (((Int64(pa.ar[i].x) - mp.x) * b + Int64(mp.y) * a) * q - Int64(pa.ar[i].y) * p * b) div aqpb; |
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
185 |
if abs(b) > abs(q) then |
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
186 |
ix:= (iy - mp.y) * a div b + mp.x |
10199 | 187 |
else |
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
188 |
ix:= (iy - pa.ar[i].y) * p div q + pa.ar[i].x; |
10199 | 189 |
|
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
190 |
d:= DistanceI(mp.y - iy, mp.x - ix).Round; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
191 |
t1:= b * (mp.y - iy) + a * (mp.x - ix); |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
192 |
if t1 > 0 then distL:= min(d, distL) else distR:= min(d, distR); |
10199 | 193 |
end; |
194 |
end; |
|
195 |
end; |
|
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
196 |
|
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
197 |
// go through all points, including fill points |
10564
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10562
diff
changeset
|
198 |
for i:= 0 to Pred(LongInt(pa.Count + fillPointsCount)) do |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
199 |
// if this point isn't on current segment |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
200 |
if (si <> i) and (i <> si + 1) and (pa.ar[i].x <> NTPX) then |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
201 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
202 |
// also check intersection with rays through pa.ar[i] if this point is good |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
203 |
t1:= (p1.x - pa.ar[i].x) * b - a * (p1.y - pa.ar[i].y); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
204 |
t2:= (p2.x - pa.ar[i].x) * b - a * (p2.y - pa.ar[i].y); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
205 |
if (t1 > 0) <> (t2 > 0) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
206 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
207 |
// ray from p1 |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
208 |
p:= pa.ar[i].x - p1.x; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
209 |
q:= pa.ar[i].y - p1.y; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
210 |
aqpb:= a * q - p * b; |
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
211 |
|
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
212 |
if (aqpb <> 0) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
213 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
214 |
// (ix; iy) is intersection point |
10494 | 215 |
iy:= (((Int64(p1.x) - mp.x) * b + Int64(mp.y) * a) * q - Int64(p1.y) * p * b) div aqpb; |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
216 |
if abs(b) > abs(q) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
217 |
ix:= (iy - mp.y) * a div b + mp.x |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
218 |
else |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
219 |
ix:= (iy - p1.y) * p div q + p1.x; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
220 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
221 |
d:= DistanceI(mp.y - iy, mp.x - ix).Round; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
222 |
t1:= b * (mp.y - iy) + a * (mp.x - ix); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
223 |
if t1 > 0 then distL:= min(d, distL) else distR:= min(d, distR); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
224 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
225 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
226 |
// and ray from p2 |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
227 |
p:= pa.ar[i].x - p2.x; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
228 |
q:= pa.ar[i].y - p2.y; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
229 |
aqpb:= a * q - p * b; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
230 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
231 |
if (aqpb <> 0) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
232 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
233 |
// (ix; iy) is intersection point |
10494 | 234 |
iy:= (((Int64(p2.x) - mp.x) * b + Int64(mp.y) * a) * q - Int64(p2.y) * p * b) div aqpb; |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
235 |
if abs(b) > abs(q) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
236 |
ix:= (iy - mp.y) * a div b + mp.x |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
237 |
else |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
238 |
ix:= (iy - p2.y) * p div q + p2.x; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
239 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
240 |
d:= DistanceI(mp.y - iy, mp.x - ix).Round; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
241 |
t2:= b * (mp.y - iy) + a * (mp.x - ix); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
242 |
if t2 > 0 then distL:= min(d, distL) else distR:= min(d, distR); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
243 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
244 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
245 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
246 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
247 |
// don't move new point for more than length of initial segment |
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
248 |
// adjust/parametrize for more flat surfaces (try values 3/4, 1/2 of dab, or even 1/4) |
10499 | 249 |
d:= dab * 100 div dabDiv; |
10495
6d61b44a5652
- Comment out getrandom in addgear for hedgehog, causes preview-game desync
unc0rr
parents:
10494
diff
changeset
|
250 |
//d:= dab * (1 + abs(cFeatureSize - 8)) div 6; |
6d61b44a5652
- Comment out getrandom in addgear for hedgehog, causes preview-game desync
unc0rr
parents:
10494
diff
changeset
|
251 |
//d:= dab * (14 + cFeatureSize) div 20; |
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
252 |
if distL > d then distL:= d; |
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
253 |
if distR > d then distR:= d; |
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
254 |
|
10204 | 255 |
if distR + distL < minDistance * 2 + 10 then |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
256 |
begin |
10225 | 257 |
// limits are too narrow, just divide |
258 |
newPoint.x:= mp.x; |
|
259 |
newPoint.y:= mp.y; |
|
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
260 |
end |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
261 |
else |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
262 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
263 |
// select distance within [-distL; distR] |
10209 | 264 |
d:= -distL + minDistance + LongInt(GetRandom(distR + distL - minDistance * 2)); |
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
265 |
//d:= distR - minDistance; |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
266 |
//d:= - distL + minDistance; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
267 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
268 |
// calculate new point |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
269 |
newPoint.x:= mp.x + a * d div dab; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
270 |
newPoint.y:= mp.y + b * d div dab; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
271 |
end; |
10199 | 272 |
end; |
273 |
||
10209 | 274 |
procedure DivideEdges(fillPointsCount: LongWord; var pa: TPixAr); |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
275 |
var i, t: LongInt; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
276 |
newPoint: TPoint; |
10199 | 277 |
begin |
10209 | 278 |
newPoint.x:= 0; |
279 |
newPoint.y:= 0; |
|
10199 | 280 |
i:= 0; |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
281 |
|
10564
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10562
diff
changeset
|
282 |
while i < LongInt(pa.Count) - 1 do |
10199 | 283 |
begin |
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
284 |
FindPoint(i, fillPointsCount, newPoint, pa); |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
285 |
|
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
286 |
if (newPoint.x <> pa.ar[i].x) or (newPoint.y <> pa.ar[i].y) then |
10199 | 287 |
begin |
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
288 |
// point found, free a slot for it in array, don't forget to move appended fill points |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
289 |
for t:= pa.Count + fillPointsCount downto i + 2 do |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
290 |
pa.ar[t]:= pa.ar[t - 1]; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
291 |
inc(pa.Count); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
292 |
pa.ar[i + 1]:= newPoint; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
293 |
inc(i) |
10199 | 294 |
end; |
295 |
inc(i) |
|
296 |
end; |
|
297 |
end; |
|
298 |
||
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
299 |
procedure Distort2(var Template: TEdgeTemplate; fps: PPointArray; var pa: TPixAr); |
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
300 |
var i: Longword; |
10199 | 301 |
begin |
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
302 |
// append fill points to ensure distortion won't move them to other side of segment |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
303 |
for i:= 0 to pred(Template.FillPointsCount) do |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
304 |
begin |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
305 |
pa.ar[pa.Count + i].x:= fps^[i].x; |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
306 |
pa.ar[pa.Count + i].y:= fps^[i].y; |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
307 |
end; |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
308 |
|
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
309 |
// divide while it divides |
10203 | 310 |
repeat |
311 |
i:= pa.Count; |
|
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
312 |
DivideEdges(Template.FillPointsCount, pa) |
10203 | 313 |
until i = pa.Count; |
10201
9bee9541edf1
Fix detection of intersections, still need to check if passing any point in move, but result is already okayish
unc0rr
parents:
10200
diff
changeset
|
314 |
|
11175
e1a098f950a9
- Attempt to fix a crash while selecting 'Random' for map generation
antonc27 <antonc27@mail.ru>
parents:
10564
diff
changeset
|
315 |
{$IFDEF IPHONEOS} |
e1a098f950a9
- Attempt to fix a crash while selecting 'Random' for map generation
antonc27 <antonc27@mail.ru>
parents:
10564
diff
changeset
|
316 |
if GameType <> gmtLandPreview then |
e1a098f950a9
- Attempt to fix a crash while selecting 'Random' for map generation
antonc27 <antonc27@mail.ru>
parents:
10564
diff
changeset
|
317 |
{$ENDIF} |
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
318 |
// make it smooth |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
319 |
BezierizeEdge(pa, _0_2); |
10199 | 320 |
end; |
321 |
||
322 |
||
10198 | 323 |
procedure GenTemplated(var Template: TEdgeTemplate); |
324 |
var pa: TPixAr; |
|
325 |
i: Longword; |
|
326 |
y, x: Longword; |
|
327 |
fps: TPointArray; |
|
328 |
begin |
|
329 |
fps:=Template.FillPoints^; |
|
330 |
ResizeLand(Template.TemplateWidth, Template.TemplateHeight); |
|
331 |
for y:= 0 to LAND_HEIGHT - 1 do |
|
332 |
for x:= 0 to LAND_WIDTH - 1 do |
|
333 |
Land[y, x]:= lfBasic; |
|
10510 | 334 |
|
10495
6d61b44a5652
- Comment out getrandom in addgear for hedgehog, causes preview-game desync
unc0rr
parents:
10494
diff
changeset
|
335 |
minDistance:= sqr(cFeatureSize) div 8 + 10; |
10499 | 336 |
//dabDiv:= getRandom(41)+60; |
337 |
//dabDiv:= getRandom(31)+70; |
|
338 |
dabDiv:= getRandom(21)+100; |
|
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
339 |
MaxHedgehogs:= Template.MaxHedgehogs; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
340 |
hasGirders:= Template.hasGirders; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
341 |
playHeight:= Template.TemplateHeight; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
342 |
playWidth:= Template.TemplateWidth; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
343 |
leftX:= (LAND_WIDTH - playWidth) div 2; |
10209 | 344 |
rightX:= Pred(leftX + playWidth); |
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
345 |
topY:= LAND_HEIGHT - playHeight; |
10510 | 346 |
|
10198 | 347 |
{$HINTS OFF} |
348 |
SetPoints(Template, pa, @fps); |
|
349 |
{$HINTS ON} |
|
350 |
||
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
351 |
Distort2(Template, @fps, pa); |
10198 | 352 |
|
353 |
DrawEdge(pa, 0); |
|
354 |
||
355 |
with Template do |
|
356 |
for i:= 0 to pred(FillPointsCount) do |
|
357 |
with fps[i] do |
|
358 |
FillLand(x, y, 0, 0); |
|
359 |
||
360 |
DrawEdge(pa, lfBasic); |
|
361 |
||
362 |
// HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ? |
|
363 |
if (cTemplateFilter = 4) |
|
364 |
or (Template.canInvert and (getrandom(2) = 0)) |
|
365 |
or (not Template.canInvert and Template.isNegative) then |
|
366 |
begin |
|
367 |
hasBorder:= true; |
|
368 |
for y:= 0 to LAND_HEIGHT - 1 do |
|
369 |
for x:= 0 to LAND_WIDTH - 1 do |
|
14306
a7810074c20d
these need casting too, but overall switching type is a win.
nemo
parents:
14303
diff
changeset
|
370 |
if (y < LongWord(topY)) or (x < LongWord(leftX)) or (x > LongWord(rightX)) then |
10198 | 371 |
Land[y, x]:= 0 |
372 |
else |
|
373 |
begin |
|
374 |
if Land[y, x] = 0 then |
|
375 |
Land[y, x]:= lfBasic |
|
376 |
else if Land[y, x] = lfBasic then |
|
377 |
Land[y, x]:= 0; |
|
378 |
end; |
|
379 |
end; |
|
380 |
end; |
|
381 |
||
382 |
||
383 |
end. |