2014-09-09 17:48:39 +02:00
|
|
|
// Copyright 2014 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/zlib"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-09-09 23:33:53 +02:00
|
|
|
"log"
|
2014-09-09 17:48:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrMapContentSizeMismatch = errors.New("Content size does not match.")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mapBlockSize = 16
|
|
|
|
nodeCount = mapBlockSize * mapBlockSize * mapBlockSize
|
|
|
|
)
|
|
|
|
|
|
|
|
type DecodedBlock struct {
|
|
|
|
Version byte
|
|
|
|
MapContent []byte
|
|
|
|
AirId int
|
|
|
|
IgnoreId int
|
2014-09-09 23:33:53 +02:00
|
|
|
IndexMap map[int]int
|
2014-09-09 17:48:39 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 08:35:20 +02:00
|
|
|
// The content of the map and the meta data are compressed with zlib.
|
|
|
|
// Unfortunately the byte length of this two structures are not stored
|
|
|
|
// explicitly in the block data. To access the informations behind
|
|
|
|
// them (e.g. the node id mappings) we have to count the bytes consumed
|
|
|
|
// by the zlib reader and continue our extraction process behind this
|
|
|
|
// offset. PosBuf implements such a counting reader source.
|
2014-09-09 17:48:39 +02:00
|
|
|
type PosBuf struct {
|
|
|
|
Data []byte
|
|
|
|
Pos int
|
|
|
|
}
|
|
|
|
|
2014-09-09 23:33:53 +02:00
|
|
|
func NewDecodedBlock(data []byte, nameIndex map[string]int) (db *DecodedBlock, err error) {
|
2014-09-09 17:48:39 +02:00
|
|
|
version := data[0]
|
|
|
|
|
|
|
|
contentWidth := int(data[2])
|
|
|
|
paramsWidth := int(data[3])
|
|
|
|
|
|
|
|
uncompressedLen := nodeCount * (contentWidth + paramsWidth)
|
|
|
|
|
|
|
|
offset := 2
|
|
|
|
if version >= 22 {
|
|
|
|
offset = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := NewPosBuf(data[offset:])
|
|
|
|
var zr io.ReadCloser
|
|
|
|
if zr, err = zlib.NewReader(buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapContent []byte
|
2014-09-10 17:33:13 +02:00
|
|
|
mapContent, err = ioutil.ReadAll(zr)
|
|
|
|
zr.Close()
|
|
|
|
if err != nil {
|
2014-09-09 17:48:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if uncompressedLen != len(mapContent) {
|
|
|
|
err = ErrMapContentSizeMismatch
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += buf.Pos
|
|
|
|
buf.Pos = 0
|
|
|
|
buf.Data = data[offset:]
|
|
|
|
|
|
|
|
if zr, err = zlib.NewReader(buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard the meta data.
|
|
|
|
_, err = io.Copy(ioutil.Discard, zr)
|
|
|
|
zr.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += buf.Pos
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case version <= 21:
|
|
|
|
offset += 2
|
|
|
|
case version == 23:
|
|
|
|
offset++
|
|
|
|
case version == 24:
|
|
|
|
ver := data[offset]
|
|
|
|
offset++
|
|
|
|
if ver == 1 {
|
|
|
|
num := int(binary.BigEndian.Uint16(data[offset:]))
|
|
|
|
offset += 2 + 10*num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset++
|
|
|
|
numStaticObjects := int(binary.BigEndian.Uint16(data[offset:]))
|
|
|
|
offset += 2
|
|
|
|
for i := 0; i < numStaticObjects; i++ {
|
|
|
|
offset += 13
|
|
|
|
dataSize := int(binary.BigEndian.Uint16(data[offset:]))
|
|
|
|
offset += dataSize + 2
|
|
|
|
}
|
|
|
|
offset += 4
|
|
|
|
|
|
|
|
airId, ignoreId := -1, -1
|
2014-09-09 23:33:53 +02:00
|
|
|
indexMap := make(map[int]int)
|
2014-09-09 17:48:39 +02:00
|
|
|
if version >= 22 {
|
|
|
|
offset++
|
|
|
|
numMappings := int(binary.BigEndian.Uint16(data[offset:]))
|
|
|
|
offset += 2
|
|
|
|
for i := 0; i < numMappings; i++ {
|
|
|
|
nodeId := int(binary.BigEndian.Uint16(data[offset:]))
|
|
|
|
offset += 2
|
|
|
|
nameLen := int(binary.BigEndian.Uint16(data[offset:]))
|
|
|
|
offset += 2
|
|
|
|
name := string(data[offset : offset+nameLen])
|
|
|
|
offset += nameLen
|
|
|
|
switch name {
|
|
|
|
case "air":
|
|
|
|
airId = nodeId
|
|
|
|
case "ignore":
|
|
|
|
ignoreId = nodeId
|
|
|
|
default:
|
2014-09-09 23:33:53 +02:00
|
|
|
if index, found := nameIndex[name]; found {
|
|
|
|
indexMap[nodeId] = index
|
|
|
|
} else {
|
|
|
|
log.Printf("Missing color entry for %s.", name)
|
|
|
|
}
|
2014-09-09 17:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db = &DecodedBlock{
|
|
|
|
Version: version,
|
|
|
|
MapContent: mapContent,
|
|
|
|
AirId: airId,
|
|
|
|
IgnoreId: ignoreId,
|
2014-09-09 23:33:53 +02:00
|
|
|
IndexMap: indexMap}
|
2014-09-09 17:48:39 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:21:51 +02:00
|
|
|
func (db *DecodedBlock) Content(x, y, z int) (content int, found bool) {
|
2014-09-10 01:21:55 +02:00
|
|
|
pos := z<<8 + y<<4 + x
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case db.Version >= 24:
|
|
|
|
pos <<= 1
|
2014-09-10 08:21:51 +02:00
|
|
|
content = int(db.MapContent[pos])<<8 | int(db.MapContent[pos+1])
|
2014-09-10 01:21:55 +02:00
|
|
|
case db.Version >= 20:
|
|
|
|
if db.MapContent[pos] <= 0x80 {
|
2014-09-10 08:21:51 +02:00
|
|
|
content = int(db.MapContent[pos])
|
|
|
|
} else {
|
|
|
|
content = int(db.MapContent[pos])<<4 | int(db.MapContent[pos+0x2000])>>4
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
|
|
|
default:
|
2014-09-10 08:21:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if content != db.AirId && content != db.IgnoreId {
|
|
|
|
content, found = db.IndexMap[content]
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
2014-09-10 08:21:51 +02:00
|
|
|
return
|
2014-09-10 01:21:55 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 17:48:39 +02:00
|
|
|
func NewPosBuf(data []byte) *PosBuf {
|
|
|
|
return &PosBuf{Data: data}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *PosBuf) Read(p []byte) (int, error) {
|
|
|
|
pl := len(p)
|
|
|
|
ml := len(pb.Data)
|
|
|
|
if pb.Pos >= ml {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
rest := ml - pb.Pos
|
|
|
|
if pl > rest {
|
|
|
|
copy(p, pb.Data[pb.Pos:])
|
|
|
|
pb.Pos = ml
|
|
|
|
return rest, io.EOF
|
|
|
|
}
|
|
|
|
copy(p, pb.Data[pb.Pos:pb.Pos+pl])
|
|
|
|
pb.Pos += pl
|
|
|
|
return pl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *PosBuf) ReadByte() (byte, error) {
|
|
|
|
if pb.Pos >= len(pb.Data) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
c := pb.Data[pb.Pos]
|
|
|
|
pb.Pos++
|
|
|
|
return c, nil
|
|
|
|
}
|