mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-07-01 23:50:44 +02:00
Moved sub programs into folder cmd to clean up project structure.
This commit is contained in:
141
cmd/tilemapper/client.go
Normal file
141
cmd/tilemapper/client.go
Normal file
@ -0,0 +1,141 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"bufio"
|
||||
|
||||
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
conn net.Conn
|
||||
reader *bufio.Reader
|
||||
}
|
||||
|
||||
func NewClient(network, address string) (client *Client, err error) {
|
||||
var conn net.Conn
|
||||
if conn, err = net.Dial(network, address); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client = &Client{conn: conn, reader: bufio.NewReaderSize(conn, 8*1024)}
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) Close() error {
|
||||
return client.conn.Close()
|
||||
}
|
||||
|
||||
func (client *Client) writeArray(size int) (err error) {
|
||||
_, err = client.conn.Write([]byte(fmt.Sprintf("*%d\r\n", size)))
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) writeBulkString(data []byte) (err error) {
|
||||
if _, err = client.conn.Write([]byte(fmt.Sprintf("$%d\r\n", len(data)))); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = client.conn.Write(data); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = client.conn.Write([]byte("\r\n"))
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) writeHSpatial(p1, p2 int64) (err error) {
|
||||
if err = client.writeArray(4); err != nil {
|
||||
return
|
||||
}
|
||||
if err = client.writeBulkString([]byte("HSPATIAL")); err != nil {
|
||||
return
|
||||
}
|
||||
if err = client.writeBulkString([]byte("IGNORE")); err != nil {
|
||||
return
|
||||
}
|
||||
if err = client.writeBulkString(common.StringToBytes(p1)); err != nil {
|
||||
return
|
||||
}
|
||||
err = client.writeBulkString(common.StringToBytes(p2))
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) readLine() (data []byte, err error) {
|
||||
return client.reader.ReadBytes('\n')
|
||||
}
|
||||
|
||||
func isError(line []byte) error {
|
||||
if len(line) > 0 && line[0] == '-' {
|
||||
return fmt.Errorf("error: %s", line[1:])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (client *Client) readBulkString(data *[]byte) (size int, err error) {
|
||||
var line []byte
|
||||
if line, err = client.readLine(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = isError(line); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = fmt.Sscanf(string(line), "$%d\r\n", &size); err != nil {
|
||||
return
|
||||
}
|
||||
if size <= 0 {
|
||||
return
|
||||
}
|
||||
if cap(*data) < size {
|
||||
*data = make([]byte, size)
|
||||
}
|
||||
for rest := size; rest > 0; {
|
||||
var n int
|
||||
if n, err = client.reader.Read((*data)[size-rest : size]); err != nil {
|
||||
return
|
||||
}
|
||||
rest -= n
|
||||
}
|
||||
if _, err = client.reader.ReadBytes('\n'); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) QueryCuboid(cuboid common.Cuboid, fn func(*common.Block)) (err error) {
|
||||
p1 := common.CoordToPlain(cuboid.P1)
|
||||
p2 := common.CoordToPlain(cuboid.P2)
|
||||
if err = client.writeHSpatial(p1, p2); err != nil {
|
||||
return
|
||||
}
|
||||
var (
|
||||
data = make([]byte, 8*1024)
|
||||
block = common.Block{}
|
||||
size int
|
||||
key int64
|
||||
)
|
||||
|
||||
for {
|
||||
if size, err = client.readBulkString(&data); err != nil {
|
||||
return
|
||||
}
|
||||
if size <= 0 {
|
||||
break
|
||||
}
|
||||
if key, err = common.DecodeStringFromBytes(data[0:size]); err != nil {
|
||||
return
|
||||
}
|
||||
block.Coord = common.PlainToCoord(key)
|
||||
if size, err = client.readBulkString(&data); err != nil {
|
||||
return
|
||||
}
|
||||
block.Data = data[0:size]
|
||||
fn(&block)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
49
cmd/tilemapper/colors.go
Normal file
49
cmd/tilemapper/colors.go
Normal file
@ -0,0 +1,49 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Colors struct {
|
||||
Colors []color.RGBA
|
||||
NameIndex map[string]int32
|
||||
}
|
||||
|
||||
func ParseColors(filename string) (colors *Colors, err error) {
|
||||
|
||||
var file *os.File
|
||||
if file, err = os.Open(filename); err != nil {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
nameIndex := make(map[string]int32)
|
||||
cols := make([]color.RGBA, 0, 2200)
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
c := color.RGBA{A: 0xff}
|
||||
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
|
||||
}
|
||||
}
|
||||
err = scanner.Err()
|
||||
colors = &Colors{Colors: cols, NameIndex: nameIndex}
|
||||
return
|
||||
}
|
2140
cmd/tilemapper/colors.txt
Normal file
2140
cmd/tilemapper/colors.txt
Normal file
File diff suppressed because it is too large
Load Diff
24
cmd/tilemapper/image.go
Normal file
24
cmd/tilemapper/image.go
Normal file
@ -0,0 +1,24 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"image"
|
||||
"image/png"
|
||||
"os"
|
||||
)
|
||||
|
||||
func SaveAsPNG(path string, img image.Image) (err error) {
|
||||
var file *os.File
|
||||
if file, err = os.Create(path); err != nil {
|
||||
return
|
||||
}
|
||||
writer := bufio.NewWriter(file)
|
||||
err = png.Encode(writer, img)
|
||||
writer.Flush()
|
||||
file.Close()
|
||||
return
|
||||
}
|
120
cmd/tilemapper/main.go
Normal file
120
cmd/tilemapper/main.go
Normal file
@ -0,0 +1,120 @@
|
||||
// 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 (
|
||||
"flag"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
|
||||
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
port int
|
||||
host string
|
||||
x, y, z int
|
||||
width, height, depth int
|
||||
colorsfile string
|
||||
outfile string
|
||||
shaded bool
|
||||
)
|
||||
|
||||
flag.IntVar(&port, "port", 6379, "port to of mtredisalize server")
|
||||
flag.IntVar(&port, "p", 6379, "port to of mtredisalize server (shorthand)")
|
||||
flag.StringVar(&host, "host", "localhost", "host to mtredisalize server")
|
||||
flag.IntVar(&x, "x", 0, "x of query cuboid")
|
||||
flag.IntVar(&y, "y", -75, "y of query cuboid")
|
||||
flag.IntVar(&z, "z", 0, "z of query cuboid")
|
||||
flag.IntVar(&width, "width", 16, "width of query cuboid")
|
||||
flag.IntVar(&height, "height", 16, "height of query cuboid")
|
||||
flag.IntVar(&depth, "depth", 150, "depth of query cuboid")
|
||||
flag.IntVar(&width, "w", 16, "width of query cuboid (shorthand)")
|
||||
flag.IntVar(&height, "h", 16, "height of query cuboid (shorthand)")
|
||||
flag.IntVar(&depth, "d", 150, "depth of query cuboid (shorthand)")
|
||||
flag.StringVar(&colorsfile, "colors", "colors.txt", "definition of colors")
|
||||
flag.StringVar(&outfile, "output", "out.png", "image file of result")
|
||||
flag.StringVar(&outfile, "o", "out.png", "image file of result (shorthand)")
|
||||
flag.BoolVar(&shaded, "shaded", true, "draw relief")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
var err error
|
||||
var colors *Colors
|
||||
|
||||
if colors, err = ParseColors(colorsfile); err != nil {
|
||||
log.Fatalf("Cannot open color file: %s", err)
|
||||
}
|
||||
|
||||
address := fmt.Sprintf("%s:%d", host, port)
|
||||
|
||||
var client *Client
|
||||
|
||||
if client, err = NewClient("tcp", address); err != nil {
|
||||
log.Fatalf("Cannot connect to '%s': %s", address, err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
if shaded {
|
||||
width += 2
|
||||
height += 2
|
||||
x--
|
||||
z--
|
||||
}
|
||||
|
||||
q1x, q1y, q1z := int16(x), int16(y), int16(z)
|
||||
q2x, q2y, q2z := q1x+int16(width)-1, q1y+int16(depth)-1, q1z+int16(height)-1
|
||||
|
||||
renderer := common.NewRenderer(q1x, q1z, width, height)
|
||||
yOrder := common.NewYOrder(renderer, 512)
|
||||
|
||||
numBlocks := 0
|
||||
drawBlock := func(block *common.Block) {
|
||||
if err := yOrder.RenderBlock(block, colors.NameIndex); err != nil {
|
||||
log.Printf("WARN: rendering block failed: %s", err)
|
||||
}
|
||||
numBlocks++
|
||||
}
|
||||
|
||||
c1 := common.Coord{X: q1x, Z: q1z}
|
||||
c2 := common.Coord{X: q2x, Z: q2z}
|
||||
for c2.Y = q2y; c2.Y > q1y; c2.Y -= 8 {
|
||||
c1.Y = c2.Y - 7
|
||||
if c1.Y < q1y {
|
||||
c1.Y = q1y
|
||||
}
|
||||
cuboid := common.Cuboid{P1: common.MinCoord(c1, c2), P2: common.MaxCoord(c1, c2)}
|
||||
if err = client.QueryCuboid(cuboid, drawBlock); err != nil {
|
||||
log.Fatalf("query failed: %s", err)
|
||||
}
|
||||
if err = yOrder.Drain(colors.NameIndex); err != nil {
|
||||
log.Printf("WARN: rendering block failed: %s", err)
|
||||
}
|
||||
if renderer.IsFilled() {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var image image.Image
|
||||
|
||||
if shaded {
|
||||
image = renderer.CreateShadedImage(
|
||||
16, 16, (width-2)*16, (height-2)*16,
|
||||
colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
|
||||
} else {
|
||||
image = renderer.CreateImage(
|
||||
colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
|
||||
}
|
||||
|
||||
if err = SaveAsPNG(outfile, image); err != nil {
|
||||
log.Fatalf("writing image failed: %s", err)
|
||||
}
|
||||
|
||||
log.Printf("num blocks: %d\n", numBlocks)
|
||||
log.Printf("rejected blocks: %d\n", renderer.Rejected)
|
||||
}
|
Reference in New Issue
Block a user