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