-#include "Chaser.hpp"
-#include "Controller.hpp"
-#include "RandomWalk.hpp"
+#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 "../graphics/glm.hpp"
+#include "../rand/GaloisLFSR.hpp"
#include "../world/Entity.hpp"
#include "../world/World.hpp"
#include "../world/WorldCollision.hpp"
-#include <glm/glm.hpp>
+#include <cmath>
+#include <limits>
namespace blank {
-Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept
-: Controller(ctrl)
-, world(world)
-, tgt(tgt)
-, chase_speed(0.002f)
-, flee_speed(-0.005f)
-, stop_dist(10)
-, flee_dist(5) {
+namespace {
+ChaseState chase;
+FleeState flee;
+IdleState idle;
+RoamState roam;
+
+}
+
+AIController::AIController(World &world, Entity &entity)
+: world(world)
+, state(&idle)
+, sight_dist(64.0f)
+, sight_angle(0.707f)
+, think_timer(0.5f)
+, decision_timer(1.0f) {
+ think_timer.Start();
+ state->Enter(*this, entity);
}
-Chaser::~Chaser() {
+AIController::~AIController() {
+ // ignore this for now
+ // state->Exit(*this, entity);
+}
+void AIController::SetState(const AIState &s, Entity &entity) {
+ state->Exit(*this, entity);
+ state = &s;
+ state->Enter(*this, entity);
}
-void Chaser::Update(int dt) {
- glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
- float dist = length(diff);
- glm::vec3 norm_diff(diff / dist);
+void AIController::Update(Entity &e, float dt) {
+ think_timer.Update(dt);
+ decision_timer.Update(dt);
+ state->Update(*this, e, dt);
- bool line_of_sight = true;
- // FIXME: this only works if target is in the reference chunk (which is true for the player)
- Ray aim{Target().Position() - diff, norm_diff};
- WorldCollision coll;
- if (world.Intersection(aim, glm::mat4(1.0f), coll)) {
- line_of_sight = coll.depth > dist;
+ if (e.Moving()) {
+ // orient head towards heading
+ glm::vec3 heading(e.Heading());
+ // only half pitch, so we don't crane our neck
+ float tgt_pitch = std::atan(heading.y / glm::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);
}
+}
- if (!line_of_sight) {
- Controlled().Velocity(glm::vec3(0.0f));
- } else if (dist > stop_dist) {
- Controlled().Velocity(norm_diff * chase_speed);
- } else if (dist < flee_dist) {
- Controlled().Velocity(norm_diff * flee_speed);
- } else {
- Controlled().Velocity(glm::vec3(0.0f));
+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 = glm::length(diff);
+ if (dist > distance) continue;
+
+ // FOV test, 45° in each direction
+ if (glm::dot(diff / dist, aim.dir) < sight_angle) {
+ continue;
+ }
+
+ // 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 target;
+}
+
+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 = glm::length(diff);
+ if (dist > sight_dist || glm::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 * world.Random().SNorm());
+ decision_timer.Start();
+}
+
+bool AIController::DecisionDue() const noexcept {
+ return decision_timer.HitOnce();
+}
+
+unsigned int AIController::Decide(unsigned int num_choices) noexcept {
+ return world.Random().Next<unsigned int>() % num_choices;
}
-Controller::Controller(Entity &e) noexcept
-: entity(e) {
+// chase
+void ChaseState::Enter(AIController &ctrl, Entity &e) const {
+ e.GetSteering()
+ .SetAcceleration(5.0f)
+ .SetSpeed(4.0f)
+ .Enable(Steering::PURSUE_TARGET)
+ ;
}
-Controller::~Controller() {
+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 = glm::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 ChaseState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::HALT | Steering::PURSUE_TARGET);
}
+// flee
+
+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);
+}
-RandomWalk::RandomWalk(Entity &e) noexcept
-: Controller(e)
-, time_left(0) {
+void FleeState::Update(AIController &ctrl, Entity &e, float dt) const {
+ if (!ctrl.DecisionDue()) return;
+ ctrl.SetState(idle, e);
+}
+void FleeState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::EVADE_TARGET);
}
-RandomWalk::~RandomWalk() {
+// idle
+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 RandomWalk::Update(int dt) {
- time_left -= dt;
- if (time_left > 0) return;
- time_left += 2500 + (rand() % 5000);
+void IdleState::Update(AIController &ctrl, Entity &e, float dt) const {
+ if (ctrl.MayThink()) {
+ const Player *player = ctrl.ClosestVisiblePlayer(e);
+ if (player) {
+ e.GetSteering().SetTargetEntity(player->GetEntity());
+ ctrl.SetState(chase, e);
+ return;
+ }
+ }
- constexpr float move_vel = 0.0005f;
+ if (!ctrl.DecisionDue()) return;
- glm::vec3 new_vel = Controlled().Velocity();
+ unsigned int d = ctrl.Decide(10);
+ 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 {
+ // .5 chance of doing nothing
+ e.GetSteering().Disable(Steering::WANDER).Enable(Steering::HALT);
+ }
+ ctrl.CueDecision(10.0f, 5.0f);
+}
+
+void IdleState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::HALT | Steering::WANDER);
+}
+
+// roam
- switch (rand() % 9) {
- case 0:
- new_vel.x = -move_vel;
- break;
- case 1:
- new_vel.x = 0.0f;
- break;
- case 2:
- new_vel.x = move_vel;
- break;
- case 3:
- new_vel.y = -move_vel;
- break;
- case 4:
- new_vel.y = 0.0f;
- break;
- case 5:
- new_vel.y = move_vel;
- break;
- case 6:
- new_vel.z = -move_vel;
- break;
- case 7:
- new_vel.z = 0.0f;
- break;
- case 8:
- new_vel.z = move_vel;
- break;
+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 {
+ if (ctrl.MayThink()) {
+ const Player *player = ctrl.ClosestVisiblePlayer(e);
+ if (player) {
+ e.GetSteering().SetTargetEntity(player->GetEntity());
+ ctrl.SetState(chase, e);
+ return;
+ }
}
- Controlled().Velocity(new_vel);
+ if (!ctrl.DecisionDue()) return;
+
+ unsigned int d = ctrl.Decide(10);
+ if (d == 0) {
+ // .1 chance of idling
+ ctrl.SetState(idle, e);
+ }
+ ctrl.CueDecision(10.0f, 5.0f);
+}
+
+void RoamState::Exit(AIController &ctrl, Entity &e) const {
+ e.GetSteering().Disable(Steering::WANDER);
}
}