2014-09-20 15:21:01 +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 common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
2015-07-22 01:11:14 +02:00
|
|
|
"io/ioutil"
|
2014-09-20 15:21:01 +02:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tileWidth = 18
|
|
|
|
tileHeight = 18
|
|
|
|
yOrderCapacity = 512
|
|
|
|
)
|
|
|
|
|
2015-07-27 19:03:47 +02:00
|
|
|
const (
|
|
|
|
MaxHeight = 1934
|
|
|
|
MinHeight = -1934
|
|
|
|
)
|
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
// To scan the whole height in terms of the y coordinate
|
|
|
|
// the database is queried in height units defined in the tileDepths table.
|
|
|
|
var tileDepths = [...][2]int16{
|
2015-07-27 19:03:47 +02:00
|
|
|
{1024, MaxHeight},
|
2014-09-20 15:21:01 +02:00
|
|
|
{256, 1023},
|
|
|
|
{128, 255},
|
|
|
|
{64, 127},
|
|
|
|
{32, 63},
|
|
|
|
{16, 31},
|
|
|
|
{8, 15},
|
|
|
|
{4, 7},
|
|
|
|
{2, 3},
|
|
|
|
{0, 1},
|
|
|
|
{-1, 0},
|
|
|
|
{-4, -2},
|
|
|
|
{-8, -5},
|
|
|
|
{-16, -9},
|
|
|
|
{-32, -17},
|
|
|
|
{-64, -33},
|
|
|
|
{-128, -65},
|
|
|
|
{-256, -129},
|
|
|
|
{-1024, -257},
|
2015-07-27 19:03:47 +02:00
|
|
|
{MinHeight, -1025}}
|
2014-09-20 15:21:01 +02:00
|
|
|
|
2015-07-22 01:11:14 +02:00
|
|
|
var BackgroundColor = color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
|
|
|
|
2016-05-08 17:33:51 +02:00
|
|
|
type BaseTileUpdateFunc func(x, y int, hash []byte) bool
|
2016-05-08 15:38:50 +02:00
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
type BaseTileCreator struct {
|
2022-02-27 21:02:16 +01:00
|
|
|
client DBClient
|
2017-04-15 13:08:19 +02:00
|
|
|
colors *Colors
|
|
|
|
renderer *Renderer
|
|
|
|
yOrder *YOrder
|
|
|
|
yMin int16
|
|
|
|
yMax int16
|
|
|
|
baseDir string
|
2015-07-22 01:11:14 +02:00
|
|
|
emptyImage []byte
|
2016-04-23 16:45:33 +02:00
|
|
|
bg color.RGBA
|
2014-09-20 15:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseTileCreator(
|
2022-02-27 21:02:16 +01:00
|
|
|
client DBClient,
|
2014-09-20 15:21:01 +02:00
|
|
|
colors *Colors,
|
2016-04-23 16:45:33 +02:00
|
|
|
bg color.RGBA,
|
2015-07-27 19:03:47 +02:00
|
|
|
yMin, yMax int16,
|
2014-10-26 18:36:47 +01:00
|
|
|
transparent bool,
|
2017-03-06 15:43:30 +01:00
|
|
|
baseDir string) *BaseTileCreator {
|
2017-04-15 13:08:19 +02:00
|
|
|
|
2014-10-26 18:36:47 +01:00
|
|
|
renderer := NewRenderer(tileWidth, tileHeight, transparent)
|
2015-07-27 19:03:47 +02:00
|
|
|
yMin, yMax = Order16(yMin, yMax)
|
2017-03-01 15:05:51 +01:00
|
|
|
btc := &BaseTileCreator{
|
2014-09-20 15:21:01 +02:00
|
|
|
client: client,
|
|
|
|
colors: colors,
|
2016-04-23 16:45:33 +02:00
|
|
|
bg: bg,
|
2014-09-20 15:21:01 +02:00
|
|
|
renderer: renderer,
|
2015-07-27 19:03:47 +02:00
|
|
|
yMin: yMin,
|
|
|
|
yMax: yMax,
|
2014-09-20 15:21:01 +02:00
|
|
|
baseDir: baseDir,
|
2017-03-06 15:43:30 +01:00
|
|
|
}
|
2017-03-01 15:05:51 +01:00
|
|
|
btc.yOrder = NewYOrder(btc.renderBlock, yOrderCapacity)
|
|
|
|
return btc
|
2014-09-20 15:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (btc *BaseTileCreator) Close() error {
|
|
|
|
return btc.client.Close()
|
|
|
|
}
|
|
|
|
|
2017-03-01 15:05:51 +01:00
|
|
|
// renderBlock is a callback to draw a block with a YOrder.
|
|
|
|
func (btc *BaseTileCreator) renderBlock(block *Block) error {
|
|
|
|
return btc.renderer.RenderBlock(block, btc.colors)
|
|
|
|
}
|
|
|
|
|
|
|
|
// blockLoaded is a callback for RedisClient.QueryCuboid.
|
2017-03-03 23:00:29 +01:00
|
|
|
func (btc *BaseTileCreator) blockLoaded(block *Block) *Block {
|
|
|
|
block, err := btc.yOrder.RenderBlock(block)
|
|
|
|
if err != nil {
|
2017-02-26 12:57:38 +01:00
|
|
|
log.Printf("WARN: rendering block failed: %s\n", err)
|
|
|
|
}
|
2017-03-03 23:00:29 +01:00
|
|
|
return block
|
2017-02-26 12:57:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-06 15:43:30 +01:00
|
|
|
func (btc *BaseTileCreator) RenderArea(x, z int16) error {
|
2014-09-20 15:21:01 +02:00
|
|
|
btc.renderer.Reset()
|
|
|
|
btc.renderer.SetPos(x, z)
|
|
|
|
btc.yOrder.Reset()
|
|
|
|
|
|
|
|
var c1, c2 Coord
|
|
|
|
|
2017-02-26 17:01:15 +01:00
|
|
|
nareas := make([]Area, 0, tileWidth*tileHeight/2)
|
|
|
|
areas := make([]Area, 1, tileWidth*tileHeight/2)
|
2014-09-20 15:21:01 +02:00
|
|
|
|
2017-02-26 17:01:15 +01:00
|
|
|
areas[0] = Area{
|
2014-09-20 15:21:01 +02:00
|
|
|
X1: 0, Z1: 0,
|
|
|
|
X2: int16(tileWidth) - 1, Z2: int16(tileHeight) - 1}
|
|
|
|
|
|
|
|
for _, yRange := range tileDepths {
|
2015-07-27 19:03:47 +02:00
|
|
|
if yRange[0] > btc.yMax || yRange[1] < btc.yMin {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-26 12:57:38 +01:00
|
|
|
|
2015-07-27 19:03:47 +02:00
|
|
|
c1.Y = max16(yRange[0], btc.yMin)
|
|
|
|
c2.Y = min16(yRange[1], btc.yMax)
|
2014-09-20 15:21:01 +02:00
|
|
|
|
2017-02-26 17:01:15 +01:00
|
|
|
for _, area := range areas {
|
2014-09-20 15:21:01 +02:00
|
|
|
c1.X = area.X1 + x
|
|
|
|
c1.Z = area.Z1 + z
|
|
|
|
c2.X = area.X2 + x
|
|
|
|
c2.Z = area.Z2 + z
|
|
|
|
query := Cuboid{P1: c1, P2: c2}
|
2017-02-26 12:57:38 +01:00
|
|
|
var count int
|
|
|
|
var err error
|
2017-03-01 15:05:51 +01:00
|
|
|
if count, err = btc.client.QueryCuboid(query, btc.blockLoaded); err != nil {
|
2017-03-06 15:43:30 +01:00
|
|
|
return err
|
2014-09-20 15:21:01 +02:00
|
|
|
}
|
2017-03-01 15:05:51 +01:00
|
|
|
if err = btc.yOrder.Drain(); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: rendering block failed: %s\n", err)
|
2014-09-20 15:21:01 +02:00
|
|
|
}
|
2017-02-26 17:01:15 +01:00
|
|
|
|
|
|
|
// If there where loaded blocks in this area recalculate coverage.
|
|
|
|
if count > 0 {
|
|
|
|
nareas = area.recalculate(btc.renderer, nareas)
|
|
|
|
} else {
|
|
|
|
nareas = append(nareas, area)
|
2017-02-26 12:57:38 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 17:01:15 +01:00
|
|
|
|
|
|
|
if len(nareas) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
areas, nareas = nareas, areas[:0]
|
2014-09-20 15:21:01 +02:00
|
|
|
}
|
2017-03-06 15:43:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btc *BaseTileCreator) blankImage() []byte {
|
|
|
|
// To avoid redundant encoding cache the resulting empty image.
|
|
|
|
if btc.emptyImage == nil {
|
|
|
|
m := BackgroundImage((tileWidth-2)*16, (tileHeight-2)*16, btc.bg)
|
|
|
|
btc.emptyImage = EncodeToMem(m)
|
|
|
|
}
|
|
|
|
return btc.emptyImage
|
|
|
|
}
|
|
|
|
|
2017-04-15 13:08:19 +02:00
|
|
|
// WriteFunc returns a function intended to be run in background so
|
|
|
|
// the creation of the next tile with this creator can be done
|
|
|
|
// concurrently.
|
2017-03-06 15:43:30 +01:00
|
|
|
func (btc *BaseTileCreator) WriteFunc(i, j int, update BaseTileUpdateFunc) func() (bool, error) {
|
2014-09-20 15:21:01 +02:00
|
|
|
|
2015-07-22 01:11:14 +02:00
|
|
|
path := filepath.Join(btc.baseDir, strconv.Itoa(i), strconv.Itoa(j)+".png")
|
|
|
|
|
|
|
|
// Empty images are likely to be produced during seeding.
|
2017-03-06 15:43:30 +01:00
|
|
|
if update == nil && btc.renderer.IsEmpty() {
|
|
|
|
return func() (bool, error) {
|
|
|
|
//log.Printf("Writing empty (%d, %d) to file %s\n", x, z, path)
|
|
|
|
return true, ioutil.WriteFile(path, btc.blankImage(), 0666)
|
2015-07-22 01:11:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
image := btc.renderer.CreateShadedImage(
|
|
|
|
16, 16, (tileWidth-2)*16, (tileHeight-2)*16,
|
2016-04-23 16:45:33 +02:00
|
|
|
btc.colors, btc.bg)
|
2014-09-20 15:21:01 +02:00
|
|
|
|
2017-03-06 15:43:30 +01:00
|
|
|
x, z := btc.renderer.GetPos()
|
2014-09-20 15:21:01 +02:00
|
|
|
|
2017-03-06 15:43:30 +01:00
|
|
|
if update == nil {
|
|
|
|
return func() (bool, error) {
|
|
|
|
log.Printf("Writing (%d, %d) to file %s.\n", x, z, path)
|
|
|
|
return true, SaveAsPNG(path, image)
|
|
|
|
}
|
2014-09-20 15:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 15:43:30 +01:00
|
|
|
return func() (bool, error) {
|
|
|
|
if update(i, j, HashImage(image)) {
|
|
|
|
log.Printf("Writing (%d, %d) to file %s.\n", x, z, path)
|
|
|
|
return true, SaveAsPNGAtomic(path, image)
|
|
|
|
}
|
2016-05-08 15:38:50 +02:00
|
|
|
|
2022-02-28 16:21:26 +01:00
|
|
|
//log.Printf("(%d, %d) is unchanged.\n", x, z)
|
2017-03-06 15:43:30 +01:00
|
|
|
return false, nil
|
|
|
|
}
|
2014-09-20 15:21:01 +02:00
|
|
|
}
|