]> git.localhorst.tv Git - blank.git/blob - src/ai/ai.cpp
a1abfdc3cf73444102ed63e6210a00b7e2566f1e
[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         if (dist < std::numeric_limits<float>::epsilon()) {
34                 Controlled().Velocity(glm::vec3(0.0f));
35                 return;
36         }
37         glm::vec3 norm_diff(diff / dist);
38
39         bool line_of_sight = true;
40         Ray aim{Target().Position() - diff, norm_diff};
41         WorldCollision coll;
42         if (world.Intersection(aim, glm::mat4(1.0f), Target().ChunkCoords(), coll)) {
43                 line_of_sight = coll.depth > dist;
44         }
45
46         if (!line_of_sight) {
47                 Controlled().Velocity(glm::vec3(0.0f));
48         } else if (dist > stop_dist) {
49                 Controlled().Velocity(norm_diff * chase_speed);
50         } else if (dist < flee_dist) {
51                 Controlled().Velocity(norm_diff * flee_speed);
52         } else {
53                 Controlled().Velocity(glm::vec3(0.0f));
54         }
55 }
56
57
58 Controller::Controller(Entity &e) noexcept
59 : entity(e) {
60         entity.Ref();
61 }
62
63 Controller::~Controller() {
64         entity.UnRef();
65 }
66
67
68 RandomWalk::RandomWalk(Entity &e, std::uint64_t seed) noexcept
69 : Controller(e)
70 , random(seed)
71 , start_vel(e.Velocity())
72 , target_vel(start_vel)
73 , start_rot(e.AngularVelocity())
74 , target_rot(start_rot)
75 , switch_time(0)
76 , lerp_max(1.0f)
77 , lerp_time(0.0f) {
78
79 }
80
81 RandomWalk::~RandomWalk() {
82
83 }
84
85 void RandomWalk::Update(int dt) {
86         switch_time -= dt;
87         lerp_time -= dt;
88         if (switch_time < 0) {
89                 switch_time += 2500 + (random.Next<unsigned short>() % 5000);
90                 lerp_max = 1500 + (random.Next<unsigned short>() % 1000);
91                 lerp_time = lerp_max;
92                 Change();
93         } else if (lerp_time > 0) {
94                 float a = std::min(lerp_time / lerp_max, 1.0f);
95                 Controlled().Velocity(mix(target_vel, start_vel, a));
96                 Controlled().AngularVelocity(mix(target_rot, start_rot, a));
97         } else {
98                 Controlled().Velocity(target_vel);
99                 Controlled().AngularVelocity(target_rot);
100         }
101 }
102
103 void RandomWalk::Change() noexcept {
104         start_vel = target_vel;
105         start_rot = target_rot;
106
107         constexpr float base = 0.000001f;
108
109         target_vel.x = base * (random.Next<short>() % 1024);
110         target_vel.y = base * (random.Next<short>() % 1024);
111         target_vel.z = base * (random.Next<short>() % 1024);
112
113         target_rot.x = base * (random.Next<short>() % 1024);
114         target_rot.y = base * (random.Next<short>() % 1024);
115         target_rot.z = base * (random.Next<short>() % 1024);
116 }
117
118 }