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

@ -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);
}
}