forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollective_teams.cpp
114 lines (94 loc) · 2.99 KB
/
collective_teams.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "stdafx.h"
#include "collective_teams.h"
#include "creature.h"
#include "team_order.h"
bool CollectiveTeams::contains(TeamId team, const Creature* c) const {
return teamInfo.at(team).creatures.contains(c);
}
void CollectiveTeams::add(TeamId team, Creature* c) {
if(!teamInfo[team].creatures.contains(c))
teamInfo[team].creatures.push_back(c);
}
void CollectiveTeams::remove(TeamId team, Creature* c) {
teamInfo[team].creatures.removeElement(c);
if (teamInfo[team].creatures.empty())
cancel(team);
}
void CollectiveTeams::activate(TeamId team) {
teamInfo[team].active = true;
}
void CollectiveTeams::deactivate(TeamId team) {
teamInfo[team].active = false;
}
void CollectiveTeams::setLeader(TeamId team, Creature* c) {
if (!teamInfo[team].creatures.contains(c))
add(team, c);
swap(teamInfo[team].creatures[0], teamInfo[team].creatures[*teamInfo[team].creatures.findElement(c)]);
}
void CollectiveTeams::rotateLeader(TeamId team) {
vector<Creature*> tmp = teamInfo.at(team).creatures;
teamInfo.at(team).creatures.clear();
for (int i = 1; i < tmp.size(); ++i)
teamInfo.at(team).creatures.push_back(tmp[i]);
teamInfo.at(team).creatures.push_back(tmp[0]);
}
Creature* CollectiveTeams::getLeader(TeamId team) const {
CHECK(!teamInfo.at(team).creatures.empty());
return teamInfo.at(team).creatures[0];
}
const vector<Creature*>& CollectiveTeams::getMembers(TeamId team) const {
return teamInfo.at(team).creatures;
}
vector<TeamId> CollectiveTeams::getContaining(const Creature* c) const {
vector<TeamId> ret;
for (auto& team : teamInfo)
if (contains(team.first, c))
ret.push_back(team.first);
return ret;
}
vector<TeamId> CollectiveTeams::getAll() const {
return getKeys(teamInfo);
}
vector<TeamId> CollectiveTeams::getActive(const Creature* c) const {
vector<TeamId> ret;
for (TeamId t : getContaining(c))
if (isActive(t))
ret.push_back(t);
return ret;
}
vector<TeamId> CollectiveTeams::getAllActive() const {
vector<TeamId> ret;
for (TeamId t : getKeys(teamInfo))
if (isActive(t))
ret.push_back(t);
return ret;
}
TeamId CollectiveTeams::create(vector<Creature*> c) {
CHECK(!c.empty());
teamInfo[nextId].creatures = c;
return nextId++;
}
bool CollectiveTeams::hasTeamOrder(TeamId id, TeamOrder order) const {
CHECK(exists(id));
return teamInfo.at(id).teamOrders.count(order);
}
void CollectiveTeams::setTeamOrder(TeamId id, TeamOrder order, bool state) {
CHECK(exists(id));
if (state) {
teamInfo.at(id).teamOrders.insert(order);
for (auto order2 : ENUM_ALL(TeamOrder))
if (order != order2 && conflict(order, order2))
teamInfo.at(id).teamOrders.erase(order2);
} else
teamInfo.at(id).teamOrders.erase(order);
}
bool CollectiveTeams::exists(TeamId id) const {
return teamInfo.count(id);
}
bool CollectiveTeams::isActive(TeamId team) const {
return teamInfo.at(team).active;
}
void CollectiveTeams::cancel(TeamId team) {
teamInfo.erase(team);
}
SERIALIZE_DEF(CollectiveTeams, teamInfo, nextId)