From f4f4a33b8b710904d5f59a6ca1656f8c997ad458 Mon Sep 17 00:00:00 2001 From: FrozenCow Date: Tue, 1 Mar 2011 01:59:33 +0100 Subject: [PATCH] Implemented FlatMap fullrender + required clientlib. --- src/main/java/org/dynmap/flat/FlatMap.java | 61 ++++++++++++++-------- web/flatmap.js | 41 +++++++++++++++ 2 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 web/flatmap.js diff --git a/src/main/java/org/dynmap/flat/FlatMap.java b/src/main/java/org/dynmap/flat/FlatMap.java index a71b43ae..9e7f2726 100644 --- a/src/main/java/org/dynmap/flat/FlatMap.java +++ b/src/main/java/org/dynmap/flat/FlatMap.java @@ -18,41 +18,59 @@ import org.dynmap.debug.Debug; import org.dynmap.kzedmap.KzedMap; public class FlatMap extends MapType { - + public FlatMap(Map configuration) { } - + @Override public MapTile[] getTiles(Location l) { - return new MapTile[] { - new FlatMapTile(l.getWorld(), this, (int)Math.floor(l.getBlockX() / 128.0), (int)Math.floor(l.getBlockZ() / 128.0), 128) - }; + return new MapTile[] { new FlatMapTile(l.getWorld(), this, (int) Math.floor(l.getBlockX() / 128.0), (int) Math.floor(l.getBlockZ() / 128.0), 128) }; } @Override public MapTile[] getAdjecentTiles(MapTile tile) { - // TODO Auto-generated method stub - return null; + FlatMapTile t = (FlatMapTile) tile; + World w = t.getWorld(); + int x = t.x; + int y = t.y; + int s = t.size; + return new MapTile[] { + new FlatMapTile(w, this, x, y - 1, s), + new FlatMapTile(w, this, x + 1, y, s), + new FlatMapTile(w, this, x, y + 1, s), + new FlatMapTile(w, this, x - 1, y, s) }; } @Override public DynmapChunk[] getRequiredChunks(MapTile tile) { - // TODO Auto-generated method stub - return null; + FlatMapTile t = (FlatMapTile) tile; + int chunksPerTile = t.size / 16; + int sx = t.x * chunksPerTile; + int sz = t.y * chunksPerTile; + + DynmapChunk[] result = new DynmapChunk[chunksPerTile * chunksPerTile]; + int index = 0; + for (int x = 0; x < chunksPerTile; x++) + for (int z = 0; z < chunksPerTile; z++) { + result[index] = new DynmapChunk(sx + x, sz + z); + index++; + } + return result; } - + @Override public boolean render(MapTile tile, File outputFile) { - FlatMapTile t = (FlatMapTile)tile; + FlatMapTile t = (FlatMapTile) tile; World w = t.getWorld(); - + + boolean rendered = false; BufferedImage im = new BufferedImage(t.size, t.size, BufferedImage.TYPE_INT_RGB); WritableRaster r = im.getRaster(); - - for(int x = 0; x < t.size; x++) + + for (int x = 0; x < t.size; x++) for (int y = 0; y < t.size; y++) { - int mx = x+t.x*t.size; - int mz = y+t.y*t.size; + int mx = x + t.x * t.size; + int mz = y + t.y * t.size; int my = w.getHighestBlockYAt(mx, mz) - 1; int blockType = w.getBlockTypeIdAt(mx, my, mz); Color[] colors = KzedMap.colors.get(blockType); @@ -65,8 +83,9 @@ public class FlatMap extends MapType { c.getRed(), c.getGreen(), c.getBlue() }); + rendered = true; } - + try { ImageIO.write(im, "png", outputFile); } catch (IOException e) { @@ -75,22 +94,22 @@ public class FlatMap extends MapType { Debug.error("Failed to save image (NullPointerException): " + outputFile.getPath(), e); } im.flush(); - return false; + return rendered; } public class FlatMapTile extends MapTile { - + public int x; public int y; public int size; - + public FlatMapTile(World world, FlatMap map, int x, int y, int size) { super(world, map); this.x = x; this.y = y; this.size = size; } - + @Override public String getFilename() { return "flat_" + size + "_" + x + "_" + y + ".png"; diff --git a/web/flatmap.js b/web/flatmap.js new file mode 100644 index 00000000..3a976595 --- /dev/null +++ b/web/flatmap.js @@ -0,0 +1,41 @@ +function FlatProjection() {} +FlatProjection.prototype = { + fromLatLngToPoint: function(latLng) { + return new google.maps.Point(latLng.lat()*128.0, latLng.lng()*128.0); + }, + fromPointToLatLng: function(point) { + return new google.maps.LatLng(point.x/128.0, point.y/128.0); + }, + fromWorldToLatLng: function(x, y, z) { + return new google.maps.LatLng(x / 128.0, z / 128.0); + } +}; + +function FlatMapType(configuration) { $.extend(this, configuration); } +FlatMapType.prototype = $.extend(new DynMapType(), { + constructor: FlatMapType, + projection: new FlatProjection(), + tileSize: new google.maps.Size(128.0, 128.0), + minZoom: 0, + maxZoom: 0, + getTile: function(coord, zoom, doc) { + var tileName; + var tile = $('') + .attr('src', this.dynmap.getTileUrl(tileName = 'flat_128_' + coord.x + '_' + coord.y + '.png')) + .error(function() { tile.hide(); }) + .bind('load', function() { tile.show(); }) + .css({ + width: '128px', + height: '128px', + borderStyle: 'none' + }) + .hide(); + this.dynmap.registerTile(this, tileName, tile); + //this.dynmap.unregisterTile(this, tileName); + return tile.get(0); + }, + updateTileSize: function(zoom) { + } +}); + +maptypes.FlatMapType = function(configuration) { return new FlatMapType(configuration); }; \ No newline at end of file