]> git.localhorst.tv Git - blank.git/blob - src/ai/Spawner.cpp
e5bc68f9d97461d2396de2076b338f025c0d967e
[blank.git] / src / ai / Spawner.cpp
1 #include "Spawner.hpp"
2
3 #include "Chaser.hpp"
4 #include "RandomWalk.hpp"
5 #include "../world/BlockType.hpp"
6 #include "../world/BlockTypeRegistry.hpp"
7 #include "../world/Entity.hpp"
8 #include "../world/World.hpp"
9
10
11 namespace blank {
12
13 Spawner::Spawner(World &world)
14 : world(world)
15 , controllers()
16 , timer(8096)
17 , despawn_range(128 * 128)
18 , spawn_distance(16 * 16)
19 , max_entities(16)
20 , chunk_range(4) {
21         EntityModel::Buffer buf;
22         for (size_t i = 0; i < 14; ++i) {
23                 world.BlockTypes()[i + 1].FillEntityModel(buf);
24                 models[i].Update(buf);
25                 buf.Clear();
26         }
27
28         timer.Start();
29         Spawn(world.Player().ChunkCoords(), { 0.5f, 0.5f, 0.5f });
30 }
31
32 Spawner::~Spawner() {
33         for (auto &ctrl : controllers) {
34                 delete ctrl;
35         }
36 }
37
38
39 void Spawner::Update(int dt) {
40         CheckDespawn();
41         timer.Update(dt);
42         if (timer.Hit()) {
43                 TrySpawn();
44         }
45         for (auto &ctrl : controllers) {
46                 ctrl->Update(dt);
47         }
48 }
49
50
51 void Spawner::CheckDespawn() noexcept {
52         const Entity &reference = world.Player();
53         for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
54                 Entity &e = (*iter)->Controlled();
55                 glm::vec3 diff(reference.AbsoluteDifference(e));
56                 if (dot(diff, diff) > despawn_range) {
57                         e.Remove();
58                         delete *iter;
59                         iter = controllers.erase(iter);
60                 } else {
61                         ++iter;
62                 }
63         }
64 }
65
66 void Spawner::TrySpawn() {
67         if (controllers.size() >= max_entities) return;
68
69         glm::ivec3 chunk(
70                 (rand() % (chunk_range * 2 + 1)) - chunk_range,
71                 (rand() % (chunk_range * 2 + 1)) - chunk_range,
72                 (rand() % (chunk_range * 2 + 1)) - chunk_range
73         );
74
75         glm::ivec3 pos(
76                 rand() % Chunk::width,
77                 rand() % Chunk::height,
78                 rand() % Chunk::depth
79         );
80
81
82         // distance check
83         glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + world.Player().Position());
84         float dist = dot(diff, diff);
85         if (dist > despawn_range || dist < spawn_distance) {
86                 return;
87         }
88
89         // block check
90         // TODO: avoid force load, abort spawn if chunk unavailble
91         Chunk &tgt_chunk = world.Next(world.PlayerChunk(), chunk);
92         // TODO: don't use visibility for spawn check
93         //       also, check for more than one block space
94         if (tgt_chunk.Type(tgt_chunk.BlockAt(pos)).visible) {
95                 return;
96         }
97
98         Spawn(world.Player().ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f));
99 }
100
101 void Spawner::Spawn(const glm::ivec3 &chunk, const glm::vec3 &pos) {
102         glm::vec3 rot(0.000001f);
103         rot.x *= (rand() % 1024);
104         rot.y *= (rand() % 1024);
105         rot.z *= (rand() % 1024);
106
107         Entity &e = world.AddEntity();
108         e.Name("test");
109         e.Position(chunk, pos);
110         e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
111         e.WorldCollidable(true);
112         e.GetModel().SetNodeModel(&models[rand() % 14]);
113         e.AngularVelocity(rot);
114         Controller *ctrl;
115         if (rand() % 2) {
116                 ctrl = new RandomWalk(e);
117         } else {
118                 ctrl = new Chaser(world, e, world.Player());
119         }
120         controllers.emplace_back(ctrl);
121 }
122
123 }