Skip to content

Commit

Permalink
refactor: create random utils to replace rand()
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumichocopengin8 committed Mar 28, 2022
1 parent f64cdec commit 8f96282
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ add_executable(
block/types_of_block.h
camera/Camera.cpp
camera/Camera.h
utils/Utils.cpp
utils/Utils.h
window/Window.cpp
window/Window.h
player/Player.cpp
Expand Down
22 changes: 8 additions & 14 deletions chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../structures/Cloud.h"
#include "../structures/SnowFlake.h"
#include "../main.h"
#include "../utils/Utils.h"


Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
Expand All @@ -24,7 +25,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {


// PerlinNoise Setup
uint32_t seed = rand();
uint32_t seed = utils::random(RAND_MAX);
double frequency = 0.1;
int octaves = 4;
const siv::PerlinNoise perlin(seed);
Expand Down Expand Up @@ -123,7 +124,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
key = std::to_string(this->x + 1) + "_" + std::to_string(this->z);
if (worldPtr->chunks.count(key) > 0) {
if (this->biome != worldPtr->chunks[key]->biome) {
int random_number = rand() % 100;
int random_number = utils::random(RAND_MAX) % 100;
int prob_actual_next_ground = 0;

if (i == CHUNK_SIZE - 1) prob_actual_next_ground = 80;
Expand All @@ -138,7 +139,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
key = std::to_string(this->x) + "_" + std::to_string(this->z + 1);
if (worldPtr->chunks.count(key) > 0) {
if (this->biome != worldPtr->chunks[key]->biome) {
int random_number = rand() % 100;
int random_number = utils::random(RAND_MAX) % 100;
int prob_actual_next_ground = 0;

if (j == CHUNK_SIZE - 1) prob_actual_next_ground = 80;
Expand All @@ -153,7 +154,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
key = std::to_string(this->x - 1) + "_" + std::to_string(this->z);
if (worldPtr->chunks.count(key) > 0) {
if (this->biome != worldPtr->chunks[key]->biome) {
int random_number = rand() % 100;
int random_number = utils::random(RAND_MAX) % 100;
int prob_actual_next_ground = 0;

if (i == 0) prob_actual_next_ground = 80;
Expand All @@ -168,7 +169,7 @@ Chunk::Chunk(int chunk_x, int chunk_z, BiomeType *biome) {
key = std::to_string(this->x) + "_" + std::to_string(this->z - 1);
if (worldPtr->chunks.count(key) > 0) {
if (this->biome != worldPtr->chunks[key]->biome) {
int random_number = rand() % 100;
int random_number = utils::random(RAND_MAX) % 100;
int prob_actual_next_ground = 0;

if (j == 0) prob_actual_next_ground = 80;
Expand Down Expand Up @@ -212,13 +213,6 @@ void Chunk::render() {
}
}

int Chunk::random(int max) {
std::random_device seed_gen;
std::mt19937_64 engine(seed_gen()); // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
std::uniform_int_distribution<> dist(0, max);
return dist(engine);
}

int Chunk::getBlock(int x, int y, int z) {
return this->blocks[x][y][z];
}
Expand Down Expand Up @@ -251,7 +245,7 @@ void Chunk::generateStructures() {

// Get surface_height
int surface_height = this->heights[i][j];
int random_number = this->random(100);
auto random_number = utils::random(100);

// Trees
if (random_number < this->biome->tree_frequency * 100) {
Expand All @@ -276,7 +270,7 @@ void Chunk::generateStructures() {
// Cloud
for (int i = 5; i < CHUNK_SIZE - 5; ++i) {
for (int j = 5; j < CHUNK_SIZE - 5; ++j) {
int prob = this->random(1000);
int prob = utils::random(1000);
if (prob < 50) {
int surface_height = 0;
for (int k = 0; k < CHUNK_HEIGHT; ++k) {
Expand Down
2 changes: 0 additions & 2 deletions chunk/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Chunk {

void render();

int random(int max);

int getBlock(int x, int y, int z);

int getHeight(int x, int z);
Expand Down
10 changes: 2 additions & 8 deletions structures/Cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

#include "Cloud.h"
#include "../utils/Utils.h"
#include <random>


Expand All @@ -12,7 +13,7 @@ namespace {
}

Cloud::Cloud(int x, int y, int z, Chunk *chunk) {
int radius_cloud = MIN_RADIUS_CLOUD + Cloud::random(MAX_RADIUS_CLOUD - MIN_RADIUS_CLOUD);
int radius_cloud = MIN_RADIUS_CLOUD + utils::random(MAX_RADIUS_CLOUD - MIN_RADIUS_CLOUD);

for (int i = -radius_cloud; i < radius_cloud; ++i) {
for (int j = -radius_cloud; j < radius_cloud; ++j) {
Expand All @@ -23,10 +24,3 @@ Cloud::Cloud(int x, int y, int z, Chunk *chunk) {
}
}
}

int Cloud::random(int max) {
std::random_device seed_gen;
std::mt19937_64 engine(seed_gen()); // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
std::uniform_int_distribution<> dist(0, max);
return dist(engine);
}
2 changes: 0 additions & 2 deletions structures/Cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
class Cloud {
public:
explicit Cloud(int x, int y, int z, Chunk *chunk);

static int random(int max);
};


Expand Down
5 changes: 3 additions & 2 deletions structures/Rock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

#include "Rock.h"
#include "../utils/Utils.h"
#include <random>

Rock::Rock(int x, int y, int z, Chunk *chunk) {
Expand All @@ -13,10 +14,10 @@ Rock::Rock(int x, int y, int z, Chunk *chunk) {
for (int i = -2; i < 2; ++i) {
for (int j = -2; j < 2; ++j) {

int random_number = rand() % 100;
int random_number = utils::random(RAND_MAX) % 100;
if (random_number < proba) {
chunk->blocks[x + i][chunk->heights[x + i][z + j] + 1][z + j] = ROCK;
random_number = rand() % 100;
random_number = utils::random(RAND_MAX) % 100;
if (random_number < proba_mount) {
chunk->blocks[x + i][chunk->heights[x + i][z + j] + 2][z + j] = ROCK;
}
Expand Down
7 changes: 4 additions & 3 deletions structures/Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

#include "Tree.h"
#include "../utils/Utils.h"
#include <random>
#include <cmath>

Expand All @@ -17,7 +18,7 @@ namespace {

Tree::Tree(int x, int y, int z, Chunk *chunk) {

int height_tree = MIN_HEIGHT_TREE + rand() % (MAX_HEIGHT_TREE - MIN_HEIGHT_TREE);
int height_tree = MIN_HEIGHT_TREE + utils::random(RAND_MAX) % (MAX_HEIGHT_TREE - MIN_HEIGHT_TREE);

for (int i = 0; i < height_tree; ++i) {
if (chunk->blocks[x][y + i][z] == AIR) {
Expand All @@ -26,7 +27,7 @@ Tree::Tree(int x, int y, int z, Chunk *chunk) {
}


int radius_tree = MIN_RADIUS_TREE + rand() % (MAX_RADIUS_TREE - MIN_RADIUS_TREE);
int radius_tree = MIN_RADIUS_TREE + utils::random(RAND_MAX) % (MAX_RADIUS_TREE - MIN_RADIUS_TREE);
int texture = Tree::chooseTexture();

for (int i = -radius_tree + 1; i <= radius_tree - 1; i++) {
Expand Down Expand Up @@ -69,7 +70,7 @@ Tree::Tree(int x, int y, int z, Chunk *chunk) {
}

int Tree::chooseTexture() {
int num = rand() % 100;
int num = utils::random(RAND_MAX) % 100;
int texture = 0;
if (0 <= num && num < 33) {
texture = TREELEAVES;
Expand Down
13 changes: 13 additions & 0 deletions utils/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Created by Keita Nonaka on 3/28/22.
//

#include <random>
#include "Utils.h"

int utils::random(int max) {
std::random_device seed_gen;
std::mt19937_64 engine(seed_gen()); // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
std::uniform_int_distribution<> dist(0, max);
return dist(engine);
}
12 changes: 12 additions & 0 deletions utils/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by Keita Nonaka on 3/28/22.
//

#ifndef PROJECT_UTILS_H
#define PROJECT_UTILS_H

namespace utils {
int random(int max);
}

#endif //PROJECT_UTILS_H
3 changes: 2 additions & 1 deletion world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cmath>
#include "World.h"
#include "../main.h"
#include "../utils/Utils.h"

World::World() {
this->chunks = std::map<std::string, Chunk *>();
Expand Down Expand Up @@ -139,7 +140,7 @@ BiomeType *World::chooseChunkBiome(int chunk_x, int chunk_y) {


auto it = possibleBiomeTypes.begin();
advance(it, rand() % possibleBiomeTypes.size());
advance(it, utils::random(RAND_MAX) % possibleBiomeTypes.size());
Biome biome(*it, chunk_x, chunk_y, this->biome_types[*it]);
this->biomes.push_back(biome);
return this->biome_types[*it];
Expand Down

0 comments on commit 8f96282

Please sign in to comment.