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