]> git.localhorst.tv Git - blank.git/blob - src/ai/ai.cpp
use collision structures for ray tests
[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
24 }
25
26 Chaser::~Chaser() {
27
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         // FIXME: this only works if target is in the reference chunk (which is true for the player)
37         Ray aim{Target().Position() - diff, norm_diff};
38         WorldCollision coll;
39         if (world.Intersection(aim, glm::mat4(1.0f), coll)) {
40                 line_of_sight = coll.depth > dist;
41         }
42
43         if (!line_of_sight) {
44                 Controlled().Velocity(glm::vec3(0.0f));
45         } else if (dist > stop_dist) {
46                 Controlled().Velocity(norm_diff * chase_speed);
47         } else if (dist < flee_dist) {
48                 Controlled().Velocity(norm_diff * flee_speed);
49         } else {
50                 Controlled().Velocity(glm::vec3(0.0f));
51         }
52 }
53
54
55 Controller::Controller(Entity &e) noexcept
56 : entity(e) {
57
58 }
59
60 Controller::~Controller() {
61
62 }
63
64
65 RandomWalk::RandomWalk(Entity &e) noexcept
66 : Controller(e)
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 + (rand() % 5000);
79
80         constexpr float move_vel = 0.0005f;
81
82         glm::vec3 new_vel = Controlled().Velocity();
83
84         switch (rand() % 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 }