From 726a147becd9cfac381eca455b0ff6986581d4e1 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 13 Dec 2022 00:11:42 +0100 Subject: Random target selection is now really random --- sim/src/BaseBattle.cpp | 2 +- sim/src/TeamManager.cpp | 18 ++++++++++++------ sim/src/TeamManager.h | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) (limited to 'sim') diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 945ef89..b85c0eb 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -64,7 +64,7 @@ BaseBattle::pick_random_targets() auto view = _registry.view(); for (auto&& [entity, team, ai] : view.each()) { if (!_registry.valid(ai.target)) - ai.target = manager.random((team.id + 1) % 2); // FIXME + ai.target = manager.random(team.id); } } diff --git a/sim/src/TeamManager.cpp b/sim/src/TeamManager.cpp index 2dc3196..b792297 100644 --- a/sim/src/TeamManager.cpp +++ b/sim/src/TeamManager.cpp @@ -1,9 +1,9 @@ #include "TeamManager.h" #include +#include #include #include -#include #include @@ -38,13 +38,19 @@ TeamManager::get(int team) const entt::entity -TeamManager::random(int team) +TeamManager::random(const int exclude) { - auto& members = teams.at(team); - if (members.size() == 0) + std::vector open; + open.resize(teams.size()); + std::iota(open.begin(), open.end(), 0); + auto is_closed = [&](const int team){ return team == exclude || teams.at(team).empty(); }; + open.erase(std::remove_if(open.begin(), open.end(), is_closed), open.end()); + if (open.empty()) return entt::null; - std::uniform_int_distribution uniform{0, members.size() - 1}; - return members.at(uniform(generator)); + std::uniform_int_distribution team{0, open.size() - 1}; + const auto& members = teams.at(open.at(team(generator))); + std::uniform_int_distribution member{0, members.size() - 1}; + return members.at(member(generator)); } diff --git a/sim/src/TeamManager.h b/sim/src/TeamManager.h index abc6d23..050af94 100644 --- a/sim/src/TeamManager.h +++ b/sim/src/TeamManager.h @@ -19,7 +19,7 @@ public: TeamManager(); void add(int team, entt::entity entity); Team get(int team) const; - entt::entity random(int team); + entt::entity random(int exclude); void clear(entt::registry& registry); private: std::vector teams; -- cgit v1.1