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