+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);
+}
+
+// obstacle avoidance
+
+void AIController::StartAvoidingObstacles() noexcept {
+ avoid_obstacles = true;
+}
+
+void AIController::StopAvoidingObstacles() noexcept {
+ avoid_obstacles = false;
+}
+
+bool AIController::IsAvoidingObstacles() const noexcept {
+ return avoid_obstacles;
+}
+
+namespace {
+
+std::vector<WorldCollision> col;
+
+}
+
+glm::vec3 AIController::GetObstacleAvoidanceForce(const Entity &e, const EntityState &state) const noexcept {
+ if (!e.Moving()) {
+ return glm::vec3(0.0f);
+ }
+ col.clear();
+ if (!world.Intersection(obstacle_box, obstacle_transform, e.ChunkCoords(), col)) {
+ return glm::vec3(0.0f);
+ }
+ // find the nearest block
+ WorldCollision *nearest = nullptr;
+ glm::vec3 difference(0.0f);
+ float distance = std::numeric_limits<float>::infinity();
+ for (WorldCollision &c : col) {
+ // diff points from block to state
+ glm::vec3 diff = state.RelativePosition(c.ChunkPos()) - c.BlockCoords();
+ float dist = length_squared(diff);
+ if (dist < distance) {
+ nearest = &c;
+ difference = diff;
+ distance = dist;
+ }
+ }
+ if (!nearest) {
+ // intersection test lied to us
+ return glm::vec3(0.0f);
+ }
+ // and steer away from it
+ // to_go is the distance between our position and the
+ // point on the "velocity ray" closest to obstacle
+ float to_go = dot(difference, e.Heading());
+ // point is our future position if we keep going our way
+ glm::vec3 point(e.GetState().block_pos + e.Heading() * to_go);
+ // now steer away in the direction of (point - block)
+ // with a magniture proportional to speed/distance
+ return normalize(point - nearest->BlockCoords()) * (e.Speed() / std::sqrt(distance));
+}
+
+// flee
+
+void AIController::StartFleeing() noexcept {
+ fleeing = true;