Splitted up MapManager.
This commit is contained in:
parent
90afd9ef82
commit
8860eb8c04
15 changed files with 655 additions and 595 deletions
|
|
@ -35,10 +35,10 @@ import java.util.logging.Logger;
|
|||
|
||||
import org.bukkit.*;
|
||||
import org.dynmap.debug.Debugger;
|
||||
import org.dynmap.render.CaveTileRenderer;
|
||||
import org.dynmap.render.CombinedTileRenderer;
|
||||
import org.dynmap.render.DayTileRenderer;
|
||||
import org.dynmap.render.MapTileRenderer;
|
||||
import org.dynmap.kzedmap.CaveTileRenderer;
|
||||
import org.dynmap.kzedmap.DayTileRenderer;
|
||||
import org.dynmap.kzedmap.KzedMap;
|
||||
import org.dynmap.kzedmap.MapTileRenderer;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
|
|
@ -47,37 +47,15 @@ public class MapManager extends Thread {
|
|||
|
||||
private World world;
|
||||
private Debugger debugger;
|
||||
private MapTileRenderer renderer;
|
||||
|
||||
/* dimensions of a map tile */
|
||||
public static final int tileWidth = 128;
|
||||
public static final int tileHeight = 128;
|
||||
|
||||
/* (logical!) dimensions of a zoomed out map tile
|
||||
* must be twice the size of the normal tile */
|
||||
public static final int zTileWidth = 256;
|
||||
public static final int zTileHeight = 256;
|
||||
private org.dynmap.Map map;
|
||||
public StaleQueue staleQueue;
|
||||
|
||||
/* lock for our data structures */
|
||||
public static final Object lock = new Object();
|
||||
|
||||
/* a hash table of known MapTiles, by their key (projection coords) */
|
||||
private HashMap<Long, MapTile> tileStore;
|
||||
|
||||
/* a list of MapTiles to be updated */
|
||||
private LinkedList<MapTile> staleTiles;
|
||||
|
||||
/* this list stores the tile updates */
|
||||
public LinkedList<TileUpdate> tileUpdates = null;
|
||||
|
||||
/* whether the worker thread should be running now */
|
||||
private boolean running = false;
|
||||
|
||||
/* map x, y, z for projection origin */
|
||||
public static final int anchorx = 0;
|
||||
public static final int anchory = 127;
|
||||
public static final int anchorz = 0;
|
||||
|
||||
/* path to colors.txt */
|
||||
private String colorsetpath = "colors.txt";
|
||||
|
||||
|
|
@ -95,9 +73,6 @@ public class MapManager extends Thread {
|
|||
|
||||
/* time to pause between rendering tiles (ms) */
|
||||
public int renderWait = 500;
|
||||
|
||||
/* remember up to this old tile updates (ms) */
|
||||
private static final int maxTileAge = 60000;
|
||||
|
||||
public void debug(String msg)
|
||||
{
|
||||
|
|
@ -108,6 +83,7 @@ public class MapManager extends Thread {
|
|||
{
|
||||
this.world = world;
|
||||
this.debugger = debugger;
|
||||
this.staleQueue = new StaleQueue();
|
||||
|
||||
tilepath = "/srv/http/dynmap/tiles/";
|
||||
colorsetpath = "colors.txt";
|
||||
|
|
@ -115,55 +91,14 @@ public class MapManager extends Thread {
|
|||
bindaddress = "0.0.0.0";
|
||||
//webPath = "/srv/http/dynmap/";
|
||||
webPath = "[JAR]";
|
||||
|
||||
tileStore = new HashMap<Long, MapTile>();
|
||||
staleTiles = new LinkedList<MapTile>();
|
||||
tileUpdates = new LinkedList<TileUpdate>();
|
||||
|
||||
Map<Integer, Color[]> colors = loadColorSet(colorsetpath);
|
||||
renderer = new CombinedTileRenderer(new MapTileRenderer[] {
|
||||
map = new KzedMap(world, staleQueue, new MapTileRenderer[] {
|
||||
new DayTileRenderer(debugger, colors, tilepath + "t_{X}_{Y}.png"),
|
||||
new CaveTileRenderer(debugger, colors, tilepath + "ct_{X}_{Y}.png")
|
||||
});
|
||||
}
|
||||
|
||||
/* tile X for position x */
|
||||
static int tilex(int x)
|
||||
{
|
||||
if(x < 0)
|
||||
return x - (tileWidth + (x % tileWidth));
|
||||
else
|
||||
return x - (x % tileWidth);
|
||||
}
|
||||
|
||||
/* tile Y for position y */
|
||||
static int tiley(int y)
|
||||
{
|
||||
if(y < 0)
|
||||
return y - (tileHeight + (y % tileHeight));
|
||||
else
|
||||
return y - (y % tileHeight);
|
||||
}
|
||||
|
||||
/* zoomed-out tile X for tile position x */
|
||||
static int ztilex(int x)
|
||||
{
|
||||
if(x < 0)
|
||||
return x + x % zTileWidth;
|
||||
else
|
||||
return x - (x % zTileWidth);
|
||||
}
|
||||
|
||||
/* zoomed-out tile Y for tile position y */
|
||||
static int ztiley(int y)
|
||||
{
|
||||
if(y < 0)
|
||||
return y + y % zTileHeight;
|
||||
//return y - (zTileHeight + (y % zTileHeight));
|
||||
else
|
||||
return y - (y % zTileHeight);
|
||||
}
|
||||
|
||||
/* initialize and start map manager */
|
||||
public void startManager()
|
||||
{
|
||||
|
|
@ -193,23 +128,6 @@ public class MapManager extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
/* update tile update list */
|
||||
private void updateUpdates(MapTile t, LinkedList<TileUpdate> lst)
|
||||
{
|
||||
long now = System.currentTimeMillis();
|
||||
long deadline = now - maxTileAge;
|
||||
|
||||
synchronized(lock) {
|
||||
ListIterator<TileUpdate> it = lst.listIterator(0);
|
||||
while(it.hasNext()) {
|
||||
TileUpdate tu = it.next();
|
||||
if(tu.at < deadline || tu.tile == t)
|
||||
it.remove();
|
||||
}
|
||||
lst.addLast(new TileUpdate(now, t));
|
||||
}
|
||||
}
|
||||
|
||||
/* the worker/renderer thread */
|
||||
public void run()
|
||||
{
|
||||
|
|
@ -218,12 +136,12 @@ public class MapManager extends Thread {
|
|||
while(running) {
|
||||
boolean found = false;
|
||||
|
||||
MapTile t = this.popStaleTile();
|
||||
MapTile t = staleQueue.popStaleTile();
|
||||
if(t != null) {
|
||||
debugger.debug("rendering tile " + t + "...");
|
||||
renderer.render(t);
|
||||
t.getMap().render(t);
|
||||
|
||||
updateUpdates(t, tileUpdates);
|
||||
staleQueue.freshenTile(t);
|
||||
|
||||
try {
|
||||
this.sleep(renderWait);
|
||||
|
|
@ -244,306 +162,9 @@ public class MapManager extends Thread {
|
|||
log.info("Map renderer has stopped.");
|
||||
}
|
||||
|
||||
/* "touch" a block - its map tile will be regenerated */
|
||||
public boolean touch(int x, int y, int z)
|
||||
{
|
||||
debugger.debug("touched: " + x + "," + y + "," + z);
|
||||
|
||||
int dx = x - anchorx;
|
||||
int dy = y - anchory;
|
||||
int dz = z - anchorz;
|
||||
int px = dx + dz;
|
||||
int py = dx - dz - dy;
|
||||
|
||||
int tx = tilex(px);
|
||||
int ty = tiley(py);
|
||||
|
||||
boolean r;
|
||||
|
||||
r = pushStaleTile(tx, ty);
|
||||
|
||||
boolean ledge = tilex(px - 4) != tx;
|
||||
boolean tedge = tiley(py - 4) != ty;
|
||||
boolean redge = tilex(px + 4) != tx;
|
||||
boolean bedge = tiley(py + 4) != ty;
|
||||
|
||||
if(ledge)
|
||||
r = pushStaleTile(tx - tileWidth, ty) || r;
|
||||
if(redge)
|
||||
r = pushStaleTile(tx + tileWidth, ty) || r;
|
||||
if(tedge)
|
||||
r = pushStaleTile(tx, ty - tileHeight) || r;
|
||||
if(bedge)
|
||||
r = pushStaleTile(tx, ty + tileHeight) || r;
|
||||
|
||||
if(ledge && tedge)
|
||||
r = pushStaleTile(tx - tileWidth, ty - tileHeight) || r;
|
||||
if(ledge && bedge)
|
||||
r = pushStaleTile(tx - tileWidth, ty + tileHeight) || r;
|
||||
if(redge && tedge)
|
||||
r = pushStaleTile(tx + tileWidth, ty - tileHeight) || r;
|
||||
if(redge && bedge)
|
||||
r = pushStaleTile(tx + tileWidth, ty + tileHeight) || r;
|
||||
|
||||
return r;
|
||||
public void touch(int x, int y, int z) {
|
||||
map.touch(new Location(world, x, y, z));
|
||||
}
|
||||
|
||||
/* get next MapTile that needs to be regenerated, or null
|
||||
* the mapTile is removed from the list of stale tiles! */
|
||||
public MapTile popStaleTile()
|
||||
{
|
||||
synchronized(lock) {
|
||||
try {
|
||||
MapTile t = staleTiles.removeFirst();
|
||||
t.stale = false;
|
||||
return t;
|
||||
} catch(NoSuchElementException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* put a MapTile that needs to be regenerated on the list of stale tiles */
|
||||
public boolean pushStaleTile(MapTile m)
|
||||
{
|
||||
synchronized(lock) {
|
||||
boolean ret = false;
|
||||
|
||||
if(!m.stale) {
|
||||
m.stale = true;
|
||||
staleTiles.addLast(m);
|
||||
|
||||
debug(m.toString() + " is now stale");
|
||||
|
||||
ret = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/* make a MapTile stale by projection position */
|
||||
public boolean pushStaleTile(int tx, int ty)
|
||||
{
|
||||
return pushStaleTile(getTileByPosition(tx, ty));
|
||||
}
|
||||
|
||||
/* get (or create) MapTile by projection position */
|
||||
private MapTile getTileByPosition(int px, int py)
|
||||
{
|
||||
Long key = MapTile.key(px, py);
|
||||
synchronized(lock) {
|
||||
MapTile t = tileStore.get(key);
|
||||
if(t == null) {
|
||||
/* no maptile exists, need to create one */
|
||||
|
||||
t = new MapTile(world, px, py, ztilex(px), ztiley(py));
|
||||
tileStore.put(key, t);
|
||||
return t;
|
||||
} else {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* return number of stale tiles */
|
||||
public int getStaleCount()
|
||||
{
|
||||
synchronized(lock) {
|
||||
return staleTiles.size();
|
||||
}
|
||||
}
|
||||
|
||||
/* return number of recently updated tiles */
|
||||
public int getRecentUpdateCount()
|
||||
{
|
||||
synchronized(lock) {
|
||||
return tileUpdates.size();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// regenerate the entire map, starting at position
|
||||
public void regenerate(int x, int y, int z)
|
||||
{
|
||||
int dx = x - anchorx;
|
||||
int dy = y - anchory;
|
||||
int dz = z - anchorz;
|
||||
int px = dx + dz;
|
||||
int py = dx - dz - dy;
|
||||
|
||||
int tx = tilex(px);
|
||||
int ty = tiley(py);
|
||||
|
||||
MapTile first = getTileByPosition(tx, ty);
|
||||
|
||||
Vector<MapTile> open = new Vector<MapTile>();
|
||||
open.add(first);
|
||||
|
||||
while(open.size() > 0) {
|
||||
MapTile t = open.remove(open.size() - 1);
|
||||
if(t.stale) continue;
|
||||
int h = world.getHighestBlockYAt(t.mx, t.mz);
|
||||
|
||||
log.info("walking: " + t.mx + ", " + t.mz + ", h = " + h);
|
||||
if(h < 1)
|
||||
continue;
|
||||
|
||||
pushStaleTile(t);
|
||||
|
||||
open.add(getTileByPosition(t.px + tileWidth, t.py));
|
||||
open.add(getTileByPosition(t.px - tileWidth, t.py));
|
||||
open.add(getTileByPosition(t.px, t.py + tileHeight));
|
||||
open.add(getTileByPosition(t.px, t.py - tileHeight));
|
||||
}
|
||||
}
|
||||
|
||||
// regenerate all zoom tiles, starting at position
|
||||
public void regenerateZoom(int x, int y, int z)
|
||||
{
|
||||
int dx = x - anchorx;
|
||||
int dy = y - anchory;
|
||||
int dz = z - anchorz;
|
||||
int px = dx + dz;
|
||||
int py = dx - dz - dy;
|
||||
|
||||
int fzpx = ztilex(tilex(px));
|
||||
int fzpy = ztiley(tiley(py));
|
||||
|
||||
class Pair implements Comparator {
|
||||
public int x;
|
||||
public int y;
|
||||
public Pair(int x, int y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int hashCode()
|
||||
{
|
||||
return (x << 16) ^ y;
|
||||
}
|
||||
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
Pair p = (Pair) o;
|
||||
return x == p.x && y == p.y;
|
||||
}
|
||||
|
||||
public int compare(Object o1, Object o2)
|
||||
{
|
||||
Pair p1 = (Pair) o1;
|
||||
Pair p2 = (Pair) o2;
|
||||
if(p1.x < p1.x) return -1;
|
||||
if(p1.x > p1.x) return 1;
|
||||
if(p1.y < p1.y) return -1;
|
||||
if(p1.y > p1.y) return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
HashSet<Pair> visited = new HashSet<Pair>();
|
||||
Vector<Pair> open = new Vector<Pair>();
|
||||
|
||||
Pair fp = new Pair(fzpx, fzpy);
|
||||
open.add(fp);
|
||||
visited.add(fp);
|
||||
|
||||
while(open.size() > 0) {
|
||||
Pair p = open.remove(open.size() - 1);
|
||||
|
||||
int zpx = p.x;
|
||||
int zpy = p.y;
|
||||
|
||||
log.info("Regenerating zoom tile " + zpx + "," + zpy);
|
||||
|
||||
int g = regenZoomTile(zpx, zpy);
|
||||
|
||||
if(g > 0) {
|
||||
Pair[] np = new Pair[4];
|
||||
np[0] = new Pair(zpx-zTileWidth, zpy);
|
||||
np[1] = new Pair(zpx+zTileWidth, zpy);
|
||||
np[2] = new Pair(zpx, zpy-zTileHeight);
|
||||
np[3] = new Pair(zpx, zpy+zTileHeight);
|
||||
|
||||
for(int i=0; i<4; i++) {
|
||||
if(!visited.contains(np[i])) {
|
||||
visited.add(np[i]);
|
||||
open.add(np[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// regenerate zoom-out tile
|
||||
// returns number of valid subtiles
|
||||
public int regenZoomTile(int zpx, int zpy)
|
||||
{
|
||||
int px1 = zpx + tileWidth;
|
||||
int py1 = zpy;
|
||||
int px2 = zpx;
|
||||
int py2 = py1 + tileHeight;
|
||||
|
||||
MapTile t1 = getTileByPosition(px1, py1);
|
||||
MapTile t2 = getTileByPosition(px2, py1);
|
||||
MapTile t3 = getTileByPosition(px1, py2);
|
||||
MapTile t4 = getTileByPosition(px2, py2);
|
||||
|
||||
BufferedImage im1 = t1.loadTile(this);
|
||||
BufferedImage im2 = t2.loadTile(this);
|
||||
BufferedImage im3 = t3.loadTile(this);
|
||||
BufferedImage im4 = t4.loadTile(this);
|
||||
|
||||
BufferedImage zIm = new BufferedImage(MapManager.tileWidth, MapManager.tileHeight, BufferedImage.TYPE_INT_RGB);
|
||||
WritableRaster zr = zIm.getRaster();
|
||||
Graphics2D g2 = zIm.createGraphics();
|
||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
|
||||
int scw = tileWidth / 2;
|
||||
int sch = tileHeight / 2;
|
||||
|
||||
int good = 0;
|
||||
|
||||
if(im1 != null) {
|
||||
g2.drawImage(im1, 0, 0, scw, sch, null);
|
||||
good ++;
|
||||
}
|
||||
|
||||
if(im2 != null) {
|
||||
g2.drawImage(im2, scw, 0, scw, sch, null);
|
||||
good ++;
|
||||
}
|
||||
|
||||
if(im3 != null) {
|
||||
g2.drawImage(im3, 0, sch, scw, sch, null);
|
||||
good ++;
|
||||
}
|
||||
|
||||
if(im4 != null) {
|
||||
g2.drawImage(im4, scw, sch, scw, sch, null);
|
||||
good ++;
|
||||
}
|
||||
|
||||
if(good == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
String zPath = t1.getZoomPath(this);
|
||||
// save zoom-out tile
|
||||
try {
|
||||
File file = new File(zPath);
|
||||
ImageIO.write(zIm, "png", file);
|
||||
log.info("regenZoomTile saved zoom-out tile at " + zPath);
|
||||
} catch(IOException e) {
|
||||
log.log(Level.SEVERE, "Failed to save zoom-out tile: " + zPath, e);
|
||||
} catch(java.lang.NullPointerException e) {
|
||||
log.log(Level.SEVERE, "Failed to save zoom-out tile (NullPointerException): " + zPath, e);
|
||||
}
|
||||
|
||||
return good;
|
||||
}
|
||||
*/
|
||||
|
||||
public Map<Integer, Color[]> loadColorSet(String colorsetpath) {
|
||||
Map<Integer, Color[]> colors = new HashMap<Integer, Color[]>();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue