X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fai%2Fai.cpp;h=1d424cc218c73c9224f3d0abe6eea37a41bae06f;hb=150d065f431d665326fd8028748c48a74ad956bb;hp=a1abfdc3cf73444102ed63e6210a00b7e2566f1e;hpb=e57cc01be658357de5829fae655ae52309e83743;p=blank.git diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index a1abfdc..1d424cc 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -1,118 +1,67 @@ -#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 +#include #include 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) { - 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); +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) { - bool line_of_sight = true; - Ray aim{Target().Position() - diff, norm_diff}; - 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)); - } 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)); - } } +AIController::~AIController() { -Controller::Controller(Entity &e) noexcept -: entity(e) { - entity.Ref(); -} - -Controller::~Controller() { - entity.UnRef(); } +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::epsilon()) { + wander_pos = normalize(wander_pos + displacement * dt) * wander_radius; + } -RandomWalk::RandomWalk(Entity &e, std::uint64_t seed) noexcept -: Controller(e) -, random(seed) -, start_vel(e.Velocity()) -, target_vel(start_vel) -, start_rot(e.AngularVelocity()) -, target_rot(start_rot) -, switch_time(0) -, lerp_max(1.0f) -, lerp_time(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); + } } -RandomWalk::~RandomWalk() { - +glm::vec3 AIController::ControlForce(const EntityState &state) const { + return (Heading(state) * wander_dist + wander_pos) * wander_speed; } -void RandomWalk::Update(int dt) { - 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().Velocity(mix(target_vel, start_vel, a)); - Controlled().AngularVelocity(mix(target_rot, start_rot, a)); +glm::vec3 AIController::Heading(const EntityState &state) noexcept { + if (dot(state.velocity, state.velocity) > std::numeric_limits::epsilon()) { + return normalize(state.velocity); } else { - Controlled().Velocity(target_vel); - Controlled().AngularVelocity(target_rot); + float cp = std::cos(state.pitch); + return glm::vec3(std::cos(state.yaw) * cp, std::sin(state.yaw) * cp, std::sin(state.pitch)); } } -void RandomWalk::Change() noexcept { - start_vel = target_vel; - start_rot = target_rot; - - constexpr float base = 0.000001f; - - target_vel.x = base * (random.Next() % 1024); - target_vel.y = base * (random.Next() % 1024); - target_vel.z = base * (random.Next() % 1024); - - target_rot.x = base * (random.Next() % 1024); - target_rot.y = base * (random.Next() % 1024); - target_rot.z = base * (random.Next() % 1024); -} - }