3 #include "AIController.hpp"
5 #include "../model/Model.hpp"
6 #include "../model/ModelRegistry.hpp"
7 #include "../rand/GaloisLFSR.hpp"
8 #include "../world/BlockLookup.hpp"
9 #include "../world/BlockType.hpp"
10 #include "../world/ChunkIndex.hpp"
11 #include "../world/Entity.hpp"
12 #include "../world/World.hpp"
21 Spawner::Spawner(World &world, ModelRegistry &models)
26 , despawn_range(128 * 128)
27 , spawn_distance(16 * 16)
31 , model_length(models.size()) {
36 for (Entity *e : entities) {
42 void Spawner::LimitModels(size_t begin, size_t end) {
43 if (begin >= models.size() || end > models.size() || begin >= end) {
44 cout << "warning, models limit out of bounds or invalid range given" << endl;
47 model_length = end - begin;
51 void Spawner::Update(int dt) {
60 void Spawner::CheckDespawn() noexcept {
61 const auto &refs = world.Players();
62 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
66 iter = entities.erase(iter);
71 for (const Player &ref : refs) {
72 glm::vec3 diff(ref.GetEntity().AbsoluteDifference(e));
73 if (glm::length2(diff) < despawn_range) {
81 iter = entities.erase(iter);
89 void Spawner::TrySpawn() {
90 if (entities.size() >= max_entities || model_length == 0) return;
92 // select random player to punish
93 auto &players = world.Players();
94 if (players.size() == 0) return;
95 size_t player_num = world.Random().Next<unsigned short>() % players.size();
96 auto i = players.begin(), end = players.end();
97 for (; player_num > 0 && i != end; ++i, --player_num) {
99 const Player &player = *i;
101 BlockLookup spawn_block(player.GetChunks().RandomBlock(world.Random()));
104 //glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.entity->Position());
105 //float dist = dot(diff, diff);
106 //if (dist > despawn_range || dist < spawn_distance) {
110 // check if the spawn block and the one above it are loaded and inhabitable
111 if (!spawn_block || spawn_block.GetType().collide_block) {
115 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
116 if (!head_block || head_block.GetType().collide_block) {
120 Spawn(player.GetEntity(), spawn_block.GetChunk().Position(), spawn_block.GetBlockCoords());
123 void Spawner::Spawn(Entity &, const glm::ivec3 &chunk, const glm::vec3 &pos) {
124 Entity &e = world.AddEntity();
125 e.Position(chunk, pos);
126 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
127 e.WorldCollidable(true);
128 RandomModel().Instantiate(e.GetModel());
129 e.SetController(new AIController(world, e));
132 entities.emplace_back(&e);
135 Model &Spawner::RandomModel() noexcept {
136 std::size_t offset = (world.Random().Next<std::size_t>() % model_length) + model_offset;
137 return models[offset];