-
Notifications
You must be signed in to change notification settings - Fork 139
/
tick.go
308 lines (273 loc) · 8.47 KB
/
tick.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package world
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/internal/sliceutil"
"golang.org/x/exp/maps"
"math/rand"
"slices"
"time"
)
// ticker implements World ticking methods. World embeds this struct, so any exported methods on ticker are exported
// methods on World.
type ticker struct{ w *World }
// tickLoop starts ticking the World 20 times every second, updating all entities, blocks and other features such as
// the time and weather of the world, as required.
func (t ticker) tickLoop() {
tc := time.NewTicker(time.Second / 20)
defer tc.Stop()
t.w.running.Add(1)
for {
select {
case <-tc.C:
t.tick()
case <-t.w.closing:
// World is being closed: Stop ticking and get rid of a task.
t.w.running.Done()
return
}
}
}
// tick performs a tick on the World and updates the time, weather, blocks and entities that require updates.
func (t ticker) tick() {
viewers, loaders := t.w.allViewers()
t.w.set.Lock()
if len(viewers) == 0 && t.w.set.CurrentTick != 0 {
t.w.set.Unlock()
return
}
if t.w.advance {
t.w.set.CurrentTick++
if t.w.set.TimeCycle {
t.w.set.Time++
}
if t.w.set.WeatherCycle {
t.w.advanceWeather()
}
}
rain, thunder, tick, tim := t.w.set.Raining, t.w.set.Thundering && t.w.set.Raining, t.w.set.CurrentTick, int(t.w.set.Time)
t.w.set.Unlock()
if tick%20 == 0 {
for _, viewer := range viewers {
if t.w.conf.Dim.TimeCycle() {
viewer.ViewTime(tim)
}
if t.w.conf.Dim.WeatherCycle() {
viewer.ViewWeather(rain, thunder)
}
}
}
if thunder {
t.w.tickLightning()
}
t.tickEntities(tick)
t.tickBlocksRandomly(loaders, tick)
t.tickScheduledBlocks(tick)
t.performNeighbourUpdates()
}
// tickScheduledBlocks executes scheduled block updates in chunks that are currently loaded.
func (t ticker) tickScheduledBlocks(tick int64) {
t.w.updateMu.Lock()
positions := make([]cube.Pos, 0, len(t.w.scheduledUpdates)/4)
for pos, scheduledTick := range t.w.scheduledUpdates {
if scheduledTick <= tick {
positions = append(positions, pos)
delete(t.w.scheduledUpdates, pos)
}
}
t.w.updateMu.Unlock()
for _, pos := range positions {
if ticker, ok := t.w.Block(pos).(ScheduledTicker); ok {
ticker.ScheduledTick(pos, t.w, t.w.r)
}
if liquid, ok := t.w.additionalLiquid(pos); ok {
if ticker, ok := liquid.(ScheduledTicker); ok {
ticker.ScheduledTick(pos, t.w, t.w.r)
}
}
}
}
// performNeighbourUpdates performs all block updates that came as a result of a neighbouring block being changed.
func (t ticker) performNeighbourUpdates() {
t.w.updateMu.Lock()
positions := slices.Clone(t.w.neighbourUpdates)
t.w.neighbourUpdates = t.w.neighbourUpdates[:0]
t.w.updateMu.Unlock()
for _, update := range positions {
pos, changedNeighbour := update.pos, update.neighbour
if ticker, ok := t.w.Block(pos).(NeighbourUpdateTicker); ok {
ticker.NeighbourUpdateTick(pos, changedNeighbour, t.w)
}
if liquid, ok := t.w.additionalLiquid(pos); ok {
if ticker, ok := liquid.(NeighbourUpdateTicker); ok {
ticker.NeighbourUpdateTick(pos, changedNeighbour, t.w)
}
}
}
}
// tickBlocksRandomly executes random block ticks in each sub chunk in the world that has at least one viewer
// registered from the viewers passed.
func (t ticker) tickBlocksRandomly(loaders []*Loader, tick int64) {
var (
r = int32(t.w.tickRange())
g randUint4
blockEntities []cube.Pos
randomBlocks []cube.Pos
)
if r == 0 {
// NOP if the simulation distance is 0.
return
}
loaded := make([]ChunkPos, 0, len(loaders))
for _, loader := range loaders {
loader.mu.RLock()
pos := loader.pos
loader.mu.RUnlock()
loaded = append(loaded, pos)
}
t.w.chunkMu.Lock()
for pos, c := range t.w.chunks {
if !t.anyWithinDistance(pos, loaded, r) {
// No loaders in this chunk that are within the simulation distance, so proceed to the next.
continue
}
c.Lock()
blockEntities = append(blockEntities, maps.Keys(c.BlockEntities)...)
cx, cz := int(pos[0]<<4), int(pos[1]<<4)
// We generate up to j random positions for every sub chunk.
for j := 0; j < t.w.conf.RandomTickSpeed; j++ {
x, y, z := g.uint4(t.w.r), g.uint4(t.w.r), g.uint4(t.w.r)
for i, sub := range c.Sub() {
if sub.Empty() {
// SubChunk is empty, so skip it right away.
continue
}
// Generally we would want to make sure the block has its block entities, but provided blocks
// with block entities are generally ticked already, we are safe to assume that blocks
// implementing the RandomTicker don't rely on additional block entity data.
if rid := sub.Layers()[0].At(x, y, z); randomTickBlocks[rid] {
subY := (i + (t.w.Range().Min() >> 4)) << 4
randomBlocks = append(randomBlocks, cube.Pos{cx + int(x), subY + int(y), cz + int(z)})
// Only generate new coordinates if a tickable block was actually found. If not, we can just re-use
// the coordinates for the next sub chunk.
x, y, z = g.uint4(t.w.r), g.uint4(t.w.r), g.uint4(t.w.r)
}
}
}
c.Unlock()
}
t.w.chunkMu.Unlock()
for _, pos := range randomBlocks {
if rb, ok := t.w.Block(pos).(RandomTicker); ok {
rb.RandomTick(pos, t.w, t.w.r)
}
}
for _, pos := range blockEntities {
if tb, ok := t.w.Block(pos).(TickerBlock); ok {
tb.Tick(tick, pos, t.w)
}
}
}
// anyWithinDistance checks if any of the ChunkPos loaded are within the distance r of the ChunkPos pos.
func (t ticker) anyWithinDistance(pos ChunkPos, loaded []ChunkPos, r int32) bool {
for _, chunkPos := range loaded {
xDiff, zDiff := chunkPos[0]-pos[0], chunkPos[1]-pos[1]
if (xDiff*xDiff)+(zDiff*zDiff) <= r*r {
// The chunk was within the simulation distance of at least one viewer, so we can proceed to
// ticking the block.
return true
}
}
return false
}
// tickEntities ticks all entities in the world, making sure they are still located in the correct chunks and
// updating where necessary.
func (t ticker) tickEntities(tick int64) {
type entityToMove struct {
e Entity
after *Column
viewersBefore []Viewer
}
var (
entitiesToMove []entityToMove
entitiesToTick []TickerEntity
)
t.w.chunkMu.Lock()
t.w.entityMu.Lock()
for e, lastPos := range t.w.entities {
chunkPos := chunkPosFromVec3(e.Position())
c, ok := t.w.chunks[chunkPos]
if !ok {
continue
}
c.Lock()
v := len(c.viewers)
c.Unlock()
if v > 0 {
if ticker, ok := e.(TickerEntity); ok {
entitiesToTick = append(entitiesToTick, ticker)
}
}
if lastPos != chunkPos {
// The entity was stored using an outdated chunk position. We update it and make sure it is ready
// for loaders to view it.
t.w.entities[e] = chunkPos
var viewers []Viewer
// When changing an entity's world, then teleporting it immediately, we could end up in a situation
// where the old chunk of the entity was not loaded. In this case, it should be safe simply to ignore
// the loaders from the old chunk. We can assume they never saw the entity in the first place.
if old, ok := t.w.chunks[lastPos]; ok {
old.Lock()
old.Entities = sliceutil.DeleteVal(old.Entities, e)
viewers = slices.Clone(old.viewers)
old.Unlock()
}
entitiesToMove = append(entitiesToMove, entityToMove{e: e, viewersBefore: viewers, after: c})
}
}
t.w.entityMu.Unlock()
t.w.chunkMu.Unlock()
for _, move := range entitiesToMove {
move.after.Lock()
move.after.Entities = append(move.after.Entities, move.e)
viewersAfter := move.after.viewers
move.after.Unlock()
for _, viewer := range move.viewersBefore {
if sliceutil.Index(viewersAfter, viewer) == -1 {
// First we hide the entity from all loaders that were previously viewing it, but no
// longer are.
viewer.HideEntity(move.e)
}
}
for _, viewer := range viewersAfter {
if sliceutil.Index(move.viewersBefore, viewer) == -1 {
// Then we show the entity to all loaders that are now viewing the entity in the new
// chunk.
showEntity(move.e, viewer)
}
}
}
for _, ticker := range entitiesToTick {
// Make sure the entity is still in world and has not been closed.
if ticker.World() == t.w {
// We gather entities to ticker and ticker them later, so that the lock on the entity mutex is no longer
// active.
ticker.Tick(t.w, tick)
}
}
}
// randUint4 is a structure used to generate random uint4s.
type randUint4 struct {
x uint64
n uint8
}
// uint4 returns a random uint4.
func (g *randUint4) uint4(r *rand.Rand) uint8 {
if g.n == 0 {
g.x = r.Uint64()
g.n = 16
}
val := g.x & 0b1111
g.x >>= 4
g.n--
return uint8(val)
}