package ru.dbotthepony.kstarbound.util.random

import ru.dbotthepony.kstarbound.defs.PerlinNoiseParameters

class PerlinNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(parameters) {
	init {
		require(parameters.type == PerlinNoiseParameters.Type.PERLIN)
	}

	override fun get(x: Double): Double {
		checkInit()
		var sum = 0.0
		var p = x * parameters.frequency
		var scale = 1.0

		for (i in 0 until parameters.octaves) {
			sum += noise1(p) / scale
			scale *= parameters.alpha
			p *= parameters.beta
		}

		return sum * parameters.amplitude + parameters.bias
	}

	override fun get(x: Double, y: Double): Double {
		checkInit()
		var sum = 0.0
		var px = x * parameters.frequency
		var py = y * parameters.frequency
		var scale = 1.0

		for (i in 0 until parameters.octaves) {
			sum += noise2(px, py) / scale
			scale *= parameters.alpha
			px *= parameters.beta
			py *= parameters.beta
		}

		return sum * parameters.amplitude + parameters.bias
	}

	override fun get(x: Double, y: Double, z: Double): Double {
		checkInit()
		var sum = 0.0
		var px = x * parameters.frequency
		var py = y * parameters.frequency
		var pz = z * parameters.frequency
		var scale = 1.0

		for (i in 0 until parameters.octaves) {
			sum += noise3(px, py, pz) / scale
			scale *= parameters.alpha
			px *= parameters.beta
			py *= parameters.beta
			pz *= parameters.beta
		}

		return sum * parameters.amplitude + parameters.bias
	}
}
