#include "AIController.hpp"
+#include "ChaseState.hpp"
+#include "FleeState.hpp"
#include "IdleState.hpp"
#include "RoamState.hpp"
-#include "../model/geometry.hpp"
+#include "../geometry/distance.hpp"
+#include "../geometry/rotation.hpp"
#include "../rand/GaloisLFSR.hpp"
#include "../world/Entity.hpp"
#include "../world/World.hpp"
namespace {
+ChaseState chase;
+FleeState flee;
IdleState idle;
RoamState roam;
}
-AIController::AIController(GaloisLFSR &rand)
-: random(rand)
+AIController::AIController(World &world, Entity &entity)
+: world(world)
, state(&idle)
-, decision_timer(1.0f)
-, halted(false)
-, halt_speed(1.0f)
-, flee_target(nullptr)
-, flee_speed(5.0f)
-, seek_target(nullptr)
-, seek_speed(5.0f)
-, wandering(false)
-, wander_pos(1.0f, 0.0f, 0.0f)
-, wander_speed(1.0f)
-, wander_dist(2.0f)
-, wander_radius(1.5f)
-, wander_disp(1.0f) {
- state->Enter(*this);
+, sight_dist(64.0f)
+, sight_angle(0.707f)
+, think_timer(0.5f)
+, decision_timer(1.0f) {
+ think_timer.Start();
+ state->Enter(*this, entity);
}
AIController::~AIController() {
- state->Exit(*this);
+ // ignore this for now
+ // state->Exit(*this, entity);
}
-void AIController::SetState(const AIState &s) {
- state->Exit(*this);
+void AIController::SetState(const AIState &s, Entity &entity) {
+ state->Exit(*this, entity);
state = &s;
- state->Enter(*this);
+ state->Enter(*this, entity);
}
void AIController::Update(Entity &e, float dt) {
+ think_timer.Update(dt);
decision_timer.Update(dt);
state->Update(*this, e, dt);
- if (wandering) {
- glm::vec3 displacement(
- random.SNorm() * wander_disp,
- random.SNorm() * wander_disp,
- random.SNorm() * wander_disp
- );
- if (!iszero(displacement)) {
- wander_pos = normalize(wander_pos + displacement * dt) * wander_radius;
- }
- }
-
if (e.Moving()) {
// orient head towards heading
- glm::vec3 heading(Heading(e.GetState()));
- float tgt_pitch = std::atan(heading.y / length(glm::vec2(heading.x, heading.z)));
- float tgt_yaw = std::atan2(-heading.x, -heading.z);
+ glm::vec3 heading(e.Heading());
+ // only half pitch, so we don't crane our neck
+ float tgt_pitch = std::atan(heading.y / length(glm::vec2(heading.x, heading.z))) * 0.5f;
+ // always look straight ahead
+ // maybe look at the pursuit target if there is one
+ float tgt_yaw = 0.0f;
e.SetHead(tgt_pitch, tgt_yaw);
+ e.OrientBody(dt);
}
}
-glm::vec3 AIController::ControlForce(const Entity &entity, const EntityState &state) const {
- if (IsHalted()) {
- return Halt(state, halt_speed);
- }
- glm::vec3 force(0.0f);
- if (IsFleeing()) {
- glm::vec3 diff(GetFleeTarget().GetState().Diff(state));
- if (MaxOutForce(force, TargetVelocity(normalize(diff) * flee_speed, state, 0.5f), entity.MaxControlForce())) {
- return force;
- }
- }
- if (IsSeeking()) {
- glm::vec3 diff(state.Diff(GetSeekTarget().GetState()));
- if (MaxOutForce(force, TargetVelocity(normalize(diff) * seek_speed, state, 0.5f), entity.MaxControlForce())) {
- return force;
+Player *AIController::ClosestVisiblePlayer(const Entity &e) noexcept {
+ Player *target = nullptr;
+ float distance = sight_dist;
+ const glm::ivec3 &reference(e.ChunkCoords());
+ Ray aim(e.Aim(reference));
+ for (Player &p : world.Players()) {
+ const Entity &pe = p.GetEntity();
+
+ // distance test
+ const glm::vec3 diff(pe.AbsoluteDifference(e));
+ float dist = length(diff);
+ if (dist > distance) continue;
+
+ // FOV test, 45° in each direction
+ if (dot(diff / dist, aim.dir) < sight_angle) {
+ continue;
}
- }
- if (IsWandering()) {
- glm::vec3 wander_target(normalize(Heading(state) * wander_dist + wander_pos) * wander_speed);
- if (MaxOutForce(force, TargetVelocity(wander_target, state, 2.0f), entity.MaxControlForce())) {
- return force;
+
+ // LOS test, assumes all entities are see-through
+ WorldCollision col;
+ if (world.Intersection(aim, reference, col) && col.depth < dist) {
+ continue;
}
+
+ // we got a match
+ target = &p;
+ distance = dist;
}
- return force;
+ return target;
}
-glm::vec3 AIController::Heading(const EntityState &state) noexcept {
- if (dot(state.velocity, state.velocity) > std::numeric_limits<float>::epsilon()) {
- return normalize(state.velocity);
- } else {
- float cp = std::cos(state.pitch);
- return glm::vec3(std::cos(state.yaw) * cp, std::sin(state.yaw) * cp, std::sin(state.pitch));
+bool AIController::LineOfSight(const Entity &from, const Entity &to) const noexcept {
+ const glm::ivec3 &reference(from.ChunkCoords());
+ Ray aim(from.Aim(reference));
+ const glm::vec3 diff(to.AbsoluteDifference(from));
+ float dist = length(diff);
+ if (dist > sight_dist || dot(diff / dist, aim.dir) < sight_angle) {
+ return false;
+ }
+ WorldCollision col;
+ if (world.Intersection(aim, reference, col) && col.depth < dist) {
+ return false;
}
+ return true;
}
+// think
+
+bool AIController::MayThink() const noexcept {
+ return think_timer.Hit();
+}
+
+void AIController::SetThinkInterval(float i) noexcept {
+ think_timer = FineTimer(i);
+ think_timer.Start();
+}
+
+// decide
+
void AIController::CueDecision(
float minimum,
float variance
) noexcept {
- decision_timer = FineTimer(minimum + variance * random.SNorm());
+ decision_timer = FineTimer(minimum + variance * world.Random().SNorm());
decision_timer.Start();
}
}
unsigned int AIController::Decide(unsigned int num_choices) noexcept {
- return random.Next<unsigned int>() % num_choices;
+ return world.Random().Next<unsigned int>() % num_choices;
}
-void AIController::EnterHalt(float speed) noexcept {
- halted = true;
- halt_speed = speed;
-}
-void AIController::ExitHalt() noexcept {
- halted = false;
-}
+// chase
-bool AIController::IsHalted() const noexcept {
- return halted;
+void ChaseState::Enter(AIController &ctrl, Entity &e) const {
+ e.GetSteering()
+ .SetAcceleration(5.0f)
+ .SetSpeed(4.0f)
+ .Enable(Steering::PURSUE_TARGET)
+ ;
}
-void AIController::StartFleeing(const Entity &e, float speed) noexcept {
- flee_target = &e;
- flee_speed = speed;
-}
-
-void AIController::StopFleeing() noexcept {
- flee_target = nullptr;
-}
-
-bool AIController::IsFleeing() const noexcept {
- return flee_target;
-}
-
-const Entity &AIController::GetFleeTarget() const noexcept {
- return *flee_target;
-}
-
-void AIController::StartSeeking(const Entity &e, float speed) noexcept {
- seek_target = &e;
- seek_speed = speed;
+void ChaseState::Update(AIController &ctrl, Entity &e, float dt) const {
+ Steering &steering = e.GetSteering();
+ // check if target still alive and in sight
+ if (
+ !steering.HasTargetEntity() || // lost
+ steering.GetTargetEntity().Dead() || // dead
+ !ctrl.LineOfSight(e, steering.GetTargetEntity()) // escaped
+ ) {
+ steering.ClearTargetEntity();
+ ctrl.SetState(idle, e);
+ return;
+ }
+ // halt if we're close enough, flee if we're too close
+ float dist_sq = length2(e.AbsoluteDifference(steering.GetTargetEntity()));
+ if (dist_sq < 8.0f) {
+ ctrl.SetState(flee, e);
+ } else if (dist_sq < 25.0f) {
+ steering.Enable(Steering::HALT).Disable(Steering::PURSUE_TARGET);
+ } else {
+ steering.Enable(Steering::PURSUE_TARGET).Disable(Steering::HALT);
+ }
}
-void AIController::StopSeeking() noexcept {
- seek_target = nullptr;
+void ChaseState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::HALT | Steering::PURSUE_TARGET);
}
-bool AIController::IsSeeking() const noexcept {
- return seek_target;
-}
+// flee
-const Entity &AIController::GetSeekTarget() const noexcept {
- return *seek_target;
+void FleeState::Enter(AIController &ctrl, Entity &e) const {
+ e.GetSteering()
+ .SetAcceleration(5.0f)
+ .SetSpeed(4.0f)
+ .Enable(Steering::EVADE_TARGET)
+ ;
+ ctrl.CueDecision(6.0f, 3.0f);
}
-void AIController::StartWandering(
- float speed,
- float distance,
- float radius,
- float displacement
-) noexcept {
- wandering = true;
- wander_speed = speed;
- wander_dist = distance;
- wander_radius = radius;
- wander_disp = displacement;
-}
-
-void AIController::StopWandering() noexcept {
- wandering = false;
+void FleeState::Update(AIController &ctrl, Entity &e, float dt) const {
+ if (!ctrl.DecisionDue()) return;
+ ctrl.SetState(idle, e);
}
-bool AIController::IsWandering() const noexcept {
- return wandering;
+void FleeState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::EVADE_TARGET);
}
+// idle
-void IdleState::Enter(AIController &ctrl) const {
- ctrl.EnterHalt(2.0f);
- ctrl.StartWandering(0.001f, 1.1f);
+void IdleState::Enter(AIController &ctrl, Entity &e) const {
+ e.GetSteering()
+ .SetAcceleration(0.5f)
+ .SetSpeed(0.01f)
+ .Enable(Steering::HALT)
+ .SetWanderParams(1.0f, 1.1f, 1.0f)
+ ;
ctrl.CueDecision(10.0f, 5.0f);
}
void IdleState::Update(AIController &ctrl, Entity &e, float dt) const {
- // TODO: check for players
+ if (ctrl.MayThink()) {
+ const Player *player = ctrl.ClosestVisiblePlayer(e);
+ if (player) {
+ e.GetSteering().SetTargetEntity(player->GetEntity());
+ ctrl.SetState(chase, e);
+ return;
+ }
+ }
+
if (!ctrl.DecisionDue()) return;
unsigned int d = ctrl.Decide(10);
- if (d == 0) {
- // .1 chance to start going
- ctrl.SetState(roam);
- } else if (d < 3) {
- // .2 chance of looking around
- ctrl.ExitHalt();
+ if (d < 2) {
+ // .2 chance to start going
+ ctrl.SetState(roam, e);
+ } else if (d < 5) {
+ // .3 chance of looking around
+ e.GetSteering().Disable(Steering::HALT).Enable(Steering::WANDER);
} else {
- ctrl.EnterHalt(2.0f);
+ // .5 chance of doing nothing
+ e.GetSteering().Disable(Steering::WANDER).Enable(Steering::HALT);
}
ctrl.CueDecision(10.0f, 5.0f);
}
-void IdleState::Exit(AIController &ctrl) const {
- ctrl.ExitHalt();
- ctrl.StopWandering();
+void IdleState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::HALT | Steering::WANDER);
}
-void RoamState::Enter(AIController &ctrl) const {
- ctrl.StartWandering(1.0f);
+// roam
+
+void RoamState::Enter(AIController &ctrl, Entity &e) const {
+ e.GetSteering()
+ .SetAcceleration(0.5f)
+ .SetSpeed(1.0f)
+ .SetWanderParams(1.0f, 2.0f, 1.0f)
+ .Enable(Steering::WANDER)
+ ;
ctrl.CueDecision(10.0f, 5.0f);
}
void RoamState::Update(AIController &ctrl, Entity &e, float dt) const {
- // TODO: check for players
+ if (ctrl.MayThink()) {
+ const Player *player = ctrl.ClosestVisiblePlayer(e);
+ if (player) {
+ e.GetSteering().SetTargetEntity(player->GetEntity());
+ ctrl.SetState(chase, e);
+ return;
+ }
+ }
+
if (!ctrl.DecisionDue()) return;
unsigned int d = ctrl.Decide(10);
if (d == 0) {
// .1 chance of idling
- ctrl.SetState(idle);
+ ctrl.SetState(idle, e);
}
ctrl.CueDecision(10.0f, 5.0f);
}
-void RoamState::Exit(AIController &ctrl) const {
- ctrl.StopWandering();
+void RoamState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::WANDER);
}
}