X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fai%2Fai.cpp;h=8cd4f1411abb1b9b21be99edb5e679a947c1c4e9;hb=cda510e480aba3702e95059163378822ccac8d17;hp=6b380e4d1027a8e7e2cc1cba11f5d1d24bb539e3;hpb=a34fbcb0581056bd464158acfa30289a3e2c2c2d;p=blank.git diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index 6b380e4..8cd4f14 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -5,6 +5,7 @@ #include "../model/geometry.hpp" #include "../world/Entity.hpp" #include "../world/World.hpp" +#include "../world/WorldCollision.hpp" #include @@ -19,27 +20,27 @@ Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept , flee_speed(-0.005f) , stop_dist(10) , flee_dist(5) { - + tgt.Ref(); } Chaser::~Chaser() { - + tgt.UnRef(); } void Chaser::Update(int dt) { glm::vec3 diff(Target().AbsoluteDifference(Controlled())); float dist = length(diff); + if (dist < std::numeric_limits::epsilon()) { + Controlled().Velocity(glm::vec3(0.0f)); + return; + } 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; + WorldCollision coll; + if (world.Intersection(aim, glm::mat4(1.0f), Target().ChunkCoords(), coll)) { + line_of_sight = coll.depth > dist; } if (!line_of_sight) { @@ -56,16 +57,17 @@ void Chaser::Update(int dt) { Controller::Controller(Entity &e) noexcept : entity(e) { - + entity.Ref(); } Controller::~Controller() { - + entity.UnRef(); } -RandomWalk::RandomWalk(Entity &e) noexcept +RandomWalk::RandomWalk(Entity &e, std::uint64_t seed) noexcept : Controller(e) +, random(seed) , time_left(0) { } @@ -77,13 +79,13 @@ RandomWalk::~RandomWalk() { void RandomWalk::Update(int dt) { time_left -= dt; if (time_left > 0) return; - time_left += 2500 + (rand() % 5000); + time_left += 2500 + (random.Next() % 5000); constexpr float move_vel = 0.0005f; glm::vec3 new_vel = Controlled().Velocity(); - switch (rand() % 9) { + switch (random.Next() % 9) { case 0: new_vel.x = -move_vel; break;