refactor AI system
This commit is contained in:
56
src/lib/ecs/system/ai/RetreatEvaluator.cpp
Normal file
56
src/lib/ecs/system/ai/RetreatEvaluator.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "RetreatEvaluator.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <QVector2D>
|
||||
|
||||
#include "AttackBehavior.h"
|
||||
#include "BehaviorScores.h"
|
||||
#include "EntityAdmin.h"
|
||||
#include "FactionComponent.h"
|
||||
#include "HealthComponent.h"
|
||||
#include "PositionComponent.h"
|
||||
#include "RetreatBehavior.h"
|
||||
#include "SensorRangeComponent.h"
|
||||
#include "ShipIdentityComponent.h"
|
||||
#include "tracing.h"
|
||||
|
||||
void RetreatEvaluator::evaluate(EntityAdmin& admin)
|
||||
{
|
||||
TRACE();
|
||||
|
||||
// Snapshot enemy ship positions for threat detection.
|
||||
std::vector<QVector2D> enemyShips;
|
||||
admin.forEach<ShipIdentityComponent, PositionComponent, FactionComponent>(
|
||||
[&enemyShips](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
|
||||
const PositionComponent& pos, const FactionComponent& f)
|
||||
{
|
||||
if (f.isEnemy) { enemyShips.push_back(pos.value); }
|
||||
});
|
||||
|
||||
admin.forEach<RetreatBehavior, PositionComponent, HealthComponent, SensorRangeComponent>(
|
||||
[&](entt::entity e, RetreatBehavior& retreat, const PositionComponent& pos,
|
||||
const HealthComponent& health, const SensorRangeComponent& sensor)
|
||||
{
|
||||
const bool lowHp = (health.maxHp > 0.0f)
|
||||
&& (health.hp / health.maxHp < retreat.retreatHpFraction);
|
||||
|
||||
bool threatened = false;
|
||||
const bool hasWeapons = admin.hasAll<AttackBehavior>(e);
|
||||
if (!hasWeapons)
|
||||
{
|
||||
for (const QVector2D& enemy : enemyShips)
|
||||
{
|
||||
if ((enemy - pos.value).length() <= sensor.value_tiles)
|
||||
{
|
||||
threatened = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
retreat.score = (lowHp || threatened)
|
||||
? BehaviorScores::kRetreat
|
||||
: BehaviorScores::kInactive;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user