From 52b0bdaba5b38790d144bf1e800ea34be937ded0 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 4 May 2023 00:07:51 +0200 Subject: Refactored Actions to be owned by registry Registry is now also responsible for switching between the actions and each action is expected to have one system. Technically an std::variant could be used here, but let's wait a bit and see how this part evolves. --- kurator/src/inspect.cpp | 9 ++++++++ sim/include/kurator/sim/ai.h | 13 ++++++----- sim/src/BaseSimulation.cpp | 3 ++- sim/src/ai.cpp | 55 +++++++++++++------------------------------- sim/src/ai.h | 3 ++- 5 files changed, 36 insertions(+), 47 deletions(-) diff --git a/kurator/src/inspect.cpp b/kurator/src/inspect.cpp index ab52bb8..d49a917 100644 --- a/kurator/src/inspect.cpp +++ b/kurator/src/inspect.cpp @@ -54,6 +54,7 @@ InspectionWindow::show() inspect(*it); inspect(*it); inspect(*it); + inspect(*it); inspect(*it); inspect(*it); inspect(*it); @@ -184,6 +185,14 @@ inspect(entt::handle&, sim::AIShip& ai) template <> void +inspect(entt::handle&, sim::KeepAtRange& action) +{ + ImGui::InputDouble("Keep At Range", &action.distance, 0, 0, "%.1f"); +} + + +template <> +void inspect(entt::handle&, AIVisuals& visuals) { ImGui::Checkbox("Show Target", &visuals.show_target); diff --git a/sim/include/kurator/sim/ai.h b/sim/include/kurator/sim/ai.h index 0ffa1cc..4953d4f 100644 --- a/sim/include/kurator/sim/ai.h +++ b/sim/include/kurator/sim/ai.h @@ -1,7 +1,5 @@ #pragma once -#include - #include #include @@ -15,16 +13,19 @@ namespace sim { -using Action = std::function; - - struct AIShip { - Action action; engine::Point destination; entt::entity target = entt::null; }; +struct KeepAtRange +{ + double distance; + entt::entity target = entt::null; +}; + + } // namespace sim } // namespace kurator diff --git a/sim/src/BaseSimulation.cpp b/sim/src/BaseSimulation.cpp index 2731c67..ffd43f9 100644 --- a/sim/src/BaseSimulation.cpp +++ b/sim/src/BaseSimulation.cpp @@ -16,6 +16,7 @@ namespace sim BaseSimulation::BaseSimulation(State& ctx) { + setup_ai_components(ctx); manager.extend(ctx); } @@ -24,7 +25,7 @@ void BaseSimulation::operator()(State& ctx) { pick_random_targets(ctx, manager); - take_actions(ctx); + keep_at_range(ctx); FloatingMovement::update(ctx); update_turrets(ctx); shoot_at_targets(ctx); diff --git a/sim/src/ai.cpp b/sim/src/ai.cpp index 7a0f637..7b6f700 100644 --- a/sim/src/ai.cpp +++ b/sim/src/ai.cpp @@ -1,7 +1,6 @@ #include "ai.h" #include -#include #include @@ -21,37 +20,11 @@ namespace sim static double find_worst_range(State& ctx, entt::entity ship); -class KeepAtRange +void +setup_ai_components(State&) { -public: - KeepAtRange(State& ctx_, AIShip& ai_, entt::entity self_, entt::entity target_, double distance_) : - ctx {ctx_}, - ai {ai_}, - self {std::move(self_)}, - target {std::move(target_)}, - distance {distance_} - { - } - - void operator()() - { - if (!ctx.registry.valid(target)) { - if (ctx.registry.valid(ai.target)) - ai.action = KeepAtRange(ctx, ai, self, ai.target, distance); - return; - } - const auto here = ctx.registry.get(self).position; - const auto there = ctx.registry.get(target).position; - const auto offset = there - here; - ai.destination = there - offset.normalized().scale(distance); - } -private: - State& ctx; - AIShip& ai; - entt::entity self; - entt::entity target; - double distance; -}; + // e.g., remove other actions if new one is inserted. +} void @@ -59,21 +32,25 @@ pick_random_targets(State& ctx, TeamManager& manager) { auto view = ctx.registry.view(); for (auto&& [entity, team, ai] : view.each()) { - if (!ctx.registry.valid(ai.target)) + if (!ctx.registry.valid(ai.target)) { ai.target = manager.random(team.id); - if (!ai.action) - ai.action = KeepAtRange(ctx, ai, entity, ai.target, find_worst_range(ctx, entity)); + ctx.registry.emplace_or_replace(entity, find_worst_range(ctx, entity), ai.target); + } } } void -take_actions(State& ctx) +keep_at_range(State& ctx) { - auto view = ctx.registry.view(); - for (auto&& [_, ai] : view.each()) { - if (ai.action) - ai.action(); + auto view = ctx.registry.view(); + for (auto&& [entity, ai, action] : view.each()) { + if (!ctx.registry.valid(action.target)) + continue; + const auto here = ctx.registry.get(entity).position; + const auto there = ctx.registry.get(action.target).position; + const auto offset = there - here; + ai.destination = there - offset.normalized().scale(action.distance); } } diff --git a/sim/src/ai.h b/sim/src/ai.h index d1e9c29..e306be1 100644 --- a/sim/src/ai.h +++ b/sim/src/ai.h @@ -12,8 +12,9 @@ namespace sim { +void setup_ai_components(State& ctx); void pick_random_targets(State& ctx, TeamManager& manager); -void take_actions(State& ctx); +void keep_at_range(State& ctx); void shoot_at_targets(State& ctx); -- cgit v1.1