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 // ActivateTeamplay();
152 fraglimit_override = autocvar_g_runematch_point_limit;
153 leadlimit_override = autocvar_g_runematch_point_leadlimit;
159 fraglimit_override = autocvar_g_lms_lives_override;
160 leadlimit_override = 0; // not supported by LMS
161 if(fraglimit_override == 0)
162 fraglimit_override = -1;
163 lms_lowest_lives = 9999;
170 fraglimit_override = autocvar_g_arena_point_limit;
171 leadlimit_override = autocvar_g_arena_point_leadlimit;
172 MUTATOR_ADD(gamemode_arena);
178 fraglimit_override = autocvar_g_ca_point_limit;
179 leadlimit_override = autocvar_g_ca_point_leadlimit;
180 MUTATOR_ADD(gamemode_ca);
186 fraglimit_override = autocvar_g_keyhunt_point_limit;
187 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
188 MUTATOR_ADD(gamemode_keyhunt);
194 fraglimit_override = autocvar_g_freezetag_point_limit;
195 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
196 MUTATOR_ADD(gamemode_freezetag);
202 ScoreRules_assault();
203 have_team_spawns = -1; // request team spawns
209 have_team_spawns = -1; // request team spawns
210 MUTATOR_ADD(gamemode_onslaught);
216 if(autocvar_g_race_teams)
219 race_teams = bound(2, autocvar_g_race_teams, 4);
220 have_team_spawns = -1; // request team spawns
225 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
226 fraglimit_override = autocvar_g_race_laps_limit;
227 leadlimit_override = 0; // currently not supported by race
232 g_race_qualifying = 1;
233 fraglimit_override = 0;
234 leadlimit_override = 0;
239 fraglimit_override = autocvar_g_nexball_goallimit;
240 leadlimit_override = autocvar_g_nexball_goalleadlimit;
242 have_team_spawns = -1; // request team spawns
243 MUTATOR_ADD(gamemode_nexball);
248 MUTATOR_ADD(gamemode_keepaway);
254 cache_mutatormsg = strzone("");
255 cache_lastmutatormsg = strzone("");
257 // enforce the server's universal frag/time limits
258 if(!autocvar_g_campaign)
260 if(fraglimit_override >= 0)
261 cvar_set("fraglimit", ftos(fraglimit_override));
262 if(timelimit_override >= 0)
263 cvar_set("timelimit", ftos(timelimit_override));
264 if(leadlimit_override >= 0)
265 cvar_set("leadlimit", ftos(leadlimit_override));
266 if(qualifying_override >= 0)
267 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
272 // we need to find out the correct value for g_race_qualifying
273 if(autocvar_g_campaign)
275 g_race_qualifying = 1;
277 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
279 g_race_qualifying = 2;
280 race_fraglimit = autocvar_fraglimit;
281 race_leadlimit = autocvar_leadlimit;
282 race_timelimit = autocvar_timelimit;
283 cvar_set("fraglimit", "0");
284 cvar_set("leadlimit", "0");
285 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
288 g_race_qualifying = 0;
293 if(g_race_qualifying)
294 independent_players = 1;
299 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
302 string GetClientVersionMessage() {
304 if (self.version_mismatch) {
305 if(self.version < autocvar_gameversion) {
306 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
308 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
311 versionmsg = "^2client version and server version are compatible.^8";
316 string getwelcomemessage(void)
318 string s, modifications, motd;
321 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
322 modifications = ret_string;
325 modifications = strcat(modifications, ", MinstaGib");
328 if(g_weaponarena_random)
329 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
331 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
333 if(autocvar_g_start_weapon_laser == 0)
334 modifications = strcat(modifications, ", No start weapons");
335 if(autocvar_sv_gravity < 800)
336 modifications = strcat(modifications, ", Low gravity");
337 if(g_cloaked && !g_cts)
338 modifications = strcat(modifications, ", Cloaked");
340 modifications = strcat(modifications, ", Hook");
342 modifications = strcat(modifications, ", Midair");
344 modifications = strcat(modifications, ", Piñata");
345 if(g_weapon_stay && !g_cts)
346 modifications = strcat(modifications, ", Weapons stay");
348 modifications = strcat(modifications, ", Blood loss");
350 modifications = strcat(modifications, ", Jet pack");
351 if(autocvar_g_powerups == 0)
352 modifications = strcat(modifications, ", No powerups");
353 if(autocvar_g_powerups > 0)
354 modifications = strcat(modifications, ", Powerups");
355 modifications = substring(modifications, 2, strlen(modifications) - 2);
357 string versionmessage;
358 versionmessage = GetClientVersionMessage();
360 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
361 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
363 if(modifications != "")
364 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
366 if (g_grappling_hook)
367 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
369 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
371 if(cache_lastmutatormsg)
372 strunzone(cache_lastmutatormsg);
374 strunzone(cache_mutatormsg);
375 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
376 cache_mutatormsg = strzone(cache_lastmutatormsg);
379 if (cache_mutatormsg != "") {
380 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
383 motd = autocvar_sv_motd;
385 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
390 void SetPlayerColors(entity pl, float _color)
394 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
396 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
397 pl.clientcolors = 16*cl + cl;*/
400 pants = _color & 0x0F;
401 shirt = _color & 0xF0;
405 setcolor(pl, 16*pants + pants);
407 setcolor(pl, shirt + pants);
411 void SetPlayerTeam(entity pl, float t, float s, float noprint)
416 _color = COLOR_TEAM4 - 1;
418 _color = COLOR_TEAM3 - 1;
420 _color = COLOR_TEAM2 - 1;
422 _color = COLOR_TEAM1 - 1;
424 SetPlayerColors(pl,_color);
427 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
430 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
435 // set c1...c4 to show what teams are allowed
436 void CheckAllowedTeams (entity for_whom)
442 c1 = c2 = c3 = c4 = -1;
443 cb1 = cb2 = cb3 = cb4 = 0;
445 teament_name = string_null;
448 // onslaught is special
449 head = findchain(classname, "onslaught_generator");
452 if (head.team == COLOR_TEAM1) c1 = 0;
453 if (head.team == COLOR_TEAM2) c2 = 0;
454 if (head.team == COLOR_TEAM3) c3 = 0;
455 if (head.team == COLOR_TEAM4) c4 = 0;
459 else if(g_domination)
460 teament_name = "dom_team";
462 teament_name = "ctf_team";
464 teament_name = "tdm_team";
466 teament_name = "nexball_team";
468 c1 = c2 = 0; // Assault always has 2 teams
471 // cover anything else by treating it like tdm with no teams spawned
478 MUTATOR_CALLHOOK(GetTeamCount);
482 c1 = c2 = c3 = c4 = 0;
489 // find out what teams are allowed if necessary
492 head = find(world, classname, teament_name);
495 if(!(g_domination && head.netname == ""))
497 if(head.team == COLOR_TEAM1)
499 else if(head.team == COLOR_TEAM2)
501 else if(head.team == COLOR_TEAM3)
503 else if(head.team == COLOR_TEAM4)
506 head = find(head, classname, teament_name);
510 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
512 if(autocvar_bot_vs_human && for_whom)
514 if(autocvar_bot_vs_human > 0)
517 if(clienttype(for_whom) == CLIENTTYPE_BOT)
525 if(clienttype(for_whom) == CLIENTTYPE_BOT)
532 // if player has a forced team, ONLY allow that one
533 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
535 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
537 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
539 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
543 float PlayerValue(entity p)
546 // FIXME: it always returns 1...
549 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
550 // teams that are allowed will now have their player counts stored in c1...c4
551 void GetTeamCounts(entity ignore)
555 // now count how many players are on each team already
557 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
558 // also remember the lowest-scoring player
560 FOR_EACH_CLIENT(head)
563 if(head.classname == "player")
565 else if(head.team_forced > 0)
566 t = head.team_forced; // reserve the spot
569 if(head != ignore)// && head.netname != "")
571 value = PlayerValue(head);
572 if(clienttype(head) == CLIENTTYPE_BOT)
611 // if the player who has a forced team has not joined yet, reserve the spot
612 if(autocvar_g_campaign)
614 switch(autocvar_g_campaign_forceteam)
616 case 1: if(c1 == cb1) ++c1; break;
617 case 2: if(c2 == cb2) ++c2; break;
618 case 3: if(c3 == cb3) ++c3; break;
619 case 4: if(c4 == cb4) ++c4; break;
624 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
626 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
628 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
632 case 1: ca = c1; cba = cb1; sa = team1_score; break;
633 case 2: ca = c2; cba = cb2; sa = team2_score; break;
634 case 3: ca = c3; cba = cb3; sa = team3_score; break;
635 case 4: ca = c4; cba = cb4; sa = team4_score; break;
639 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
640 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
641 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
642 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
653 if(clienttype(e) == CLIENTTYPE_REAL)
662 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
678 // the more we're at the end of the match, the more take scores into account
679 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
686 // returns # of smallest team (1, 2, 3, 4)
687 // NOTE: Assumes CheckAllowedTeams has already been called!
688 float FindSmallestTeam(entity pl, float ignore_pl)
693 // find out what teams are available
694 //CheckAllowedTeams();
696 // make sure there are at least 2 teams to join
698 totalteams = totalteams + 1;
700 totalteams = totalteams + 1;
702 totalteams = totalteams + 1;
704 totalteams = totalteams + 1;
706 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
711 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
712 return 1; // special case for campaign and player joining
713 else if(g_domination)
714 error("Too few teams available for domination\n");
716 error("Too few teams available for ctf\n");
718 error("Too few teams available for key hunt\n");
720 error("Too few teams available for freeze tag\n");
722 error("Too few teams available for team deathmatch\n");
725 // count how many players are in each team
729 GetTeamCounts(world);
731 RandomSelection_Init();
734 if(TeamSmallerEqThanTeam(2, t, pl))
736 if(TeamSmallerEqThanTeam(3, t, pl))
738 if(TeamSmallerEqThanTeam(4, t, pl))
741 // now t is the minimum, or A minimum!
742 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
743 RandomSelection_Add(world, 1, string_null, 1, 1);
744 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
745 RandomSelection_Add(world, 2, string_null, 1, 1);
746 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
747 RandomSelection_Add(world, 3, string_null, 1, 1);
748 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
749 RandomSelection_Add(world, 4, string_null, 1, 1);
751 return RandomSelection_chosen_float;
754 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
756 float smallest, selectedteam;
758 // don't join a team if we're not playing a team game
762 // find out what teams are available
763 CheckAllowedTeams(pl);
765 // if we don't care what team he ends up on, put him on whatever team he entered as.
766 // if he's not on a valid team, then let other code put him on the smallest team
769 if( c1 >= 0 && pl.team == COLOR_TEAM1)
770 selectedteam = pl.team;
771 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
772 selectedteam = pl.team;
773 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
774 selectedteam = pl.team;
775 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
776 selectedteam = pl.team;
782 if(!only_return_best)
784 SetPlayerColors(pl, selectedteam - 1);
786 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
787 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
788 LogTeamchange(pl.playerid, pl.team, 99);
792 // otherwise end up on the smallest team (handled below)
795 smallest = FindSmallestTeam(pl, TRUE);
797 if(!only_return_best && !pl.bot_forced_team)
799 TeamchangeFrags(self);
802 SetPlayerColors(pl, COLOR_TEAM1 - 1);
804 else if(smallest == 2)
806 SetPlayerColors(pl, COLOR_TEAM2 - 1);
808 else if(smallest == 3)
810 SetPlayerColors(pl, COLOR_TEAM3 - 1);
812 else if(smallest == 4)
814 SetPlayerColors(pl, COLOR_TEAM4 - 1);
818 error("smallest team: invalid team\n");
821 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
823 if(pl.deadflag == DEAD_NO)
824 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
830 //void() ctf_playerchanged;
831 void SV_ChangeTeam(float _color)
833 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
835 // in normal deathmatch we can just apply the color and we're done
837 SetPlayerColors(self, _color);
841 scolor = self.clientcolors & 0x0F;
842 dcolor = _color & 0x0F;
844 if(scolor == COLOR_TEAM1 - 1)
846 else if(scolor == COLOR_TEAM2 - 1)
848 else if(scolor == COLOR_TEAM3 - 1)
850 else // if(scolor == COLOR_TEAM4 - 1)
852 if(dcolor == COLOR_TEAM1 - 1)
854 else if(dcolor == COLOR_TEAM2 - 1)
856 else if(dcolor == COLOR_TEAM3 - 1)
858 else // if(dcolor == COLOR_TEAM4 - 1)
861 CheckAllowedTeams(self);
863 if(dteam == 1 && c1 < 0) dteam = 4;
864 if(dteam == 4 && c4 < 0) dteam = 3;
865 if(dteam == 3 && c3 < 0) dteam = 2;
866 if(dteam == 2 && c2 < 0) dteam = 1;
868 // not changing teams
871 //bprint("same team change\n");
872 SetPlayerTeam(self, dteam, steam, TRUE);
876 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
877 sprint(self, "Team changes not allowed\n");
878 return; // changing teams is not allowed
881 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
882 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
885 if(!TeamSmallerEqThanTeam(dteam, steam, self))
887 sprint(self, "Cannot change to a larger/better/shinier team\n");
892 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
894 if(self.classname == "player" && steam != dteam)
896 // reduce frags during a team change
897 TeamchangeFrags(self);
900 SetPlayerTeam(self, dteam, steam, FALSE);
902 if(self.classname == "player" && steam != dteam)
904 // kill player when changing teams
905 if(self.deadflag == DEAD_NO)
906 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
910 void ShufflePlayerOutOfTeam (float source_team)
912 float smallestteam, smallestteam_count, steam;
913 float lowest_bot_score, lowest_player_score;
914 entity head, lowest_bot, lowest_player, selected;
917 smallestteam_count = 999999999;
919 if(c1 >= 0 && c1 < smallestteam_count)
922 smallestteam_count = c1;
924 if(c2 >= 0 && c2 < smallestteam_count)
927 smallestteam_count = c2;
929 if(c3 >= 0 && c3 < smallestteam_count)
932 smallestteam_count = c3;
934 if(c4 >= 0 && c4 < smallestteam_count)
937 smallestteam_count = c4;
942 bprint("warning: no smallest team\n");
948 else if(source_team == 2)
950 else if(source_team == 3)
952 else // if(source_team == 4)
956 lowest_bot_score = 999999999;
957 lowest_player = world;
958 lowest_player_score = 999999999;
960 // find the lowest-scoring player & bot of that team
961 FOR_EACH_PLAYER(head)
963 if(head.team == steam)
967 if(head.totalfrags < lowest_bot_score)
970 lowest_bot_score = head.totalfrags;
975 if(head.totalfrags < lowest_player_score)
977 lowest_player = head;
978 lowest_player_score = head.totalfrags;
984 // prefers to move a bot...
985 if(lowest_bot != world)
986 selected = lowest_bot;
987 // but it will move a player if it has to
989 selected = lowest_player;
990 // don't do anything if it couldn't find anyone
993 bprint("warning: couldn't find a player to move from team\n");
997 // smallest team gains a member
998 if(smallestteam == 1)
1002 else if(smallestteam == 2)
1006 else if(smallestteam == 3)
1010 else if(smallestteam == 4)
1016 bprint("warning: destination team invalid\n");
1019 // source team loses a member
1020 if(source_team == 1)
1024 else if(source_team == 2)
1028 else if(source_team == 3)
1032 else if(source_team == 4)
1038 bprint("warning: source team invalid\n");
1042 // move the player to the new team
1043 TeamchangeFrags(selected);
1044 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1046 if(selected.deadflag == DEAD_NO)
1047 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1048 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1051 // code from here on is just to support maps that don't have team entities
1052 void tdm_spawnteam (string teamname, float teamcolor)
1056 e.classname = "tdm_team";
1057 e.netname = teamname;
1062 // spawn some default teams if the map is not set up for tdm
1063 void tdm_spawnteams()
1067 numteams = autocvar_g_tdm_teams_override;
1069 numteams = autocvar_g_tdm_teams;
1070 numteams = bound(2, numteams, 4);
1072 tdm_spawnteam("Red", COLOR_TEAM1-1);
1073 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1075 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1077 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1080 void tdm_delayedinit()
1082 // if no teams are found, spawn defaults
1083 if (find(world, classname, "tdm_team") == world)
1089 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);