diff options
Diffstat (limited to 'src/shared/world/chunk.hh')
| -rw-r--r-- | src/shared/world/chunk.hh | 89 |
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 |
