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"
20 Spawner::Spawner(World &world, Skeletons &skeletons, uint64_t seed)
22 , skeletons(skeletons)
26 , despawn_range(128 * 128)
27 , spawn_distance(16 * 16)
31 , skeletons_length(skeletons.Size()) {
36 for (auto &ctrl : controllers) {
42 void Spawner::LimitSkeletons(size_t begin, size_t end) {
43 if (begin >= skeletons.Size() || end > skeletons.Size() || begin >= end) {
44 cout << "warning, skeleton limit out of bounds or invalid range given" << endl;
46 skeletons_offset = begin;
47 skeletons_length = end - begin;
51 void Spawner::Update(int dt) {
57 for (auto &ctrl : controllers) {
63 void Spawner::CheckDespawn() noexcept {
64 const auto &refs = world.Players();
65 for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
66 Entity &e = (*iter)->Controlled();
69 iter = controllers.erase(iter);
70 end = controllers.end();
74 for (const Player &ref : refs) {
75 glm::vec3 diff(ref.entity->AbsoluteDifference(e));
76 if (dot(diff, diff) < despawn_range) {
84 iter = controllers.erase(iter);
85 end = controllers.end();
92 void Spawner::TrySpawn() {
93 if (controllers.size() >= max_entities || skeletons_length == 0) return;
95 // select random player to punish
96 auto &players = world.Players();
97 if (players.size() == 0) return;
98 const Player &player = players[random.Next<unsigned short>() % players.size()];
100 int index = random.Next<unsigned int>() % player.chunks->TotalChunks();
102 glm::ivec3 chunk(player.chunks->PositionOf(index));
105 random.Next<unsigned char>() % Chunk::width,
106 random.Next<unsigned char>() % Chunk::height,
107 random.Next<unsigned char>() % Chunk::depth
111 //glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.entity->Position());
112 //float dist = dot(diff, diff);
113 //if (dist > despawn_range || dist < spawn_distance) {
117 // check if the spawn block and the one above it are loaded and inhabitable
118 BlockLookup spawn_block((*player.chunks)[index], pos);
119 if (!spawn_block || spawn_block.GetType().collide_block) {
123 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
124 if (!head_block || head_block.GetType().collide_block) {
128 Spawn(*player.entity, chunk, glm::vec3(pos) + glm::vec3(0.5f));
131 void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) {
132 glm::vec3 rot(0.000001f);
133 rot.x *= (random.Next<unsigned short>() % 1024);
134 rot.y *= (random.Next<unsigned short>() % 1024);
135 rot.z *= (random.Next<unsigned short>() % 1024);
137 Entity &e = world.AddEntity();
138 e.Position(chunk, pos);
139 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
140 e.WorldCollidable(true);
141 RandomSkeleton().Instantiate(e.GetModel());
142 e.AngularVelocity(rot);
145 ctrl = new RandomWalk(e, random.Next<std::uint64_t>());
148 ctrl = new Chaser(world, e, reference);
151 controllers.emplace_back(ctrl);
154 CompositeModel &Spawner::RandomSkeleton() noexcept {
155 std::size_t offset = (random.Next<std::size_t>() % skeletons_length) + skeletons_offset;
156 return skeletons[offset];