From 3db079f90f929aa491582ae5152a84d3d4d05fa4 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Mon, 9 Jan 2012 00:58:37 +0800 Subject: [PATCH] More porting prep, tune up invalidates and touches --- src/main/java/org/dynmap/DynmapLocation.java | 16 ++++ src/main/java/org/dynmap/DynmapPlugin.java | 82 +++++++++++-------- src/main/java/org/dynmap/MapManager.java | 20 +++-- src/main/java/org/dynmap/MapType.java | 4 +- src/main/java/org/dynmap/flat/FlatMap.java | 15 +--- src/main/java/org/dynmap/hdmap/HDMap.java | 8 +- .../java/org/dynmap/hdmap/HDPerspective.java | 5 +- .../org/dynmap/hdmap/IsoHDPerspective.java | 22 ++--- src/main/java/org/dynmap/kzedmap/KzedMap.java | 23 ++---- .../java/org/dynmap/utils/SnapshotCache.java | 28 +++++++ 10 files changed, 135 insertions(+), 88 deletions(-) create mode 100644 src/main/java/org/dynmap/DynmapLocation.java diff --git a/src/main/java/org/dynmap/DynmapLocation.java b/src/main/java/org/dynmap/DynmapLocation.java new file mode 100644 index 00000000..c51e6308 --- /dev/null +++ b/src/main/java/org/dynmap/DynmapLocation.java @@ -0,0 +1,16 @@ +package org.dynmap; + +/** + * Generic block location + */ +public class DynmapLocation { + public int x, y, z; + public String world; + + public DynmapLocation() {} + + public DynmapLocation(String w, int x, int y, int z) { + world = w; + this.x = x; this.y = y; this.z = z; + } +} diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index e01874c1..880b7b80 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -724,21 +724,31 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { /* Register entity event triggers */ EntityListener entityTrigger = new EntityListener() { - private DynmapLocation dloc = new DynmapLocation(); - private DynmapLocation toLoc(Location loc) { - dloc.x = loc.getBlockX(); dloc.y = loc.getBlockY(); - dloc.z = loc.getBlockZ(); dloc.world = loc.getWorld().getName(); - return dloc; - } @Override public void onEntityExplode(EntityExplodeEvent event) { + Location loc = event.getLocation(); + String wname = loc.getWorld().getName(); + int minx, maxx, miny, maxy, minz, maxz; + minx = maxx = loc.getBlockX(); + miny = maxy = loc.getBlockY(); + minz = maxz = loc.getBlockZ(); + /* Calculate volume impacted by explosion */ List blocks = event.blockList(); for(Block b: blocks) { - toLoc(b.getLocation()); - mapManager.sscache.invalidateSnapshot(dloc); - if(onexplosion) { - mapManager.touch(dloc, "entityexplode"); - } + Location l = b.getLocation(); + int x = l.getBlockX(); + if(x < minx) minx = x; + if(x > maxx) maxx = x; + int y = l.getBlockY(); + if(y < miny) miny = y; + if(y > maxy) maxy = y; + int z = l.getBlockZ(); + if(z < minz) minz = z; + if(z > maxz) maxz = z; + } + mapManager.sscache.invalidateSnapshot(wname, minx, miny, minz, maxx, maxy, maxz); + if(onexplosion) { + mapManager.touchVolume(wname, minx, miny, minz, maxx, maxy, maxz, "entityexplode"); } } }; @@ -755,21 +765,17 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { return; Chunk c = event.getChunk(); /* Touch extreme corners */ - dloc.world = event.getWorld().getName(); - dloc.x = c.getX() << 4; - dloc.y = 0; - dloc.z = c.getZ() << 4; - mapManager.touchVolume(dloc, 16, 128, 16, "chunkload"); + int x = c.getX() << 4; + int z = c.getZ() << 4; + mapManager.touchVolume(event.getWorld().getName(), x, 0, z, x+15, 128, z+16, "chunkload"); } @Override public void onChunkPopulate(ChunkPopulateEvent event) { Chunk c = event.getChunk(); /* Touch extreme corners */ - dloc.world = event.getWorld().getName(); - dloc.x = c.getX() << 4; - dloc.y = 0; - dloc.z = c.getZ() << 4; - mapManager.touchVolume(dloc, 16, 128, 16, "chunkpopulate"); + int x = c.getX() << 4; + int z = c.getZ() << 4; + mapManager.touchVolume(event.getWorld().getName(), x, 0, z, x+15, 128, z+16, "chunkpopulate"); } @Override public void onWorldLoad(WorldLoadEvent event) { @@ -1689,16 +1695,16 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { public int triggerRenderOfVolume(Location l0, Location l1) { if(mapManager != null) { if(l1 == null) - return mapManager.touch(toLoc(l0), "api"); + return mapManager.touch(l0.getWorld().getName(), l0.getBlockX(), l0.getBlockY(), l0.getBlockZ(), "api"); else { - DynmapLocation dloc = toLoc(l0); - int sx = l1.getBlockX() - dloc.x + 1; - int sy = l1.getBlockY() - dloc.y + 1; - int sz = l1.getBlockZ() - dloc.z + 1; - if(sx < 1) { sx = -sx + 2; dloc.x = l1.getBlockX(); } - if(sy < 1) { sy = -sy + 2; dloc.y = l1.getBlockY(); } - if(sz < 1) { sz = -sz + 2; dloc.z = l1.getBlockZ(); } - return mapManager.touchVolume(dloc, sx, sy, sz, "api"); + int minx = Math.min(l0.getBlockX(), l1.getBlockX()); + int maxx = Math.max(l0.getBlockX(), l1.getBlockX()); + int miny = Math.min(l0.getBlockY(), l1.getBlockY()); + int maxy = Math.max(l0.getBlockY(), l1.getBlockY()); + int minz = Math.min(l0.getBlockZ(), l1.getBlockZ()); + int maxz = Math.max(l0.getBlockZ(), l1.getBlockZ()); + + return mapManager.touchVolume(l0.getWorld().getName(), minx, miny, minz, maxx, maxy, maxz, "api"); } } return 0; @@ -1867,12 +1873,20 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { } @Override - public int triggerRenderOfVolume(DynmapLocation loc, int sx, int sy, int sz) { + public int triggerRenderOfBlock(String wid, int x, int y, int z) { + if(mapManager != null) + return mapManager.touch(wid, x, y, z, "api"); + else + return 0; + } + + @Override + public int triggerRenderOfVolume(String wid, int minx, int miny, int minz, int maxx, int maxy, int maxz) { if(mapManager != null) { - if((sx == 1) && (sy == 1) && (sz == 1)) - return mapManager.touch(loc, "api"); + if((minx == maxx) && (miny == maxy) && (minz == maxz)) + return mapManager.touch(wid, minx, miny, minz, "api"); else - return mapManager.touchVolume(loc, sx, sy, sz, "api"); + return mapManager.touchVolume(wid, minx, miny, minz, maxx, maxy, maxz, "api"); } return 0; } diff --git a/src/main/java/org/dynmap/MapManager.java b/src/main/java/org/dynmap/MapManager.java index a50eaf1e..20f8603b 100644 --- a/src/main/java/org/dynmap/MapManager.java +++ b/src/main/java/org/dynmap/MapManager.java @@ -453,7 +453,7 @@ public class MapManager { renderedmaps.addAll(map.getMapsSharingRender(world)); /* Now, prime the render queue */ - for (MapTile mt : map.getTiles(loc)) { + for (MapTile mt : map.getTiles(world, loc.x, loc.y, loc.z)) { if (!found.getFlag(mt.tileOrdinalX(), mt.tileOrdinalY())) { found.setFlag(mt.tileOrdinalX(), mt.tileOrdinalY(), true); renderQueue.add(mt); @@ -462,7 +462,7 @@ public class MapManager { if(!updaterender) { /* Only add other seed points for fullrender */ /* Add spawn location too (helps with some worlds where 0,64,0 may not be generated */ DynmapLocation sloc = world.getSpawnLocation(); - for (MapTile mt : map.getTiles(sloc)) { + for (MapTile mt : map.getTiles(world, sloc.x, sloc.y, sloc.z)) { if (!found.getFlag(mt.tileOrdinalX(), mt.tileOrdinalY())) { found.setFlag(mt.tileOrdinalX(), mt.tileOrdinalY(), true); renderQueue.add(mt); @@ -470,7 +470,7 @@ public class MapManager { } if(world.seedloc != null) { for(DynmapLocation seed : world.seedloc) { - for (MapTile mt : map.getTiles(seed)) { + for (MapTile mt : map.getTiles(world, seed.x, seed.y, seed.z)) { if (!found.getFlag(mt.tileOrdinalX(),mt.tileOrdinalY())) { found.setFlag(mt.tileOrdinalX(),mt.tileOrdinalY(), true); renderQueue.add(mt); @@ -1011,12 +1011,16 @@ public class MapManager { } public int touch(DynmapLocation l, String reason) { - DynmapWorld world = getWorld(l.world); + return touch(l.world, l.x, l.y, l.z, reason); + } + + public int touch(String wname, int x, int y, int z, String reason) { + DynmapWorld world = getWorld(wname); if (world == null) return 0; int invalidates = 0; for (int i = 0; i < world.maps.size(); i++) { - MapTile[] tiles = world.maps.get(i).getTiles(l); + MapTile[] tiles = world.maps.get(i).getTiles(world, x, y, z); for (int j = 0; j < tiles.length; j++) { if(invalidateTile(tiles[j])) invalidates++; @@ -1037,13 +1041,13 @@ public class MapManager { return invalidates; } - public int touchVolume(DynmapLocation l, int sx, int sy, int sz, String reason) { - DynmapWorld world = getWorld(l.world); + public int touchVolume(String wname, int minx, int miny, int minz, int maxx, int maxy, int maxz, String reason) { + DynmapWorld world = getWorld(wname); if (world == null) return 0; int invalidates = 0; for (int i = 0; i < world.maps.size(); i++) { - MapTile[] tiles = world.maps.get(i).getTiles(l, sx, sy, sz); + MapTile[] tiles = world.maps.get(i).getTiles(world, minx, miny, minz, maxx, maxy, maxz); for (int j = 0; j < tiles.length; j++) { if(invalidateTile(tiles[j])) invalidates++; diff --git a/src/main/java/org/dynmap/MapType.java b/src/main/java/org/dynmap/MapType.java index 1daef73f..e3b4f050 100644 --- a/src/main/java/org/dynmap/MapType.java +++ b/src/main/java/org/dynmap/MapType.java @@ -37,9 +37,9 @@ public abstract class MapType { public ZoomInfo(String pre, int bg) { prefix = pre; background_argb = bg; } } - public abstract MapTile[] getTiles(DynmapLocation l); + public abstract MapTile[] getTiles(DynmapWorld w, int x, int y, int z); - public abstract MapTile[] getTiles(DynmapLocation l0, int sx, int sy, int sz); + public abstract MapTile[] getTiles(DynmapWorld w, int minx, int miny, int minz, int maxx, int maxy, int maxz); public abstract MapTile[] getAdjecentTiles(MapTile tile); diff --git a/src/main/java/org/dynmap/flat/FlatMap.java b/src/main/java/org/dynmap/flat/FlatMap.java index 8922c2b8..2cee908c 100644 --- a/src/main/java/org/dynmap/flat/FlatMap.java +++ b/src/main/java/org/dynmap/flat/FlatMap.java @@ -85,21 +85,12 @@ public class FlatMap extends MapType { } @Override - public MapTile[] getTiles(DynmapLocation l) { - DynmapWorld w = MapManager.mapman.getWorld(l.world); - return new MapTile[] { new FlatMapTile(w, this, (int) Math.floor(l.x / 128.0), (int) Math.floor(l.z / 128.0), 128) }; + public MapTile[] getTiles(DynmapWorld w, int x, int y, int z) { + return new MapTile[] { new FlatMapTile(w, this, x>>7, z>>7, 128) }; } @Override - public MapTile[] getTiles(DynmapLocation l, int sx, int sy, int sz) { - DynmapWorld w = MapManager.mapman.getWorld(l.world); - int xmin, xmax, zmin, zmax; - - xmin = l.x >> 7; - zmin = l.z >> 7; - xmax = (l.x + sx) >> 7; - zmax = (l.z + sz) >> 7; - + public MapTile[] getTiles(DynmapWorld w, int xmin, int ymin, int zmin, int xmax, int ymax, int zmax) { ArrayList rslt = new ArrayList(); for(int i = xmin; i <= xmax; i++) { for(int j = zmin; j < zmax; j++) { diff --git a/src/main/java/org/dynmap/hdmap/HDMap.java b/src/main/java/org/dynmap/hdmap/HDMap.java index d38e60d7..2d30b639 100644 --- a/src/main/java/org/dynmap/hdmap/HDMap.java +++ b/src/main/java/org/dynmap/hdmap/HDMap.java @@ -128,13 +128,13 @@ public class HDMap extends MapType { public HDLighting getLighting() { return lighting; } @Override - public MapTile[] getTiles(DynmapLocation loc) { - return perspective.getTiles(loc); + public MapTile[] getTiles(DynmapWorld w, int x, int y, int z) { + return perspective.getTiles(w, x, y, z); } @Override - public MapTile[] getTiles(DynmapLocation loc, int sx, int sy, int sz) { - return perspective.getTiles(loc, sx, sy, sz); + public MapTile[] getTiles(DynmapWorld w, int minx, int miny, int minz, int maxx, int maxy, int maxz) { + return perspective.getTiles(w, minx, miny, minz, maxx, maxy, maxz); } @Override diff --git a/src/main/java/org/dynmap/hdmap/HDPerspective.java b/src/main/java/org/dynmap/hdmap/HDPerspective.java index b1c21bd5..a9bbf791 100644 --- a/src/main/java/org/dynmap/hdmap/HDPerspective.java +++ b/src/main/java/org/dynmap/hdmap/HDPerspective.java @@ -4,6 +4,7 @@ import java.util.List; import org.dynmap.DynmapChunk; import org.dynmap.DynmapLocation; +import org.dynmap.DynmapWorld; import org.dynmap.MapTile; import org.dynmap.utils.MapChunkCache; import org.json.simple.JSONObject; @@ -12,9 +13,9 @@ public interface HDPerspective { /* Get name of perspective */ String getName(); /* Get tiles invalidated by change at given location */ - MapTile[] getTiles(DynmapLocation loc); + MapTile[] getTiles(DynmapWorld w, int x, int y, int z); /* Get tiles invalidated by change at given volume, defined by 2 opposite corner locations */ - MapTile[] getTiles(DynmapLocation loc0, int sx, int sy, int sz); + MapTile[] getTiles(DynmapWorld w, int minx, int miny, int minz, int maxx, int maxy, int maxz); /* Get tiles adjacent to given tile */ MapTile[] getAdjecentTiles(MapTile tile); /* Get chunks needed for given tile */ diff --git a/src/main/java/org/dynmap/hdmap/IsoHDPerspective.java b/src/main/java/org/dynmap/hdmap/IsoHDPerspective.java index 1161d1d7..1cbacd2d 100644 --- a/src/main/java/org/dynmap/hdmap/IsoHDPerspective.java +++ b/src/main/java/org/dynmap/hdmap/IsoHDPerspective.java @@ -816,11 +816,12 @@ public class IsoHDPerspective implements HDPerspective { } @Override - public MapTile[] getTiles(DynmapLocation loc) { - DynmapWorld world = MapManager.mapman.getWorld(loc.world); + public MapTile[] getTiles(DynmapWorld world, int x, int y, int z) { HashSet tiles = new HashSet(); Vector3D block = new Vector3D(); - block.setFromLocation(loc); /* Get coordinate for block */ + block.x = x; + block.y = y; + block.z = z; Vector3D corner = new Vector3D(); /* Loop through corners of the cube */ for(int i = 0; i < 2; i++) { @@ -843,16 +844,15 @@ public class IsoHDPerspective implements HDPerspective { } @Override - public MapTile[] getTiles(DynmapLocation loc, int sx, int sy, int sz) { - DynmapWorld world = MapManager.mapman.getWorld(loc.world); + public MapTile[] getTiles(DynmapWorld world, int minx, int miny, int minz, int maxx, int maxy, int maxz) { HashSet tiles = new HashSet(); Vector3D blocks[] = new Vector3D[] { new Vector3D(), new Vector3D() }; - blocks[0].x = loc.x - 1; - blocks[0].y = loc.y - 1; - blocks[0].z = loc.z - 1; - blocks[1].x = loc.x + sx; - blocks[1].y = loc.y + sy; - blocks[1].z = loc.z + sz; + blocks[0].x = minx - 1; + blocks[0].y = miny - 1; + blocks[0].z = minz - 1; + blocks[1].x = maxx + 1; + blocks[1].y = maxy + 1; + blocks[1].z = maxz + 1; Vector3D corner = new Vector3D(); Vector3D tcorner = new Vector3D(); diff --git a/src/main/java/org/dynmap/kzedmap/KzedMap.java b/src/main/java/org/dynmap/kzedmap/KzedMap.java index 8d035370..a473db7c 100644 --- a/src/main/java/org/dynmap/kzedmap/KzedMap.java +++ b/src/main/java/org/dynmap/kzedmap/KzedMap.java @@ -50,13 +50,7 @@ public class KzedMap extends MapType { } @Override - public MapTile[] getTiles(DynmapLocation l) { - DynmapWorld world = MapManager.mapman.getWorld(l.world); - - int x = l.x; - int y = l.y; - int z = l.z; - + public MapTile[] getTiles(DynmapWorld world, int x, int y, int z) { int dx = x - anchorx; int dy = y - anchory; int dz = z - anchorz; @@ -99,18 +93,17 @@ public class KzedMap extends MapType { } @Override - public MapTile[] getTiles(DynmapLocation loc, int sx, int sy, int sz) { - DynmapWorld world = MapManager.mapman.getWorld(loc.world); + public MapTile[] getTiles(DynmapWorld world, int minx, int miny, int minz, int maxx, int maxy, int maxz) { ArrayList tiles = new ArrayList(); /* Transform both to tile coordinates */ - int dx = loc.x - anchorx; - int dy = loc.y - anchory; - int dz = loc.z - anchorz; + int dx = minx - anchorx; + int dy = miny - anchory; + int dz = minz - anchorz; int px0 = dx + dz; int py0 = dx - dz - dy; - dx = loc.x + sx - anchorx; - dy = loc.y + sy - anchory; - dz = loc.z + sz - anchorz; + dx = maxx - anchorx; + dy = maxy - anchory; + dz = maxz - anchorz; int px1 = dx + dz; int py1 = dx - dz - dy; /* Compute ranges */ diff --git a/src/main/java/org/dynmap/utils/SnapshotCache.java b/src/main/java/org/dynmap/utils/SnapshotCache.java index 98085738..27ae97a7 100644 --- a/src/main/java/org/dynmap/utils/SnapshotCache.java +++ b/src/main/java/org/dynmap/utils/SnapshotCache.java @@ -67,6 +67,34 @@ public class SnapshotCache { } processRefQueue(); } + /** + * Invalidate cached snapshot, if in cache + */ + public void invalidateSnapshot(String w, int x, int y, int z) { + String key = getKey(w, x>>4, z>>4); + CacheRec rec = snapcache.remove(key); + if(rec != null) { + snapcache.reverselookup.remove(rec.ref); + rec.ref.clear(); + } + processRefQueue(); + } + /** + * Invalidate cached snapshot, if in cache + */ + public void invalidateSnapshot(String w, int x0, int y0, int z0, int x1, int y1, int z1) { + for(int xx = (x0>>4); xx <= (x1>>4); xx++) { + for(int zz = (z0>>4); zz <= (z1>>4); zz++) { + String key = getKey(w, xx, zz); + CacheRec rec = snapcache.remove(key); + if(rec != null) { + snapcache.reverselookup.remove(rec.ref); + rec.ref.clear(); + } + } + } + processRefQueue(); + } /** * Look for chunk snapshot in cache */