1
0
forked from MTSR/mapserver

api/viewblock

This commit is contained in:
NatureFreshMilk 2019-12-13 11:03:34 +01:00
parent 467429ace8
commit 780fda8460
3 changed files with 63 additions and 8 deletions

View File

@ -1,13 +1,13 @@
var camera, scene, renderer; var camera, scene, renderer;
var colormapping, geometry; var geometry;
init(); init();
animate(); animate();
function getNodePos(x,y,z){ return x + (y * 16) + (z * 256); } function getNodePos(x,y,z){ return x + (y * 16) + (z * 256); }
function drawMapblock(posx,posy,posz){ function drawMapblock(posx,posy,posz,colormapping){
m.request("api/mapblock/"+posx+"/"+posy+"/"+posz) m.request("api/viewblock/"+posx+"/"+posy+"/"+posz)
.then(function(mapblock){ .then(function(mapblock){
if (!mapblock) if (!mapblock)
return; return;
@ -16,7 +16,7 @@ function drawMapblock(posx,posy,posz){
for (var y=0; y<16; y++){ for (var y=0; y<16; y++){
for (var z=0; z<16; z++){ for (var z=0; z<16; z++){
var i = getNodePos(x,y,z); var i = getNodePos(x,y,z);
var contentId = mapblock.mapdata.contentid[i]; var contentId = mapblock.contentid[i];
var nodeName = mapblock.blockmapping[contentId] var nodeName = mapblock.blockmapping[contentId]
var colorObj = colormapping[nodeName]; var colorObj = colormapping[nodeName];
@ -50,12 +50,12 @@ function init() {
geometry = new THREE.BoxGeometry( 3, 3, 3 ); geometry = new THREE.BoxGeometry( 3, 3, 3 );
m.request("api/colormapping") m.request("api/colormapping")
.then(function(_colormapping){ .then(function(colormapping){
colormapping = _colormapping;
for (var x=-4; x<4; x++){ for (var x=0; x<14; x++){
for (var y=0; y<2; y++){ for (var y=0; y<2; y++){
for (var z=-4; z<4; z++){ for (var z=-4; z<4; z++){
drawMapblock(x,y,z); drawMapblock(x,y,z,colormapping);
} }
} }
} }

View File

@ -29,6 +29,7 @@ func Serve(ctx *app.App) {
mux.Handle("/api/minetest", &Minetest{ctx: ctx}) mux.Handle("/api/minetest", &Minetest{ctx: ctx})
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx}) mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})
mux.Handle("/api/colormapping/", &ColorMappingHandler{ctx: ctx}) mux.Handle("/api/colormapping/", &ColorMappingHandler{ctx: ctx})
mux.Handle("/api/viewblock/", &ViewMapblockHandler{ctx: ctx})
if ctx.Config.MapObjects.Areas { if ctx.Config.MapObjects.Areas {
mux.Handle("/api/areas", &AreasHandler{ctx: ctx}) mux.Handle("/api/areas", &AreasHandler{ctx: ctx})

54
web/viewblock.go Normal file
View File

@ -0,0 +1,54 @@
package web
import (
"encoding/json"
"mapserver/app"
"mapserver/coords"
"net/http"
"strconv"
"strings"
)
type ViewBlock struct {
BlockMapping map[int]string `json:"blockmapping"`
ContentId []int `json:"contentid"`
}
type ViewMapblockHandler struct {
ctx *app.App
}
func (h *ViewMapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/viewblock/")
parts := strings.Split(str, "/")
if len(parts) != 3 {
resp.WriteHeader(500)
resp.Write([]byte("wrong number of arguments"))
return
}
x, _ := strconv.Atoi(parts[0])
y, _ := strconv.Atoi(parts[1])
z, _ := strconv.Atoi(parts[2])
c := coords.NewMapBlockCoords(x, y, z)
mb, err := h.ctx.MapBlockAccessor.GetMapBlock(c)
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
} else {
var vb *ViewBlock
if mb != nil {
vb = &ViewBlock{}
vb.BlockMapping = mb.BlockMapping
vb.ContentId = mb.Mapdata.ContentId
}
resp.Header().Add("content-type", "application/json")
json.NewEncoder(resp).Encode(vb)
}
}