From fe2baedc760c2f29e2c720f6b1132a2de33c5430 Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 8 Jul 2023 03:25:44 +0200 Subject: Use own size types --- src/World/BiomeType.hpp | 6 +-- src/World/BlockSide.hpp | 6 +-- src/World/BlockType.hpp | 8 ++-- src/World/Chunk.cpp | 10 ++-- src/World/Chunk.hpp | 22 ++++----- src/World/ChunkIndex.hpp | 12 ++--- src/World/Generation/ChunkMeshing.cpp | 54 +++++++++++----------- src/World/Generation/Decoration.cpp | 42 ++++++++--------- src/World/Generation/Decoration.hpp | 10 ++-- src/World/Generation/Generator.cpp | 86 +++++++++++++++++------------------ src/World/Generation/Generator.hpp | 49 ++++++++++---------- src/World/World.cpp | 24 +++++----- src/World/World.hpp | 16 +++---- 13 files changed, 172 insertions(+), 173 deletions(-) (limited to 'src/World') diff --git a/src/World/BiomeType.hpp b/src/World/BiomeType.hpp index 40583d9..2380b60 100644 --- a/src/World/BiomeType.hpp +++ b/src/World/BiomeType.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include "../Common/Sizes.hpp" #include #include @@ -8,7 +8,7 @@ namespace MC::World { class BiomeType { public: - enum Value : uint8_t { + enum Value : U8 { Plains, Forest, Alpine, @@ -21,7 +21,7 @@ public: Ocean, }; - static constexpr uint8_t Size = Ocean + 1; + static constexpr U8 Size = Ocean + 1; BiomeType() : m_biome(Plains) {} BiomeType(const Value biome) : m_biome(biome) {} diff --git a/src/World/BlockSide.hpp b/src/World/BlockSide.hpp index 57d3c68..69df159 100644 --- a/src/World/BlockSide.hpp +++ b/src/World/BlockSide.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include "../Common/Sizes.hpp" #include #include #include "../Math/Vector.hpp" @@ -9,7 +9,7 @@ namespace MC::World { class BlockSide { public: - enum Value : uint8_t { + enum Value : U8 { Front, Back, @@ -25,7 +25,7 @@ public: operator Value() const { return m_side; } - std::array, 4> face() { + std::array, 4> face() { switch (m_side) { case Front: return {{ diff --git a/src/World/BlockType.hpp b/src/World/BlockType.hpp index 82c63d3..dbf6f78 100644 --- a/src/World/BlockType.hpp +++ b/src/World/BlockType.hpp @@ -1,13 +1,13 @@ #pragma once -#include +#include "../Common/Sizes.hpp" #include namespace MC::World { class BlockType { public: - enum Value : uint8_t { + enum Value : U8 { Air, Dirt, Grass, @@ -19,14 +19,14 @@ public: Water, }; - static constexpr uint8_t Size = Water + 1; + static constexpr U8 Size = Water + 1; BlockType() : m_block(Air) {} BlockType(Value block) : m_block(block) {} operator Value() const { return m_block; } - bool is_transparent() const { + Bool is_transparent() const { switch (m_block) { case Air: case Leaves: diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index 079575a..94e757e 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -3,15 +3,15 @@ namespace MC::World { -void Chunk::set(uint32_t x, uint32_t y, uint32_t z, BlockData data) { +void Chunk::set(U32 x, U32 y, U32 z, BlockData data) { m_blocks.at(pos(x, y, z)) = data; } -Chunk::BlockData Chunk::get(uint32_t x, uint32_t y, uint32_t z) const { +Chunk::BlockData Chunk::get(U32 x, U32 y, U32 z) const { return m_blocks.at(pos(x, y, z)); } -bool Chunk::is_empty(uint32_t x, uint32_t y, uint32_t z) const { +Bool Chunk::is_empty(U32 x, U32 y, U32 z) const { return get(x, y, z).empty(); } @@ -19,11 +19,11 @@ Vector<3> Chunk::position() const { return m_position; } -bool Chunk::is_valid_position(uint32_t x, uint32_t y, uint32_t z) { +Bool Chunk::is_valid_position(U32 x, U32 y, U32 z) { return x < Width && y < Height && z < Width; } -uint64_t Chunk::pos(uint32_t x, uint32_t y, uint32_t z) { +U64 Chunk::pos(U32 x, U32 y, U32 z) { return x + Width * y + Width * Height * z; } diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index b3eb06f..bc44137 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include "../Common/Sizes.hpp" #include "BiomeType.hpp" #include "BlockType.hpp" #include "../GFX/Mesh.hpp" @@ -10,22 +10,22 @@ namespace MC::World { class Chunk { public: - static constexpr uint32_t Width = 16; - static constexpr uint32_t Height = 128; + static constexpr U32 Width = 16; + static constexpr U32 Height = 128; - Chunk(int64_t x, int64_t y) + Chunk(I64 x, I64 y) : m_blocks{Width * Height * Width, {BlockType::Air}}, - m_position{(float)x * Width, 0.0f, (float)y * Width} {} + m_position{(Real)x * Width, 0.0f, (Real)y * Width} {} struct BlockData { BlockType type; - bool empty() const { return type == BlockType::Air; } + Bool empty() const { return type == BlockType::Air; } }; - void set(uint32_t x, uint32_t y, uint32_t z, BlockData data); - BlockData get(uint32_t x, uint32_t y, uint32_t z) const; - bool is_empty(uint32_t x, uint32_t y, uint32_t z) const; + void set(U32 x, U32 y, U32 z, BlockData data); + BlockData get(U32 x, U32 y, U32 z) const; + Bool is_empty(U32 x, U32 y, U32 z) const; struct Details { Matrix landmass_values{}; @@ -41,9 +41,9 @@ public: Vector<3> position() const; - static bool is_valid_position(uint32_t x, uint32_t y, uint32_t z); + static Bool is_valid_position(U32 x, U32 y, U32 z); private: - static uint64_t pos(uint32_t x, uint32_t y, uint32_t z); + static U64 pos(U32 x, U32 y, U32 z); Vector<3> m_position; std::vector m_blocks; diff --git a/src/World/ChunkIndex.hpp b/src/World/ChunkIndex.hpp index ef61f24..7d6fdbf 100644 --- a/src/World/ChunkIndex.hpp +++ b/src/World/ChunkIndex.hpp @@ -1,30 +1,30 @@ #pragma once -#include +#include "../Common/Sizes.hpp" namespace MC::World { struct ChunkIndex { ChunkIndex() : x(0), y(0) {} - ChunkIndex(int32_t x, int32_t y) : x(x), y(y) {} + ChunkIndex(I32 x, I32 y) : x(x), y(y) {} Vector<3> middle() const { return {(x + 0.5f) * Chunk::Width, Chunk::Height / 2.0f, (y + 0.5f) * Chunk::Width}; } - int32_t x, y; + I32 x, y; }; } template<> struct std::equal_to { - bool operator()(const MC::World::ChunkIndex& i1, const MC::World::ChunkIndex& i2) const noexcept { + Bool operator()(const MC::World::ChunkIndex& i1, const MC::World::ChunkIndex& i2) const noexcept { return i1.x == i2.x && i1.y == i2.y; } }; template<> struct std::hash { - size_t operator()(const MC::World::ChunkIndex& i) const noexcept { - return (int64_t)i.x << 32 | i.y; + USize operator()(const MC::World::ChunkIndex& i) const noexcept { + return (I64)i.x << 32 | i.y; } }; \ No newline at end of file diff --git a/src/World/Generation/ChunkMeshing.cpp b/src/World/Generation/ChunkMeshing.cpp index e065987..8996169 100644 --- a/src/World/Generation/ChunkMeshing.cpp +++ b/src/World/Generation/ChunkMeshing.cpp @@ -2,20 +2,20 @@ namespace MC::World::Generation { -std::array, 4> face_tex_coords(BlockType type, BlockSide side) { - uint8_t atlas_width = 4; - uint8_t atlas_height = 4; +std::array, 4> face_tex_coords(BlockType type, BlockSide side) { + U8 atlas_width = 4; + U8 atlas_height = 4; - float width_step = 1.0f / atlas_width; - float height_step = 1.0f / atlas_height; + Real width_step = 1.0f / atlas_width; + Real height_step = 1.0f / atlas_height; - auto block_coords = [=](uint8_t x, uint8_t y) { + auto block_coords = [=](U8 x, U8 y) { auto t = y * height_step; auto l = x * width_step; auto b = t + height_step; auto r = l + width_step; - return std::array, 4>{{ + return std::array, 4>{{ {l, b}, {r, b}, {r, t}, {l, t}, }}; }; @@ -71,10 +71,10 @@ std::array, 4> face_tex_coords(BlockType type, BlockSide side) { } } -std::array, 4> face_normals(BlockSide side) { - auto is_side = [=](BlockSide s) -> float { return s == side; }; +std::array, 4> face_normals(BlockSide side) { + auto is_side = [=](BlockSide s) -> Real { return s == side; }; - Vector<3> normal = { + Vector<3, F32> normal = { is_side(BlockSide::Right) - is_side(BlockSide::Left), is_side(BlockSide::Top) - is_side(BlockSide::Bottom), is_side(BlockSide::Front) - is_side(BlockSide::Back), @@ -83,8 +83,8 @@ std::array, 4> face_normals(BlockSide side) { return {normal, normal, normal, normal}; } -bool is_face_visible(Chunk& chunk, const ChunkMeshing::ChunkNeighbors& neighbors, uint32_t x, uint32_t y, uint32_t z, BlockSide side) { - Vector<3, int32_t> offset{}; +Bool is_face_visible(Chunk& chunk, const ChunkMeshing::ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) { + Vector<3, I32> offset{}; switch (side) { case BlockSide::Front: offset[2] = 1; @@ -106,8 +106,8 @@ bool is_face_visible(Chunk& chunk, const ChunkMeshing::ChunkNeighbors& neighbors break; } - Vector<3, int32_t> neighbor_pos{ - (int32_t)x + offset.x(), (int32_t)y + offset.y(), (int32_t)z + offset.z(), + Vector<3, I32> neighbor_pos{ + (I32)x + offset.x(), (I32)y + offset.y(), (I32)z + offset.z(), }; Chunk* chunk_to_ask; @@ -115,10 +115,10 @@ bool is_face_visible(Chunk& chunk, const ChunkMeshing::ChunkNeighbors& neighbors if (neighbor_pos.z() < 0) { chunk_to_ask = &neighbors.north; neighbor_pos.z() += Chunk::Width; - } else if (neighbor_pos.x() >= (int32_t)Chunk::Width) { + } else if (neighbor_pos.x() >= (I32)Chunk::Width) { chunk_to_ask = &neighbors.east; neighbor_pos.x() -= Chunk::Width; - } else if (neighbor_pos.z() >= (int32_t)Chunk::Width) { + } else if (neighbor_pos.z() >= (I32)Chunk::Width) { chunk_to_ask = &neighbors.south; neighbor_pos.z() -= Chunk::Width; } else if (neighbor_pos.x() < 0) { @@ -133,14 +133,14 @@ bool is_face_visible(Chunk& chunk, const ChunkMeshing::ChunkNeighbors& neighbors } GFX::Mesh ChunkMeshing::create_mesh_for_chunk(Chunk& chunk, const ChunkNeighbors& neighbors) { - std::vector> positions{}; - std::vector> normals{}; - std::vector> tex_coords{}; - std::vector indices{}; - - for (int x = 0; x < Chunk::Width; x++) { - for (int y = 0; y < Chunk::Height; y++) { - for (int z = 0; z < Chunk::Width; z++) { + std::vector> positions{}; + std::vector> normals{}; + std::vector> tex_coords{}; + std::vector indices{}; + + for (Int x = 0; x < Chunk::Width; x++) { + for (Int y = 0; y < Chunk::Height; y++) { + for (Int z = 0; z < Chunk::Width; z++) { auto type = chunk.get(x, y, z).type; if (type == BlockType::Air) { continue; @@ -156,10 +156,10 @@ GFX::Mesh ChunkMeshing::create_mesh_for_chunk(Chunk& chunk, const ChunkNeighbors auto side_tex_coords = face_tex_coords(type, side); for (auto& position : side_positions) { - position = position + Vector<3>{static_cast(x), static_cast(y), static_cast(z)}; + position = position + Vector<3, F32>{x, y, z}; } - uint32_t s = positions.size(); + U32 s = positions.size(); positions.insert(positions.end(), side_positions.begin(), side_positions.end()); normals.insert(normals.end(), side_normals.begin(), side_normals.end()); @@ -170,7 +170,7 @@ GFX::Mesh ChunkMeshing::create_mesh_for_chunk(Chunk& chunk, const ChunkNeighbors } } - return GFX::Mesh{ + return { {positions, normals, tex_coords}, indices, }; diff --git a/src/World/Generation/Decoration.cpp b/src/World/Generation/Decoration.cpp index 26eb397..f3f7c46 100644 --- a/src/World/Generation/Decoration.cpp +++ b/src/World/Generation/Decoration.cpp @@ -12,26 +12,26 @@ void Decorator::put_block(Chunk& chunk, Pos pos, BlockType block) { } } -void Decorator::draw_column(Chunk& chunk, Pos pos, uint height, BlockType block) { +void Decorator::draw_column(Chunk& chunk, Pos pos, UInt height, BlockType block) { Pos current_pos = pos; - for (uint i = 0; i < height; i++) { + for (UInt i = 0; i < height; i++) { put_block(chunk, current_pos, block); current_pos += Pos::up(); } } -void Decorator::draw_circle(Chunk& chunk, Pos pos, Vector<3> axis, float radius, BlockType block) { +void Decorator::draw_circle(Chunk& chunk, Pos pos, Vector<3> axis, Real radius, BlockType block) { auto normalized_axis = axis.normalize(); auto ortho1 = normalized_axis.any_orthogonal(); auto ortho2 = normalized_axis.cross(ortho1); - auto r = [](const float x) { return static_cast(std::round(x)); }; + auto r = [](const Real x) { return static_cast(std::round(x)); }; - int radius_round = std::round(radius); - for (int32_t d1 = -radius_round; d1 <= radius_round; d1++) { - float height = std::sqrt(radius * radius - d1 * d1); - for (int32_t d2 = -height; d2 <= (int)height; d2++) { + Int radius_round = std::round(radius); + for (I32 d1 = -radius_round; d1 <= radius_round; d1++) { + Real height = std::sqrt(radius * radius - d1 * d1); + for (I32 d2 = -height; d2 <= (Int)height; d2++) { auto p = ortho1 * d1 + ortho2 * d2; Pos block_pos = pos + Pos{r(p.x()), r(p.y()), r(p.z())}; put_block(chunk, block_pos, block); @@ -41,9 +41,9 @@ void Decorator::draw_circle(Chunk& chunk, Pos pos, Vector<3> axis, float radius, void TreeDecorator::decorate_chunk(Chunk& chunk) { Pos last_tree = Pos::max(); - for (uint x = 0; x < Chunk::Width; x++) { - for (uint z = 0; z < Chunk::Width; z++) { - for (uint y = Chunk::Height; y > 1; y--) { + for (UInt x = 0; x < Chunk::Width; x++) { + for (UInt z = 0; z < Chunk::Width; z++) { + for (UInt y = Chunk::Height; y > 1; y--) { Pos pos{x, y, z}; if (!is_valid_position(pos)) continue; @@ -56,7 +56,7 @@ void TreeDecorator::decorate_chunk(Chunk& chunk) { if (type != BlockType::Snow && type != BlockType::Grass && type != BlockType::Dirt) break; - auto noise = m_tree_noise.at({(float)x, (float)z}); + auto noise = m_tree_noise.at({(Real)x, (Real)z}); if (noise < 0.8f) continue; @@ -73,24 +73,24 @@ void TreeDecorator::decorate_chunk(Chunk& chunk) { } void TreeDecorator::draw_tree(Chunk& chunk, Pos pos) const { - auto noise = m_tree_noise.at({(float)pos.x(), (float)pos.z()}); - uint height = std::round(10 * noise - 4.75f); + auto noise = m_tree_noise.at({(Real)pos.x(), (Real)pos.z()}); + UInt height = std::round(10 * noise - 4.75f); draw_column(chunk, pos, height, BlockType::Wood); - uint max_leaf_height = 4; - for (int x = 0; x < max_leaf_height; x++) { + UInt max_leaf_height = 4; + for (Int x = 0; x < max_leaf_height; x++) { Pos p{pos.x(), pos.y() + height + x - 2, pos.z()}; - float radius = s_tree_radius - 0.5f + x * ((0.3f * x - 1.45f) * x + 1.25f); + Real radius = s_tree_radius - 0.5f + x * ((0.3f * x - 1.45f) * x + 1.25f); draw_circle(chunk, p, Vector<3>::up(), radius, BlockType::Leaves); } } -bool TreeDecorator::is_valid_position(Vector<3, uint> pos) { - int tree_radius = s_tree_radius; - return (int)pos.x() - tree_radius >= 0 +Bool TreeDecorator::is_valid_position(Vector<3, UInt> pos) { + Int tree_radius = s_tree_radius; + return (Int)pos.x() - tree_radius >= 0 && pos.x() + tree_radius <= Chunk::Width - && (int)pos.z() - tree_radius >= 0 + && (Int)pos.z() - tree_radius >= 0 && pos.z() + tree_radius <= Chunk::Width; } diff --git a/src/World/Generation/Decoration.hpp b/src/World/Generation/Decoration.hpp index a592e72..b7a839b 100644 --- a/src/World/Generation/Decoration.hpp +++ b/src/World/Generation/Decoration.hpp @@ -7,15 +7,15 @@ namespace MC::World::Generation { class Decorator { public: - using Pos = Vector<3, uint>; + using Pos = Vector<3, UInt>; virtual ~Decorator() = default; virtual void decorate_chunk(Chunk& chunk) = 0; static void put_block(Chunk& chunk, Pos pos, BlockType block); - static void draw_column(Chunk& chunk, Pos pos, uint height, BlockType block); - static void draw_circle(Chunk& chunk, Pos pos, Vector<3> axis, float radius, BlockType block); + static void draw_column(Chunk& chunk, Pos pos, UInt height, BlockType block); + static void draw_circle(Chunk& chunk, Pos pos, Vector<3> axis, Real radius, BlockType block); }; class TreeDecorator final : public Decorator { @@ -24,9 +24,9 @@ public: private: void draw_tree(Chunk& chunk, Pos pos) const; - static bool is_valid_position(Pos pos); + static Bool is_valid_position(Pos pos); - static constexpr uint s_tree_radius = 3; + static constexpr UInt s_tree_radius = 3; Math::Random::Noise<2> m_tree_noise; }; diff --git a/src/World/Generation/Generator.cpp b/src/World/Generation/Generator.cpp index 19a3dc8..88e69a7 100644 --- a/src/World/Generation/Generator.cpp +++ b/src/World/Generation/Generator.cpp @@ -4,7 +4,7 @@ namespace MC::World::Generation { -Chunk Generator::generate(int64_t chunk_x, int64_t chunk_y) { +Chunk Generator::generate(I64 chunk_x, I64 chunk_y) { Chunk chunk(chunk_x, chunk_y); auto landmass_map = generate_landmass_map(chunk_x, chunk_y); @@ -24,10 +24,10 @@ Chunk Generator::generate(int64_t chunk_x, int64_t chunk_y) { } #define SIMPLE_MAP_GENERATOR(name, getter) \ -Generator::Map2D Generator::name(int64_t chunk_x, int64_t chunk_y) { \ +Generator::Map2D Generator::name(I64 chunk_x, I64 chunk_y) { \ Matrix map{}; \ - for (uint x = 0; x < Chunk::Width; x++) { \ - for (uint y = 0; y < Chunk::Width; y++) { \ + for (UInt x = 0; x < Chunk::Width; x++) { \ + for (UInt y = 0; y < Chunk::Width; y++) { \ auto pos = chunk_position_to_world_vector(chunk_x, chunk_y, x, y); \ map(x, y) = getter(pos); \ } \ @@ -38,11 +38,11 @@ Generator::Map2D Generator::name(int64_t chunk_x, int64_t chunk_y) { SIMPLE_MAP_GENERATOR(generate_landmass_map, get_landmass) SIMPLE_MAP_GENERATOR(generate_hill_map, get_hill) -Generator::Map2D Generator::generate_height_map(Map2D& landmass_map, Map2D& hill_map) { - Map2D height_map{}; +Generator::Map2D Generator::generate_height_map(Map2D& landmass_map, Map2D& hill_map) { + Map2D height_map{}; - for (uint x = 0; x < Chunk::Width; x++) { - for (uint y = 0; y < Chunk::Width; y++) { + for (UInt x = 0; x < Chunk::Width; x++) { + for (UInt y = 0; y < Chunk::Width; y++) { auto landmass_effect = landmass_map(x, y); auto hill_effect = hill_map(x, y); @@ -62,18 +62,18 @@ SIMPLE_MAP_GENERATOR(generate_temperature_map, get_temperature) SIMPLE_MAP_GENERATOR(generate_humidity_map, get_humidity) Generator::Map2D Generator::generate_biome_map( - Map2D& landmass_map, Map2D& hill_map, - Map2D& temperature_map, Map2D& humidity_map + Map2D& landmass_map, Map2D& hill_map, + Map2D& temperature_map, Map2D& humidity_map ) { Map2D biome_map{}; - for (uint x = 0; x < Chunk::Width; x++) { - for (uint y = 0; y < Chunk::Width; y++) { - float landmass = landmass_map(x, y); - float hill = hill_map(x, y); + for (UInt x = 0; x < Chunk::Width; x++) { + for (UInt y = 0; y < Chunk::Width; y++) { + Real landmass = landmass_map(x, y); + Real hill = hill_map(x, y); - float temperature = temperature_map(x, y); - float humidity = humidity_map(x, y); + Real temperature = temperature_map(x, y); + Real humidity = humidity_map(x, y); HillSlice hill_slice; if (hill > 0.55) { hill_slice = HillSlice::Mountain; } @@ -103,17 +103,17 @@ Generator::Map2D Generator::generate_biome_map( return biome_map; } -Generator::Map3D Generator::generate_terrain(Map2D& height_map, int64_t chunk_x, int64_t chunk_y) { - float jaggedness = 0.10f; +Generator::Map3D Generator::generate_terrain(Map2D& height_map, I64 chunk_x, I64 chunk_y) { + Real jaggedness = 0.10f; - Map3D terrain_map{}; - for (uint x = 0; x < Chunk::Width; x++) { - for (uint z = 0; z < Chunk::Width; z++) { + Map3D terrain_map{}; + for (UInt x = 0; x < Chunk::Width; x++) { + for (UInt z = 0; z < Chunk::Width; z++) { auto height = height_map(x, z); - for (uint y = 0; y < Chunk::Height; y++) { - float density = get_density({Chunk::Width * chunk_x + (float)x, (float)y, Chunk::Width * chunk_y + (float)z}); - float threshold = Math::sigmoid(((float)y / (Chunk::Height * height * 2) - 0.5f) / jaggedness); + for (UInt y = 0; y < Chunk::Height; y++) { + Real density = get_density({Chunk::Width * chunk_x + (Real)x, (Real)y, Chunk::Width * chunk_y + (Real)z}); + Real threshold = Math::sigmoid(((Real)y / (Chunk::Height * height * 2) - 0.5f) / jaggedness); if (density > threshold) { terrain_map(x, y, z) = true; @@ -125,12 +125,12 @@ Generator::Map3D Generator::generate_terrain(Map2D& height_map, int return terrain_map; } -void Generator::decorate_soil(Chunk& chunk, Map2D& biome_map, Map3D& terrain_map) { - constexpr uint dirt_depth = 4; - constexpr uint water_height = 40; +void Generator::decorate_soil(Chunk& chunk, Map2D& biome_map, Map3D& terrain_map) { + constexpr UInt dirt_depth = 4; + constexpr UInt water_height = 40; - for (uint x = 0; x < Chunk::Width; x++) { - for (uint z = 0; z < Chunk::Width; z++) { + for (UInt x = 0; x < Chunk::Width; x++) { + for (UInt z = 0; z < Chunk::Width; z++) { auto biome = biome_map(x, z); BlockType top_block{}; @@ -160,7 +160,7 @@ void Generator::decorate_soil(Chunk& chunk, Map2D& biome_map, Map3D 0; y--) { + for (UInt y = Chunk::Height - 1; y > 0; y--) { auto block = terrain_map(x, y, z); if (block) { if (column_depth == 0 && y >= water_height - 1) { @@ -186,11 +186,11 @@ void Generator::decorate_soil(Chunk& chunk, Map2D& biome_map, Map3D pos) const { +Real Generator::get_landmass(Vector<2> pos) const { auto v = m_landmass_noise.at(pos); CURVE_START(1.0f); @@ -205,7 +205,7 @@ float Generator::get_landmass(Vector<2> pos) const { CURVE_END(1.0f); } -float Generator::get_hill(Vector<2> pos) const { +Real Generator::get_hill(Vector<2> pos) const { auto v = m_hill_noise.at(pos); CURVE_START(0.33f); @@ -219,30 +219,30 @@ float Generator::get_hill(Vector<2> pos) const { CURVE_END(0.33f); } -float Generator::get_humidity(Vector<2> pos) const { +Real Generator::get_humidity(Vector<2> pos) const { auto v = m_humidity_noise.at(pos); return v; } -float Generator::get_temperature(Vector<2> pos) const { +Real Generator::get_temperature(Vector<2> pos) const { auto v = m_temperature_noise.at(pos); return v; } -float Generator::get_density(Vector<3> pos) const { +Real Generator::get_density(Vector<3> pos) const { auto v = m_density_noise.at(pos); return v; } -Vector<2> Generator::chunk_position_to_world_vector(int64_t chunk_x, int64_t chunk_y, uint x, uint y) { - return {(float)x + chunk_x * Chunk::Width, (float)y + chunk_y * Chunk::Width}; +Vector<2> Generator::chunk_position_to_world_vector(I64 chunk_x, I64 chunk_y, UInt x, UInt y) { + return {(Real)x + chunk_x * Chunk::Width, (Real)y + chunk_y * Chunk::Width}; } std::array Generator::create_biome_lookup_table() { std::array table{}; - auto inc = [](auto& x) { x = (std::remove_reference_t)((uint)x + 1); }; - auto cmp = [](auto x, auto s) { return (uint)x < (uint)s; }; + auto inc = [](auto& x) { x = (std::remove_reference_t)((UInt)x + 1); }; + auto cmp = [](auto x, auto s) { return (UInt)x < (UInt)s; }; for (HillSlice hill_slice{}; cmp(hill_slice, HillSliceSize); inc(hill_slice)) { for (LandmassSlice landmass_slice{}; cmp(landmass_slice, LandmassSliceSize); inc(landmass_slice)) { @@ -317,9 +317,9 @@ std::array Generator::create_biom return table; } -size_t Generator::biome_lookup_table_index(HillSlice hill_slice, LandmassSlice landmass_slice, TemperatureZone temperature_zone, HumidityZone humidity_zone) { - auto hs = (uint8_t)hill_slice; auto ls = (uint8_t)landmass_slice; auto tz = (uint8_t)temperature_zone; auto hz = (uint8_t)humidity_zone; - auto LS_S = (uint8_t)LandmassSliceSize; auto TZ_S = (uint8_t)TemperatureZoneSize; auto HZ_S = (uint8_t)HumidityZoneSize; +USize Generator::biome_lookup_table_index(HillSlice hill_slice, LandmassSlice landmass_slice, TemperatureZone temperature_zone, HumidityZone humidity_zone) { + auto hs = (U8)hill_slice; auto ls = (U8)landmass_slice; auto tz = (U8)temperature_zone; auto hz = (U8)humidity_zone; + auto LS_S = (U8)LandmassSliceSize; auto TZ_S = (U8)TemperatureZoneSize; auto HZ_S = (U8)HumidityZoneSize; return (hs * LS_S * TZ_S * HZ_S) + (ls * TZ_S * HZ_S) + (tz * HZ_S) + hz; } diff --git a/src/World/Generation/Generator.hpp b/src/World/Generation/Generator.hpp index c18de45..ee78dc5 100644 --- a/src/World/Generation/Generator.hpp +++ b/src/World/Generation/Generator.hpp @@ -1,48 +1,47 @@ #pragma once -#include - -#include "Decoration.hpp" +#include "../../Common/Sizes.hpp" #include "../Chunk.hpp" #include "../BiomeType.hpp" #include "../../Math/Perlin.hpp" #include "../../Math/Tensor.hpp" +#include "Decoration.hpp" namespace MC::World::Generation { class Generator { public: Generator() = default; - Chunk generate(int64_t chunk_x, int64_t chunk_y); + Chunk generate(I64 chunk_x, I64 chunk_y); private: template using Map2D = Matrix; template using Map3D = Tensor<3, V, Chunk::Width, Chunk::Height, Chunk::Width>; - Map2D generate_landmass_map(int64_t chunk_x, int64_t chunk_y); - Map2D generate_hill_map(int64_t chunk_x, int64_t chunk_y); - Map2D generate_height_map(Map2D& landmass_map, Map2D& hill_map); + Map2D generate_landmass_map(I64 chunk_x, I64 chunk_y); + Map2D generate_hill_map(I64 chunk_x, I64 chunk_y); + Map2D generate_height_map(Map2D& landmass_map, Map2D& hill_map); - Map2D generate_temperature_map(int64_t chunk_x, int64_t chunk_y); - Map2D generate_humidity_map(int64_t chunk_x, int64_t chunk_y); + Map2D generate_temperature_map(I64 chunk_x, I64 chunk_y); + Map2D generate_humidity_map(I64 chunk_x, I64 chunk_y); Map2D generate_biome_map( - Map2D& landmass_map, Map2D& hill_map, - Map2D& temperature_map, Map2D& humidity_map + Map2D& landmass_map, Map2D& hill_map, + Map2D& temperature_map, Map2D& humidity_map ); - Map3D generate_terrain(Map2D& height_map, int64_t chunk_x, int64_t chunk_y); + Map3D generate_terrain(Map2D& height_map, I64 chunk_x, I64 chunk_y); - void decorate_soil(Chunk& chunk, Map2D& biome_map, Map3D& terrain_map); + void decorate_soil(Chunk& chunk, Map2D& biome_map, Map3D& terrain_map); - [[nodiscard]] float get_landmass(Vector<2> pos) const; - [[nodiscard]] float get_hill(Vector<2> pos) const; + [[nodiscard]] Real get_landmass(Vector<2> pos) const; + [[nodiscard]] Real get_hill(Vector<2> pos) const; - [[nodiscard]] float get_humidity(Vector<2> pos) const; - [[nodiscard]] float get_temperature(Vector<2> pos) const; + [[nodiscard]] Real get_humidity(Vector<2> pos) const; + [[nodiscard]] Real get_temperature(Vector<2> pos) const; - [[nodiscard]] float get_density(Vector<3> pos) const; + [[nodiscard]] Real get_density(Vector<3> pos) const; - static Vector<2> chunk_position_to_world_vector(int64_t chunk_x, int64_t chunk_y, uint x, uint y); + static Vector<2> chunk_position_to_world_vector(I64 chunk_x, I64 chunk_y, UInt x, UInt y); static inline std::vector s_decorators = { new TreeDecorator(), @@ -60,14 +59,14 @@ private: enum class LandmassSlice { Land, Beach, Ocean }; enum class TemperatureZone { Hot, Fair, Cold }; enum class HumidityZone { Wet, Lush, Temperate, Dry }; - static constexpr uint HillSliceSize = (uint)HillSlice::Valley + 1; - static constexpr uint LandmassSliceSize = (uint)LandmassSlice::Ocean + 1; - static constexpr uint TemperatureZoneSize = (uint)TemperatureZone::Cold + 1; - static constexpr uint HumidityZoneSize = (uint)HumidityZone::Dry + 1; + static constexpr UInt HillSliceSize = (UInt)HillSlice::Valley + 1; + static constexpr UInt LandmassSliceSize = (UInt)LandmassSlice::Ocean + 1; + static constexpr UInt TemperatureZoneSize = (UInt)TemperatureZone::Cold + 1; + static constexpr UInt HumidityZoneSize = (UInt)HumidityZone::Dry + 1; - static constexpr size_t biome_lookup_table_size = (size_t)HillSliceSize * (size_t)LandmassSliceSize * (size_t)TemperatureZoneSize * (size_t)HumidityZoneSize; + static constexpr USize biome_lookup_table_size = (USize)HillSliceSize * (USize)LandmassSliceSize * (USize)TemperatureZoneSize * (USize)HumidityZoneSize; static std::array create_biome_lookup_table(); - static size_t biome_lookup_table_index(HillSlice hill_slice, LandmassSlice landmass_slice, TemperatureZone temperature_zone, HumidityZone humidity_zone); + static USize biome_lookup_table_index(HillSlice hill_slice, LandmassSlice landmass_slice, TemperatureZone temperature_zone, HumidityZone humidity_zone); static BiomeType lookup_biome(HillSlice hill_slice, LandmassSlice landmass_slice, TemperatureZone temperature_zone, HumidityZone humidity_zone); static inline std::array biome_lookup_table = create_biome_lookup_table(); }; diff --git a/src/World/World.cpp b/src/World/World.cpp index 7570d8c..a0b2b0d 100644 --- a/src/World/World.cpp +++ b/src/World/World.cpp @@ -36,8 +36,8 @@ std::vector World::get_visible_chunks(Vector<3> position) { } Chunk* World::get_chunk_for_position(Vector<3> position) { - int32_t x = std::round(position.x() / Chunk::Width); - int32_t y = std::round(position.z() / Chunk::Width); + I32 x = std::round(position.x() / Chunk::Width); + I32 y = std::round(position.z() / Chunk::Width); auto& data = get({x, y}); if (data.chunk.has_value()) { return &data.chunk.value(); @@ -56,15 +56,15 @@ void World::process_chunk_visibility_updates(const std::unordered_set World::get_visible_chunk_indices(const Vector<3> position) const { - int32_t center_x = std::round(position.x() / Chunk::Width); - int32_t center_y = std::round(position.z() / Chunk::Width); + I32 center_x = std::round(position.x() / Chunk::Width); + I32 center_y = std::round(position.z() / Chunk::Width); std::unordered_set indices{}; indices.reserve(m_view_distance_radius * m_view_distance_radius * 4); auto radius = m_view_distance_radius; - for (int32_t x = -radius; x <= radius; x++) { - int32_t height = std::round(std::sqrt(radius * radius - x * x) + 0.5); - for (int32_t y = -height; y <= height; y++) { + for (I32 x = -radius; x <= radius; x++) { + I32 height = std::round(std::sqrt(radius * radius - x * x) + 0.5); + for (I32 y = -height; y <= height; y++) { indices.emplace(x + center_x, y + center_y); } } @@ -85,7 +85,7 @@ std::unordered_set World::load_finished_chunks_from_queue() { return indices; } -void World::request_generation(ChunkIndex index, float priority) { +void World::request_generation(ChunkIndex index, Real priority) { m_queue.add(index, priority, [=]() -> GenerationResult { auto start = timestamp(); auto chunk = m_generator.generate(index.x, index.y); @@ -105,7 +105,7 @@ World::ChunkData& World::get(ChunkIndex index) { return entry->second; } -uint64_t World::timestamp() { +U64 World::timestamp() { auto time = std::chrono::system_clock::now().time_since_epoch(); auto ms = std::chrono::duration_cast(time); return ms.count(); @@ -135,12 +135,12 @@ void World::try_to_create_mesh_for_chunk(ChunkData& data) { data.status = ChunkStatus::Done; } -void World::log_chunk_time(uint64_t chunk_time_ms) { +void World::log_chunk_time(U64 chunk_time_ms) { m_statistics.chunk_time_sample_count++; - m_statistics.average_chunk_time_ms += ((float)chunk_time_ms - m_statistics.average_chunk_time_ms) / m_statistics.chunk_time_sample_count; + m_statistics.average_chunk_time_ms += ((Real)chunk_time_ms - m_statistics.average_chunk_time_ms) / m_statistics.chunk_time_sample_count; } -float World::get_average_chunk_time() const { +Real World::get_average_chunk_time() const { return m_statistics.average_chunk_time_ms; } diff --git a/src/World/World.hpp b/src/World/World.hpp index bc008b3..ae9f2a4 100644 --- a/src/World/World.hpp +++ b/src/World/World.hpp @@ -32,25 +32,25 @@ public: std::vector get_visible_chunks(Vector<3> position); Chunk* get_chunk_for_position(Vector<3> position); - float get_average_chunk_time() const; + Real get_average_chunk_time() const; private: std::unordered_set get_visible_chunk_indices(Vector<3> position) const; std::unordered_set load_finished_chunks_from_queue(); void process_chunk_visibility_updates(const std::unordered_set& new_chunks, Vector<3> player); - void request_generation(ChunkIndex index, float priority); + void request_generation(ChunkIndex index, Real priority); void try_to_create_mesh_for_chunk(ChunkData& data); - void log_chunk_time(uint64_t chunk_time_ms); + void log_chunk_time(U64 chunk_time_ms); ChunkData& get(ChunkIndex index); - static uint64_t timestamp(); + static U64 timestamp(); - uint8_t m_view_distance_radius = 13; + U8 m_view_distance_radius = 13; struct GenerationResult { Chunk chunk; - uint64_t generation_duration; + U64 generation_duration; }; Compute::Queue m_queue; Generation::Generator m_generator; @@ -59,8 +59,8 @@ private: std::unordered_set m_visible_chunks; struct Statistics { - uint chunk_time_sample_count; - float average_chunk_time_ms; + UInt chunk_time_sample_count; + Real average_chunk_time_ms; }; Statistics m_statistics{0, 0.0f}; }; -- cgit 1.4.1