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)
39 AIController::~AIController() {
43 void AIController::SetState(const AIState &s) {
49 void AIController::Update(Entity &e, float dt) {
50 // movement: for now, wander only
52 glm::vec3 displacement(
53 random.SNorm() * wander_disp,
54 random.SNorm() * wander_disp,
55 random.SNorm() * wander_disp
57 if (!iszero(displacement)) {
58 wander_pos = normalize(wander_pos + displacement * dt) * wander_radius;
63 // orient head towards heading
64 glm::vec3 heading(Heading(e.GetState()));
65 float tgt_pitch = std::atan(heading.y / length(glm::vec2(heading.x, heading.z)));
66 float tgt_yaw = std::atan2(-heading.x, -heading.z);
67 e.SetHead(tgt_pitch, tgt_yaw);
71 glm::vec3 AIController::ControlForce(const Entity &entity, const EntityState &state) const {
72 glm::vec3 force(0.0f);
74 glm::vec3 diff(GetFleeTarget().GetState().Diff(state));
75 if (MaxOutForce(force, TargetVelocity(normalize(diff) * flee_speed, state, 0.5f), entity.MaxControlForce())) {
80 glm::vec3 diff(state.Diff(GetSeekTarget().GetState()));
81 if (MaxOutForce(force, TargetVelocity(normalize(diff) * seek_speed, state, 0.5f), entity.MaxControlForce())) {
86 glm::vec3 wander_target(normalize(Heading(state) * wander_dist + wander_pos) * wander_speed);
87 if (MaxOutForce(force, TargetVelocity(wander_target, state, 2.0f), entity.MaxControlForce())) {
94 glm::vec3 AIController::Heading(const EntityState &state) noexcept {
95 if (dot(state.velocity, state.velocity) > std::numeric_limits<float>::epsilon()) {
96 return normalize(state.velocity);
98 float cp = std::cos(state.pitch);
99 return glm::vec3(std::cos(state.yaw) * cp, std::sin(state.yaw) * cp, std::sin(state.pitch));
103 void AIController::StartFleeing(const Entity &e, float speed) noexcept {
108 void AIController::StopFleeing() noexcept {
109 flee_target = nullptr;
112 bool AIController::IsFleeing() const noexcept {
116 const Entity &AIController::GetFleeTarget() const noexcept {
120 void AIController::StartSeeking(const Entity &e, float speed) noexcept {
125 void AIController::StopSeeking() noexcept {
126 seek_target = nullptr;
129 bool AIController::IsSeeking() const noexcept {
133 const Entity &AIController::GetSeekTarget() const noexcept {
137 void AIController::StartWandering(
144 wander_speed = speed;
145 wander_dist = distance;
146 wander_radius = radius;
147 wander_disp = displacement;
149 void AIController::StopWandering() noexcept {
154 void IdleState::Enter(AIController &ctrl) const {
155 ctrl.StartWandering(1.0f);
158 void IdleState::Update(AIController &ctrl, Entity &e, float dt) const {
161 void IdleState::Exit(AIController &ctrl) const {
162 ctrl.StopWandering();