X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fai%2Fai.cpp;h=ef0fc23c65e7c311a732b73314f9bf7c5307e686;hb=dcd54cacda98c2c0f7cf0c7a9131fb858d8ee10a;hp=9ec9fecad2ab0542e72df3e82b199ab7bc7ce01a;hpb=808d9dbd3ab101c0ff10697e36ef2c45a23b6ef5;p=blank.git diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index 9ec9fec..ef0fc23 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -1,114 +1,281 @@ -#include "Chaser.hpp" -#include "Controller.hpp" -#include "RandomWalk.hpp" +#include "AIController.hpp" +#include "ChaseState.hpp" +#include "FleeState.hpp" +#include "IdleState.hpp" +#include "RoamState.hpp" -#include "../model/geometry.hpp" +#include "../geometry/distance.hpp" +#include "../geometry/rotation.hpp" +#include "../graphics/glm.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(2.0f) -, flee_speed(-5.0f) -, stop_dist(10) -, flee_dist(5) { - tgt.Ref(); +namespace { + +ChaseState chase; +FleeState flee; +IdleState idle; +RoamState roam; + } -Chaser::~Chaser() { - tgt.UnRef(); +AIController::AIController(World &world, Entity &entity) +: world(world) +, state(&idle) +, sight_dist(64.0f) +, sight_angle(0.707f) +, think_timer(0.5f) +, decision_timer(1.0f) { + think_timer.Start(); + state->Enter(*this, entity); } -void Chaser::Update(int dt) { - if (Target().Dead()) { - Controlled().Kill(); - return; +AIController::~AIController() { + // ignore this for now + // state->Exit(*this, entity); +} + +void AIController::SetState(const AIState &s, Entity &entity) { + state->Exit(*this, entity); + state = &s; + state->Enter(*this, entity); +} + +void AIController::Update(Entity &e, float dt) { + think_timer.Update(dt); + decision_timer.Update(dt); + state->Update(*this, e, dt); + + if (e.Moving()) { + // orient head towards heading + glm::vec3 heading(e.Heading()); + // only half pitch, so we don't crane our neck + float tgt_pitch = std::atan(heading.y / glm::length(glm::vec2(heading.x, heading.z))) * 0.5f; + // always look straight ahead + // maybe look at the pursuit target if there is one + float tgt_yaw = 0.0f; + e.SetHead(tgt_pitch, tgt_yaw); + e.OrientBody(dt); } +} - glm::vec3 diff(Target().AbsoluteDifference(Controlled())); - float dist = length(diff); - if (dist < std::numeric_limits::epsilon()) { - Controlled().TargetVelocity(glm::vec3(0.0f)); - return; +Player *AIController::ClosestVisiblePlayer(const Entity &e) noexcept { + Player *target = nullptr; + float distance = sight_dist; + const glm::ivec3 &reference(e.ChunkCoords()); + Ray aim(e.Aim(reference)); + for (Player &p : world.Players()) { + const Entity &pe = p.GetEntity(); + + // distance test + const glm::vec3 diff(pe.AbsoluteDifference(e)); + float dist = glm::length(diff); + if (dist > distance) continue; + + // FOV test, 45° in each direction + if (glm::dot(diff / dist, aim.dir) < sight_angle) { + continue; + } + + // LOS test, assumes all entities are see-through + WorldCollision col; + if (world.Intersection(aim, reference, col) && col.depth < dist) { + continue; + } + + // we got a match + target = &p; + distance = dist; } - glm::vec3 norm_diff(diff / dist); + return target; +} - 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; +bool AIController::LineOfSight(const Entity &from, const Entity &to) const noexcept { + const glm::ivec3 &reference(from.ChunkCoords()); + Ray aim(from.Aim(reference)); + const glm::vec3 diff(to.AbsoluteDifference(from)); + float dist = glm::length(diff); + if (dist > sight_dist || glm::dot(diff / dist, aim.dir) < sight_angle) { + return false; + } + WorldCollision col; + if (world.Intersection(aim, reference, col) && col.depth < dist) { + return false; } + return true; +} + +// think + +bool AIController::MayThink() const noexcept { + return think_timer.Hit(); +} + +void AIController::SetThinkInterval(float i) noexcept { + think_timer = FineTimer(i); + think_timer.Start(); +} + +// decide + +void AIController::CueDecision( + float minimum, + float variance +) noexcept { + decision_timer = FineTimer(minimum + variance * world.Random().SNorm()); + decision_timer.Start(); +} - if (!line_of_sight) { - Controlled().TargetVelocity(glm::vec3(0.0f)); - } else if (dist > stop_dist) { - Controlled().TargetVelocity(norm_diff * chase_speed); - } else if (dist < flee_dist) { - Controlled().TargetVelocity(norm_diff * flee_speed); +bool AIController::DecisionDue() const noexcept { + return decision_timer.HitOnce(); +} + +unsigned int AIController::Decide(unsigned int num_choices) noexcept { + return world.Random().Next() % num_choices; +} + + +// chase + +void ChaseState::Enter(AIController &ctrl, Entity &e) const { + e.GetSteering() + .SetAcceleration(5.0f) + .SetSpeed(4.0f) + .Enable(Steering::PURSUE_TARGET) + ; +} + +void ChaseState::Update(AIController &ctrl, Entity &e, float dt) const { + Steering &steering = e.GetSteering(); + // check if target still alive and in sight + if ( + !steering.HasTargetEntity() || // lost + steering.GetTargetEntity().Dead() || // dead + !ctrl.LineOfSight(e, steering.GetTargetEntity()) // escaped + ) { + steering.ClearTargetEntity(); + ctrl.SetState(idle, e); + return; + } + // halt if we're close enough, flee if we're too close + float dist_sq = glm::length2(e.AbsoluteDifference(steering.GetTargetEntity())); + if (dist_sq < 8.0f) { + ctrl.SetState(flee, e); + } else if (dist_sq < 25.0f) { + steering.Enable(Steering::HALT).Disable(Steering::PURSUE_TARGET); } else { - Controlled().TargetVelocity(glm::vec3(0.0f)); + steering.Enable(Steering::PURSUE_TARGET).Disable(Steering::HALT); } } +void ChaseState::Exit(AIController &ctrl, Entity &e) const { + e.GetSteering().Disable(Steering::HALT | Steering::PURSUE_TARGET); +} + +// flee -Controller::Controller(Entity &e) noexcept -: entity(e) { - entity.Ref(); +void FleeState::Enter(AIController &ctrl, Entity &e) const { + e.GetSteering() + .SetAcceleration(5.0f) + .SetSpeed(4.0f) + .Enable(Steering::EVADE_TARGET) + ; + ctrl.CueDecision(6.0f, 3.0f); } -Controller::~Controller() { - entity.UnRef(); +void FleeState::Update(AIController &ctrl, Entity &e, float dt) const { + if (!ctrl.DecisionDue()) return; + ctrl.SetState(idle, e); } +void FleeState::Exit(AIController &ctrl, Entity &e) const { + e.GetSteering().Disable(Steering::EVADE_TARGET); +} -RandomWalk::RandomWalk(Entity &e, std::uint64_t seed) noexcept -: Controller(e) -, random(seed) -, start_vel(e.Velocity()) -, target_vel(start_vel) -, switch_time(0) -, lerp_max(1.0f) -, lerp_time(0.0f) { +// idle +void IdleState::Enter(AIController &ctrl, Entity &e) const { + e.GetSteering() + .SetAcceleration(0.5f) + .SetSpeed(0.01f) + .Enable(Steering::HALT) + .SetWanderParams(1.0f, 1.1f, 1.0f) + ; + ctrl.CueDecision(10.0f, 5.0f); } -RandomWalk::~RandomWalk() { +void IdleState::Update(AIController &ctrl, Entity &e, float dt) const { + if (ctrl.MayThink()) { + const Player *player = ctrl.ClosestVisiblePlayer(e); + if (player) { + e.GetSteering().SetTargetEntity(player->GetEntity()); + ctrl.SetState(chase, e); + return; + } + } -} + if (!ctrl.DecisionDue()) return; -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().TargetVelocity(mix(target_vel, start_vel, a)); + unsigned int d = ctrl.Decide(10); + if (d < 2) { + // .2 chance to start going + ctrl.SetState(roam, e); + } else if (d < 5) { + // .3 chance of looking around + e.GetSteering().Disable(Steering::HALT).Enable(Steering::WANDER); } else { - Controlled().TargetVelocity(target_vel); + // .5 chance of doing nothing + e.GetSteering().Disable(Steering::WANDER).Enable(Steering::HALT); } + ctrl.CueDecision(10.0f, 5.0f); +} + +void IdleState::Exit(AIController &ctrl, Entity &e) const { + e.GetSteering().Disable(Steering::HALT | Steering::WANDER); +} + +// roam + +void RoamState::Enter(AIController &ctrl, Entity &e) const { + e.GetSteering() + .SetAcceleration(0.5f) + .SetSpeed(1.0f) + .SetWanderParams(1.0f, 2.0f, 1.0f) + .Enable(Steering::WANDER) + ; + ctrl.CueDecision(10.0f, 5.0f); } -void RandomWalk::Change() noexcept { - start_vel = target_vel; +void RoamState::Update(AIController &ctrl, Entity &e, float dt) const { + if (ctrl.MayThink()) { + const Player *player = ctrl.ClosestVisiblePlayer(e); + if (player) { + e.GetSteering().SetTargetEntity(player->GetEntity()); + ctrl.SetState(chase, e); + return; + } + } + + if (!ctrl.DecisionDue()) return; - constexpr float base = 0.001f; + unsigned int d = ctrl.Decide(10); + if (d == 0) { + // .1 chance of idling + ctrl.SetState(idle, e); + } + ctrl.CueDecision(10.0f, 5.0f); +} - target_vel.x = base * (random.Next() % 1024); - target_vel.y = base * (random.Next() % 1024); - target_vel.z = base * (random.Next() % 1024); +void RoamState::Exit(AIController &ctrl, Entity &e) const { + e.GetSteering().Disable(Steering::WANDER); } }