mtseeder: Make base level generator use the BaseTileCreator, too.

This commit is contained in:
Sascha L. Teichmann 2014-09-20 15:50:07 +02:00
parent 216f641fa3
commit ba301372f1

View File

@ -5,11 +5,10 @@
package main package main
import ( import (
"fmt"
"image/color"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"sync" "sync"
"bitbucket.org/s_l_teichmann/mtredisalize/common" "bitbucket.org/s_l_teichmann/mtredisalize/common"
@ -22,124 +21,20 @@ const (
yOrderCapacity = 512 yOrderCapacity = 512
) )
// To scan the whole height in terms of the y coordinate
// the database is queried in height units defined in the yRanges table.
var yRanges = [][]int16{
{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},
{-1936, -1025}}
type blockPos struct { type blockPos struct {
x, z int16 x, z int16
j, i int j, i int
} }
type baseLevelWorker struct { func createTiles(
client *common.RedisClient btc *common.BaseTileCreator,
colors *common.Colors jobs chan blockPos,
renderer *common.Renderer done *sync.WaitGroup) {
yOrder *common.YOrder
baseDir string
}
func newBaseLevelWorker(client *common.RedisClient,
colors *common.Colors, baseDir string) *baseLevelWorker {
renderer := common.NewRenderer(width, height)
return &baseLevelWorker{
client: client,
colors: colors,
baseDir: baseDir,
renderer: renderer,
yOrder: common.NewYOrder(renderer, yOrderCapacity)}
}
func (blw *baseLevelWorker) close() error {
return blw.client.Close()
}
func (blw *baseLevelWorker) createTile(x, z int16, i, j int) {
blw.renderer.Reset()
blw.renderer.SetPos(x, z)
blw.yOrder.Reset()
drawBlock := func(block *common.Block) {
if err := blw.yOrder.RenderBlock(block, blw.colors.NameIndex); err != nil {
log.Printf("WARN: rendering block failed: %s", err)
}
}
var c1, c2 common.Coord
nareas := make([]common.Area, 0, width*height/2)
oareas := make([]common.Area, 1, width*height/2)
oareas[0] = common.Area{
X1: 0, Z1: 0,
X2: int16(width) - 1, Z2: int16(height) - 1}
var err error
for _, yRange := range yRanges {
c1.Y = yRange[0]
c2.Y = yRange[1]
nareas = blw.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 = blw.client.QueryCuboid(query, drawBlock); err != nil {
log.Printf("WARN: query failed: %s", err)
return
}
if err = blw.yOrder.Drain(blw.colors.NameIndex); err != nil {
log.Printf("WARN: rendering block failed: %s", err)
}
}
oareas, nareas = nareas, oareas[0:0]
}
image := blw.renderer.CreateShadedImage(
16, 16, (width-2)*16, (height-2)*16,
blw.colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
path := filepath.Join(blw.baseDir, fmt.Sprintf("%d", i), fmt.Sprintf("%d.png", j))
log.Printf("Writing (%d, %d) (%d, %d)", i, j, x, z)
if err = common.SaveAsPNG(path, image); err != nil {
log.Fatalf("writing image failed: %s", err)
}
}
func (blw *baseLevelWorker) run(jobs chan blockPos, done *sync.WaitGroup) {
defer done.Done() defer done.Done()
defer btc.Close()
for job := range jobs { for job := range jobs {
blw.createTile(job.x-1, job.z-1, job.i, job.j) btc.CreateTile(job.x-1, job.z-1, job.i, job.j)
} }
} }
@ -176,15 +71,14 @@ func createBaseLevel(
return return
} }
done.Add(1) done.Add(1)
blw := newBaseLevelWorker(client, colors, baseDir) btc := common.NewBaseTileCreator(client, colors, baseDir, false)
defer blw.close() go createTiles(btc, jobs, &done)
go blw.run(jobs, &done)
} }
zMin, zMax = order(zMin, zMax) zMin, zMax = order(zMin, zMax)
for x, i := int16(xMin), 0; x <= int16(xMax); x += 16 { for x, i := int16(xMin), 0; x <= int16(xMax); x += 16 {
xDir := filepath.Join(baseDir, fmt.Sprintf("%d", i)) xDir := filepath.Join(baseDir, strconv.Itoa(i))
log.Printf("creating dir: %s", xDir) log.Printf("creating dir: %s", xDir)
if err = os.MkdirAll(xDir, os.ModePerm); err != nil { if err = os.MkdirAll(xDir, os.ModePerm); err != nil {
log.Fatalf("Cannot create directory '%s': %s", xDir, err) log.Fatalf("Cannot create directory '%s': %s", xDir, err)