diff options
author | Aki <please@ignore.pl> | 2022-12-13 00:11:42 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-12-13 00:11:42 +0100 |
commit | 726a147becd9cfac381eca455b0ff6986581d4e1 (patch) | |
tree | 211f89f472609718035c368903bd4e2709b0a3da /sim | |
parent | 42d5170e3add3dc5e7ea2c944bcfd785a5390586 (diff) | |
download | kurator-726a147becd9cfac381eca455b0ff6986581d4e1.zip kurator-726a147becd9cfac381eca455b0ff6986581d4e1.tar.gz kurator-726a147becd9cfac381eca455b0ff6986581d4e1.tar.bz2 |
Random target selection is now really random
Diffstat (limited to 'sim')
-rw-r--r-- | sim/src/BaseBattle.cpp | 2 | ||||
-rw-r--r-- | sim/src/TeamManager.cpp | 18 | ||||
-rw-r--r-- | sim/src/TeamManager.h | 2 |
3 files changed, 14 insertions, 8 deletions
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<Team, AIState>(); 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 <algorithm> +#include <numeric> #include <random> #include <utility> -#include <vector> #include <entt/entity/registry.hpp> @@ -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<int> 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<Team::size_type> uniform{0, members.size() - 1}; - return members.at(uniform(generator)); + std::uniform_int_distribution<Team::size_type> team{0, open.size() - 1}; + const auto& members = teams.at(open.at(team(generator))); + std::uniform_int_distribution<Team::size_type> 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<Team> teams; |