-
Notifications
You must be signed in to change notification settings - Fork 131
/
world.go
279 lines (254 loc) · 5.19 KB
/
world.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
273
274
275
276
277
278
279
package main
import (
"log"
"sync"
"github.com/go-gl/mathgl/mgl32"
lru "github.com/hashicorp/golang-lru"
)
type World struct {
mutex sync.Mutex
chunks *lru.Cache // map[Vec3]*Chunk
}
func NewWorld() *World {
m := (*renderRadius) * (*renderRadius) * 4
chunks, _ := lru.New(m)
return &World{
chunks: chunks,
}
}
func (w *World) loadChunk(id Vec3) (*Chunk, bool) {
chunk, ok := w.chunks.Get(id)
if !ok {
return nil, false
}
return chunk.(*Chunk), true
}
func (w *World) storeChunk(id Vec3, chunk *Chunk) {
w.chunks.Add(id, chunk)
}
func (w *World) Collide(pos mgl32.Vec3) (mgl32.Vec3, bool) {
x, y, z := pos.X(), pos.Y(), pos.Z()
nx, ny, nz := round(pos.X()), round(pos.Y()), round(pos.Z())
const pad = 0.25
head := Vec3{int(nx), int(ny), int(nz)}
foot := head.Down()
stop := false
for _, b := range []Vec3{foot, head} {
if IsObstacle(w.Block(b.Left())) && x < nx && nx-x > pad {
x = nx - pad
}
if IsObstacle(w.Block(b.Right())) && x > nx && x-nx > pad {
x = nx + pad
}
if IsObstacle(w.Block(b.Down())) && y < ny && ny-y > pad {
y = ny - pad
stop = true
}
if IsObstacle(w.Block(b.Up())) && y > ny && y-ny > pad {
y = ny + pad
stop = true
}
if IsObstacle(w.Block(b.Back())) && z < nz && nz-z > pad {
z = nz - pad
}
if IsObstacle(w.Block(b.Front())) && z > nz && z-nz > pad {
z = nz + pad
}
}
return mgl32.Vec3{x, y, z}, stop
}
func (w *World) HitTest(pos mgl32.Vec3, vec mgl32.Vec3) (*Vec3, *Vec3) {
var (
maxLen = float32(8.0)
step = float32(0.125)
block, prev Vec3
pprev *Vec3
)
for len := float32(0); len < maxLen; len += step {
block = NearBlock(pos.Add(vec.Mul(len)))
if prev != block && w.HasBlock(block) {
return &block, pprev
}
prev = block
pprev = &prev
}
return nil, nil
}
func (w *World) Block(id Vec3) int {
chunk := w.BlockChunk(id)
if chunk == nil {
return -1
}
return chunk.Block(id)
}
func (w *World) BlockChunk(block Vec3) *Chunk {
cid := block.Chunkid()
chunk, ok := w.loadChunk(cid)
if !ok {
return nil
}
return chunk
}
func (w *World) UpdateBlock(id Vec3, tp int) {
chunk := w.BlockChunk(id)
if chunk != nil {
if tp != 0 {
chunk.add(id, tp)
} else {
chunk.del(id)
}
}
store.UpdateBlock(id, tp)
}
func IsPlant(tp int) bool {
if tp >= 17 && tp <= 31 {
return true
}
return false
}
func IsTransparent(tp int) bool {
if IsPlant(tp) {
return true
}
switch tp {
case -1, 0, 10, 15:
return true
default:
return false
}
}
func IsObstacle(tp int) bool {
if IsPlant(tp) {
return false
}
switch tp {
case -1:
return true
case 0:
return false
default:
return true
}
}
func (w *World) HasBlock(id Vec3) bool {
tp := w.Block(id)
return tp != -1 && tp != 0
}
func (w *World) Chunk(id Vec3) *Chunk {
p, ok := w.loadChunk(id)
if ok {
return p
}
chunk := NewChunk(id)
blocks := makeChunkMap(id)
for block, tp := range blocks {
chunk.add(block, tp)
}
err := store.RangeBlocks(id, func(bid Vec3, w int) {
if w == 0 {
chunk.del(bid)
return
}
chunk.add(bid, w)
})
if err != nil {
log.Printf("fetch chunk(%v) from db error:%s", id, err)
return nil
}
ClientFetchChunk(id, func(bid Vec3, w int) {
if w == 0 {
chunk.del(bid)
return
}
chunk.add(bid, w)
store.UpdateBlock(bid, w)
})
w.storeChunk(id, chunk)
return chunk
}
func (w *World) Chunks(ids []Vec3) []*Chunk {
ch := make(chan *Chunk)
var chunks []*Chunk
for _, id := range ids {
id := id
go func() {
ch <- w.Chunk(id)
}()
}
for range ids {
chunk := <-ch
if chunk != nil {
chunks = append(chunks, chunk)
}
}
return chunks
}
func makeChunkMap(cid Vec3) map[Vec3]int {
const (
grassBlock = 1
sandBlock = 2
grass = 17
leaves = 15
wood = 5
)
m := make(map[Vec3]int)
p, q := cid.X, cid.Z
for dx := 0; dx < ChunkWidth; dx++ {
for dz := 0; dz < ChunkWidth; dz++ {
x, z := p*ChunkWidth+dx, q*ChunkWidth+dz
f := noise2(float32(x)*0.01, float32(z)*0.01, 4, 0.5, 2)
g := noise2(float32(-x)*0.01, float32(-z)*0.01, 2, 0.9, 2)
mh := int(g*32 + 16)
h := int(f * float32(mh))
w := grassBlock
if h <= 12 {
h = 12
w = sandBlock
}
// grass and sand
for y := 0; y < h; y++ {
m[Vec3{x, y, z}] = w
}
// flowers
if w == grassBlock {
if noise2(-float32(x)*0.1, float32(z)*0.1, 4, 0.8, 2) > 0.6 {
m[Vec3{x, h, z}] = grass
}
if noise2(float32(x)*0.05, float32(-z)*0.05, 4, 0.8, 2) > 0.7 {
w := 18 + int(noise2(float32(x)*0.1, float32(z)*0.1, 4, 0.8, 2)*7)
m[Vec3{x, h, z}] = w
}
}
// tree
if w == 1 {
ok := true
if dx-4 < 0 || dz-4 < 0 ||
dx+4 > ChunkWidth || dz+4 > ChunkWidth {
ok = false
}
if ok && noise2(float32(x), float32(z), 6, 0.5, 2) > 0.79 {
for y := h + 3; y < h+8; y++ {
for ox := -3; ox <= 3; ox++ {
for oz := -3; oz <= 3; oz++ {
d := ox*ox + oz*oz + (y-h-4)*(y-h-4)
if d < 11 {
m[Vec3{x + ox, y, z + oz}] = leaves
}
}
}
}
for y := h; y < h+7; y++ {
m[Vec3{x, y, z}] = wood
}
}
}
// cloud
for y := 64; y < 72; y++ {
if noise3(float32(x)*0.01, float32(y)*0.1, float32(z)*0.01, 8, 0.5, 2) > 0.69 {
m[Vec3{x, y, z}] = 16
}
}
}
}
return m
}