-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmap_generator.go
172 lines (139 loc) · 2.9 KB
/
map_generator.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
package main
import (
"math/rand"
"time"
)
type generator struct {
cell [][]uint32
minx int
maxx int
minz int
maxz int
wide int
deep int
cellSize int
}
func (g *generator) NewWorld(sizeX, sizeZ, numSteps, stepLength int) []uint32 {
rand.Seed(time.Now().Unix())
g.cellSize = 20
g.wide = sizeX / g.cellSize
g.deep = sizeZ / g.cellSize
g.minx, g.minz = 1, 1
g.maxx = g.wide - 2
g.maxz = g.deep - 2
g.cell = make([][]uint32, g.wide)
for x := 0; x < g.wide; x++ {
g.cell[x] = make([]uint32, g.deep)
}
g.randomWalk(numSteps, stepLength)
g.cleanDeadEnds()
g.cleanDeadEnds()
pixels := make([]uint32, 0)
for x := 0; x < g.wide; x++ {
for z := 0; z < g.deep; z++ {
if g.cell[x][z] == 1 {
for i := 0; i < g.cellSize; i++ {
for j := 0; j < g.cellSize; j++ {
//world_.AddPixel(x*g.cell_size+i, z*g.cell_size+j, uint32(0xFF0000FF))
pixels = append(pixels, uint32(x*g.cellSize+i))
pixels = append(pixels, uint32(z*g.cellSize+j))
}
}
}
}
}
return pixels
}
func (g *generator) randomWalk(numSteps, stepLength int) {
px := g.wide / 2
pz := g.deep / 2
for i := 0; i < numSteps; i++ {
g.cell[px][pz] = 1
if rand.Float64() < 0.5 {
g.makeRoom(px, pz, 1, stepLength/2+1)
}
dx := []int{0, 1, 0, -1}
dz := []int{1, 0, -1, 0}
d := rand.Intn(4)
nx := px + dx[d]*stepLength
nz := pz + dz[d]*stepLength
for !g.validCell(nx, nz) {
d = rand.Intn(4)
nx = px + dx[d]*stepLength
nz = pz + dz[d]*stepLength
}
for x := px; x != nx; x += dx[d] {
g.cell[x][pz] = 1
}
for z := pz; z != nz; z += dz[d] {
g.cell[px][z] = 1
}
px = nx
pz = nz
}
}
func (g *generator) random(min, max int) int {
return rand.Intn(max-min) + min
}
func (g *generator) validCell(x, z int) bool {
if x < g.minx || x > g.maxx {
return false
}
if z < g.minz || z > g.maxz {
return false
}
return true
}
func (g *generator) makeRoom(x, z, minw, maxw int) {
roomWidth := g.random(minw, maxw)
roomDepth := g.random(minw, maxw)
for cx := x - roomWidth; cx <= x+roomWidth; cx++ {
for cz := z; cz <= z+roomDepth; cz++ {
if g.validCell(cx, cz) {
g.cell[cx][cz] = 1
}
}
}
}
func (g *generator) cleanDeadEnds() {
dx := []int{0, 1, 0, -1}
dz := []int{1, 0, -1, 0}
for x := 0; x < g.wide; x++ {
for z := 0; z < g.deep; z++ {
if g.cell[x][z] == 1 {
sum := 0
for i := 0; i < 4; i++ {
nx := x + dx[i]
nz := z + dz[i]
if g.validCell(nx, nz) {
if g.cell[nx][nz] == 1 {
sum++
}
}
}
if sum <= 1 {
g.cell[x][z] = 0
}
}
}
}
for x := g.wide - 1; x >= 0; x-- {
for z := g.deep - 1; z >= 0; z-- {
if g.cell[x][z] == 1 {
sum := 0
for i := 0; i < 4; i++ {
nx := x + dx[i]
nz := z + dz[i]
if g.validCell(nx, nz) {
if g.cell[nx][nz] == 1 {
sum++
}
}
}
if sum <= 1 {
g.cell[x][z] = 0
}
}
}
}
}