package Hexel.generation.terrainGenerator.originalPlateGenerator.plate;
import java.util.concurrent.ConcurrentHashMap;
import Hexel.math.Vector2i;
public class PlateSumChunks {
private ConcurrentHashMap<Vector2i, PlateSumChunk> inMemoryPSCs = new ConcurrentHashMap<Vector2i, PlateSumChunk>();
private Object[] locks = new Object[100];
PlateSumChunkGenerator pscg;
public PlateSumChunks() {
this.pscg = new PlateSumChunkGenerator();
for (int i = 0; i < this.locks.length; i++) {
this.locks[i] = new Object();
}
}
public PlateSumChunk get(int x, int y) {
return this.get(new Vector2i(x, y));
}
public PlateSumChunk get(Vector2i ppos) {
if (hasPSCInMemory(ppos)) {
return this.inMemoryPSCs.get(ppos);
}
maybeGenPSC(ppos);
return this.inMemoryPSCs.get(ppos);
}
private void maybeGenPSC(Vector2i ppos) {
int hash = Math.abs(ppos.hashCode() % this.locks.length);
synchronized (this.locks[hash]) {
if (hasPSCInMemory(ppos))
return;
PlateSumChunk psc = this.pscg.gen(ppos.x, ppos.y);
this.inMemoryPSCs.put(ppos, psc);
}
}
private boolean hasPSCInMemory(Vector2i ppos) {
return this.inMemoryPSCs.containsKey(ppos);
}
public void unloadPSC(Vector2i ppos) {
this.inMemoryPSCs.remove(ppos);
}
public PlateSum getPlateSum(int x, int y) {
int px = (int) Math.floor(x * 1.0 / PlateChunk.WIDTH);
int py = (int) Math.floor(y * 1.0 / PlateChunk.HEIGHT);
x -= px * PlateChunk.WIDTH;
y -= py * PlateChunk.HEIGHT;
return this.get(px, py).plateSums[x][y];
}
}