]> git.localhorst.tv Git - blank.git/blob - src/ai/ai.cpp
reference count entities for safer removal
[blank.git] / src / ai / ai.cpp
1 #include "Chaser.hpp"
2 #include "Controller.hpp"
3 #include "RandomWalk.hpp"
4
5 #include "../model/geometry.hpp"
6 #include "../world/Entity.hpp"
7 #include "../world/World.hpp"
8 #include "../world/WorldCollision.hpp"
9
10 #include <glm/glm.hpp>
11
12
13 namespace blank {
14
15 Chaser::Chaser(World &world, Entity &ctrl, Entity &tgt) noexcept
16 : Controller(ctrl)
17 , world(world)
18 , tgt(tgt)
19 , chase_speed(0.002f)
20 , flee_speed(-0.005f)
21 , stop_dist(10)
22 , flee_dist(5) {
23         tgt.Ref();
24 }
25
26 Chaser::~Chaser() {
27         tgt.UnRef();
28 }
29
30 void Chaser::Update(int dt) {
31         glm::vec3 diff(Target().AbsoluteDifference(Controlled()));
32         float dist = length(diff);
33         glm::vec3 norm_diff(diff / dist);
34
35         bool line_of_sight = true;
36         Ray aim{Target().Position() - diff, norm_diff};
37         WorldCollision coll;
38         if (world.Intersection(aim, glm::mat4(1.0f), Target().ChunkCoords(), coll)) {
39                 line_of_sight = coll.depth > dist;
40         }
41
42         if (!line_of_sight) {
43                 Controlled().Velocity(glm::vec3(0.0f));
44         } else if (dist > stop_dist) {
45                 Controlled().Velocity(norm_diff * chase_speed);
46         } else if (dist < flee_dist) {
47                 Controlled().Velocity(norm_diff * flee_speed);
48         } else {
49                 Controlled().Velocity(glm::vec3(0.0f));
50         }
51 }
52
53
54 Controller::Controller(Entity &e) noexcept
55 : entity(e) {
56         entity.Ref();
57 }
58
59 Controller::~Controller() {
60         entity.UnRef();
61 }
62
63
64 RandomWalk::RandomWalk(Entity &e) noexcept
65 : Controller(e)
66 , time_left(0) {
67
68 }
69
70 RandomWalk::~RandomWalk() {
71
72 }
73
74 void RandomWalk::Update(int dt) {
75         time_left -= dt;
76         if (time_left > 0) return;
77         time_left += 2500 + (rand() % 5000);
78
79         constexpr float move_vel = 0.0005f;
80
81         glm::vec3 new_vel = Controlled().Velocity();
82
83         switch (rand() % 9) {
84                 case 0:
85                         new_vel.x = -move_vel;
86                         break;
87                 case 1:
88                         new_vel.x = 0.0f;
89                         break;
90                 case 2:
91                         new_vel.x = move_vel;
92                         break;
93                 case 3:
94                         new_vel.y = -move_vel;
95                         break;
96                 case 4:
97                         new_vel.y = 0.0f;
98                         break;
99                 case 5:
100                         new_vel.y = move_vel;
101                         break;
102                 case 6:
103                         new_vel.z = -move_vel;
104                         break;
105                 case 7:
106                         new_vel.z = 0.0f;
107                         break;
108                 case 8:
109                         new_vel.z = move_vel;
110                         break;
111         }
112
113         Controlled().Velocity(new_vel);
114 }
115
116 }