refactor AI system
This commit is contained in:
58
src/lib/ecs/system/ai/RepairEvaluator.cpp
Normal file
58
src/lib/ecs/system/ai/RepairEvaluator.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "RepairEvaluator.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "BehaviorScores.h"
|
||||
#include "BehaviorTargeting.h"
|
||||
#include "EntityAdmin.h"
|
||||
#include "HealthComponent.h"
|
||||
#include "PositionComponent.h"
|
||||
#include "RepairBehavior.h"
|
||||
#include "SensorRangeComponent.h"
|
||||
#include "tracing.h"
|
||||
|
||||
void RepairEvaluator::evaluate(EntityAdmin& admin)
|
||||
{
|
||||
TRACE();
|
||||
const std::vector<RepairableInfo> repairables = buildRepairables(admin);
|
||||
|
||||
admin.forEach<RepairBehavior, PositionComponent, SensorRangeComponent>(
|
||||
[&](entt::entity e, RepairBehavior& repair, const PositionComponent& pos,
|
||||
const SensorRangeComponent& sensor)
|
||||
{
|
||||
// Validate current target: alive and still damaged.
|
||||
bool targetValid = false;
|
||||
if (repair.currentTarget)
|
||||
{
|
||||
const entt::entity t = *repair.currentTarget;
|
||||
if (admin.isValid(t) && admin.hasAll<HealthComponent>(t))
|
||||
{
|
||||
const HealthComponent& th = admin.get<HealthComponent>(t);
|
||||
if (th.hp > 0.0f && th.hp < th.maxHp) { targetValid = true; }
|
||||
}
|
||||
}
|
||||
|
||||
// Acquire nearest damaged friendly within sensor range.
|
||||
if (!targetValid)
|
||||
{
|
||||
repair.currentTarget = std::nullopt;
|
||||
float bestDist = sensor.value_tiles;
|
||||
for (const RepairableInfo& r : repairables)
|
||||
{
|
||||
if (r.entity == e) { continue; }
|
||||
if (r.isEnemy) { continue; }
|
||||
if (r.hp <= 0.0f || r.hp >= r.maxHp) { continue; }
|
||||
const float dist = (r.position - pos.value).length();
|
||||
if (dist < bestDist)
|
||||
{
|
||||
bestDist = dist;
|
||||
repair.currentTarget = r.entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
repair.score = repair.currentTarget
|
||||
? BehaviorScores::kRepair
|
||||
: BehaviorScores::kInactive;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user