X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fai%2FSpawner.cpp;h=946e3519c3fb359d8d6bba766c36d42adb4440df;hb=b066e776622f96e906600a0c4a08de392bd03676;hp=eaaa1f8a03f30b4d1df690d6a1da0fffc0bcd954;hpb=68f47f2824989b21ff9a480a367a6d0a41804f41;p=blank.git diff --git a/src/ai/Spawner.cpp b/src/ai/Spawner.cpp index eaaa1f8..946e351 100644 --- a/src/ai/Spawner.cpp +++ b/src/ai/Spawner.cpp @@ -10,10 +10,14 @@ #include "../world/Entity.hpp" #include "../world/World.hpp" +#include + +using namespace std; + namespace blank { -Spawner::Spawner(World &world, Skeletons &skeletons, std::uint64_t seed) +Spawner::Spawner(World &world, Skeletons &skeletons, uint64_t seed) : world(world) , skeletons(skeletons) , controllers() @@ -22,7 +26,9 @@ Spawner::Spawner(World &world, Skeletons &skeletons, std::uint64_t seed) , despawn_range(128 * 128) , spawn_distance(16 * 16) , max_entities(16) -, chunk_range(4) { +, chunk_range(4) +, skeletons_offset(0) +, skeletons_length(skeletons.Size()) { timer.Start(); } @@ -33,6 +39,15 @@ Spawner::~Spawner() { } +void Spawner::LimitSkeletons(size_t begin, size_t end) { + if (begin >= skeletons.Size() || end > skeletons.Size() || begin >= end) { + cout << "warning, skeleton limit out of bounds or invalid range given" << endl; + } else { + skeletons_offset = begin; + skeletons_length = end - begin; + } +} + void Spawner::Update(int dt) { CheckDespawn(); timer.Update(dt); @@ -57,7 +72,7 @@ void Spawner::CheckDespawn() noexcept { } bool safe = false; for (const Player &ref : refs) { - glm::vec3 diff(ref.entity->AbsoluteDifference(e)); + glm::vec3 diff(ref.GetEntity().AbsoluteDifference(e)); if (dot(diff, diff) < despawn_range) { safe = true; break; @@ -75,16 +90,20 @@ void Spawner::CheckDespawn() noexcept { } void Spawner::TrySpawn() { - if (controllers.size() >= max_entities) return; + if (controllers.size() >= max_entities || skeletons_length == 0) return; // select random player to punish auto &players = world.Players(); if (players.size() == 0) return; - const Player &player = players[random.Next() % players.size()]; + size_t player_num = random.Next() % players.size(); + auto i = players.begin(), end = players.end(); + for (; player_num > 0 && i != end; ++i, --player_num) { + } + const Player &player = *i; - int index = random.Next() % player.chunks->TotalChunks(); + int index = random.Next() % player.GetChunks().TotalChunks(); - glm::ivec3 chunk(player.chunks->PositionOf(index)); + glm::ivec3 chunk(player.GetChunks().PositionOf(index)); glm::ivec3 pos( random.Next() % Chunk::width, @@ -100,7 +119,7 @@ void Spawner::TrySpawn() { //} // check if the spawn block and the one above it are loaded and inhabitable - BlockLookup spawn_block((*player.chunks)[index], pos); + BlockLookup spawn_block(player.GetChunks()[index], pos); if (!spawn_block || spawn_block.GetType().collide_block) { return; } @@ -110,7 +129,7 @@ void Spawner::TrySpawn() { return; } - Spawn(*player.entity, chunk, glm::vec3(pos) + glm::vec3(0.5f)); + Spawn(player.GetEntity(), chunk, glm::vec3(pos) + glm::vec3(0.5f)); } void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) { @@ -123,17 +142,22 @@ void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 e.Position(chunk, pos); e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } }); e.WorldCollidable(true); - skeletons[random.Next() % skeletons.Size()].Instantiate(e.GetModel()); + RandomSkeleton().Instantiate(e.GetModel()); e.AngularVelocity(rot); Controller *ctrl; if (random()) { ctrl = new RandomWalk(e, random.Next()); - e.Name("spawned walker"); + e.Name("walker"); } else { ctrl = new Chaser(world, e, reference); - e.Name("spawned chaser"); + e.Name("chaser"); } controllers.emplace_back(ctrl); } +CompositeModel &Spawner::RandomSkeleton() noexcept { + std::size_t offset = (random.Next() % skeletons_length) + skeletons_offset; + return skeletons[offset]; +} + }