-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildRingPaths.go
235 lines (200 loc) · 5.26 KB
/
buildRingPaths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package procedural
import (
"fmt"
"math"
"math/rand"
"github.com/griffithsh/squads/geom"
)
// meander from a start to a goal, generating a less-than-ideal path.
func meander(prng *rand.Rand, start, goal geom.Key, avoid []geom.Key) []geom.Key {
f := geom.NewField(3, 5, 12)
current := start
banned := map[geom.Key]struct{}{}
for _, k := range avoid {
banned[k] = struct{}{}
}
// Yo if we hit a length longer than this something is real wrong.
max := start.HexesFrom(goal) * 5
result := []geom.Key{start}
for {
n := current.Neighbors()
// Check for goal in site.
if _, ok := n[goal]; ok {
// We found the goal!
return append(result, goal)
}
if len(result) > max {
fmt.Printf("\tgave up looking for path from %v to %v after %d steps %v\n", start, goal, max, result)
return []geom.Key{}
}
// Remove any banned keys from neighbors.
for k := range n {
if _, ok := banned[k]; ok {
delete(n, k)
}
}
if len(n) == 0 {
fmt.Printf("\tmeander from %v to %v: no available neighbors for %v\n", start, goal, current)
return []geom.Key{}
}
// Score neighbors.
type score struct {
k geom.Key
val float64
}
scored := []score{}
// distance from current
currentDistance := f.DistanceBetween(current, goal)
for _, k := range shuffledGeomKeys(prng, n) {
nd := f.DistanceBetween(k, goal)
s := score{k: k, val: nd - currentDistance}
s.val *= -1
if s.val < 0 {
s.val = 0.1
}
scored = append(scored, s)
}
// roll for next
sum := 0.0
for _, s := range scored {
sum += s.val
}
roll := prng.Float64() * sum
consumed := 0.0
for _, s := range scored {
if s.val+consumed >= roll {
// Found it.
// append next to result and add it to banned
result = append(result, s.k)
banned[s.k] = struct{}{}
// assign new current
current = s.k
break
}
consumed += s.val
}
}
}
// buildRingPaths might implement lakes, islands with impassable interiors, or atolls.
func buildRingPaths(seed int64, level int) (Paths, error) {
// drive these by level in some way?
rotations := 10
minRing := 7
maxRing := 7
// pick N points on a ring, connect each to the next in turn, connecting the
// last to the first.
fmt.Printf("buildRingPaths(%d)\n", seed)
prng := rand.New(rand.NewSource(seed))
f := geom.NewField(3, 5, 12)
sin, cos := math.Sincos(math.Pi * 2 / float64(rotations))
result := Paths{
Algorithm: "ring-paths",
Seed: seed,
Nodes: map[geom.Key]Placement{},
}
contenders := geom.Key{}.ExpandBy(minRing, maxRing)
sortKeys(contenders)
i := DeterministicIndexOf(prng, contenders)
current := contenders[i]
result.Start = current
goalIndex := rotations/2 + prng.Intn((rotations/3)*2) - (rotations / 3)
toConnect := []struct{ a, b geom.Key }{}
for i = 0; i < rotations-1; i++ {
x, y := f.Ktow(current)
x2, y2 := x*cos-y*sin, x*sin+y*cos
next := f.Wtok(x2, y2)
// Blur next a bit.
next = shuffledGeomKeys(prng, next.Neighbors())[0]
toConnect = append(toConnect, struct{ a, b geom.Key }{a: current, b: next})
if goalIndex == i {
result.Goal = next
}
current = next
}
if geom.Equal(&result.Goal, &geom.Key{}) {
fmt.Printf("\tdid not assign goal from index %d\n", goalIndex)
}
toConnect = append(toConnect, struct{ a, b geom.Key }{
// Link last to first.
a: toConnect[len(toConnect)-1].b,
b: toConnect[0].a,
})
for _, c := range toConnect {
banned := keysOf(result.Nodes)
if geom.Equal(&c.a, &c.b) {
// nothing to connect!
fmt.Printf("\tnot connecting %v to itself\n", c.a)
continue
}
aPath := []geom.Key{}
for i := 2; i >= 0; i-- {
aPath = meander(prng, c.a, c.b, banned)
if len(aPath) > 0 {
break
}
fmt.Printf("\tmeander from %v to %v failed\n", c.a, c.b)
if i == 0 {
return Paths{}, fmt.Errorf("buildRingPaths: could not meander from %v to %v: exhausted retries", c.a, c.b)
}
}
if len(aPath) == 0 {
return Paths{}, fmt.Errorf("buildRingPaths: could not meander from %v to %v: exhausted retries", c.a, c.b)
}
fmt.Printf("\tmeandering from %v to %v: %v\n", c.a, c.b, aPath)
step := aPath[0]
for i := 1; i < len(aPath); i++ {
result.Connect(step, aPath[i])
step = aPath[i]
}
}
// Right, ring done, let's add tendrils for interest
for _, c := range toConnect {
avoid := keysOf(result.Nodes)
banned := map[geom.Key]struct{}{}
for _, k := range avoid {
banned[k] = struct{}{}
}
branch := c.a
for i := 0; i < 4; i++ {
type score struct {
k geom.Key
val float64
}
scored := []score{}
// distance from current
currentDistance := f.DistanceBetween(branch, geom.Key{})
n := branch.Neighbors()
for k := range n {
if _, ok := banned[k]; ok {
delete(n, k)
}
}
if len(n) == 0 {
break
}
for _, k := range shuffledGeomKeys(prng, n) {
nd := f.DistanceBetween(k, geom.Key{})
s := score{k: k, val: math.Abs(nd - currentDistance)}
scored = append(scored, s)
}
// roll for next
sum := 0.0
for _, s := range scored {
sum += s.val
}
roll := prng.Float64() * sum
consumed := 0.0
for _, s := range scored {
if s.val+consumed >= roll {
// Found it.
result.Connect(branch, s.k)
banned[s.k] = struct{}{}
branch = s.k
break
}
consumed += s.val
}
}
}
return result, nil
}