Add support for limiting visible areas on each world, selective fill

of hidden areas (air, stone, ocean)
This commit is contained in:
Mike Primm 2011-06-16 23:37:28 -05:00
parent 81dbb8483f
commit ffc08173b4
6 changed files with 336 additions and 100 deletions

View file

@ -5,6 +5,7 @@ import java.util.List;
import org.bukkit.World;
import org.bukkit.Location;
import org.dynmap.utils.MapChunkCache;
public class DynmapWorld {
public World world;
@ -12,6 +13,8 @@ public class DynmapWorld {
public UpdateQueue updates = new UpdateQueue();
public ConfigurationNode configuration;
public List<Location> seedloc;
public List<MapChunkCache.VisibilityLimit> visibility_limits;
public MapChunkCache.HiddenChunkStyle hiddenchunkstyle;
public int servertime;
public boolean sendposition;
public boolean sendhealth;

View file

@ -164,7 +164,7 @@ public class MapManager {
World w = world.world;
/* Fetch chunk cache from server thread */
DynmapChunk[] requiredChunks = tile.getMap().getRequiredChunks(tile);
MapChunkCache cache = createMapChunkCache(w, requiredChunks);
MapChunkCache cache = createMapChunkCache(world, requiredChunks);
if(cache == null) {
cleanup();
return; /* Cancelled/aborted */
@ -309,6 +309,29 @@ public class MapManager {
dynmapWorld.seedloc.add(lx);
}
}
/* Load visibility limits, if any are defined */
List<ConfigurationNode> vislimits = worldConfiguration.getNodes("visibilitylimits");
if(vislimits != null) {
dynmapWorld.visibility_limits = new ArrayList<MapChunkCache.VisibilityLimit>();
for(ConfigurationNode vis : vislimits) {
MapChunkCache.VisibilityLimit lim = new MapChunkCache.VisibilityLimit();
lim.x0 = vis.getInteger("x0", 0);
lim.x1 = vis.getInteger("x1", 0);
lim.z0 = vis.getInteger("z0", 0);
lim.z1 = vis.getInteger("z1", 0);
dynmapWorld.visibility_limits.add(lim);
/* Also, add a seed location for the middle of each visible area */
dynmapWorld.seedloc.add(new Location(w, (lim.x0+lim.x1)/2, 64, (lim.z0+lim.z1)/2));
}
}
String hiddenchunkstyle = worldConfiguration.getString("hidestyle", "stone");
if(hiddenchunkstyle.equals("air"))
dynmapWorld.hiddenchunkstyle = MapChunkCache.HiddenChunkStyle.FILL_AIR;
else if(hiddenchunkstyle.equals("ocean"))
dynmapWorld.hiddenchunkstyle = MapChunkCache.HiddenChunkStyle.FILL_OCEAN;
else
dynmapWorld.hiddenchunkstyle = MapChunkCache.HiddenChunkStyle.FILL_STONE_PLAIN;
// TODO: Make this less... weird...
// Insert the world on the same spot as in the configuration.
@ -416,7 +439,7 @@ public class MapManager {
/**
* Render processor helper - used by code running on render threads to request chunk snapshot cache from server/sync thread
*/
public MapChunkCache createMapChunkCache(final World w, final DynmapChunk[] chunks) {
public MapChunkCache createMapChunkCache(final DynmapWorld w, final DynmapChunk[] chunks) {
Callable<MapChunkCache> job = new Callable<MapChunkCache>() {
public MapChunkCache call() {
MapChunkCache c = null;
@ -428,7 +451,13 @@ public class MapManager {
}
if(c == null)
c = new LegacyMapChunkCache();
c.loadChunks(w, chunks);
if(w.visibility_limits != null) {
for(MapChunkCache.VisibilityLimit limit: w.visibility_limits) {
c.setVisibleRange(limit);
}
c.setHiddenFillStyle(w.hiddenchunkstyle);
}
c.loadChunks(w.world, chunks);
return c;
}
};

View file

@ -1,19 +1,20 @@
package org.dynmap.utils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.lang.reflect.Field;
import java.util.LinkedList;
import org.bukkit.World;
import org.bukkit.Chunk;
import org.bukkit.entity.Entity;
import org.dynmap.DynmapChunk;
import org.dynmap.Log;
import java.util.List;
/**
* Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread
*/
public class LegacyMapChunkCache implements MapChunkCache {
private World w;
private static Method getchunkdata = null;
private static Method gethandle = null;
private static Method poppreservedchunk = null;
@ -22,6 +23,8 @@ public class LegacyMapChunkCache implements MapChunkCache {
private int x_min, x_max, z_min, z_max;
private int x_dim;
private HiddenChunkStyle hidestyle = HiddenChunkStyle.FILL_AIR;
private List<VisibilityLimit> visible_limits = null;
private LegacyChunkSnapshot[] snaparray; /* Index = (x-x_min) + ((z-z_min)*x_dim) */
@ -115,7 +118,7 @@ public class LegacyMapChunkCache implements MapChunkCache {
}
/**
* Chunk cache for representing unloaded chunk
* Chunk cache for representing unloaded chunk (or air chunk)
*/
private static class EmptyChunk implements LegacyChunkSnapshot {
public final int getBlockTypeId(int x, int y, int z) {
@ -131,11 +134,42 @@ public class LegacyMapChunkCache implements MapChunkCache {
return 0;
}
public final int getHighestBlockYAt(int x, int z) {
return 1;
return 0;
}
}
/**
* Chunk cache for representing hidden chunk as stone
*/
private static class PlainChunk implements LegacyChunkSnapshot {
private int fillid;
PlainChunk(int fillid) { this.fillid = fillid; }
public final int getBlockTypeId(int x, int y, int z) {
if(y < 64)
return fillid;
return 0;
}
public final int getBlockData(int x, int y, int z) {
return 0;
}
public final int getBlockSkyLight(int x, int y, int z) {
if(y < 64)
return 0;
return 15;
}
public final int getBlockEmittedLight(int x, int y, int z) {
return 0;
}
public final int getHighestBlockYAt(int x, int z) {
return 64;
}
}
private static final EmptyChunk EMPTY = new EmptyChunk();
private static final PlainChunk STONE = new PlainChunk(1);
private static final PlainChunk OCEAN = new PlainChunk(9);
/**
* Construct empty cache
@ -175,7 +209,6 @@ public class LegacyMapChunkCache implements MapChunkCache {
}
x_dim = x_max - x_min + 1;
}
this.w = w;
if(!initialized) {
try {
@ -208,22 +241,43 @@ public class LegacyMapChunkCache implements MapChunkCache {
if(gethandle != null) {
// Load the required chunks.
for (DynmapChunk chunk : chunks) {
boolean vis = true;
if(visible_limits != null) {
vis = false;
for(VisibilityLimit limit : visible_limits) {
if((chunk.x >= limit.x0) && (chunk.x <= limit.x1) && (chunk.z >= limit.z0) && (chunk.z <= limit.z1)) {
vis = true;
break;
}
}
}
boolean wasLoaded = w.isChunkLoaded(chunk.x, chunk.z);
boolean didload = w.loadChunk(chunk.x, chunk.z, false);
/* If it did load, make cache of it */
if(didload) {
Chunk c = w.getChunkAt(chunk.x, chunk.z);
try {
Object cc = gethandle.invoke(c);
byte[] buf = new byte[32768 + 16384 + 16384 + 16384]; /* Get big enough buffer for whole chunk */
getchunkdata.invoke(cc, buf, 0, 0, 0, 16, 128, 16, 0);
byte[] h = (byte[])heightmap.get(cc);
byte[] hmap = new byte[256];
System.arraycopy(h, 0, hmap, 0, 256);
CraftChunkSnapshot ss = new CraftChunkSnapshot(chunk.x, chunk.z, buf, hmap);
snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss;
} catch (Exception x) {
LegacyChunkSnapshot ss = null;
if(!vis) {
if(hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN)
ss = STONE;
else if(hidestyle == HiddenChunkStyle.FILL_OCEAN)
ss = OCEAN;
else
ss = EMPTY;
}
else {
Chunk c = w.getChunkAt(chunk.x, chunk.z);
try {
Object cc = gethandle.invoke(c);
byte[] buf = new byte[32768 + 16384 + 16384 + 16384]; /* Get big enough buffer for whole chunk */
getchunkdata.invoke(cc, buf, 0, 0, 0, 16, 128, 16, 0);
byte[] h = (byte[])heightmap.get(cc);
byte[] hmap = new byte[256];
System.arraycopy(h, 0, hmap, 0, 256);
ss = new CraftChunkSnapshot(chunk.x, chunk.z, buf, hmap);
} catch (Exception x) {
}
}
snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss;
}
if ((!wasLoaded) && didload) {
/* It looks like bukkit "leaks" entities - they don't get removed from the world-level table
@ -305,4 +359,33 @@ public class LegacyMapChunkCache implements MapChunkCache {
public MapIterator getIterator(int x, int y, int z) {
return new OurMapIterator(x, y, z);
}
/**
* Set hidden chunk style (default is FILL_AIR)
*/
public void setHiddenFillStyle(HiddenChunkStyle style) {
this.hidestyle = style;
}
/**
* Add visible area limit - can be called more than once
* Needs to be set before chunks are loaded
* Coordinates are block coordinates
*/
public void setVisibleRange(VisibilityLimit lim) {
VisibilityLimit limit = new VisibilityLimit();
if(lim.x0 > lim.x1) {
limit.x0 = (lim.x1 >> 4); limit.x1 = ((lim.x0+15) >> 4);
}
else {
limit.x0 = (lim.x0 >> 4); limit.x1 = ((lim.x1+15) >> 4);
}
if(lim.z0 > lim.z1) {
limit.z0 = (lim.z1 >> 4); limit.z1 = ((lim.z0+15) >> 4);
}
else {
limit.z0 = (lim.z0 >> 4); limit.z1 = ((lim.z1+15) >> 4);
}
if(visible_limits == null)
visible_limits = new ArrayList<VisibilityLimit>();
visible_limits.add(limit);
}
}

View file

@ -3,6 +3,14 @@ import org.bukkit.World;
import org.dynmap.DynmapChunk;
public interface MapChunkCache {
public enum HiddenChunkStyle {
FILL_AIR,
FILL_STONE_PLAIN,
FILL_OCEAN
};
public static class VisibilityLimit {
public int x0, x1, z0, z1;
}
/**
* Load chunks into cache
* @param w - world
@ -37,4 +45,14 @@ public interface MapChunkCache {
* Get cache iterator
*/
public MapIterator getIterator(int x, int y, int z);
/**
* Set hidden chunk style (default is FILL_AIR)
*/
public void setHiddenFillStyle(HiddenChunkStyle style);
/**
* Add visible area limit - can be called more than once
* Needs to be set before chunks are loaded
* Coordinates are block coordinates
*/
public void setVisibleRange(VisibilityLimit limit);
}

View file

@ -1,7 +1,9 @@
package org.dynmap.utils;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.World;
import org.bukkit.Chunk;
import org.bukkit.block.Biome;
@ -14,13 +16,13 @@ import org.dynmap.Log;
* Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread
*/
public class NewMapChunkCache implements MapChunkCache {
private World w;
private static Method poppreservedchunk = null;
private int x_min, x_max, z_min, z_max;
private int x_dim;
private HiddenChunkStyle hidestyle = HiddenChunkStyle.FILL_AIR;
private List<VisibilityLimit> visible_limits = null;
private ChunkSnapshot[] snaparray; /* Index = (x-x_min) + ((z-z_min)*x_dim) */
/**
@ -113,7 +115,7 @@ public class NewMapChunkCache implements MapChunkCache {
}
/**
* Chunk cache for representing unloaded chunk
* Chunk cache for representing unloaded chunk (or air)
*/
private static class EmptyChunk implements ChunkSnapshot {
/* Need these for interface, but not used */
@ -138,11 +140,49 @@ public class NewMapChunkCache implements MapChunkCache {
return 0;
}
public final int getHighestBlockYAt(int x, int z) {
return 1;
return 0;
}
}
/**
* Chunk cache for representing generic stone chunk
*/
private static class PlainChunk implements ChunkSnapshot {
private int fillid;
PlainChunk(int fillid) { this.fillid = fillid; }
/* Need these for interface, but not used */
public int getX() { return 0; }
public int getZ() { return 0; }
public String getWorldName() { return ""; }
public Biome getBiome(int x, int z) { return null; }
public double getRawBiomeTemperature(int x, int z) { return 0.0; }
public double getRawBiomeRainfall(int x, int z) { return 0.0; }
public long getCaptureFullTime() { return 0; }
public final int getBlockTypeId(int x, int y, int z) {
if(y < 64) return fillid;
return 0;
}
public final int getBlockData(int x, int y, int z) {
return 0;
}
public final int getBlockSkyLight(int x, int y, int z) {
if(y < 64)
return 0;
return 15;
}
public final int getBlockEmittedLight(int x, int y, int z) {
return 0;
}
public final int getHighestBlockYAt(int x, int z) {
return 64;
}
}
private static final EmptyChunk EMPTY = new EmptyChunk();
private static final PlainChunk STONE = new PlainChunk(1);
private static final PlainChunk OCEAN = new PlainChunk(9);
/**
* Construct empty cache
@ -191,17 +231,38 @@ public class NewMapChunkCache implements MapChunkCache {
}
x_dim = x_max - x_min + 1;
}
this.w = w;
snaparray = new ChunkSnapshot[x_dim * (z_max-z_min+1)];
// Load the required chunks.
for (DynmapChunk chunk : chunks) {
boolean vis = true;
if(visible_limits != null) {
vis = false;
for(VisibilityLimit limit : visible_limits) {
if((chunk.x >= limit.x0) && (chunk.x <= limit.x1) && (chunk.z >= limit.z0) && (chunk.z <= limit.z1)) {
vis = true;
break;
}
}
}
boolean wasLoaded = w.isChunkLoaded(chunk.x, chunk.z);
boolean didload = w.loadChunk(chunk.x, chunk.z, false);
/* If it did load, make cache of it */
if(didload) {
Chunk c = w.getChunkAt(chunk.x, chunk.z);
snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = c.getChunkSnapshot();
ChunkSnapshot ss = null;
if(!vis) {
if(hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN)
ss = STONE;
else if(hidestyle == HiddenChunkStyle.FILL_OCEAN)
ss = OCEAN;
else
ss = EMPTY;
}
else {
Chunk c = w.getChunkAt(chunk.x, chunk.z);
ss = c.getChunkSnapshot();
}
snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss;
}
if ((!wasLoaded) && didload) {
/* It looks like bukkit "leaks" entities - they don't get removed from the world-level table
@ -282,4 +343,33 @@ public class NewMapChunkCache implements MapChunkCache {
public MapIterator getIterator(int x, int y, int z) {
return new OurMapIterator(x, y, z);
}
/**
* Set hidden chunk style (default is FILL_AIR)
*/
public void setHiddenFillStyle(HiddenChunkStyle style) {
this.hidestyle = style;
}
/**
* Add visible area limit - can be called more than once
* Needs to be set before chunks are loaded
* Coordinates are block coordinates
*/
public void setVisibleRange(VisibilityLimit lim) {
VisibilityLimit limit = new VisibilityLimit();
if(lim.x0 > lim.x1) {
limit.x0 = (lim.x1 >> 4); limit.x1 = ((lim.x0+15) >> 4);
}
else {
limit.x0 = (lim.x0 >> 4); limit.x1 = ((lim.x1+15) >> 4);
}
if(lim.z0 > lim.z1) {
limit.z0 = (lim.z1 >> 4); limit.z1 = ((lim.z0+15) >> 4);
}
else {
limit.z0 = (lim.z0 >> 4); limit.z1 = ((lim.z1+15) >> 4);
}
if(visible_limits == null)
visible_limits = new ArrayList<VisibilityLimit>();
visible_limits.add(limit);
}
}