/
generateChunks.go
422 lines (344 loc) · 10.5 KB
/
generateChunks.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package main
import (
"bytes"
"compress/zlib"
"encoding/binary"
nbt "github.com/rphsoftware/go.nbt"
"github.com/rphsoftware/mcpackedarray"
"io/ioutil"
"math"
"runtime"
"sync"
)
type HeightMap struct {
MotionBlocking []uint64 `nbt:"MOTION_BLOCKING"`
MotionBlockingNoLeaves []int64 `nbt:"MOTION_BLOCKING_NO_LEAVES"`
OceanFloor []int64 `nbt:"OCEAN_FLOOR"`
OceanFloorWG []int64 `nbt:"OCEAN_FLOOR_WG"`
WorldSurface []int64 `nbt:"WORLD_SURFACE"`
WorldSurfaceWG []int64 `nbt:"WORLD_SURFACE_WG"`
}
type CleanHeightMap struct {
MotionBlocking [36]uint64 `nbt:"MOTION_BLOCKING"`
}
type PaletteEntry struct {
Name string
Properties map[string]interface{}
}
type ChunkSection struct {
Y byte
Palette []PaletteEntry
BlockLight []byte
BlockStates []uint64
SkyLight []byte
}
type NBTChunkInner struct {
Heightmaps HeightMap `nbt:"Heightmaps"`
Status string
ZPos uint32 `nbt:"zPos"`
XPos uint32 `nbt:"xPos"`
LastUpdate uint64
Biomes []uint32
InhabitedTime uint64
TileEntities []interface{}
Entities []interface{}
IsLightOn byte `nbt:"isLightOn"`
TileTicks []interface{}
Sections []ChunkSection
PostProcessing []interface{}
Structures map[string]interface{}
LiquidTicks []interface{}
}
type NBTChunk struct {
Level NBTChunkInner `nbt:"Level"`
DataVersion uint32
}
type CleanSection struct {
data []uint64
palette map[int]string
paletteRev map[string]int
}
type ChunkEmissionJob struct {
x int
z int
reg int
data []byte
}
// TODO: Multithread the shit out of this!!!!
var size int = 0
//
//func parseChunkAndEmitPacket(x int, z int, reg int, chunkData NBTChunk) { // Test code
// finalChunk := make([]byte, 316155)
// cache := make([]byte, 64)
// var packetPointer = 0
//
// // Write chunk X
// binary.BigEndian.PutUint32(cache, uint32(x))
// copy(finalChunk[packetPointer:packetPointer+4], cache)
// packetPointer += 4
//
// // Write chunk Z
// binary.BigEndian.PutUint32(cache, uint32(z))
// copy(finalChunk[packetPointer:packetPointer+4], cache)
// packetPointer += 4
//
// // Full chunk flag
// finalChunk[packetPointer] = 1
// packetPointer += 1
//
// bitMask := 63 // 111111 (6 cubes)
//
// cache, size = writeVarInt(bitMask)
// copy(finalChunk[packetPointer:packetPointer+size], cache)
// packetPointer += size
//
// // Prepare height map
// hmPa := mcpackedarray.NewPackedArray(9, 256)
// for i := int32(0); i < 256; i++ {
// hmPa.Set(i, 95)
// }
//
// hmBytes := hmPa.Serialise()
//
// hm := new(CleanHeightMap)
// for i := 0; i < 36; i++ {
// hm.MotionBlocking[i] = binary.BigEndian.Uint64(hmBytes[i * 8 : i * 8 + 8])
// }
//
// fmt.Println(hm.MotionBlocking)
//
// var hmData bytes.Buffer
// nbt.Marshal(nbt.Uncompressed, &hmData, hm)
//
// tmp := hmData.Bytes()
// copy(finalChunk[packetPointer:], tmp)
// packetPointer += len(tmp)
//
// fmt.Println(hex.Dump(tmp))
//}
func parseChunkAndEmitPacket(x int, z int, reg int, chunkData NBTChunk) { // 1.13-1.15 code!!
finalChunk := make([]byte, 316155)
cache := make([]byte, 64)
var packetPointer = 0
// Write chunk X
binary.BigEndian.PutUint32(cache, uint32(x))
copy(finalChunk[packetPointer:packetPointer+4], cache)
packetPointer += 4
// Write chunk Z
binary.BigEndian.PutUint32(cache, uint32(z))
copy(finalChunk[packetPointer:packetPointer+4], cache)
packetPointer += 4
// Full chunk flag
finalChunk[packetPointer] = 1
packetPointer += 1
// Bit-mask
bitMask := 0
for _, section := range chunkData.Level.Sections {
if section.Y < 17 && len(section.Palette) > 0 {
bitMask += int(math.Pow(2, float64(section.Y)))
}
}
cache, size = writeVarInt(bitMask)
copy(finalChunk[packetPointer:packetPointer+size], cache)
packetPointer += size
// Prepare height map
// TODO: Optimize the appends.........
hm := new(CleanHeightMap)
for i, value := range chunkData.Level.Heightmaps.MotionBlocking {
hm.MotionBlocking[i] = value
}
var hmData bytes.Buffer
nbt.Marshal(nbt.Uncompressed, &hmData, hm)
tmp := hmData.Bytes()
copy(finalChunk[packetPointer:], tmp)
packetPointer += len(tmp)
// Allocate biomes
for _, value := range chunkData.Level.Biomes {
a := value >> 24
finalChunk[packetPointer] = byte(a)
value -= a * 16777216
a = value >> 16
finalChunk[packetPointer+1] = byte(a)
value -= a * 65536
a = value >> 8
finalChunk[packetPointer+2] = byte(a)
value -= a * 256
finalChunk[packetPointer+3] = byte(value)
packetPointer += 4
}
// TODO: Continue optim under this line.
// Actually generate chunk information
// Issue: we must write data size before we write chunk data
// Solution: Allocate a NEW chunk data object in accordance with our previous findings!
fullChunkData := make([]byte, 311392)
fullChunkDataPtr := 0
for _, section := range chunkData.Level.Sections {
if section.Y < 17 && len(section.Palette) > 0 {
// Read palette
sectionBlocks := make([]string, len(section.Palette))
palettePtr := 0
absolutePalette := false
mappedPaletteEntrySize := 4
for _, paletteEntry := range section.Palette {
props := make([]string, len(paletteEntry.Properties))
propPtr := 0
for i, v := range paletteEntry.Properties {
if valu, ok := v.(string); ok {
props[propPtr] = i + "=" + valu
}
propPtr++
}
sectionBlocks[palettePtr] = makeBlockStateIdentifier(paletteEntry.Name, props)
palettePtr++
}
if len(sectionBlocks) <= 16 {
absolutePalette = false
mappedPaletteEntrySize = 4
} else if len(sectionBlocks) > 16 && len(sectionBlocks) <= 256 {
absolutePalette = false
mappedPaletteEntrySize = int(math.Ceil(math.Log2(float64(len(sectionBlocks)))))
} else {
absolutePalette = true
mappedPaletteEntrySize = 14
}
// Load block states into a virtual data object
byteData := make([]byte, len(section.BlockStates)*8)
byteBit := make([]byte, 8)
for v := 0; v < len(section.BlockStates); v++ {
binary.BigEndian.PutUint64(byteBit, section.BlockStates[v])
byteData[v*8+0] = byteBit[0]
byteData[v*8+1] = byteBit[1]
byteData[v*8+2] = byteBit[2]
byteData[v*8+3] = byteBit[3]
byteData[v*8+4] = byteBit[4]
byteData[v*8+5] = byteBit[5]
byteData[v*8+6] = byteBit[6]
byteData[v*8+7] = byteBit[7]
}
bitsPerEntrySource := math.Ceil(math.Log2(float64(len(section.Palette))))
if bitsPerEntrySource < 4 {
bitsPerEntrySource = 4
}
blockStates := mcpackedarray.PackedArrayFromData(byteData, byte(bitsPerEntrySource))
// Calculate non-air blocks
var nonAirBlocks int16 = 0
for _, blockState := range blockStates.Entries {
if blockState != 0 {
nonAirBlocks++
}
}
topByte := byte(nonAirBlocks >> 8)
lowerByte := byte((nonAirBlocks) - (int16(topByte) * 256))
fullChunkData[fullChunkDataPtr] = topByte
fullChunkData[fullChunkDataPtr+1] = lowerByte
fullChunkData[fullChunkDataPtr+2] = byte(mappedPaletteEntrySize)
fullChunkDataPtr += 3
if absolutePalette == false {
// Write palette
cache, size = writeVarInt(len(sectionBlocks))
copy(fullChunkData[fullChunkDataPtr:fullChunkDataPtr+size], cache)
fullChunkDataPtr += size
for _, paletteEntry := range sectionBlocks {
registryId := registry[reg].blockStates[paletteEntry]
cache, size = writeVarInt(registryId)
copy(fullChunkData[fullChunkDataPtr:fullChunkDataPtr+size], cache)
fullChunkDataPtr += size
}
}
// Calculate size of data array
sizeOfData := (mappedPaletteEntrySize * 512) / 8
cache, size := writeVarInt(int(sizeOfData))
copy(fullChunkData[fullChunkDataPtr:fullChunkDataPtr+size], cache)
fullChunkDataPtr += size
// Create the actual compacted array for the chunk data
chunkBlockStates := mcpackedarray.NewPackedArray(byte(mappedPaletteEntrySize), 4096)
for i := int32(0); i < 4096; i++ {
if absolutePalette == true {
chunkBlockStates.Set(i, uint32(registry[reg].blockStates[sectionBlocks[blockStates.Get(i)]]))
} else {
chunkBlockStates.Set(i, blockStates.Get(i))
}
}
tmp = chunkBlockStates.Serialise()
copy(fullChunkData[fullChunkDataPtr:fullChunkDataPtr+len(tmp)], tmp)
fullChunkDataPtr += len(tmp)
}
}
cache, size = writeVarInt(fullChunkDataPtr)
copy(finalChunk[packetPointer:packetPointer+size], cache)
packetPointer += size
copy(finalChunk[packetPointer:packetPointer+fullChunkDataPtr], fullChunkData)
packetPointer += fullChunkDataPtr
finalChunk[packetPointer] = 0
packetPointer += 1
finalChunk = finalChunk[:packetPointer]
packet := make([]byte, 0)
packet = append(packet, 0)
var compressed bytes.Buffer
compressedWriter := zlib.NewWriter(&compressed)
compressedWriter.Write(finalChunk)
compressedWriter.Close()
chunkLock.Lock()
// All the thread-unsafe IO operations with the global chunk map
if _, ok := chunkCache[reg]; ok != true {
chunkCache[reg] = make(map[int]map[int][]byte)
}
if _, ok := chunkCache[reg][x]; ok != true {
chunkCache[reg][x] = make(map[int][]byte)
}
chunkCache[reg][x][z] = finalChunk
size += len(chunkCache[reg][x][z])
chunkLock.Unlock()
}
func runTaskSet(wg *sync.WaitGroup, jobs []ChunkEmissionJob) {
for _, entry := range jobs {
r, _ := zlib.NewReader(bytes.NewReader(entry.data))
chunkNbtData, _ := ioutil.ReadAll(r)
var aa NBTChunk
_ = nbt.Unmarshal(nbt.Uncompressed, bytes.NewReader(chunkNbtData), &aa)
// Generate chunk packet itself
parseChunkAndEmitPacket(entry.x, entry.z, entry.reg, aa)
r.Close()
aa = NBTChunk{}
}
wg.Done()
}
func chunkGeneration(compressedChunks map[int]map[int][]byte) {
for a, _ := range registry {
var jobList = make([]ChunkEmissionJob, len(compressedChunks)*len(compressedChunks[0]))
var ptr = 0
for i := 0; i < len(compressedChunks); i++ {
for j := 0; j < len(compressedChunks[i]); j++ {
index := i*len(compressedChunks[i]) + j
index += 1
entry := compressedChunks[i][j]
compressionScheme := entry[4]
if compressionScheme == 2 {
nbtData := entry[5:]
job := ChunkEmissionJob{
x: i,
z: j,
reg: a,
data: nbtData,
}
jobList[ptr] = job
ptr++
} else {
panic("!!!! UNSUPPORTED COMPRESSION SCHEME !!!! THIS MAP IS NOT STANDARD !!!!")
}
}
}
log("> Running jobs")
threadsToStart := runtime.NumCPU()
var wg sync.WaitGroup
for i := threadsToStart; i >= 1; i-- {
part := jobList[0:int(math.Floor(float64(len(jobList)/i)))]
jobList = jobList[int(math.Floor(float64(len(jobList)/i))):]
wg.Add(1)
go runTaskSet(&wg, part)
}
log("Tasks started")
wg.Wait()
log("[CHUNK GENERATION] Done for registry", a)
}
}