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"
14 Spawner::Spawner(World &world, std::uint64_t seed)
19 , despawn_range(128 * 128)
20 , spawn_distance(16 * 16)
23 EntityModel::Buffer buf;
25 AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
26 CuboidShape shape(bounds);
27 shape.Vertices(buf, 1.0f);
28 buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
29 models[0].Update(buf);
30 skeletons[0].Bounds(bounds);
31 skeletons[0].SetNodeModel(&models[0]);
34 AABB bounds{{ -0.5f, -0.25f, -0.5f }, { 0.5f, 0.25f, 0.5f }};
35 CuboidShape shape(bounds);
37 shape.Vertices(buf, 2.0f);
38 buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
39 models[1].Update(buf);
40 skeletons[1].Bounds(bounds);
41 skeletons[1].SetNodeModel(&models[1]);
44 AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
45 StairShape shape(bounds, { 0.4f, 0.4f });
47 shape.Vertices(buf, 3.0f);
48 buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
49 models[2].Update(buf);
50 skeletons[2].Bounds(bounds);
51 skeletons[2].SetNodeModel(&models[2]);
58 for (auto &ctrl : controllers) {
64 void Spawner::Update(int dt) {
70 for (auto &ctrl : controllers) {
76 void Spawner::CheckDespawn() noexcept {
77 const auto &refs = world.Players();
78 for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
79 Entity &e = (*iter)->Controlled();
82 iter = controllers.erase(iter);
86 for (const Entity *ref : refs) {
87 glm::vec3 diff(ref->AbsoluteDifference(e));
88 if (dot(diff, diff) < despawn_range) {
96 iter = controllers.erase(iter);
103 void Spawner::TrySpawn() {
104 if (controllers.size() >= max_entities) return;
106 // select random player to punish
107 auto &players = world.Players();
108 if (players.size() == 0) return;
109 Entity &player = *players[random.Next<unsigned short>() % players.size()];
112 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range,
113 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range,
114 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range
118 random.Next<unsigned char>() % Chunk::width,
119 random.Next<unsigned char>() % Chunk::height,
120 random.Next<unsigned char>() % Chunk::depth
125 glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.Position());
126 float dist = dot(diff, diff);
127 if (dist > despawn_range || dist < spawn_distance) {
131 // check if the spawn block and the one above it are loaded and inhabitable
132 BlockLookup spawn_block(
133 world.Loader().Loaded(player.ChunkCoords()),
134 chunk * Chunk::Extent() + pos);
135 if (!spawn_block || spawn_block.GetType().collide_block) {
139 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
140 if (!head_block || head_block.GetType().collide_block) {
144 Spawn(player, player.ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f));
147 void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) {
148 glm::vec3 rot(0.000001f);
149 rot.x *= (random.Next<unsigned short>() % 1024);
150 rot.y *= (random.Next<unsigned short>() % 1024);
151 rot.z *= (random.Next<unsigned short>() % 1024);
153 Entity &e = world.AddEntity();
155 e.Position(chunk, pos);
156 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
157 e.WorldCollidable(true);
158 skeletons[random.Next<unsigned char>() % 3].Instantiate(e.GetModel());
159 e.AngularVelocity(rot);
162 ctrl = new RandomWalk(e, random.Next<std::uint64_t>());
164 ctrl = new Chaser(world, e, reference);
166 controllers.emplace_back(ctrl);