Skip to content

Commit

Permalink
Faster world generation and fixed getting stuck if the max chunk dist…
Browse files Browse the repository at this point in the history
…ance was too small
  • Loading branch information
Vogtinator committed Apr 18, 2014
1 parent 379d8c6 commit d6c686b
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 16 deletions.
17 changes: 8 additions & 9 deletions chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,9 @@ void Chunk::generate()
for(int x = 0; x < SIZE; x++)
for(int z = 0; z < SIZE; z++)
{
float noise_val = noise.noise((float(x)/Chunk::SIZE + this->x)/2, (float(z)/Chunk::SIZE + this->z)/2, 10);
constexpr int world_gen_min = 8, world_gen_max = World::HEIGHT * Chunk::SIZE * 0.7;
int height = world_gen_min + noise_val * (world_gen_max - world_gen_min);
GLFix noise_val = noise.noise((GLFix(x)/Chunk::SIZE + this->x)/2, (GLFix(z)/Chunk::SIZE + this->z)/2, 10);
int world_gen_min = 8, world_gen_max = World::HEIGHT * Chunk::SIZE * 0.7;
int height = world_gen_min + (noise_val * (world_gen_max - world_gen_min)).round();
int height_left = height - this->y * Chunk::SIZE;
int height_here = std::min(height_left, Chunk::SIZE);

Expand All @@ -552,13 +552,12 @@ void Chunk::generate()
//Deep underground
if(to_surface > 5)
{
noise_val = noise.noise(float(x)/Chunk::SIZE + this->x, float(z)/Chunk::SIZE + this->z, 10.5f);
if(noise_val < 0.4f)
noise_val = noise.noise(GLFix(x)/Chunk::SIZE + this->x, GLFix(z)/Chunk::SIZE + this->z, 10.5f);
if(noise_val < GLFix(0.4f))
{
noise_val = noise.noise(float(x)/Chunk::SIZE + this->x, float(z)/Chunk::SIZE + this->z, 1000.5f);
noise_val = noise.noise(GLFix(x)/Chunk::SIZE + this->x, GLFix(z)/Chunk::SIZE + this->z, 1000.5f);

uint8_t test = 256 * noise_val;
test &= 0xF;
uint8_t test = noise_val.value & 0xF;

if(test < 2)
blocks[x][y][z] = BLOCK_DIAMOND_ORE;
Expand All @@ -577,7 +576,7 @@ void Chunk::generate()
else
blocks[x][y][z] = BLOCK_SAND;
}
if(trees < max_trees && height_left >= 0 && height_left <= Chunk::SIZE && height > 5 && noise.noise(float(x)/Chunk::SIZE + this->x, float(z)/Chunk::SIZE + this->z, 25) < 0.3)
if(trees < max_trees && height_left >= 0 && height_left <= Chunk::SIZE && height > 5 && noise.noise(GLFix(x)/Chunk::SIZE + this->x, GLFix(z)/Chunk::SIZE + this->z, 25) < GLFix(0.3f))
{
makeTree(x, height_here, z);
trees++;
Expand Down
2 changes: 1 addition & 1 deletion fastmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void init_fastmath()
float rad = 0;
while(deg.value < LUT_SIZE)
{
table_sin[deg.value].fromFloat(sin(rad));
table_sin[deg.value].fromFloat(sinf(rad));
deg.value++;
rad += incr;
}
Expand Down
2 changes: 1 addition & 1 deletion gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void nglDisplay()
{
uint32_t *real_screen = reinterpret_cast<uint32_t*>(SCREEN_BASE_ADDRESS);
uint32_t *buffer = reinterpret_cast<uint32_t*>(screen);
for(unsigned int x = 0; x < SCREEN_WIDTH*SCREEN_HEIGHT/8; x++)
for(unsigned int x = SCREEN_WIDTH*SCREEN_HEIGHT/8; x--;)
{
*(real_screen++) = *(buffer++);
*(real_screen++) = *(buffer++);
Expand Down
2 changes: 1 addition & 1 deletion glbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GLBox : public GLDrawable
{
public:
GLBox(GLFix width, GLFix height, GLFix length, GLFix cenX, GLFix cenY, GLFix cenZ, TextureAtlasEntry fr, TextureAtlasEntry ba, TextureAtlasEntry to, TextureAtlasEntry bot, TextureAtlasEntry le, TextureAtlasEntry ri, bool draw_front, bool draw_back, bool draw_top, bool draw_bottom, bool draw_left, bool draw_right);
GLBox(float width, float height, float length, float cenX, float cenY, float cenZ, TextureAtlasEntry fr, TextureAtlasEntry ba, TextureAtlasEntry to, TextureAtlasEntry bot, TextureAtlasEntry le, TextureAtlasEntry ri)
GLBox(GLFix width, GLFix height, GLFix length, GLFix cenX, GLFix cenY, GLFix cenZ, TextureAtlasEntry fr, TextureAtlasEntry ba, TextureAtlasEntry to, TextureAtlasEntry bot, TextureAtlasEntry le, TextureAtlasEntry ri)
: GLBox(width, height, length, cenX, cenY, cenZ, fr, ba, to, bot, le, ri, true, true, true, true, true, true) {}

void addChild(std::shared_ptr<GLDrawable> child) { children.push_back(child); }
Expand Down
2 changes: 1 addition & 1 deletion perlinnoise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void PerlinNoise::setSeed(unsigned int seed)
p.insert(p.end(), p.begin(), p.end());
}

float PerlinNoise::noise(GLFix x, GLFix y, GLFix z) const {
GLFix PerlinNoise::noise(GLFix x, GLFix y, GLFix z) const {
// Find the unit cube that contains the point
int X = x.floor() & 255;
int Y = y.floor() & 255;
Expand Down
2 changes: 1 addition & 1 deletion perlinnoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PerlinNoise {
// Generate a new permutation vector based on the value of seed
void setSeed(unsigned int seed);
// Get a noise value, for 2D images z can have any value
float noise(GLFix x, GLFix y, GLFix z) const;
GLFix noise(GLFix x, GLFix y, GLFix z) const;
private:
GLFix fade(GLFix t) const;
GLFix lerp(GLFix t, GLFix a, GLFix b) const;
Expand Down
4 changes: 2 additions & 2 deletions world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void World::generateSeed()

BLOCK World::getBlock(int x, int y, int z) const
{
int chunk_x = floor(float(x) / Chunk::SIZE), chunk_y = floor(float(y) / Chunk::SIZE), chunk_z = floor(float(z) / Chunk::SIZE);
int chunk_x = (GLFix(x) / Chunk::SIZE).floor(), chunk_y = (GLFix(y) / Chunk::SIZE).floor(), chunk_z = (GLFix(z) / Chunk::SIZE).floor();

Chunk *c = findChunk(chunk_x, chunk_y, chunk_z);
if(!c)
Expand Down Expand Up @@ -80,7 +80,7 @@ void World::setChunkVisible(int x, int y, int z)

void World::setPosition(int x, int y, int z)
{
int chunk_x = round(float(x) / (Chunk::SIZE * BLOCK_SIZE)), chunk_y = round(float(y) / (Chunk::SIZE * BLOCK_SIZE)), chunk_z = round(float(z) / (Chunk::SIZE * BLOCK_SIZE));
int chunk_x = (GLFix(x) / (Chunk::SIZE * BLOCK_SIZE)).floor(), chunk_y = (GLFix(y) / (Chunk::SIZE * BLOCK_SIZE)).floor(), chunk_z = (GLFix(z) / (Chunk::SIZE * BLOCK_SIZE)).floor();

chunk_y = std::max(0, std::min(chunk_y, World::HEIGHT - 1));

Expand Down

0 comments on commit d6c686b

Please sign in to comment.