-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.go
272 lines (241 loc) · 7.33 KB
/
generate.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package overworld
import (
"fmt"
"math/rand"
"github.com/griffithsh/squads/graph"
"github.com/griffithsh/squads/baddy"
"github.com/griffithsh/squads/game"
"github.com/griffithsh/squads/geom"
"github.com/griffithsh/squads/squad"
)
// connect two Nodes by adding each to the other's Connected map.
func connect(n1 *Node, n2 *Node) error {
if n1 == nil {
return fmt.Errorf("cannot connect n1(nil) to n2(%v)", n2)
}
if n2 == nil {
return fmt.Errorf("cannot connect n1(%v) to n2(nil)", n1)
}
n1Neighbors := n1.ID.Neighbors()
dirOfN2, ok := n1Neighbors[n2.ID]
if !ok {
fmt.Printf("neighbors of n1 (%v): %v\n", n1.ID, n1Neighbors)
fmt.Printf("neighbors of n2 (%v): %v\n", n2.ID, n2.ID.Neighbors())
return fmt.Errorf("cannot connect n1 (%v) to n2 (%v): not neighbors", n1, n2)
}
dirOfN1 := geom.Opposite[dirOfN2]
if n1.Connected == nil {
n1.Connected = map[geom.DirectionType]geom.Key{}
}
n1.Connected[dirOfN2] = n2.ID
if n2.Connected == nil {
n2.Connected = map[geom.DirectionType]geom.Key{}
}
n2.Connected[dirOfN1] = n1.ID
return nil
}
// recurse generates pathways between nodes by iterating through hexes adjacent to start.
func recurse(rng *rand.Rand, origin geom.Key, d *Map, paths map[KeyPair]struct{}) {
dirs := []geom.DirectionType{geom.S, geom.SW, geom.NW, geom.N, geom.NE, geom.SE}
rng.Shuffle(len(dirs), func(i, j int) {
dirs[i], dirs[j] = dirs[j], dirs[i]
})
neighbors := origin.Adjacent()
for _, dir := range dirs {
// threshold is how likely this neighbor is to not continue in this
// direction
threshold := 0.0
switch len(d.Nodes[origin].Connected) {
case 0: // 100% chance of this node connecting to anything
case 1: // 65% chance of this node connecting to 2 things
threshold = 0.45
case 2:
threshold = 0.85
case 3:
threshold = 0.925
default:
threshold = 0.99999
}
if rng.Float64() > threshold {
neigh := neighbors[dir]
// if this direction is not in potentials, then pull out.
if _, ok := paths[KeyPair{origin, neigh}]; !ok {
continue
}
// Does this direction already have a node?
if _, ok := d.Nodes[neigh]; ok {
// We have a chance to continue, and not connect the nodes
if rng.Float64() > 0.25 {
continue
}
} else {
d.Nodes[neigh] = &Node{ID: neigh}
}
err := connect(d.Nodes[origin], d.Nodes[neigh])
if err != nil {
fmt.Printf("connect %v to %v failed: %v\n", origin, neigh, err)
continue
}
recurse(rng, neigh, d, paths)
}
}
}
// generate a Map from a Recipe and a base enemy level for opponents by calling
// randomness from rng.
func generate(rng *rand.Rand, recipe *Recipe, lvl int) Map {
// TODO: use lvl to make the baddies stronger
d := Map{
Terrain: recipe.Terrain,
Nodes: map[geom.Key]*Node{},
Enemies: map[geom.Key][]*game.Character{},
}
// Pick the first key of a random pair in the recipe's permissable paths.
origin := recipe.Paths[rng.Intn(len(recipe.Paths)-1)].First
d.Nodes[origin] = &Node{ID: origin}
// unpack paths into a two-way set available paths.
pathSet := map[KeyPair]struct{}{}
keySet := map[geom.Key]struct{}{}
for _, path := range recipe.Paths {
pathSet[KeyPair{path.First, path.Second}] = struct{}{}
pathSet[KeyPair{path.Second, path.First}] = struct{}{}
keySet[path.First] = struct{}{}
keySet[path.Second] = struct{}{}
}
// recurse through, generating a random path.
recurse(rng, origin, &d, pathSet)
// Roll for Points of interest that must be included in the Map's Nodes
must := []geom.Key{}
for _, poi := range recipe.Interesting {
// Randomly select poi.Pick number of Options from each InterestRoll, by
// constructing a shuffled sice of indices, then truncating the excess.
indices := []int{}
for i := range poi.Options {
indices = append(indices, i)
}
rng.Shuffle(len(indices), func(i, j int) {
indices[i], indices[j] = indices[j], indices[i]
})
indices = indices[:poi.Pick]
for _, i := range indices {
must = append(must, poi.Options[i])
}
}
// Link every rolled point of interest by connecting it to the origin that
// we started generating with.
for _, poi := range must {
if _, ok := d.Nodes[poi]; ok {
// This poi is already in the existing nodes.
continue
}
cost := func(_ graph.Vertex, v graph.Vertex) float64 {
k := v.(geom.Key)
if _, ok := d.Nodes[k]; ok {
return 0.0
}
return 1.0
}
edge := func(v graph.Vertex) []graph.Vertex {
k := v.(geom.Key)
result := make([]graph.Vertex, 0, 6)
neighbors := []geom.Key{
k.ToN(),
k.ToNE(),
k.ToSE(),
k.ToS(),
k.ToSW(),
k.ToNW(),
}
for _, k := range neighbors {
if _, ok := keySet[k]; ok {
result = append(result, k)
}
}
return result
}
guess := func(v1, v2 graph.Vertex) float64 {
squareDiff := func(a, b int) float64 {
diff := a - b
if diff < 0 {
diff = -diff
}
return float64(diff * diff)
}
a, b := v1.(geom.Key), v2.(geom.Key)
return squareDiff(a.M, b.M) + squareDiff(a.N, b.N)
}
steps := graph.NewSearcher(cost, edge, guess).Search(origin, poi)
if steps == nil {
// Points of interest should never be located in areas of the map
// that are inaccessible? This can only panic on recipes with
// non-contiguous Paths, right?
panic(fmt.Sprintf("there was no path from %v to the PoI at %v", origin, poi))
}
var prev geom.Key
prev = geom.Key{M: steps[0].V.(geom.Key).M, N: steps[0].V.(geom.Key).N}
for i, step := range steps[1:] {
k := geom.Key{M: step.V.(geom.Key).M, N: step.V.(geom.Key).N}
// If this step is not already in nodes, add it.
if _, ok := d.Nodes[k]; !ok {
// Add a new node.
current := Node{ID: k}
d.Nodes[k] = ¤t
// connect the new node to the previous step's node.
err := connect(d.Nodes[prev], ¤t)
if err != nil {
fmt.Printf("connect step %d: %v\n", i, err)
}
}
// Prepare for next loop.
prev = k
}
}
if len(d.Nodes) < 2 {
fmt.Println("impossible Map generated: not enough room for both start and exit")
// try again?
return generate(rng, recipe, lvl)
}
// Sort then shuffle keys, so that the results of this function are
// deterministic based on the provided PRNG.
keys := d.SortedNodeKeys()
rand.Shuffle(len(keys), func(i, j int) {
keys[i], keys[j] = keys[j], keys[i]
})
for i, key := range keys {
if i == 0 {
// First key is the player start.
d.Start = key
continue
} else if i == 1 {
// Second key is the exit gate.
d.Gate = key
continue
}
// TODO: The selection of squads should be controlled by the overworld
// Recipe.
if rng.Intn(3) == 0 { // 1 in 3 chance of adding an enemy squad here.
rollCharacters := func(rng *rand.Rand, id squad.RecipeID) []*game.Character {
result := []*game.Character{}
for _, recipeID := range squad.Recipes[id].Construct(rng) {
char := baddy.Recipes[recipeID].Construct(rng)
char.Level = lvl
vit := int(char.VitalityPerLevel * float64(char.Level))
hp := char.BaseHealth
char.CurrentHealth = game.MaxHealth(hp, vit)
result = append(result, char)
}
return result
}
switch rng.Intn(4) {
case 0:
d.Enemies[key] = rollCharacters(rng, squad.SoloSkellington)
case 1:
d.Enemies[key] = rollCharacters(rng, squad.WolfPack1)
case 2:
d.Enemies[key] = rollCharacters(rng, squad.SoloNecro)
case 3:
d.Enemies[key] = rollCharacters(rng, squad.NecroCohort)
}
}
}
return d
}