]> git.localhorst.tv Git - blank.git/blob - src/ai/Spawner.cpp
split graphics stuff from AI spawner
[blank.git] / src / ai / Spawner.cpp
1 #include "Spawner.hpp"
2
3 #include "Chaser.hpp"
4 #include "RandomWalk.hpp"
5 #include "../model/CompositeModel.hpp"
6 #include "../model/Skeletons.hpp"
7 #include "../world/BlockLookup.hpp"
8 #include "../world/BlockType.hpp"
9 #include "../world/Entity.hpp"
10 #include "../world/World.hpp"
11
12
13 namespace blank {
14
15 Spawner::Spawner(World &world, Skeletons &skeletons, std::uint64_t seed)
16 : world(world)
17 , skeletons(skeletons)
18 , controllers()
19 , random(seed)
20 , timer(64)
21 , despawn_range(128 * 128)
22 , spawn_distance(16 * 16)
23 , max_entities(16)
24 , chunk_range(4) {
25         timer.Start();
26 }
27
28 Spawner::~Spawner() {
29         for (auto &ctrl : controllers) {
30                 delete ctrl;
31         }
32 }
33
34
35 void Spawner::Update(int dt) {
36         CheckDespawn();
37         timer.Update(dt);
38         if (timer.Hit()) {
39                 TrySpawn();
40         }
41         for (auto &ctrl : controllers) {
42                 ctrl->Update(dt);
43         }
44 }
45
46
47 void Spawner::CheckDespawn() noexcept {
48         const auto &refs = world.Players();
49         for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
50                 Entity &e = (*iter)->Controlled();
51                 if (e.Dead()) {
52                         delete *iter;
53                         iter = controllers.erase(iter);
54                         continue;
55                 }
56                 bool safe = false;
57                 for (const Entity *ref : refs) {
58                         glm::vec3 diff(ref->AbsoluteDifference(e));
59                         if (dot(diff, diff) < despawn_range) {
60                                 safe = true;
61                                 break;
62                         }
63                 }
64                 if (!safe) {
65                         e.Kill();
66                         delete *iter;
67                         iter = controllers.erase(iter);
68                 } else {
69                         ++iter;
70                 }
71         }
72 }
73
74 void Spawner::TrySpawn() {
75         if (controllers.size() >= max_entities) return;
76
77         // select random player to punish
78         auto &players = world.Players();
79         if (players.size() == 0) return;
80         Entity &player = *players[random.Next<unsigned short>() % players.size()];
81
82         glm::ivec3 chunk(
83                 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range,
84                 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range,
85                 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range
86         );
87
88         glm::ivec3 pos(
89                 random.Next<unsigned char>() % Chunk::width,
90                 random.Next<unsigned char>() % Chunk::height,
91                 random.Next<unsigned char>() % Chunk::depth
92         );
93
94         // distance check
95         glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.Position());
96         float dist = dot(diff, diff);
97         if (dist > despawn_range || dist < spawn_distance) {
98                 return;
99         }
100
101         // check if the spawn block and the one above it are loaded and inhabitable
102         BlockLookup spawn_block(
103                 world.Loader().Loaded(player.ChunkCoords()),
104                 chunk * Chunk::Extent() + pos);
105         if (!spawn_block || spawn_block.GetType().collide_block) {
106                 return;
107         }
108
109         BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
110         if (!head_block || head_block.GetType().collide_block) {
111                 return;
112         }
113
114         Spawn(player, player.ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f));
115 }
116
117 void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) {
118         glm::vec3 rot(0.000001f);
119         rot.x *= (random.Next<unsigned short>() % 1024);
120         rot.y *= (random.Next<unsigned short>() % 1024);
121         rot.z *= (random.Next<unsigned short>() % 1024);
122
123         Entity &e = world.AddEntity();
124         e.Name("spawned");
125         e.Position(chunk, pos);
126         e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
127         e.WorldCollidable(true);
128         skeletons[random.Next<unsigned char>() % skeletons.Size()].Instantiate(e.GetModel());
129         e.AngularVelocity(rot);
130         Controller *ctrl;
131         if (random()) {
132                 ctrl = new RandomWalk(e, random.Next<std::uint64_t>());
133         } else {
134                 ctrl = new Chaser(world, e, reference);
135         }
136         controllers.emplace_back(ctrl);
137 }
138
139 }