aboutsummaryrefslogtreecommitdiff
path: root/src/shared/world/chunk.hh
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
commite4483eca01b48b943cd0461e24a74ae1a3139ed4 (patch)
treeed58c3c246e3af1af337697695d780aa31f6ad9a /src/shared/world/chunk.hh
parent1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (diff)
Update to most recent version (old initial commit)
Diffstat (limited to 'src/shared/world/chunk.hh')
-rw-r--r--src/shared/world/chunk.hh89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/shared/world/chunk.hh b/src/shared/world/chunk.hh
new file mode 100644
index 0000000..8e724a3
--- /dev/null
+++ b/src/shared/world/chunk.hh
@@ -0,0 +1,89 @@
+#ifndef SHARED_WORLD_CHUNK_HH_
+#define SHARED_WORLD_CHUNK_HH_
+
+#include <algorithm>
+#include <array>
+#include <exception>
+#include <functional>
+#include <istream>
+#include <memory>
+#include <random>
+#include <type_traits>
+#include <vector>
+
+#include <boost/functional/hash.hpp>
+#include <glm/glm.hpp>
+
+#include "shared/math/math.hh"
+#include "shared/net/proto.hh"
+#include "shared/shared.hh"
+#include "shared/world/block.hh"
+
+namespace shared {
+namespace world {
+
+class chunk {
+public:
+ static constexpr int WIDTH = 16;
+ static constexpr int HEIGHT = 256;
+ static constexpr int VOLUME = WIDTH * WIDTH * HEIGHT;
+
+ // Stuff for unordered_map.
+ static std::size_t hash(const shared::math::coords& c) noexcept {
+ std::size_t seed = 0;
+ boost::hash_combine(seed, boost::hash_value(c.x));
+ boost::hash_combine(seed, boost::hash_value(c.z));
+ return seed;
+ }
+ static std::size_t equal(const shared::math::coords& a,
+ const shared::math::coords& b) noexcept {
+ return a == b;
+ }
+ enum class biome { desert, islands, ocean, plains, forest, tundra, alpine };
+
+ using block_array_t = std::array<block, VOLUME>;
+ using blocks_t = std::unique_ptr<block_array_t>;
+ using biome_array_t = std::array<std::array<enum biome, WIDTH>, WIDTH>;
+ using biomes_t = std::unique_ptr<biome_array_t>;
+
+protected:
+ shared::math::coords pos;
+ blocks_t blocks; // Use get_block for 3d index access.
+
+ // Simple 2d array of enums where each column maps to the dominant biome
+ // at that x, z location. Call get_biome_str for string.
+ biomes_t biomes;
+
+public:
+ static bool is_outside_chunk(const glm::ivec3& v) noexcept;
+
+ static shared::math::coords
+ get_normalised_chunk(const shared::math::coords& coords, const int x,
+ const int z) noexcept;
+
+ static std::pair<unsigned short, unsigned short>
+ get_normalised_coords(const int x, const int z) noexcept;
+
+ static glm::ivec3 get_normalised_coords(const glm::ivec3& c) noexcept;
+
+public:
+ chunk(const std::uint64_t& seed,
+ const shared::math::coords& coords) noexcept;
+ chunk(const std::uint64_t& seed, const proto::chunk& chunk) noexcept;
+ virtual ~chunk() noexcept {};
+
+protected:
+ void pack(proto::chunk* const proto) const noexcept;
+
+public:
+ block& get_block(const glm::ivec3& v) noexcept;
+ const block& get_block(const glm::ivec3& v) const noexcept;
+ const shared::math::coords& get_pos() const noexcept { return this->pos; }
+
+ const char* get_biome(const int x, const int z) const noexcept;
+};
+
+} // namespace world
+} // namespace shared
+
+#endif