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