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.GetEntity().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 size_t player_num = random.Next<unsigned short>() % players.size();
99 auto i = players.begin(), end = players.end();
100 for (; player_num > 0 && i != end; ++i, --player_num) {
102 const Player &player = *i;
104 int index = random.Next<unsigned int>() % player.GetChunks().TotalChunks();
106 glm::ivec3 chunk(player.GetChunks().PositionOf(index));
109 random.Next<unsigned char>() % Chunk::width,
110 random.Next<unsigned char>() % Chunk::height,
111 random.Next<unsigned char>() % Chunk::depth
115 //glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.entity->Position());
116 //float dist = dot(diff, diff);
117 //if (dist > despawn_range || dist < spawn_distance) {
121 // check if the spawn block and the one above it are loaded and inhabitable
122 BlockLookup spawn_block(player.GetChunks()[index], pos);
123 if (!spawn_block || spawn_block.GetType().collide_block) {
127 BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
128 if (!head_block || head_block.GetType().collide_block) {
132 Spawn(player.GetEntity(), chunk, glm::vec3(pos) + glm::vec3(0.5f));
135 void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) {
136 glm::vec3 rot(0.000001f);
137 rot.x *= (random.Next<unsigned short>() % 1024);
138 rot.y *= (random.Next<unsigned short>() % 1024);
139 rot.z *= (random.Next<unsigned short>() % 1024);
141 Entity &e = world.AddEntity();
142 e.Position(chunk, pos);
143 e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
144 e.WorldCollidable(true);
145 RandomSkeleton().Instantiate(e.GetModel());
146 e.AngularVelocity(rot);
149 ctrl = new RandomWalk(e, random.Next<std::uint64_t>());
152 ctrl = new Chaser(world, e, reference);
155 controllers.emplace_back(ctrl);
158 CompositeModel &Spawner::RandomSkeleton() noexcept {
159 std::size_t offset = (random.Next<std::size_t>() % skeletons_length) + skeletons_offset;
160 return skeletons[offset];