]> git.localhorst.tv Git - blank.git/blob - src/ai/Spawner.cpp
split chunk redering from world model
[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                 glm::vec3 diff(reference.AbsoluteDifference(e));
72                 if (dot(diff, diff) > despawn_range) {
73                         e.Remove();
74                         delete *iter;
75                         iter = controllers.erase(iter);
76                 } else {
77                         ++iter;
78                 }
79         }
80 }
81
82 void Spawner::TrySpawn() {
83         if (controllers.size() >= max_entities) return;
84
85         glm::ivec3 chunk(
86                 (rand() % (chunk_range * 2 + 1)) - chunk_range,
87                 (rand() % (chunk_range * 2 + 1)) - chunk_range,
88                 (rand() % (chunk_range * 2 + 1)) - chunk_range
89         );
90
91         glm::ivec3 pos(
92                 rand() % Chunk::width,
93                 rand() % Chunk::height,
94                 rand() % Chunk::depth
95         );
96
97
98         // distance check
99         glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + world.Player().Position());
100         float dist = dot(diff, diff);
101         if (dist > despawn_range || dist < spawn_distance) {
102                 return;
103         }
104
105         // check if the spawn block and the one above it are loaded and inhabitable
106         BlockLookup spawn_block(&world.PlayerChunk(), chunk * Chunk::Extent() + pos);
107         if (!spawn_block || spawn_block.GetType().collide_block) {
108                 return;
109         }
110
111         BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
112         if (!head_block || head_block.GetType().collide_block) {
113                 return;
114         }
115
116         Spawn(world.Player().ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f));
117 }
118
119 void Spawner::Spawn(const glm::ivec3 &chunk, const glm::vec3 &pos) {
120         glm::vec3 rot(0.000001f);
121         rot.x *= (rand() % 1024);
122         rot.y *= (rand() % 1024);
123         rot.z *= (rand() % 1024);
124
125         Entity &e = world.AddEntity();
126         e.Name("spawned");
127         e.Position(chunk, pos);
128         e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
129         e.WorldCollidable(true);
130         e.GetModel().SetNodeModel(&models[rand() % 3]);
131         e.AngularVelocity(rot);
132         Controller *ctrl;
133         if (rand() % 2) {
134                 ctrl = new RandomWalk(e);
135         } else {
136                 ctrl = new Chaser(world, e, world.Player());
137         }
138         controllers.emplace_back(ctrl);
139 }
140
141 }