forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
200 lines (171 loc) · 5.36 KB
/
loader.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
package world
import (
"github.com/go-gl/mathgl/mgl64"
"math"
"sync"
)
// Loader implements the loading of the world. A loader can typically be moved around the world to load
// different parts of the world. An example usage is the player, which uses a loader to load chunks around it
// so that it can view them.
type Loader struct {
r int
w *World
viewer Viewer
mu sync.RWMutex
pos ChunkPos
loadQueue []ChunkPos
loaded map[ChunkPos]*Column
closed bool
}
// NewLoader creates a new loader using the chunk radius passed. Chunks beyond this radius from the position
// of the loader will never be loaded.
// The Viewer passed will handle the loading of chunks, including the viewing of entities that were loaded in
// those chunks.
func NewLoader(chunkRadius int, world *World, v Viewer) *Loader {
l := &Loader{r: chunkRadius, loaded: make(map[ChunkPos]*Column), viewer: v}
l.world(world)
return l
}
// World returns the World that the Loader is in.
func (l *Loader) World() *World {
l.mu.RLock()
defer l.mu.RUnlock()
return l.w
}
// ChangeWorld changes the World of the Loader. The currently loaded chunks are reset and any future loading
// is done from the new World.
func (l *Loader) ChangeWorld(new *World) {
l.mu.Lock()
defer l.mu.Unlock()
l.reset()
l.world(new)
}
// ChangeRadius changes the maximum chunk radius of the Loader.
func (l *Loader) ChangeRadius(new int) {
l.mu.Lock()
defer l.mu.Unlock()
l.r = new
l.evictUnused()
l.populateLoadQueue()
}
// Move moves the loader to the position passed. The position is translated to a chunk position to load
func (l *Loader) Move(pos mgl64.Vec3) {
l.mu.Lock()
defer l.mu.Unlock()
chunkPos := chunkPosFromVec3(pos)
if chunkPos == l.pos {
return
}
l.pos = chunkPos
l.evictUnused()
l.populateLoadQueue()
}
// Load loads n chunks around the centre of the chunk, starting with the middle and working outwards. For
// every chunk loaded, the Viewer passed through construction in New has its ViewChunk method called.
// Load does nothing for n <= 0.
func (l *Loader) Load(n int) {
l.mu.Lock()
defer l.mu.Unlock()
if l.closed || l.w == nil {
return
}
for i := 0; i < n; i++ {
if len(l.loadQueue) == 0 {
break
}
pos := l.loadQueue[0]
c := l.w.chunk(pos)
l.viewer.ViewChunk(pos, c.Chunk, c.BlockEntities)
l.w.addViewer(c, l)
l.loaded[pos] = c
// Shift the first element from the load queue off so that we can take a new one during the next
// iteration.
l.loadQueue = l.loadQueue[1:]
}
}
// Chunk attempts to return a chunk at the given ChunkPos. If the chunk is not loaded, the second return value will
// be false.
func (l *Loader) Chunk(pos ChunkPos) (*Column, bool) {
l.mu.RLock()
c, ok := l.loaded[pos]
l.mu.RUnlock()
return c, ok
}
// Close closes the loader. It unloads all chunks currently loaded for the viewer, and hides all entities that
// are currently shown to it.
func (l *Loader) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
l.reset()
l.closed = true
l.viewer = nil
return nil
}
// Reset clears all chunks loaded by the Loader and repopulates the loading queue so that they can all be loaded again.
func (l *Loader) Reset() {
l.mu.Lock()
defer l.mu.Unlock()
l.reset()
l.w.addWorldViewer(l)
l.populateLoadQueue()
}
// reset clears the Loader so that it may be used as if it was created again with NewLoader.
func (l *Loader) reset() {
for pos := range l.loaded {
l.w.removeViewer(pos, l)
}
l.loaded = map[ChunkPos]*Column{}
l.w.removeWorldViewer(l)
}
// world sets the loader's world, adds them to the world's viewer list, then starts populating the load queue.
// This is only here to get rid of duplicated code, ChangeWorld should be used instead of this.
func (l *Loader) world(new *World) {
l.w = new
l.w.addWorldViewer(l)
l.populateLoadQueue()
}
// evictUnused gets rid of chunks in the loaded map which are no longer within the chunk radius of the loader,
// and should therefore be removed.
func (l *Loader) evictUnused() {
for pos := range l.loaded {
diffX, diffZ := pos[0]-l.pos[0], pos[1]-l.pos[1]
dist := math.Sqrt(float64(diffX*diffX) + float64(diffZ*diffZ))
if int(dist) > l.r {
delete(l.loaded, pos)
l.w.removeViewer(pos, l)
}
}
}
// populateLoadQueue populates the load queue of the loader. This method is called once to create the order in
// which chunks around the position the loader is now in should be loaded. Chunks are ordered to be loaded
// from the middle outwards.
func (l *Loader) populateLoadQueue() {
// We'll first load the chunk positions to load in a map indexed by the distance to the center (basically,
// what precedence it should have), and put them in the loadQueue in that order.
queue := map[int32][]ChunkPos{}
r := int32(l.r)
for x := -r; x <= r; x++ {
for z := -r; z <= r; z++ {
distance := math.Sqrt(float64(x*x) + float64(z*z))
chunkDistance := int32(math.Round(distance))
if chunkDistance > r {
// The chunk was outside the chunk radius.
continue
}
pos := ChunkPos{x + l.pos[0], z + l.pos[1]}
if _, ok := l.loaded[pos]; ok {
// The chunk was already loaded, so we don't need to do anything.
continue
}
if m, ok := queue[chunkDistance]; ok {
queue[chunkDistance] = append(m, pos)
continue
}
queue[chunkDistance] = []ChunkPos{pos}
}
}
l.loadQueue = l.loadQueue[:0]
for i := int32(0); i < r; i++ {
l.loadQueue = append(l.loadQueue, queue[i]...)
}
}