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