1 string cache_mutatormsg;
2 string cache_lastmutatormsg;
4 // client counts for each team
6 // # of bots on those teams
7 float cb1, cb2, cb3, cb4;
9 //float audit_teams_time;
11 void TeamchangeFrags(entity e)
16 vector TeamColor(float teem)
21 return '1 0.0625 0.0625';
23 return '0.0625 0.0625 1';
33 string TeamName(float t)
35 return strcat(Team_ColorName(t), " Team");
37 string ColoredTeamName(float t)
39 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
41 string TeamNoName(float t)
43 // fixme: Search for team entities and get their .netname's!
52 return "Neutral Team";
55 void runematch_init();
59 void LogTeamchange(float player_id, float team_number, float type)
61 if(!autocvar_sv_eventlog)
67 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
70 void default_delayedinit()
72 if(!scores_initialized)
76 void ActivateTeamplay()
78 serverflags |= SERVERFLAG_TEAMPLAY;
82 void InitGameplayMode()
84 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
86 qualifying_override = -1;
90 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
95 MapInfo_LoadMapSettings(mapname);
97 serverflags &~= SERVERFLAG_TEAMPLAY;
99 if not(cvar_value_issafe(world.fog))
101 print("The current map contains a potentially harmful fog setting, ignored\n");
102 world.fog = string_null;
104 if(MapInfo_Map_fog != "")
105 if(MapInfo_Map_fog == "none")
106 world.fog = string_null;
108 world.fog = strzone(MapInfo_Map_fog);
109 clientstuff = strzone(MapInfo_Map_clientstuff);
111 MapInfo_ClearTemps();
113 // set both here, gamemode can override it later
114 timelimit_override = autocvar_timelimit_override;
115 fraglimit_override = autocvar_fraglimit_override;
116 leadlimit_override = autocvar_leadlimit_override;
117 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
127 if(autocvar_g_tdm_team_spawns)
128 have_team_spawns = -1; // request team spawns
134 fraglimit_override = autocvar_g_domination_point_limit;
135 leadlimit_override = autocvar_g_domination_point_leadlimit;
136 MUTATOR_ADD(gamemode_domination);
137 have_team_spawns = -1; // request team spawns
143 fraglimit_override = autocvar_capturelimit_override;
144 leadlimit_override = autocvar_captureleadlimit_override;
145 MUTATOR_ADD(gamemode_ctf);
146 have_team_spawns = -1; // request team spawns
151 fraglimit_override = 0; // no limits in TD - it's a survival mode
152 leadlimit_override = 0;
153 MUTATOR_ADD(gamemode_td);
158 // ActivateTeamplay();
159 fraglimit_override = autocvar_g_runematch_point_limit;
160 leadlimit_override = autocvar_g_runematch_point_leadlimit;
166 fraglimit_override = autocvar_g_lms_lives_override;
167 leadlimit_override = 0; // not supported by LMS
168 if(fraglimit_override == 0)
169 fraglimit_override = -1;
170 lms_lowest_lives = 9999;
177 fraglimit_override = autocvar_g_arena_point_limit;
178 leadlimit_override = autocvar_g_arena_point_leadlimit;
179 maxspawned = autocvar_g_arena_maxspawned;
182 arena_roundbased = autocvar_g_arena_roundbased;
188 fraglimit_override = autocvar_g_ca_point_limit;
189 leadlimit_override = autocvar_g_ca_point_leadlimit;
190 precache_sound("ctf/red_capture.wav");
191 precache_sound("ctf/blue_capture.wav");
196 fraglimit_override = autocvar_g_keyhunt_point_limit;
197 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
198 MUTATOR_ADD(gamemode_keyhunt);
204 fraglimit_override = autocvar_g_freezetag_point_limit;
205 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
206 MUTATOR_ADD(gamemode_freezetag);
212 ScoreRules_assault();
213 have_team_spawns = -1; // request team spawns
219 have_team_spawns = -1; // request team spawns
220 MUTATOR_ADD(gamemode_onslaught);
226 if(autocvar_g_race_teams)
229 race_teams = bound(2, autocvar_g_race_teams, 4);
230 have_team_spawns = -1; // request team spawns
235 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
236 fraglimit_override = autocvar_g_race_laps_limit;
237 leadlimit_override = 0; // currently not supported by race
242 g_race_qualifying = 1;
243 fraglimit_override = 0;
244 leadlimit_override = 0;
249 fraglimit_override = autocvar_g_nexball_goallimit;
250 leadlimit_override = autocvar_g_nexball_goalleadlimit;
252 have_team_spawns = -1; // request team spawns
253 MUTATOR_ADD(gamemode_nexball);
258 MUTATOR_ADD(gamemode_keepaway);
264 cache_mutatormsg = strzone("");
265 cache_lastmutatormsg = strzone("");
267 // enforce the server's universal frag/time limits
268 if(!autocvar_g_campaign)
270 if(fraglimit_override >= 0)
271 cvar_set("fraglimit", ftos(fraglimit_override));
272 if(timelimit_override >= 0)
273 cvar_set("timelimit", ftos(timelimit_override));
274 if(leadlimit_override >= 0)
275 cvar_set("leadlimit", ftos(leadlimit_override));
276 if(qualifying_override >= 0)
277 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
282 // we need to find out the correct value for g_race_qualifying
283 if(autocvar_g_campaign)
285 g_race_qualifying = 1;
287 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
289 g_race_qualifying = 2;
290 race_fraglimit = autocvar_fraglimit;
291 race_leadlimit = autocvar_leadlimit;
292 race_timelimit = autocvar_timelimit;
293 cvar_set("fraglimit", "0");
294 cvar_set("leadlimit", "0");
295 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
298 g_race_qualifying = 0;
303 if(g_race_qualifying)
304 independent_players = 1;
309 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
312 string GetClientVersionMessage() {
314 if (self.version_mismatch) {
315 if(self.version < autocvar_gameversion) {
316 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
318 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
321 versionmsg = "^2client version and server version are compatible.^8";
326 string getwelcomemessage(void)
328 string s, modifications, motd;
331 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
332 modifications = ret_string;
335 modifications = strcat(modifications, ", MinstaGib");
338 if(g_weaponarena_random)
339 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
341 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
343 if(autocvar_g_start_weapon_laser == 0)
344 modifications = strcat(modifications, ", No start weapons");
345 if(autocvar_sv_gravity < 800)
346 modifications = strcat(modifications, ", Low gravity");
347 if(g_cloaked && !g_cts)
348 modifications = strcat(modifications, ", Cloaked");
350 modifications = strcat(modifications, ", Hook");
352 modifications = strcat(modifications, ", Midair");
354 modifications = strcat(modifications, ", Piñata");
355 if(g_weapon_stay && !g_cts)
356 modifications = strcat(modifications, ", Weapons stay");
358 modifications = strcat(modifications, ", Blood loss");
360 modifications = strcat(modifications, ", Jet pack");
361 if(autocvar_g_powerups == 0)
362 modifications = strcat(modifications, ", No powerups");
363 if(autocvar_g_powerups > 0)
364 modifications = strcat(modifications, ", Powerups");
365 modifications = substring(modifications, 2, strlen(modifications) - 2);
367 string versionmessage;
368 versionmessage = GetClientVersionMessage();
370 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
371 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
373 if(modifications != "")
374 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
376 if (g_grappling_hook)
377 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
379 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
381 if(cache_lastmutatormsg)
382 strunzone(cache_lastmutatormsg);
384 strunzone(cache_mutatormsg);
385 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
386 cache_mutatormsg = strzone(cache_lastmutatormsg);
389 if (cache_mutatormsg != "") {
390 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
393 motd = autocvar_sv_motd;
395 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
400 void SetPlayerColors(entity pl, float _color)
404 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
406 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
407 pl.clientcolors = 16*cl + cl;*/
410 pants = _color & 0x0F;
411 shirt = _color & 0xF0;
415 setcolor(pl, 16*pants + pants);
417 setcolor(pl, shirt + pants);
421 void SetPlayerTeam(entity pl, float t, float s, float noprint)
426 _color = COLOR_TEAM4 - 1;
428 _color = COLOR_TEAM3 - 1;
430 _color = COLOR_TEAM2 - 1;
432 _color = COLOR_TEAM1 - 1;
434 SetPlayerColors(pl,_color);
437 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
440 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
445 // set c1...c4 to show what teams are allowed
446 void CheckAllowedTeams (entity for_whom)
452 c1 = c2 = c3 = c4 = -1;
453 cb1 = cb2 = cb3 = cb4 = 0;
455 teament_name = string_null;
458 // onslaught is special
459 head = findchain(classname, "onslaught_generator");
462 if (head.team == COLOR_TEAM1) c1 = 0;
463 if (head.team == COLOR_TEAM2) c2 = 0;
464 if (head.team == COLOR_TEAM3) c3 = 0;
465 if (head.team == COLOR_TEAM4) c4 = 0;
469 else if(g_domination)
470 teament_name = "dom_team";
472 teament_name = "ctf_team";
474 teament_name = "tdm_team";
476 teament_name = "nexball_team";
478 c1 = c2 = 0; // Assault always has 2 teams
481 // cover anything else by treating it like tdm with no teams spawned
488 MUTATOR_CALLHOOK(GetTeamCount);
492 c1 = c2 = c3 = c4 = 0;
499 // find out what teams are allowed if necessary
502 head = find(world, classname, teament_name);
505 if(!(g_domination && head.netname == ""))
507 if(head.team == COLOR_TEAM1)
509 else if(head.team == COLOR_TEAM2)
511 else if(head.team == COLOR_TEAM3)
513 else if(head.team == COLOR_TEAM4)
516 head = find(head, classname, teament_name);
520 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
522 if(autocvar_bot_vs_human && for_whom)
524 if(autocvar_bot_vs_human > 0)
527 if(clienttype(for_whom) == CLIENTTYPE_BOT)
535 if(clienttype(for_whom) == CLIENTTYPE_BOT)
542 // if player has a forced team, ONLY allow that one
543 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
545 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
547 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
549 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
553 float PlayerValue(entity p)
556 // FIXME: it always returns 1...
559 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
560 // teams that are allowed will now have their player counts stored in c1...c4
561 void GetTeamCounts(entity ignore)
565 // now count how many players are on each team already
567 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
568 // also remember the lowest-scoring player
570 FOR_EACH_CLIENT(head)
573 if(head.classname == "player")
575 else if(head.team_forced > 0)
576 t = head.team_forced; // reserve the spot
579 if(head != ignore)// && head.netname != "")
581 value = PlayerValue(head);
582 if(clienttype(head) == CLIENTTYPE_BOT)
621 // if the player who has a forced team has not joined yet, reserve the spot
622 if(autocvar_g_campaign)
624 switch(autocvar_g_campaign_forceteam)
626 case 1: if(c1 == cb1) ++c1; break;
627 case 2: if(c2 == cb2) ++c2; break;
628 case 3: if(c3 == cb3) ++c3; break;
629 case 4: if(c4 == cb4) ++c4; break;
634 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
636 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
638 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
642 case 1: ca = c1; cba = cb1; sa = team1_score; break;
643 case 2: ca = c2; cba = cb2; sa = team2_score; break;
644 case 3: ca = c3; cba = cb3; sa = team3_score; break;
645 case 4: ca = c4; cba = cb4; sa = team4_score; break;
649 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
650 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
651 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
652 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
663 if(clienttype(e) == CLIENTTYPE_REAL)
672 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
688 // the more we're at the end of the match, the more take scores into account
689 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
696 // returns # of smallest team (1, 2, 3, 4)
697 // NOTE: Assumes CheckAllowedTeams has already been called!
698 float FindSmallestTeam(entity pl, float ignore_pl)
703 // find out what teams are available
704 //CheckAllowedTeams();
706 // make sure there are at least 2 teams to join
708 totalteams = totalteams + 1;
710 totalteams = totalteams + 1;
712 totalteams = totalteams + 1;
714 totalteams = totalteams + 1;
716 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
721 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
722 return 1; // special case for campaign and player joining
723 else if(g_domination)
724 error("Too few teams available for domination\n");
726 error("Too few teams available for ctf\n");
728 error("Too few teams available for key hunt\n");
730 error("Too few teams available for freeze tag\n");
732 error("Too few teams available for team deathmatch\n");
735 // count how many players are in each team
739 GetTeamCounts(world);
741 RandomSelection_Init();
744 if(TeamSmallerEqThanTeam(2, t, pl))
746 if(TeamSmallerEqThanTeam(3, t, pl))
748 if(TeamSmallerEqThanTeam(4, t, pl))
751 // now t is the minimum, or A minimum!
752 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
753 RandomSelection_Add(world, 1, string_null, 1, 1);
754 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
755 RandomSelection_Add(world, 2, string_null, 1, 1);
756 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
757 RandomSelection_Add(world, 3, string_null, 1, 1);
758 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
759 RandomSelection_Add(world, 4, string_null, 1, 1);
761 return RandomSelection_chosen_float;
764 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
766 float smallest, selectedteam;
768 // don't join a team if we're not playing a team game
772 // find out what teams are available
773 CheckAllowedTeams(pl);
775 // if we don't care what team he ends up on, put him on whatever team he entered as.
776 // if he's not on a valid team, then let other code put him on the smallest team
779 if( c1 >= 0 && pl.team == COLOR_TEAM1)
780 selectedteam = pl.team;
781 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
782 selectedteam = pl.team;
783 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
784 selectedteam = pl.team;
785 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
786 selectedteam = pl.team;
792 if(!only_return_best)
794 SetPlayerColors(pl, selectedteam - 1);
796 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
797 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
798 LogTeamchange(pl.playerid, pl.team, 99);
802 // otherwise end up on the smallest team (handled below)
805 smallest = FindSmallestTeam(pl, TRUE);
807 if(!only_return_best && !pl.bot_forced_team)
809 TeamchangeFrags(self);
812 SetPlayerColors(pl, COLOR_TEAM1 - 1);
814 else if(smallest == 2)
816 SetPlayerColors(pl, COLOR_TEAM2 - 1);
818 else if(smallest == 3)
820 SetPlayerColors(pl, COLOR_TEAM3 - 1);
822 else if(smallest == 4)
824 SetPlayerColors(pl, COLOR_TEAM4 - 1);
828 error("smallest team: invalid team\n");
831 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
833 if(pl.deadflag == DEAD_NO)
834 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
840 //void() ctf_playerchanged;
841 void SV_ChangeTeam(float _color)
843 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
845 // in normal deathmatch we can just apply the color and we're done
847 SetPlayerColors(self, _color);
851 scolor = self.clientcolors & 0x0F;
852 dcolor = _color & 0x0F;
854 if(scolor == COLOR_TEAM1 - 1)
856 else if(scolor == COLOR_TEAM2 - 1)
858 else if(scolor == COLOR_TEAM3 - 1)
860 else // if(scolor == COLOR_TEAM4 - 1)
862 if(dcolor == COLOR_TEAM1 - 1)
864 else if(dcolor == COLOR_TEAM2 - 1)
866 else if(dcolor == COLOR_TEAM3 - 1)
868 else // if(dcolor == COLOR_TEAM4 - 1)
871 CheckAllowedTeams(self);
873 if(dteam == 1 && c1 < 0) dteam = 4;
874 if(dteam == 4 && c4 < 0) dteam = 3;
875 if(dteam == 3 && c3 < 0) dteam = 2;
876 if(dteam == 2 && c2 < 0) dteam = 1;
878 // not changing teams
881 //bprint("same team change\n");
882 SetPlayerTeam(self, dteam, steam, TRUE);
886 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
887 sprint(self, "Team changes not allowed\n");
888 return; // changing teams is not allowed
891 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
892 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
895 if(!TeamSmallerEqThanTeam(dteam, steam, self))
897 sprint(self, "Cannot change to a larger/better/shinier team\n");
902 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
904 if(self.classname == "player" && steam != dteam)
906 // reduce frags during a team change
907 TeamchangeFrags(self);
910 SetPlayerTeam(self, dteam, steam, FALSE);
912 if(self.classname == "player" && steam != dteam)
914 // kill player when changing teams
915 if(self.deadflag == DEAD_NO)
916 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
920 void ShufflePlayerOutOfTeam (float source_team)
922 float smallestteam, smallestteam_count, steam;
923 float lowest_bot_score, lowest_player_score;
924 entity head, lowest_bot, lowest_player, selected;
927 smallestteam_count = 999999999;
929 if(c1 >= 0 && c1 < smallestteam_count)
932 smallestteam_count = c1;
934 if(c2 >= 0 && c2 < smallestteam_count)
937 smallestteam_count = c2;
939 if(c3 >= 0 && c3 < smallestteam_count)
942 smallestteam_count = c3;
944 if(c4 >= 0 && c4 < smallestteam_count)
947 smallestteam_count = c4;
952 bprint("warning: no smallest team\n");
958 else if(source_team == 2)
960 else if(source_team == 3)
962 else // if(source_team == 4)
966 lowest_bot_score = 999999999;
967 lowest_player = world;
968 lowest_player_score = 999999999;
970 // find the lowest-scoring player & bot of that team
971 FOR_EACH_PLAYER(head)
973 if(head.team == steam)
977 if(head.totalfrags < lowest_bot_score)
980 lowest_bot_score = head.totalfrags;
985 if(head.totalfrags < lowest_player_score)
987 lowest_player = head;
988 lowest_player_score = head.totalfrags;
994 // prefers to move a bot...
995 if(lowest_bot != world)
996 selected = lowest_bot;
997 // but it will move a player if it has to
999 selected = lowest_player;
1000 // don't do anything if it couldn't find anyone
1003 bprint("warning: couldn't find a player to move from team\n");
1007 // smallest team gains a member
1008 if(smallestteam == 1)
1012 else if(smallestteam == 2)
1016 else if(smallestteam == 3)
1020 else if(smallestteam == 4)
1026 bprint("warning: destination team invalid\n");
1029 // source team loses a member
1030 if(source_team == 1)
1034 else if(source_team == 2)
1038 else if(source_team == 3)
1042 else if(source_team == 4)
1048 bprint("warning: source team invalid\n");
1052 // move the player to the new team
1053 TeamchangeFrags(selected);
1054 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1056 if(selected.deadflag == DEAD_NO)
1057 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1058 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1061 // code from here on is just to support maps that don't have team entities
1062 void tdm_spawnteam (string teamname, float teamcolor)
1066 e.classname = "tdm_team";
1067 e.netname = teamname;
1072 // spawn some default teams if the map is not set up for tdm
1073 void tdm_spawnteams()
1077 numteams = autocvar_g_tdm_teams_override;
1079 numteams = autocvar_g_tdm_teams;
1080 numteams = bound(2, numteams, 4);
1082 tdm_spawnteam("Red", COLOR_TEAM1-1);
1083 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1085 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1087 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1090 void tdm_delayedinit()
1092 // if no teams are found, spawn defaults
1093 if (find(world, classname, "tdm_team") == world)
1099 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);