package ca.recrown.islandsurvivalcraft.world.generation;

import java.util.Random;

import org.bukkit.util.noise.SimplexOctaveGenerator;

import ca.recrown.islandsurvivalcraft.datatypes.Point2;
import ca.recrown.islandsurvivalcraft.caching.Cache;

class TemperatureMapGenerator {
    private final Cache<Point2, Float> temperatureCache;
    private final double frequency = 0.5D;
    private final double amplitude = 0.5D;

    private volatile SimplexOctaveGenerator noiseGenerator;

    public TemperatureMapGenerator() {
        temperatureCache = new Cache<>(1024);
    }

    public TemperatureMapGenerator(long seed) {
        this();
        setSeed(seed);
    }

    public void setSeed(long seed) {
        noiseGenerator = new SimplexOctaveGenerator(new Random(seed), 2);
        noiseGenerator.setScale(0.005D);
    }

    public float getTemperature(int worldX, int worldZ) {
        Point2 loc = new Point2(worldX/4, worldZ/4);
        Float val = temperatureCache.get(loc);
        if (val == null) {
            val = (float) noiseGenerator.noise(worldX/4, worldZ/4, frequency, amplitude, true);
            temperatureCache.set(loc, val);
        }
        return val;
    }
}