-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmap.go
More file actions
125 lines (107 loc) · 2.28 KB
/
Copy pathmap.go
File metadata and controls
125 lines (107 loc) · 2.28 KB
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
package typhoon
import "github.com/seebs/nbt"
type BlockPalette interface {
GetId(name string) int
RecoverName(id int) string
GetSize() int
GetContent() []string
}
type ChunkBlockPalette struct {
Map []string
}
type ChunkSection struct {
Palette BlockPalette
Blocks [16 * 16 * 16]int
}
type Chunk struct {
ChunkX int32
ChunkZ int32
Sections [16]*ChunkSection
}
type Map struct {
Spawn Location
Dimension Dimension
Chunks []*Chunk
}
func (m *Map) SetBlock(x, y, z int, typ string) {
chunk := m.GetChunk(int32(x)/16, int32(z)/16)
chunkSection := chunk.GetSection(y / 16)
chunkSection.SetBlock(x%16, y%16, z%16, typ)
}
func (m *Map) GetChunk(x int32, z int32) *Chunk {
for _, c := range m.Chunks {
if c.ChunkX == x && c.ChunkZ == z {
return c
}
}
c := &Chunk{
ChunkX: x,
ChunkZ: z,
}
for i := 0; i < 16; i++ {
c.Sections[i] = &ChunkSection{
Palette: &ChunkBlockPalette{
[]string{"minecraft:air"},
},
}
}
m.Chunks = append(m.Chunks, c)
return c
}
func (chunk *Chunk) GetSection(y int) *ChunkSection {
if chunk.Sections[y] != nil {
return chunk.Sections[y]
}
c := &ChunkSection{
Palette: &ChunkBlockPalette{
[]string{"minecraft:air"},
},
}
chunk.Sections[y] = c
return c
}
func (palette *ChunkBlockPalette) GetId(name string) int {
for i, v := range palette.Map {
if v == name {
return i
}
}
palette.Map = append(palette.Map, name)
return len(palette.Map) - 1
}
func (palette *ChunkBlockPalette) RecoverName(id int) string {
if id < 0 || id >= len(palette.Map) {
return "minecraft:air"
}
return palette.Map[id]
}
func (palette *ChunkBlockPalette) GetSize() int {
return len(palette.Map)
}
func (palette *ChunkBlockPalette) GetContent() []string {
return palette.Map
}
func (section *ChunkSection) SetBlock(x, y, z int, typ string) {
section.Blocks[y<<8|z<<4|x] = section.Palette.GetId(typ)
}
func (m *Map) SendSpawnChunks(p *Player) {
sx := int32(m.Spawn.X) / 16
sz := int32(m.Spawn.Z) / 16
for x := -4; x < 4; x++ {
for z := -4; z < 4; z++ {
c := m.GetChunk(int32(x)+sx, int32(z)+sz)
biomes := make([]byte, 256)
packet := &PacketPlayChunkData{
c.ChunkX,
c.ChunkZ,
m.Dimension,
true,
false,
c.Sections[:],
&biomes,
make([]nbt.Compound, 0),
}
p.WritePacket(packet)
}
}
}