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