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