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";
58 void LogTeamchange(float player_id, float team_number, float type)
60 if(!autocvar_sv_eventlog)
66 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
69 void default_delayedinit()
71 if(!scores_initialized)
75 void ActivateTeamplay()
77 serverflags |= SERVERFLAG_TEAMPLAY;
81 void InitGameplayMode()
83 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
85 qualifying_override = -1;
89 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
94 MapInfo_LoadMapSettings(mapname);
96 serverflags &~= SERVERFLAG_TEAMPLAY;
98 if not(cvar_value_issafe(world.fog))
100 print("The current map contains a potentially harmful fog setting, ignored\n");
101 world.fog = string_null;
103 if(MapInfo_Map_fog != "")
104 if(MapInfo_Map_fog == "none")
105 world.fog = string_null;
107 world.fog = strzone(MapInfo_Map_fog);
108 clientstuff = strzone(MapInfo_Map_clientstuff);
110 MapInfo_ClearTemps();
112 // set both here, gamemode can override it later
113 timelimit_override = autocvar_timelimit_override;
114 fraglimit_override = autocvar_fraglimit_override;
115 leadlimit_override = autocvar_leadlimit_override;
116 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
126 if(autocvar_g_tdm_team_spawns)
127 have_team_spawns = -1; // request team spawns
133 fraglimit_override = autocvar_g_domination_point_limit;
134 leadlimit_override = autocvar_g_domination_point_leadlimit;
135 MUTATOR_ADD(gamemode_domination);
136 have_team_spawns = -1; // request team spawns
142 fraglimit_override = autocvar_capturelimit_override;
143 leadlimit_override = autocvar_captureleadlimit_override;
144 MUTATOR_ADD(gamemode_ctf);
145 have_team_spawns = -1; // request team spawns
150 fraglimit_override = autocvar_g_lms_lives_override;
151 leadlimit_override = 0; // not supported by LMS
152 if(fraglimit_override == 0)
153 fraglimit_override = -1;
154 lms_lowest_lives = 9999;
161 fraglimit_override = autocvar_g_arena_point_limit;
162 leadlimit_override = autocvar_g_arena_point_leadlimit;
163 maxspawned = autocvar_g_arena_maxspawned;
166 arena_roundbased = autocvar_g_arena_roundbased;
172 fraglimit_override = autocvar_g_ca_point_limit;
173 leadlimit_override = autocvar_g_ca_point_leadlimit;
174 precache_sound("ctf/red_capture.wav");
175 precache_sound("ctf/blue_capture.wav");
180 fraglimit_override = autocvar_g_keyhunt_point_limit;
181 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
182 MUTATOR_ADD(gamemode_keyhunt);
188 fraglimit_override = autocvar_g_freezetag_point_limit;
189 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
190 MUTATOR_ADD(gamemode_freezetag);
196 ScoreRules_assault();
197 have_team_spawns = -1; // request team spawns
203 have_team_spawns = -1; // request team spawns
204 MUTATOR_ADD(gamemode_onslaught);
210 if(autocvar_g_race_teams)
213 race_teams = bound(2, autocvar_g_race_teams, 4);
214 have_team_spawns = -1; // request team spawns
219 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
220 fraglimit_override = autocvar_g_race_laps_limit;
221 leadlimit_override = 0; // currently not supported by race
226 g_race_qualifying = 1;
227 fraglimit_override = 0;
228 leadlimit_override = 0;
233 fraglimit_override = autocvar_g_nexball_goallimit;
234 leadlimit_override = autocvar_g_nexball_goalleadlimit;
236 have_team_spawns = -1; // request team spawns
237 MUTATOR_ADD(gamemode_nexball);
242 MUTATOR_ADD(gamemode_keepaway);
248 cache_mutatormsg = strzone("");
249 cache_lastmutatormsg = strzone("");
251 // enforce the server's universal frag/time limits
252 if(!autocvar_g_campaign)
254 if(fraglimit_override >= 0)
255 cvar_set("fraglimit", ftos(fraglimit_override));
256 if(timelimit_override >= 0)
257 cvar_set("timelimit", ftos(timelimit_override));
258 if(leadlimit_override >= 0)
259 cvar_set("leadlimit", ftos(leadlimit_override));
260 if(qualifying_override >= 0)
261 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
266 // we need to find out the correct value for g_race_qualifying
267 if(autocvar_g_campaign)
269 g_race_qualifying = 1;
271 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
273 g_race_qualifying = 2;
274 race_fraglimit = autocvar_fraglimit;
275 race_leadlimit = autocvar_leadlimit;
276 race_timelimit = autocvar_timelimit;
277 cvar_set("fraglimit", "0");
278 cvar_set("leadlimit", "0");
279 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
282 g_race_qualifying = 0;
287 if(g_race_qualifying)
288 independent_players = 1;
293 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
296 string GetClientVersionMessage() {
298 if (self.version_mismatch) {
299 if(self.version < autocvar_gameversion) {
300 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
302 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
305 versionmsg = "^2client version and server version are compatible.^8";
310 string getwelcomemessage(void)
312 string s, modifications, motd;
315 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
316 modifications = ret_string;
319 modifications = strcat(modifications, ", MinstaGib");
322 if(g_weaponarena_random)
323 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
325 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
327 if(autocvar_g_start_weapon_laser == 0)
328 modifications = strcat(modifications, ", No start weapons");
329 if(autocvar_sv_gravity < 800)
330 modifications = strcat(modifications, ", Low gravity");
331 if(g_cloaked && !g_cts)
332 modifications = strcat(modifications, ", Cloaked");
334 modifications = strcat(modifications, ", Hook");
336 modifications = strcat(modifications, ", Midair");
338 modifications = strcat(modifications, ", Piñata");
339 if(g_weapon_stay && !g_cts)
340 modifications = strcat(modifications, ", Weapons stay");
342 modifications = strcat(modifications, ", Blood loss");
344 modifications = strcat(modifications, ", Jet pack");
345 if(autocvar_g_powerups == 0)
346 modifications = strcat(modifications, ", No powerups");
347 if(autocvar_g_powerups > 0)
348 modifications = strcat(modifications, ", Powerups");
349 modifications = substring(modifications, 2, strlen(modifications) - 2);
351 string versionmessage;
352 versionmessage = GetClientVersionMessage();
354 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
355 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
357 if(modifications != "")
358 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
360 if (g_grappling_hook)
361 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
363 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
365 if(cache_lastmutatormsg)
366 strunzone(cache_lastmutatormsg);
368 strunzone(cache_mutatormsg);
369 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
370 cache_mutatormsg = strzone(cache_lastmutatormsg);
373 if (cache_mutatormsg != "") {
374 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
377 motd = autocvar_sv_motd;
379 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
384 void SetPlayerColors(entity pl, float _color)
388 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
390 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
391 pl.clientcolors = 16*cl + cl;*/
394 pants = _color & 0x0F;
395 shirt = _color & 0xF0;
399 setcolor(pl, 16*pants + pants);
401 setcolor(pl, shirt + pants);
405 void SetPlayerTeam(entity pl, float t, float s, float noprint)
410 _color = COLOR_TEAM4 - 1;
412 _color = COLOR_TEAM3 - 1;
414 _color = COLOR_TEAM2 - 1;
416 _color = COLOR_TEAM1 - 1;
418 SetPlayerColors(pl,_color);
421 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
424 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
429 // set c1...c4 to show what teams are allowed
430 void CheckAllowedTeams (entity for_whom)
436 c1 = c2 = c3 = c4 = -1;
437 cb1 = cb2 = cb3 = cb4 = 0;
439 teament_name = string_null;
442 // onslaught is special
443 head = findchain(classname, "onslaught_generator");
446 if (head.team == COLOR_TEAM1) c1 = 0;
447 if (head.team == COLOR_TEAM2) c2 = 0;
448 if (head.team == COLOR_TEAM3) c3 = 0;
449 if (head.team == COLOR_TEAM4) c4 = 0;
453 else if(g_domination)
454 teament_name = "dom_team";
456 teament_name = "ctf_team";
458 teament_name = "tdm_team";
460 teament_name = "nexball_team";
462 c1 = c2 = 0; // Assault always has 2 teams
465 // cover anything else by treating it like tdm with no teams spawned
472 MUTATOR_CALLHOOK(GetTeamCount);
476 c1 = c2 = c3 = c4 = 0;
483 // find out what teams are allowed if necessary
486 head = find(world, classname, teament_name);
489 if(!(g_domination && head.netname == ""))
491 if(head.team == COLOR_TEAM1)
493 else if(head.team == COLOR_TEAM2)
495 else if(head.team == COLOR_TEAM3)
497 else if(head.team == COLOR_TEAM4)
500 head = find(head, classname, teament_name);
504 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
506 if(autocvar_bot_vs_human && for_whom)
508 if(autocvar_bot_vs_human > 0)
511 if(clienttype(for_whom) == CLIENTTYPE_BOT)
519 if(clienttype(for_whom) == CLIENTTYPE_BOT)
526 // if player has a forced team, ONLY allow that one
527 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
529 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
531 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
533 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
537 float PlayerValue(entity p)
540 // FIXME: it always returns 1...
543 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
544 // teams that are allowed will now have their player counts stored in c1...c4
545 void GetTeamCounts(entity ignore)
549 // now count how many players are on each team already
551 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
552 // also remember the lowest-scoring player
554 FOR_EACH_CLIENT(head)
557 if(head.classname == "player")
559 else if(head.team_forced > 0)
560 t = head.team_forced; // reserve the spot
563 if(head != ignore)// && head.netname != "")
565 value = PlayerValue(head);
566 if(clienttype(head) == CLIENTTYPE_BOT)
605 // if the player who has a forced team has not joined yet, reserve the spot
606 if(autocvar_g_campaign)
608 switch(autocvar_g_campaign_forceteam)
610 case 1: if(c1 == cb1) ++c1; break;
611 case 2: if(c2 == cb2) ++c2; break;
612 case 3: if(c3 == cb3) ++c3; break;
613 case 4: if(c4 == cb4) ++c4; break;
618 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
620 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
622 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
626 case 1: ca = c1; cba = cb1; sa = team1_score; break;
627 case 2: ca = c2; cba = cb2; sa = team2_score; break;
628 case 3: ca = c3; cba = cb3; sa = team3_score; break;
629 case 4: ca = c4; cba = cb4; sa = team4_score; break;
633 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
634 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
635 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
636 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
647 if(clienttype(e) == CLIENTTYPE_REAL)
656 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
672 // the more we're at the end of the match, the more take scores into account
673 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
680 // returns # of smallest team (1, 2, 3, 4)
681 // NOTE: Assumes CheckAllowedTeams has already been called!
682 float FindSmallestTeam(entity pl, float ignore_pl)
687 // find out what teams are available
688 //CheckAllowedTeams();
690 // make sure there are at least 2 teams to join
692 totalteams = totalteams + 1;
694 totalteams = totalteams + 1;
696 totalteams = totalteams + 1;
698 totalteams = totalteams + 1;
700 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
705 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
706 return 1; // special case for campaign and player joining
707 else if(g_domination)
708 error("Too few teams available for domination\n");
710 error("Too few teams available for ctf\n");
712 error("Too few teams available for key hunt\n");
714 error("Too few teams available for freeze tag\n");
716 error("Too few teams available for team deathmatch\n");
719 // count how many players are in each team
723 GetTeamCounts(world);
725 RandomSelection_Init();
728 if(TeamSmallerEqThanTeam(2, t, pl))
730 if(TeamSmallerEqThanTeam(3, t, pl))
732 if(TeamSmallerEqThanTeam(4, t, pl))
735 // now t is the minimum, or A minimum!
736 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
737 RandomSelection_Add(world, 1, string_null, 1, 1);
738 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
739 RandomSelection_Add(world, 2, string_null, 1, 1);
740 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
741 RandomSelection_Add(world, 3, string_null, 1, 1);
742 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
743 RandomSelection_Add(world, 4, string_null, 1, 1);
745 return RandomSelection_chosen_float;
748 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
750 float smallest, selectedteam;
752 // don't join a team if we're not playing a team game
756 // find out what teams are available
757 CheckAllowedTeams(pl);
759 // if we don't care what team he ends up on, put him on whatever team he entered as.
760 // if he's not on a valid team, then let other code put him on the smallest team
763 if( c1 >= 0 && pl.team == COLOR_TEAM1)
764 selectedteam = pl.team;
765 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
766 selectedteam = pl.team;
767 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
768 selectedteam = pl.team;
769 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
770 selectedteam = pl.team;
776 if(!only_return_best)
778 SetPlayerColors(pl, selectedteam - 1);
780 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
781 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
782 LogTeamchange(pl.playerid, pl.team, 99);
786 // otherwise end up on the smallest team (handled below)
789 smallest = FindSmallestTeam(pl, TRUE);
791 if(!only_return_best && !pl.bot_forced_team)
793 TeamchangeFrags(self);
796 SetPlayerColors(pl, COLOR_TEAM1 - 1);
798 else if(smallest == 2)
800 SetPlayerColors(pl, COLOR_TEAM2 - 1);
802 else if(smallest == 3)
804 SetPlayerColors(pl, COLOR_TEAM3 - 1);
806 else if(smallest == 4)
808 SetPlayerColors(pl, COLOR_TEAM4 - 1);
812 error("smallest team: invalid team\n");
815 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
817 if(pl.deadflag == DEAD_NO)
818 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
824 //void() ctf_playerchanged;
825 void SV_ChangeTeam(float _color)
827 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
829 // in normal deathmatch we can just apply the color and we're done
831 SetPlayerColors(self, _color);
835 scolor = self.clientcolors & 0x0F;
836 dcolor = _color & 0x0F;
838 if(scolor == COLOR_TEAM1 - 1)
840 else if(scolor == COLOR_TEAM2 - 1)
842 else if(scolor == COLOR_TEAM3 - 1)
844 else // if(scolor == COLOR_TEAM4 - 1)
846 if(dcolor == COLOR_TEAM1 - 1)
848 else if(dcolor == COLOR_TEAM2 - 1)
850 else if(dcolor == COLOR_TEAM3 - 1)
852 else // if(dcolor == COLOR_TEAM4 - 1)
855 CheckAllowedTeams(self);
857 if(dteam == 1 && c1 < 0) dteam = 4;
858 if(dteam == 4 && c4 < 0) dteam = 3;
859 if(dteam == 3 && c3 < 0) dteam = 2;
860 if(dteam == 2 && c2 < 0) dteam = 1;
862 // not changing teams
865 //bprint("same team change\n");
866 SetPlayerTeam(self, dteam, steam, TRUE);
870 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
871 sprint(self, "Team changes not allowed\n");
872 return; // changing teams is not allowed
875 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
876 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
879 if(!TeamSmallerEqThanTeam(dteam, steam, self))
881 sprint(self, "Cannot change to a larger/better/shinier team\n");
886 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
888 if(self.classname == "player" && steam != dteam)
890 // reduce frags during a team change
891 TeamchangeFrags(self);
894 SetPlayerTeam(self, dteam, steam, FALSE);
896 if(self.classname == "player" && steam != dteam)
898 // kill player when changing teams
899 if(self.deadflag == DEAD_NO)
900 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
904 void ShufflePlayerOutOfTeam (float source_team)
906 float smallestteam, smallestteam_count, steam;
907 float lowest_bot_score, lowest_player_score;
908 entity head, lowest_bot, lowest_player, selected;
911 smallestteam_count = 999999999;
913 if(c1 >= 0 && c1 < smallestteam_count)
916 smallestteam_count = c1;
918 if(c2 >= 0 && c2 < smallestteam_count)
921 smallestteam_count = c2;
923 if(c3 >= 0 && c3 < smallestteam_count)
926 smallestteam_count = c3;
928 if(c4 >= 0 && c4 < smallestteam_count)
931 smallestteam_count = c4;
936 bprint("warning: no smallest team\n");
942 else if(source_team == 2)
944 else if(source_team == 3)
946 else // if(source_team == 4)
950 lowest_bot_score = 999999999;
951 lowest_player = world;
952 lowest_player_score = 999999999;
954 // find the lowest-scoring player & bot of that team
955 FOR_EACH_PLAYER(head)
957 if(head.team == steam)
961 if(head.totalfrags < lowest_bot_score)
964 lowest_bot_score = head.totalfrags;
969 if(head.totalfrags < lowest_player_score)
971 lowest_player = head;
972 lowest_player_score = head.totalfrags;
978 // prefers to move a bot...
979 if(lowest_bot != world)
980 selected = lowest_bot;
981 // but it will move a player if it has to
983 selected = lowest_player;
984 // don't do anything if it couldn't find anyone
987 bprint("warning: couldn't find a player to move from team\n");
991 // smallest team gains a member
992 if(smallestteam == 1)
996 else if(smallestteam == 2)
1000 else if(smallestteam == 3)
1004 else if(smallestteam == 4)
1010 bprint("warning: destination team invalid\n");
1013 // source team loses a member
1014 if(source_team == 1)
1018 else if(source_team == 2)
1022 else if(source_team == 3)
1026 else if(source_team == 4)
1032 bprint("warning: source team invalid\n");
1036 // move the player to the new team
1037 TeamchangeFrags(selected);
1038 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1040 if(selected.deadflag == DEAD_NO)
1041 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1042 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1045 // code from here on is just to support maps that don't have team entities
1046 void tdm_spawnteam (string teamname, float teamcolor)
1050 e.classname = "tdm_team";
1051 e.netname = teamname;
1056 // spawn some default teams if the map is not set up for tdm
1057 void tdm_spawnteams()
1061 numteams = autocvar_g_tdm_teams_override;
1063 numteams = autocvar_g_tdm_teams;
1064 numteams = bound(2, numteams, 4);
1066 tdm_spawnteam("Red", COLOR_TEAM1-1);
1067 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1069 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1071 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1074 void tdm_delayedinit()
1076 // if no teams are found, spawn defaults
1077 if (find(world, classname, "tdm_team") == world)
1083 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);