-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildMazePaths.go
188 lines (165 loc) · 4.57 KB
/
buildMazePaths.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
package procedural
import (
"fmt"
"math/rand"
"sort"
"time"
"github.com/griffithsh/squads/geom"
)
func buildMazePaths(seed int64, level int) (Paths, error) {
prng := rand.New(rand.NewSource(seed))
// TODO: Tunables - passed level should drive these values.
seeds := 10
goalComplexity := 75
expandMin := 3
expandMax := 12
start := time.Now()
availableSeeds := geom.Key{M: 0, N: 0}.ExpandBy(expandMin, expandMax)
sort.Slice(availableSeeds, func(i, j int) bool {
if availableSeeds[i].M == availableSeeds[j].M {
return availableSeeds[i].N < availableSeeds[j].N
}
return availableSeeds[i].M < availableSeeds[j].M
})
seedsSet := map[geom.Key]struct{}{}
for len(seedsSet) < seeds {
index := rand.Intn(len(availableSeeds))
seedsSet[availableSeeds[index]] = struct{}{}
}
fmt.Printf("Seed: %v (%v)\n", prng, seedsSet)
// toHandle is the queue of hexes we still need to connect to stuff.
toHandle := shuffledGeomKeys(prng, seedsSet)
placed := map[geom.Key]Placement{}
for len(toHandle) > 0 {
curr := toHandle[0]
toHandle = toHandle[1:]
if _, ok := placed[curr]; ok {
fmt.Printf("\nskipping %v, already placed\n", curr)
continue
}
fmt.Printf("\ndealing with %v -- complexity is %v\n", curr, len(placed))
toPlace := Placement{
Connections: map[geom.DirectionType]struct{}{},
}
adj := shuffledGeomKeys(prng, curr.Neighbors())
for _, k := range adj {
dir := curr.Neighbors()[k]
// Is this adjacent hex already placed?
neighbor, isPlaced := placed[k]
if isPlaced {
// Does it already expect a connection from curr`?
if _, ok := neighbor.Connections[geom.Opposite[dir]]; ok {
toPlace.Connections[dir] = struct{}{}
fmt.Printf("\tneighbor %v already expects a connection\n", k)
}
continue
}
// Bail out logic .....
segments := pathSegments(placed)
if len(segments) > 0 && len(segments[0]) > goalComplexity {
continue
}
if rollConnection(prng, len(toPlace.Connections), len(placed), goalComplexity) {
toPlace.Connections[dir] = struct{}{}
fmt.Printf("\tAdding %v\n", curr.Adjacent()[dir])
toHandle = append(toHandle, curr.Adjacent()[dir])
}
}
if len(toPlace.Connections) > 0 {
fmt.Printf("\tadded with connections %v\n", keysOf(toPlace.Connections))
// Done, chuck it into the placed set.
placed[curr] = toPlace
} else {
fmt.Printf("\tAborting %v, not connected\n", curr)
}
}
segments := pathSegments(placed)
if len(segments) > 1 {
fmt.Printf("dropping %v vestiges:", len(segments)-1)
for i := 1; i < len(segments); i++ {
fmt.Printf(" %v", len(segments[i]))
for _, key := range segments[i] {
delete(placed, key)
}
}
fmt.Println()
}
duration := time.Since(start)
fmt.Printf("complexity: %v of %v (%v)\n", len(placed), goalComplexity, duration)
result := Paths{
Algorithm: "maze-paths",
Seed: seed,
Nodes: placed,
}
for _, k := range shuffledGeomKeys(prng, placed) {
if len(placed[k].Connections) == 1 {
// found a contender
if geom.Equal(&result.Start, &geom.Key{}) {
result.Start = k
continue
} else if geom.Equal(&result.Goal, &geom.Key{}) {
result.Goal = k
break
}
}
}
return result, nil
}
func rollConnection(prng *rand.Rand, numConnections int, currComplexity, goalComplexity int) bool {
completion := float64(currComplexity) / float64(goalComplexity)
completion -= 0.8
if completion < 0 {
completion = 0
}
completion *= 5
reduction := 1.0 - completion
baseChance := 0.0
switch numConnections {
case 0:
baseChance = 0.95
case 1:
baseChance = 0.2
case 2:
baseChance = 0.08
case 3:
baseChance = 0.03
case 4:
baseChance = 0.01
default: // 5
baseChance = 0.005
}
fmt.Printf("\treduction:%v\n", reduction)
return prng.Float64() < baseChance //*reduction
}
func pathSegments(placed map[geom.Key]Placement) [][]geom.Key {
// queue of keys that have not had their connections mapped yet.
queue := map[geom.Key]struct{}{}
for k := range placed {
queue[k] = struct{}{}
}
segments := [][]geom.Key{}
for len(queue) > 0 {
curr := deterministicKeyFrom(queue)
connected := []geom.Key{}
segment := []geom.Key{}
for {
if _, ok := queue[curr]; ok {
segment = append(segment, curr)
delete(queue, curr)
for dir := range placed[curr].Connections {
neighbor := curr.Adjacent()[dir]
connected = append(connected, neighbor)
}
}
if len(connected) == 0 {
break
}
curr, connected = connected[0], connected[1:]
}
segments = append(segments, segment)
}
sort.Slice(segments, func(i, j int) bool {
return len(segments[i]) > len(segments[j])
})
return segments
}