1 #include "AIController.hpp"
2 #include "IdleState.hpp"
4 #include "../model/geometry.hpp"
5 #include "../rand/GaloisLFSR.hpp"
6 #include "../world/Entity.hpp"
7 #include "../world/World.hpp"
8 #include "../world/WorldCollision.hpp"
12 #include <glm/glm.hpp>
23 AIController::AIController(GaloisLFSR &rand)
26 , flee_target(nullptr)
28 , seek_target(nullptr)
31 , wander_pos(1.0f, 0.0f, 0.0f)
35 , wander_speed(1.0f) {
39 AIController::~AIController() {
43 void AIController::SetState(const AIState &s) {
49 void AIController::Update(Entity &e, float dt) {
50 // movement: for now, wander only
51 glm::vec3 displacement(
52 random.SNorm() * wander_disp,
53 random.SNorm() * wander_disp,
54 random.SNorm() * wander_disp
56 if (dot(displacement, displacement) > std::numeric_limits<float>::epsilon()) {
57 wander_pos = normalize(wander_pos + displacement * dt) * wander_radius;
61 // orient head towards heading
62 glm::vec3 heading(Heading(e.GetState()));
63 float tgt_pitch = std::atan(heading.y / length(glm::vec2(heading.x, heading.z)));
64 float tgt_yaw = std::atan2(-heading.x, -heading.z);
65 e.SetHead(tgt_pitch, tgt_yaw);
69 glm::vec3 AIController::ControlForce(const EntityState &state) const {
70 glm::vec3 force(0.0f);
72 glm::vec3 diff(GetFleeTarget().GetState().Diff(state));
73 if (dot(diff, diff) > std::numeric_limits<float>::epsilon()) {
74 force += normalize(diff) * flee_speed;
78 glm::vec3 diff(state.Diff(GetSeekTarget().GetState()));
79 if (dot(diff, diff) > std::numeric_limits<float>::epsilon()) {
80 force += normalize(diff) * seek_speed;
84 force += (Heading(state) * wander_dist + wander_pos) * wander_speed;
89 glm::vec3 AIController::Heading(const EntityState &state) noexcept {
90 if (dot(state.velocity, state.velocity) > std::numeric_limits<float>::epsilon()) {
91 return normalize(state.velocity);
93 float cp = std::cos(state.pitch);
94 return glm::vec3(std::cos(state.yaw) * cp, std::sin(state.yaw) * cp, std::sin(state.pitch));
98 void AIController::StartFleeing(const Entity &e, float speed) noexcept {
103 void AIController::StopFleeing() noexcept {
104 flee_target = nullptr;
107 bool AIController::IsFleeing() const noexcept {
111 const Entity &AIController::GetFleeTarget() const noexcept {
115 void AIController::StartSeeking(const Entity &e, float speed) noexcept {
120 void AIController::StopSeeking() noexcept {
121 seek_target = nullptr;
124 bool AIController::IsSeeking() const noexcept {
128 const Entity &AIController::GetSeekTarget() const noexcept {
132 void AIController::StartWandering() noexcept {
135 void AIController::StopWandering() noexcept {
140 void IdleState::Enter(AIController &ctrl) const {
141 ctrl.StartWandering();
144 void IdleState::Update(AIController &ctrl, Entity &e, float dt) const {
147 void IdleState::Exit(AIController &ctrl) const {
148 ctrl.StopWandering();