diff --git a/static/js/test.js b/static/js/test.js index 16201e0..ed8ba3b 100644 --- a/static/js/test.js +++ b/static/js/test.js @@ -1,13 +1,13 @@ var camera, scene, renderer; -var colormapping, geometry; +var geometry; init(); animate(); function getNodePos(x,y,z){ return x + (y * 16) + (z * 256); } -function drawMapblock(posx,posy,posz){ - m.request("api/mapblock/"+posx+"/"+posy+"/"+posz) +function drawMapblock(posx,posy,posz,colormapping){ + m.request("api/viewblock/"+posx+"/"+posy+"/"+posz) .then(function(mapblock){ if (!mapblock) return; @@ -16,7 +16,7 @@ function drawMapblock(posx,posy,posz){ for (var y=0; y<16; y++){ for (var z=0; z<16; z++){ var i = getNodePos(x,y,z); - var contentId = mapblock.mapdata.contentid[i]; + var contentId = mapblock.contentid[i]; var nodeName = mapblock.blockmapping[contentId] var colorObj = colormapping[nodeName]; @@ -50,12 +50,12 @@ function init() { geometry = new THREE.BoxGeometry( 3, 3, 3 ); m.request("api/colormapping") - .then(function(_colormapping){ - colormapping = _colormapping; - for (var x=-4; x<4; x++){ + .then(function(colormapping){ + + for (var x=0; x<14; x++){ for (var y=0; y<2; y++){ for (var z=-4; z<4; z++){ - drawMapblock(x,y,z); + drawMapblock(x,y,z,colormapping); } } } diff --git a/web/serve.go b/web/serve.go index b3bdd63..c7d7a66 100644 --- a/web/serve.go +++ b/web/serve.go @@ -29,6 +29,7 @@ func Serve(ctx *app.App) { mux.Handle("/api/minetest", &Minetest{ctx: ctx}) mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx}) mux.Handle("/api/colormapping/", &ColorMappingHandler{ctx: ctx}) + mux.Handle("/api/viewblock/", &ViewMapblockHandler{ctx: ctx}) if ctx.Config.MapObjects.Areas { mux.Handle("/api/areas", &AreasHandler{ctx: ctx}) diff --git a/web/viewblock.go b/web/viewblock.go new file mode 100644 index 0000000..a8986d7 --- /dev/null +++ b/web/viewblock.go @@ -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) + + } +}