diff --git a/include/Cubed/gameplay/server_entity_manager.hpp b/include/Cubed/gameplay/server_entity_manager.hpp index 7bfb7dd..38dddad 100644 --- a/include/Cubed/gameplay/server_entity_manager.hpp +++ b/include/Cubed/gameplay/server_entity_manager.hpp @@ -44,9 +44,10 @@ private: void handle_entity_destory(EntityID id); void handle_task(); void send_all_entities(std::shared_ptr& session); - void update_ai(); - void update_move(); - void update_send(); + void update_ai(entt::entity e); + void update_move(entt::entity e); + void update_send(entt::entity e, + std::span> sessions); template EntityID create_entity_in_factory(Args&&... args) { auto entity = m_registry.create(); diff --git a/include/Cubed/gameplay/server_world.hpp b/include/Cubed/gameplay/server_world.hpp index d229e1f..6b46be4 100644 --- a/include/Cubed/gameplay/server_world.hpp +++ b/include/Cubed/gameplay/server_world.hpp @@ -94,6 +94,8 @@ public: std::vector> get_all_session() const; + uint32_t get_chunk_ref_count(const glm::vec3& pos) const; + int get_block(const glm::ivec3& block_pos) const override; bool is_solid(const glm::ivec3& block_pos) const override; bool can_pass_block(const glm::ivec3& block_pos) const override; diff --git a/include/Cubed/gameplay/systems/physical_system.hpp b/include/Cubed/gameplay/systems/physical_system.hpp index f2e1dfd..5a57406 100644 --- a/include/Cubed/gameplay/systems/physical_system.hpp +++ b/include/Cubed/gameplay/systems/physical_system.hpp @@ -8,7 +8,8 @@ class PhysicalSystem { public: static glm::vec3 get_move_distance(const TickVelocity& v); - static void update(ServerWorld& world, entt::registry& registry); + static void update(ServerWorld& world, entt::registry& registry, + entt::entity e); private: }; diff --git a/include/Cubed/gameplay/systems/speed_system.hpp b/include/Cubed/gameplay/systems/speed_system.hpp index 6ec5e02..090b5e4 100644 --- a/include/Cubed/gameplay/systems/speed_system.hpp +++ b/include/Cubed/gameplay/systems/speed_system.hpp @@ -4,7 +4,7 @@ namespace Cubed { class SpeedSystem { public: - static void update(float dt, entt::registry& registry); + static void update(float dt, entt::registry& registry, entt::entity e); private: }; diff --git a/include/Cubed/gameplay/systems/wander_ai_system.hpp b/include/Cubed/gameplay/systems/wander_ai_system.hpp index 934a62a..24c7f12 100644 --- a/include/Cubed/gameplay/systems/wander_ai_system.hpp +++ b/include/Cubed/gameplay/systems/wander_ai_system.hpp @@ -6,7 +6,7 @@ namespace Cubed { class WanderAISystem { public: - static void update(entt::registry& registry); + static void update(entt::registry& registry, entt::entity e); private: static void do_ai(BaseServerCreature& creature, MoveBoost& move_boost); diff --git a/src/gameplay/server_entity_manager.cpp b/src/gameplay/server_entity_manager.cpp index 6a2b082..7837ff4 100644 --- a/src/gameplay/server_entity_manager.cpp +++ b/src/gameplay/server_entity_manager.cpp @@ -31,37 +31,50 @@ void ServerEntityManager::init() { } void ServerEntityManager::update() { handle_task(); - update_ai(); - update_move(); - update_send(); -} - -void ServerEntityManager::update_ai() { WanderAISystem::update(m_registry); } - -void ServerEntityManager::update_move() { - SpeedSystem::update( - static_cast(m_world.get_per_tick_time()) / 1000.0f, m_registry); - PhysicalSystem::update(m_world, m_registry); -} - -void ServerEntityManager::update_send() { + auto view = m_registry.view(); auto sessions = m_world.get_all_session(); - auto view = m_registry.view(); - - for (auto& e : view) { - auto [entity, creature] = view.get(e); - Arena arena; - auto* p = Arena::Create(&arena); - p->set_id(entity.id); - Tools::set_net_pos(p, creature.transform.position.value); - - Tools::set_net_vec3(p->mutable_direction(), - creature.transform.direction.value); - - for (auto& s : sessions) { - s->send(make_packet(p)); + for (auto e : view) { + const auto& c = view.get(e); + if (!m_world.get_chunk_ref_count(c.transform.position.value)) { + continue; } + update_ai(e); + update_move(e); + update_send(e, sessions); + } +} + +void ServerEntityManager::update_ai(entt::entity e) { + WanderAISystem::update(m_registry, e); +} + +void ServerEntityManager::update_move(entt::entity e) { + SpeedSystem::update(static_cast(m_world.get_per_tick_time()) / + 1000.0f, + m_registry, e); + PhysicalSystem::update(m_world, m_registry, e); +} + +void ServerEntityManager::update_send( + entt::entity e, std::span> sessions) { + + if (!m_registry.all_of(e)) { + return; + } + + const auto [entity, creature] = + m_registry.get(e); + Arena arena; + auto* p = Arena::Create(&arena); + p->set_id(entity.id); + Tools::set_net_pos(p, creature.transform.position.value); + + Tools::set_net_vec3(p->mutable_direction(), + creature.transform.direction.value); + + for (auto& s : sessions) { + s->send(make_packet(p)); } } diff --git a/src/gameplay/server_world.cpp b/src/gameplay/server_world.cpp index 9e93369..f53b847 100644 --- a/src/gameplay/server_world.cpp +++ b/src/gameplay/server_world.cpp @@ -1020,6 +1020,15 @@ std::vector> ServerWorld::get_all_session() const { return sessions; } +uint32_t ServerWorld::get_chunk_ref_count(const glm::vec3& pos) const { + ChunkPos p = get_chunk_pos(pos.x, pos.z); + chunk_cacc cacc; + if (!m_chunks.find(cacc, p)) { + return 0; + } + return cacc->second.ref_count; +} + int ServerWorld::get_block(const glm::ivec3& block_pos) const { auto [chunk_x, chunk_z] = get_chunk_pos(block_pos.x, block_pos.z); chunk_cacc cacc; diff --git a/src/gameplay/systems/physical_system.cpp b/src/gameplay/systems/physical_system.cpp index fc2eda5..f0a471a 100644 --- a/src/gameplay/systems/physical_system.cpp +++ b/src/gameplay/systems/physical_system.cpp @@ -102,61 +102,62 @@ bool update_z(const glm::vec3& pos, const glm::vec3& distance, } // namespace -void PhysicalSystem::update(ServerWorld& world, entt::registry& registry) { +void PhysicalSystem::update(ServerWorld& world, entt::registry& registry, + entt::entity e) { - auto view = registry.view(); - for (auto e : view) { - auto& creature = view.get(e); - auto distance = get_move_distance(creature.velocity); - auto box = HitboxManager::hitbox(creature.hitbox); - auto& pos = creature.transform.position.value; - auto& v = creature.velocity; - bool ground = - !update_y(pos, glm::vec3{0.0f, -0.1f, 0.0f}, world, box.box); - bool stepped = false; - if (update_y(pos, distance, world, box.box)) { - pos.y += distance.y; + if (!registry.all_of(e)) { + return; + } - } else { - v.value.y = 0.0f; - } - if (update_x(pos, distance, world, box.box)) { - pos.x += distance.x; + auto& creature = registry.get(e); + auto distance = get_move_distance(creature.velocity); + auto box = HitboxManager::hitbox(creature.hitbox); + auto& pos = creature.transform.position.value; + auto& v = creature.velocity; + bool ground = !update_y(pos, glm::vec3{0.0f, -0.1f, 0.0f}, world, box.box); + bool stepped = false; + if (update_y(pos, distance, world, box.box)) { + pos.y += distance.y; - } else { - if (ground && !stepped) { + } else { + v.value.y = 0.0f; + } + if (update_x(pos, distance, world, box.box)) { + pos.x += distance.x; - glm::vec3 step_pos = pos + glm::vec3{0.0f, 1.0f, 0.0f}; - if (update_x(step_pos, distance, world, box.box)) { - pos.x += distance.x; - pos.y += 1.0f; - stepped = true; - } else { - v.value.x = 0.0f; - } + } else { + if (ground && !stepped) { + + glm::vec3 step_pos = pos + glm::vec3{0.0f, 1.0f, 0.0f}; + if (update_x(step_pos, distance, world, box.box)) { + pos.x += distance.x; + pos.y += 1.0f; + stepped = true; } else { v.value.x = 0.0f; } - } - - if (update_z(pos, distance, world, box.box)) { - pos.z += distance.z; - } else { - if (ground && !stepped) { + v.value.x = 0.0f; + } + } - glm::vec3 step_pos = pos + glm::vec3{0.0f, 1.0f, 0.0f}; - if (update_z(step_pos, distance, world, box.box)) { - pos.z += distance.z; - pos.y += 1.0f; - stepped = true; + if (update_z(pos, distance, world, box.box)) { + pos.z += distance.z; + + } else { + if (ground && !stepped) { + + glm::vec3 step_pos = pos + glm::vec3{0.0f, 1.0f, 0.0f}; + if (update_z(step_pos, distance, world, box.box)) { + pos.z += distance.z; + pos.y += 1.0f; + stepped = true; - } else { - v.value.z = 0.0f; - } } else { v.value.z = 0.0f; } + } else { + v.value.z = 0.0f; } } } diff --git a/src/gameplay/systems/speed_system.cpp b/src/gameplay/systems/speed_system.cpp index 68e1cb4..19854c0 100644 --- a/src/gameplay/systems/speed_system.cpp +++ b/src/gameplay/systems/speed_system.cpp @@ -5,38 +5,38 @@ namespace Cubed { -void SpeedSystem::update(float dt, entt::registry& registry) { - auto view = registry.view(); - - for (auto e : view) { - auto [creature, moveboost] = view.get(e); - auto& v = creature.velocity.value; - if (moveboost.count <= moveboost.duration) { - ++moveboost.count; - v += creature.transform.direction.value * - creature.movement.acceleration; - } else { - // Decelerated by friction in all directions - auto decay = [](float& c, float d) { - if (c > 0.0f) - c = std::max(0.0f, c - d); - else if (c < 0.0f) - c = std::min(0.0f, c + d); - }; - decay(v.x, creature.movement.deceleration); - decay(v.z, creature.movement.deceleration); - } - v.y += -creature.gravity.value * dt; - auto v_clamp = [](float& c, float max) { - if (max < 0.0f) { - return; //-1 = unlimited - } - c = std::clamp(c, -max, max); - }; - v_clamp(v.x, creature.velocity.max.x); - v_clamp(v.y, creature.velocity.max.y); - v_clamp(v.z, creature.velocity.max.z); +void SpeedSystem::update(float dt, entt::registry& registry, entt::entity e) { + if (!registry.all_of(e)) { + return; } + + auto [creature, moveboost] = registry.get(e); + auto& v = creature.velocity.value; + if (moveboost.count <= moveboost.duration) { + ++moveboost.count; + v += + creature.transform.direction.value * creature.movement.acceleration; + } else { + // Decelerated by friction in all directions + auto decay = [](float& c, float d) { + if (c > 0.0f) + c = std::max(0.0f, c - d); + else if (c < 0.0f) + c = std::min(0.0f, c + d); + }; + decay(v.x, creature.movement.deceleration); + decay(v.z, creature.movement.deceleration); + } + v.y += -creature.gravity.value * dt; + auto v_clamp = [](float& c, float max) { + if (max < 0.0f) { + return; //-1 = unlimited + } + c = std::clamp(c, -max, max); + }; + v_clamp(v.x, creature.velocity.max.x); + v_clamp(v.y, creature.velocity.max.y); + v_clamp(v.z, creature.velocity.max.z); } } // namespace Cubed \ No newline at end of file diff --git a/src/gameplay/systems/wander_ai_system.cpp b/src/gameplay/systems/wander_ai_system.cpp index 3a4e570..ba79665 100644 --- a/src/gameplay/systems/wander_ai_system.cpp +++ b/src/gameplay/systems/wander_ai_system.cpp @@ -10,18 +10,18 @@ constexpr double MOVE_PROBABILITY = 0.01; } // namespace namespace Cubed { -void WanderAISystem::update(entt::registry& registry) { - auto view = - registry.view(); +void WanderAISystem::update(entt::registry& registry, entt::entity e) { + if (!registry.all_of( + e)) { + return; + } - for (auto e : view) { - auto [ai, creature, move_boost] = - view.get(e); - ++ai.count; - if (ai.count >= ai.interval) { - ai.count = 0; - do_ai(creature, move_boost); - } + auto [ai, creature, move_boost] = + registry.get(e); + ++ai.count; + if (ai.count >= ai.interval) { + ai.count = 0; + do_ai(creature, move_boost); } }