+void AIController::SetState(const AIState &s) {
+ state->Exit(*this);
+ state = &s;
+ state->Enter(*this);
+}
+
+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(e.Heading());
+ float tgt_pitch = std::atan(heading.y / length(glm::vec2(heading.x, heading.z)));
+ float tgt_yaw = std::atan2(-heading.x, -heading.z);
+ e.SetHead(tgt_pitch, tgt_yaw);
+ }
+}
+
+glm::vec3 AIController::ControlForce(const Entity &entity, const EntityState &state) const {
+ if (IsHalted()) {
+ return GetHaltForce(entity, state);
+ }
+ glm::vec3 force(0.0f);
+ if (IsFleeing()) {
+ if (MaxOutForce(force, GetFleeForce(entity, state), entity.MaxControlForce())) {
+ return force;
+ }
+ }
+ if (IsSeeking()) {
+ if (MaxOutForce(force, GetSeekForce(entity, state), entity.MaxControlForce())) {
+ return force;
+ }
+ }
+ if (IsEvading()) {
+ if (MaxOutForce(force, GetEvadeForce(entity, state), entity.MaxControlForce())) {
+ return force;
+ }
+ }
+ if (IsPursuing()) {
+ if (MaxOutForce(force, GetPursuitForce(entity, state), entity.MaxControlForce())) {
+ return force;
+ }
+ }
+ if (IsWandering()) {
+ if (MaxOutForce(force, GetWanderForce(entity, state), entity.MaxControlForce())) {
+ return force;
+ }
+ }
+ 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;
+ }
+
+ // LOS test, assumes all entities are see-through
+ WorldCollision col;
+ if (world.Intersection(aim, glm::mat4(1.0f), 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 = length(diff);
+ if (dist > sight_dist || dot(diff / dist, aim.dir) < sight_angle) {
+ return false;
+ }
+ WorldCollision col;
+ if (world.Intersection(aim, glm::mat4(1.0f), 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.Start();
+}
+
+bool AIController::DecisionDue() const noexcept {
+ return decision_timer.HitOnce();
+}
+
+unsigned int AIController::Decide(unsigned int num_choices) noexcept {
+ return random.Next<unsigned int>() % num_choices;
+}
+
+// halt
+
+void AIController::EnterHalt() noexcept {
+ halted = true;
+}
+
+void AIController::ExitHalt() noexcept {
+ halted = false;
+}
+
+bool AIController::IsHalted() const noexcept {
+ return halted;
+}
+
+void AIController::SetHaltSpeed(float speed) noexcept {
+ halt_speed = speed;
+}
+
+glm::vec3 AIController::GetHaltForce(const Entity &, const EntityState &state) const noexcept {
+ return Halt(state, halt_speed);
+}
+
+// flee
+
+void AIController::StartFleeing() noexcept {
+ fleeing = true;
+}
+
+void AIController::StopFleeing() noexcept {
+ fleeing = false;
+ if (flee_target) {
+ flee_target->UnRef();
+ flee_target = nullptr;
+ }
+}
+
+bool AIController::IsFleeing() const noexcept {
+ return fleeing && flee_target;
+}
+
+void AIController::SetFleeTarget(Entity &e) noexcept {
+ if (flee_target) {
+ flee_target->UnRef();
+ }
+ flee_target = &e;
+ flee_target->Ref();
+}
+
+void AIController::SetFleeSpeed(float speed) noexcept {
+ flee_speed = speed;
+}
+
+Entity &AIController::GetFleeTarget() noexcept {
+ return *flee_target;
+}
+
+const Entity &AIController::GetFleeTarget() const noexcept {
+ return *flee_target;
+}
+
+glm::vec3 AIController::GetFleeForce(const Entity &, const EntityState &state) const noexcept {
+ return Flee(state, GetFleeTarget().GetState(), flee_speed, 2.0f);
+}
+
+// seek
+
+void AIController::StartSeeking() noexcept {
+ seeking = true;
+}
+
+void AIController::StopSeeking() noexcept {
+ seeking = false;
+ if (seek_target) {
+ seek_target->UnRef();
+ seek_target = nullptr;
+ }
+}
+
+bool AIController::IsSeeking() const noexcept {
+ return seeking && seek_target;
+}
+
+void AIController::SetSeekTarget(Entity &e) noexcept {
+ if (seek_target) {
+ seek_target->UnRef();