-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
260 lines (230 loc) · 8.65 KB
/
data.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
package structure
import (
"fmt"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/chunk"
"github.com/df-mc/worldupgrader/blockupgrader"
"strconv"
"unsafe"
)
// structure is the outer wrapper of the structure. It holds the version of the structure, its dimensions,
// origin in the world it was created in, and the actual structure data.
type structure struct {
FormatVersion int32 `nbt:"format_version"`
Size []int32 `nbt:"size"`
Origin []int32 `nbt:"structure_world_origin"`
Structure structureData `nbt:"structure"`
palette *palette
paletteName string
parsedPalette []parsedBlock
l, h int
blocks, liquids []int32
blocksPtr, liquidsPtr, palettePtr unsafe.Pointer
}
// parsedBlock is a palette entry that has been parsed in advance.
type parsedBlock struct {
b world.Block
hasNBT bool
}
const version = 1
var (
// Check to ensure that *structure implements the world.Structure interface.
_ world.Structure = (*structure)(nil)
sizeOfBlock = unsafe.Sizeof(parsedBlock{})
)
// Dimensions returns the dimensions of the structure as set in the Origin field.
func (s *structure) Dimensions() [3]int {
return [3]int{int(s.Size[0]), int(s.Size[1]), int(s.Size[2])}
}
// prepare moves and converts several fields such as the structure's dimensions and block index slices to a form that
// can be accessed more quickly in At and Set.
func (s *structure) prepare() {
s.l, s.h = int(s.Size[2]), int(s.Size[1])
n := s.Size[0] * s.Size[1] * s.Size[2]
if n == 0 {
return
}
s.blocks = s.Structure.BlockIndices[0]
s.blocksPtr = unsafe.Pointer(&s.blocks[0])
if len(s.Structure.BlockIndices) == 1 {
// No liquids present, but for the sake of performance we'll add them
// anyway. This means we can always assume they exist.
liquids := make([]int32, n)
for i := range liquids {
liquids[i] = -1
}
s.Structure.BlockIndices = append(s.Structure.BlockIndices, liquids)
}
s.liquids = s.Structure.BlockIndices[1]
s.liquidsPtr = unsafe.Pointer(&s.liquids[0])
s.palettePtr = unsafe.Pointer(&s.parsedPalette[0])
}
// Set sets the block at a specific position within the structure to the world.Block passed. Set will panic
// if the x, y or z exceed the bounds of the structure. The world.Liquid passed may be nil to avoid waterlogging the
// block.
func (s *structure) Set(x, y, z int, b world.Block, liq world.Liquid) {
offset := (x * s.l * s.h) + (y * s.l) + z
s.blocks[offset] = s.ptrFor(b)
if nbtBlock, ok := b.(world.NBTer); ok {
s.palette.BlockPositionData[strconv.Itoa(offset)] = blockPositionData{BlockEntityData: nbtBlock.EncodeNBT()}
}
if liq == nil {
// No liquid passed to be placed in the background.
s.liquids[offset] = -1
return
}
s.liquids[offset] = s.ptrFor(liq)
}
// ptrFor looks up a palette pointer for the world.Block passed. If not found, it adds the block to the palette of the
// structure and returns a pointer to the new value in the palette.
func (s *structure) ptrFor(b world.Block) int32 {
name, properties := b.EncodeBlock()
ptr := s.lookup(name, properties)
if ptr == -1 {
// No pointer found, add a new block to the palette.
ptr = int32(len(s.palette.BlockPalette))
bl := block{
Name: name,
States: properties,
Version: chunk.CurrentBlockVersion,
}
s.palette.BlockPalette = append(s.palette.BlockPalette, bl)
s.parsePaletteEntry(bl)
// Update the palette pointer because appending might have changed the
// location of the underlying array.
s.palettePtr = unsafe.Pointer(&s.parsedPalette[0])
}
return ptr
}
// At returns the block at the x, y and z passed in the structure.
func (s *structure) At(x, y, z int, _ func(x int, y int, z int) world.Block) (world.Block, world.Liquid) {
offset := (x * s.l * s.h) + (y * s.l) + z
index := *(*int32)(unsafe.Pointer(uintptr(s.blocksPtr) + uintptr(offset<<2)))
if index == -1 {
// Minecraft structures use -1 to indicate that there is no block at a position.
return nil, nil
}
entry := *(*parsedBlock)(unsafe.Pointer(uintptr(s.palettePtr) + uintptr(index)*sizeOfBlock))
b := entry.b
if entry.hasNBT {
if nbtData, ok := s.palette.BlockPositionData[strconv.Itoa(offset)]; ok {
b = entry.b.(world.NBTer).DecodeNBT(nbtData.BlockEntityData).(world.Block)
}
}
index = *(*int32)(unsafe.Pointer(uintptr(s.liquidsPtr) + uintptr(offset<<2)))
if index == -1 {
// Minecraft structures use -1 to indicate that there is no block at a position.
return b, nil
}
en := *(*parsedBlock)(unsafe.Pointer(uintptr(s.palettePtr) + uintptr(index)*sizeOfBlock))
return b, en.b.(world.Liquid)
}
// parsePalette parses the palette of the structure so that blocks can be looked up more quickly using At.
func (s *structure) parsePalette() {
s.parsedPalette = make([]parsedBlock, 0, len(s.palette.BlockPalette))
for _, bl := range s.palette.BlockPalette {
s.parsePaletteEntry(bl)
}
}
// parsePaletteEntry parses a single palette entry and adds it to the parsed palette.
func (s *structure) parsePaletteEntry(bl block) {
upgraded := blockupgrader.Upgrade(blockupgrader.BlockState{
Name: bl.Name,
Properties: bl.States,
Version: bl.Version,
})
b, _ := world.BlockByName(upgraded.Name, upgraded.Properties)
_, n := b.(world.NBTer)
s.parsedPalette = append(s.parsedPalette, parsedBlock{
b: b,
hasNBT: n,
})
}
// lookup looks up the world.Block passed in the palette of the structure. If not found, the value returned is
// -1.
func (s *structure) lookup(name string, properties map[string]interface{}) int32 {
for index, block := range s.palette.BlockPalette {
if block.Name == name {
allEqual := true
for k, v := range block.States {
if bVal, _ := properties[k]; bVal != v {
allEqual = false
break
}
}
if allEqual {
return int32(index)
}
}
}
return -1
}
// check verifies if the structure is valid. It returns an error if anything in the structure was found to be
// incorrect.
func (s *structure) check() error {
if s.FormatVersion != version {
return fmt.Errorf("unsupported format version %v: expected version %v", s.FormatVersion, version)
}
if l := len(s.Size); l != 3 {
return fmt.Errorf("structure size must have 3 values, but got %v (%v)", l, s.Size)
}
if l := len(s.Origin); l != 3 {
return fmt.Errorf("structure origin must have 3 values, but got %v (%v)", l, s.Origin)
}
if s.Structure.Palettes == nil {
s.Structure.Palettes = map[string]palette{}
}
if len(s.Structure.BlockIndices) == 0 {
return fmt.Errorf("structure has no blocks in it")
}
if len(s.Structure.Palettes) == 0 {
return fmt.Errorf("structure has no palettes in it")
}
size := int(s.Size[0] * s.Size[1] * s.Size[2])
if size <= 0 {
return fmt.Errorf("structure has a total size of 0 blocks or less (%v)", size)
}
for i, indices := range s.Structure.BlockIndices {
if len(indices) != size {
return fmt.Errorf("structure is %vx%vx%v and should have %v blocks, but got only %v in storage %v", s.Size[0], s.Size[1], s.Size[2], size, len(indices), i)
}
}
paletteLen := -1
for _, p := range s.Structure.Palettes {
if paletteLen == -1 {
paletteLen = len(p.BlockPalette)
continue
}
if len(p.BlockPalette) != paletteLen {
return fmt.Errorf("all palettes must have the same length, but got one with length %v and one with length %v", paletteLen, len(p.BlockPalette))
}
}
return nil
}
// structureData holds the actual data of the structure. This includes both blocks and entities.
type structureData struct {
// BlockIndices holds the actual block data. This is a two-dimensional slice, where the first indicates
// the layer that these blocks are in. The int32s held are pointers to blocks in the palette used. Note
// that an index may be -1 to indicate that neither air nor any other block should be placed on this
// position.
BlockIndices [][]int32 `nbt:"block_indices"`
Entities []map[string]interface{} `nbt:"entities"`
Palettes map[string]palette `nbt:"palette"`
}
// palette represents the palette of a single structure.
type palette struct {
BlockPalette []block `nbt:"block_palette"`
BlockPositionData map[string]blockPositionData `nbt:"block_position_data"`
}
// block represents a single block entry, holding a block name and its states. These entries also hold a
// version.
type block struct {
Name string `nbt:"name"`
States map[string]interface{} `nbt:"states"`
Version int32 `nbt:"version"`
}
// blockPositionData holds additional data associated with specific block positions in the structure. At the
// moment, these appear to be limited to block entity data.
type blockPositionData struct {
BlockEntityData map[string]interface{} `nbt:"block_entity_data"`
}