This commit is contained in:
Sascha L. Teichmann
2014-10-21 01:11:37 +02:00
7 changed files with 298 additions and 255 deletions

View File

@ -76,7 +76,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
btc.yOrder.Reset()
drawBlock := func(block *Block) {
if err := btc.yOrder.RenderBlock(block, btc.colors.NameIndex); err != nil {
if err := btc.yOrder.RenderBlock(block, btc.colors); err != nil {
log.Printf("WARN: rendering block failed: %s", err)
}
}
@ -109,7 +109,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
if err := btc.client.QueryCuboid(query, drawBlock); err != nil {
return err
}
if err := btc.yOrder.Drain(btc.colors.NameIndex); err != nil {
if err := btc.yOrder.Drain(btc.colors); err != nil {
log.Printf("WARN: rendering block failed: %s", err)
}
}

View File

@ -47,6 +47,7 @@ type (
MapContent []byte
AirId int32
IgnoreId int32
AirOnly bool
IndexMap map[int32]int32
}
)
@ -62,7 +63,7 @@ type posBuf struct {
Pos int
}
func NewDecodedBlock(data []byte, nameIndex map[string]int32) (db *DecodedBlock, err error) {
func NewDecodedBlock(data []byte, colors *Colors) (db *DecodedBlock, err error) {
version := data[0]
contentWidth := int(data[2])
@ -136,6 +137,7 @@ func NewDecodedBlock(data []byte, nameIndex map[string]int32) (db *DecodedBlock,
airId, ignoreId := int32(-1), int32(-1)
indexMap := make(map[int32]int32)
var airOnly bool
if version >= 22 {
offset++
numMappings := int(binary.BigEndian.Uint16(data[offset:]))
@ -153,13 +155,14 @@ func NewDecodedBlock(data []byte, nameIndex map[string]int32) (db *DecodedBlock,
case "ignore":
ignoreId = nodeId
default:
if index, found := nameIndex[name]; found {
if index, found := colors.NameIndex[name]; found {
indexMap[nodeId] = index
} else {
log.Printf("Missing color entry for %s.", name)
}
}
}
airOnly = airId != -1 && len(indexMap) == 0
}
db = &DecodedBlock{
@ -167,6 +170,7 @@ func NewDecodedBlock(data []byte, nameIndex map[string]int32) (db *DecodedBlock,
MapContent: mapContent,
AirId: airId,
IgnoreId: ignoreId,
AirOnly: airOnly,
IndexMap: indexMap}
return

View File

@ -9,12 +9,33 @@ import (
"fmt"
"image/color"
"os"
"sort"
"strings"
)
type Colors struct {
Colors []color.RGBA
NameIndex map[string]int32
Colors []color.RGBA
NameIndex map[string]int32
Transparent int
}
type namedColor struct {
name string
color color.RGBA
}
type sortByAlpha []namedColor
func (colors sortByAlpha) Less(i, j int) bool {
return colors[i].color.A < colors[j].color.A
}
func (colors sortByAlpha) Len() int {
return len(colors)
}
func (colors sortByAlpha) Swap(i, j int) {
colors[i], colors[j] = colors[j], colors[i]
}
func ParseColors(filename string) (colors *Colors, err error) {
@ -25,8 +46,7 @@ func ParseColors(filename string) (colors *Colors, err error) {
}
defer file.Close()
nameIndex := make(map[string]int32)
cols := make([]color.RGBA, 0, 2200)
cols := make([]namedColor, 0, 2200)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
@ -38,12 +58,26 @@ func ParseColors(filename string) (colors *Colors, err error) {
var name string
if n, _ := fmt.Sscanf(
line, "%s %d %d %d %d", &name, &c.R, &c.G, &c.B, &c.A); n > 0 {
idx := int32(len(cols))
cols = append(cols, c)
nameIndex[name] = idx
cols = append(cols, namedColor{name: name, color: c})
}
}
err = scanner.Err()
colors = &Colors{Colors: cols, NameIndex: nameIndex}
// Sort transparent colors to front. Makes it easier to figure out
// if an index corresponds to a transparent color (i < Transparent).
sort.Sort(sortByAlpha(cols))
cs := make([]color.RGBA, len(cols))
nameIndex := make(map[string]int32, len(cols))
transparent := 0
for i, nc := range cols {
if nc.color.A < 0xff {
transparent++
}
cs[i] = nc.color
nameIndex[nc.name] = int32(i)
}
colors = &Colors{Colors: cs, NameIndex: nameIndex, Transparent: transparent}
return
}

View File

@ -58,18 +58,18 @@ func copyData(data []byte) []byte {
return ndata
}
func (yo *YOrder) RenderBlock(block *Block, nameIndex map[string]int32) (err error) {
func (yo *YOrder) RenderBlock(block *Block, colors *Colors) (err error) {
var nblock *Block
if len(yo.blocks) == yo.capacity {
oblock := yo.blocks[0]
if oblock.Coord.Y < block.Coord.Y {
// New one is above highest old. Directly render new.
err = yo.Renderer.RenderBlock(block, nameIndex)
err = yo.Renderer.RenderBlock(block, colors)
return
}
// Render old one. Store copy of new in heap.
heap.Pop(yo)
err = yo.Renderer.RenderBlock(oblock, nameIndex)
err = yo.Renderer.RenderBlock(oblock, colors)
l := len(block.Data)
if cap(oblock.Data) < l {
oblock.Data = make([]byte, l, max(l, 8*1024))
@ -87,9 +87,9 @@ func (yo *YOrder) RenderBlock(block *Block, nameIndex map[string]int32) (err err
return
}
func (yo *YOrder) Drain(nameIndex map[string]int32) (err error) {
func (yo *YOrder) Drain(colors *Colors) (err error) {
for len(yo.blocks) > 0 {
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*Block), nameIndex); err != nil {
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*Block), colors); err != nil {
return
}
}
@ -162,7 +162,7 @@ func (r *Renderer) IsFilled() bool {
return true
}
func (r *Renderer) RenderBlock(block *Block, nameIndex map[string]int32) (err error) {
func (r *Renderer) RenderBlock(block *Block, colors *Colors) (err error) {
bx := block.Coord.X - r.xOfs
bz := block.Coord.Z - r.zOfs
@ -178,7 +178,12 @@ func (r *Renderer) RenderBlock(block *Block, nameIndex map[string]int32) (err er
// Decoding is pretty expensive so do it that late.
var db *DecodedBlock
if db, err = NewDecodedBlock(block.Data, nameIndex); err != nil {
if db, err = NewDecodedBlock(block.Data, colors); err != nil {
return
}
if db.AirOnly {
r.Rejected++
return
}