Cleanup/Refactoring.

This commit is contained in:
FrozenCow 2011-01-08 18:56:08 +01:00
parent 8860eb8c04
commit e1a3ac60e2
10 changed files with 119 additions and 116 deletions

View file

@ -7,10 +7,10 @@ import org.bukkit.World;
import org.dynmap.MapManager;
import org.dynmap.debug.Debugger;
public class CaveTileRenderer extends DayTileRenderer {
public class CaveTileRenderer extends DefaultTileRenderer {
public CaveTileRenderer(Debugger debugger, Map<Integer, Color[]> colors, String outputPath) {
super(debugger, colors, outputPath);
public CaveTileRenderer(String name, Debugger debugger) {
super(name, debugger);
}
@Override

View file

@ -18,30 +18,20 @@ import org.dynmap.MapManager;
import org.dynmap.MapTile;
import org.dynmap.debug.Debugger;
public class DayTileRenderer implements MapTileRenderer {
public class DefaultTileRenderer implements MapTileRenderer {
private String name;
protected Debugger debugger;
protected String outputPath;
protected String outputZoomPath;
private Map<Integer, Color[]> colors;
public DayTileRenderer(Debugger debugger, Map<Integer, Color[]> colors, String outputPath) {
this(debugger, colors, outputPath, convertToZoomPath(outputPath));
public String getName() {
return name;
}
public DayTileRenderer(Debugger debugger, Map<Integer, Color[]> colors, String outputPath, String outputZoomPath) {
public DefaultTileRenderer(String name, Debugger debugger) {
this.name = name;
this.debugger = debugger;
this.colors = colors;
this.outputPath = outputPath;
this.outputZoomPath = outputZoomPath;
}
private static String convertToZoomPath(String outputPath) {
File outputFile = new File(outputPath);
String zoomFilename = "z" + outputFile.getName();
return new File(outputFile.getParentFile(), zoomFilename).getPath();
}
public void render(KzedMapTile tile) {
public void render(KzedMapTile tile, String path) {
World world = tile.getMap().getWorld();
BufferedImage im = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB);
@ -93,7 +83,7 @@ public class DayTileRenderer implements MapTileRenderer {
}
/* save the generated tile */
saveTile(tile, im);
saveTile(tile, im, path);
}
protected Color scan(World world, int x, int y, int z, int seq)
@ -122,7 +112,7 @@ public class DayTileRenderer implements MapTileRenderer {
seq = (seq + 1) & 3;
if(id != 0) {
Color[] colors = this.colors.get(id);
Color[] colors = KzedMap.colors.get(id);
if(colors != null) {
Color c = colors[seq];
if(c.getAlpha() > 0) {
@ -152,11 +142,9 @@ public class DayTileRenderer implements MapTileRenderer {
}
/* save rendered tile, update zoom-out tile */
public void saveTile(KzedMapTile tile, BufferedImage im)
public void saveTile(KzedMapTile tile, BufferedImage im, String path)
{
String tilePath = outputPath
.replace("{X}", Integer.toString(tile.px))
.replace("{Y}", Integer.toString(tile.py));
String tilePath = getPath(tile, path);
debugger.debug("saving tile " + tilePath);
@ -237,8 +225,13 @@ public class DayTileRenderer implements MapTileRenderer {
}*/
}
public String getPath(KzedMapTile tile, String outputPath)
{
return new File(new File(outputPath), tile.getName() + ".png").getPath();
}
/* try to load already generated image */
public BufferedImage loadTile(KzedMapTile tile)
/*public BufferedImage loadTile(KzedMapTile tile)
{
try {
String path = getPath(tile);
@ -252,14 +245,7 @@ public class DayTileRenderer implements MapTileRenderer {
}
return null;
}
public String getPath(KzedMapTile tile)
{
return outputPath
.replace("{X}", Integer.toString(tile.px))
.replace("{Y}", Integer.toString(tile.py));
}
}*/
/*
// generate a path name for this map tile

View file

@ -1,14 +1,20 @@
package org.dynmap.kzedmap;
import java.awt.Color;
import java.io.File;
import java.util.HashMap;
import java.util.Scanner;
import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.World;
import org.dynmap.Map;
import org.dynmap.MapManager;
import org.dynmap.MapTile;
import org.dynmap.StaleQueue;
import org.dynmap.debug.Debugger;
public class KzedMap extends Map {
MapTileRenderer[] renderers;
/* dimensions of a map tile */
public static final int tileWidth = 128;
public static final int tileHeight = 128;
@ -23,9 +29,17 @@ public class KzedMap extends Map {
public static final int anchory = 127;
public static final int anchorz = 0;
public KzedMap(World world, StaleQueue queue, MapTileRenderer[] renderers) {
super(world, queue);
this.renderers = renderers;
public static java.util.Map<Integer, Color[]> colors;
MapTileRenderer[] renderers;
public KzedMap(MapManager manager, World world, Debugger debugger) {
super(manager, world, debugger);
if (colors == null)
colors = loadColorSet("colors.txt");
renderers = new MapTileRenderer[] {
new DefaultTileRenderer("t", debugger),
new CaveTileRenderer("ct", debugger)
};
}
@Override
@ -62,15 +76,15 @@ public class KzedMap extends Map {
}
public void invalidateTile(int px, int py) {
invalidateTile(new KzedMapTile(this, "t", px, py, ztilex(px), ztiley(py)));
for(MapTileRenderer renderer : renderers) {
invalidateTile(new KzedMapTile(this, renderer, px, py));
}
}
@Override
public void render(MapTile tile) {
KzedMapTile t = (KzedMapTile)tile;
for(MapTileRenderer renderer : renderers) {
renderer.render(t);
}
t.renderer.render(t, getMapManager().tilepath);
}
/* tile X for position x */
@ -292,4 +306,44 @@ public class KzedMap extends Map {
return good;
}
*/
public static java.util.Map<Integer, Color[]> loadColorSet(String colorsetpath) {
java.util.Map<Integer, Color[]> colors = new HashMap<Integer, Color[]>();
/* load colorset */
File cfile = new File(colorsetpath);
try {
Scanner scanner = new Scanner(cfile);
int nc = 0;
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#") || line.equals("")) {
continue;
}
String[] split = line.split("\t");
if (split.length < 17) {
continue;
}
Integer id = new Integer(split[0]);
Color[] c = new Color[4];
/* store colors by raycast sequence number */
c[0] = new Color(Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]));
c[3] = new Color(Integer.parseInt(split[5]), Integer.parseInt(split[6]), Integer.parseInt(split[7]), Integer.parseInt(split[8]));
c[1] = new Color(Integer.parseInt(split[9]), Integer.parseInt(split[10]), Integer.parseInt(split[11]), Integer.parseInt(split[12]));
c[2] = new Color(Integer.parseInt(split[13]), Integer.parseInt(split[14]), Integer.parseInt(split[15]), Integer.parseInt(split[16]));
colors.put(id, c);
nc += 1;
}
scanner.close();
} catch(Exception e) {
return null;
}
return colors;
}
}

View file

@ -21,25 +21,20 @@ public class KzedMapTile extends MapTile {
public KzedMap map;
public String prefix;
public MapTileRenderer renderer;
/* projection position */
public int px, py;
/* projection position of zoom-out tile */
public int zpx, zpy;
/* minecraft space origin */
public int mx, my, mz;
/* create new MapTile */
public KzedMapTile(KzedMap map, String prefix, int px, int py, int zpx, int zpy) {
public KzedMapTile(KzedMap map, MapTileRenderer renderer, int px, int py) {
super(map);
this.prefix = prefix;
this.renderer = renderer;
this.px = px;
this.py = py;
this.zpx = zpx;
this.zpy = zpy;
mx = KzedMap.anchorx + px / 2 + py / 2;
my = KzedMap.anchory;
@ -48,7 +43,7 @@ public class KzedMapTile extends MapTile {
@Override
public String getName() {
return prefix + "_" + this.px + "_" + this.py;
return renderer.getName() + "_" + px + "_" + py;
}
/* try to get the server to load the relevant chunks */
@ -126,7 +121,7 @@ public class KzedMapTile extends MapTile {
/* hash value, based on projection position */
public int hashCode()
{
return (px << 16) ^ py;
return ((px << 16) ^ py) ^ getName().hashCode();
}
@Override
@ -140,12 +135,12 @@ public class KzedMapTile extends MapTile {
/* equality comparison - based on projection position */
public boolean equals(KzedMapTile o)
{
return o.px == px && o.py == py;
return o.getName().equals(getName()) && o.px == px && o.py == py;
}
/* return a simple string representation... */
public String toString()
{
return px + "_" + py;
return getName() + "_" + px + "_" + py;
}
}

View file

@ -3,5 +3,6 @@ package org.dynmap.kzedmap;
import org.dynmap.MapTile;
public interface MapTileRenderer {
void render(KzedMapTile tile);
String getName();
void render(KzedMapTile tile, String path);
}