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 InitGameplayMode()
44 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
48 // currently, NetRadiant's limit is 131072 qu for each side
49 // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
50 // set the distance according to map size but don't go over the limit to avoid issues with float precision
51 // in case somebody makes extremely large maps
52 max_shot_distance = min(230000, vlen(world.maxs - world.mins));
54 MapInfo_LoadMapSettings(mapname);
55 GameRules_teams(false);
57 if (!cvar_value_issafe(world.fog))
59 LOG_INFO("The current map contains a potentially harmful fog setting, ignored");
60 world.fog = string_null;
62 if(MapInfo_Map_fog != "")
63 if(MapInfo_Map_fog == "none")
64 world.fog = string_null;
66 world.fog = strzone(MapInfo_Map_fog);
67 clientstuff = strzone(MapInfo_Map_clientstuff);
71 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
73 cache_mutatormsg = strzone("");
74 cache_lastmutatormsg = strzone("");
76 InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
79 string GetClientVersionMessage(entity this)
81 if (CS(this).version_mismatch) {
82 if(CS(this).version < autocvar_gameversion) {
83 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
84 "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
86 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
87 "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
90 return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
94 string getwelcomemessage(entity this)
96 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
97 string modifications = M_ARGV(0, string);
101 if(g_weaponarena_random)
102 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
104 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
106 else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
107 modifications = strcat(modifications, ", No start weapons");
108 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
109 modifications = strcat(modifications, ", Low gravity");
110 if(g_weapon_stay && !g_cts)
111 modifications = strcat(modifications, ", Weapons stay");
113 modifications = strcat(modifications, ", Jet pack");
114 if(autocvar_g_powerups == 0)
115 modifications = strcat(modifications, ", No powerups");
116 if(autocvar_g_powerups > 0)
117 modifications = strcat(modifications, ", Powerups");
118 modifications = substring(modifications, 2, strlen(modifications) - 2);
120 string versionmessage = GetClientVersionMessage(this);
121 string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
123 if(modifications != "")
124 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
126 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
128 if(cache_lastmutatormsg)
129 strunzone(cache_lastmutatormsg);
131 strunzone(cache_mutatormsg);
132 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
133 cache_mutatormsg = strzone(cache_lastmutatormsg);
136 if (cache_mutatormsg != "") {
137 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
140 string mutator_msg = "";
141 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
142 mutator_msg = M_ARGV(0, string);
144 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
146 string motd = autocvar_sv_motd;
148 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
153 void setcolor(entity this, int clr)
156 this.clientcolors = clr;
157 this.team = (clr & 15) + 1;
159 builtin_setcolor(this, clr);
163 void SetPlayerColors(entity pl, float _color)
167 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
169 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
170 pl.clientcolors = 16*cl + cl;*/
173 pants = _color & 0x0F;
174 shirt = _color & 0xF0;
178 setcolor(pl, 16*pants + pants);
180 setcolor(pl, shirt + pants);
184 void SetPlayerTeam(entity pl, float t, float s, float noprint)
189 _color = NUM_TEAM_4 - 1;
191 _color = NUM_TEAM_3 - 1;
193 _color = NUM_TEAM_2 - 1;
195 _color = NUM_TEAM_1 - 1;
197 SetPlayerColors(pl,_color);
200 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
203 bprint(playername(pl, false), "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
208 // set c1...c4 to show what teams are allowed
209 void CheckAllowedTeams (entity for_whom)
213 c1 = c2 = c3 = c4 = -1;
214 cb1 = cb2 = cb3 = cb4 = 0;
216 string teament_name = string_null;
218 bool mutator_returnvalue = MUTATOR_CALLHOOK(CheckAllowedTeams, teams_mask, teament_name, for_whom);
219 teams_mask = M_ARGV(0, float);
220 teament_name = M_ARGV(1, string);
222 if(!mutator_returnvalue)
224 if(teams_mask & BIT(0)) c1 = 0;
225 if(teams_mask & BIT(1)) c2 = 0;
226 if(teams_mask & BIT(2)) c3 = 0;
227 if(teams_mask & BIT(3)) c4 = 0;
230 // find out what teams are allowed if necessary
233 entity head = find(NULL, classname, teament_name);
238 case NUM_TEAM_1: c1 = 0; break;
239 case NUM_TEAM_2: c2 = 0; break;
240 case NUM_TEAM_3: c3 = 0; break;
241 case NUM_TEAM_4: c4 = 0; break;
244 head = find(head, classname, teament_name);
248 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
249 if(AvailableTeams() == 2)
250 if(autocvar_bot_vs_human && for_whom)
252 if(autocvar_bot_vs_human > 0)
254 // find last team available
256 if(IS_BOT_CLIENT(for_whom))
258 if(c4 >= 0) { c3 = c2 = c1 = -1; }
259 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
260 else { c4 = c3 = c1 = -1; }
261 // no further cases, we know at least 2 teams exist
265 if(c1 >= 0) { c2 = c3 = c4 = -1; }
266 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
267 else { c1 = c2 = c4 = -1; }
268 // no further cases, bots have one of the teams
273 // find first team available
275 if(IS_BOT_CLIENT(for_whom))
277 if(c1 >= 0) { c2 = c3 = c4 = -1; }
278 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
279 else { c1 = c2 = c4 = -1; }
280 // no further cases, we know at least 2 teams exist
284 if(c4 >= 0) { c3 = c2 = c1 = -1; }
285 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
286 else { c4 = c3 = c1 = -1; }
287 // no further cases, bots have one of the teams
295 // if player has a forced team, ONLY allow that one
296 if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
298 else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
300 else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
302 else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
306 float PlayerValue(entity p)
309 // FIXME: it always returns 1...
312 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
313 // teams that are allowed will now have their player counts stored in c1...c4
314 void GetTeamCounts(entity ignore)
317 // now count how many players are on each team already
319 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
320 // also remember the lowest-scoring player
322 FOREACH_CLIENT(true, {
324 if(IS_PLAYER(it) || it.caplayer)
326 else if(it.team_forced > 0)
327 t = it.team_forced; // reserve the spot
330 if(it != ignore)// && it.netname != "")
332 value = PlayerValue(it);
333 if(IS_BOT_CLIENT(it))
345 else if(t == NUM_TEAM_2)
353 else if(t == NUM_TEAM_3)
361 else if(t == NUM_TEAM_4)
372 // if the player who has a forced team has not joined yet, reserve the spot
373 if(autocvar_g_campaign)
375 switch(autocvar_g_campaign_forceteam)
377 case 1: if(c1 == cb1) ++c1; break;
378 case 2: if(c2 == cb2) ++c2; break;
379 case 3: if(c3 == cb3) ++c3; break;
380 case 4: if(c4 == cb4) ++c4; break;
385 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
387 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
389 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
393 case 1: ca = c1; cba = cb1; sa = team1_score; break;
394 case 2: ca = c2; cba = cb2; sa = team2_score; break;
395 case 3: ca = c3; cba = cb3; sa = team3_score; break;
396 case 4: ca = c4; cba = cb4; sa = team4_score; break;
400 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
401 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
402 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
403 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
414 if(IS_REAL_CLIENT(e))
423 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
439 // the more we're at the end of the match, the more take scores into account
440 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
447 // returns # of smallest team (1, 2, 3, 4)
448 // NOTE: Assumes CheckAllowedTeams has already been called!
449 float FindSmallestTeam(entity pl, float ignore_pl)
452 int t = 1; // initialize with a random team?
458 // find out what teams are available
459 //CheckAllowedTeams();
461 // make sure there are at least 2 teams to join
463 totalteams = totalteams + 1;
465 totalteams = totalteams + 1;
467 totalteams = totalteams + 1;
469 totalteams = totalteams + 1;
471 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
476 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
477 return 1; // special case for campaign and player joining
478 else if(totalteams == 1) // single team
479 LOG_TRACEF("Only 1 team available for %s, you may need to fix your map", MapInfo_Type_ToString(MapInfo_CurrentGametype()));
480 else // no teams, major no no
481 error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
484 // count how many players are in each team
490 RandomSelection_Init();
492 if(TeamSmallerEqThanTeam(1, t, pl))
494 if(TeamSmallerEqThanTeam(2, t, pl))
496 if(TeamSmallerEqThanTeam(3, t, pl))
498 if(TeamSmallerEqThanTeam(4, t, pl))
501 // now t is the minimum, or A minimum!
502 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
503 RandomSelection_AddFloat(1, 1, 1);
504 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
505 RandomSelection_AddFloat(2, 1, 1);
506 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
507 RandomSelection_AddFloat(3, 1, 1);
508 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
509 RandomSelection_AddFloat(4, 1, 1);
511 return RandomSelection_chosen_float;
514 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
516 float smallest, selectedteam;
518 // don't join a team if we're not playing a team game
522 // find out what teams are available
523 CheckAllowedTeams(this);
525 // if we don't care what team he ends up on, put him on whatever team he entered as.
526 // if he's not on a valid team, then let other code put him on the smallest team
529 if( c1 >= 0 && this.team == NUM_TEAM_1)
530 selectedteam = this.team;
531 else if(c2 >= 0 && this.team == NUM_TEAM_2)
532 selectedteam = this.team;
533 else if(c3 >= 0 && this.team == NUM_TEAM_3)
534 selectedteam = this.team;
535 else if(c4 >= 0 && this.team == NUM_TEAM_4)
536 selectedteam = this.team;
542 if(!only_return_best)
544 SetPlayerColors(this, selectedteam - 1);
546 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
547 // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
548 LogTeamchange(this.playerid, this.team, 99);
552 // otherwise end up on the smallest team (handled below)
555 smallest = FindSmallestTeam(this, true);
557 if(!only_return_best && !this.bot_forced_team)
559 TeamchangeFrags(this);
562 SetPlayerColors(this, NUM_TEAM_1 - 1);
564 else if(smallest == 2)
566 SetPlayerColors(this, NUM_TEAM_2 - 1);
568 else if(smallest == 3)
570 SetPlayerColors(this, NUM_TEAM_3 - 1);
572 else if(smallest == 4)
574 SetPlayerColors(this, NUM_TEAM_4 - 1);
578 error("smallest team: invalid team\n");
581 LogTeamchange(this.playerid, this.team, 2); // log auto join
584 Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
590 //void() ctf_playerchanged;
591 void SV_ChangeTeam(entity this, float _color)
593 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
595 // in normal deathmatch we can just apply the color and we're done
597 SetPlayerColors(this, _color);
601 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
602 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
609 scolor = this.clientcolors & 0x0F;
610 dcolor = _color & 0x0F;
612 if(scolor == NUM_TEAM_1 - 1)
614 else if(scolor == NUM_TEAM_2 - 1)
616 else if(scolor == NUM_TEAM_3 - 1)
618 else // if(scolor == NUM_TEAM_4 - 1)
620 if(dcolor == NUM_TEAM_1 - 1)
622 else if(dcolor == NUM_TEAM_2 - 1)
624 else if(dcolor == NUM_TEAM_3 - 1)
626 else // if(dcolor == NUM_TEAM_4 - 1)
629 CheckAllowedTeams(this);
631 if(dteam == 1 && c1 < 0) dteam = 4;
632 if(dteam == 4 && c4 < 0) dteam = 3;
633 if(dteam == 3 && c3 < 0) dteam = 2;
634 if(dteam == 2 && c2 < 0) dteam = 1;
636 // not changing teams
639 //bprint("same team change\n");
640 SetPlayerTeam(this, dteam, steam, true);
644 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
645 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
646 return; // changing teams is not allowed
649 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
650 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
653 if(!TeamSmallerEqThanTeam(dteam, steam, this))
655 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
660 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
662 if(IS_PLAYER(this) && steam != dteam)
664 // reduce frags during a team change
665 TeamchangeFrags(this);
668 MUTATOR_CALLHOOK(Player_ChangeTeam, this, steam, dteam);
670 SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
672 if(IS_PLAYER(this) && steam != dteam)
674 // kill player when changing teams
676 Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
680 void ShufflePlayerOutOfTeam (float source_team)
682 float smallestteam, smallestteam_count, steam;
683 float lowest_bot_score, lowest_player_score;
684 entity lowest_bot, lowest_player, selected;
687 smallestteam_count = 999999999;
689 if(c1 >= 0 && c1 < smallestteam_count)
692 smallestteam_count = c1;
694 if(c2 >= 0 && c2 < smallestteam_count)
697 smallestteam_count = c2;
699 if(c3 >= 0 && c3 < smallestteam_count)
702 smallestteam_count = c3;
704 if(c4 >= 0 && c4 < smallestteam_count)
707 smallestteam_count = c4;
712 bprint("warning: no smallest team\n");
718 else if(source_team == 2)
720 else if(source_team == 3)
722 else // if(source_team == 4)
726 lowest_bot_score = 999999999;
727 lowest_player = NULL;
728 lowest_player_score = 999999999;
730 // find the lowest-scoring player & bot of that team
731 FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, {
734 if(it.totalfrags < lowest_bot_score)
737 lowest_bot_score = it.totalfrags;
742 if(it.totalfrags < lowest_player_score)
745 lowest_player_score = it.totalfrags;
750 // prefers to move a bot...
751 if(lowest_bot != NULL)
752 selected = lowest_bot;
753 // but it will move a player if it has to
755 selected = lowest_player;
756 // don't do anything if it couldn't find anyone
759 bprint("warning: couldn't find a player to move from team\n");
763 // smallest team gains a member
764 if(smallestteam == 1)
768 else if(smallestteam == 2)
772 else if(smallestteam == 3)
776 else if(smallestteam == 4)
782 bprint("warning: destination team invalid\n");
785 // source team loses a member
790 else if(source_team == 2)
794 else if(source_team == 3)
798 else if(source_team == 4)
804 bprint("warning: source team invalid\n");
808 // move the player to the new team
809 TeamchangeFrags(selected);
810 SetPlayerTeam(selected, smallestteam, source_team, false);
812 if(!IS_DEAD(selected))
813 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
814 Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);