From 31fd5bbc08234686cf798a93a18e0bb73615d1bf Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 1 Sep 2023 22:27:30 +0200 Subject: rename entity -> object --- src/character.cpp | 16 +-- src/character.hpp | 14 +-- src/chunk-collision.cpp | 12 +- src/chunk-scenery.cpp | 30 ++--- src/chunk-scenery.hpp | 10 +- src/chunk.cpp | 44 +++---- src/chunk.hpp | 32 +++--- src/entity-type.hpp | 10 -- src/entity.cpp | 300 ------------------------------------------------ src/entity.hpp | 89 -------------- src/light.cpp | 8 +- src/light.hpp | 12 +- src/object-type.hpp | 10 ++ src/object.cpp | 300 ++++++++++++++++++++++++++++++++++++++++++++++++ src/object.hpp | 89 ++++++++++++++ src/scenery.cpp | 12 +- src/scenery.hpp | 14 +-- src/world.cpp | 54 ++++----- src/world.hpp | 54 ++++----- 19 files changed, 555 insertions(+), 555 deletions(-) delete mode 100644 src/entity-type.hpp delete mode 100644 src/entity.cpp delete mode 100644 src/entity.hpp create mode 100644 src/object-type.hpp create mode 100644 src/object.cpp create mode 100644 src/object.hpp (limited to 'src') diff --git a/src/character.cpp b/src/character.cpp index b05d8093..3e252e7a 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -2,7 +2,7 @@ #include "src/anim-atlas.hpp" #include "loader/loader.hpp" #include "src/world.hpp" -#include "src/entity.hpp" +#include "src/object.hpp" #include "shaders/shader.hpp" #include "src/RTree-search.hpp" #include "compat/exception.hpp" @@ -91,16 +91,16 @@ character_proto& character_proto::operator=(const character_proto&) = default; character_proto::character_proto() { - type = entity_type::character; + type = object_type::character; atlas = loader.anim_atlas("npc-walk", loader.ANIM_PATH); } -bool character_proto::operator==(const entity_proto& e0) const +bool character_proto::operator==(const object_proto& e0) const { if (type != e0.type) return false; - if (!entity_proto::operator==(e0)) + if (!object_proto::operator==(e0)) return false; const auto& s0 = static_cast(e0); @@ -195,26 +195,26 @@ done: } } -entity_type character::type() const noexcept { return entity_type::character; } +object_type character::type() const noexcept { return object_type::character; } character::operator character_proto() const { character_proto ret; - static_cast(ret) = entity::operator entity_proto(); + static_cast(ret) = object::operator object_proto(); ret.name = name; ret.playable = playable; return ret; } character::character(object_id id, struct chunk& c, const character_proto& proto) : - entity{id, c, proto}, + object{id, c, proto}, name{proto.name}, playable{proto.playable} { if (!name) name = "(Unnamed)"_s; fm_soft_assert(atlas->check_rotation(r)); - entity::set_bbox_(offset, bbox_offset, Vector2ub(iTILE_SIZE2/2), pass); + object::set_bbox_(offset, bbox_offset, Vector2ub(iTILE_SIZE2/2), pass); } } // namespace floormat diff --git a/src/character.hpp b/src/character.hpp index 06833f75..2c7543a5 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -1,7 +1,7 @@ #pragma once #include "src/global-coords.hpp" #include "src/rotation.hpp" -#include "src/entity.hpp" +#include "src/object.hpp" #include namespace floormat { @@ -9,7 +9,7 @@ namespace floormat { struct anim_atlas; struct world; -struct character_proto : entity_proto +struct character_proto : object_proto { String name; bool playable : 1 = false; @@ -18,12 +18,12 @@ struct character_proto : entity_proto character_proto(const character_proto&); ~character_proto() noexcept override; character_proto& operator=(const character_proto&); - bool operator==(const entity_proto& proto) const override; + bool operator==(const object_proto& proto) const override; }; -struct character final : entity +struct character final : object { - entity_type type() const noexcept override; + object_type type() const noexcept override; explicit operator character_proto() const; void update(size_t i, float dt) override; @@ -44,7 +44,7 @@ private: character(object_id id, struct chunk& c, const character_proto& proto); }; -template<> struct entity_type_ : std::integral_constant {}; -template<> struct entity_type_ : std::integral_constant {}; +template<> struct object_type_ : std::integral_constant {}; +template<> struct object_type_ : std::integral_constant {}; } // namespace floormat diff --git a/src/chunk-collision.cpp b/src/chunk-collision.cpp index 9810cee3..db7f3ede 100644 --- a/src/chunk-collision.cpp +++ b/src/chunk-collision.cpp @@ -1,6 +1,6 @@ #include "chunk.hpp" #include "tile-atlas.hpp" -#include "entity.hpp" +#include "object.hpp" #include "src/RTree-search.hpp" #include "src/chunk-scenery.hpp" #include "src/tile-bbox.hpp" @@ -22,7 +22,7 @@ constexpr object_id make_id(collision_type type, pass_mode p, object_id id) void chunk::ensure_passability() noexcept { - fm_assert(_entities_sorted); // not strictly necessary + fm_assert(_objects_sorted); // not strictly necessary if (!_pass_modified) return; @@ -30,7 +30,7 @@ void chunk::ensure_passability() noexcept _rtree.RemoveAll(); - for (const std::shared_ptr& s : entities()) + for (const std::shared_ptr& s : objects()) { bbox box; if (_bbox_for_scenery(*s, box)) @@ -64,7 +64,7 @@ void chunk::ensure_passability() noexcept } } -bool chunk::_bbox_for_scenery(const entity& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept +bool chunk::_bbox_for_scenery(const object& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept { auto [start, end] = scenery_tile(local, offset, bbox_offset, bbox_size); auto id = make_id(collision_type::scenery, s.pass, s.id); @@ -72,7 +72,7 @@ bool chunk::_bbox_for_scenery(const entity& s, local_coords local, Vector2b offs return s.atlas && !Vector2ui(s.bbox_size).isZero(); } -bool chunk::_bbox_for_scenery(const entity& s, bbox& value) noexcept +bool chunk::_bbox_for_scenery(const object& s, bbox& value) noexcept { return _bbox_for_scenery(s, s.coord.local(), s.offset, s.bbox_offset, s.bbox_size, value); } @@ -116,7 +116,7 @@ void chunk::_replace_bbox(const bbox& x0, const bbox& x1, bool b0, bool b1) CORRADE_ASSUME(false); } -bool chunk::can_place_entity(const entity_proto& proto, local_coords pos) +bool chunk::can_place_object(const object_proto& proto, local_coords pos) { (void)ensure_scenery_mesh(); diff --git a/src/chunk-scenery.cpp b/src/chunk-scenery.cpp index 6a3987aa..d211b6a4 100644 --- a/src/chunk-scenery.cpp +++ b/src/chunk-scenery.cpp @@ -1,6 +1,6 @@ #include "chunk-scenery.hpp" #include "shaders/shader.hpp" -#include "entity.hpp" +#include "object.hpp" #include "anim-atlas.hpp" #include "tile-atlas.hpp" #include @@ -11,7 +11,7 @@ namespace floormat { auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple { - Array array; + Array array; Array> scenery_vertexes; Array> scenery_indexes; return ensure_scenery_mesh({array, scenery_vertexes, scenery_indexes}); @@ -23,7 +23,7 @@ bool chunk::topo_sort_data::intersects(const topo_sort_data& o) const min[1] <= o.max[1] && max[1] >= o.min[1]; } -static void topo_dfs(Array& array, size_t& output, size_t i, size_t size) // NOLINT(misc-no-recursion) +static void topo_dfs(Array& array, size_t& output, size_t i, size_t size) // NOLINT(misc-no-recursion) { using m = typename chunk::topo_sort_data::m; @@ -69,7 +69,7 @@ static void topo_dfs(Array& array, size_t& output, siz output++; } -static void topological_sort(Array& array, size_t size) +static void topological_sort(Array& array, size_t size) { size_t output = 0; @@ -79,7 +79,7 @@ static void topological_sort(Array& array, size_t size fm_assert(output == size); } -auto chunk::make_topo_sort_data(entity& e, uint32_t mesh_idx) -> topo_sort_data +auto chunk::make_topo_sort_data(object& e, uint32_t mesh_idx) -> topo_sort_data { const auto& a = *e.atlas; const auto& f = a.frame(e.r, e.frame); @@ -94,7 +94,7 @@ auto chunk::make_topo_sort_data(entity& e, uint32_t mesh_idx) -> topo_sort_data .in_mesh_idx = mesh_idx, .ord = e.ordinal(), }; - if (e.type() == entity_type::scenery && !e.is_dynamic()) + if (e.type() == object_type::scenery && !e.is_dynamic()) { const auto bb_min_ = world_pos - Vector3(Vector2(e.bbox_size/2), 0); const auto bb_max_ = bb_min_ + Vector3(Vector2(e.bbox_size), 0); @@ -122,7 +122,7 @@ auto chunk::make_topo_sort_data(entity& e, uint32_t mesh_idx) -> topo_sort_data break; } } - else if (e.type() == entity_type::character) + else if (e.type() == object_type::character) data.mode = topo_sort_data::mode_character; return data; } @@ -131,7 +131,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce { ensure_scenery_buffers(buffers); - fm_assert(_entities_sorted); + fm_assert(_objects_sorted); if (_scenery_modified) { @@ -139,7 +139,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce const auto count = fm_begin( size_t ret = 0; - for (const auto& e : _entities) + for (const auto& e : _objects) ret += !e->is_dynamic(); return ret; ); @@ -147,7 +147,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce auto& scenery_vertexes = buffers.scenery_vertexes; auto& scenery_indexes = buffers.scenery_indexes; - for (auto i = 0uz; const auto& e : _entities) + for (auto i = 0uz; const auto& e : _objects) { if (e->is_dynamic()) continue; @@ -177,22 +177,22 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce scenery_mesh = Utility::move(mesh); } - const auto size = _entities.size(); + const auto size = _objects.size(); auto& array = buffers.array; uint32_t j = 0, i = 0; - for (const auto& e : _entities) + for (const auto& e : _objects) { auto index = e->is_dynamic() ? (uint32_t)-1 : j++; array[i++] = { e.get(), (uint32_t)-1, e->ordinal(), make_topo_sort_data(*e, index) }; } topological_sort(array, i); - return { scenery_mesh, ArrayView{array, size}, j }; + return { scenery_mesh, ArrayView{array, size}, j }; } void chunk::ensure_scenery_buffers(scenery_scratch_buffers bufs) { - const size_t len_ = _entities.size(); + const size_t len_ = _objects.size(); if (len_ <= bufs.array.size()) return; @@ -204,7 +204,7 @@ void chunk::ensure_scenery_buffers(scenery_scratch_buffers bufs) else len = std::bit_ceil(len_); - bufs.array = Array{NoInit, len}; + bufs.array = Array{NoInit, len}; bufs.scenery_vertexes = Array>{NoInit, len}; bufs.scenery_indexes = Array>{NoInit, len}; } diff --git a/src/chunk-scenery.hpp b/src/chunk-scenery.hpp index 5648da5c..89e40078 100644 --- a/src/chunk-scenery.hpp +++ b/src/chunk-scenery.hpp @@ -9,7 +9,7 @@ struct chunk::topo_sort_data { enum m : uint8_t { mode_none, mode_static, mode_character, }; - entity* in = nullptr; + object* in = nullptr; Vector2i min, max, center; uint32_t in_mesh_idx; float slope = 0, ord; @@ -20,9 +20,9 @@ struct chunk::topo_sort_data bool intersects(const topo_sort_data& other) const; }; -struct chunk::entity_draw_order +struct chunk::object_draw_order { - entity *e; + object *e; uint32_t mesh_idx; float ord; topo_sort_data data; @@ -30,13 +30,13 @@ struct chunk::entity_draw_order struct chunk::scenery_mesh_tuple { GL::Mesh& mesh; - ArrayView array; + ArrayView array; size_t size; }; struct chunk::scenery_scratch_buffers { - Array& array; + Array& array; Array>& scenery_vertexes; Array>& scenery_indexes; }; diff --git a/src/chunk.cpp b/src/chunk.cpp index ff35f292..f8b823a8 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -9,7 +9,7 @@ namespace floormat { namespace { -constexpr auto entity_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; +constexpr auto object_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; size_t _reload_no_ = 0; // NOLINT @@ -27,9 +27,9 @@ bool chunk::empty(bool force) const noexcept if (!force && !_maybe_empty) return false; for (auto i = 0uz; i < TILE_COUNT; i++) - if (!_entities.empty() || _ground && _ground->_ground_atlases[i] || _walls && (_walls->_wall_atlases[i*2+0] || _walls->_wall_atlases[i*2+1])) + if (!_objects.empty() || _ground && _ground->_ground_atlases[i] || _walls && (_walls->_wall_atlases[i*2+0] || _walls->_wall_atlases[i*2+1])) return _maybe_empty = false; - if (!_entities.empty()) + if (!_objects.empty()) return false; return true; } @@ -98,7 +98,7 @@ chunk::chunk(struct world& w) noexcept : _world{&w} chunk::~chunk() noexcept { _teardown = true; - _entities.clear(); + _objects.clear(); _rtree.RemoveAll(); } @@ -107,46 +107,46 @@ chunk& chunk::operator=(chunk&&) noexcept = default; bool chunk::bbox::operator==(const bbox& other) const noexcept = default; -void chunk::add_entity_unsorted(const std::shared_ptr& e) +void chunk::add_object_unsorted(const std::shared_ptr& e) { - _entities_sorted = false; + _objects_sorted = false; if (!e->is_dynamic()) mark_scenery_modified(); if (bbox bb; _bbox_for_scenery(*e, bb)) _add_bbox(bb); - _entities.push_back(e); + _objects.push_back(e); } -void chunk::sort_entities() +void chunk::sort_objects() { - if (_entities_sorted) + if (_objects_sorted) return; - _entities_sorted = true; + _objects_sorted = true; mark_scenery_modified(); - std::sort(_entities.begin(), _entities.end(), [](const auto& a, const auto& b) { + std::sort(_objects.begin(), _objects.end(), [](const auto& a, const auto& b) { return a->id < b->id; }); } -void chunk::add_entity(const std::shared_ptr& e) +void chunk::add_object(const std::shared_ptr& e) { - fm_assert(_entities_sorted); + fm_assert(_objects_sorted); if (!e->is_dynamic()) mark_scenery_modified(); if (bbox bb; _bbox_for_scenery(*e, bb)) _add_bbox(bb); - auto& es = _entities; + auto& es = _objects; es.reserve(es.size() + 1); - auto it = std::lower_bound(es.cbegin(), es.cend(), e, entity_id_lessp); - _entities.insert(it, e); + auto it = std::lower_bound(es.cbegin(), es.cend(), e, object_id_lessp); + _objects.insert(it, e); } -void chunk::remove_entity(size_t i) +void chunk::remove_object(size_t i) { - fm_assert(_entities_sorted); - auto& es = _entities; + fm_assert(_objects_sorted); + auto& es = _objects; fm_debug_assert(i < es.size()); const auto& e = es[i]; if (!e->is_dynamic()) @@ -156,10 +156,10 @@ void chunk::remove_entity(size_t i) es.erase(es.cbegin() + ptrdiff_t(i)); } -const std::vector>& chunk::entities() const +const std::vector>& chunk::objects() const { - fm_assert(_entities_sorted); - return _entities; + fm_assert(_objects_sorted); + return _objects; } } // namespace floormat diff --git a/src/chunk.hpp b/src/chunk.hpp index a41ed9a0..6a7e3fc9 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -13,8 +13,8 @@ namespace Corrade::Containers { template class Array; } namespace floormat { struct anim_atlas; -struct entity; -struct entity_proto; +struct object; +struct object_proto; class tile_iterator; class tile_const_iterator; @@ -23,7 +23,7 @@ enum class collision : unsigned char { }; enum class collision_type : unsigned char { - none, entity, scenery, geometry, + none, object, scenery, geometry, }; struct collision_data final { @@ -35,7 +35,7 @@ struct collision_data final { struct chunk final { friend struct tile_ref; - friend struct entity; + friend struct object; friend struct world; tile_ref operator[](size_t idx) noexcept; @@ -82,7 +82,7 @@ struct chunk final const size_t size; }; struct topo_sort_data; - struct entity_draw_order; + struct object_draw_order; struct scenery_mesh_tuple; struct scenery_scratch_buffers; @@ -108,13 +108,13 @@ struct chunk final RTree* rtree() noexcept; struct world& world() noexcept { return *_world; } - [[nodiscard]] bool can_place_entity(const entity_proto& proto, local_coords pos); + [[nodiscard]] bool can_place_object(const object_proto& proto, local_coords pos); - void add_entity(const std::shared_ptr& e); - void add_entity_unsorted(const std::shared_ptr& e); - void sort_entities(); - void remove_entity(size_t i); - const std::vector>& entities() const; + void add_object(const std::shared_ptr& e); + void add_object_unsorted(const std::shared_ptr& e); + void sort_objects(); + void remove_object(size_t i); + const std::vector>& objects() const; private: struct ground_stuff { @@ -131,7 +131,7 @@ private: Pointer _ground; Pointer _walls; - std::vector> _entities; + std::vector> _objects; struct world* _world; GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}, scenery_mesh{NoCreate}; @@ -144,10 +144,10 @@ private: _scenery_modified : 1 = true, _pass_modified : 1 = true, _teardown : 1 = false, - _entities_sorted : 1 = true; + _objects_sorted : 1 = true; void ensure_scenery_buffers(scenery_scratch_buffers bufs); - static topo_sort_data make_topo_sort_data(entity& e, uint32_t mesh_idx); + static topo_sort_data make_topo_sort_data(object& e, uint32_t mesh_idx); struct bbox final // NOLINT(cppcoreguidelines-pro-type-member-init) { @@ -156,8 +156,8 @@ private: bool operator==(const bbox& other) const noexcept; }; - static bool _bbox_for_scenery(const entity& s, bbox& value) noexcept; - static bool _bbox_for_scenery(const entity& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept; + static bool _bbox_for_scenery(const object& s, bbox& value) noexcept; + static bool _bbox_for_scenery(const object& s, local_coords local, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, bbox& value) noexcept; void _remove_bbox(const bbox& x); void _add_bbox(const bbox& x); void _replace_bbox(const bbox& x0, const bbox& x, bool b0, bool b); diff --git a/src/entity-type.hpp b/src/entity-type.hpp deleted file mode 100644 index 308bc8a6..00000000 --- a/src/entity-type.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace floormat { - -enum class entity_type : unsigned char { - none, character, scenery, light, -}; -constexpr inline size_t entity_type_BITS = 3; - -} // namespace floormat diff --git a/src/entity.cpp b/src/entity.cpp deleted file mode 100644 index 2a8c5aa1..00000000 --- a/src/entity.cpp +++ /dev/null @@ -1,300 +0,0 @@ -#include "entity.hpp" -#include "world.hpp" -#include "rotation.inl" -#include "anim-atlas.hpp" -#include "src/RTree-search.hpp" -#include "compat/exception.hpp" -#include "shaders/shader.hpp" -#include -#include - -namespace floormat { - -namespace { - -constexpr auto entity_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; - -} // namespace - -bool entity_proto::operator==(const entity_proto&) const = default; -entity_proto& entity_proto::operator=(const entity_proto&) = default; -entity_proto::~entity_proto() noexcept = default; -entity_proto::entity_proto() = default; -entity_proto::entity_proto(const entity_proto&) = default; -entity_type entity_proto::type_of() const noexcept { return type; } - -entity::entity(object_id id, struct chunk& c, const entity_proto& proto) : - id{id}, c{&c}, atlas{proto.atlas}, - offset{proto.offset}, bbox_offset{proto.bbox_offset}, - bbox_size{proto.bbox_size}, delta{proto.delta}, - frame{proto.frame}, r{proto.r}, pass{proto.pass} -{ - fm_soft_assert(atlas); - fm_soft_assert(atlas->check_rotation(r)); - fm_soft_assert(frame < atlas->info().nframes); -} - -entity::~entity() noexcept -{ - fm_debug_assert(id); - if (c->_teardown || c->_world->_teardown) [[unlikely]] - return; - if (chunk::bbox bb; c->_bbox_for_scenery(*this, bb)) - c->_remove_bbox(bb); - c->_world->do_kill_entity(id); - const_cast(id) = 0; -} - -float entity::ordinal() const -{ - return ordinal(coord.local(), offset, atlas->group(r).z_offset); -} - -float entity::ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const -{ - constexpr auto inv_tile_size = 1.f/TILE_SIZE2; - auto offset_ = ordinal_offset(offset); - auto vec = Vector2(xy) + offset_*inv_tile_size; - return vec[0] + vec[1] + Vector2(z_offset).sum(); -} - -struct chunk& entity::chunk() const -{ - return *c; -} - -size_t entity::index() const -{ - auto& c = chunk(); - const auto fn = [id = id](const auto& a, const auto&) { return a->id < id; }; - auto& es = c._entities; - auto it = std::lower_bound(es.cbegin(), es.cend(), nullptr, fn); - fm_assert(it != es.cend()); - fm_assert((*it)->id == id); - return (size_t)std::distance(es.cbegin(), it); -} - -bool entity::is_virtual() const -{ - return false; -} - -bool entity::can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) -{ - if (bbox_offset.isZero() && bbox_size[0] == bbox_size[1]) - return true; - const auto offset_ = rotate_point(offset, old_r, new_r); - const auto bbox_offset_ = rotate_point(bbox_offset, old_r, new_r); - const auto bbox_size_ = rotate_size(bbox_size, old_r, new_r); - return can_move_to({}, coord, offset_, bbox_offset_, bbox_size_); -} - -bool entity::can_rotate(rotation new_r) -{ - if (new_r == r) - return true; - - return can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size); -} - -void entity::rotate(size_t, rotation new_r) -{ - fm_assert(atlas->check_rotation(new_r)); - auto offset_ = !is_dynamic() ? rotate_point(offset, r, new_r) : offset; - auto bbox_offset_ = rotate_point(bbox_offset, r, new_r); - auto bbox_size_ = rotate_size(bbox_size, r, new_r); - set_bbox(offset_, bbox_offset_, bbox_size_, pass); - if (r != new_r && !is_dynamic()) - c->mark_scenery_modified(); - - const_cast(r) = new_r; -} - -template constexpr T sgn(T val) { return T(T(0) < val) - T(val < T(0)); } - -Pair entity::normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i new_offset) -{ - auto off_tmp = Vector2i(cur_offset) + new_offset; - auto off_new = off_tmp % iTILE_SIZE2; - constexpr auto half_tile = iTILE_SIZE2/2; - for (auto i = 0uz; i < 2; i++) - { - auto sign = sgn(off_new[i]); - auto absval = std::abs(off_new[i]); - if (absval > half_tile[i]) - { - Vector2i v(0); - v[i] = sign; - coord += v; - off_new[i] = (iTILE_SIZE[i] - absval)*-sign; - } - } - return { coord, Vector2b(off_new) }; -} - -template -static bool do_search(struct chunk* c, chunk_coords_ coord, object_id id, Vector2 min, Vector2 max, Vector2b off = {}) -{ - if constexpr(neighbor) - { - const auto ch = chunk_coords{(int16_t)(coord.x + off[0]), (int16_t)(coord.y + off[1])}; - constexpr auto size = TILE_SIZE2 * TILE_MAX_DIM, grace = TILE_SIZE2 * 4; - const auto off_ = Vector2(off) * size; - min -= off_; - max -= off_; - if (!(min + grace >= Vector2{} && max - grace <= size)) [[likely]] - return true; - auto& w = c->world(); - c = w.at({ch, coord.z}); - if (!c) [[unlikely]] - return true; - } - bool ret = true; - c->rtree()->Search(min.data(), max.data(), [&](object_id data, const auto&) { - auto x = std::bit_cast(data); - if (x.data != id && x.pass != (uint64_t)pass_mode::pass) - return ret = false; - else - return true; - }); - return ret; -} - -bool entity::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) -{ - auto [coord_, offset_] = normalize_coords(coord2, offset, delta); - - if (coord_.z() != coord.z()) [[unlikely]] - return false; - - auto& w = *c->_world; - auto& c_ = coord_.chunk() == coord.chunk() ? *c : w[{coord_.chunk(), coord_.z()}]; - - const auto center = Vector2(coord_.local())*TILE_SIZE2 + Vector2(offset_) + Vector2(bbox_offset), - half_bbox = Vector2(bbox_size)*.5f, - min = center - half_bbox, max = min + Vector2(bbox_size); - auto ch = chunk_coords_{coord_.chunk(), coord_.z()}; - if (!do_search(&c_, ch, id, min, max)) - return false; - for (const auto& off : world::neighbor_offsets) - if (!do_search(&c_, ch, id, min, max, off)) - return false; - return true; -} - -bool entity::can_move_to(Vector2i delta) -{ - return can_move_to(delta, coord, offset, bbox_offset, bbox_size); -} - -size_t entity::move_to(size_t& i, Vector2i delta, rotation new_r) -{ - if (!can_rotate(new_r)) - return i; - - auto& es = c->_entities; - fm_debug_assert(i < es.size()); - auto e_ = es[i]; - - fm_assert(&*e_ == this); - auto& w = *c->_world; - const auto [coord_, offset_] = normalize_coords(coord, offset, delta); - - if (coord_ == coord && offset_ == offset) - return i; - - if (!is_dynamic()) - c->mark_scenery_modified(); - - chunk::bbox bb0, bb1; - const auto bb_offset = rotate_point(bbox_offset, r, new_r); - const auto bb_size = rotate_size(bbox_size, r, new_r); - bool b0 = c->_bbox_for_scenery(*this, bb0), - b1 = c->_bbox_for_scenery(*this, coord_.local(), offset_, bb_offset, bb_size, bb1); - - if (coord_.chunk() == coord.chunk()) - { - c->_replace_bbox(bb0, bb1, b0, b1); - const_cast(coord) = coord_; - set_bbox_(offset_, bb_offset, bb_size, pass); - const_cast(r) = new_r; - //for (auto i = 0uz; const auto& x : es) fm_debug("%zu %s %f", i++, x->atlas->name().data(), x->ordinal()); - //fm_debug("insert (%hd;%hd|%hhd;%hhd) %td -> %zu | %f", coord_.chunk().x, coord_.chunk().y, coord_.local().x, coord_.local().y, pos1, es.size(), e.ordinal()); - } - else - { - //fm_debug("change-chunk (%hd;%hd|%hhd;%hhd)", coord_.chunk().x, coord_.chunk().y, coord_.local().x, coord_.local().y); - auto& c2 = w[{coord_.chunk(), coord_.z()}]; - if (!is_dynamic()) - c2.mark_scenery_modified(); - c2._add_bbox(bb1); - c->remove_entity(i); - auto& es = c2._entities; - auto it = std::lower_bound(es.cbegin(), es.cend(), e_, entity_id_lessp); - const_cast(coord) = coord_; - set_bbox_(offset_, bb_offset, bb_size, pass); - const_cast(r) = new_r; - const_cast(c) = &c2; - i = (size_t)std::distance(es.cbegin(), it); - es.insert(it, std::move(e_)); - } - - return i; -} - -void entity::move_to(Magnum::Vector2i delta) -{ - auto i = index(); - (void)move_to(i, delta, r); -} - -void entity::set_bbox_(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass_) -{ - const_cast(offset) = offset_; - const_cast(bbox_offset) = bbox_offset_; - const_cast(bbox_size) = bbox_size_; - const_cast(pass) = pass_; -} - -entity::operator entity_proto() const -{ - entity_proto ret; - ret.atlas = atlas; - ret.offset = offset; - ret.bbox_offset = bbox_offset; - ret.bbox_size = bbox_size; - ret.delta = delta; - ret.frame = frame; - ret.type = type(); - ret.r = r; - ret.pass = pass; - return ret; -} - -void entity::set_bbox(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass) -{ - if (offset != offset_) - if (!is_dynamic()) - c->mark_scenery_modified(); - - chunk::bbox bb0, bb; - const bool b0 = c->_bbox_for_scenery(*this, bb0); - set_bbox_(offset_, bbox_offset_, bbox_size_, pass); - const bool b = c->_bbox_for_scenery(*this, bb); - c->_replace_bbox(bb0, bb, b0, b); -} - -bool entity::can_activate(size_t) const { return false; } -bool entity::activate(size_t) { return false; } - -bool entity::is_dynamic() const -{ - return atlas->info().fps > 0; -} - -entity_type entity::type_of() const noexcept -{ - return type(); -} - -} // namespace floormat diff --git a/src/entity.hpp b/src/entity.hpp deleted file mode 100644 index e332c93a..00000000 --- a/src/entity.hpp +++ /dev/null @@ -1,89 +0,0 @@ -#pragma once -#include "compat/defs.hpp" -#include "src/global-coords.hpp" -#include "src/rotation.hpp" -#include "src/pass-mode.hpp" -#include "src/entity-type.hpp" -#include "src/object-id.hpp" -#include -#include - -namespace floormat { - -template struct entity_type_; -struct anim_atlas; -struct world; -struct chunk; - -struct entity_proto -{ - std::shared_ptr atlas; - Vector2b offset, bbox_offset; - Vector2ub bbox_size = Vector2ub(iTILE_SIZE2); - uint16_t delta = 0, frame = 0; - entity_type type : 3 = entity_type::none; - rotation r : rotation_BITS = rotation::N; - pass_mode pass : pass_mode_BITS = pass_mode::see_through; - - entity_proto& operator=(const entity_proto&); - entity_proto(); - entity_proto(const entity_proto&); - - virtual bool operator==(const entity_proto&) const; - bool operator!=(const entity_proto& o) const { return !operator==(o); } - virtual ~entity_proto() noexcept; - - entity_type type_of() const noexcept; -}; - -// todo rename to 'object' -struct entity -{ - fm_DECLARE_DELETED_COPY_ASSIGNMENT(entity); - - const object_id id = 0; - struct chunk* const c; - const std::shared_ptr atlas; - const global_coords coord; - const Vector2b offset, bbox_offset; - const Vector2ub bbox_size; - uint16_t delta = 0, frame = 0; - const rotation r = rotation::N; - const pass_mode pass = pass_mode::see_through; - - virtual ~entity() noexcept; - - virtual Vector2 ordinal_offset(Vector2b offset) const = 0; - virtual float depth_offset() const = 0; - float ordinal() const; - float ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const; - struct chunk& chunk() const; - size_t index() const; - virtual bool is_virtual() const; - - explicit operator entity_proto() const; - - virtual entity_type type() const noexcept = 0; - virtual bool can_activate(size_t i) const; - virtual bool activate(size_t i); - virtual void update(size_t i, float dt) = 0; - virtual void rotate(size_t i, rotation r); - virtual bool can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size); - virtual bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize); - virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); - - entity_type type_of() const noexcept; - static Pair normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); - - virtual bool is_dynamic() const; - bool can_rotate(rotation new_r); - bool can_move_to(Vector2i delta); - size_t move_to(size_t& i, Vector2i delta, rotation new_r); - void move_to(Vector2i delta); - -protected: - entity(object_id id, struct chunk& c, const entity_proto& proto); - void set_bbox_(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); -}; - -} // namespace floormat diff --git a/src/light.cpp b/src/light.cpp index 58246d94..13f39b22 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -14,7 +14,7 @@ light_proto::light_proto() { atlas = loader.vobj("light"_s).atlas; pass = pass_mode::pass; - type = entity_type::light; + type = object_type::light; } light_proto::light_proto(const light_proto&) = default; @@ -23,7 +23,7 @@ light_proto::~light_proto() noexcept = default; bool light_proto::operator==(const light_proto&) const = default; light::light(object_id id, struct chunk& c, const light_proto& proto) : - entity{id, c, proto}, + object{id, c, proto}, max_distance{proto.max_distance}, color{proto.color}, falloff{proto.falloff}, @@ -46,7 +46,7 @@ Vector2 light::ordinal_offset(Vector2b) const light::operator light_proto() const { light_proto ret; - static_cast(ret) = entity_proto(*this); + static_cast(ret) = object_proto(*this); ret.max_distance = max_distance; ret.color = color; ret.falloff = falloff; @@ -54,7 +54,7 @@ light::operator light_proto() const return ret; } -entity_type light::type() const noexcept { return entity_type::light; } +object_type light::type() const noexcept { return object_type::light; } void light::update(size_t, float) {} bool light::is_dynamic() const { return true; } bool light::is_virtual() const { return true; } diff --git a/src/light.hpp b/src/light.hpp index a58e0221..b5c4469a 100644 --- a/src/light.hpp +++ b/src/light.hpp @@ -1,13 +1,13 @@ #pragma once #include "src/light-falloff.hpp" -#include "src/entity.hpp" +#include "src/object.hpp" #include #include #include namespace floormat { -struct light_proto : entity_proto +struct light_proto : object_proto { light_proto(); light_proto(const light_proto&); @@ -21,7 +21,7 @@ struct light_proto : entity_proto uint8_t enabled : 1 = true; }; -struct light final : entity +struct light final : object { float max_distance; Color4ub color; @@ -32,7 +32,7 @@ struct light final : entity Vector2 ordinal_offset(Vector2b offset) const override; float depth_offset() const override; - entity_type type() const noexcept override; + object_type type() const noexcept override; void update(size_t i, float dt) override; bool is_dynamic() const override; bool is_virtual() const override; @@ -42,7 +42,7 @@ struct light final : entity friend struct world; }; -template<> struct entity_type_ : std::integral_constant {}; -template<> struct entity_type_ : std::integral_constant {}; +template<> struct object_type_ : std::integral_constant {}; +template<> struct object_type_ : std::integral_constant {}; } // namespace floormat diff --git a/src/object-type.hpp b/src/object-type.hpp new file mode 100644 index 00000000..1a1d1aff --- /dev/null +++ b/src/object-type.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace floormat { + +enum class object_type : unsigned char { + none, character, scenery, light, +}; +constexpr inline size_t object_type_BITS = 3; + +} // namespace floormat diff --git a/src/object.cpp b/src/object.cpp new file mode 100644 index 00000000..1dc05f4b --- /dev/null +++ b/src/object.cpp @@ -0,0 +1,300 @@ +#include "object.hpp" +#include "world.hpp" +#include "rotation.inl" +#include "anim-atlas.hpp" +#include "src/RTree-search.hpp" +#include "compat/exception.hpp" +#include "shaders/shader.hpp" +#include +#include + +namespace floormat { + +namespace { + +constexpr auto object_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; }; + +} // namespace + +bool object_proto::operator==(const object_proto&) const = default; +object_proto& object_proto::operator=(const object_proto&) = default; +object_proto::~object_proto() noexcept = default; +object_proto::object_proto() = default; +object_proto::object_proto(const object_proto&) = default; +object_type object_proto::type_of() const noexcept { return type; } + +object::object(object_id id, struct chunk& c, const object_proto& proto) : + id{id}, c{&c}, atlas{proto.atlas}, + offset{proto.offset}, bbox_offset{proto.bbox_offset}, + bbox_size{proto.bbox_size}, delta{proto.delta}, + frame{proto.frame}, r{proto.r}, pass{proto.pass} +{ + fm_soft_assert(atlas); + fm_soft_assert(atlas->check_rotation(r)); + fm_soft_assert(frame < atlas->info().nframes); +} + +object::~object() noexcept +{ + fm_debug_assert(id); + if (c->_teardown || c->_world->_teardown) [[unlikely]] + return; + if (chunk::bbox bb; c->_bbox_for_scenery(*this, bb)) + c->_remove_bbox(bb); + c->_world->do_kill_object(id); + const_cast(id) = 0; +} + +float object::ordinal() const +{ + return ordinal(coord.local(), offset, atlas->group(r).z_offset); +} + +float object::ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const +{ + constexpr auto inv_tile_size = 1.f/TILE_SIZE2; + auto offset_ = ordinal_offset(offset); + auto vec = Vector2(xy) + offset_*inv_tile_size; + return vec[0] + vec[1] + Vector2(z_offset).sum(); +} + +struct chunk& object::chunk() const +{ + return *c; +} + +size_t object::index() const +{ + auto& c = chunk(); + const auto fn = [id = id](const auto& a, const auto&) { return a->id < id; }; + auto& es = c._objects; + auto it = std::lower_bound(es.cbegin(), es.cend(), nullptr, fn); + fm_assert(it != es.cend()); + fm_assert((*it)->id == id); + return (size_t)std::distance(es.cbegin(), it); +} + +bool object::is_virtual() const +{ + return false; +} + +bool object::can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) +{ + if (bbox_offset.isZero() && bbox_size[0] == bbox_size[1]) + return true; + const auto offset_ = rotate_point(offset, old_r, new_r); + const auto bbox_offset_ = rotate_point(bbox_offset, old_r, new_r); + const auto bbox_size_ = rotate_size(bbox_size, old_r, new_r); + return can_move_to({}, coord, offset_, bbox_offset_, bbox_size_); +} + +bool object::can_rotate(rotation new_r) +{ + if (new_r == r) + return true; + + return can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size); +} + +void object::rotate(size_t, rotation new_r) +{ + fm_assert(atlas->check_rotation(new_r)); + auto offset_ = !is_dynamic() ? rotate_point(offset, r, new_r) : offset; + auto bbox_offset_ = rotate_point(bbox_offset, r, new_r); + auto bbox_size_ = rotate_size(bbox_size, r, new_r); + set_bbox(offset_, bbox_offset_, bbox_size_, pass); + if (r != new_r && !is_dynamic()) + c->mark_scenery_modified(); + + const_cast(r) = new_r; +} + +template constexpr T sgn(T val) { return T(T(0) < val) - T(val < T(0)); } + +Pair object::normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i new_offset) +{ + auto off_tmp = Vector2i(cur_offset) + new_offset; + auto off_new = off_tmp % iTILE_SIZE2; + constexpr auto half_tile = iTILE_SIZE2/2; + for (auto i = 0uz; i < 2; i++) + { + auto sign = sgn(off_new[i]); + auto absval = std::abs(off_new[i]); + if (absval > half_tile[i]) + { + Vector2i v(0); + v[i] = sign; + coord += v; + off_new[i] = (iTILE_SIZE[i] - absval)*-sign; + } + } + return { coord, Vector2b(off_new) }; +} + +template +static bool do_search(struct chunk* c, chunk_coords_ coord, object_id id, Vector2 min, Vector2 max, Vector2b off = {}) +{ + if constexpr(neighbor) + { + const auto ch = chunk_coords{(int16_t)(coord.x + off[0]), (int16_t)(coord.y + off[1])}; + constexpr auto size = TILE_SIZE2 * TILE_MAX_DIM, grace = TILE_SIZE2 * 4; + const auto off_ = Vector2(off) * size; + min -= off_; + max -= off_; + if (!(min + grace >= Vector2{} && max - grace <= size)) [[likely]] + return true; + auto& w = c->world(); + c = w.at({ch, coord.z}); + if (!c) [[unlikely]] + return true; + } + bool ret = true; + c->rtree()->Search(min.data(), max.data(), [&](object_id data, const auto&) { + auto x = std::bit_cast(data); + if (x.data != id && x.pass != (uint64_t)pass_mode::pass) + return ret = false; + else + return true; + }); + return ret; +} + +bool object::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) +{ + auto [coord_, offset_] = normalize_coords(coord2, offset, delta); + + if (coord_.z() != coord.z()) [[unlikely]] + return false; + + auto& w = *c->_world; + auto& c_ = coord_.chunk() == coord.chunk() ? *c : w[{coord_.chunk(), coord_.z()}]; + + const auto center = Vector2(coord_.local())*TILE_SIZE2 + Vector2(offset_) + Vector2(bbox_offset), + half_bbox = Vector2(bbox_size)*.5f, + min = center - half_bbox, max = min + Vector2(bbox_size); + auto ch = chunk_coords_{coord_.chunk(), coord_.z()}; + if (!do_search(&c_, ch, id, min, max)) + return false; + for (const auto& off : world::neighbor_offsets) + if (!do_search(&c_, ch, id, min, max, off)) + return false; + return true; +} + +bool object::can_move_to(Vector2i delta) +{ + return can_move_to(delta, coord, offset, bbox_offset, bbox_size); +} + +size_t object::move_to(size_t& i, Vector2i delta, rotation new_r) +{ + if (!can_rotate(new_r)) + return i; + + auto& es = c->_objects; + fm_debug_assert(i < es.size()); + auto e_ = es[i]; + + fm_assert(&*e_ == this); + auto& w = *c->_world; + const auto [coord_, offset_] = normalize_coords(coord, offset, delta); + + if (coord_ == coord && offset_ == offset) + return i; + + if (!is_dynamic()) + c->mark_scenery_modified(); + + chunk::bbox bb0, bb1; + const auto bb_offset = rotate_point(bbox_offset, r, new_r); + const auto bb_size = rotate_size(bbox_size, r, new_r); + bool b0 = c->_bbox_for_scenery(*this, bb0), + b1 = c->_bbox_for_scenery(*this, coord_.local(), offset_, bb_offset, bb_size, bb1); + + if (coord_.chunk() == coord.chunk()) + { + c->_replace_bbox(bb0, bb1, b0, b1); + const_cast(coord) = coord_; + set_bbox_(offset_, bb_offset, bb_size, pass); + const_cast(r) = new_r; + //for (auto i = 0uz; const auto& x : es) fm_debug("%zu %s %f", i++, x->atlas->name().data(), x->ordinal()); + //fm_debug("insert (%hd;%hd|%hhd;%hhd) %td -> %zu | %f", coord_.chunk().x, coord_.chunk().y, coord_.local().x, coord_.local().y, pos1, es.size(), e.ordinal()); + } + else + { + //fm_debug("change-chunk (%hd;%hd|%hhd;%hhd)", coord_.chunk().x, coord_.chunk().y, coord_.local().x, coord_.local().y); + auto& c2 = w[{coord_.chunk(), coord_.z()}]; + if (!is_dynamic()) + c2.mark_scenery_modified(); + c2._add_bbox(bb1); + c->remove_object(i); + auto& es = c2._objects; + auto it = std::lower_bound(es.cbegin(), es.cend(), e_, object_id_lessp); + const_cast(coord) = coord_; + set_bbox_(offset_, bb_offset, bb_size, pass); + const_cast(r) = new_r; + const_cast(c) = &c2; + i = (size_t)std::distance(es.cbegin(), it); + es.insert(it, std::move(e_)); + } + + return i; +} + +void object::move_to(Magnum::Vector2i delta) +{ + auto i = index(); + (void)move_to(i, delta, r); +} + +void object::set_bbox_(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass_) +{ + const_cast(offset) = offset_; + const_cast(bbox_offset) = bbox_offset_; + const_cast(bbox_size) = bbox_size_; + const_cast(pass) = pass_; +} + +object::operator object_proto() const +{ + object_proto ret; + ret.atlas = atlas; + ret.offset = offset; + ret.bbox_offset = bbox_offset; + ret.bbox_size = bbox_size; + ret.delta = delta; + ret.frame = frame; + ret.type = type(); + ret.r = r; + ret.pass = pass; + return ret; +} + +void object::set_bbox(Vector2b offset_, Vector2b bbox_offset_, Vector2ub bbox_size_, pass_mode pass) +{ + if (offset != offset_) + if (!is_dynamic()) + c->mark_scenery_modified(); + + chunk::bbox bb0, bb; + const bool b0 = c->_bbox_for_scenery(*this, bb0); + set_bbox_(offset_, bbox_offset_, bbox_size_, pass); + const bool b = c->_bbox_for_scenery(*this, bb); + c->_replace_bbox(bb0, bb, b0, b); +} + +bool object::can_activate(size_t) const { return false; } +bool object::activate(size_t) { return false; } + +bool object::is_dynamic() const +{ + return atlas->info().fps > 0; +} + +object_type object::type_of() const noexcept +{ + return type(); +} + +} // namespace floormat diff --git a/src/object.hpp b/src/object.hpp new file mode 100644 index 00000000..f411a7ec --- /dev/null +++ b/src/object.hpp @@ -0,0 +1,89 @@ +#pragma once +#include "compat/defs.hpp" +#include "src/global-coords.hpp" +#include "src/rotation.hpp" +#include "src/pass-mode.hpp" +#include "src/object-type.hpp" +#include "src/object-id.hpp" +#include +#include + +namespace floormat { + +template struct object_type_; +struct anim_atlas; +struct world; +struct chunk; + +struct object_proto +{ + std::shared_ptr atlas; + Vector2b offset, bbox_offset; + Vector2ub bbox_size = Vector2ub(iTILE_SIZE2); + uint16_t delta = 0, frame = 0; + object_type type : 3 = object_type::none; + rotation r : rotation_BITS = rotation::N; + pass_mode pass : pass_mode_BITS = pass_mode::see_through; + + object_proto& operator=(const object_proto&); + object_proto(); + object_proto(const object_proto&); + + virtual bool operator==(const object_proto&) const; + bool operator!=(const object_proto& o) const { return !operator==(o); } + virtual ~object_proto() noexcept; + + object_type type_of() const noexcept; +}; + +// todo rename to 'object' +struct object +{ + fm_DECLARE_DELETED_COPY_ASSIGNMENT(object); + + const object_id id = 0; + struct chunk* const c; + const std::shared_ptr atlas; + const global_coords coord; + const Vector2b offset, bbox_offset; + const Vector2ub bbox_size; + uint16_t delta = 0, frame = 0; + const rotation r = rotation::N; + const pass_mode pass = pass_mode::see_through; + + virtual ~object() noexcept; + + virtual Vector2 ordinal_offset(Vector2b offset) const = 0; + virtual float depth_offset() const = 0; + float ordinal() const; + float ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const; + struct chunk& chunk() const; + size_t index() const; + virtual bool is_virtual() const; + + explicit operator object_proto() const; + + virtual object_type type() const noexcept = 0; + virtual bool can_activate(size_t i) const; + virtual bool activate(size_t i); + virtual void update(size_t i, float dt) = 0; + virtual void rotate(size_t i, rotation r); + virtual bool can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size); + virtual bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize); + virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); + + object_type type_of() const noexcept; + static Pair normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); + + virtual bool is_dynamic() const; + bool can_rotate(rotation new_r); + bool can_move_to(Vector2i delta); + size_t move_to(size_t& i, Vector2i delta, rotation new_r); + void move_to(Vector2i delta); + +protected: + object(object_id id, struct chunk& c, const object_proto& proto); + void set_bbox_(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); +}; + +} // namespace floormat diff --git a/src/scenery.cpp b/src/scenery.cpp index c5435137..22c7083a 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -11,7 +11,7 @@ namespace floormat { scenery_proto::scenery_proto() { - type = entity_type::scenery; + type = object_type::scenery; } scenery_proto& scenery_proto::operator=(const scenery_proto&) = default; @@ -129,12 +129,12 @@ bool scenery::activate(size_t) return false; } -bool scenery_proto::operator==(const entity_proto& e0) const +bool scenery_proto::operator==(const object_proto& e0) const { if (type != e0.type) return false; - if (!entity_proto::operator==(e0)) + if (!object_proto::operator==(e0)) return false; const auto& s0 = static_cast(e0); @@ -142,12 +142,12 @@ bool scenery_proto::operator==(const entity_proto& e0) const closing == s0.closing && interactive == s0.interactive; } -entity_type scenery::type() const noexcept { return entity_type::scenery; } +object_type scenery::type() const noexcept { return object_type::scenery; } scenery::operator scenery_proto() const { scenery_proto ret; - static_cast(ret) = entity::operator entity_proto(); + static_cast(ret) = object::operator object_proto(); ret.sc_type = sc_type; ret.active = active; ret.closing = closing; @@ -156,7 +156,7 @@ scenery::operator scenery_proto() const } scenery::scenery(object_id id, struct chunk& c, const scenery_proto& proto) : - entity{id, c, proto}, sc_type{proto.sc_type}, active{proto.active}, + object{id, c, proto}, sc_type{proto.sc_type}, active{proto.active}, closing{proto.closing}, interactive{proto.interactive} { fm_debug_assert(atlas); // todo add placeholder graphic diff --git a/src/scenery.hpp b/src/scenery.hpp index 17fdad66..1c3b03ad 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -2,7 +2,7 @@ #include "pass-mode.hpp" #include "tile-defs.hpp" #include "rotation.hpp" -#include "entity.hpp" +#include "object.hpp" #include #include #include @@ -18,7 +18,7 @@ enum class scenery_type : unsigned char { }; constexpr inline size_t scenery_type_BITS = 3; -struct scenery_proto : entity_proto +struct scenery_proto : object_proto { scenery_type sc_type : scenery_type_BITS = scenery_type::none; unsigned char active : 1 = false; @@ -29,11 +29,11 @@ struct scenery_proto : entity_proto scenery_proto(const scenery_proto&); ~scenery_proto() noexcept override; scenery_proto& operator=(const scenery_proto&); - bool operator==(const entity_proto& proto) const override; + bool operator==(const object_proto& proto) const override; operator bool() const; }; -struct scenery final : entity +struct scenery final : object { scenery_type sc_type : 3 = scenery_type::none; unsigned char active : 1 = false; @@ -46,7 +46,7 @@ struct scenery final : entity bool can_activate(size_t i) const override; bool activate(size_t i) override; - entity_type type() const noexcept override; + object_type type() const noexcept override; explicit operator scenery_proto() const; private: @@ -54,7 +54,7 @@ private: scenery(object_id id, struct chunk& c, const scenery_proto& proto); }; -template<> struct entity_type_ : std::integral_constant {}; -template<> struct entity_type_ : std::integral_constant {}; +template<> struct object_type_ : std::integral_constant {}; +template<> struct object_type_ : std::integral_constant {}; } // namespace floormat diff --git a/src/world.cpp b/src/world.cpp index e829ab5e..97cc4702 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,6 +1,6 @@ #include "world.hpp" #include "chunk.hpp" -#include "entity.hpp" +#include "object.hpp" #include "compat/int-hash.hpp" #include "compat/exception.hpp" @@ -47,10 +47,10 @@ world& world::operator=(world&& w) noexcept fm_debug_assert(w._unique_id == nullptr); _last_chunk = {}; _chunks = std::move(w._chunks); - _entities = std::move(w._entities); - _entity_counter = w._entity_counter; + _objects = std::move(w._objects); + _object_counter = w._object_counter; _current_frame = w._current_frame; - w._entity_counter = 0; + w._object_counter = 0; for (auto& [id, c] : _chunks) c._world = this; @@ -71,19 +71,19 @@ world::~world() noexcept v.mark_scenery_modified(); v.mark_passability_modified(); _last_chunk = {}; - v._entities.clear(); + v._objects.clear(); } _last_chunk = {}; _chunks.clear(); - _entities.clear(); + _objects.clear(); } world::world(size_t capacity) : _chunks{capacity} { _chunks.max_load_factor(max_load_factor); _chunks.reserve(initial_capacity); - _entities.max_load_factor(max_load_factor); - _entities.reserve(initial_capacity); + _objects.max_load_factor(max_load_factor); + _objects.reserve(initial_capacity); } chunk& world::operator[](chunk_coords_ coord) noexcept @@ -122,10 +122,10 @@ void world::clear() _last_collection = 0; _chunks.clear(); _chunks.rehash(initial_capacity); - _entities.clear(); - _entities.rehash(initial_capacity); + _objects.clear(); + _objects.rehash(initial_capacity); _collect_every = initial_collect_every; - _entity_counter = entity_counter_init; + _object_counter = object_counter_init; auto& [c, pos] = _last_chunk; c = nullptr; pos = chunk_tuple::invalid_coords; @@ -158,44 +158,44 @@ void world::collect(bool force) fm_debug("world: collected %zu/%zu chunks", len, len0); } -void world::do_make_entity(const std::shared_ptr& e, global_coords pos, bool sorted) +void world::do_make_object(const std::shared_ptr& e, global_coords pos, bool sorted) { fm_assert(e->id > 0); fm_debug_assert(_unique_id && e->c->world()._unique_id == _unique_id); - fm_assert(!_entities.contains(e->id)); - fm_assert(e->type() != entity_type::none); + fm_assert(!_objects.contains(e->id)); + fm_assert(e->type() != object_type::none); const_cast(e->coord) = pos; - _entities[e->id] = e; + _objects[e->id] = e; if (sorted) - e->c->add_entity(e); + e->c->add_object(e); else - e->c->add_entity_unsorted(e); + e->c->add_object_unsorted(e); } -void world::do_kill_entity(object_id id) +void world::do_kill_object(object_id id) { fm_debug_assert(id > 0); - auto cnt = _entities.erase(id); + auto cnt = _objects.erase(id); fm_debug_assert(cnt > 0); } -std::shared_ptr world::find_entity_(object_id id) +std::shared_ptr world::find_object_(object_id id) { - auto it = _entities.find(id); - auto ret = it == _entities.end() ? nullptr : it->second.lock(); + auto it = _objects.find(id); + auto ret = it == _objects.end() ? nullptr : it->second.lock(); fm_debug_assert(!ret || &ret->c->world() == this); return ret; } -void world::set_entity_counter(object_id value) +void world::set_object_counter(object_id value) { - fm_assert(value >= _entity_counter); - _entity_counter = value; + fm_assert(value >= _object_counter); + _object_counter = value; } -void world::throw_on_wrong_entity_type(object_id id, entity_type actual, entity_type expected) +void world::throw_on_wrong_object_type(object_id id, object_type actual, object_type expected) { - fm_throw("object '{}' has wrong entity type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); + fm_throw("object '{}' has wrong object type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); } auto world::neighbors(floormat::chunk_coords_ coord) -> std::array diff --git a/src/world.hpp b/src/world.hpp index c176dbbe..8f82efab 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -2,7 +2,7 @@ #include "compat/defs.hpp" #include "chunk.hpp" #include "global-coords.hpp" -#include "entity-type.hpp" +#include "object-type.hpp" #include "compat/int-hash.hpp" #include #include @@ -16,15 +16,15 @@ struct std::hash final { namespace floormat { -struct entity; -template struct entity_type_; +struct object; +template struct object_type_; struct object_id_hasher { size_t operator()(object_id id) const noexcept { return int_hash(id); } }; struct world final { - static constexpr object_id entity_counter_init = 1024; + static constexpr object_id object_counter_init = 1024; static constexpr size_t initial_capacity = 512; static constexpr float max_load_factor = .5; static constexpr size_t initial_collect_every = 64; @@ -37,22 +37,22 @@ private: } _last_chunk; std::unordered_map _chunks; - tsl::robin_map, object_id_hasher> _entities; + tsl::robin_map, object_id_hasher> _objects; size_t _last_collection = 0; size_t _collect_every = initial_collect_every; std::shared_ptr _unique_id = std::make_shared('A'); - object_id _entity_counter = entity_counter_init; - uint64_t _current_frame = 1; // zero is special for struct entity + object_id _object_counter = object_counter_init; + uint64_t _current_frame = 1; // zero is special for struct object bool _teardown : 1 = false; explicit world(size_t capacity); - void do_make_entity(const std::shared_ptr& e, global_coords pos, bool sorted); - void do_kill_entity(object_id id); - std::shared_ptr find_entity_(object_id id); - [[noreturn]] static void throw_on_wrong_entity_type(object_id id, entity_type actual, entity_type expected); + void do_make_object(const std::shared_ptr& e, global_coords pos, bool sorted); + void do_kill_object(object_id id); + std::shared_ptr find_object_(object_id id); + [[noreturn]] static void throw_on_wrong_object_type(object_id id, object_type actual, object_type expected); - friend struct entity; + friend struct object; public: explicit world(); @@ -82,21 +82,21 @@ public: template requires requires(chunk& c) { T{object_id(), c, std::declval()...}; - std::is_base_of_v; + std::is_base_of_v; } - std::shared_ptr make_entity(object_id id, global_coords pos, Xs&&... xs) + std::shared_ptr make_object(object_id id, global_coords pos, Xs&&... xs) { auto ret = std::shared_ptr(new T{id, operator[](chunk_coords_{pos.chunk(), pos.z()}), Utility::forward(xs)...}); - do_make_entity(static_pointer_cast(ret), pos, sorted); + do_make_object(static_pointer_cast(ret), pos, sorted); return ret; } - template std::shared_ptr find_entity(object_id id); + template std::shared_ptr find_object(object_id id); bool is_teardown() const { return _teardown; } - object_id entity_counter() const { return _entity_counter; } - [[nodiscard]] object_id make_id() { return ++_entity_counter; } - void set_entity_counter(object_id value); + object_id object_counter() const { return _object_counter; } + [[nodiscard]] object_id make_id() { return ++_object_counter; } + void set_object_counter(object_id value); struct neighbor_pair final { chunk* c; chunk_coords_ coord; }; @@ -113,19 +113,19 @@ public: }; template -std::shared_ptr world::find_entity(object_id id) +std::shared_ptr world::find_object(object_id id) { - static_assert(std::is_same_v || std::is_base_of_v); - // make it a dependent name so that including "src/entity.hpp" isn't needed - using U = std::conditional_t, T, entity>; - if (std::shared_ptr ptr = find_entity_(id); !ptr) + static_assert(std::is_same_v || std::is_base_of_v); + // make it a dependent name so that including "src/object.hpp" isn't needed + using U = std::conditional_t, T, object>; + if (std::shared_ptr ptr = find_object_(id); !ptr) return {}; - else if constexpr(std::is_same_v) + else if constexpr(std::is_same_v) return ptr; else { - if (!(ptr->type() == entity_type_::value)) [[unlikely]] - throw_on_wrong_entity_type(id, ptr->type(), entity_type_::value); + if (!(ptr->type() == object_type_::value)) [[unlikely]] + throw_on_wrong_object_type(id, ptr->type(), object_type_::value); return static_pointer_cast(Utility::move(ptr)); } } -- cgit v1.2.3