]> git.localhorst.tv Git - blank.git/commitdiff
check line of sight in chase ai
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 7 Aug 2015 14:49:24 +0000 (16:49 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 7 Aug 2015 14:49:24 +0000 (16:49 +0200)
src/ai/Chaser.hpp
src/ai/Spawner.cpp
src/ai/ai.cpp

index 6daaf3639934fe5d70e5609767d9d0530133eceb..43667c06d332686471f3036be79ce003dc1a017d 100644 (file)
@@ -6,11 +6,13 @@
 
 namespace blank {
 
+class World;
+
 class Chaser
 : public Controller {
 
 public:
-       Chaser(Entity &ctrl, Entity &tgt) noexcept;
+       Chaser(World &, Entity &ctrl, Entity &tgt) noexcept;
        ~Chaser();
 
        Entity &Target() noexcept { return tgt; }
@@ -19,8 +21,10 @@ public:
        void Update(int dt) override;
 
 private:
+       World &world;
        Entity &tgt;
-       float speed;
+       float chase_speed;
+       float flee_speed;
        float stop_dist;
        float flee_dist;
 
index c73bcde9a9584c2cf2c9500d8fc427653d17be11..612c3ccdadec7000ee427ce1003385f2013e0769 100644 (file)
@@ -111,7 +111,7 @@ void Spawner::Spawn(const glm::ivec3 &chunk, const glm::vec3 &pos) {
        if (rand() % 2) {
                ctrl = new RandomWalk(e);
        } else {
-               ctrl = new Chaser(e, world.Player());
+               ctrl = new Chaser(world, e, world.Player());
        }
        controllers.emplace_back(ctrl);
 }
index b3e1a0ceae5fb852c2f8fd653bfa2675cd193022..6b380e4d1027a8e7e2cc1cba11f5d1d24bb539e3 100644 (file)
@@ -2,19 +2,23 @@
 #include "Controller.hpp"
 #include "RandomWalk.hpp"
 
+#include "../model/geometry.hpp"
 #include "../world/Entity.hpp"
+#include "../world/World.hpp"
 
 #include <glm/glm.hpp>
 
 
 namespace blank {
 
-Chaser::Chaser(Entity &ctrl, Entity &tgt) noexcept
+Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept
 : Controller(ctrl)
+, world(world)
 , tgt(tgt)
-, speed(0.002f)
-, stop_dist(5 * 5)
-, flee_dist(3 * 3) {
+, chase_speed(0.002f)
+, flee_speed(-0.005f)
+, stop_dist(10)
+, flee_dist(5) {
 
 }
 
@@ -24,12 +28,26 @@ Chaser::~Chaser() {
 
 void Chaser::Update(int dt) {
        glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
-       float dist = dot (diff, diff);
-       // TODO: line of sight test
-       if (dist > stop_dist) {
-               Controlled().Velocity(normalize(diff) * speed);
+       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};
+       Chunk *chunk;
+       int blkid;
+       float distance;
+       glm::vec3 normal;
+       if (world.Intersection(aim, glm::mat4(1.0f), chunk, blkid, distance, normal)) {
+               line_of_sight = distance > dist;
+       }
+
+       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(normalize(diff) * -speed);
+               Controlled().Velocity(norm_diff * flee_speed);
        } else {
                Controlled().Velocity(glm::vec3(0.0f));
        }