]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/teamplay.qh
Transifex autosync
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qh
1 #pragma once
2
3 int autocvar_teamplay_mode;
4
5 bool autocvar_g_changeteam_banned;
6 bool autocvar_teamplay_lockonrestart;
7
8 bool autocvar_g_balance_teams;
9 bool autocvar_g_balance_teams_prevent_imbalance;
10 bool autocvar_g_balance_teams_queue;
11 bool autocvar_g_balance_teams_remove;
12 int autocvar_g_balance_teams_remove_wait;
13
14 string autocvar_g_forced_team_otherwise;
15
16 bool lockteams;
17
18 .int team_forced; // can be a team number to force a team, or 0 for default action, or -1 for forced spectator
19
20 #define IS_QUEUE_NEEDED(ignore) \
21         (teamplay && !warmup_stage && autocvar_g_balance_teams_queue && !autocvar_g_campaign \
22         && TeamBalance_AreEqual(ignore, true))
23
24 // ========================== Global teams API ================================
25
26 void Team_InitTeams();
27
28 /// \brief Returns the global team entity at the given index.
29 /// \param[in] index Index of the team.
30 /// \return Global team entity at the given index.
31 entity Team_GetTeamFromIndex(int index);
32
33 /// \brief Returns the global team entity that corresponds to the given TEAM_NUM
34 /// value.
35 /// \param[in] team_num Team value. See TEAM_NUM constants.
36 /// \return Global team entity that corresponds to the given TEAM_NUM value.
37 entity Team_GetTeam(int team_num);
38
39 // ========================= Team specific API ================================
40
41 /// \brief Returns the score of the team.
42 /// \param[in] team_ent Team entity.
43 /// \return Score of the team.
44 float Team_GetTeamScore(entity team_ent);
45
46 /// \brief Sets the score of the team.
47 /// \param[in,out] team_ent Team entity.
48 /// \param[in] score Score to set.
49 void Team_SetTeamScore(entity team_ent, float score);
50
51 /// \brief Returns the number of alive players in a team.
52 /// \param[in] team_ent Team entity.
53 /// \return Number of alive players in a team.
54 int Team_GetNumberOfAlivePlayers(entity team_ent);
55
56 /// \brief Sets the number of alive players in a team.
57 /// \param[in,out] team_ent Team entity.
58 /// \param[in] number Number of players to set.
59 void Team_SetNumberOfAlivePlayers(entity team_ent, int number);
60
61 /// \brief Returns the winner team.
62 /// \return Winner team or 0 if 2 or more teams have alive players or -1 if no team has any alive players.
63 int Team_GetWinnerAliveTeam();
64
65 /// \brief Returns the number of alive teams.
66 /// \return Number of alive teams.
67 int Team_GetNumberOfAliveTeams();
68
69 /// \brief Returns the winner team.
70 /// \param[in] min_owned_items Minimum number of items the winner team must have.
71 /// \return Winner team or 0 if 2 or more teams own items or -1 if no team own any items.
72 int Team_GetWinnerTeam_WithOwnedItems(int min_owned_items);
73
74 /// \brief Returns the number of items owned by a team.
75 /// \param[in] team_ent Team entity.
76 /// \return Number of items owned by a team.
77 int Team_GetNumberOfOwnedItems(entity team_ent);
78
79 /// \brief Sets the number of items owned by a team.
80 /// \param[in,out] team_ent Team entity.
81 /// \param[in] number Number of items to set.
82 void Team_SetNumberOfOwnedItems(entity team_ent, int number);
83
84 /// \brief Returns the number of teams that own items.
85 /// \return Number of teams that own items.
86 int Team_GetNumberOfTeamsWithOwnedItems();
87
88 // ======================= Entity specific API ================================
89
90 void setcolor(entity this, int clr);
91
92 /// \brief Returns whether the given entity belongs to a valid team.
93 /// \param[in] this Entity to check.
94 /// \return True if entity belongs to a valid team, false otherwise.
95 bool Entity_HasValidTeam(entity this);
96
97 /// \brief Returns the team index of the given entity.
98 /// \param[in] this Entity to check.
99 /// \return Team index of the entity.
100 int Entity_GetTeamIndex(entity this);
101
102 /// \brief Returns the team entity of the given entity.
103 /// \param[in] this Entity to check.
104 /// \return Team entity of the given entity or NULL if the entity doesn't belong
105 /// to any team.
106 entity Entity_GetTeam(entity this);
107
108 void SetPlayerColors(entity player, float _color);
109
110 /// \brief Sets the team of the player using its index.
111 /// \param[in,out] player Player to adjust.
112 /// \param[in] index Index of the team to set.
113 /// \return True if team switch was successful, false otherwise.
114 bool Player_SetTeamIndex(entity player, int index);
115
116 enum
117 {
118         TEAM_CHANGE_AUTO = 2, ///< The team was selected by autobalance.
119         TEAM_CHANGE_MANUAL = 3, ///< Player has manually selected their team.
120         TEAM_CHANGE_SPECTATOR = 4 ///< Player is joining spectators. //TODO: Remove?
121 };
122
123 bool QueuedPlayersReady(entity this, bool checkspecificteam);
124
125 /// \brief Sets the team of the player.
126 /// \param[in,out] player Player to adjust.
127 /// \param[in] team_index Index of the team to set.
128 /// \param[in] type Type of the team change. See TEAM_CHANGE constants.
129 /// \return True if team switch was successful, false otherwise.
130 bool SetPlayerTeam(entity player, int team_index, int type);
131
132 /// \brief Sets the team of the player with all sanity checks.
133 /// \param[in,out] player Player to adjust.
134 /// \param[in] team_index Index of the team to set.
135 void Player_SetTeamIndexChecked(entity player, int team_index);
136
137 /// \brief Moves player to the specified team.
138 /// \param[in,out] client Client to move.
139 /// \param[in] team_index Index of the team.
140 /// \param[in] type ???
141 /// \return True on success, false otherwise.
142 bool MoveToTeam(entity client, int team_index, int type);
143
144 enum
145 {
146         TEAM_FORCE_SPECTATOR = -1, ///< Force the player to spectator team.
147         TEAM_FORCE_DEFAULT = 0 ///< Don't force any team.
148 };
149
150 /// \brief Returns whether player has real forced team. Spectator team is
151 /// ignored.
152 /// \param[in] player Player to check.
153 /// \return True if player has real forced team, false otherwise.
154 bool Player_HasRealForcedTeam(entity player);
155
156 /// \brief Returns the index of the forced team of the given player.
157 /// \param[in] player Player to check.
158 /// \return Index of the forced team.
159 int Player_GetForcedTeamIndex(entity player);
160
161 /// \brief Sets the index of the forced team of the given player.
162 /// \param[in,out] player Player to adjust.
163 /// \param[in] team_index Index of the team to set.
164 void Player_SetForcedTeamIndex(entity player, int team_index);
165
166 /// \brief Determines the forced team of the player using current global config.
167 /// \param[in,out] player Player to adjust.
168 void Player_DetermineForcedTeam(entity player);
169
170 // ========================= Team balance API =================================
171
172 /// \brief Assigns the given player to a team that will make the game most
173 /// balanced.
174 /// \param[in,out] player Player to assign.
175 void TeamBalance_JoinBestTeam(entity player);
176
177 /// \brief Checks whether the player can join teams according to global
178 /// configuration and mutator settings.
179 /// \param[in] for_whom Player to check for. Pass NULL for global rules.
180 /// \return Team balance entity that holds information about teams. This entity
181 /// will be automatically destroyed on the next frame but you are encouraged to
182 /// manually destroy it by calling TeamBalance_Destroy for performance reasons.
183 entity TeamBalance_CheckAllowedTeams(entity for_whom);
184
185 /// \brief Destroy the team balance entity.
186 /// \param[in,out] balance Team balance entity to destroy.
187 /// \note Team balance entity is allowed to be NULL.
188 void TeamBalance_Destroy(entity balance);
189
190 /// \brief Returns the bitmask of allowed teams.
191 /// \param[in] balance Team balance entity.
192 /// \return Bitmask of allowed teams.
193 int TeamBalance_GetAllowedTeams(entity balance);
194
195 bool TeamBalance_AreEqual(entity ignore, bool would_leave);
196 void TeamBalance_RemoveExcessPlayers(entity ignore);
197 /** Joins queued player(s) to team(s) with a shortage,
198  * this should be more robust than only replacing the player that left.
199  * Chooses players with a specific team preference first
200  * to increase chances of everyone getting what they want.
201  * Returns true if the teams are now balanced.
202  */
203 bool TeamBalance_QueuedPlayersTagIn(entity ignore);
204
205 /// \brief Returns whether the team change to the specified team is allowed.
206 /// \param[in] balance Team balance entity.
207 /// \param[in] index Index of the team.
208 /// \return True if team change to the specified team is allowed, false
209 /// otherwise.
210 bool TeamBalance_IsTeamAllowed(entity balance, int index);
211
212 /// \brief Counts the number of players and various other information about
213 /// each team.
214 /// \param[in,out] balance Team balance entity.
215 /// \param[in] ignore Player to ignore. This is useful if you plan to switch the
216 /// player's team. Pass NULL for global information.
217 /// \note This function updates the internal state of the team balance entity.
218 void TeamBalance_GetTeamCounts(entity balance, entity ignore);
219
220 /// \brief Returns the number of players (both humans and bots) in a team.
221 /// \param[in] balance Team balance entity.
222 /// \param[in] index Index of the team.
223 /// \return Number of player (both humans and bots) in a team.
224 /// \note You need to call TeamBalance_GetTeamCounts before calling this
225 /// function.
226 int TeamBalance_GetNumberOfPlayers(entity balance, int index);
227
228 /// \brief Finds the team that will make the game most balanced if the player
229 /// joins it.
230 /// \param[in] balance Team balance entity.
231 /// \param[in] player Player to check.
232 /// \param[in] ignore_player ???
233 /// \return Index of the team that will make the game most balanced if the
234 /// player joins it. If there are several equally good teams available, the
235 /// function will pick a random one.
236 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player);
237
238 /// \brief Returns the bitmask of the teams that will make the game most
239 /// balanced if the player joins any of them.
240 /// \param[in] balance Team balance entity.
241 /// \param[in] player Player to check.
242 /// \param[in] use_score Whether to take into account team scores.
243 /// \return Bitmask of the teams that will make the game most balanced if the
244 /// player joins any of them.
245 /// \note You need to call TeamBalance_GetTeamCounts before calling this
246 /// function.
247 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score);
248
249 /// \brief Describes the result of comparing teams.
250 enum
251 {
252         TEAMS_COMPARE_INVALID, ///< One or both teams are invalid.
253         TEAMS_COMPARE_LESS, ///< First team is less than the second one.
254         TEAMS_COMPARE_EQUAL, ///< Both teams are equal.
255         TEAMS_COMPARE_GREATER ///< First team the greater than the second one.
256 };
257
258 /// \brief Compares two teams for the purposes of game balance.
259 /// \param[in] balance Team balance entity.
260 /// \param[in] team_index_a Index of the first team.
261 /// \param[in] team_index_b Index of the second team.
262 /// \param[in] player Player to check.
263 /// \param[in] use_score Whether to take into account team scores.
264 /// \return TEAMS_COMPARE value. See above.
265 /// \note You need to call TeamBalance_GetTeamCounts before calling this
266 /// function.
267 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
268         entity player, bool use_score);
269
270 /// \brief Switches a bot from one team to another if teams are not balanced.
271 void TeamBalance_AutoBalanceBots();
272
273 /// \brief Returns the index of the team with most players that is contained in
274 /// the given bitmask of teams.
275 /// \param[in] balance Team balance entity.
276 /// \param[in] teams Bitmask of teams to search in.
277 /// \return Index of the team with most players.
278 int TeamBalance_GetLargestTeamIndex(entity balance, int teams);
279
280 /// \brief Returns the player who is the most suitable for switching between
281 /// the given teams.
282 /// \param[in] source_team_index Index of the team to search in.
283 /// \param[in] destination_team_index Index of the team to switch to.
284 /// \param[in] is_bot True to search for bot, false for human.
285 /// \return Player who is the most suitable for switching between the given
286 /// teams or NULL if not found.
287 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
288         int destination_team_index, bool is_bot);
289
290 // ============================ Internal API ==================================
291
292 void LogTeamChange(float player_id, float team_number, int type);
293
294 /// \brief Kills player as a result of team change.
295 /// \param[in,out] player Player to kill.
296 void KillPlayerForTeamChange(entity player);
297
298 /// \brief Returns whether the team change to the specified team is allowed.
299 /// \param[in] balance Team balance entity.
300 /// \param[in] index Index of the team.
301 /// \return True if team change to the specified team is allowed, false
302 /// otherwise.
303 /// \note This function bypasses all the sanity checks.
304 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index);
305
306 /// \brief Bans team change to all teams except the given one.
307 /// \param[in,out] balance Team balance entity.
308 /// \param[in] index Index of the team.
309 void TeamBalance_BanTeamsExcept(entity balance, int index);
310
311 /// \brief Returns the team entity of the team balance entity at the given
312 /// index.
313 /// \param[in] balance Team balance entity.
314 /// \param[in] index Index of the team.
315 /// \return Team entity of the team balance entity at the given index.
316 entity TeamBalance_GetTeamFromIndex(entity balance, int index);
317
318 /// \brief Returns the team entity of the team balance entity that corresponds
319 /// to the given TEAM_NUM value.
320 /// \param[in] balance Team balance entity.
321 /// \param[in] team_num Team value. See TEAM_NUM constants.
322 /// \return Team entity of the team balance entity that corresponds to the given
323 /// TEAM_NUM value.
324 entity TeamBalance_GetTeam(entity balance, int team_num);
325
326 /// \brief Returns whether the team is allowed.
327 /// \param[in] team_ent Team entity.
328 /// \return True if team is allowed, false otherwise.
329 bool TeamBalanceTeam_IsAllowed(entity team_ent);
330
331 /// \brief Returns the number of players (both humans and bots) in a team.
332 /// \param[in] team_ent Team entity.
333 /// \return Number of player (both humans and bots) in a team.
334 /// \note You need to call TeamBalance_GetTeamCounts before calling this
335 /// function.
336 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent);
337
338 /// \brief Returns the number of bots in a team.
339 /// \param[in] team_ent Team entity.
340 /// \return Number of bots in a team.
341 /// \note You need to call TeamBalance_GetTeamCounts before calling this
342 /// function.
343 int TeamBalanceTeam_GetNumberOfBots(entity team_ent);
344
345 /// \brief Compares two teams for the purposes of game balance.
346 /// \param[in] team_a First team.
347 /// \param[in] team_b Second team.
348 /// \param[in] player Player to check.
349 /// \param[in] use_score Whether to take into account team scores.
350 /// \return TEAMS_COMPARE value. See above.
351 /// \note You need to call TeamBalance_GetTeamCounts before calling this
352 /// function.
353 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_index_b,
354         entity player, bool use_score);
355
356 /// \brief Called when the player changes color with the "color" command.
357 /// Note that the "color" command is always called early on player connection
358 /// \param[in,out] player Player that requested a new color.
359 /// \param[in] new_color Requested color.
360 void SV_ChangeTeam(entity player, int new_color);