package common.rng;

public class OctaveGen extends NoiseGen {
	private final ImprovedGen[] generators;
	private final int octaves;
	
    private static long floorLong(double value) {
        long i = (long)value;
        return value < (double)i ? i - 1L : i;
    }

	public OctaveGen(Random random, int octs) {
		this.octaves = octs;
		this.generators = new ImprovedGen[octs];

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

	public void generate(double[] noise, int xoff, int yoff, int zoff, int xsize, int ysize, int zsize, double xscale, double yscale,
			double zscale) {
//		if(noise == null) {
//			noise = new double[xsize * ysize * zsize];
//		}
//		else {
			for(int n = 0; n < noise.length; ++n) {
				noise[n] = 0.0D;
			}
//		}

		double factor = 1.0D;

		for(int n = 0; n < this.octaves; ++n) {
			double xo = (double)xoff * factor * xscale;
			double yo = (double)yoff * factor * yscale;
			double zo = (double)zoff * factor * zscale;
			long xn = floorLong(xo);
			long zn = floorLong(zo);
			xo = xo - (double)xn;
			zo = zo - (double)zn;
			xn = xn % 16777216L;
			zn = zn % 16777216L;
			xo = xo + (double)xn;
			zo = zo + (double)zn;
			this.generators[n].generate(noise, xo, yo, zo, xsize, ysize, zsize, xscale * factor, yscale * factor, zscale * factor, factor);
			factor /= 2.0D;
		}

//		return noise;
	}
}
