]> git.localhorst.tv Git - blank.git/blob - src/ai/Spawner.cpp
check for entities under crosshair
[blank.git] / src / ai / Spawner.cpp
1 #include "Spawner.hpp"
2
3 #include "Chaser.hpp"
4 #include "RandomWalk.hpp"
5 #include "../world/BlockLookup.hpp"
6 #include "../world/BlockType.hpp"
7 #include "../world/BlockTypeRegistry.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         for (size_t i = 0; i < 14; ++i) {
24                 world.BlockTypes()[i + 1].FillEntityModel(buf);
25                 models[i].Update(buf);
26                 buf.Clear();
27         }
28
29         timer.Start();
30         Spawn(world.Player().ChunkCoords(), { 0.5f, 0.5f, 0.5f });
31 }
32
33 Spawner::~Spawner() {
34         for (auto &ctrl : controllers) {
35                 delete ctrl;
36         }
37 }
38
39
40 void Spawner::Update(int dt) {
41         CheckDespawn();
42         timer.Update(dt);
43         if (timer.Hit()) {
44                 TrySpawn();
45         }
46         for (auto &ctrl : controllers) {
47                 ctrl->Update(dt);
48         }
49 }
50
51
52 void Spawner::CheckDespawn() noexcept {
53         const Entity &reference = world.Player();
54         for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
55                 Entity &e = (*iter)->Controlled();
56                 glm::vec3 diff(reference.AbsoluteDifference(e));
57                 if (dot(diff, diff) > despawn_range) {
58                         e.Remove();
59                         delete *iter;
60                         iter = controllers.erase(iter);
61                 } else {
62                         ++iter;
63                 }
64         }
65 }
66
67 void Spawner::TrySpawn() {
68         if (controllers.size() >= max_entities) return;
69
70         glm::ivec3 chunk(
71                 (rand() % (chunk_range * 2 + 1)) - chunk_range,
72                 (rand() % (chunk_range * 2 + 1)) - chunk_range,
73                 (rand() % (chunk_range * 2 + 1)) - chunk_range
74         );
75
76         glm::ivec3 pos(
77                 rand() % Chunk::width,
78                 rand() % Chunk::height,
79                 rand() % Chunk::depth
80         );
81
82
83         // distance check
84         glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + world.Player().Position());
85         float dist = dot(diff, diff);
86         if (dist > despawn_range || dist < spawn_distance) {
87                 return;
88         }
89
90         // check if the spawn block and the one above it are loaded and inhabitable
91         BlockLookup spawn_block(&world.PlayerChunk(), chunk * Chunk::Extent() + pos);
92         if (!spawn_block || spawn_block.GetType().collide_block) {
93                 return;
94         }
95
96         BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
97         if (!head_block || head_block.GetType().collide_block) {
98                 return;
99         }
100
101         Spawn(world.Player().ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f));
102 }
103
104 void Spawner::Spawn(const glm::ivec3 &chunk, const glm::vec3 &pos) {
105         glm::vec3 rot(0.000001f);
106         rot.x *= (rand() % 1024);
107         rot.y *= (rand() % 1024);
108         rot.z *= (rand() % 1024);
109
110         Entity &e = world.AddEntity();
111         e.Name("test");
112         e.Position(chunk, pos);
113         e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
114         e.WorldCollidable(true);
115         e.GetModel().SetNodeModel(&models[rand() % 14]);
116         e.AngularVelocity(rot);
117         Controller *ctrl;
118         if (rand() % 2) {
119                 ctrl = new RandomWalk(e);
120         } else {
121                 ctrl = new Chaser(world, e, world.Player());
122         }
123         controllers.emplace_back(ctrl);
124 }
125
126 }