Bleeding: Switch BlockCache to use CoordMap.

This commit is contained in:
asofold 2012-11-03 05:58:11 +01:00
parent dc0239fd4c
commit 01a8fe94ec

View File

@ -1,8 +1,5 @@
package fr.neatmonster.nocheatplus.utilities;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.server.IBlockAccess;
import net.minecraft.server.Material;
import net.minecraft.server.TileEntity;
@ -11,70 +8,25 @@ import net.minecraft.server.Vec3DPool;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld;
import fr.neatmonster.nocheatplus.utilities.ds.CoordMap;
/**
* Access to type-ids and data using caching techniques.
* @author mc_dev
*
*/
public class BlockCache implements IBlockAccess{
/**
* TODO: Make a map for faster queries (without object creation).
* TODO: Not sure the prime numbers are too big for normal use.
* @author mc_dev
*
*/
private static class Pos3D{
private static final int p1 = 73856093;
private static final int p2 = 19349663;
private static final int p3 = 83492791;
// Cube coordinates:
public final int x;
public final int y;
public final int z;
public final int hash;
/**
*
* @param x
* @param y
* @param z
* @param size
*/
public Pos3D (final int x, final int y, final int z){
// Cube related coordinates:
this.x = x;
this.y = y;
this.z = z;
// Hash
hash = getHash(this.x, this.y, this.z);
}
@Override
public final boolean equals(final Object obj) {
if (obj instanceof Pos3D){
final Pos3D other = (Pos3D) obj;
return other.x == x && other.y == y && other.z == z;
}
else return false;
}
@Override
public final int hashCode() {
return hash;
}
public static final int getHash(final int x, final int y, final int z) {
return p1 * x ^ p2 * y ^ p3 * z;
}
}
/**
* For getting ids.
*/
private IBlockAccess access = null;
/** Cached type-ids. */
private final Map<Pos3D, Integer> idMap = new HashMap<Pos3D, Integer>();
/** Cahced data values. */
private final Map<Pos3D, Integer> dataMap = new HashMap<Pos3D, Integer>();
private final CoordMap<Integer> idMap = new CoordMap<Integer>();
/** Cached data values. */
private final CoordMap<Integer> dataMap = new CoordMap<Integer>();
// TODO: maybe make very fast access arrays for the ray tracing checks.
// private int[] id = null;
@ -117,23 +69,21 @@ public class BlockCache implements IBlockAccess{
}
public int getTypeId(final int x, final int y, final int z){
final Pos3D pos = new Pos3D(x, y, z);
final Integer pId = idMap.get(pos);
if (pId != null) return pId;
final Integer nId = access.getTypeId(x, y, z);
idMap.put(pos, nId);
return nId;
}
public int getData(final int x, final int y, final int z){
final Pos3D pos = new Pos3D(x, y, z);
final Integer pData = dataMap.get(pos);
if (pData != null) return pData;
final Integer nData = access.getData(x, y, z);
dataMap.put(pos, nData);
return nData;
}
public int getTypeId(final int x, final int y, final int z) {
final Integer pId = idMap.get(x, y, z);
if (pId != null) return pId;
final Integer nId = access.getTypeId(x, y, z);
idMap.put(x, y, z, nId);
return nId;
}
public int getData(final int x, final int y, final int z) {
final Integer pData = dataMap.get(x, y, z);
if (pData != null) return pData;
final Integer nData = access.getData(x, y, z);
dataMap.put(x, y, z, nData);
return nData;
}
/**
* Not Optimized.