author | unc0rr |
Sun, 24 Aug 2014 23:53:33 +0400 | |
changeset 10399 | 22fbc79acd5b |
parent 10387 | cb17b79844b5 |
child 10472 | 8d04cebedb16 |
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 |
|
9 |
implementation |
|
10387 | 10 |
uses uVariables, uConsts, uFloat, uLandUtils, uRandom, SDLh, math; |
10198 | 11 |
|
12 |
||
13 |
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray); |
|
14 |
var i: LongInt; |
|
15 |
begin |
|
16 |
with Template do |
|
17 |
begin |
|
18 |
pa.Count:= BasePointsCount; |
|
19 |
for i:= 0 to pred(pa.Count) do |
|
20 |
begin |
|
21 |
pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w)); |
|
22 |
if pa.ar[i].x <> NTPX then |
|
10199 | 23 |
pa.ar[i].x:= pa.ar[i].x + ((LAND_WIDTH - Template.TemplateWidth) div 2); |
10198 | 24 |
pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) + LAND_HEIGHT - LongInt(Template.TemplateHeight) |
25 |
end; |
|
26 |
||
27 |
if canMirror then |
|
28 |
if getrandom(2) = 0 then |
|
29 |
begin |
|
30 |
for i:= 0 to pred(BasePointsCount) do |
|
31 |
if pa.ar[i].x <> NTPX then |
|
32 |
pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x; |
|
33 |
for i:= 0 to pred(FillPointsCount) do |
|
34 |
fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x; |
|
35 |
end; |
|
36 |
||
37 |
(* Experiment in making this option more useful |
|
38 |
if ((not isNegative) and (cTemplateFilter = 4)) or |
|
39 |
(canFlip and (getrandom(2) = 0)) then |
|
40 |
begin |
|
41 |
for i:= 0 to pred(BasePointsCount) do |
|
42 |
begin |
|
43 |
pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y + (LAND_HEIGHT - TemplateHeight) * 2; |
|
44 |
if pa.ar[i].y > LAND_HEIGHT - 1 then |
|
45 |
pa.ar[i].y:= LAND_HEIGHT - 1; |
|
46 |
end; |
|
47 |
for i:= 0 to pred(FillPointsCount) do |
|
48 |
begin |
|
49 |
FillPoints^[i].y:= LAND_HEIGHT - 1 - FillPoints^[i].y + (LAND_HEIGHT - TemplateHeight) * 2; |
|
50 |
if FillPoints^[i].y > LAND_HEIGHT - 1 then |
|
51 |
FillPoints^[i].y:= LAND_HEIGHT - 1; |
|
52 |
end; |
|
53 |
end; |
|
54 |
end |
|
55 |
*) |
|
56 |
// template recycling. Pull these off the floor a bit |
|
57 |
if (not isNegative) and (cTemplateFilter = 4) then |
|
58 |
begin |
|
59 |
for i:= 0 to pred(BasePointsCount) do |
|
60 |
begin |
|
61 |
dec(pa.ar[i].y, 100); |
|
62 |
if pa.ar[i].y < 0 then |
|
63 |
pa.ar[i].y:= 0; |
|
64 |
end; |
|
65 |
for i:= 0 to pred(FillPointsCount) do |
|
66 |
begin |
|
67 |
dec(fps^[i].y, 100); |
|
68 |
if fps^[i].y < 0 then |
|
69 |
fps^[i].y:= 0; |
|
70 |
end; |
|
71 |
end; |
|
72 |
||
73 |
if (canFlip and (getrandom(2) = 0)) then |
|
74 |
begin |
|
75 |
for i:= 0 to pred(BasePointsCount) do |
|
76 |
pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y; |
|
77 |
for i:= 0 to pred(FillPointsCount) do |
|
78 |
fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y; |
|
79 |
end; |
|
80 |
end |
|
81 |
end; |
|
10199 | 82 |
|
83 |
||
84 |
procedure Distort1(var Template: TEdgeTemplate; var pa: TPixAr); |
|
85 |
var i: Longword; |
|
86 |
begin |
|
87 |
for i:= 1 to Template.BezierizeCount do |
|
88 |
begin |
|
89 |
BezierizeEdge(pa, _0_5); |
|
90 |
RandomizePoints(pa); |
|
91 |
RandomizePoints(pa) |
|
92 |
end; |
|
93 |
for i:= 1 to Template.RandPassesCount do |
|
94 |
RandomizePoints(pa); |
|
95 |
BezierizeEdge(pa, _0_1); |
|
96 |
end; |
|
97 |
||
10209 | 98 |
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
|
99 |
const mapBorderMargin = 40; |
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
100 |
minDistance = 32; // adjust/parametrize this for different details size |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
101 |
var p1, p2, p4, fp, mp: TPoint; |
10226 | 102 |
i, t1, t2, iy, ix, aqpb: LongInt; |
103 |
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
|
104 |
dab, d, distL, distR: LongInt; |
10199 | 105 |
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
|
106 |
// [p1, p2] is the segment we're trying to divide |
10199 | 107 |
p1:= pa.ar[si]; |
108 |
p2:= pa.ar[si + 1]; |
|
109 |
||
10206 | 110 |
if p2.x = NTPX then |
111 |
// it is segment from last to first point, so need to find first point |
|
112 |
begin |
|
113 |
i:= si - 2; |
|
114 |
while (i >= 0) and (pa.ar[i].x <> NTPX) do |
|
115 |
dec(i); |
|
116 |
p2:= pa.ar[i + 1] |
|
117 |
end; |
|
118 |
||
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
|
119 |
// 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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
|
10206 | 124 |
// its middle point |
125 |
mp.x:= (p1.x + p2.x) div 2; |
|
126 |
mp.y:= (p1.y + p2.y) div 2; |
|
127 |
||
128 |
// don't process too short segments or those which are too close to map borders |
|
129 |
if (p1.x = NTPX) |
|
130 |
or (dab < minDistance * 3) |
|
10209 | 131 |
or (mp.x < LongInt(leftX) + mapBorderMargin) |
132 |
or (mp.x > LongInt(rightX) - mapBorderMargin) |
|
133 |
or (mp.y < LongInt(topY) + mapBorderMargin) |
|
134 |
or (mp.y > LongInt(LAND_HEIGHT) - mapBorderMargin) |
|
10206 | 135 |
then |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
136 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
137 |
newPoint:= p1; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
138 |
exit; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
139 |
end; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
140 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
141 |
// find distances to map borders |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
142 |
if a <> 0 then |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
143 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
144 |
// left border |
10209 | 145 |
iy:= (LongInt(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
|
146 |
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
|
147 |
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
|
148 |
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
|
149 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
150 |
// right border |
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
151 |
iy:= (rightX - mapBorderMargin - mp.x) * b div a + mp.y; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
152 |
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
|
153 |
if t1 > 0 then distR:= d else distL:= d; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
154 |
end; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
155 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
156 |
if b <> 0 then |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
157 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
158 |
// top border |
10209 | 159 |
ix:= (LongInt(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
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
164 |
// bottom border |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
end; |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
169 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
170 |
// 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
|
171 |
fp:= pa.ar[0]; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
172 |
for i:= 0 to pa.Count - 2 do |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
173 |
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
|
174 |
fp:= pa.ar[i + 1] |
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
175 |
else if (i <> si) then |
10199 | 176 |
begin |
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
177 |
p4:= pa.ar[i + 1]; |
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
178 |
if p4.x = NTPX then |
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
179 |
p4:= fp; |
10206 | 180 |
|
10199 | 181 |
// check if it intersects |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
182 |
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
|
183 |
t2:= (mp.x - p4.x) * b - a * (mp.y - p4.y); |
10199 | 184 |
|
185 |
if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows |
|
186 |
begin |
|
10205
fc99e124ba4d
Prevent intersections with segment from last to first point, adjust size of details
unc0rr
parents:
10204
diff
changeset
|
187 |
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
|
188 |
q:= p4.y - pa.ar[i].y; |
10199 | 189 |
aqpb:= a * q - p * b; |
190 |
||
191 |
if (aqpb <> 0) then |
|
192 |
begin |
|
193 |
// (ix; iy) is intersection point |
|
10226 | 194 |
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
|
195 |
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
|
196 |
ix:= (iy - mp.y) * a div b + mp.x |
10199 | 197 |
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
|
198 |
ix:= (iy - pa.ar[i].y) * p div q + pa.ar[i].x; |
10199 | 199 |
|
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
200 |
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
|
201 |
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
|
202 |
if t1 > 0 then distL:= min(d, distL) else distR:= min(d, distR); |
10199 | 203 |
end; |
204 |
end; |
|
205 |
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
|
206 |
|
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
207 |
// go through all points, including fill points |
10209 | 208 |
for i:= 0 to Pred(pa.Count + fillPointsCount) do |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
209 |
// 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
|
210 |
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
|
211 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
212 |
// 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
|
213 |
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
|
214 |
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
|
215 |
if (t1 > 0) <> (t2 > 0) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
216 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
217 |
// ray from p1 |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
218 |
p:= pa.ar[i].x - p1.x; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
219 |
q:= pa.ar[i].y - p1.y; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
220 |
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
|
221 |
|
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
222 |
if (aqpb <> 0) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
223 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
224 |
// (ix; iy) is intersection point |
10226 | 225 |
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
|
226 |
if abs(b) > abs(q) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
227 |
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
|
228 |
else |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
229 |
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
|
230 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
231 |
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
|
232 |
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
|
233 |
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
|
234 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
235 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
236 |
// and ray from p2 |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
237 |
p:= pa.ar[i].x - p2.x; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
238 |
q:= pa.ar[i].y - p2.y; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
239 |
aqpb:= a * q - p * b; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
240 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
241 |
if (aqpb <> 0) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
242 |
begin |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
243 |
// (ix; iy) is intersection point |
10226 | 244 |
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
|
245 |
if abs(b) > abs(q) then |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
246 |
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
|
247 |
else |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
248 |
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
|
249 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
254 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
255 |
end; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
256 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
257 |
// 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
|
258 |
// adjust/parametrize for more flat surfaces (try values 3/4, 1/2 of dab, or even 1/4) |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
259 |
d:= dab; |
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
|
260 |
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
|
261 |
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
|
262 |
|
10204 | 263 |
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
|
264 |
begin |
10225 | 265 |
// limits are too narrow, just divide |
266 |
newPoint.x:= mp.x; |
|
267 |
newPoint.y:= mp.y; |
|
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
268 |
end |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
269 |
else |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
270 |
begin |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
271 |
// select distance within [-distL; distR] |
10209 | 272 |
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
|
273 |
//d:= distR - minDistance; |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
274 |
//d:= - distL + minDistance; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
275 |
|
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
276 |
// calculate new point |
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
277 |
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
|
278 |
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
|
279 |
end; |
10199 | 280 |
end; |
281 |
||
10209 | 282 |
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
|
283 |
var i, t: LongInt; |
10200
edc2fe0ca03f
More math, implementation is nearly complete, just still have an issue to resolve
unc0rr
parents:
10199
diff
changeset
|
284 |
newPoint: TPoint; |
10199 | 285 |
begin |
10209 | 286 |
newPoint.x:= 0; |
287 |
newPoint.y:= 0; |
|
10199 | 288 |
i:= 0; |
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
289 |
|
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
290 |
while i < pa.Count - 1 do |
10199 | 291 |
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
|
292 |
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
|
293 |
|
10202
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
294 |
if (newPoint.x <> pa.ar[i].x) or (newPoint.y <> pa.ar[i].y) then |
10199 | 295 |
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
|
296 |
// 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
|
297 |
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
|
298 |
pa.ar[t]:= pa.ar[t - 1]; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
299 |
inc(pa.Count); |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
300 |
pa.ar[i + 1]:= newPoint; |
f7c8cb11a70e
No self intersections, except for weirdness between first and last point
unc0rr
parents:
10201
diff
changeset
|
301 |
inc(i) |
10199 | 302 |
end; |
303 |
inc(i) |
|
304 |
end; |
|
305 |
end; |
|
306 |
||
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
307 |
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
|
308 |
var i: Longword; |
10199 | 309 |
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
|
310 |
// 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
|
311 |
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
|
312 |
begin |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
313 |
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
|
314 |
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
|
315 |
end; |
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
316 |
|
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
317 |
// divide while it divides |
10203 | 318 |
repeat |
319 |
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
|
320 |
DivideEdges(Template.FillPointsCount, pa) |
10203 | 321 |
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
|
322 |
|
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
323 |
// 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
|
324 |
BezierizeEdge(pa, _0_2); |
10199 | 325 |
end; |
326 |
||
327 |
||
10198 | 328 |
procedure GenTemplated(var Template: TEdgeTemplate); |
329 |
var pa: TPixAr; |
|
330 |
i: Longword; |
|
331 |
y, x: Longword; |
|
332 |
fps: TPointArray; |
|
333 |
begin |
|
334 |
fps:=Template.FillPoints^; |
|
335 |
ResizeLand(Template.TemplateWidth, Template.TemplateHeight); |
|
336 |
for y:= 0 to LAND_HEIGHT - 1 do |
|
337 |
for x:= 0 to LAND_WIDTH - 1 do |
|
338 |
Land[y, x]:= lfBasic; |
|
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
339 |
|
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
340 |
MaxHedgehogs:= Template.MaxHedgehogs; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
341 |
hasGirders:= Template.hasGirders; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
342 |
playHeight:= Template.TemplateHeight; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
343 |
playWidth:= Template.TemplateWidth; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
344 |
leftX:= (LAND_WIDTH - playWidth) div 2; |
10209 | 345 |
rightX:= Pred(leftX + playWidth); |
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
346 |
topY:= LAND_HEIGHT - playHeight; |
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
10207
diff
changeset
|
347 |
|
10198 | 348 |
{$HINTS OFF} |
349 |
SetPoints(Template, pa, @fps); |
|
350 |
{$HINTS ON} |
|
351 |
||
10207
9dd3a44805a1
- Make sure distortion doesn't move fill point to other side of segment, this prevents corrupted maps
unc0rr
parents:
10206
diff
changeset
|
352 |
Distort2(Template, @fps, pa); |
10198 | 353 |
|
354 |
DrawEdge(pa, 0); |
|
355 |
||
356 |
with Template do |
|
357 |
for i:= 0 to pred(FillPointsCount) do |
|
358 |
with fps[i] do |
|
359 |
FillLand(x, y, 0, 0); |
|
360 |
||
361 |
DrawEdge(pa, lfBasic); |
|
362 |
||
363 |
// HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ? |
|
364 |
if (cTemplateFilter = 4) |
|
365 |
or (Template.canInvert and (getrandom(2) = 0)) |
|
366 |
or (not Template.canInvert and Template.isNegative) then |
|
367 |
begin |
|
368 |
hasBorder:= true; |
|
369 |
for y:= 0 to LAND_HEIGHT - 1 do |
|
370 |
for x:= 0 to LAND_WIDTH - 1 do |
|
371 |
if (y < topY) or (x < leftX) or (x > rightX) then |
|
372 |
Land[y, x]:= 0 |
|
373 |
else |
|
374 |
begin |
|
375 |
if Land[y, x] = 0 then |
|
376 |
Land[y, x]:= lfBasic |
|
377 |
else if Land[y, x] = lfBasic then |
|
378 |
Land[y, x]:= 0; |
|
379 |
end; |
|
380 |
end; |
|
381 |
end; |
|
382 |
||
383 |
||
384 |
end. |