4 #include "RandomWalk.hpp"
5 #include "../model/CompositeModel.hpp"
6 #include "../model/Skeletons.hpp"
7 #include "../world/BlockLookup.hpp"
8 #include "../world/BlockType.hpp"
9 #include "../world/ChunkIndex.hpp"
10 #include "../world/Entity.hpp"
11 #include "../world/World.hpp"
16 Spawner::Spawner(World &world, Skeletons &skeletons, std::uint64_t seed)
18 , skeletons(skeletons)
22 , despawn_range(128 * 128)
23 , spawn_distance(16 * 16)
30 for (auto &ctrl : controllers) {
36 void Spawner::Update(int dt) {
42 for (auto &ctrl : controllers) {
48 void Spawner::CheckDespawn() noexcept {
49 const auto &refs = world.Players();
50 for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
51 Entity &e = (*iter)->Controlled();
54 iter = controllers.erase(iter);
55 end = controllers.end();
59 for (const Player &ref : refs) {
60 glm::vec3 diff(ref.entity->AbsoluteDifference(e));
61 if (dot(diff, diff) < despawn_range) {
69 iter = controllers.erase(iter);
70 end = controllers.end();
77 void Spawner::TrySpawn() {
78 if (controllers.size() >= max_entities) return;
80 // select random player to punish
81 auto &players = world.Players();
82 if (players.size() == 0) return;
83 const Player &player = players[random.Next<unsigned short>() % players.size()];
85 int index = random.Next<unsigned int>() % player.chunks->TotalChunks();
87 glm::ivec3 chunk(player.chunks->PositionOf(index));
90 random.Next<unsigned char>() % Chunk::width,
91 random.Next<unsigned char>() % Chunk::height,
92 random.Next<unsigned char>() % Chunk::depth
96 //glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.entity->Position());
97 //float dist = dot(diff, diff);
98 //if (dist > despawn_range || dist < spawn_distance) {
102 // check if the spawn block and the one above it are loaded and inhabitable
103 BlockLookup spawn_block((*player.chunks)[index], pos);
104 if (!spawn_block || spawn_block.GetType().collide_block) {
108 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
109 if (!head_block || head_block.GetType().collide_block) {
113 Spawn(*player.entity, chunk, glm::vec3(pos) + glm::vec3(0.5f));
116 void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) {
117 glm::vec3 rot(0.000001f);
118 rot.x *= (random.Next<unsigned short>() % 1024);
119 rot.y *= (random.Next<unsigned short>() % 1024);
120 rot.z *= (random.Next<unsigned short>() % 1024);
122 Entity &e = world.AddEntity();
124 e.Position(chunk, pos);
125 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
126 e.WorldCollidable(true);
127 skeletons[random.Next<unsigned char>() % skeletons.Size()].Instantiate(e.GetModel());
128 e.AngularVelocity(rot);
131 ctrl = new RandomWalk(e, random.Next<std::uint64_t>());
133 ctrl = new Chaser(world, e, reference);
135 controllers.emplace_back(ctrl);