From 68694a9c9d7d27d3b79c7b96bb67f56db2f75c45 Mon Sep 17 00:00:00 2001 From: untodesu Date: Thu, 11 Sep 2025 18:18:08 +0500 Subject: Metadata voxels! --- game/shared/world/chunk.cc | 25 ++-- game/shared/world/chunk.hh | 9 +- game/shared/world/dimension.cc | 30 +++-- game/shared/world/dimension.hh | 11 +- game/shared/world/feature.cc | 7 +- game/shared/world/feature.hh | 7 +- game/shared/world/ray_dda.cc | 2 +- game/shared/world/ray_dda.hh | 3 +- game/shared/world/voxel.cc | 120 ++++++++++++++++++ game/shared/world/voxel.hh | 236 ++++++++++++++++++++++++++++++++++++ game/shared/world/voxel_registry.cc | 194 +++++------------------------ game/shared/world/voxel_registry.hh | 137 ++------------------- game/shared/world/voxels/generic.hh | 27 +++++ 13 files changed, 481 insertions(+), 327 deletions(-) create mode 100644 game/shared/world/voxel.cc create mode 100644 game/shared/world/voxel.hh create mode 100644 game/shared/world/voxels/generic.hh (limited to 'game/shared/world') diff --git a/game/shared/world/chunk.cc b/game/shared/world/chunk.cc index e59b68d..b1b19a6 100644 --- a/game/shared/world/chunk.cc +++ b/game/shared/world/chunk.cc @@ -2,6 +2,8 @@ #include "shared/world/chunk.hh" +#include "shared/world/voxel_registry.hh" + #include "shared/coord.hh" world::Chunk::Chunk(entt::entity entity, Dimension* dimension) @@ -12,30 +14,35 @@ world::Chunk::Chunk(entt::entity entity, Dimension* dimension) m_biome = BIOME_VOID; } -voxel_id world::Chunk::get_voxel(const local_pos& lpos) const +const world::Voxel* world::Chunk::get_voxel(const local_pos& lpos) const { return get_voxel(coord::to_index(lpos)); } -voxel_id world::Chunk::get_voxel(const std::size_t index) const +const world::Voxel* world::Chunk::get_voxel(const std::size_t index) const { if(index >= CHUNK_VOLUME) { - return NULL_VOXEL_ID; - } - else { - return m_voxels[index]; + return nullptr; } + + return voxel_registry::find(m_voxels[index]); } -void world::Chunk::set_voxel(voxel_id voxel, const local_pos& lpos) +void world::Chunk::set_voxel(const Voxel* voxel, const local_pos& lpos) { set_voxel(voxel, coord::to_index(lpos)); } -void world::Chunk::set_voxel(voxel_id voxel, const std::size_t index) +void world::Chunk::set_voxel(const Voxel* voxel, const std::size_t index) { if(index < CHUNK_VOLUME) { - m_voxels[index] = voxel; + if(voxel == nullptr) { + m_voxels[index] = NULL_VOXEL_ID; + return; + } + + m_voxels[index] = voxel->get_id(); + return; } } diff --git a/game/shared/world/chunk.hh b/game/shared/world/chunk.hh index c5bba12..4a1e557 100644 --- a/game/shared/world/chunk.hh +++ b/game/shared/world/chunk.hh @@ -9,6 +9,7 @@ constexpr static unsigned int BIOME_VOID = 0U; namespace world { class Dimension; +class Voxel; } // namespace world namespace world @@ -18,11 +19,11 @@ public: explicit Chunk(entt::entity entity, Dimension* dimension); virtual ~Chunk(void) = default; - voxel_id get_voxel(const local_pos& lpos) const; - voxel_id get_voxel(const std::size_t index) const; + const Voxel* get_voxel(const local_pos& lpos) const; + const Voxel* get_voxel(const std::size_t index) const; - void set_voxel(voxel_id voxel, const local_pos& lpos); - void set_voxel(voxel_id voxel, const std::size_t index); + void set_voxel(const Voxel* voxel, const local_pos& lpos); + void set_voxel(const Voxel* voxel, const std::size_t index); const VoxelStorage& get_voxels(void) const; void set_voxels(const VoxelStorage& voxels); diff --git a/game/shared/world/dimension.cc b/game/shared/world/dimension.cc index dd28449..31a19af 100644 --- a/game/shared/world/dimension.cc +++ b/game/shared/world/dimension.cc @@ -3,6 +3,7 @@ #include "shared/world/dimension.hh" #include "shared/world/chunk.hh" +#include "shared/world/voxel_registry.hh" #include "shared/coord.hh" #include "shared/globals.hh" @@ -107,7 +108,7 @@ void world::Dimension::remove_chunk(Chunk* chunk) } } -voxel_id world::Dimension::get_voxel(const voxel_pos& vpos) const +const world::Voxel* world::Dimension::get_voxel(const voxel_pos& vpos) const { auto cpos = coord::to_chunk(vpos); auto lpos = coord::to_local(vpos); @@ -115,12 +116,11 @@ voxel_id world::Dimension::get_voxel(const voxel_pos& vpos) const if(auto chunk = find_chunk(cpos)) { return chunk->get_voxel(lpos); } - else { - return NULL_VOXEL_ID; - } + + return nullptr; } -voxel_id world::Dimension::get_voxel(const chunk_pos& cpos, const local_pos& lpos) const +const world::Voxel* world::Dimension::get_voxel(const chunk_pos& cpos, const local_pos& lpos) const { // This allows accessing get_voxel with negative // local coordinates that usually would result in an @@ -128,19 +128,33 @@ voxel_id world::Dimension::get_voxel(const chunk_pos& cpos, const local_pos& lpo return get_voxel(coord::to_voxel(cpos, lpos)); } -bool world::Dimension::set_voxel(voxel_id voxel, const voxel_pos& vpos) +bool world::Dimension::set_voxel(const Voxel* voxel, const voxel_pos& vpos) { auto cpos = coord::to_chunk(vpos); auto lpos = coord::to_local(vpos); if(auto chunk = find_chunk(cpos)) { + if(auto old_voxel = chunk->get_voxel(lpos)) { + if(old_voxel != voxel) { + // Notify the old voxel that it is + // being replaced with a different voxel + old_voxel->on_remove(this, vpos); + } + } + chunk->set_voxel(voxel, lpos); + if(voxel) { + // If we're not placing air, notify the + // new voxel that it has been placed + voxel->on_place(this, vpos); + } + VoxelSetEvent event; event.dimension = this; + event.voxel = voxel; event.cpos = cpos; event.lpos = lpos; - event.voxel = voxel; event.chunk = chunk; globals::dispatcher.trigger(event); @@ -151,7 +165,7 @@ bool world::Dimension::set_voxel(voxel_id voxel, const voxel_pos& vpos) return false; } -bool world::Dimension::set_voxel(voxel_id voxel, const chunk_pos& cpos, const local_pos& lpos) +bool world::Dimension::set_voxel(const Voxel* voxel, const chunk_pos& cpos, const local_pos& lpos) { // This allows accessing set_voxel with negative // local coordinates that usually would result in an diff --git a/game/shared/world/dimension.hh b/game/shared/world/dimension.hh index bf9bfe1..58e0765 100644 --- a/game/shared/world/dimension.hh +++ b/game/shared/world/dimension.hh @@ -11,6 +11,7 @@ class ConfigMap; namespace world { class Chunk; +class Voxel; class VoxelStorage; } // namespace world @@ -40,11 +41,11 @@ public: void remove_chunk(Chunk* chunk); public: - voxel_id get_voxel(const voxel_pos& vpos) const; - voxel_id get_voxel(const chunk_pos& cpos, const local_pos& lpos) const; + const Voxel* get_voxel(const voxel_pos& vpos) const; + const Voxel* get_voxel(const chunk_pos& cpos, const local_pos& lpos) const; - bool set_voxel(voxel_id voxel, const voxel_pos& vpos); - bool set_voxel(voxel_id voxel, const chunk_pos& cpos, const local_pos& lpos); + bool set_voxel(const Voxel* voxel, const voxel_pos& vpos); + bool set_voxel(const Voxel* voxel, const chunk_pos& cpos, const local_pos& lpos); public: virtual void init(io::ConfigMap& config); @@ -92,9 +93,9 @@ struct ChunkUpdateEvent final { struct VoxelSetEvent final { Dimension* dimension; + const Voxel* voxel; chunk_pos cpos; local_pos lpos; - voxel_id voxel; Chunk* chunk; }; } // namespace world diff --git a/game/shared/world/feature.cc b/game/shared/world/feature.cc index 4212043..cbf660e 100644 --- a/game/shared/world/feature.cc +++ b/game/shared/world/feature.cc @@ -4,7 +4,6 @@ #include "shared/world/chunk.hh" #include "shared/world/dimension.hh" -#include "shared/world/voxel_storage.hh" #include "shared/coord.hh" @@ -30,7 +29,7 @@ void world::Feature::place(const voxel_pos& vpos, Dimension* dimension) const } } -void world::Feature::place(const voxel_pos& vpos, const chunk_pos& cpos, VoxelStorage& voxels) const +void world::Feature::place(const voxel_pos& vpos, const chunk_pos& cpos, Chunk& chunk) const { for(const auto [rpos, voxel, overwrite] : (*this)) { auto it_vpos = vpos + rpos; @@ -40,14 +39,14 @@ void world::Feature::place(const voxel_pos& vpos, const chunk_pos& cpos, VoxelSt auto it_lpos = coord::to_local(it_vpos); auto it_index = coord::to_index(it_lpos); - if(voxels[it_index] && !overwrite) { + if(chunk.get_voxel(it_index) && !overwrite) { // There is something in the way // and the called intentionally requested // we do not force feature to overwrite voxels continue; } - voxels[it_index] = voxel; + chunk.set_voxel(voxel, it_index); } } } diff --git a/game/shared/world/feature.hh b/game/shared/world/feature.hh index 20a809b..09b913e 100644 --- a/game/shared/world/feature.hh +++ b/game/shared/world/feature.hh @@ -4,19 +4,20 @@ namespace world { +class Chunk; class Dimension; -class VoxelStorage; +class Voxel; } // namespace world namespace world { -class Feature final : public std::vector> { +class Feature final : public std::vector> { public: Feature(void) = default; virtual ~Feature(void) = default; public: void place(const voxel_pos& vpos, Dimension* dimension) const; - void place(const voxel_pos& vpos, const chunk_pos& cpos, VoxelStorage& voxels) const; + void place(const voxel_pos& vpos, const chunk_pos& cpos, Chunk& chunk) const; }; } // namespace world diff --git a/game/shared/world/ray_dda.cc b/game/shared/world/ray_dda.cc index b337e9d..7bb1068 100644 --- a/game/shared/world/ray_dda.cc +++ b/game/shared/world/ray_dda.cc @@ -71,7 +71,7 @@ void world::RayDDA::reset(const world::Dimension& dimension, const chunk_pos& st reset(&dimension, start_chunk, start_fpos, direction); } -voxel_id world::RayDDA::step(void) +const world::Voxel* world::RayDDA::step(void) { if(side_dist.x < side_dist.z) { if(side_dist.x < side_dist.y) { diff --git a/game/shared/world/ray_dda.hh b/game/shared/world/ray_dda.hh index 54bd48a..3071be2 100644 --- a/game/shared/world/ray_dda.hh +++ b/game/shared/world/ray_dda.hh @@ -7,6 +7,7 @@ namespace world { class Dimension; +class Voxel; } // namespace world namespace world @@ -20,7 +21,7 @@ public: void reset(const Dimension* dimension, const chunk_pos& start_chunk, const glm::fvec3& start_fpos, const glm::fvec3& direction); void reset(const Dimension& dimension, const chunk_pos& start_chunk, const glm::fvec3& start_fpos, const glm::fvec3& direction); - voxel_id step(void); + const Voxel* step(void); public: const Dimension* dimension; diff --git a/game/shared/world/voxel.cc b/game/shared/world/voxel.cc new file mode 100644 index 0000000..d51b5a1 --- /dev/null +++ b/game/shared/world/voxel.cc @@ -0,0 +1,120 @@ +#include "shared/pch.hh" + +#include "shared/world/voxel.hh" + +#include "core/math/crc64.hh" + +#include "shared/world/dimension.hh" + +void world::Voxel::on_place(Dimension* dimension, const voxel_pos& vpos) const +{ + // empty +} + +void world::Voxel::on_remove(Dimension* dimension, const voxel_pos& vpos) const +{ + // empty +} + +void world::Voxel::on_tick(Dimension* dimension, const voxel_pos& vpos) const +{ + // empty +} + +void world::Voxel::set_id(voxel_id id) noexcept +{ + m_id = id; +} + +std::size_t world::Voxel::get_random_texture_index(VoxelFace face, const voxel_pos& vpos) const +{ + const auto& textures = get_face_textures(face); + + assert(textures.size()); + + std::uint64_t hash = 0U; + hash = math::crc64(&vpos.x, sizeof(vpos.x), hash); + hash = math::crc64(&vpos.y, sizeof(vpos.y), hash); + hash = math::crc64(&vpos.z, sizeof(vpos.z), hash); + + return static_cast(hash % textures.size()); +} + +void world::Voxel::set_face_cache(VoxelFace face, std::size_t offset, std::size_t plane) +{ + assert(face < m_cached_face_offsets.size()); + assert(face < m_cached_face_planes.size()); + + m_cached_face_offsets[face] = offset; + m_cached_face_planes[face] = plane; +} + +std::uint64_t world::Voxel::calculate_checksum(std::uint64_t combine) const +{ + combine = math::crc64(m_name.data(), m_name.size(), combine); + combine += static_cast(m_shape); + combine += static_cast(m_render_mode); + return combine; +} + +std::shared_ptr world::Voxel::clone(void) const +{ + return std::make_shared(*this); +} + +void world::Voxel::set_name(std::string_view name) noexcept +{ + assert(name.size()); + + m_name = name; +} + +void world::Voxel::set_render_mode(VoxelRender mode) noexcept +{ + m_render_mode = mode; +} + +void world::Voxel::set_shape(VoxelShape shape) noexcept +{ + m_shape = shape; +} + +void world::Voxel::set_animated(bool animated) noexcept +{ + m_animated = animated; +} + +void world::Voxel::set_touch_type(VoxelTouch type) noexcept +{ + m_touch_type = type; +} + +void world::Voxel::set_touch_values(const glm::fvec3& values) noexcept +{ + m_touch_values = values; +} + +void world::Voxel::set_surface_material(VoxelMaterial material) noexcept +{ + m_surface_material = material; +} + +void world::Voxel::set_collision(const math::AABBf& box) noexcept +{ + m_collision = box; +} + +void world::Voxel::add_default_texture(std::string_view path) +{ + assert(path.size()); + + m_default_textures.emplace_back(path); +} + +void world::Voxel::add_face_texture(VoxelFace face, std::string_view path) +{ + assert(face < m_face_textures.size()); + assert(path.size()); + + m_face_textures[face].emplace_back(path); +} diff --git a/game/shared/world/voxel.hh b/game/shared/world/voxel.hh new file mode 100644 index 0000000..0bd83b7 --- /dev/null +++ b/game/shared/world/voxel.hh @@ -0,0 +1,236 @@ +#pragma once + +#include "core/math/aabb.hh" + +#include "shared/types.hh" + +namespace world +{ +class Dimension; +} // namespace world + +namespace world +{ +enum VoxelRender : unsigned short { + VRENDER_NONE = 0U, ///< The voxel is not rendered at all + VRENDER_OPAQUE, ///< The voxel is fully opaque + VRENDER_BLEND, ///< The voxel is blended (e.g. water, glass) +}; + +enum VoxelShape : unsigned short { + VSHAPE_CUBE = 0U, ///< Full cube shape + VSHAPE_CROSS, ///< TODO: Cross shape + VSHAPE_MODEL, ///< TODO: Custom model shape +}; + +enum VoxelFace : unsigned short { + VFACE_NORTH = 0U, ///< Positive Z face + VFACE_SOUTH, ///< Negative Z face + VFACE_EAST, ///< Positive X face + VFACE_WEST, ///< Negative X face + VFACE_TOP, ///< Positive Y face + VFACE_BOTTOM, ///< Negative Y face + VFACE_CROSS_NWSE, ///< Diagonal cross face northwest-southeast + VFACE_CROSS_NESW, ///< Diagonal cross face northeast-southwest + VFACE_COUNT +}; + +enum VoxelTouch : unsigned short { + VTOUCH_NONE = 0xFFFFU, + VTOUCH_SOLID = 0U, ///< The entity is stopped in its tracks + VTOUCH_BOUNCE, ///< The entity bounces back with some energy loss + VTOUCH_SINK, ///< The entity phases/sinks through the voxel +}; + +enum VoxelMaterial : unsigned short { + VMAT_DEFAULT = 0U, + VMAT_STONE, + VMAT_DIRT, + VMAT_GLASS, + VMAT_GRASS, + VMAT_GRAVEL, + VMAT_METAL, + VMAT_SAND, + VMAT_WOOD, + VMAT_SLOSH, + VMAT_COUNT +}; + +enum VoxelVisBits : unsigned short { + VVIS_NORTH = 1U << VFACE_NORTH, ///< Positive Z + VVIS_SOUTH = 1U << VFACE_SOUTH, ///< Negative Z + VVIS_EAST = 1U << VFACE_EAST, ///< Positive X + VVIS_WEST = 1U << VFACE_WEST, ///< Negative X + VVIS_UP = 1U << VFACE_TOP, ///< Positive Y + VVIS_DOWN = 1U << VFACE_BOTTOM, ///< Negative Y +}; +} // namespace world + +namespace world +{ +class Voxel { +public: + /// Called when the voxel is placed in the world + /// @param dimension The dimension the voxel is placed in + /// @param vpos The absolute voxel position where the voxel is placed + virtual void on_place(Dimension* dimension, const voxel_pos& vpos) const; + + /// Called when the voxel is removed from the world + /// @param dimension The dimension the voxel is removed from + /// @param vpos The absolute voxel position where the voxel is removed + virtual void on_remove(Dimension* dimension, const voxel_pos& vpos) const; + + /// Called when the voxel is ticked by something + /// @param dimension The dimension the voxel is ticked in + /// @param vpos The absolute voxel position where the voxel is ticked + virtual void on_tick(Dimension* dimension, const voxel_pos& vpos) const; + + constexpr std::string_view get_name(void) const noexcept; + constexpr voxel_id get_id(void) const noexcept; + void set_id(voxel_id id) noexcept; + + constexpr VoxelRender get_render_mode(void) const noexcept; + constexpr VoxelShape get_shape(void) const noexcept; + constexpr bool is_animated(void) const noexcept; + + constexpr VoxelTouch get_touch_type(void) const noexcept; + constexpr const glm::fvec3& get_touch_values(void) const noexcept; + constexpr VoxelMaterial get_surface_material(void) const noexcept; + + constexpr const math::AABBf& get_collision(void) const noexcept; + + constexpr const std::vector& get_default_textures(void) const noexcept; + constexpr const std::vector& get_face_textures(VoxelFace face) const noexcept; + constexpr std::size_t get_cached_face_offset(VoxelFace face) const noexcept; + constexpr std::size_t get_cached_face_plane(VoxelFace face) const noexcept; + + /// Non-model voxel shapes support texture variation based on the + /// voxel position on the world; this method handles the math behind this + /// @param face The face of the voxel to get the texture index for + /// @param vpos The absolute voxel position to get the texture index for + /// @return The index of the texture to use for the given face at the given position + /// @remarks On client-side: plane[get_cached_face_plane][get_cached_face_offset + thisFunctionResult] + std::size_t get_random_texture_index(VoxelFace face, const voxel_pos& vpos) const; + + /// Assign cached plane index and plane offset for a given face + /// @param face The face to assign the cache for + /// @param offset The offset to assign to the face + /// @param plane The plane index to assign to the face + void set_face_cache(VoxelFace face, std::size_t offset, std::size_t plane); + + /// Calculate a checksum for the voxel's properties + /// @param combine An optional initial checksum to combine with + /// @return The calculated checksum + std::uint64_t calculate_checksum(std::uint64_t combine = 0U) const; + + /// Produce a copy of itself as a shared pointer; the voxel registry + /// does not change the owner of a voxel instance it registers + virtual std::shared_ptr clone(void) const; + +protected: + void set_name(std::string_view name) noexcept; + + void set_render_mode(VoxelRender mode) noexcept; + void set_shape(VoxelShape shape) noexcept; + void set_animated(bool animated) noexcept; + + void set_touch_type(VoxelTouch type) noexcept; + void set_touch_values(const glm::fvec3& values) noexcept; + void set_surface_material(VoxelMaterial material) noexcept; + + void set_collision(const math::AABBf& box) noexcept; + + void add_default_texture(std::string_view path); + void add_face_texture(VoxelFace face, std::string_view path); + +private: + std::string m_name; + voxel_id m_id { NULL_VOXEL_ID }; + + VoxelRender m_render_mode { VRENDER_OPAQUE }; + VoxelShape m_shape { VSHAPE_CUBE }; + bool m_animated { false }; + + VoxelTouch m_touch_type { VTOUCH_SOLID }; + glm::fvec3 m_touch_values { 0.0f }; + VoxelMaterial m_surface_material { VMAT_DEFAULT }; + + math::AABBf m_collision { { 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f } }; + + std::vector m_default_textures; + std::array, VFACE_COUNT> m_face_textures; + std::array m_cached_face_offsets; + std::array m_cached_face_planes; +}; +} // namespace world + +constexpr std::string_view world::Voxel::get_name(void) const noexcept +{ + return m_name; +} + +constexpr voxel_id world::Voxel::get_id(void) const noexcept +{ + return m_id; +} + +constexpr world::VoxelRender world::Voxel::get_render_mode(void) const noexcept +{ + return m_render_mode; +} + +constexpr world::VoxelShape world::Voxel::get_shape(void) const noexcept +{ + return m_shape; +} + +constexpr bool world::Voxel::is_animated(void) const noexcept +{ + return m_animated; +} + +constexpr world::VoxelTouch world::Voxel::get_touch_type(void) const noexcept +{ + return m_touch_type; +} + +constexpr const glm::fvec3& world::Voxel::get_touch_values(void) const noexcept +{ + return m_touch_values; +} + +constexpr world::VoxelMaterial world::Voxel::get_surface_material(void) const noexcept +{ + return m_surface_material; +} + +constexpr const math::AABBf& world::Voxel::get_collision(void) const noexcept +{ + return m_collision; +} + +constexpr const std::vector& world::Voxel::get_default_textures(void) const noexcept +{ + return m_default_textures; +} + +constexpr const std::vector& world::Voxel::get_face_textures(VoxelFace face) const noexcept +{ + assert(face <= m_face_textures.size()); + + return m_face_textures[face]; +} + +constexpr std::size_t world::Voxel::get_cached_face_offset(VoxelFace face) const noexcept +{ + assert(face <= m_cached_face_offsets.size()); + + return m_cached_face_offsets[face]; +} + +constexpr std::size_t world::Voxel::get_cached_face_plane(VoxelFace face) const noexcept +{ + assert(face <= m_cached_face_planes.size()); + + return m_cached_face_planes[face]; +} diff --git a/game/shared/world/voxel_registry.cc b/game/shared/world/voxel_registry.cc index b44845e..4c2f360 100644 --- a/game/shared/world/voxel_registry.cc +++ b/game/shared/world/voxel_registry.cc @@ -2,195 +2,65 @@ #include "shared/world/voxel_registry.hh" -#include "core/math/crc64.hh" +static std::uint64_t registry_checksum = 0U; +emhash8::HashMap world::voxel_registry::names; +std::vector> world::voxel_registry::voxels; -std::unordered_map world::voxel_registry::builders = {}; -std::unordered_map world::voxel_registry::names = {}; -std::vector> world::voxel_registry::voxels = {}; - -world::VoxelInfoBuilder::VoxelInfoBuilder(std::string_view name, voxel_type type, bool animated, bool blending) -{ - prototype.name = name; - prototype.type = type; - prototype.animated = animated; - prototype.blending = blending; - - switch(type) { - case voxel_type::CUBE: - prototype.textures.resize(static_cast(voxel_face::CUBE__NR)); - break; - case voxel_type::CROSS: - prototype.textures.resize(static_cast(voxel_face::CROSS__NR)); - break; - case voxel_type::MODEL: - // Custom models should use a different texture - // resource management that is not a voxel atlas - // TODO: actually implement custom models lol - prototype.textures.resize(0); - break; - default: - // Something really bad should happen if we end up here. - // The outside code would static_cast an int to VoxelType - // and possibly fuck a lot of things up to cause this - spdlog::critical("voxel_registry: {}: unknown voxel type {}", name, static_cast(type)); - std::terminate(); - } - - // Physics properties - prototype.touch_type = voxel_touch::SOLID; - prototype.touch_values = glm::fvec3(0.0f, 0.0f, 0.0f); - prototype.surface = voxel_surface::DEFAULT; - - // Things set in future by item_def - prototype.item_pick = NULL_ITEM_ID; -} - -world::VoxelInfoBuilder& world::VoxelInfoBuilder::add_texture_default(std::string_view texture) +static void recalculate_checksum(void) { - default_texture.paths.push_back(std::string(texture)); - return *this; -} + registry_checksum = 0U; -world::VoxelInfoBuilder& world::VoxelInfoBuilder::add_texture(voxel_face face, std::string_view texture) -{ - const auto index = static_cast(face); - prototype.textures[index].paths.push_back(std::string(texture)); - return *this; -} - -world::VoxelInfoBuilder& world::VoxelInfoBuilder::set_touch(voxel_touch type, const glm::fvec3& values) -{ - prototype.touch_type = type; - prototype.touch_values = values; - return *this; + for(const auto& voxel : world::voxel_registry::voxels) { + registry_checksum = voxel->calculate_checksum(registry_checksum); + } } -world::VoxelInfoBuilder& world::VoxelInfoBuilder::set_surface(voxel_surface surface) +world::Voxel* world::voxel_registry::register_voxel(const Voxel& voxel_template) { - prototype.surface = surface; - return *this; -} + assert(voxel_template.get_name().size()); + assert(nullptr == find(voxel_template.get_name())); -voxel_id world::VoxelInfoBuilder::build(void) const -{ - const auto it = world::voxel_registry::names.find(prototype.name); + const auto id = static_cast(voxels.size()); - if(it != world::voxel_registry::names.cend()) { - spdlog::warn("voxel_registry: cannot build {}: name already present", prototype.name); - return it->second; - } + auto voxel = voxel_template.clone(); + voxel->set_id(id); - std::size_t state_count; - - switch(prototype.type) { - case voxel_type::CUBE: - case voxel_type::CROSS: - case voxel_type::MODEL: - state_count = 1; - break; - default: - // Something really bad should happen if we end up here. - // The outside code would static_cast an int to VoxelType - // and possibly fuck a lot of things up to cause this - spdlog::critical("voxel_registry: {}: unknown voxel type {}", prototype.name, static_cast(prototype.type)); - std::terminate(); - } + names.emplace(std::string(voxel_template.get_name()), id); + voxels.push_back(std::move(voxel)); - if((world::voxel_registry::voxels.size() + state_count) >= MAX_VOXEL_ID) { - spdlog::critical("voxel_registry: voxel registry overflow"); - std::terminate(); - } + recalculate_checksum(); - auto new_info = std::make_shared(); - new_info->name = prototype.name; - new_info->type = prototype.type; - new_info->animated = prototype.animated; - new_info->blending = prototype.blending; - - new_info->textures.resize(prototype.textures.size()); - - for(std::size_t i = 0; i < prototype.textures.size(); ++i) { - if(prototype.textures[i].paths.empty()) { - new_info->textures[i].paths = default_texture.paths; - new_info->textures[i].cached_offset = SIZE_MAX; - new_info->textures[i].cached_plane = SIZE_MAX; - } - else { - new_info->textures[i].paths = prototype.textures[i].paths; - new_info->textures[i].cached_offset = SIZE_MAX; - new_info->textures[i].cached_plane = SIZE_MAX; - } - } - - // Physics properties - new_info->touch_type = prototype.touch_type; - new_info->touch_values = prototype.touch_values; - new_info->surface = prototype.surface; - - // Things set in future by item_def - new_info->item_pick = prototype.item_pick; - - // Base voxel identifier offset - new_info->base_voxel = world::voxel_registry::voxels.size() + 1; - - for(std::size_t i = 0; i < state_count; ++i) - world::voxel_registry::voxels.push_back(new_info); - world::voxel_registry::names.insert_or_assign(new_info->name, new_info->base_voxel); - - return new_info->base_voxel; -} - -world::VoxelInfoBuilder& world::voxel_registry::construct(std::string_view name, voxel_type type, bool animated, bool blending) -{ - const auto it = world::voxel_registry::builders.find(std::string(name)); - - if(it != world::voxel_registry::builders.cend()) { - return it->second; - } - else { - return world::voxel_registry::builders.emplace(std::string(name), VoxelInfoBuilder(name, type, animated, blending)).first->second; - } + return voxels.back().get(); } -world::VoxelInfo* world::voxel_registry::find(std::string_view name) +world::Voxel* world::voxel_registry::find(std::string_view name) { - const auto it = world::voxel_registry::names.find(std::string(name)); + const auto it = names.find(name); - if(it != world::voxel_registry::names.cend()) { - return world::voxel_registry::find(it->second); - } - else { + if(it == names.end()) { return nullptr; } + + return voxels[it->second].get(); } -world::VoxelInfo* world::voxel_registry::find(const voxel_id voxel) +world::Voxel* world::voxel_registry::find(voxel_id id) { - if((voxel != NULL_VOXEL_ID) && (voxel <= world::voxel_registry::voxels.size())) { - return world::voxel_registry::voxels[voxel - 1].get(); - } - else { + if(id >= voxels.size()) { return nullptr; } + + return voxels[id].get(); } void world::voxel_registry::purge(void) { - world::voxel_registry::builders.clear(); - world::voxel_registry::names.clear(); - world::voxel_registry::voxels.clear(); + registry_checksum = 0U; + voxels.clear(); + names.clear(); } -std::uint64_t world::voxel_registry::calculate_checksum(void) +std::uint64_t world::voxel_registry::get_checksum(void) { - std::uint64_t result = 0; - - for(const std::shared_ptr& info : world::voxel_registry::voxels) { - result = math::crc64(info->name, result); - result += static_cast(info->type); - result += static_cast(info->base_voxel); - result += info->blending ? 256 : 1; - } - - return result; + return registry_checksum; } diff --git a/game/shared/world/voxel_registry.hh b/game/shared/world/voxel_registry.hh index a75f59f..b9d9a34 100644 --- a/game/shared/world/voxel_registry.hh +++ b/game/shared/world/voxel_registry.hh @@ -1,141 +1,18 @@ #pragma once -#include "shared/types.hh" - -namespace world -{ -enum class voxel_face : unsigned short { - CUBE_NORTH = 0x0000, - CUBE_SOUTH = 0x0001, - CUBE_EAST = 0x0002, - CUBE_WEST = 0x0003, - CUBE_TOP = 0x0004, - CUBE_BOTTOM = 0x0005, - CUBE__NR = 0x0006, - - CROSS_NESW = 0x0000, - CROSS_NWSE = 0x0001, - CROSS__NR = 0x0002, -}; - -enum class voxel_type : unsigned short { - CUBE = 0x0000, - CROSS = 0x0001, // TODO - MODEL = 0x0002, // TODO -}; - -enum class voxel_facing : unsigned short { - NORTH = 0x0000, - SOUTH = 0x0001, - EAST = 0x0002, - WEST = 0x0003, - UP = 0x0004, - DOWN = 0x0005, - NESW = 0x0006, - NWSE = 0x0007, -}; - -enum class voxel_touch : unsigned short { - SOLID = 0x0000, // The entity is stopped in its tracks - BOUNCE = 0x0001, // The entity bounces back with some energy loss - SINK = 0x0002, // The entity phases/sinks through the voxel - NOTHING = 0xFFFF, -}; - -enum class voxel_surface : unsigned short { - DEFAULT = 0x0000, - STONE = 0x0001, - DIRT = 0x0002, - GLASS = 0x0003, - GRASS = 0x0004, - GRAVEL = 0x0005, - METAL = 0x0006, - SAND = 0x0007, - WOOD = 0x0008, - SLOSH = 0x0009, - COUNT = 0x000A, - UNKNOWN = 0xFFFF, -}; - -using voxel_vis = unsigned short; -constexpr static voxel_vis VIS_NORTH = 1 << static_cast(voxel_facing::NORTH); -constexpr static voxel_vis VIS_SOUTH = 1 << static_cast(voxel_facing::SOUTH); -constexpr static voxel_vis VIS_EAST = 1 << static_cast(voxel_facing::EAST); -constexpr static voxel_vis VIS_WEST = 1 << static_cast(voxel_facing::WEST); -constexpr static voxel_vis VIS_UP = 1 << static_cast(voxel_facing::UP); -constexpr static voxel_vis VIS_DOWN = 1 << static_cast(voxel_facing::DOWN); -} // namespace world - -namespace world -{ -struct VoxelTexture final { - std::vector paths; - std::size_t cached_offset; // client-side only - std::size_t cached_plane; // client-side only -}; - -struct VoxelInfo final { - std::string name; - voxel_type type; - bool animated; - bool blending; - - std::vector textures; - - // Physics properties go here - // TODO: player_move friction modifiers - // that would make the voxel very sticky or - // very slippery to walk on - voxel_touch touch_type; - glm::fvec3 touch_values; - voxel_surface surface; - - // Some voxel types might occupy multiple voxel_id - // values that reference to the exact same VoxelInfo - // structure; the actual numeric state is figured out by - // subtracting base_voxel from the checking voxel_id - voxel_id base_voxel; - - // These will be set by item_registry - // and by default set to NULL_ITEM_ID - item_id item_pick; -}; -} // namespace world - -namespace world -{ -class VoxelInfoBuilder final { -public: - explicit VoxelInfoBuilder(std::string_view name, voxel_type type, bool animated, bool blending); - virtual ~VoxelInfoBuilder(void) = default; - -public: - VoxelInfoBuilder& add_texture_default(std::string_view texture); - VoxelInfoBuilder& add_texture(voxel_face face, std::string_view texture); - VoxelInfoBuilder& set_touch(voxel_touch type, const glm::fvec3& values); - VoxelInfoBuilder& set_surface(voxel_surface surface); - -public: - voxel_id build(void) const; - -private: - VoxelTexture default_texture; - VoxelInfo prototype; -}; -} // namespace world +#include "shared/world/voxel.hh" namespace world::voxel_registry { -extern std::unordered_map builders; -extern std::unordered_map names; -extern std::vector> voxels; +extern emhash8::HashMap names; +extern std::vector> voxels; } // namespace world::voxel_registry namespace world::voxel_registry { -VoxelInfoBuilder& construct(std::string_view name, voxel_type type, bool animated, bool blending); -VoxelInfo* find(std::string_view name); -VoxelInfo* find(const voxel_id voxel); +Voxel* register_voxel(const Voxel& voxel_template); +Voxel* find(std::string_view name); +Voxel* find(voxel_id id); } // namespace world::voxel_registry namespace world::voxel_registry @@ -145,5 +22,5 @@ void purge(void); namespace world::voxel_registry { -std::uint64_t calculate_checksum(void); +std::uint64_t get_checksum(void); } // namespace world::voxel_registry diff --git a/game/shared/world/voxels/generic.hh b/game/shared/world/voxels/generic.hh new file mode 100644 index 0000000..a661792 --- /dev/null +++ b/game/shared/world/voxels/generic.hh @@ -0,0 +1,27 @@ +#pragma once + +#include "shared/world/voxel.hh" + +namespace world::voxels +{ +class GenericCube final : public Voxel { +public: + template + requires(std::is_convertible_v && ...) + explicit GenericCube(std::string_view name, VoxelRender render_mode, bool animated, VoxelMaterial surface_material, VoxelTouch touch, + const glm::fvec3& touch_values, TexturesT&&... textures) noexcept + { + set_name(name); + + set_shape(VoxelShape::CUBE); + set_render_mode(render_mode); + set_animated(animated); + + set_surface_material(surface_material); + set_touch_values(touch_values); + set_touch_type(touch); + + add_texture_default(std::forward(textures)...); + } +}; +} // namespace world::voxels -- cgit From e9076f22fe2a49d1cd8933e54b7b00c5dd943269 Mon Sep 17 00:00:00 2001 From: untodesu Date: Fri, 12 Sep 2025 13:33:52 +0500 Subject: It compiles --- game/shared/world/CMakeLists.txt | 4 +- game/shared/world/feature.cc | 28 ++++++++ game/shared/world/feature.hh | 2 + game/shared/world/item_registry.cc | 9 ++- game/shared/world/item_registry.hh | 9 ++- game/shared/world/voxel.cc | 95 ++++++++++++++++++++------ game/shared/world/voxel.hh | 130 +++++++++++++++++++++++++----------- game/shared/world/voxel_registry.cc | 22 +++--- game/shared/world/voxel_registry.hh | 4 +- game/shared/world/voxels/generic.hh | 27 -------- 10 files changed, 223 insertions(+), 107 deletions(-) delete mode 100644 game/shared/world/voxels/generic.hh (limited to 'game/shared/world') diff --git a/game/shared/world/CMakeLists.txt b/game/shared/world/CMakeLists.txt index 15d5b59..cc7ed90 100644 --- a/game/shared/world/CMakeLists.txt +++ b/game/shared/world/CMakeLists.txt @@ -13,4 +13,6 @@ target_sources(shared PRIVATE "${CMAKE_CURRENT_LIST_DIR}/voxel_registry.cc" "${CMAKE_CURRENT_LIST_DIR}/voxel_registry.hh" "${CMAKE_CURRENT_LIST_DIR}/voxel_storage.cc" - "${CMAKE_CURRENT_LIST_DIR}/voxel_storage.hh") + "${CMAKE_CURRENT_LIST_DIR}/voxel_storage.hh" + "${CMAKE_CURRENT_LIST_DIR}/voxel.cc" + "${CMAKE_CURRENT_LIST_DIR}/voxel.hh") diff --git a/game/shared/world/feature.cc b/game/shared/world/feature.cc index cbf660e..e704ced 100644 --- a/game/shared/world/feature.cc +++ b/game/shared/world/feature.cc @@ -4,6 +4,7 @@ #include "shared/world/chunk.hh" #include "shared/world/dimension.hh" +#include "shared/world/voxel.hh" #include "shared/coord.hh" @@ -50,3 +51,30 @@ void world::Feature::place(const voxel_pos& vpos, const chunk_pos& cpos, Chunk& } } } + +void world::Feature::place(const voxel_pos& vpos, const chunk_pos& cpos, VoxelStorage& voxels) const +{ + for(const auto [rpos, voxel, overwrite] : (*this)) { + auto it_vpos = vpos + rpos; + auto it_cpos = coord::to_chunk(it_vpos); + + if(it_cpos == cpos) { + auto it_lpos = coord::to_local(it_vpos); + auto it_index = coord::to_index(it_lpos); + + if(voxels[it_index] && !overwrite) { + // There is something in the way + // and the called intentionally requested + // we do not force feature to overwrite voxels + continue; + } + + if(voxel == nullptr) { + voxels[it_index] = NULL_VOXEL_ID; + } + else { + voxels[it_index] = voxel->get_id(); + } + } + } +} diff --git a/game/shared/world/feature.hh b/game/shared/world/feature.hh index 09b913e..a543632 100644 --- a/game/shared/world/feature.hh +++ b/game/shared/world/feature.hh @@ -7,6 +7,7 @@ namespace world class Chunk; class Dimension; class Voxel; +class VoxelStorage; } // namespace world namespace world @@ -19,5 +20,6 @@ public: public: void place(const voxel_pos& vpos, Dimension* dimension) const; void place(const voxel_pos& vpos, const chunk_pos& cpos, Chunk& chunk) const; + void place(const voxel_pos& vpos, const chunk_pos& cpos, VoxelStorage& voxels) const; }; } // namespace world diff --git a/game/shared/world/item_registry.cc b/game/shared/world/item_registry.cc index d1b9ff4..783c85e 100644 --- a/game/shared/world/item_registry.cc +++ b/game/shared/world/item_registry.cc @@ -14,7 +14,7 @@ world::ItemInfoBuilder::ItemInfoBuilder(std::string_view name) { prototype.name = name; prototype.texture = std::string(); - prototype.place_voxel = NULL_VOXEL_ID; + prototype.place_voxel = nullptr; prototype.cached_texture = nullptr; } @@ -25,7 +25,7 @@ world::ItemInfoBuilder& world::ItemInfoBuilder::set_texture(std::string_view tex return *this; } -world::ItemInfoBuilder& world::ItemInfoBuilder::set_place_voxel(voxel_id place_voxel) +world::ItemInfoBuilder& world::ItemInfoBuilder::set_place_voxel(const Voxel* place_voxel) { prototype.place_voxel = place_voxel; return *this; @@ -99,7 +99,10 @@ std::uint64_t world::item_registry::calculate_checksum(void) for(const auto& info : world::item_registry::items) { result = math::crc64(info->name, result); - result += static_cast(info->place_voxel); + + if(info->place_voxel) { + result += static_cast(info->place_voxel->get_id()); + } } return result; diff --git a/game/shared/world/item_registry.hh b/game/shared/world/item_registry.hh index c3e6cf9..2274da2 100644 --- a/game/shared/world/item_registry.hh +++ b/game/shared/world/item_registry.hh @@ -9,12 +9,17 @@ // anywhere else in the shared and server code struct TextureGUI; +namespace world +{ +class Voxel; +} // namespace world + namespace world { struct ItemInfo final { std::string name; std::string texture; - voxel_id place_voxel; + const Voxel* place_voxel; resource_ptr cached_texture; // Client-side only }; @@ -29,7 +34,7 @@ public: public: ItemInfoBuilder& set_texture(std::string_view texture); - ItemInfoBuilder& set_place_voxel(voxel_id place_voxel); + ItemInfoBuilder& set_place_voxel(const Voxel* place_voxel); public: item_id build(void) const; diff --git a/game/shared/world/voxel.cc b/game/shared/world/voxel.cc index d51b5a1..1cd1504 100644 --- a/game/shared/world/voxel.cc +++ b/game/shared/world/voxel.cc @@ -6,24 +6,30 @@ #include "shared/world/dimension.hh" -void world::Voxel::on_place(Dimension* dimension, const voxel_pos& vpos) const +world::Voxel::Voxel(const Voxel& source, voxel_id id) noexcept : Voxel(source) { - // empty + m_id = id; } -void world::Voxel::on_remove(Dimension* dimension, const voxel_pos& vpos) const +void world::Voxel::on_place(Dimension* dimension, const voxel_pos& vpos) const { - // empty + if(m_on_place) { + m_on_place(dimension, vpos); + } } -void world::Voxel::on_tick(Dimension* dimension, const voxel_pos& vpos) const +void world::Voxel::on_remove(Dimension* dimension, const voxel_pos& vpos) const { - // empty + if(m_on_remove) { + m_on_remove(dimension, vpos); + } } -void world::Voxel::set_id(voxel_id id) noexcept +void world::Voxel::on_tick(Dimension* dimension, const voxel_pos& vpos) const { - m_id = id; + if(m_on_tick) { + m_on_tick(dimension, vpos); + } } std::size_t world::Voxel::get_random_texture_index(VoxelFace face, const voxel_pos& vpos) const @@ -57,64 +63,113 @@ std::uint64_t world::Voxel::calculate_checksum(std::uint64_t combine) const return combine; } -std::shared_ptr world::Voxel::clone(void) const +world::VoxelBuilder::VoxelBuilder(std::string_view name) +{ + set_name(name); +} + +world::VoxelBuilder& world::VoxelBuilder::set_on_place(VoxelOnPlaceFunc func) noexcept +{ + m_on_place = std::move(func); + + return *this; +} + +world::VoxelBuilder& world::VoxelBuilder::set_on_remove(VoxelOnRemoveFunc func) noexcept +{ + m_on_remove = std::move(func); + + return *this; +} + +world::VoxelBuilder& world::VoxelBuilder::set_on_tick(VoxelOnTickFunc func) noexcept { - return std::make_shared(*this); + m_on_tick = std::move(func); + + return *this; } -void world::Voxel::set_name(std::string_view name) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_name(std::string_view name) noexcept { assert(name.size()); m_name = name; + + return *this; } -void world::Voxel::set_render_mode(VoxelRender mode) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_render_mode(VoxelRender mode) noexcept { m_render_mode = mode; + + return *this; } -void world::Voxel::set_shape(VoxelShape shape) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_shape(VoxelShape shape) noexcept { m_shape = shape; + + return *this; } -void world::Voxel::set_animated(bool animated) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_animated(bool animated) noexcept { m_animated = animated; + + return *this; } -void world::Voxel::set_touch_type(VoxelTouch type) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_touch_type(VoxelTouch type) noexcept { m_touch_type = type; + + return *this; } -void world::Voxel::set_touch_values(const glm::fvec3& values) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_touch_values(const glm::fvec3& values) noexcept { m_touch_values = values; + + return *this; } -void world::Voxel::set_surface_material(VoxelMaterial material) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_surface_material(VoxelMaterial material) noexcept { m_surface_material = material; + + return *this; } -void world::Voxel::set_collision(const math::AABBf& box) noexcept +world::VoxelBuilder& world::VoxelBuilder::set_collision(const math::AABBf& box) noexcept { m_collision = box; + + return *this; } -void world::Voxel::add_default_texture(std::string_view path) +world::VoxelBuilder& world::VoxelBuilder::add_default_texture(std::string_view path) { assert(path.size()); m_default_textures.emplace_back(path); + + return *this; } -void world::Voxel::add_face_texture(VoxelFace face, std::string_view path) +world::VoxelBuilder& world::VoxelBuilder::add_face_texture(VoxelFace face, std::string_view path) { assert(face < m_face_textures.size()); assert(path.size()); m_face_textures[face].emplace_back(path); + + return *this; +} + +std::unique_ptr world::VoxelBuilder::build(voxel_id id) const +{ + assert(m_name.size()); + assert(id); + + return std::make_unique(*this, id); } diff --git a/game/shared/world/voxel.hh b/game/shared/world/voxel.hh index 0bd83b7..9cc0e4c 100644 --- a/game/shared/world/voxel.hh +++ b/game/shared/world/voxel.hh @@ -11,19 +11,19 @@ class Dimension; namespace world { -enum VoxelRender : unsigned short { +enum VoxelRender : unsigned int { VRENDER_NONE = 0U, ///< The voxel is not rendered at all VRENDER_OPAQUE, ///< The voxel is fully opaque VRENDER_BLEND, ///< The voxel is blended (e.g. water, glass) }; -enum VoxelShape : unsigned short { +enum VoxelShape : unsigned int { VSHAPE_CUBE = 0U, ///< Full cube shape VSHAPE_CROSS, ///< TODO: Cross shape VSHAPE_MODEL, ///< TODO: Custom model shape }; -enum VoxelFace : unsigned short { +enum VoxelFace : unsigned int { VFACE_NORTH = 0U, ///< Positive Z face VFACE_SOUTH, ///< Negative Z face VFACE_EAST, ///< Positive X face @@ -35,14 +35,15 @@ enum VoxelFace : unsigned short { VFACE_COUNT }; -enum VoxelTouch : unsigned short { +enum VoxelTouch : unsigned int { VTOUCH_NONE = 0xFFFFU, VTOUCH_SOLID = 0U, ///< The entity is stopped in its tracks VTOUCH_BOUNCE, ///< The entity bounces back with some energy loss VTOUCH_SINK, ///< The entity phases/sinks through the voxel }; -enum VoxelMaterial : unsigned short { +enum VoxelMaterial : unsigned int { + VMAT_UNKNOWN = 0xFFFFU, VMAT_DEFAULT = 0U, VMAT_STONE, VMAT_DIRT, @@ -56,7 +57,7 @@ enum VoxelMaterial : unsigned short { VMAT_COUNT }; -enum VoxelVisBits : unsigned short { +enum VoxelVisBits : unsigned int { VVIS_NORTH = 1U << VFACE_NORTH, ///< Positive Z VVIS_SOUTH = 1U << VFACE_SOUTH, ///< Negative Z VVIS_EAST = 1U << VFACE_EAST, ///< Positive X @@ -66,28 +67,26 @@ enum VoxelVisBits : unsigned short { }; } // namespace world +namespace world +{ +using VoxelOnPlaceFunc = std::function; +using VoxelOnRemoveFunc = std::function; +using VoxelOnTickFunc = std::function; +} // namespace world + namespace world { class Voxel { public: - /// Called when the voxel is placed in the world - /// @param dimension The dimension the voxel is placed in - /// @param vpos The absolute voxel position where the voxel is placed - virtual void on_place(Dimension* dimension, const voxel_pos& vpos) const; - - /// Called when the voxel is removed from the world - /// @param dimension The dimension the voxel is removed from - /// @param vpos The absolute voxel position where the voxel is removed - virtual void on_remove(Dimension* dimension, const voxel_pos& vpos) const; + Voxel(void) = default; + explicit Voxel(const Voxel& source, voxel_id id) noexcept; - /// Called when the voxel is ticked by something - /// @param dimension The dimension the voxel is ticked in - /// @param vpos The absolute voxel position where the voxel is ticked - virtual void on_tick(Dimension* dimension, const voxel_pos& vpos) const; + void on_place(Dimension* dimension, const voxel_pos& vpos) const; + void on_remove(Dimension* dimension, const voxel_pos& vpos) const; + void on_tick(Dimension* dimension, const voxel_pos& vpos) const; constexpr std::string_view get_name(void) const noexcept; constexpr voxel_id get_id(void) const noexcept; - void set_id(voxel_id id) noexcept; constexpr VoxelRender get_render_mode(void) const noexcept; constexpr VoxelShape get_shape(void) const noexcept; @@ -104,6 +103,15 @@ public: constexpr std::size_t get_cached_face_offset(VoxelFace face) const noexcept; constexpr std::size_t get_cached_face_plane(VoxelFace face) const noexcept; + template + constexpr bool is_render_mode(void) const noexcept; + template + constexpr bool is_shape(void) const noexcept; + template + constexpr bool is_touch_type(void) const noexcept; + template + constexpr bool is_surface_material(void) const noexcept; + /// Non-model voxel shapes support texture variation based on the /// voxel position on the world; this method handles the math behind this /// @param face The face of the voxel to get the texture index for @@ -123,27 +131,7 @@ public: /// @return The calculated checksum std::uint64_t calculate_checksum(std::uint64_t combine = 0U) const; - /// Produce a copy of itself as a shared pointer; the voxel registry - /// does not change the owner of a voxel instance it registers - virtual std::shared_ptr clone(void) const; - protected: - void set_name(std::string_view name) noexcept; - - void set_render_mode(VoxelRender mode) noexcept; - void set_shape(VoxelShape shape) noexcept; - void set_animated(bool animated) noexcept; - - void set_touch_type(VoxelTouch type) noexcept; - void set_touch_values(const glm::fvec3& values) noexcept; - void set_surface_material(VoxelMaterial material) noexcept; - - void set_collision(const math::AABBf& box) noexcept; - - void add_default_texture(std::string_view path); - void add_face_texture(VoxelFace face, std::string_view path); - -private: std::string m_name; voxel_id m_id { NULL_VOXEL_ID }; @@ -161,6 +149,40 @@ private: std::array, VFACE_COUNT> m_face_textures; std::array m_cached_face_offsets; std::array m_cached_face_planes; + + VoxelOnPlaceFunc m_on_place; + VoxelOnRemoveFunc m_on_remove; + VoxelOnTickFunc m_on_tick; +}; +} // namespace world + +namespace world +{ +class VoxelBuilder final : public Voxel { +public: + VoxelBuilder(void) = default; + explicit VoxelBuilder(std::string_view name); + + VoxelBuilder& set_on_place(VoxelOnPlaceFunc func) noexcept; + VoxelBuilder& set_on_remove(VoxelOnRemoveFunc func) noexcept; + VoxelBuilder& set_on_tick(VoxelOnTickFunc func) noexcept; + + VoxelBuilder& set_name(std::string_view name) noexcept; + + VoxelBuilder& set_render_mode(VoxelRender mode) noexcept; + VoxelBuilder& set_shape(VoxelShape shape) noexcept; + VoxelBuilder& set_animated(bool animated) noexcept; + + VoxelBuilder& set_touch_type(VoxelTouch type) noexcept; + VoxelBuilder& set_touch_values(const glm::fvec3& values) noexcept; + VoxelBuilder& set_surface_material(VoxelMaterial material) noexcept; + + VoxelBuilder& set_collision(const math::AABBf& box) noexcept; + + VoxelBuilder& add_default_texture(std::string_view path); + VoxelBuilder& add_face_texture(VoxelFace face, std::string_view path); + + std::unique_ptr build(voxel_id id) const; }; } // namespace world @@ -218,6 +240,10 @@ constexpr const std::vector& world::Voxel::get_face_textures(VoxelF { assert(face <= m_face_textures.size()); + if(m_face_textures[face].empty()) { + return m_default_textures; + } + return m_face_textures[face]; } @@ -234,3 +260,27 @@ constexpr std::size_t world::Voxel::get_cached_face_plane(VoxelFace face) const return m_cached_face_planes[face]; } + +template +constexpr bool world::Voxel::is_render_mode(void) const noexcept +{ + return m_render_mode == RenderMode; +} + +template +constexpr bool world::Voxel::is_shape(void) const noexcept +{ + return m_shape == Shape; +} + +template +constexpr bool world::Voxel::is_touch_type(void) const noexcept +{ + return m_touch_type == TouchType; +} + +template +constexpr bool world::Voxel::is_surface_material(void) const noexcept +{ + return m_surface_material == Material; +} diff --git a/game/shared/world/voxel_registry.cc b/game/shared/world/voxel_registry.cc index 4c2f360..97485e6 100644 --- a/game/shared/world/voxel_registry.cc +++ b/game/shared/world/voxel_registry.cc @@ -4,7 +4,7 @@ static std::uint64_t registry_checksum = 0U; emhash8::HashMap world::voxel_registry::names; -std::vector> world::voxel_registry::voxels; +std::vector> world::voxel_registry::voxels; static void recalculate_checksum(void) { @@ -15,17 +15,15 @@ static void recalculate_checksum(void) } } -world::Voxel* world::voxel_registry::register_voxel(const Voxel& voxel_template) +world::Voxel* world::voxel_registry::register_voxel(const VoxelBuilder& builder) { - assert(voxel_template.get_name().size()); - assert(nullptr == find(voxel_template.get_name())); + assert(builder.get_name().size()); + assert(nullptr == find(builder.get_name())); - const auto id = static_cast(voxels.size()); + const auto id = static_cast(1 + voxels.size()); - auto voxel = voxel_template.clone(); - voxel->set_id(id); - - names.emplace(std::string(voxel_template.get_name()), id); + std::unique_ptr voxel(builder.build(id)); + names.emplace(std::string(builder.get_name()), id); voxels.push_back(std::move(voxel)); recalculate_checksum(); @@ -35,7 +33,7 @@ world::Voxel* world::voxel_registry::register_voxel(const Voxel& voxel_template) world::Voxel* world::voxel_registry::find(std::string_view name) { - const auto it = names.find(name); + const auto it = names.find(std::string(name)); if(it == names.end()) { return nullptr; @@ -46,11 +44,11 @@ world::Voxel* world::voxel_registry::find(std::string_view name) world::Voxel* world::voxel_registry::find(voxel_id id) { - if(id >= voxels.size()) { + if(id == NULL_VOXEL_ID || id > voxels.size()) { return nullptr; } - return voxels[id].get(); + return voxels[id - 1].get(); } void world::voxel_registry::purge(void) diff --git a/game/shared/world/voxel_registry.hh b/game/shared/world/voxel_registry.hh index b9d9a34..5ba2aed 100644 --- a/game/shared/world/voxel_registry.hh +++ b/game/shared/world/voxel_registry.hh @@ -5,12 +5,12 @@ namespace world::voxel_registry { extern emhash8::HashMap names; -extern std::vector> voxels; +extern std::vector> voxels; } // namespace world::voxel_registry namespace world::voxel_registry { -Voxel* register_voxel(const Voxel& voxel_template); +Voxel* register_voxel(const VoxelBuilder& builder); Voxel* find(std::string_view name); Voxel* find(voxel_id id); } // namespace world::voxel_registry diff --git a/game/shared/world/voxels/generic.hh b/game/shared/world/voxels/generic.hh deleted file mode 100644 index a661792..0000000 --- a/game/shared/world/voxels/generic.hh +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "shared/world/voxel.hh" - -namespace world::voxels -{ -class GenericCube final : public Voxel { -public: - template - requires(std::is_convertible_v && ...) - explicit GenericCube(std::string_view name, VoxelRender render_mode, bool animated, VoxelMaterial surface_material, VoxelTouch touch, - const glm::fvec3& touch_values, TexturesT&&... textures) noexcept - { - set_name(name); - - set_shape(VoxelShape::CUBE); - set_render_mode(render_mode); - set_animated(animated); - - set_surface_material(surface_material); - set_touch_values(touch_values); - set_touch_type(touch); - - add_texture_default(std::forward(textures)...); - } -}; -} // namespace world::voxels -- cgit From 73cbcdd6e8c849e32abbf9757e603e6a6654e870 Mon Sep 17 00:00:00 2001 From: untodesu Date: Fri, 12 Sep 2025 14:09:34 +0500 Subject: Metaitems --- game/shared/world/CMakeLists.txt | 2 + game/shared/world/chunk.cc | 7 +-- game/shared/world/feature.cc | 7 +-- game/shared/world/item.cc | 55 ++++++++++++++++++++ game/shared/world/item.hh | 84 ++++++++++++++++++++++++++++++ game/shared/world/item_registry.cc | 101 +++++++++++------------------------- game/shared/world/item_registry.hh | 55 +++----------------- game/shared/world/voxel.cc | 54 +++++-------------- game/shared/world/voxel.hh | 28 +++++----- game/shared/world/voxel_registry.cc | 4 +- 10 files changed, 209 insertions(+), 188 deletions(-) create mode 100644 game/shared/world/item.cc create mode 100644 game/shared/world/item.hh (limited to 'game/shared/world') diff --git a/game/shared/world/CMakeLists.txt b/game/shared/world/CMakeLists.txt index cc7ed90..db3f370 100644 --- a/game/shared/world/CMakeLists.txt +++ b/game/shared/world/CMakeLists.txt @@ -8,6 +8,8 @@ target_sources(shared PRIVATE "${CMAKE_CURRENT_LIST_DIR}/feature.hh" "${CMAKE_CURRENT_LIST_DIR}/item_registry.cc" "${CMAKE_CURRENT_LIST_DIR}/item_registry.hh" + "${CMAKE_CURRENT_LIST_DIR}/item.cc" + "${CMAKE_CURRENT_LIST_DIR}/item.hh" "${CMAKE_CURRENT_LIST_DIR}/ray_dda.cc" "${CMAKE_CURRENT_LIST_DIR}/ray_dda.hh" "${CMAKE_CURRENT_LIST_DIR}/voxel_registry.cc" diff --git a/game/shared/world/chunk.cc b/game/shared/world/chunk.cc index b1b19a6..f8f7b93 100644 --- a/game/shared/world/chunk.cc +++ b/game/shared/world/chunk.cc @@ -36,12 +36,7 @@ void world::Chunk::set_voxel(const Voxel* voxel, const local_pos& lpos) void world::Chunk::set_voxel(const Voxel* voxel, const std::size_t index) { if(index < CHUNK_VOLUME) { - if(voxel == nullptr) { - m_voxels[index] = NULL_VOXEL_ID; - return; - } - - m_voxels[index] = voxel->get_id(); + m_voxels[index] = voxel ? voxel->get_id() : NULL_VOXEL_ID; return; } } diff --git a/game/shared/world/feature.cc b/game/shared/world/feature.cc index e704ced..8fe95f1 100644 --- a/game/shared/world/feature.cc +++ b/game/shared/world/feature.cc @@ -69,12 +69,7 @@ void world::Feature::place(const voxel_pos& vpos, const chunk_pos& cpos, VoxelSt continue; } - if(voxel == nullptr) { - voxels[it_index] = NULL_VOXEL_ID; - } - else { - voxels[it_index] = voxel->get_id(); - } + voxels[it_index] = voxel ? voxel->get_id() : NULL_VOXEL_ID; } } } diff --git a/game/shared/world/item.cc b/game/shared/world/item.cc new file mode 100644 index 0000000..5e60609 --- /dev/null +++ b/game/shared/world/item.cc @@ -0,0 +1,55 @@ +#include "shared/pch.hh" + +#include "shared/world/item.hh" + +#include "core/math/crc64.hh" + +#include "shared/world/voxel.hh" + +world::Item::Item(const Item& source, item_id id) noexcept : Item(source) +{ + m_id = id; +} + +void world::Item::set_cached_texture(resource_ptr texture) const noexcept +{ + m_cached_texture = std::move(texture); +} + +std::uint64_t world::Item::get_checksum(std::uint64_t combine) const +{ + combine = math::crc64(m_name.data(), m_name.size(), combine); + combine = math::crc64(m_texture.data(), m_texture.size(), combine); + + std::uint32_t id = m_place_voxel ? m_place_voxel->get_id() : NULL_VOXEL_ID; + combine = math::crc64(&id, sizeof(id), combine); + + return combine; +} + +world::ItemBuilder::ItemBuilder(std::string_view name) +{ + set_name(name); +} + +void world::ItemBuilder::set_name(std::string_view name) +{ + assert(name.size()); + + m_name = name; +} + +void world::ItemBuilder::set_texture(std::string_view texture) +{ + m_texture = texture; +} + +void world::ItemBuilder::set_place_voxel(const Voxel* place_voxel) +{ + m_place_voxel = place_voxel; +} + +std::unique_ptr world::ItemBuilder::build(item_id id) const +{ + return std::make_unique(*this, id); +} diff --git a/game/shared/world/item.hh b/game/shared/world/item.hh new file mode 100644 index 0000000..ffa7f5c --- /dev/null +++ b/game/shared/world/item.hh @@ -0,0 +1,84 @@ +#pragma once + +#include "core/resource/resource.hh" + +#include "shared/types.hh" + +// This resource is only defined client-side and +// resource_ptr should remain set to null +// anywhere else in the shared and server code +struct TextureGUI; + +namespace world +{ +class Voxel; +} // namespace world + +namespace world +{ +class Item { +public: + Item(void) = default; + explicit Item(const Item& source, item_id id) noexcept; + + constexpr std::string_view get_name(void) const noexcept; + constexpr item_id get_id(void) const noexcept; + + constexpr std::string_view get_texture(void) const noexcept; + constexpr const Voxel* get_place_voxel(void) const noexcept; + + constexpr resource_ptr& get_cached_texture(void) const noexcept; + void set_cached_texture(resource_ptr texture) const noexcept; + + std::uint64_t get_checksum(std::uint64_t combine = 0U) const; + +protected: + std::string m_name; + item_id m_id { NULL_ITEM_ID }; + + std::string m_texture; + const Voxel* m_place_voxel { nullptr }; + + mutable resource_ptr m_cached_texture; // Client-side only +}; +} // namespace world + +namespace world +{ +class ItemBuilder final : public Item { +public: + explicit ItemBuilder(std::string_view name); + + void set_name(std::string_view name); + + void set_texture(std::string_view texture); + void set_place_voxel(const Voxel* place_voxel); + + std::unique_ptr build(item_id id) const; +}; +} // namespace world + +constexpr std::string_view world::Item::get_name(void) const noexcept +{ + return m_name; +} + +constexpr item_id world::Item::get_id(void) const noexcept +{ + return m_id; +} + +constexpr std::string_view world::Item::get_texture(void) const noexcept +{ + return m_texture; +} + +constexpr const world::Voxel* world::Item::get_place_voxel(void) const noexcept +{ + return m_place_voxel; +} + +constexpr resource_ptr& world::Item::get_cached_texture(void) const noexcept +{ + return m_cached_texture; +} diff --git a/game/shared/world/item_registry.cc b/game/shared/world/item_registry.cc index 783c85e..4e0932c 100644 --- a/game/shared/world/item_registry.cc +++ b/game/shared/world/item_registry.cc @@ -6,104 +6,63 @@ #include "shared/world/voxel_registry.hh" -std::unordered_map world::item_registry::builders = {}; +static std::uint64_t registry_checksum = 0U; std::unordered_map world::item_registry::names = {}; -std::vector> world::item_registry::items = {}; +std::vector> world::item_registry::items = {}; -world::ItemInfoBuilder::ItemInfoBuilder(std::string_view name) +static void recalculate_checksum(void) { - prototype.name = name; - prototype.texture = std::string(); - prototype.place_voxel = nullptr; - prototype.cached_texture = nullptr; -} + registry_checksum = 0U; -world::ItemInfoBuilder& world::ItemInfoBuilder::set_texture(std::string_view texture) -{ - prototype.texture = texture; - prototype.cached_texture = nullptr; - return *this; + for(const auto& item : world::item_registry::items) { + registry_checksum = item->get_checksum(registry_checksum); + } } -world::ItemInfoBuilder& world::ItemInfoBuilder::set_place_voxel(const Voxel* place_voxel) +world::Item* world::item_registry::register_item(const ItemBuilder& builder) { - prototype.place_voxel = place_voxel; - return *this; -} + assert(builder.get_name().size()); + assert(nullptr == find(builder.get_name())); -item_id world::ItemInfoBuilder::build(void) const -{ - const auto it = world::item_registry::names.find(prototype.name); + const auto id = static_cast(1 + items.size()); - if(it != world::item_registry::names.cend()) { - spdlog::warn("item_registry: cannot build {}: name already present", prototype.name); - return it->second; - } + std::unique_ptr item(builder.build(id)); + names.emplace(std::string(builder.get_name()), id); + items.push_back(std::move(item)); - auto new_info = std::make_shared(); - new_info->name = prototype.name; - new_info->texture = prototype.texture; - new_info->place_voxel = prototype.place_voxel; - new_info->cached_texture = nullptr; + recalculate_checksum(); - world::item_registry::items.push_back(new_info); - world::item_registry::names.insert_or_assign(prototype.name, static_cast(world::item_registry::items.size())); - - return static_cast(world::item_registry::items.size()); + return items.back().get(); } -world::ItemInfoBuilder& world::item_registry::construct(std::string_view name) +world::Item* world::item_registry::find(std::string_view name) { - const auto it = world::item_registry::builders.find(std::string(name)); - - if(it != world::item_registry::builders.cend()) { - return it->second; - } - else { - return world::item_registry::builders.emplace(std::string(name), ItemInfoBuilder(name)).first->second; - } -} + const auto it = names.find(std::string(name)); -world::ItemInfo* world::item_registry::find(std::string_view name) -{ - const auto it = world::item_registry::names.find(std::string(name)); - - if(it != world::item_registry::names.cend()) { - return world::item_registry::find(it->second); - } - else { + if(it == names.end()) { return nullptr; } + + return items[it->second - 1].get(); } -world::ItemInfo* world::item_registry::find(const item_id item) +world::Item* world::item_registry::find(const item_id item) { - if((item != NULL_ITEM_ID) && (item <= world::item_registry::items.size())) { - return world::item_registry::items[item - 1].get(); - } - else { + if(item == NULL_ITEM_ID || item > items.size()) { return nullptr; } + + return items[item - 1].get(); } void world::item_registry::purge(void) { - world::item_registry::builders.clear(); - world::item_registry::names.clear(); - world::item_registry::items.clear(); + registry_checksum = 0U; + items.clear(); + names.clear(); } -std::uint64_t world::item_registry::calculate_checksum(void) +std::uint64_t world::item_registry::get_checksum(void) { - std::uint64_t result = 0; - - for(const auto& info : world::item_registry::items) { - result = math::crc64(info->name, result); - - if(info->place_voxel) { - result += static_cast(info->place_voxel->get_id()); - } - } - - return result; + return registry_checksum; } diff --git a/game/shared/world/item_registry.hh b/game/shared/world/item_registry.hh index 2274da2..b4c9fda 100644 --- a/game/shared/world/item_registry.hh +++ b/game/shared/world/item_registry.hh @@ -1,61 +1,18 @@ #pragma once -#include "core/resource/resource.hh" - -#include "shared/types.hh" - -// This resource is only defined client-side and -// resource_ptr should remain set to null -// anywhere else in the shared and server code -struct TextureGUI; - -namespace world -{ -class Voxel; -} // namespace world - -namespace world -{ -struct ItemInfo final { - std::string name; - std::string texture; - const Voxel* place_voxel; - - resource_ptr cached_texture; // Client-side only -}; -} // namespace world - -namespace world -{ -class ItemInfoBuilder final { -public: - explicit ItemInfoBuilder(std::string_view name); - virtual ~ItemInfoBuilder(void) = default; - -public: - ItemInfoBuilder& set_texture(std::string_view texture); - ItemInfoBuilder& set_place_voxel(const Voxel* place_voxel); - -public: - item_id build(void) const; - -private: - ItemInfo prototype; -}; -} // namespace world +#include "shared/world/item.hh" namespace world::item_registry { -extern std::unordered_map builders; extern std::unordered_map names; -extern std::vector> items; +extern std::vector> items; } // namespace world::item_registry namespace world::item_registry { -ItemInfoBuilder& construct(std::string_view name); -ItemInfo* find(std::string_view name); -ItemInfo* find(const item_id item); +Item* register_item(const ItemBuilder& builder); +Item* find(std::string_view name); +Item* find(const item_id item); } // namespace world::item_registry namespace world::item_registry @@ -65,5 +22,5 @@ void purge(void); namespace world::item_registry { -std::uint64_t calculate_checksum(void); +std::uint64_t get_checksum(void); } // namespace world::item_registry diff --git a/game/shared/world/voxel.cc b/game/shared/world/voxel.cc index 1cd1504..21fe62c 100644 --- a/game/shared/world/voxel.cc +++ b/game/shared/world/voxel.cc @@ -55,7 +55,7 @@ void world::Voxel::set_face_cache(VoxelFace face, std::size_t offset, std::size_ m_cached_face_planes[face] = plane; } -std::uint64_t world::Voxel::calculate_checksum(std::uint64_t combine) const +std::uint64_t world::Voxel::get_checksum(std::uint64_t combine) const { combine = math::crc64(m_name.data(), m_name.size(), combine); combine += static_cast(m_shape); @@ -68,102 +68,76 @@ world::VoxelBuilder::VoxelBuilder(std::string_view name) set_name(name); } -world::VoxelBuilder& world::VoxelBuilder::set_on_place(VoxelOnPlaceFunc func) noexcept +void world::VoxelBuilder::set_on_place(VoxelOnPlaceFunc func) noexcept { m_on_place = std::move(func); - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_on_remove(VoxelOnRemoveFunc func) noexcept +void world::VoxelBuilder::set_on_remove(VoxelOnRemoveFunc func) noexcept { m_on_remove = std::move(func); - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_on_tick(VoxelOnTickFunc func) noexcept +void world::VoxelBuilder::set_on_tick(VoxelOnTickFunc func) noexcept { m_on_tick = std::move(func); - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_name(std::string_view name) noexcept +void world::VoxelBuilder::set_name(std::string_view name) noexcept { assert(name.size()); m_name = name; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_render_mode(VoxelRender mode) noexcept +void world::VoxelBuilder::set_render_mode(VoxelRender mode) noexcept { m_render_mode = mode; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_shape(VoxelShape shape) noexcept +void world::VoxelBuilder::set_shape(VoxelShape shape) noexcept { m_shape = shape; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_animated(bool animated) noexcept +void world::VoxelBuilder::set_animated(bool animated) noexcept { m_animated = animated; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_touch_type(VoxelTouch type) noexcept +void world::VoxelBuilder::set_touch_type(VoxelTouch type) noexcept { m_touch_type = type; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_touch_values(const glm::fvec3& values) noexcept +void world::VoxelBuilder::set_touch_values(const glm::fvec3& values) noexcept { m_touch_values = values; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_surface_material(VoxelMaterial material) noexcept +void world::VoxelBuilder::set_surface_material(VoxelMaterial material) noexcept { m_surface_material = material; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::set_collision(const math::AABBf& box) noexcept +void world::VoxelBuilder::set_collision(const math::AABBf& box) noexcept { m_collision = box; - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::add_default_texture(std::string_view path) +void world::VoxelBuilder::add_default_texture(std::string_view path) { assert(path.size()); m_default_textures.emplace_back(path); - - return *this; } -world::VoxelBuilder& world::VoxelBuilder::add_face_texture(VoxelFace face, std::string_view path) +void world::VoxelBuilder::add_face_texture(VoxelFace face, std::string_view path) { assert(face < m_face_textures.size()); assert(path.size()); m_face_textures[face].emplace_back(path); - - return *this; } std::unique_ptr world::VoxelBuilder::build(voxel_id id) const diff --git a/game/shared/world/voxel.hh b/game/shared/world/voxel.hh index 9cc0e4c..6013962 100644 --- a/game/shared/world/voxel.hh +++ b/game/shared/world/voxel.hh @@ -129,7 +129,7 @@ public: /// Calculate a checksum for the voxel's properties /// @param combine An optional initial checksum to combine with /// @return The calculated checksum - std::uint64_t calculate_checksum(std::uint64_t combine = 0U) const; + std::uint64_t get_checksum(std::uint64_t combine = 0U) const; protected: std::string m_name; @@ -163,24 +163,24 @@ public: VoxelBuilder(void) = default; explicit VoxelBuilder(std::string_view name); - VoxelBuilder& set_on_place(VoxelOnPlaceFunc func) noexcept; - VoxelBuilder& set_on_remove(VoxelOnRemoveFunc func) noexcept; - VoxelBuilder& set_on_tick(VoxelOnTickFunc func) noexcept; + void set_on_place(VoxelOnPlaceFunc func) noexcept; + void set_on_remove(VoxelOnRemoveFunc func) noexcept; + void set_on_tick(VoxelOnTickFunc func) noexcept; - VoxelBuilder& set_name(std::string_view name) noexcept; + void set_name(std::string_view name) noexcept; - VoxelBuilder& set_render_mode(VoxelRender mode) noexcept; - VoxelBuilder& set_shape(VoxelShape shape) noexcept; - VoxelBuilder& set_animated(bool animated) noexcept; + void set_render_mode(VoxelRender mode) noexcept; + void set_shape(VoxelShape shape) noexcept; + void set_animated(bool animated) noexcept; - VoxelBuilder& set_touch_type(VoxelTouch type) noexcept; - VoxelBuilder& set_touch_values(const glm::fvec3& values) noexcept; - VoxelBuilder& set_surface_material(VoxelMaterial material) noexcept; + void set_touch_type(VoxelTouch type) noexcept; + void set_touch_values(const glm::fvec3& values) noexcept; + void set_surface_material(VoxelMaterial material) noexcept; - VoxelBuilder& set_collision(const math::AABBf& box) noexcept; + void set_collision(const math::AABBf& box) noexcept; - VoxelBuilder& add_default_texture(std::string_view path); - VoxelBuilder& add_face_texture(VoxelFace face, std::string_view path); + void add_default_texture(std::string_view path); + void add_face_texture(VoxelFace face, std::string_view path); std::unique_ptr build(voxel_id id) const; }; diff --git a/game/shared/world/voxel_registry.cc b/game/shared/world/voxel_registry.cc index 97485e6..573a606 100644 --- a/game/shared/world/voxel_registry.cc +++ b/game/shared/world/voxel_registry.cc @@ -11,7 +11,7 @@ static void recalculate_checksum(void) registry_checksum = 0U; for(const auto& voxel : world::voxel_registry::voxels) { - registry_checksum = voxel->calculate_checksum(registry_checksum); + registry_checksum = voxel->get_checksum(registry_checksum); } } @@ -39,7 +39,7 @@ world::Voxel* world::voxel_registry::find(std::string_view name) return nullptr; } - return voxels[it->second].get(); + return voxels[it->second - 1].get(); } world::Voxel* world::voxel_registry::find(voxel_id id) -- cgit From f210a86c1406ccc6dfd6f14181dd7a1274ee0de4 Mon Sep 17 00:00:00 2001 From: untodesu Date: Fri, 12 Sep 2025 15:09:01 +0500 Subject: Random ticking? In my game?! Hell yeah! --- game/shared/world/chunk_aabb.hh | 4 ---- game/shared/world/ray_dda.hh | 4 ---- 2 files changed, 8 deletions(-) (limited to 'game/shared/world') diff --git a/game/shared/world/chunk_aabb.hh b/game/shared/world/chunk_aabb.hh index 3a2d26f..d926b55 100644 --- a/game/shared/world/chunk_aabb.hh +++ b/game/shared/world/chunk_aabb.hh @@ -1,5 +1,3 @@ -#ifndef SHARED_CHUNK_AABB -#define SHARED_CHUNK_AABB 1 #pragma once #include "core/math/aabb.hh" @@ -10,5 +8,3 @@ namespace world { using ChunkAABB = math::AABB; } // namespace world - -#endif // SHARED_CHUNK_AABB diff --git a/game/shared/world/ray_dda.hh b/game/shared/world/ray_dda.hh index 3071be2..110e3d4 100644 --- a/game/shared/world/ray_dda.hh +++ b/game/shared/world/ray_dda.hh @@ -1,5 +1,3 @@ -#ifndef SHARED_RAY_DDA -#define SHARED_RAY_DDA 1 #pragma once #include "shared/types.hh" @@ -38,5 +36,3 @@ public: voxel_pos vpos; }; } // namespace world - -#endif // SHARED_RAY_DDA -- cgit