-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildVinePaths.go
173 lines (153 loc) · 4.02 KB
/
buildVinePaths.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
package procedural
import (
"fmt"
"math/rand"
"github.com/griffithsh/squads/geom"
)
// buildVinePaths generates long maps, as a single correct pathway with
// alternative dead-ending branches is generated trending in a single direction.
// It is intended to implement beaches.
func buildVinePaths(seed int64, level int) (Paths, error) {
prng := rand.New(rand.NewSource(seed))
fmt.Println("buildVinePaths")
directions := []geom.DirectionType{
geom.NE,
geom.SE,
geom.S,
geom.SW,
geom.NW,
geom.N,
}
// sunshine is the direction that vines grow in.
sunshine := directions[DeterministicIndexOf(prng, directions)]
fl := geom.Actualize(sunshine, geom.ForwardLeft)
fr := geom.Actualize(sunshine, geom.ForwardRight)
type tip struct {
remaining int
key geom.Key
}
type branchChance struct {
chance int
relDir geom.RelativeDirection
key geom.Key
}
// rollOnward randomly picks a direction from rolled sunshine direction
rollOnward := func(prng *rand.Rand) geom.DirectionType {
roll := prng.Intn(10)
if roll < 2 {
return fl
} else if roll < 4 {
return fr
} else {
return sunshine
}
}
filterTaken := func(chances []branchChance, result Paths, current geom.Key) []branchChance {
i := 0
l := len(chances)
for i < l {
rel := chances[i].relDir
dir := geom.Actualize(sunshine, rel)
chances[i].key = current.ToDirection(dir)
if _, ok := result.Nodes[chances[i].key]; ok {
chances = append(chances[:i], chances[i+1:]...)
l--
} else {
i++
}
}
return chances[:i]
}
randomFrom := func(prng *rand.Rand, chances []branchChance) (branchChance, []branchChance) {
sum := 0
for _, chance := range chances {
sum += chance.chance
}
roll := prng.Intn(sum)
i := 0
l := len(chances)
running := 0
for i < l {
if roll < running+chances[i].chance {
// got it!
break
} else {
running = running + chances[i].chance
i++
}
}
return chances[i], append(chances[0:i], chances[i+1:]...)
}
result := Paths{
Algorithm: "vine-paths",
Seed: seed,
Nodes: map[geom.Key]Placement{},
}
current := geom.Key{}
// start := current
tips := []tip{}
nextBranch := 2 + prng.Intn(4)
for i := 0; i < 21+prng.Intn(7); i++ {
nextBranch--
if nextBranch > 0 {
// grow the vine in the general direction of sunshine
chances := []branchChance{
{chance: 29, relDir: geom.ForwardLeft},
{chance: 17, relDir: geom.ForwardRight},
{chance: 60, relDir: geom.Forward},
}
chances = filterTaken(chances, result, current)
if len(chances) == 0 {
fmt.Printf("\tmain trunk is dead; all adjacent hexes are unavailable\n")
break
}
trunk, _ := randomFrom(prng, chances)
result.Connect(current, trunk.key)
current = trunk.key
continue
}
// this is a branching point! Will we succeed??? who knows
nextBranch = 2 + prng.Intn(4)
chances := []branchChance{
{chance: 10, relDir: geom.BackLeft},
{chance: 10, relDir: geom.BackRight},
{chance: 20, relDir: geom.ForwardLeft},
{chance: 20, relDir: geom.ForwardRight},
{chance: 50, relDir: geom.Forward},
}
// filter taken spots
chances = filterTaken(chances, result, current)
if len(chances) == 0 {
fmt.Printf("\tmain trunk is dead; all adjacent hexes are unavailable\n")
break
}
trunk, chances := randomFrom(prng, chances)
result.Connect(current, trunk.key)
if len(chances) > 0 {
branch, _ := randomFrom(prng, chances)
tips = append(tips, tip{
remaining: 2 + prng.Intn(4),
key: branch.key,
})
result.Connect(current, branch.key)
} else {
fmt.Printf("\tbranch from %v is dead; all adjacent hexes are unavailable", current)
}
result.Goal = trunk.key
}
for i, tip := range tips {
for tip.remaining > 0 {
tip.remaining--
onward := rollOnward(prng)
next := tip.key.ToDirection(onward)
if _, ok := result.Nodes[next]; ok {
// no good!
fmt.Printf("\ttip %d/%d cannot grow, blocked at %v\n", i+1, len(tips), next)
continue
}
result.Connect(tip.key, next)
tip.key = next
}
}
return result, nil
}