world gen

pull/12/head
valoeghese 2020-02-26 22:23:01 +13:00
parent 7af779ff49
commit 8768c7583c
14 changed files with 1009 additions and 36 deletions

View File

@ -4,6 +4,7 @@ import java.util.Random;
import com.github.halotroop.litecraft.screens.TitleScreen;
import com.github.halotroop.litecraft.world.World;
import com.github.halotroop.litecraft.world.gen.Dimension;
import com.github.hydos.ginger.engine.api.*;
import com.github.hydos.ginger.engine.api.game.*;
import com.github.hydos.ginger.engine.cameras.*;
@ -18,6 +19,8 @@ import com.github.hydos.ginger.engine.render.models.TexturedModel;
import com.github.hydos.ginger.engine.utils.Loader;
import com.github.hydos.ginger.main.settings.Constants;
import tk.valoeghese.gateways.client.io.*;
public class Litecraft extends Game
{
private World world;
@ -36,6 +39,11 @@ public class Litecraft extends Game
Constants.gravity = new org.joml.Vector3f(0, -0.0000000005f, 0);
Constants.jumpPower = 0.00005f;
Window.create(1200, 800, "LiteCraft", 60);
KeyCallbackHandler.trackWindow(Window.window);
MouseCallbackHandler.trackWindow(Window.window);
setupKeybinds();
GingerUtils.init();
Window.setBackgroundColour(0.2f, 0.2f, 0.6f);
TexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png");
@ -65,6 +73,11 @@ public class Litecraft extends Game
ginger3D.startGame();
}
private void setupKeybinds()
{
Input.addPressCallback(Keybind.EXIT, this::exit);
}
@Override
public void exit()
{ ginger3D.cleanup(); }
@ -92,6 +105,7 @@ public class Litecraft extends Game
@Override
public void update()
{
Input.invokeAllListeners();
data.player.updateMovement();
}
@ -102,7 +116,7 @@ public class Litecraft extends Game
public void onPlayButtonClick() {
if (world == null)
{
world = new World((long) new Random().nextInt(), 10);
world = new World((long) new Random().nextInt(), 10, Dimension.OVERWORLD);
}
}
}

View File

@ -0,0 +1,8 @@
package com.github.halotroop.litecraft.logic;
import tk.valoeghese.sod.BinaryData;
public interface StoredData {
void read(BinaryData data);
void write(BinaryData data);
}

View File

@ -0,0 +1,40 @@
package com.github.halotroop.litecraft.util.noise;
import java.util.Random;
public final class OctaveSimplexNoise
{
protected SimplexNoise[] samplers;
private double clamp;
private double spread, amplitudeLow, amplitudeHigh;
public OctaveSimplexNoise(Random rand, int octaves, double spread, double amplitudeHigh, double amplitudeLow)
{
this.samplers = new SimplexNoise[octaves];
this.clamp = 1D / (1D - (1D / Math.pow(2, octaves)));
for (int i = 0; i < octaves; ++i)
{
samplers[i] = new SimplexNoise(rand.nextLong());
}
this.spread = spread;
this.amplitudeLow = amplitudeLow;
this.amplitudeHigh = amplitudeHigh;
}
public double sample(double x, double y)
{
double amplFreq = 0.5D;
double result = 0;
for (SimplexNoise sampler : this.samplers)
{
result += (amplFreq * sampler.sample(x / (amplFreq * this.spread), y / (amplFreq * this.spread)));
amplFreq *= 0.5D;
}
result = result * this.clamp;
return result > 0 ? result * this.amplitudeHigh : result * this.amplitudeLow;
}
}

View File

@ -0,0 +1,783 @@
package com.github.halotroop.litecraft.util.noise;
/*
* OpenSimplex Noise in Java.
* (Using implementation by Kurt Spencer)
*
* v1.1 (October 5, 2014)
* - Added 2D and 4D implementations.
* - Proper gradient sets for all dimensions, from a
* dimensionally-generalizable scheme with an actual
* rhyme and reason behind it.
* - Removed default permutation array in favor of
* default seed.
* - Changed seed-based constructor to be independent
* of any particular randomization library, so results
* will be the same when ported to other languages.
*/
public class SimplexNoise {
private static final double STRETCH_CONSTANT_2D = -0.211324865405187; //(1/Math.sqrt(2+1)-1)/2;
private static final double SQUISH_CONSTANT_2D = 0.366025403784439; //(Math.sqrt(2+1)-1)/2;
private static final double STRETCH_CONSTANT_3D = -1.0 / 6; //(1/Math.sqrt(3+1)-1)/3;
private static final double SQUISH_CONSTANT_3D = 1.0 / 3; //(Math.sqrt(3+1)-1)/3;
private static final double NORM_CONSTANT_2D = 47;
private static final double NORM_CONSTANT_3D = 103;
private static final long DEFAULT_SEED = 0;
//Gradients for 2D. They approximate the directions to the
//vertices of an octagon from the center.
private static byte[] gradients2D = new byte[]{
5, 2, 2, 5,
-5, 2, -2, 5,
5, -2, 2, -5,
-5, -2, -2, -5,
};
//Gradients for 3D. They approximate the directions to the
//vertices of a rhombicuboctahedron from the center, skewed so
//that the triangular and square facets can be inscribed inside
//circles of the same radius.
private static byte[] gradients3D = new byte[]{
-11, 4, 4, -4, 11, 4, -4, 4, 11,
11, 4, 4, 4, 11, 4, 4, 4, 11,
-11, -4, 4, -4, -11, 4, -4, -4, 11,
11, -4, 4, 4, -11, 4, 4, -4, 11,
-11, 4, -4, -4, 11, -4, -4, 4, -11,
11, 4, -4, 4, 11, -4, 4, 4, -11,
-11, -4, -4, -4, -11, -4, -4, -4, -11,
11, -4, -4, 4, -11, -4, 4, -4, -11,
};
private short[] perm;
private short[] permGradIndex3D;
private long seed = -1L;
public SimplexNoise() {
this(DEFAULT_SEED);
}
//Initializes the class using a permutation array generated from a 64-bit seed.
//Generates a proper permutation (i.e. doesn't merely perform N successive pair swaps on a base array)
//Uses a simple 64-bit LCG.
public SimplexNoise(long seed) {
this.seed = seed;
perm = new short[256];
permGradIndex3D = new short[256];
short[] source = new short[256];
for (short i = 0; i < 256; i++)
source[i] = i;
seed = seed * 6364136223846793005L + 1442695040888963407L;
seed = seed * 6364136223846793005L + 1442695040888963407L;
seed = seed * 6364136223846793005L + 1442695040888963407L;
for (int i = 255; i >= 0; i--) {
seed = seed * 6364136223846793005L + 1442695040888963407L;
int r = (int) ((seed + 31) % (i + 1));
if (r < 0)
r += (i + 1);
perm[i] = source[r];
permGradIndex3D[i] = (short) ((perm[i] % (gradients3D.length / 3)) * 3);
source[r] = source[i];
}
}
private static int fastFloor(double x) {
int xi = (int) x;
return x < xi ? xi - 1 : xi;
}
/**
* Function added to noise by Valoeghese to get seed.
*/
public long getSeed() {
return this.seed;
}
//2D OpenSimplex Noise.
public double sample(double x, double y) {
//Place input coordinates onto grid.
double stretchOffset = (x + y) * STRETCH_CONSTANT_2D;
double xs = x + stretchOffset;
double ys = y + stretchOffset;
//Floor to get grid coordinates of rhombus (stretched square) super-cell origin.
int xsb = (int) Math.floor(xs);
int ysb = (int) Math.floor(ys);
//Skew out to get actual coordinates of rhombus origin. We'll need these later.
double squishOffset = (xsb + ysb) * SQUISH_CONSTANT_2D;
double xb = xsb + squishOffset;
double yb = ysb + squishOffset;
//Compute grid coordinates relative to rhombus origin.
double xins = xs - xsb;
double yins = ys - ysb;
//Sum those together to get a value that determines which region we're in.
double inSum = xins + yins;
//Positions relative to origin point.
double dx0 = x - xb;
double dy0 = y - yb;
//We'll be defining these inside the next block and using them afterwards.
double dx_ext, dy_ext;
int xsv_ext, ysv_ext;
double value = 0;
//Contribution (1,0)
double dx1 = dx0 - 1 - SQUISH_CONSTANT_2D;
double dy1 = dy0 - 0 - SQUISH_CONSTANT_2D;
double attn1 = 2 - dx1 * dx1 - dy1 * dy1;
if (attn1 > 0) {
attn1 *= attn1;
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, dx1, dy1);
}
//Contribution (0,1)
double dx2 = dx0 - 0 - SQUISH_CONSTANT_2D;
double dy2 = dy0 - 1 - SQUISH_CONSTANT_2D;
double attn2 = 2 - dx2 * dx2 - dy2 * dy2;
if (attn2 > 0) {
attn2 *= attn2;
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, dx2, dy2);
}
if (inSum <= 1) { //We're inside the triangle (2-Simplex) at (0,0)
double zins = 1 - inSum;
if (zins > xins || zins > yins) { //(0,0) is one of the closest two triangular vertices
if (xins > yins) {
xsv_ext = xsb + 1;
ysv_ext = ysb - 1;
dx_ext = dx0 - 1;
dy_ext = dy0 + 1;
} else {
xsv_ext = xsb - 1;
ysv_ext = ysb + 1;
dx_ext = dx0 + 1;
dy_ext = dy0 - 1;
}
} else { //(1,0) and (0,1) are the closest two vertices.
xsv_ext = xsb + 1;
ysv_ext = ysb + 1;
dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
}
} else { //We're inside the triangle (2-Simplex) at (1,1)
double zins = 2 - inSum;
if (zins < xins || zins < yins) { //(0,0) is one of the closest two triangular vertices
if (xins > yins) {
xsv_ext = xsb + 2;
ysv_ext = ysb + 0;
dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT_2D;
dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT_2D;
} else {
xsv_ext = xsb + 0;
ysv_ext = ysb + 2;
dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT_2D;
dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT_2D;
}
} else { //(1,0) and (0,1) are the closest two vertices.
dx_ext = dx0;
dy_ext = dy0;
xsv_ext = xsb;
ysv_ext = ysb;
}
xsb += 1;
ysb += 1;
dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
}
//Contribution (0,0) or (1,1)
double attn0 = 2 - dx0 * dx0 - dy0 * dy0;
if (attn0 > 0) {
attn0 *= attn0;
value += attn0 * attn0 * extrapolate(xsb, ysb, dx0, dy0);
}
//Extra Vertex
double attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext;
if (attn_ext > 0) {
attn_ext *= attn_ext;
value += attn_ext * attn_ext * extrapolate(xsv_ext, ysv_ext, dx_ext, dy_ext);
}
return value / NORM_CONSTANT_2D;
}
//3D OpenSimplex Noise.
public double sample(double x, double y, double z) {
//Place input coordinates on simplectic honeycomb.
double stretchOffset = (x + y + z) * STRETCH_CONSTANT_3D;
double xs = x + stretchOffset;
double ys = y + stretchOffset;
double zs = z + stretchOffset;
//Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
int xsb = fastFloor(xs);
int ysb = fastFloor(ys);
int zsb = fastFloor(zs);
//Skew out to get actual coordinates of rhombohedron origin. We'll need these later.
double squishOffset = (xsb + ysb + zsb) * SQUISH_CONSTANT_3D;
double xb = xsb + squishOffset;
double yb = ysb + squishOffset;
double zb = zsb + squishOffset;
//Compute simplectic honeycomb coordinates relative to rhombohedral origin.
double xins = xs - xsb;
double yins = ys - ysb;
double zins = zs - zsb;
//Sum those together to get a value that determines which region we're in.
double inSum = xins + yins + zins;
//Positions relative to origin point.
double dx0 = x - xb;
double dy0 = y - yb;
double dz0 = z - zb;
//We'll be defining these inside the next block and using them afterwards.
double dx_ext0, dy_ext0, dz_ext0;
double dx_ext1, dy_ext1, dz_ext1;
int xsv_ext0, ysv_ext0, zsv_ext0;
int xsv_ext1, ysv_ext1, zsv_ext1;
double value = 0;
if (inSum <= 1) { //We're inside the tetrahedron (3-Simplex) at (0,0,0)
//Determine which two of (0,0,1), (0,1,0), (1,0,0) are closest.
byte aPoint = 0x01;
double aScore = xins;
byte bPoint = 0x02;
double bScore = yins;
if (aScore >= bScore && zins > bScore) {
bScore = zins;
bPoint = 0x04;
} else if (aScore < bScore && zins > aScore) {
aScore = zins;
aPoint = 0x04;
}
//Now we determine the two lattice points not part of the tetrahedron that may contribute.
//This depends on the closest two tetrahedral vertices, including (0,0,0)
double wins = 1 - inSum;
if (wins > aScore || wins > bScore) { //(0,0,0) is one of the closest two tetrahedral vertices.
byte c = (bScore > aScore ? bPoint : aPoint); //Our other closest vertex is the closest out of a and b.
if ((c & 0x01) == 0) {
xsv_ext0 = xsb - 1;
xsv_ext1 = xsb;
dx_ext0 = dx0 + 1;
dx_ext1 = dx0;
} else {
xsv_ext0 = xsv_ext1 = xsb + 1;
dx_ext0 = dx_ext1 = dx0 - 1;
}
if ((c & 0x02) == 0) {
ysv_ext0 = ysv_ext1 = ysb;
dy_ext0 = dy_ext1 = dy0;
if ((c & 0x01) == 0) {
ysv_ext1 -= 1;
dy_ext1 += 1;
} else {
ysv_ext0 -= 1;
dy_ext0 += 1;
}
} else {
ysv_ext0 = ysv_ext1 = ysb + 1;
dy_ext0 = dy_ext1 = dy0 - 1;
}
if ((c & 0x04) == 0) {
zsv_ext0 = zsb;
zsv_ext1 = zsb - 1;
dz_ext0 = dz0;
dz_ext1 = dz0 + 1;
} else {
zsv_ext0 = zsv_ext1 = zsb + 1;
dz_ext0 = dz_ext1 = dz0 - 1;
}
} else { //(0,0,0) is not one of the closest two tetrahedral vertices.
byte c = (byte) (aPoint | bPoint); //Our two extra vertices are determined by the closest two.
if ((c & 0x01) == 0) {
xsv_ext0 = xsb;
xsv_ext1 = xsb - 1;
dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_3D;
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D;
} else {
xsv_ext0 = xsv_ext1 = xsb + 1;
dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
}
if ((c & 0x02) == 0) {
ysv_ext0 = ysb;
ysv_ext1 = ysb - 1;
dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_3D;
dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D;
} else {
ysv_ext0 = ysv_ext1 = ysb + 1;
dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
}
if ((c & 0x04) == 0) {
zsv_ext0 = zsb;
zsv_ext1 = zsb - 1;
dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_3D;
dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D;
} else {
zsv_ext0 = zsv_ext1 = zsb + 1;
dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
}
}
//Contribution (0,0,0)
double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
if (attn0 > 0) {
attn0 *= attn0;
value += attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0);
}
//Contribution (1,0,0)
double dx1 = dx0 - 1 - SQUISH_CONSTANT_3D;
double dy1 = dy0 - 0 - SQUISH_CONSTANT_3D;
double dz1 = dz0 - 0 - SQUISH_CONSTANT_3D;
double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
if (attn1 > 0) {
attn1 *= attn1;
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1);
}
//Contribution (0,1,0)
double dx2 = dx0 - 0 - SQUISH_CONSTANT_3D;
double dy2 = dy0 - 1 - SQUISH_CONSTANT_3D;
double dz2 = dz1;
double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
if (attn2 > 0) {
attn2 *= attn2;
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2);
}
//Contribution (0,0,1)
double dx3 = dx2;
double dy3 = dy1;
double dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
if (attn3 > 0) {
attn3 *= attn3;
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3);
}
} else if (inSum >= 2) { //We're inside the tetrahedron (3-Simplex) at (1,1,1)
//Determine which two tetrahedral vertices are the closest, out of (1,1,0), (1,0,1), (0,1,1) but not (1,1,1).
byte aPoint = 0x06;
double aScore = xins;
byte bPoint = 0x05;
double bScore = yins;
if (aScore <= bScore && zins < bScore) {
bScore = zins;
bPoint = 0x03;
} else if (aScore > bScore && zins < aScore) {
aScore = zins;
aPoint = 0x03;
}
//Now we determine the two lattice points not part of the tetrahedron that may contribute.
//This depends on the closest two tetrahedral vertices, including (1,1,1)
double wins = 3 - inSum;
if (wins < aScore || wins < bScore) { //(1,1,1) is one of the closest two tetrahedral vertices.
byte c = (bScore < aScore ? bPoint : aPoint); //Our other closest vertex is the closest out of a and b.
if ((c & 0x01) != 0) {
xsv_ext0 = xsb + 2;
xsv_ext1 = xsb + 1;
dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_3D;
dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
} else {
xsv_ext0 = xsv_ext1 = xsb;
dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_3D;
}
if ((c & 0x02) != 0) {
ysv_ext0 = ysv_ext1 = ysb + 1;
dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
if ((c & 0x01) != 0) {
ysv_ext1 += 1;
dy_ext1 -= 1;
} else {
ysv_ext0 += 1;
dy_ext0 -= 1;
}
} else {
ysv_ext0 = ysv_ext1 = ysb;
dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_3D;
}
if ((c & 0x04) != 0) {
zsv_ext0 = zsb + 1;
zsv_ext1 = zsb + 2;
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 2 - 3 * SQUISH_CONSTANT_3D;
} else {
zsv_ext0 = zsv_ext1 = zsb;
dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_3D;
}
} else { //(1,1,1) is not one of the closest two tetrahedral vertices.
byte c = (byte) (aPoint & bPoint); //Our two extra vertices are determined by the closest two.
if ((c & 0x01) != 0) {
xsv_ext0 = xsb + 1;
xsv_ext1 = xsb + 2;
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D;
} else {
xsv_ext0 = xsv_ext1 = xsb;
dx_ext0 = dx0 - SQUISH_CONSTANT_3D;
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
}
if ((c & 0x02) != 0) {
ysv_ext0 = ysb + 1;
ysv_ext1 = ysb + 2;
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D;
} else {
ysv_ext0 = ysv_ext1 = ysb;
dy_ext0 = dy0 - SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
}
if ((c & 0x04) != 0) {
zsv_ext0 = zsb + 1;
zsv_ext1 = zsb + 2;
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D;
} else {
zsv_ext0 = zsv_ext1 = zsb;
dz_ext0 = dz0 - SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
}
}
//Contribution (1,1,0)
double dx3 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
double dy3 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
double dz3 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D;
double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
if (attn3 > 0) {
attn3 *= attn3;
value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3);
}
//Contribution (1,0,1)
double dx2 = dx3;
double dy2 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D;
double dz2 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
if (attn2 > 0) {
attn2 *= attn2;
value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, dx2, dy2, dz2);
}
//Contribution (0,1,1)
double dx1 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D;
double dy1 = dy3;
double dz1 = dz2;
double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
if (attn1 > 0) {
attn1 *= attn1;
value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, dx1, dy1, dz1);
}
//Contribution (1,1,1)
dx0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
dy0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
dz0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
if (attn0 > 0) {
attn0 *= attn0;
value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, dx0, dy0, dz0);
}
} else { //We're inside the octahedron (Rectified 3-Simplex) in between.
double aScore;
byte aPoint;
boolean aIsFurtherSide;
double bScore;
byte bPoint;
boolean bIsFurtherSide;
//Decide between point (0,0,1) and (1,1,0) as closest
double p1 = xins + yins;
if (p1 > 1) {
aScore = p1 - 1;
aPoint = 0x03;
aIsFurtherSide = true;
} else {
aScore = 1 - p1;
aPoint = 0x04;
aIsFurtherSide = false;
}
//Decide between point (0,1,0) and (1,0,1) as closest
double p2 = xins + zins;
if (p2 > 1) {
bScore = p2 - 1;
bPoint = 0x05;
bIsFurtherSide = true;
} else {
bScore = 1 - p2;
bPoint = 0x02;
bIsFurtherSide = false;
}
//The closest out of the two (1,0,0) and (0,1,1) will replace the furthest out of the two decided above, if closer.
double p3 = yins + zins;
if (p3 > 1) {
double score = p3 - 1;
if (aScore <= bScore && aScore < score) {
aScore = score;
aPoint = 0x06;
aIsFurtherSide = true;
} else if (aScore > bScore && bScore < score) {
bScore = score;
bPoint = 0x06;
bIsFurtherSide = true;
}
} else {
double score = 1 - p3;
if (aScore <= bScore && aScore < score) {
aScore = score;
aPoint = 0x01;
aIsFurtherSide = false;
} else if (aScore > bScore && bScore < score) {
bScore = score;
bPoint = 0x01;
bIsFurtherSide = false;
}
}
//Where each of the two closest points are determines how the extra two vertices are calculated.
if (aIsFurtherSide == bIsFurtherSide) {
if (aIsFurtherSide) { //Both closest points on (1,1,1) side
//One of the two extra points is (1,1,1)
dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
dy_ext0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
xsv_ext0 = xsb + 1;
ysv_ext0 = ysb + 1;
zsv_ext0 = zsb + 1;
//Other extra point is based on the shared axis.
byte c = (byte) (aPoint & bPoint);
if ((c & 0x01) != 0) {
dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
xsv_ext1 = xsb + 2;
ysv_ext1 = ysb;
zsv_ext1 = zsb;
} else if ((c & 0x02) != 0) {
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
xsv_ext1 = xsb;
ysv_ext1 = ysb + 2;
zsv_ext1 = zsb;
} else {
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D;
xsv_ext1 = xsb;
ysv_ext1 = ysb;
zsv_ext1 = zsb + 2;
}
} else {//Both closest points on (0,0,0) side
//One of the two extra points is (0,0,0)
dx_ext0 = dx0;
dy_ext0 = dy0;
dz_ext0 = dz0;
xsv_ext0 = xsb;
ysv_ext0 = ysb;
zsv_ext0 = zsb;
//Other extra point is based on the omitted axis.
byte c = (byte) (aPoint | bPoint);
if ((c & 0x01) == 0) {
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
xsv_ext1 = xsb - 1;
ysv_ext1 = ysb + 1;
zsv_ext1 = zsb + 1;
} else if ((c & 0x02) == 0) {
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
xsv_ext1 = xsb + 1;
ysv_ext1 = ysb - 1;
zsv_ext1 = zsb + 1;
} else {
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D;
xsv_ext1 = xsb + 1;
ysv_ext1 = ysb + 1;
zsv_ext1 = zsb - 1;
}
}
} else { //One point on (0,0,0) side, one point on (1,1,1) side
byte c1, c2;
if (aIsFurtherSide) {
c1 = aPoint;
c2 = bPoint;
} else {
c1 = bPoint;
c2 = aPoint;
}
//One contribution is a permutation of (1,1,-1)
if ((c1 & 0x01) == 0) {
dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_3D;
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
xsv_ext0 = xsb - 1;
ysv_ext0 = ysb + 1;
zsv_ext0 = zsb + 1;
} else if ((c1 & 0x02) == 0) {
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
dy_ext0 = dy0 + 1 - SQUISH_CONSTANT_3D;
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
xsv_ext0 = xsb + 1;
ysv_ext0 = ysb - 1;
zsv_ext0 = zsb + 1;
} else {
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
dz_ext0 = dz0 + 1 - SQUISH_CONSTANT_3D;
xsv_ext0 = xsb + 1;
ysv_ext0 = ysb + 1;
zsv_ext0 = zsb - 1;
}
//One contribution is a permutation of (0,0,2)
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
xsv_ext1 = xsb;
ysv_ext1 = ysb;
zsv_ext1 = zsb;
if ((c2 & 0x01) != 0) {
dx_ext1 -= 2;
xsv_ext1 += 2;
} else if ((c2 & 0x02) != 0) {
dy_ext1 -= 2;
ysv_ext1 += 2;
} else {
dz_ext1 -= 2;
zsv_ext1 += 2;
}
}
//Contribution (1,0,0)
double dx1 = dx0 - 1 - SQUISH_CONSTANT_3D;
double dy1 = dy0 - 0 - SQUISH_CONSTANT_3D;
double dz1 = dz0 - 0 - SQUISH_CONSTANT_3D;
double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
if (attn1 > 0) {
attn1 *= attn1;
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1);
}
//Contribution (0,1,0)
double dx2 = dx0 - 0 - SQUISH_CONSTANT_3D;
double dy2 = dy0 - 1 - SQUISH_CONSTANT_3D;
double dz2 = dz1;
double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
if (attn2 > 0) {
attn2 *= attn2;
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2);
}
//Contribution (0,0,1)
double dx3 = dx2;
double dy3 = dy1;
double dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
if (attn3 > 0) {
attn3 *= attn3;
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3);
}
//Contribution (1,1,0)
double dx4 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
double dy4 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
double dz4 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D;
double attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4;
if (attn4 > 0) {
attn4 *= attn4;
value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx4, dy4, dz4);
}
//Contribution (1,0,1)
double dx5 = dx4;
double dy5 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D;
double dz5 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
double attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5;
if (attn5 > 0) {
attn5 *= attn5;
value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 0, zsb + 1, dx5, dy5, dz5);
}
//Contribution (0,1,1)
double dx6 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D;
double dy6 = dy4;
double dz6 = dz5;
double attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6;
if (attn6 > 0) {
attn6 *= attn6;
value += attn6 * attn6 * extrapolate(xsb + 0, ysb + 1, zsb + 1, dx6, dy6, dz6);
}
}
//First extra vertex
double attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0;
if (attn_ext0 > 0) {
attn_ext0 *= attn_ext0;
value += attn_ext0 * attn_ext0 * extrapolate(xsv_ext0, ysv_ext0, zsv_ext0, dx_ext0, dy_ext0, dz_ext0);
}
//Second extra vertex
double attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1;
if (attn_ext1 > 0) {
attn_ext1 *= attn_ext1;
value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1);
}
return value / NORM_CONSTANT_3D;
}
private double extrapolate(int xsb, int ysb, double dx, double dy) {
int index = perm[(perm[xsb & 0xFF] + ysb) & 0xFF] & 0x0E;
return gradients2D[index] * dx
+ gradients2D[index + 1] * dy;
}
private double extrapolate(int xsb, int ysb, int zsb, double dx, double dy, double dz) {
int index = permGradIndex3D[(perm[(perm[xsb & 0xFF] + ysb) & 0xFF] + zsb) & 0xFF];
return gradients3D[index] * dx
+ gradients3D[index + 1] * dy
+ gradients3D[index + 2] * dz;
}
}

View File

@ -6,9 +6,4 @@ public interface BlockAccess
{
Block getBlock(int x, int y, int z);
void setBlock(int x, int y, int z, Block block);
static final int POS_SHIFT = 3;
static final int DOUBLE_SHIFT = POS_SHIFT * 2;
static final int CHUNK_SIZE = (int) Math.pow(2, POS_SHIFT);
static final int MAX_POS = CHUNK_SIZE - 1;
}

View File

@ -3,12 +3,13 @@ package com.github.halotroop.litecraft.world;
import java.util.*;
import com.github.halotroop.litecraft.types.block.*;
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
import com.github.hydos.ginger.engine.render.renderers.ObjectRenderer;
import it.unimi.dsi.fastutil.longs.*;
public class Chunk implements BlockAccess
public class Chunk implements BlockAccess, WorldGenConstants
{
/** @param x in-chunk x coordinate.
* @param y in-chunk y coordinate.
@ -22,7 +23,8 @@ public class Chunk implements BlockAccess
private final Long2ObjectMap<BlockEntity> blockEntities = new Long2ObjectArrayMap<>();
private boolean render = false;
public final int chunkX, chunkY, chunkZ;
private final int chunkStartX, chunkStartY, chunkStartZ;
public final int chunkStartX, chunkStartY, chunkStartZ;
private boolean fullyGenerated = false;
public Chunk(int chunkX, int chunkY, int chunkZ)
{
@ -30,14 +32,17 @@ public class Chunk implements BlockAccess
this.chunkX = chunkX;
this.chunkY = chunkY;
this.chunkZ = chunkZ;
this.chunkStartX = chunkX << 3;
this.chunkStartY = chunkY << 3;
this.chunkStartZ = chunkZ << 3;
this.chunkStartX = chunkX << POS_SHIFT;
this.chunkStartY = chunkY << POS_SHIFT;
this.chunkStartZ = chunkZ << POS_SHIFT;
}
public boolean doRender()
{ return this.render; }
public void setFullyGenerated(boolean fullyGenerated)
{ this.fullyGenerated = fullyGenerated; }
@Override
public Block getBlock(int x, int y, int z)
{
@ -91,20 +96,6 @@ public class Chunk implements BlockAccess
}
}
public static Chunk generateChunk(int chunkX, int chunkY, int chunkZ) {
Chunk result = new Chunk(chunkX, chunkY, chunkZ);
for (int x = 0; x < CHUNK_SIZE; ++x) {
for (int z = 0; z < CHUNK_SIZE; ++z) {
for (int y = 0; y < CHUNK_SIZE; ++y) {
result.setBlock(x, y, z, y == 7 ? Block.DIRT : Block.STONE);
}
}
}
return result;
}
public void setRender(boolean render)
{
if (render && !this.render) // if it has been changed to true
@ -134,4 +125,7 @@ public class Chunk implements BlockAccess
}
this.render = render;
}
public boolean isFullyGenerated()
{ return this.fullyGenerated; }
}

View File

@ -1,6 +1,9 @@
package com.github.halotroop.litecraft.world;
import java.util.*;
import com.github.halotroop.litecraft.types.block.Block;
import com.github.halotroop.litecraft.world.gen.*;
import com.github.hydos.ginger.engine.elements.objects.Player;
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
import com.github.hydos.ginger.engine.obj.ModelLoader;
@ -9,14 +12,21 @@ import com.github.hydos.ginger.engine.render.renderers.ObjectRenderer;
import it.unimi.dsi.fastutil.longs.*;
public class World implements BlockAccess
public class World implements BlockAccess, WorldGenConstants
{
private final Long2ObjectMap<Chunk> chunks;
private final WorldModifier[] worldModifiers;
private final ChunkGenerator chunkGenerator;
private final long seed;
public Player player;
public World(long seed, int size)
public World(long seed, int size, Dimension<?> dim)
{
chunks = new Long2ObjectArrayMap<>();
this.chunks = new Long2ObjectArrayMap<>();
this.seed = seed;
this.chunkGenerator = dim.createChunkGenerator(seed);
this.worldModifiers = dim.getWorldModifierArray();
for (int i = (0 - (size/2)); i < (size/2); i++)
for (int k = (0 - (size/2)); k < (size/2); k++)
@ -27,10 +37,31 @@ public class World implements BlockAccess
}
public Chunk getChunk(int chunkX, int chunkY, int chunkZ)
{ return this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> Chunk.generateChunk(chunkX, chunkY, chunkZ)); }
{
Chunk chunk = this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ));
private static long posHash(int x, int y, int z)
{ return ((long) x & 0x3FF) | (((long) y & 0x3FF) << 10) | (((long) z & 0x3FF) << 20); }
if (chunk.isFullyGenerated()) return chunk;
this.populateChunk(chunkX, chunkY, chunkZ, chunk.chunkStartX, chunk.chunkStartY, chunk.chunkStartZ);
chunk.setFullyGenerated(true);
return chunk;
}
private void populateChunk(int chunkX, int chunkY, int chunkZ, int chunkStartX, int chunkStartY, int chunkStartZ)
{
Random rand = new Random(this.seed + 5828671L * (long) chunkX + -47245139L * (long) chunkY + 8972357 * (long) chunkZ);
for (WorldModifier modifier : this.worldModifiers)
{
modifier.modifyWorld(rand, chunkStartX, chunkStartY, chunkStartZ);
}
}
Chunk getPartiallyGeneratedChunk(int chunkX, int chunkY, int chunkZ)
{ return this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ)); }
private static long posHash(int chunkX, int chunkY, int chunkZ)
{ return ((long) chunkX & 0x3FF) | (((long) chunkY & 0x3FF) << 10) | (((long) chunkZ & 0x3FF) << 20); }
@Override
public Block getBlock(int x, int y, int z)

View File

@ -0,0 +1,8 @@
package com.github.halotroop.litecraft.world.gen;
import com.github.halotroop.litecraft.world.Chunk;
public interface ChunkGenerator
{
Chunk generateChunk(int chunkX, int chunkY, int chunkZ);
}

View File

@ -0,0 +1,24 @@
package com.github.halotroop.litecraft.world.gen;
import java.util.*;
import com.github.halotroop.litecraft.world.Chunk;
public abstract class Dimension<T extends ChunkGenerator>
{
public List<WorldModifier> worldModifiers = new ArrayList<>();
public Dimension addWorldModifier(WorldModifier modifier)
{
this.worldModifiers.add(modifier);
return this;
}
public WorldModifier[] getWorldModifierArray() {
return this.worldModifiers.toArray(new WorldModifier[0]);
}
public abstract T createChunkGenerator(long seed);
public static final Dimension OVERWORLD = new OverworldDimension();
}

View File

@ -0,0 +1,50 @@
package com.github.halotroop.litecraft.world.gen;
import java.util.Random;
import com.github.halotroop.litecraft.types.block.Block;
import com.github.halotroop.litecraft.util.noise.OctaveSimplexNoise;
import com.github.halotroop.litecraft.world.Chunk;
public class OverworldChunkGenerator implements ChunkGenerator, WorldGenConstants
{
public OverworldChunkGenerator(long seed)
{
this.noise = new OctaveSimplexNoise(new Random(seed), 3, 250.0, 35.0, 10.0);
}
private final OctaveSimplexNoise noise;
@Override
public Chunk generateChunk(int chunkX, int chunkY, int chunkZ)
{
Chunk chunk = new Chunk(chunkX, chunkY, chunkZ);
for (int x = 0; x < CHUNK_SIZE; ++x) {
double totalX = x + chunk.chunkStartX;
for (int z = 0; z < CHUNK_SIZE; ++z) {
int height = (int) this.noise.sample(totalX, (double) (chunk.chunkStartZ + z));
for (int y = 0; y < CHUNK_SIZE; ++y) {
int totalY = chunk.chunkStartY + y;
Block block = Block.AIR;
if (totalY < height - 3)
{
block = Block.DIRT;
}
else if (totalY < height)
{
block = Block.STONE;
}
chunk.setBlock(x, y, z, block);
}
}
}
return chunk;
}
}

View File

@ -0,0 +1,10 @@
package com.github.halotroop.litecraft.world.gen;
public class OverworldDimension extends Dimension<OverworldChunkGenerator>
{
@Override
public OverworldChunkGenerator createChunkGenerator(long seed)
{
return new OverworldChunkGenerator(seed);
}
}

View File

@ -0,0 +1,9 @@
package com.github.halotroop.litecraft.world.gen;
public interface WorldGenConstants
{
static final int POS_SHIFT = 3;
static final int DOUBLE_SHIFT = POS_SHIFT * 2;
static final int CHUNK_SIZE = (int) Math.pow(2, POS_SHIFT);
static final int MAX_POS = CHUNK_SIZE - 1;
}

View File

@ -0,0 +1,7 @@
package com.github.halotroop.litecraft.world.gen;
import java.util.Random;
public interface WorldModifier {
void modifyWorld(Random rand, int chunkStartX, int chunkStartY, int chunkStartZ);
}

View File

@ -92,7 +92,7 @@ public class ObjectRenderer extends Renderer
shader.loadViewMatrix(GingerRegister.getInstance().game.data.camera);
for (RenderObject entity : renderList)
{
if(entity.getModel() != null) {
if (entity != null && entity.getModel() != null) {
TexturedModel model = entity.getModel();
prepareTexturedModel(model);
prepareInstance(entity);