Skip to content

Commit

Permalink
[TerrainGenerator] Working on new terrain generation. [SimplexNoise] …
Browse files Browse the repository at this point in the history
…Added.
  • Loading branch information
Unarelith committed Jan 25, 2020
1 parent 3eda95a commit 4c3b55d
Show file tree
Hide file tree
Showing 5 changed files with 535 additions and 5 deletions.
4 changes: 2 additions & 2 deletions client/source/states/PauseMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ PauseMenuState::PauseMenuState(Client &client, gk::ApplicationState *parent)
m_menuWidget.addButton("Options...", [this] (TextButton &) { m_stateStack->push<SettingsMenuState>(m_parent); });

m_menuWidget.addButton("Title Screen", [this] (TextButton &) {
m_client.disconnect();
// m_client.disconnect();

m_stateStack->clear();
m_stateStack->push<TitleScreenState>();
});

m_menuWidget.addButton("Exit", [this] (TextButton &) {
m_client.disconnect();
// m_client.disconnect();

while(!m_stateStack->empty())
m_stateStack->pop();
Expand Down
87 changes: 87 additions & 0 deletions server/include/world/SimplexNoise.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* A speed-improved simplex noise algorithm for 2D, 3D and 4D in Java.
*
* Based on example code by Stefan Gustavson ([email protected]).
* Optimisations by Peter Eastman ([email protected]).
* Better rank ordering method for 4D by Stefan Gustavson in 2012.
*
* This could be speeded up even further, but it's useful as it is.
*
* Version 2012-03-09
*
* This code was placed in the public domain by its original author,
* Stefan Gustavson. You may use it as you see fit, but
* attribution is appreciated.
*
* Original link: http://staffwww.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java
* Adapted to C++ by Unarelith
*
*/
#ifndef SIMPLEXNOISE_HPP_
#define SIMPLEXNOISE_HPP_

class SimplexNoise {
public:
// 2D simplex noise
static double noise(double xin, double yin);

// 3D simplex noise
static double noise(double xin, double yin, double zin);

// 4D simplex noise
static double noise(double xin, double yin, double zin, double win);

private:
// Inner class to speed upp gradient computations
// (In Java, array access is a lot slower than member access)
// FIXME: Is C++ different on that topic?
struct Grad {
Grad(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}
Grad(double _x, double _y, double _z, double _w) : x(_x), y(_y), z(_z), w(_w) {}

double x = 0;
double y = 0;
double z = 0;
double w = 0;
};

// This method is a *lot* faster than using (int)Math.floor(x)
static int fastfloor(double x) {
int xi = (int)x;
return x<xi ? xi-1 : xi;
}

static double dot(Grad g, double x, double y) {
return g.x*x + g.y*y;
}

static double dot(Grad g, double x, double y, double z) {
return g.x*x + g.y*y + g.z*z;
}

static double dot(Grad g, double x, double y, double z, double w) {
return g.x*x + g.y*y + g.z*z + g.w*w;
}

// Initialization function
static void init();

static Grad grad3[12];
static Grad grad4[32];

static unsigned short p[];

static short perm[512];
static short permMod12[512];

static double F2;
static double G2;
static double F3;
static double G3;
static double F4;
static double G4;

static bool isInitialized;
};

#endif // SIMPLEXNOISE_HPP_
4 changes: 4 additions & 0 deletions server/include/world/TerrainGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ class TerrainGenerator {
void lightTestGeneration(ServerChunk &chunk) const;
void basicGeneration(ServerChunk &chunk) const;
void testCraftGeneration(ServerChunk &chunk) const;
void newGeneration(ServerChunk &chunk) const;

static float noise2d(float x, float y, int octaves, float persistence);
static float noise3d_abs(float x, float y, float z, int octaves, float persistence);

static float newNoise2d(float x, float y, int octaves, float persistence);
static float newNoise3d_abs(float x, float y, float z, int octaves, float persistence);
};

#endif // TERRAINGENERATOR_HPP_
Loading

0 comments on commit 4c3b55d

Please sign in to comment.