This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
forked from ZeppelinMC/Zeppelin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtick.go
110 lines (104 loc) · 2.82 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
package main
import (
"fmt"
"time"
"github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/level"
pk "github.com/Tnze/go-mc/net/packet"
)
func (world World) TickLoop() {
var n uint
for range time.Tick(time.Microsecond * 20) {
world.Tick(n)
n++
}
}
func (world World) Tick(n uint) {
world.TickLock.Lock()
defer world.TickLock.Unlock()
if n%8 == 0 {
world.SubtickChunkLoad(n)
}
//world.TickPlayerUpdate(n)
}
/*func (world World) TickPlayerUpdate(tick uint) {
for _, p := range server.Players.Players {
if p.Data.Dimension != world.Name {
continue
}
x := int32(p.Position[0])
y := int32(p.Position[1])
z := int32(p.Position[2])
if p.Position != p.OldPosition {
server.BroadcastPacketExcept(pk.Marshal(packetid.ClientboundMoveEntityPos,
pk.VarInt(p.EntityID),
pk.Short((x*32-p.OldPosition[0]*32)*128),
pk.Short((y*32-p.OldPosition[1]*32)*128),
pk.Short((z*32-p.OldPosition[2]*32)*128),
pk.Boolean(true),
), p.UUID.String)
}
p.LastTick = tick
}
}*/
func (world World) SubtickChunkLoad(tick uint) {
for _, p := range server.Players.Players {
if p.Data.Dimension != world.Name {
continue
}
x := int32(p.Position[0]) >> 4
y := int32(p.Position[1]) >> 4
z := int32(p.Position[2]) >> 4
if newChunkPos := [3]int32{x, y, z}; newChunkPos != p.ChunkPos {
p.ChunkPos = newChunkPos
p.Connection.WritePacket(pk.Marshal(packetid.ClientboundSetChunkCacheCenter, pk.VarInt(x), pk.VarInt(z)))
fmt.Println("sent packet 78 for player", p.Name)
}
p.LastTick = tick
}
LoadChunk:
for _, player := range server.Players.Players {
if player.Data.Dimension != world.Name {
continue
}
player.CalculateLoadingQueue()
for _, pos := range player.LoadQueue {
if _, ok := world.Chunks[pos]; !ok {
if !world.LoadChunk(pos) {
break LoadChunk
}
}
lc := world.Chunks[pos]
if lc == nil {
break LoadChunk
}
player.LoadedChunks[pos] = struct{}{}
lc.AddViewer(player.UUID.String)
lc.Lock()
player.Connection.WritePacket(pk.Marshal(packetid.ClientboundLevelChunkWithLight, level.ChunkPos(pos), lc.Chunk))
fmt.Println("sent packet 36 for player", player.Name, pos, len(lc.Chunk.Sections))
lc.Unlock()
}
}
for _, player := range server.Players.Players {
if player.Data.Dimension != world.Name {
continue
}
player.CalculateUnusedChunks()
for _, pos := range player.UnloadQueue {
delete(player.LoadedChunks, pos)
world.Chunks[pos].RemoveViewer(player.UUID.String)
player.Connection.WritePacket(pk.Marshal(packetid.ClientboundForgetLevelChunk, level.ChunkPos(pos)))
fmt.Println("sent packet 30 for player", player.Name)
}
}
var unloadQueue [][2]int32
for pos, chunk := range world.Chunks {
if len(chunk.Viewers) == 0 {
unloadQueue = append(unloadQueue, pos)
}
}
for i := range unloadQueue {
world.UnloadChunk(unloadQueue[i])
}
}