-
Notifications
You must be signed in to change notification settings - Fork 2
/
noiseTerrainStrategy.go
82 lines (71 loc) · 1.74 KB
/
noiseTerrainStrategy.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
package procedural
import (
"math/rand"
"github.com/griffithsh/squads/geom"
)
type TerrainChances []struct {
Value Code
Chance int
}
func (tc TerrainChances) roll(prng *rand.Rand) Code {
sum := 0
for _, c := range tc {
sum += c.Chance
}
got := prng.Intn(sum)
running := 0
for _, chance := range tc {
if got < chance.Chance+running {
return chance.Value
}
running += chance.Chance
}
// FIXME: [0]? Why roll chances then?
return tc[0].Value
}
type NoiseTerrainStrategy struct {
Smoothing int
Outside Code
Components TerrainChances
}
func (ts *NoiseTerrainStrategy) Build(prng *rand.Rand, paths Paths) map[geom.Key]Code {
bloated := map[geom.Key]struct{}{}
for key := range paths.Nodes {
bloated[key] = struct{}{}
neighbors := key.ExpandBy(0, 2)
for _, neighbor := range neighbors {
bloated[neighbor] = struct{}{}
}
}
result := map[geom.Key]Code{}
for _, k := range shuffledGeomKeys(prng, bloated) {
result[k] = ts.Components.roll(prng)
}
// Apply smoothing.
// If more than 3 neighbors of a hex have the same code, the hex becomes that code.
// If exactly 3 neighbors of a hex have the same code, and the other three
// neighbors are spread across 2 or more codes, the hex becomes the code of
// the three neighbors.
nextGen := map[geom.Key]Code{}
for i := 0; i < ts.Smoothing; i++ {
for k, code := range result {
m := map[geom.Key]Code{}
for _, key := range k.Adjacent() {
if code, ok := result[key]; ok {
m[key] = code
} else {
m[key] = ts.Outside
}
}
grouped := counts(m)
nextGen[k] = code
for v, count := range grouped {
if (count == 3 && len(grouped) > 3) || count > 3 {
nextGen[k] = v
}
}
}
nextGen, result = result, nextGen
}
return result
}