]> git.localhorst.tv Git - blank.git/blob - src/ai/AIController.hpp
make AI entities avoid world collisions
[blank.git] / src / ai / AIController.hpp
1 #ifndef BLANK_AI_AICONTROLLER_HPP_
2 #define BLANK_AI_AICONTROLLER_HPP_
3
4 #include "../app/IntervalTimer.hpp"
5 #include "../model/geometry.hpp"
6 #include "../world/EntityController.hpp"
7
8 #include <glm/glm.hpp>
9
10
11 namespace blank {
12
13 class AIState;
14 class Entity;
15 class GaloisLFSR;
16 class Player;
17 class World;
18
19 class AIController
20 : public EntityController {
21
22 public:
23         AIController(World &, GaloisLFSR &);
24         ~AIController();
25
26         void SetState(const AIState &);
27
28         void Update(Entity &, float dt) override;
29
30         glm::vec3 ControlForce(const Entity &, const EntityState &) const override;
31
32         /// get the closest player that given entity can see
33         /// returns nullptr if none are in sight
34         Player *ClosestVisiblePlayer(const Entity &) noexcept;
35         /// true if to entity is in visible range of from entity
36         bool LineOfSight(const Entity &from, const Entity &to) const noexcept;
37
38         /// true if the controller may do expensive calculations
39         bool MayThink() const noexcept;
40         void SetThinkInterval(float) noexcept;
41
42         /// schedule a decision in the next minimum ± variance seconds
43         void CueDecision(
44                 float minimum,
45                 float variance
46         ) noexcept;
47         /// check if the scheduled decision is due already
48         bool DecisionDue() const noexcept;
49         /// random choice of 0 to num_choices - 1
50         unsigned int Decide(unsigned int num_choices) noexcept;
51
52         void EnterHalt() noexcept;
53         void ExitHalt() noexcept;
54         bool IsHalted() const noexcept;
55         void SetHaltSpeed(float) noexcept;
56         glm::vec3 GetHaltForce(const Entity &, const EntityState &) const noexcept;
57
58         void StartAvoidingObstacles() noexcept;
59         void StopAvoidingObstacles() noexcept;
60         bool IsAvoidingObstacles() const noexcept;
61         glm::vec3 GetObstacleAvoidanceForce(const Entity &, const EntityState &) const noexcept;
62
63         void StartFleeing() noexcept;
64         void StopFleeing() noexcept;
65         bool IsFleeing() const noexcept;
66         void SetFleeTarget(Entity &) noexcept;
67         void SetFleeSpeed(float) noexcept;
68         Entity &GetFleeTarget() noexcept;
69         const Entity &GetFleeTarget() const noexcept;
70         glm::vec3 GetFleeForce(const Entity &, const EntityState &) const noexcept;
71
72         void StartSeeking() noexcept;
73         void StopSeeking() noexcept;
74         bool IsSeeking() const noexcept;
75         void SetSeekTarget(Entity &) noexcept;
76         void SetSeekSpeed(float) noexcept;
77         Entity &GetSeekTarget() noexcept;
78         const Entity &GetSeekTarget() const noexcept;
79         glm::vec3 GetSeekForce(const Entity &, const EntityState &) const noexcept;
80
81         void StartEvading() noexcept;
82         void StopEvading() noexcept;
83         bool IsEvading() const noexcept;
84         void SetEvadeTarget(Entity &) noexcept;
85         void SetEvadeSpeed(float) noexcept;
86         Entity &GetEvadeTarget() noexcept;
87         const Entity &GetEvadeTarget() const noexcept;
88         glm::vec3 GetEvadeForce(const Entity &, const EntityState &) const noexcept;
89
90         void StartPursuing() noexcept;
91         void StopPursuing() noexcept;
92         bool IsPursuing() const noexcept;
93         void SetPursuitTarget(Entity &) noexcept;
94         void SetPursuitSpeed(float) noexcept;
95         Entity &GetPursuitTarget() noexcept;
96         const Entity &GetPursuitTarget() const noexcept;
97         glm::vec3 GetPursuitForce(const Entity &, const EntityState &) const noexcept;
98
99         /// start wandering randomly
100         void StartWandering() noexcept;
101         void StopWandering() noexcept;
102         bool IsWandering() const noexcept;
103         /// change how wandering is performed
104         /// the trajectory is modified by targetting a blip on a sphere
105         /// in front of the entity which moves randomly
106         /// the displacement is given (roughly) in units per second
107         void SetWanderParams(
108                 float speed,
109                 float distance = 2.0f,
110                 float radius = 1.0f,
111                 float displacement = 1.0f
112         ) noexcept;
113         glm::vec3 GetWanderForce(const Entity &, const EntityState &) const noexcept;
114
115 private:
116         World &world;
117         GaloisLFSR &random;
118         const AIState *state;
119
120         /// how far controlled entities can see
121         float sight_dist;
122         /// cosine of the half angle of FOV of controlled entities
123         float sight_angle;
124
125         FineTimer think_timer;
126         FineTimer decision_timer;
127
128         bool halted;
129         float halt_speed;
130
131         bool avoid_obstacles;
132         AABB obstacle_box;
133         glm::mat4 obstacle_transform;
134
135         bool fleeing;
136         Entity *flee_target;
137         float flee_speed;
138
139         bool seeking;
140         Entity *seek_target;
141         float seek_speed;
142
143         bool evading;
144         Entity *evade_target;
145         float evade_speed;
146
147         bool pursuing;
148         Entity *pursuit_target;
149         float pursuit_speed;
150
151         bool wandering;
152         glm::vec3 wander_pos;
153         float wander_speed;
154         float wander_dist;
155         float wander_radius;
156         float wander_disp;
157
158 };
159
160 }
161
162 #endif