+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 (avoid_obstacles && e.Moving()) {
+ obstacle_box = e.Bounds();
+ obstacle_box.min.z = -e.Speed();
+ obstacle_box.max.x = 0.0f;
+ // our box is oriented for -Z velocity
+ obstacle_transform = glm::mat4(find_rotation(glm::vec3(0.0f, 0.0f, -1.0f), e.Heading()));
+ // and positioned relative to the entity's chunk
+ obstacle_transform[3] = glm::vec4(e.GetState().pos.block, 1.0f);
+ }
+
+ 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());
+ // 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 GetHaltForce(entity, state);
+ }
+ glm::vec3 force(0.0f);
+ if (IsAvoidingObstacles() && entity.Moving()) {
+ if (MaxOutForce(force, GetObstacleAvoidanceForce(entity, state), entity.MaxControlForce())) {
+ return force;
+ }
+ }
+ 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, 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, 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 = length2(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().pos.block + 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;
+}
+
+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;