From b4e622daa5514799385393252a7cd7d00902777f Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Mon, 3 Aug 2026 21:06:06 +0200 Subject: [PATCH] extract the shared Centroid helper into ai/Centroid.h --- src/lib/ecs/system/CMakeLists.txt | 1 + src/lib/ecs/system/ai/AdvanceExecutor.cpp | 23 +------------------- src/lib/ecs/system/ai/Centroid.h | 26 +++++++++++++++++++++++ src/lib/ecs/system/ai/StandbyExecutor.cpp | 23 +------------------- 4 files changed, 29 insertions(+), 44 deletions(-) create mode 100644 src/lib/ecs/system/ai/Centroid.h diff --git a/src/lib/ecs/system/CMakeLists.txt b/src/lib/ecs/system/CMakeLists.txt index f127656..ed99ca3 100644 --- a/src/lib/ecs/system/CMakeLists.txt +++ b/src/lib/ecs/system/CMakeLists.txt @@ -5,6 +5,7 @@ SET(HDRS ${CMAKE_CURRENT_SOURCE_DIR}/ai/AttackEvaluator.h ${CMAKE_CURRENT_SOURCE_DIR}/ai/AttackExecutor.h ${CMAKE_CURRENT_SOURCE_DIR}/ai/BehaviorTargeting.h + ${CMAKE_CURRENT_SOURCE_DIR}/ai/Centroid.h ${CMAKE_CURRENT_SOURCE_DIR}/ai/DeliverScrapEvaluator.h ${CMAKE_CURRENT_SOURCE_DIR}/ai/DeliverScrapExecutor.h ${CMAKE_CURRENT_SOURCE_DIR}/ai/RallyEvaluator.h diff --git a/src/lib/ecs/system/ai/AdvanceExecutor.cpp b/src/lib/ecs/system/ai/AdvanceExecutor.cpp index 0c6095b..32da3c0 100644 --- a/src/lib/ecs/system/ai/AdvanceExecutor.cpp +++ b/src/lib/ecs/system/ai/AdvanceExecutor.cpp @@ -6,6 +6,7 @@ #include "AdvanceBehavior.h" #include "BehaviorKind.h" +#include "Centroid.h" #include "EntityAdmin.h" #include "FactionComponent.h" #include "HealthComponent.h" @@ -16,28 +17,6 @@ #include "StationBodyComponent.h" #include "tracing.h" -namespace -{ -// Accumulates positions to produce their centroid (the center between them). -struct Centroid -{ - QVector2D sum; - int count = 0; - - void add(const QVector2D& point) - { - sum += point; - count += 1; - } - - std::optional value() const - { - if (count == 0) { return std::nullopt; } - return sum / static_cast(count); - } -}; -} - void AdvanceExecutor::execute(EntityAdmin& admin) { TRACE(); diff --git a/src/lib/ecs/system/ai/Centroid.h b/src/lib/ecs/system/ai/Centroid.h new file mode 100644 index 0000000..3396da8 --- /dev/null +++ b/src/lib/ecs/system/ai/Centroid.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include + +// Accumulates positions to produce their centroid (the center between them). +// Shared by the behavior executors that steer toward the middle of a group of +// entities (AdvanceExecutor: defence stations; StandbyExecutor: friendly ships). +struct Centroid +{ + QVector2D sum; + int count = 0; + + void add(const QVector2D& point) + { + sum += point; + count += 1; + } + + std::optional value() const + { + if (count == 0) { return std::nullopt; } + return sum / static_cast(count); + } +}; diff --git a/src/lib/ecs/system/ai/StandbyExecutor.cpp b/src/lib/ecs/system/ai/StandbyExecutor.cpp index e29b11d..eff674c 100644 --- a/src/lib/ecs/system/ai/StandbyExecutor.cpp +++ b/src/lib/ecs/system/ai/StandbyExecutor.cpp @@ -5,6 +5,7 @@ #include #include "BehaviorKind.h" +#include "Centroid.h" #include "EntityAdmin.h" #include "FactionComponent.h" #include "HealthComponent.h" @@ -16,28 +17,6 @@ #include "StationBodyComponent.h" #include "tracing.h" -namespace -{ -// Accumulates positions to produce their centroid (the center between them). -struct Centroid -{ - QVector2D sum; - int count = 0; - - void add(const QVector2D& point) - { - sum += point; - count += 1; - } - - std::optional value() const - { - if (count == 0) { return std::nullopt; } - return sum / static_cast(count); - } -}; -} - void StandbyExecutor::execute(EntityAdmin& admin) { TRACE();