package ru.dbotthepony.kstarbound.util.random

import ru.dbotthepony.kstarbound.defs.PerlinNoiseParameters
import kotlin.math.absoluteValue

class BillowNoise(parameters: PerlinNoiseParameters) : AbstractPerlinNoise(parameters) {
	init {
		require(parameters.type == PerlinNoiseParameters.Type.BILLOW)
	}

	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 += (2.0 * noise1(p).absoluteValue - 1.0) / scale
			scale *= parameters.alpha
			p *= parameters.beta
		}

		return (sum + 0.5) * 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 += (2.0 * noise2(px, py).absoluteValue - 1.0) / scale
			scale *= parameters.alpha
			px *= parameters.beta
			py *= parameters.beta
		}

		return (sum + 0.5) * 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 += (2.0 * noise3(px, py, pz).absoluteValue - 1.0) / scale
			scale *= parameters.alpha
			px *= parameters.beta
			py *= parameters.beta
			pz *= parameters.beta
		}

		return (sum + 0.5) * parameters.amplitude + parameters.bias
	}
}
