]> git.localhorst.tv Git - blank.git/blob - src/ai/ai.cpp
4a2e3102b90d7f6dbdb88ba1bf39d2bd80d171bd
[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, std::uint64_t seed) noexcept
65 : Controller(e)
66 , random(seed)
67 , time_left(0) {
68
69 }
70
71 RandomWalk::~RandomWalk() {
72
73 }
74
75 void RandomWalk::Update(int dt) {
76         time_left -= dt;
77         if (time_left > 0) return;
78         time_left += 2500 + (random.Next<unsigned short>() % 5000);
79
80         constexpr float move_vel = 0.0005f;
81
82         glm::vec3 new_vel = Controlled().Velocity();
83
84         switch (random.Next<unsigned char>() % 9) {
85                 case 0:
86                         new_vel.x = -move_vel;
87                         break;
88                 case 1:
89                         new_vel.x = 0.0f;
90                         break;
91                 case 2:
92                         new_vel.x = move_vel;
93                         break;
94                 case 3:
95                         new_vel.y = -move_vel;
96                         break;
97                 case 4:
98                         new_vel.y = 0.0f;
99                         break;
100                 case 5:
101                         new_vel.y = move_vel;
102                         break;
103                 case 6:
104                         new_vel.z = -move_vel;
105                         break;
106                 case 7:
107                         new_vel.z = 0.0f;
108                         break;
109                 case 8:
110                         new_vel.z = move_vel;
111                         break;
112         }
113
114         Controlled().Velocity(new_vel);
115 }
116
117 }