]> git.localhorst.tv Git - blank.git/blobdiff - src/ai/ai.cpp
centralize entity controllers
[blank.git] / src / ai / ai.cpp
index 5f5474b5f3320030e2f7e902badbb86c58328a79..1d424cc218c73c9224f3d0abe6eea37a41bae06f 100644 (file)
-#include "Chaser.hpp"
-#include "Controller.hpp"
-#include "RandomWalk.hpp"
+#include "AIController.hpp"
 
 #include "../model/geometry.hpp"
+#include "../rand/GaloisLFSR.hpp"
 #include "../world/Entity.hpp"
 #include "../world/World.hpp"
 #include "../world/WorldCollision.hpp"
 
+#include <cmath>
+#include <limits>
 #include <glm/glm.hpp>
 
 
 namespace blank {
 
-Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept
-: Controller(ctrl)
-, world(world)
-, tgt(tgt)
-, chase_speed(0.002f)
-, flee_speed(-0.005f)
-, stop_dist(10)
-, flee_dist(5) {
+AIController::AIController(GaloisLFSR &rand)
+: random(rand)
+, chase_speed(2.0f)
+, flee_speed(-5.0f)
+, stop_dist(10.0f)
+, flee_dist(5.0f)
+, wander_pos(1.0f, 0.0f, 0.0f)
+, wander_dist(2.0f)
+, wander_radius(1.0f)
+, wander_disp(1.0f)
+, wander_speed(1.0f) {
 
 }
 
-Chaser::~Chaser() {
+AIController::~AIController() {
 
 }
 
-void Chaser::Update(int dt) {
-       glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
-       float dist = length(diff);
-       glm::vec3 norm_diff(diff / dist);
-
-       bool line_of_sight = true;
-       // FIXME: this only works if target is in the reference chunk (which is true for the player)
-       Ray aim{Target().Position() - diff, norm_diff};
-       WorldCollision coll;
-       if (world.Intersection(aim, glm::mat4(1.0f), coll)) {
-               line_of_sight = coll.depth > dist;
+void AIController::Update(Entity &e, float dt) {
+       // movement: for now, wander only
+       glm::vec3 displacement(
+               random.SNorm() * wander_disp,
+               random.SNorm() * wander_disp,
+               random.SNorm() * wander_disp
+       );
+       if (dot(displacement, displacement) > std::numeric_limits<float>::epsilon()) {
+               wander_pos = normalize(wander_pos + displacement * dt) * wander_radius;
        }
 
-       if (!line_of_sight) {
-               Controlled().Velocity(glm::vec3(0.0f));
-       } else if (dist > stop_dist) {
-               Controlled().Velocity(norm_diff * chase_speed);
-       } else if (dist < flee_dist) {
-               Controlled().Velocity(norm_diff * flee_speed);
-       } else {
-               Controlled().Velocity(glm::vec3(0.0f));
+       if (e.Moving()) {
+               // orient head towards heading
+               glm::vec3 heading(Heading(e.GetState()));
+               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);
        }
 }
 
-
-Controller::Controller(Entity &e) noexcept
-: entity(e) {
-
-}
-
-Controller::~Controller() {
-
+glm::vec3 AIController::ControlForce(const EntityState &state) const {
+       return (Heading(state) * wander_dist + wander_pos) * wander_speed;
 }
 
-
-RandomWalk::RandomWalk(Entity &e) noexcept
-: Controller(e)
-, time_left(0) {
-
-}
-
-RandomWalk::~RandomWalk() {
-
-}
-
-void RandomWalk::Update(int dt) {
-       time_left -= dt;
-       if (time_left > 0) return;
-       time_left += 2500 + (rand() % 5000);
-
-       constexpr float move_vel = 0.0005f;
-
-       glm::vec3 new_vel = Controlled().Velocity();
-
-       switch (rand() % 9) {
-               case 0:
-                       new_vel.x = -move_vel;
-                       break;
-               case 1:
-                       new_vel.x = 0.0f;
-                       break;
-               case 2:
-                       new_vel.x = move_vel;
-                       break;
-               case 3:
-                       new_vel.y = -move_vel;
-                       break;
-               case 4:
-                       new_vel.y = 0.0f;
-                       break;
-               case 5:
-                       new_vel.y = move_vel;
-                       break;
-               case 6:
-                       new_vel.z = -move_vel;
-                       break;
-               case 7:
-                       new_vel.z = 0.0f;
-                       break;
-               case 8:
-                       new_vel.z = move_vel;
-                       break;
+glm::vec3 AIController::Heading(const EntityState &state) noexcept {
+       if (dot(state.velocity, state.velocity) > std::numeric_limits<float>::epsilon()) {
+               return normalize(state.velocity);
+       } else {
+               float cp = std::cos(state.pitch);
+               return glm::vec3(std::cos(state.yaw) * cp, std::sin(state.yaw) * cp, std::sin(state.pitch));
        }
-
-       Controlled().Velocity(new_vel);
 }
 
 }