package Hexel.generation.terrainGenerator.heightMaps;
import java.util.Random;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import Hexel.blocks.types.Block;
import Hexel.math.Vector2i;
import Hexel.util.Util;
public class WaterHeightMap {
private int HM_W = 256;
private int HM_H = 256;
private FractalHexagonalHeightMap hm;
private int seed;
private LoadingCache<Vector2i, HeightMap> hmapCache;
public WaterHeightMap(int seed, FractalHexagonalHeightMap hm){
this.seed = seed;
this.hm = hm;
this.hmapCache = CacheBuilder.newBuilder().maximumSize(100)
.removalListener(new RemovalListener<Vector2i, HeightMap>() {
@Override
public void onRemoval(
RemovalNotification<Vector2i, HeightMap> notification) { }
}).build(new CacheLoader<Vector2i, HeightMap>() {
@Override
public HeightMap load(Vector2i p) {
return genHeightMap(p);
}
});
}
private HeightMap getHeightMap(Vector2i p) {
try {
HeightMap h = this.hmapCache.getIfPresent(p);
if (h != null)
return h;
else
return this.hmapCache.get(new Vector2i(p));
} catch (Exception e) {
System.out.println("Caught Exception retrieving height map from cache!");
e.printStackTrace();
System.exit(1);
return null;
}
}
public int getHeight(int x, int y, Vector2i tmp) {
int cx = (int) Math.floor(x*1.0 / HM_W);
int cy = (int) Math.floor(y*1.0 / HM_H);
int bx = x - cx * HM_W;
int by = y - cy * HM_H;
tmp.x = cx;
tmp.y = cy;
return this.getHeightMap(tmp).map[bx][by];
}
private HeightMap genHeightMap(Vector2i p){
HeightMap h = new HeightMap(HM_W, HM_H, p.x, p.y);
for (int x = 0; x < HM_W; x++){
for (int y = 0; y < HM_H; y++){
h.map[x][y] = 0;
}
}
return h;
}
}