summaryrefslogtreecommitdiff
path: root/sim/src/ai.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sim/src/ai.cpp')
-rw-r--r--sim/src/ai.cpp55
1 files changed, 16 insertions, 39 deletions
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 <algorithm>
-#include <utility>
#include <entt/entt.hpp>
@@ -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<Transform>(self).position;
- const auto there = ctx.registry.get<Transform>(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<Team, AIShip>();
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<KeepAtRange>(entity, find_worst_range(ctx, entity), ai.target);
+ }
}
}
void
-take_actions(State& ctx)
+keep_at_range(State& ctx)
{
- auto view = ctx.registry.view<AIShip>();
- for (auto&& [_, ai] : view.each()) {
- if (ai.action)
- ai.action();
+ auto view = ctx.registry.view<AIShip, KeepAtRange>();
+ for (auto&& [entity, ai, action] : view.each()) {
+ if (!ctx.registry.valid(action.target))
+ continue;
+ const auto here = ctx.registry.get<Transform>(entity).position;
+ const auto there = ctx.registry.get<Transform>(action.target).position;
+ const auto offset = there - here;
+ ai.destination = there - offset.normalized().scale(action.distance);
}
}