1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include "TeamManager.h"
#include <algorithm>
#include <numeric>
#include <random>
#include <utility>
#include <entt/entity/registry.hpp>
#include <kurator/sim/components.h>
#include <kurator/sim/events.h>
#include <kurator/sim/State.h>
#include <kurator/universe/ShipType.h>
namespace kurator
{
namespace sim
{
TeamManager::TeamManager() :
teams {},
generator {std::random_device{}()}
{
}
void
TeamManager::extend(State& ctx)
{
auto ships = ctx.registry.view<universe::ShipType, sim::Team>();
for (const auto& [entity, type, team] : ships.each())
add(team.id, entity);
}
void
TeamManager::add(int team, entt::entity entity)
{
for (int i = teams.size(); i < team + 1; ++i)
teams.emplace_back();
teams.at(team).push_back(std::move(entity));
}
entt::entity
TeamManager::random(const int exclude)
{
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> 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));
}
void
TeamManager::update(State& ctx)
{
for (auto& members : teams) {
auto is_valid = [&](entt::entity entity){ return !ctx.registry.valid(entity); };
members.erase(std::remove_if(members.begin(), members.end(), is_valid), members.end());
}
const auto left = std::count_if(teams.begin(), teams.end(), [](const Team& team){ return team.size() > 0; });
if (left < 2)
ctx.dispatcher.trigger(End{});
}
} // namespace sim
} // namespace kurator
|