package common.rng;

public class PerlinGen {
	private final SimplexGen[] generators;
	private final int iterations;

	public PerlinGen(Random random, int iters) {
		this.iterations = iters;
		this.generators = new SimplexGen[iters];

		for(int n = 0; n < iters; ++n) {
			this.generators[n] = new SimplexGen(random);
		}
	}

	public double generate(double x, double z) {
		double sum = 0.0D;
		double factor = 1.0D;

		for(int n = 0; n < this.iterations; ++n) {
			sum += this.generators[n].generate(x * factor, z * factor) / factor;
			factor /= 2.0D;
		}

		return sum;
	}

	public void generate(double[] noise, double xoff, double zoff, int xsize, int zsize, double xscale, double zscale, double amp) {
		double mult = 0.5D;
		for(int n = 0; n < noise.length; ++n) {
			noise[n] = 0.0D;
		}
		double div = 1.0D;
		double scale = 1.0D;

		for(int n = 0; n < this.iterations; ++n) {
			this.generators[n].generate(noise, xoff, zoff, xsize, zsize, xscale * scale * div, zscale * scale * div, 0.55D / div);
			scale *= amp;
			div *= mult;
		}
	}
}
