summaryrefslogtreecommitdiff
path: root/sim/src/ai.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-04-23 12:54:14 +0200
committerAki <please@ignore.pl>2024-04-05 19:41:19 +0200
commit975339d07aeb193564221adff31b55a913d92a1e (patch)
tree957b37e922e494780874bbc8e495a1a5e412fb76 /sim/src/ai.cpp
parentbdfb27ba04528f213dcadc00afdcf1f35a61f6fc (diff)
downloadkurator-975339d07aeb193564221adff31b55a913d92a1e.zip
kurator-975339d07aeb193564221adff31b55a913d92a1e.tar.gz
kurator-975339d07aeb193564221adff31b55a913d92a1e.tar.bz2
Separated Turret from AI, shuffled their units
Diffstat (limited to 'sim/src/ai.cpp')
-rw-r--r--sim/src/ai.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/sim/src/ai.cpp b/sim/src/ai.cpp
new file mode 100644
index 0000000..1f2a4ec
--- /dev/null
+++ b/sim/src/ai.cpp
@@ -0,0 +1,60 @@
+#include "ai.h"
+
+#include <kurator/sim/components.h>
+#include <kurator/sim/State.h>
+#include <kurator/sim/weapons.h>
+
+#include "TeamManager.h"
+
+
+namespace kurator
+{
+namespace sim
+{
+
+
+void
+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))
+ ai.target = manager.random(team.id);
+ }
+}
+
+
+void
+keep_at_range(State& ctx)
+{
+ auto view = ctx.registry.view<Transform, AIShip>();
+ for (auto&& [entity, self, ai] : view.each()) {
+ if (!ctx.registry.valid(ai.target))
+ continue;
+ const auto target = ctx.registry.get<Transform>(ai.target);
+ const auto offset = target.position - self.position;
+ ai.destination = target.position - offset.normalized().scale(ai.keep_at_range);
+ }
+}
+
+
+void
+shoot_at_targets(State& ctx)
+{
+ auto view = ctx.registry.view<Turret>();
+ for (auto&& [entity, turret] : view.each()) {
+ if (!ctx.registry.all_of<AIShip, Transform>(turret.owner))
+ continue;
+ const auto& [state, transform] = ctx.registry.get<AIShip, Transform>(turret.owner);
+ if (!ctx.registry.valid(state.target))
+ continue;
+ const auto& target = ctx.registry.get<Transform>(state.target);
+ const auto distance = transform.position.distance(target.position);
+ if (distance <= turret.type.effective_range())
+ turret.shoot_at(ctx, state.target, distance); // passing distance here is wrong
+ }
+}
+
+
+} // namespace sim
+} // namespace kurator