X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fai%2Fai.cpp;h=9ec9fecad2ab0542e72df3e82b199ab7bc7ce01a;hb=33b37e7242e4cbfa76e4a0d6e5bb54223b541162;hp=6b380e4d1027a8e7e2cc1cba11f5d1d24bb539e3;hpb=a34fbcb0581056bd464158acfa30289a3e2c2c2d;p=blank.git diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index 6b380e4..9ec9fec 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 @@ -15,58 +16,68 @@ Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept : Controller(ctrl) , world(world) , tgt(tgt) -, chase_speed(0.002f) -, flee_speed(-0.005f) +, chase_speed(2.0f) +, flee_speed(-5.0f) , stop_dist(10) , flee_dist(5) { - + tgt.Ref(); } Chaser::~Chaser() { - + tgt.UnRef(); } void Chaser::Update(int dt) { + if (Target().Dead()) { + Controlled().Kill(); + return; + } + glm::vec3 diff(Target().AbsoluteDifference(Controlled())); float dist = length(diff); + if (dist < std::numeric_limits::epsilon()) { + Controlled().TargetVelocity(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) { - Controlled().Velocity(glm::vec3(0.0f)); + Controlled().TargetVelocity(glm::vec3(0.0f)); } else if (dist > stop_dist) { - Controlled().Velocity(norm_diff * chase_speed); + Controlled().TargetVelocity(norm_diff * chase_speed); } else if (dist < flee_dist) { - Controlled().Velocity(norm_diff * flee_speed); + Controlled().TargetVelocity(norm_diff * flee_speed); } else { - Controlled().Velocity(glm::vec3(0.0f)); + Controlled().TargetVelocity(glm::vec3(0.0f)); } } 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) -, time_left(0) { +, random(seed) +, start_vel(e.Velocity()) +, target_vel(start_vel) +, switch_time(0) +, lerp_max(1.0f) +, lerp_time(0.0f) { } @@ -75,45 +86,29 @@ 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; + switch_time -= dt; + lerp_time -= dt; + if (switch_time < 0) { + switch_time += 2500 + (random.Next() % 5000); + lerp_max = 1500 + (random.Next() % 1000); + lerp_time = lerp_max; + Change(); + } else if (lerp_time > 0) { + float a = std::min(lerp_time / lerp_max, 1.0f); + Controlled().TargetVelocity(mix(target_vel, start_vel, a)); + } else { + Controlled().TargetVelocity(target_vel); } +} + +void RandomWalk::Change() noexcept { + start_vel = target_vel; + + constexpr float base = 0.001f; - Controlled().Velocity(new_vel); + target_vel.x = base * (random.Next() % 1024); + target_vel.y = base * (random.Next() % 1024); + target_vel.z = base * (random.Next() % 1024); } }