package core.engine.terrain;

import utils.vectors.Vector3f;

import java.util.Random;

public class TerrainGenerator {

    private final Random random = new Random();
    private final int seed_red;
    private final int seed_blue;
    private final int seed_green;

    public TerrainGenerator() {
        this.seed_red = random.nextInt(1000000000);
        this.seed_blue =  random.nextInt(1000000000);
        this.seed_green =  random.nextInt(1000000000);
    }

    public Vector3f generateColor(int x, int z) {
        return new Vector3f(getSmoothNoise(x, z, seed_red), getSmoothNoise(x, z, seed_green), getSmoothNoise(x, z, seed_blue));
    }

    private float getSmoothNoise(int x, int z, int seed) {
        float corners = (getNoise(x -1, z-1, seed) + getNoise(x+1, z-1, seed) +
                getNoise(x-1, z+1, seed) + getNoise(x+1, z+1, seed)) / 16f;
        float sides = (getNoise(x-1, z, seed) + getNoise(x+1, z, seed) +
                getNoise(x, z-1, seed) + getNoise(x, z+1, seed)) / 8f;
        float center = getNoise(x, z, seed) / 4f;
        return corners + sides + center;
    }

    private float getNoise(int x, int z, int seed) {
        random.setSeed(x * 49632L + z * 325176L + seed);
        return random.nextFloat();
    }
}
