]> git.localhorst.tv Git - blank.git/blob - src/world/Steering.hpp
glm backwards compatibility
[blank.git] / src / world / Steering.hpp
1 #ifndef BLANK_WORLD_STEERING_HPP
2 #define BLANK_WORLD_STEERING_HPP
3
4 #include "../geometry/Location.hpp"
5 #include "../geometry/primitive.hpp"
6 #include "../graphics/glm.hpp"
7
8
9 namespace blank {
10
11 class Entity;
12 class EntityState;
13 class World;
14
15 class Steering {
16
17 public:
18         enum Behaviour {
19                 // standalone behaviours
20                 HALT = 0x0001,
21                 WANDER = 0x0002,
22                 OBSTACLE_AVOIDANCE = 0x0004,
23
24                 // requires target velocity
25                 TARGET_VELOCITY = 0x0008,
26
27                 // behaviours requiring a target entity
28                 EVADE_TARGET = 0x0010,
29                 PURSUE_TARGET = 0x0020,
30         };
31
32         explicit Steering(const Entity &);
33         ~Steering();
34
35         Steering &Enable(unsigned int b) noexcept { enabled |= b; return *this; }
36         Steering &Disable(unsigned int b) noexcept { enabled &= ~b; return *this; }
37         bool AnyEnabled(unsigned int b) const noexcept { return enabled & b; }
38         bool AllEnabled(unsigned int b) const noexcept { return (enabled & b) == b; }
39
40         Steering &SetTargetEntity(Entity &) noexcept;
41         Steering &ClearTargetEntity() noexcept;
42         bool HasTargetEntity() const noexcept { return target_entity; }
43         const Entity &GetTargetEntity() const noexcept { return *target_entity; }
44
45         Steering &SetTargetVelocity(const glm::vec3 &v) noexcept { target_velocity = v; return *this; }
46         const glm::vec3 &GetTargetVelocity() const noexcept { return target_velocity; }
47
48         /// time in seconds in which steering tried to arrive at target velocity
49         Steering &SetAcceleration(float a) noexcept { accel = a; return *this; }
50         /// maximum magnitude of velocity that behaviours generate
51         Steering &SetSpeed(float s) noexcept { speed = s; return *this; }
52
53         /// configure wandering
54         /// r is the radius of the sphere
55         /// dist is the distance between the entity and the sphere's center
56         /// disp is the maximum variance of the point on the sphere in units per second
57         Steering &SetWanderParams(float r, float dist, float disp) noexcept {
58                 wander_radius = r;
59                 wander_dist = dist;
60                 wander_disp = disp;
61                 return *this;
62         }
63
64         void Update(World &, float dt);
65
66         glm::vec3 Force(const EntityState &) const noexcept;
67
68 private:
69         void UpdateWander(World &, float dt);
70         void UpdateObstacle(World &);
71
72         /// try to add as much of in to out so it doesn't exceed max
73         /// returns true if it's maxed out
74         static bool SumForce(glm::vec3 &out, const glm::vec3 &in, float max) noexcept;
75
76         /// slow down to a halt
77         glm::vec3 Halt(const EntityState &) const noexcept;
78         /// accelerate to match given velocity
79         glm::vec3 TargetVelocity(const EntityState &, const glm::vec3 &) const noexcept;
80         /// move towards given location
81         glm::vec3 Seek(const EntityState &, const ExactLocation &) const noexcept;
82         /// move away from given location
83         glm::vec3 Flee(const EntityState &, const ExactLocation &) const noexcept;
84         /// try to halt at given location
85         glm::vec3 Arrive(const EntityState &, const ExactLocation &) const noexcept;
86         /// seek given entity's predicted position
87         glm::vec3 Pursuit(const EntityState &, const Entity &) const noexcept;
88         /// flee given entity's predicted position
89         glm::vec3 Evade(const EntityState &, const Entity &) const noexcept;
90         /// move around for no reason
91         glm::vec3 Wander(const EntityState &) const noexcept;
92         /// try not to crash into blocks
93         glm::vec3 ObstacleAvoidance(const EntityState &) const noexcept;
94
95 private:
96         const Entity &entity;
97
98         Entity *target_entity;
99         glm::vec3 target_velocity;
100
101         float accel;
102         float speed;
103
104         float wander_radius;
105         float wander_dist;
106         float wander_disp;
107         glm::vec3 wander_pos;
108
109         glm::vec3 obstacle_dir;
110
111         unsigned int enabled;
112
113
114
115 };
116
117 }
118
119 #endif