2014-09-18 07:52:37 +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
|
|
|
|
|
2014-09-18 15:21:40 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-09-19 13:06:04 +02:00
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2014-09-18 15:21:40 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2014-09-20 12:28:38 +02:00
|
|
|
"os"
|
2014-09-19 13:06:04 +02:00
|
|
|
"path/filepath"
|
2014-09-20 12:28:38 +02:00
|
|
|
"strconv"
|
2014-09-19 13:06:04 +02:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tileWidth = 18
|
|
|
|
tileHeight = 18
|
|
|
|
yOrderCapacity = 512
|
2014-09-18 15:21:40 +02:00
|
|
|
)
|
2014-09-18 07:52:37 +02:00
|
|
|
|
2014-09-19 13:06:04 +02:00
|
|
|
// To scan the whole height in terms of the y coordinate
|
2014-09-20 14:39:51 +02:00
|
|
|
// the database is queried in height units defined in the tileDepths table.
|
|
|
|
var tileDepths = [...][2]int16{
|
2014-09-19 13:06:04 +02:00
|
|
|
{1024, 1934},
|
|
|
|
{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},
|
|
|
|
{-1934, -1025}}
|
|
|
|
|
2014-09-18 07:52:37 +02:00
|
|
|
type tileUpdater struct {
|
2014-09-19 13:06:04 +02:00
|
|
|
changes map[xz]bool
|
|
|
|
mapDir string
|
|
|
|
redisAddress string
|
|
|
|
colors *common.Colors
|
|
|
|
workers int
|
|
|
|
cond *sync.Cond
|
|
|
|
mu sync.Mutex
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
|
|
|
|
2014-09-18 15:21:40 +02:00
|
|
|
type xz struct {
|
|
|
|
X int16
|
|
|
|
Z int16
|
|
|
|
}
|
|
|
|
|
2014-09-19 13:06:04 +02:00
|
|
|
func (c xz) quantize() xz {
|
|
|
|
return xz{X: (c.X - -1936) / 16, Z: (c.Z - -1936) / 16}
|
|
|
|
}
|
2014-09-18 07:52:37 +02:00
|
|
|
|
2014-09-19 13:06:04 +02:00
|
|
|
func newTileUpdater(mapDir, redisAddress string, colors *common.Colors, workers int) *tileUpdater {
|
|
|
|
tu := tileUpdater{
|
|
|
|
mapDir: mapDir,
|
|
|
|
redisAddress: redisAddress,
|
|
|
|
changes: map[xz]bool{},
|
|
|
|
colors: colors,
|
|
|
|
workers: workers}
|
|
|
|
tu.cond = sync.NewCond(&tu.mu)
|
|
|
|
return &tu
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tu *tileUpdater) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2014-09-18 15:21:40 +02:00
|
|
|
var err error
|
2014-09-19 13:06:04 +02:00
|
|
|
var newChanges []xz
|
2014-09-18 15:21:40 +02:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
2014-09-19 13:06:04 +02:00
|
|
|
if err = decoder.Decode(&newChanges); err != nil {
|
2014-09-18 15:21:40 +02:00
|
|
|
log.Printf("WARN: JSON document broken: %s", err)
|
|
|
|
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 14:39:51 +02:00
|
|
|
if len(newChanges) > 0 {
|
|
|
|
tu.cond.L.Lock()
|
|
|
|
for _, c := range newChanges {
|
|
|
|
tu.changes[c.quantize()] = true
|
|
|
|
}
|
|
|
|
tu.cond.L.Unlock()
|
|
|
|
tu.cond.Signal()
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-18 15:21:40 +02:00
|
|
|
rw.WriteHeader(http.StatusOK)
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
2014-09-19 13:06:04 +02:00
|
|
|
|
|
|
|
func (tu *tileUpdater) doUpdates() (err error) {
|
|
|
|
|
|
|
|
jobs := make(chan xz)
|
|
|
|
|
|
|
|
baseDir := filepath.Join(tu.mapDir, "8")
|
|
|
|
|
|
|
|
for i := 0; i < tu.workers; i++ {
|
|
|
|
var client *common.RedisClient
|
|
|
|
if client, err = common.NewRedisClient("tcp", tu.redisAddress); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-09-20 12:28:38 +02:00
|
|
|
btc := NewBaseTileCreator(client, tu.colors, baseDir, true)
|
2014-09-19 13:06:04 +02:00
|
|
|
go btc.run(jobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
var changes map[xz]bool
|
|
|
|
tu.cond.L.Lock()
|
|
|
|
for len(tu.changes) == 0 {
|
|
|
|
tu.cond.Wait()
|
|
|
|
}
|
|
|
|
changes = tu.changes
|
|
|
|
tu.changes = map[xz]bool{}
|
|
|
|
tu.cond.L.Unlock()
|
|
|
|
|
|
|
|
for c, _ := range changes {
|
|
|
|
log.Printf("job: %+v", c)
|
|
|
|
jobs <- c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type BaseTileCreator struct {
|
|
|
|
client *common.RedisClient
|
|
|
|
colors *common.Colors
|
|
|
|
renderer *common.Renderer
|
|
|
|
yOrder *common.YOrder
|
|
|
|
baseDir string
|
2014-09-20 12:28:38 +02:00
|
|
|
update bool
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 12:28:38 +02:00
|
|
|
func NewBaseTileCreator(
|
|
|
|
client *common.RedisClient,
|
|
|
|
colors *common.Colors,
|
|
|
|
baseDir string,
|
|
|
|
update bool) *BaseTileCreator {
|
2014-09-19 13:06:04 +02:00
|
|
|
renderer := common.NewRenderer(tileWidth, tileHeight)
|
|
|
|
return &BaseTileCreator{
|
|
|
|
client: client,
|
|
|
|
colors: colors,
|
|
|
|
renderer: renderer,
|
2014-09-20 12:28:38 +02:00
|
|
|
yOrder: common.NewYOrder(renderer, yOrderCapacity),
|
|
|
|
baseDir: baseDir,
|
|
|
|
update: update}
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (btc *BaseTileCreator) run(jobs chan xz) {
|
|
|
|
for job := range jobs {
|
|
|
|
x := job.X*16 + -1936 - 1
|
|
|
|
z := job.Z*16 + -1936 - 1
|
|
|
|
log.Printf("%d/%d %d/%d", x, z, job.X, job.Z)
|
|
|
|
if err := btc.createTile(x, z, int(job.X), int(job.Z)); err != nil {
|
|
|
|
log.Printf("WARN: create tile failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btc *BaseTileCreator) close() error {
|
|
|
|
return btc.client.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btc *BaseTileCreator) createTile(x, z int16, i, j int) error {
|
|
|
|
btc.renderer.Reset()
|
|
|
|
btc.renderer.SetPos(x, z)
|
|
|
|
btc.yOrder.Reset()
|
|
|
|
|
|
|
|
drawBlock := func(block *common.Block) {
|
|
|
|
if err := btc.yOrder.RenderBlock(block, btc.colors.NameIndex); err != nil {
|
|
|
|
log.Printf("WARN: rendering block failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var c1, c2 common.Coord
|
|
|
|
|
|
|
|
nareas := make([]common.Area, 0, tileWidth*tileHeight/2)
|
|
|
|
oareas := make([]common.Area, 1, tileWidth*tileHeight/2)
|
|
|
|
|
|
|
|
oareas[0] = common.Area{
|
|
|
|
X1: 0, Z1: 0,
|
|
|
|
X2: int16(tileWidth) - 1, Z2: int16(tileHeight) - 1}
|
|
|
|
|
2014-09-20 14:39:51 +02:00
|
|
|
for _, yRange := range tileDepths {
|
2014-09-19 13:06:04 +02:00
|
|
|
c1.Y = yRange[0]
|
|
|
|
c2.Y = yRange[1]
|
|
|
|
|
|
|
|
nareas = btc.renderer.UncoveredAreas(nareas, oareas)
|
|
|
|
|
|
|
|
if len(nareas) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, area := range nareas {
|
|
|
|
c1.X = area.X1 + x
|
|
|
|
c1.Z = area.Z1 + z
|
|
|
|
c2.X = area.X2 + x
|
|
|
|
c2.Z = area.Z2 + z
|
|
|
|
query := common.Cuboid{P1: c1, P2: c2}
|
|
|
|
if err := btc.client.QueryCuboid(query, drawBlock); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := btc.yOrder.Drain(btc.colors.NameIndex); err != nil {
|
|
|
|
log.Printf("WARN: rendering block failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oareas, nareas = nareas, oareas[0:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
image := btc.renderer.CreateShadedImage(
|
|
|
|
16, 16, (tileWidth-2)*16, (tileHeight-2)*16,
|
|
|
|
btc.colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
|
|
|
|
|
2014-09-20 12:28:38 +02:00
|
|
|
path := filepath.Join(btc.baseDir, strconv.Itoa(i), fmt.Sprintf("%d.png", j))
|
|
|
|
|
|
|
|
log.Printf("Writing (%d, %d) to file %s", x, z, path)
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 12:28:38 +02:00
|
|
|
if !btc.update {
|
|
|
|
return common.SaveAsPNG(path, image)
|
|
|
|
}
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 12:28:38 +02:00
|
|
|
// Try to make creation of update "atomic" by first writing to tmp file
|
|
|
|
// and rename the tmp file to the original one afterwards.
|
|
|
|
pathTmp := path + "tmp"
|
|
|
|
pathTmpPre := pathTmp
|
|
|
|
tc := 0
|
|
|
|
for _, err := os.Stat(pathTmp); err == nil; _, err = os.Stat(pathTmp) {
|
|
|
|
pathTmp = fmt.Sprintf("%s%d", pathTmpPre, tc)
|
|
|
|
tc++
|
|
|
|
}
|
|
|
|
if err := common.SaveAsPNG(pathTmp, image); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Rename(pathTmp, path)
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|