6 #include "scores_rules.qh"
10 #include "command/vote.qh"
12 #include "mutators/_mod.qh"
14 #include "../common/deathtypes/all.qh"
15 #include "../common/gamemodes/_mod.qh"
16 #include "../common/teams.qh"
18 void TeamchangeFrags(entity e)
23 void LogTeamchange(float player_id, float team_number, float type)
25 if(!autocvar_sv_eventlog)
31 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
34 void default_delayedinit(entity this)
36 if(!scores_initialized)
40 void ActivateTeamplay()
42 serverflags |= SERVERFLAG_TEAMPLAY;
44 cvar_set("teamplay", "2"); // DP needs this for sending proper getstatus replies.
47 void InitGameplayMode()
51 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
55 // currently, NetRadiant's limit is 131072 qu for each side
56 // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
57 // set the distance according to map size but don't go over the limit to avoid issues with float precision
58 // in case somebody makes extremely large maps
59 max_shot_distance = min(230000, vlen(world.maxs - world.mins));
61 MapInfo_LoadMapSettings(mapname);
62 serverflags &= ~SERVERFLAG_TEAMPLAY;
64 cvar_set("teamplay", "0"); // DP needs this for sending proper getstatus replies.
66 if (!cvar_value_issafe(world.fog))
68 LOG_INFO("The current map contains a potentially harmful fog setting, ignored\n");
69 world.fog = string_null;
71 if(MapInfo_Map_fog != "")
72 if(MapInfo_Map_fog == "none")
73 world.fog = string_null;
75 world.fog = strzone(MapInfo_Map_fog);
76 clientstuff = strzone(MapInfo_Map_clientstuff);
80 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
82 cache_mutatormsg = strzone("");
83 cache_lastmutatormsg = strzone("");
85 InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
88 string GetClientVersionMessage(entity this)
90 if (this.version_mismatch) {
91 if(this.version < autocvar_gameversion) {
92 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
93 "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
95 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
96 "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
99 return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
103 string getwelcomemessage(entity this)
105 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
106 string modifications = M_ARGV(0, string);
110 if(g_weaponarena_random)
111 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
113 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
115 else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
116 modifications = strcat(modifications, ", No start weapons");
117 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
118 modifications = strcat(modifications, ", Low gravity");
119 if(g_weapon_stay && !g_cts)
120 modifications = strcat(modifications, ", Weapons stay");
122 modifications = strcat(modifications, ", Jet pack");
123 if(autocvar_g_powerups == 0)
124 modifications = strcat(modifications, ", No powerups");
125 if(autocvar_g_powerups > 0)
126 modifications = strcat(modifications, ", Powerups");
127 modifications = substring(modifications, 2, strlen(modifications) - 2);
129 string versionmessage = GetClientVersionMessage(this);
130 string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
132 if(modifications != "")
133 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
135 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
137 if(cache_lastmutatormsg)
138 strunzone(cache_lastmutatormsg);
140 strunzone(cache_mutatormsg);
141 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
142 cache_mutatormsg = strzone(cache_lastmutatormsg);
145 if (cache_mutatormsg != "") {
146 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
149 string mutator_msg = "";
150 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
151 mutator_msg = M_ARGV(0, string);
153 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
155 string motd = autocvar_sv_motd;
157 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
162 void setcolor(entity this, int clr)
165 this.clientcolors = clr;
166 this.team = (clr & 15) + 1;
168 builtin_setcolor(this, clr);
172 void SetPlayerColors(entity pl, float _color)
176 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
178 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
179 pl.clientcolors = 16*cl + cl;*/
182 pants = _color & 0x0F;
183 shirt = _color & 0xF0;
187 setcolor(pl, 16*pants + pants);
189 setcolor(pl, shirt + pants);
193 bool SetPlayerTeamSimple(entity player, int teamnum)
195 return MoveToTeam(player, teamnum, 6);
198 void SetPlayerTeam(entity pl, float t, float s, float noprint)
204 float teamnum = Team_NumberToTeam(t);
205 SetPlayerTeamSimple(pl, teamnum);
206 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
211 bprint(playername(pl, false), "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
214 // set c1...c4 to show what teams are allowed
215 void CheckAllowedTeams (entity for_whom)
219 c1 = c2 = c3 = c4 = -1;
220 cb1 = cb2 = cb3 = cb4 = 0;
222 string teament_name = string_null;
224 bool mutator_returnvalue = MUTATOR_CALLHOOK(CheckAllowedTeams, teams_mask, teament_name, for_whom);
225 teams_mask = M_ARGV(0, float);
226 teament_name = M_ARGV(1, string);
228 if(!mutator_returnvalue)
230 if(teams_mask & BIT(0)) c1 = 0;
231 if(teams_mask & BIT(1)) c2 = 0;
232 if(teams_mask & BIT(2)) c3 = 0;
233 if(teams_mask & BIT(3)) c4 = 0;
236 // find out what teams are allowed if necessary
239 entity head = find(NULL, classname, teament_name);
244 case NUM_TEAM_1: c1 = 0; break;
245 case NUM_TEAM_2: c2 = 0; break;
246 case NUM_TEAM_3: c3 = 0; break;
247 case NUM_TEAM_4: c4 = 0; break;
250 head = find(head, classname, teament_name);
254 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
255 if(AvailableTeams() == 2)
256 if(autocvar_bot_vs_human && for_whom)
258 if(autocvar_bot_vs_human > 0)
260 // find last team available
262 if(IS_BOT_CLIENT(for_whom))
264 if(c4 >= 0) { c3 = c2 = c1 = -1; }
265 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
266 else { c4 = c3 = c1 = -1; }
267 // no further cases, we know at least 2 teams exist
271 if(c1 >= 0) { c2 = c3 = c4 = -1; }
272 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
273 else { c1 = c2 = c4 = -1; }
274 // no further cases, bots have one of the teams
279 // find first team available
281 if(IS_BOT_CLIENT(for_whom))
283 if(c1 >= 0) { c2 = c3 = c4 = -1; }
284 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
285 else { c1 = c2 = c4 = -1; }
286 // no further cases, we know at least 2 teams exist
290 if(c4 >= 0) { c3 = c2 = c1 = -1; }
291 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
292 else { c4 = c3 = c1 = -1; }
293 // no further cases, bots have one of the teams
301 // if player has a forced team, ONLY allow that one
302 if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
304 else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
306 else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
308 else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
312 float PlayerValue(entity p)
315 // FIXME: it always returns 1...
318 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
319 // teams that are allowed will now have their player counts stored in c1...c4
320 void GetTeamCounts(entity ignore)
323 // now count how many players are on each team already
325 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
326 // also remember the lowest-scoring player
328 FOREACH_CLIENT(true, LAMBDA(
330 if(IS_PLAYER(it) || it.caplayer)
332 else if(it.team_forced > 0)
333 t = it.team_forced; // reserve the spot
336 if(it != ignore)// && it.netname != "")
338 value = PlayerValue(it);
339 if(IS_BOT_CLIENT(it))
378 // if the player who has a forced team has not joined yet, reserve the spot
379 if(autocvar_g_campaign)
381 switch(autocvar_g_campaign_forceteam)
383 case 1: if(c1 == cb1) ++c1; break;
384 case 2: if(c2 == cb2) ++c2; break;
385 case 3: if(c3 == cb3) ++c3; break;
386 case 4: if(c4 == cb4) ++c4; break;
391 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
393 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
395 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
399 case 1: ca = c1; cba = cb1; sa = team1_score; break;
400 case 2: ca = c2; cba = cb2; sa = team2_score; break;
401 case 3: ca = c3; cba = cb3; sa = team3_score; break;
402 case 4: ca = c4; cba = cb4; sa = team4_score; break;
406 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
407 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
408 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
409 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
420 if(IS_REAL_CLIENT(e))
429 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
445 // the more we're at the end of the match, the more take scores into account
446 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
453 // returns # of smallest team (1, 2, 3, 4)
454 // NOTE: Assumes CheckAllowedTeams has already been called!
455 float FindSmallestTeam(entity pl, float ignore_pl)
458 int t = 1; // initialize with a random team?
464 // find out what teams are available
465 //CheckAllowedTeams();
467 // make sure there are at least 2 teams to join
469 totalteams = totalteams + 1;
471 totalteams = totalteams + 1;
473 totalteams = totalteams + 1;
475 totalteams = totalteams + 1;
477 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
482 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
483 return 1; // special case for campaign and player joining
484 else if(totalteams == 1) // single team
485 LOG_TRACEF("Only 1 team available for %s, you may need to fix your map", MapInfo_Type_ToString(MapInfo_CurrentGametype()));
486 else // no teams, major no no
487 error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
490 // count how many players are in each team
496 RandomSelection_Init();
498 if(TeamSmallerEqThanTeam(1, t, pl))
500 if(TeamSmallerEqThanTeam(2, t, pl))
502 if(TeamSmallerEqThanTeam(3, t, pl))
504 if(TeamSmallerEqThanTeam(4, t, pl))
507 // now t is the minimum, or A minimum!
508 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
509 RandomSelection_AddFloat(1, 1, 1);
510 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
511 RandomSelection_AddFloat(2, 1, 1);
512 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
513 RandomSelection_AddFloat(3, 1, 1);
514 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
515 RandomSelection_AddFloat(4, 1, 1);
517 return RandomSelection_chosen_float;
520 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
522 float smallest, selectedteam;
524 // don't join a team if we're not playing a team game
528 // find out what teams are available
529 CheckAllowedTeams(this);
531 // if we don't care what team he ends up on, put him on whatever team he entered as.
532 // if he's not on a valid team, then let other code put him on the smallest team
535 if( c1 >= 0 && this.team == NUM_TEAM_1)
536 selectedteam = this.team;
537 else if(c2 >= 0 && this.team == NUM_TEAM_2)
538 selectedteam = this.team;
539 else if(c3 >= 0 && this.team == NUM_TEAM_3)
540 selectedteam = this.team;
541 else if(c4 >= 0 && this.team == NUM_TEAM_4)
542 selectedteam = this.team;
548 if(!only_return_best)
550 SetPlayerTeamSimple(this, selectedteam);
552 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
553 // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
554 LogTeamchange(this.playerid, this.team, 99);
558 // otherwise end up on the smallest team (handled below)
561 smallest = FindSmallestTeam(this, true);
563 if(!only_return_best && !this.bot_forced_team)
565 smallest = Team_NumberToTeam(smallest);
568 TeamchangeFrags(this);
569 SetPlayerTeamSimple(this, smallest);
573 error("smallest team: invalid team\n");
576 LogTeamchange(this.playerid, this.team, 2); // log auto join
579 Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
585 //void() ctf_playerchanged;
586 void SV_ChangeTeam(entity this, float _color)
588 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
590 // in normal deathmatch we can just apply the color and we're done
592 SetPlayerColors(this, _color);
596 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
597 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
604 scolor = this.clientcolors & 0x0F;
605 dcolor = _color & 0x0F;
607 if(scolor == NUM_TEAM_1 - 1)
609 else if(scolor == NUM_TEAM_2 - 1)
611 else if(scolor == NUM_TEAM_3 - 1)
613 else // if(scolor == NUM_TEAM_4 - 1)
615 if(dcolor == NUM_TEAM_1 - 1)
617 else if(dcolor == NUM_TEAM_2 - 1)
619 else if(dcolor == NUM_TEAM_3 - 1)
621 else // if(dcolor == NUM_TEAM_4 - 1)
624 CheckAllowedTeams(this);
626 if(dteam == 1 && c1 < 0) dteam = 4;
627 if(dteam == 4 && c4 < 0) dteam = 3;
628 if(dteam == 3 && c3 < 0) dteam = 2;
629 if(dteam == 2 && c2 < 0) dteam = 1;
631 // not changing teams
634 SetPlayerTeam(this, dteam, steam, true);
638 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && this.wasplayer)) {
639 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
640 return; // changing teams is not allowed
643 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
644 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
647 if(!TeamSmallerEqThanTeam(dteam, steam, this))
649 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
654 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
656 if(IS_PLAYER(this) && steam != dteam)
658 // reduce frags during a team change
659 TeamchangeFrags(this);
662 SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
664 if(IS_PLAYER(this) && steam != dteam)
666 // kill player when changing teams
668 Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
672 void ShufflePlayerOutOfTeam (float source_team)
674 float smallestteam, smallestteam_count, steam;
675 float lowest_bot_score, lowest_player_score;
676 entity lowest_bot, lowest_player, selected;
679 smallestteam_count = 999999999;
681 if(c1 >= 0 && c1 < smallestteam_count)
684 smallestteam_count = c1;
686 if(c2 >= 0 && c2 < smallestteam_count)
689 smallestteam_count = c2;
691 if(c3 >= 0 && c3 < smallestteam_count)
694 smallestteam_count = c3;
696 if(c4 >= 0 && c4 < smallestteam_count)
699 smallestteam_count = c4;
704 bprint("warning: no smallest team\n");
710 else if(source_team == 2)
712 else if(source_team == 3)
714 else // if(source_team == 4)
718 lowest_bot_score = 999999999;
719 lowest_player = NULL;
720 lowest_player_score = 999999999;
722 // find the lowest-scoring player & bot of that team
723 FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, LAMBDA(
726 if(it.totalfrags < lowest_bot_score)
729 lowest_bot_score = it.totalfrags;
734 if(it.totalfrags < lowest_player_score)
737 lowest_player_score = it.totalfrags;
742 // prefers to move a bot...
743 if(lowest_bot != NULL)
744 selected = lowest_bot;
745 // but it will move a player if it has to
747 selected = lowest_player;
748 // don't do anything if it couldn't find anyone
751 bprint("warning: couldn't find a player to move from team\n");
755 // smallest team gains a member
756 if(smallestteam == 1)
760 else if(smallestteam == 2)
764 else if(smallestteam == 3)
768 else if(smallestteam == 4)
774 bprint("warning: destination team invalid\n");
777 // source team loses a member
782 else if(source_team == 2)
786 else if(source_team == 3)
790 else if(source_team == 4)
796 bprint("warning: source team invalid\n");
800 // move the player to the new team
801 TeamchangeFrags(selected);
802 SetPlayerTeam(selected, smallestteam, source_team, false);
804 if(!IS_DEAD(selected))
805 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
806 Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);