]> git.localhorst.tv Git - blank.git/blobdiff - src/ai/ai.cpp
new turn style
[blank.git] / src / ai / ai.cpp
index ff39127151332dc544f23ae6784f719ec7d6bd6c..c47b4d3893de5a0483b86033ef16ad3f817f9644 100644 (file)
@@ -36,6 +36,9 @@ AIController::AIController(World &world, GaloisLFSR &rand)
 , decision_timer(1.0f)
 , halted(false)
 , halt_speed(1.0f)
+, avoid_obstacles(true)
+, obstacle_box{ glm::vec3(0.0f), glm::vec3(0.0f) }
+, obstacle_transform(1.0f)
 , fleeing(false)
 , flee_target(nullptr)
 , flee_speed(5.0f)
@@ -73,6 +76,16 @@ void AIController::Update(Entity &e, float 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().block_pos, 1.0f);
+       }
+
        if (wandering) {
                glm::vec3 displacement(
                        random.SNorm() * wander_disp,
@@ -87,9 +100,13 @@ void AIController::Update(Entity &e, float dt) {
        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);
+               // 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);
        }
 }
 
@@ -98,6 +115,11 @@ glm::vec3 AIController::ControlForce(const Entity &entity, const EntityState &st
                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;
@@ -223,6 +245,63 @@ glm::vec3 AIController::GetHaltForce(const Entity &, const EntityState &state) c
        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 {