-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcavegen.go
163 lines (141 loc) · 2.97 KB
/
cavegen.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
package main
import (
"math/rand"
)
func generateCaves() {
randomizeSeed()
CaveMap = make([][]bool, WorldWidth)
for x := range CaveMap {
CaveMap[x] = make([]bool, WorldHeight)
}
// Create caves with varying simplex noise
for x := 0; x < WorldWidth; x++ {
thresh := float32(CaveStartingThreshold)
for y := HeightMap[x]; y >= 0; y-- {
if n := rand.Float32(); n < thresh {
CaveMap[x][y] = true
}
thresh += CaveThresholdDelta
if thresh > CaveEndingThreshold {
thresh = CaveEndingThreshold
}
}
}
// Do cellular automata simulations
for i := 0; i < CaveIterations; i++ {
caveSimulationStep(CaveBirthLimit, CaveDeathLimit)
}
// Randomly kill some caves
for i := 0; i < 2000; i++ {
removeRandomCave()
}
// Do second round of cellular automata simulations (enlarge)
for i := 0; i < SecondCaveIterations; i++ {
caveSimulationStep(SecondCaveBirthLimit, SecondCaveDeathLimit)
}
// Kill tiny caves
//removeSmallCaves()
// Translate to worldmap
for x := 0; x < WorldWidth; x++ {
for y := 0; y < WorldHeight; y++ {
if CaveMap[x][y] {
if y <= HeightMap[x] {
WorldMap.RemoveWorldBlock(x, y)
createBackBlock(x, y, "backdirt")
}
}
}
}
}
func caveSimulationStep(birthLim, deathLim int) {
newMap := make([][]bool, WorldWidth)
for x := range newMap {
newMap[x] = make([]bool, WorldHeight)
}
for x := 0; x < WorldWidth; x++ {
for y := 0; y < WorldHeight; y++ {
nbs := getAliveNeighbors(x, y)
if CaveMap[x][y] {
if nbs < deathLim {
newMap[x][y] = false
} else {
newMap[x][y] = true
}
} else {
if nbs > birthLim {
newMap[x][y] = true
} else {
newMap[x][y] = false
}
}
}
}
CaveMap = newMap
}
func removeRandomCave() {
for i := 0; i < 100; i++ {
x := rand.Intn(WorldWidth)
y := rand.Intn(WorldHeight)
if CaveMap[x][y] {
killCave(x, y)
return
}
}
}
func killCave(x, y int) {
if x < 0 || x >= WorldWidth || y < 0 || y >= WorldHeight {
return
}
if CaveMap[x][y] == false {
return
}
CaveMap[x][y] = false
killCave(x+1, y)
killCave(x-1, y)
killCave(x, y+1)
killCave(x, y-1)
}
func removeSmallCaves() {
for x := 0; x < WorldWidth; x++ {
for y := HeightMap[x]; y >= 0; y-- {
if CaveMap[x][y] {
if r := recurseSize(x, y, 6); r < 12 {
killCave(x, y)
}
}
}
}
}
func recurseSize(x, y, limit int) int {
if limit < 0 {
return 0
}
if x < 0 || x >= WorldWidth || y < 0 || y >= WorldHeight {
return 0
}
if CaveMap[x][y] == false {
return 0
}
total := 1
total += recurseSize(x+1, y, limit-1)
total += recurseSize(x-1, y, limit-1)
total += recurseSize(x, y+1, limit-1)
total += recurseSize(x, y-1, limit-1)
return total
}
func getAliveNeighbors(x, y int) int {
count := 0
for i := -1; i < 2; i++ {
for j := -1; j < 2; j++ {
nx := x + i
ny := y + j
if i == 0 && j == 0 {
} else if nx < 0 || nx >= WorldWidth || ny < 0 || ny >= WorldHeight {
count++
} else if CaveMap[nx][ny] {
count++
}
}
}
return count
}