+ float cp = std::cos(state.pitch);
+ return glm::vec3(std::cos(state.yaw) * cp, std::sin(state.yaw) * cp, std::sin(state.pitch));
+ }
+}
+
+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_squared(diff);
+ if (dist > distance) continue;
+
+ // FOV test, 45° in each direction
+ if (dot(normalize(diff), 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 * 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 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 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();
+ }
+ seek_target = &e;
+ seek_target->Ref();
+}
+
+void AIController::SetSeekSpeed(float speed) noexcept {
+ seek_speed = speed;
+}
+
+Entity &AIController::GetSeekTarget() noexcept {
+ return *seek_target;
+}
+
+const Entity &AIController::GetSeekTarget() const noexcept {
+ return *seek_target;
+}
+
+glm::vec3 AIController::GetSeekForce(const EntityState &state) const noexcept {
+ return Seek(state, GetSeekTarget().GetState(), seek_speed, 2.0f);
+}
+
+// evade
+
+void AIController::StartEvading() noexcept {
+ evading = true;
+}
+
+void AIController::StopEvading() noexcept {
+ evading = false;
+ if (evade_target) {
+ evade_target->UnRef();
+ evade_target = nullptr;
+ }
+}
+
+bool AIController::IsEvading() const noexcept {
+ return evading && evade_target;
+}
+
+void AIController::SetEvadeTarget(Entity &e) noexcept {
+ if (evade_target) {
+ evade_target->UnRef();
+ }
+ evade_target = &e;
+ evade_target->Ref();
+}
+
+void AIController::SetEvadeSpeed(float speed) noexcept {
+ evade_speed = speed;
+}
+
+Entity &AIController::GetEvadeTarget() noexcept {
+ return *evade_target;
+}
+
+const Entity &AIController::GetEvadeTarget() const noexcept {
+ return *evade_target;
+}
+
+glm::vec3 AIController::GetEvadeForce(const EntityState &state) const noexcept{
+ glm::vec3 cur_diff(state.Diff(GetEvadeTarget().GetState()));
+ float time_estimate = length(cur_diff) / evade_speed;
+ EntityState pred_state(GetEvadeTarget().GetState());
+ pred_state.block_pos += pred_state.velocity * time_estimate;
+ return Flee(state, pred_state, evade_speed, 2.0f);
+}
+
+// pursuit
+
+void AIController::StartPursuing() noexcept {
+ pursuing = true;
+}
+
+void AIController::StopPursuing() noexcept {
+ pursuing = false;
+ if (pursuit_target) {
+ pursuit_target->UnRef();
+ pursuit_target = nullptr;
+ }
+}
+
+bool AIController::IsPursuing() const noexcept {
+ return pursuing && pursuit_target;
+}
+
+void AIController::SetPursuitTarget(Entity &e) noexcept {
+ if (pursuit_target) {
+ pursuit_target->UnRef();