1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 /* based on code from GlobalSynced.{cpp,h} */
4 
5 #ifndef TEAMHANDLER_H
6 #define TEAMHANDLER_H
7 
8 #include "System/creg/creg_cond.h"
9 #include "Team.h"
10 #include "AllyTeam.h"
11 
12 class CGameSetup;
13 
14 
15 /** @brief Handles teams and allyteams */
16 class CTeamHandler
17 {
18 	CR_DECLARE_STRUCT(CTeamHandler)
19 
20 public:
21 	CTeamHandler();
22 	~CTeamHandler();
23 
24 	void LoadFromSetup(const CGameSetup* setup);
25 
26 	/**
27 	 * @brief Team
28 	 * @param i index to fetch
29 	 * @return CTeam pointer
30 	 *
31 	 * Accesses a CTeam instance at a given index
32 	 */
Team(int i)33 	CTeam* Team(int i) const
34 	{
35 		assert(i >=            0);
36 		assert(i <  teams.size());
37 		return teams[i];
38 	}
39 
40 	/**
41 	 * @brief ally
42 	 * @param a first allyteam
43 	 * @param b second allyteam
44 	 * @return allies[a][b]
45 	 *
46 	 * Returns ally at [a][b]
47 	 */
Ally(int a,int b)48 	bool Ally(int a, int b) const { return allyTeams[a].allies[b]; }
49 
50 	/**
51 	 * @brief ally team
52 	 * @param team team to find
53 	 * @return the ally team
54 	 *
55 	 * returns the team2ally at given index
56 	 */
AllyTeam(int team)57 	int AllyTeam(int team) const { return teams[team]->teamAllyteam; }
GetAllyTeam(size_t id)58 	::AllyTeam& GetAllyTeam(size_t id) { return allyTeams[id]; };
59 
ValidAllyTeam(size_t id)60 	bool ValidAllyTeam(size_t id) const
61 	{
62 		return (id < allyTeams.size());
63 	}
64 
65 	/**
66 	 * @brief allied teams
67 	 * @param a first team
68 	 * @param b second team
69 	 * @return whether teams are allied
70 	 *
71 	 * Tests whether teams are allied
72 	 */
AlliedTeams(int a,int b)73 	bool AlliedTeams(int a, int b) const { return allyTeams[AllyTeam(a)].allies[AllyTeam(b)]; }
74 
75 	/**
76 	 * @brief set ally team
77 	 * @param team team to set
78 	 * @param allyteam allyteam to set
79 	 *
80 	 * Sets team's ally team
81 	 */
SetAllyTeam(int team,int allyteam)82 	void SetAllyTeam(int team, int allyteam) { teams[team]->teamAllyteam = allyteam; }
83 
84 	/**
85 	 * @brief set ally
86 	 * @param allyteamA first allyteam
87 	 * @param allyteamB second allyteam
88 	 * @param allied whether or not these two teams are allied
89 	 *
90 	 * Sets two allyteams to be allied or not
91 	 */
SetAlly(int allyteamA,int allyteamB,bool allied)92 	void SetAlly(int allyteamA, int allyteamB, bool allied) { allyTeams[allyteamA].allies[allyteamB] = allied; }
93 
94 	// accessors
GaiaTeamID()95 	int GaiaTeamID() const { return gaiaTeamID; }
GaiaAllyTeamID()96 	int GaiaAllyTeamID() const { return gaiaAllyTeamID; }
97 
98 	// number of teams and allyteams that were *INITIALLY* part
99 	// of a game (teams.size() and allyTeams.size() are runtime
100 	// constants), ie. including teams that died during it and
101 	// are actually NO LONGER "active"
102 	//
103 	// NOTE: TEAM INSTANCES ARE NEVER DELETED UNTIL SHUTDOWN, THEY ONLY GET MARKED AS DEAD!
ActiveTeams()104 	int ActiveTeams() const { return teams.size(); }
ActiveAllyTeams()105 	int ActiveAllyTeams() const { return allyTeams.size(); }
106 
IsValidTeam(int id)107 	bool IsValidTeam(int id) const {
108 		return ((id >= 0) && (id < ActiveTeams()));
109 	}
IsActiveTeam(int id)110 	bool IsActiveTeam(int id) const {
111 		return (IsValidTeam(id) && !teams[id]->isDead);
112 	}
113 
IsValidAllyTeam(int id)114 	bool IsValidAllyTeam(int id) const {
115 		return ((id >= 0) && (id < ActiveAllyTeams()));
116 	}
IsActiveAllyTeam(int id)117 	bool IsActiveAllyTeam(int id) const {
118 		return (IsValidAllyTeam(id) /*&& !allyTeams[id].isDead*/);
119 	}
120 
121 	unsigned int GetNumTeamsInAllyTeam(unsigned int allyTeam, bool countDeadTeams) const;
122 
123 	void GameFrame(int frameNum);
124 
125 	void UpdateTeamUnitLimitsPreSpawn(int liveTeamNum);
126 	void UpdateTeamUnitLimitsPreDeath(int deadTeamNum);
127 
128 private:
129 
130 	/**
131 	 * @brief gaia team
132 	 *
133 	 * gaia's team id
134 	 */
135 	int gaiaTeamID;
136 
137 	/**
138 	 * @brief gaia team
139 	 *
140 	 * gaia's team id
141 	 */
142 	int gaiaAllyTeamID;
143 
144 	/**
145 	 * @brief teams
146 	 *
147 	 * Array of CTeam instances for teams in game
148 	 */
149 	std::vector<CTeam *> teams;
150 	std::vector< ::AllyTeam > allyTeams;
151 };
152 
153 extern CTeamHandler* teamHandler;
154 
155 #endif // !TEAMHANDLER_H
156