package net.minecraft.world.level.levelgen.synth;

import net.minecraft.SharedConstants;
import net.minecraft.util.RandomSource;

public abstract class GradientNoise implements Noise {
	protected static final GradientNoise.Gradient[] GRADIENT = new GradientNoise.Gradient[]{
		new GradientNoise.Gradient(1, 1, 0),
		new GradientNoise.Gradient(-1, 1, 0),
		new GradientNoise.Gradient(1, -1, 0),
		new GradientNoise.Gradient(-1, -1, 0),
		new GradientNoise.Gradient(1, 0, 1),
		new GradientNoise.Gradient(-1, 0, 1),
		new GradientNoise.Gradient(1, 0, -1),
		new GradientNoise.Gradient(-1, 0, -1),
		new GradientNoise.Gradient(0, 1, 1),
		new GradientNoise.Gradient(0, -1, 1),
		new GradientNoise.Gradient(0, 1, -1),
		new GradientNoise.Gradient(0, -1, -1),
		new GradientNoise.Gradient(1, 1, 0),
		new GradientNoise.Gradient(0, -1, 1),
		new GradientNoise.Gradient(-1, 1, 0),
		new GradientNoise.Gradient(0, -1, -1)
	};
	private static final int ROUND_OFF = 33554432;
	private static final double HALF_ROUND_OFF = Math.nextDown(1.6777216E7);
	protected final byte[] perms = new byte[256];
	protected final double offsetX;
	protected final double offsetY;
	protected final double offsetZ;

	protected GradientNoise(final RandomSource random) {
		this(random, 256.0);
	}

	protected GradientNoise(final RandomSource random, final double noiseOffsetScale) {
		this.offsetX = random.nextDouble() * noiseOffsetScale;
		this.offsetY = random.nextDouble() * noiseOffsetScale;
		this.offsetZ = random.nextDouble() * noiseOffsetScale;

		for (int i = 0; i < 256; i++) {
			this.perms[i] = (byte)i;
		}

		for (int i = 0; i < 256; i++) {
			int offset = random.nextInt(256 - i);
			byte tmp = this.perms[i];
			this.perms[i] = this.perms[offset + i];
			this.perms[offset + i] = tmp;
		}
	}

	protected int permute(final int x) {
		return this.perms[x & 0xFF] & 0xFF;
	}

	protected GradientNoise.Gradient permuteToGrad(final int x) {
		return GRADIENT[this.permute(x) & 15];
	}

	protected static float gradDot(final int hash, final float x, final float y, final float z) {
		return GRADIENT[hash & 15].dot(x, y, z);
	}

	protected static double wrap(final double x) {
		if (SharedConstants.DEBUG_ENABLE_FARLANDS) {
			return x;
		} else {
			return x >= -HALF_ROUND_OFF && x < HALF_ROUND_OFF ? x : x - Math.floor(x / 3.3554432E7 + 0.5) * 3.3554432E7;
		}
	}

	protected record Gradient(int x, int y, int z) {
		public double dot(final double x, final double y, final double z) {
			return this.x * x + this.y * y + this.z * z;
		}

		public float dot(final float x, final float y, final float z) {
			return this.x * x + this.y * y + this.z * z;
		}

		public float dotXz(final float x, final float z) {
			return this.x * x + this.z * z;
		}
	}
}
