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)
17 void runematch_init();
21 void LogTeamchange(float player_id, float team_number, float type)
23 if(!autocvar_sv_eventlog)
29 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
32 void default_delayedinit()
34 if(!scores_initialized)
38 void ActivateTeamplay()
40 serverflags |= SERVERFLAG_TEAMPLAY;
44 void InitGameplayMode()
46 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
48 qualifying_override = -1;
52 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
57 MapInfo_LoadMapSettings(mapname);
59 serverflags &~= SERVERFLAG_TEAMPLAY;
61 if not(cvar_value_issafe(world.fog))
63 print("The current map contains a potentially harmful fog setting, ignored\n");
64 world.fog = string_null;
66 if(MapInfo_Map_fog != "")
67 if(MapInfo_Map_fog == "none")
68 world.fog = string_null;
70 world.fog = strzone(MapInfo_Map_fog);
71 clientstuff = strzone(MapInfo_Map_clientstuff);
75 // set both here, gamemode can override it later
76 timelimit_override = autocvar_timelimit_override;
77 fraglimit_override = autocvar_fraglimit_override;
78 leadlimit_override = autocvar_leadlimit_override;
79 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
89 if(autocvar_g_tdm_team_spawns)
90 have_team_spawns = -1; // request team spawns
96 fraglimit_override = autocvar_g_domination_point_limit;
97 leadlimit_override = autocvar_g_domination_point_leadlimit;
99 have_team_spawns = -1; // request team spawns
105 fraglimit_override = autocvar_capturelimit_override;
106 leadlimit_override = autocvar_captureleadlimit_override;
107 MUTATOR_ADD(gamemode_ctf);
108 have_team_spawns = -1; // request team spawns
113 // ActivateTeamplay();
114 fraglimit_override = autocvar_g_runematch_point_limit;
115 leadlimit_override = autocvar_g_runematch_point_leadlimit;
121 fraglimit_override = autocvar_g_lms_lives_override;
122 leadlimit_override = 0; // not supported by LMS
123 if(fraglimit_override == 0)
124 fraglimit_override = -1;
125 lms_lowest_lives = 9999;
132 fraglimit_override = autocvar_g_arena_point_limit;
133 leadlimit_override = autocvar_g_arena_point_leadlimit;
134 maxspawned = autocvar_g_arena_maxspawned;
137 arena_roundbased = autocvar_g_arena_roundbased;
143 fraglimit_override = autocvar_g_ca_point_limit;
144 leadlimit_override = autocvar_g_ca_point_leadlimit;
145 precache_sound("ctf/red_capture.wav");
146 precache_sound("ctf/blue_capture.wav");
151 fraglimit_override = autocvar_g_keyhunt_point_limit;
152 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
153 MUTATOR_ADD(gamemode_keyhunt);
159 fraglimit_override = autocvar_g_freezetag_point_limit;
160 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
161 MUTATOR_ADD(gamemode_freezetag);
167 ScoreRules_assault();
168 have_team_spawns = -1; // request team spawns
174 have_team_spawns = -1; // request team spawns
175 MUTATOR_ADD(gamemode_onslaught);
181 if(autocvar_g_race_teams)
184 race_teams = bound(2, autocvar_g_race_teams, 4);
185 have_team_spawns = -1; // request team spawns
190 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
191 fraglimit_override = autocvar_g_race_laps_limit;
192 leadlimit_override = 0; // currently not supported by race
197 g_race_qualifying = 1;
198 fraglimit_override = 0;
199 leadlimit_override = 0;
204 fraglimit_override = autocvar_g_nexball_goallimit;
205 leadlimit_override = autocvar_g_nexball_goalleadlimit;
207 have_team_spawns = -1; // request team spawns
208 MUTATOR_ADD(gamemode_nexball);
213 MUTATOR_ADD(gamemode_keepaway);
219 cache_mutatormsg = strzone("");
220 cache_lastmutatormsg = strzone("");
222 // enforce the server's universal frag/time limits
223 if(!autocvar_g_campaign)
225 if(fraglimit_override >= 0)
226 cvar_set("fraglimit", ftos(fraglimit_override));
227 if(timelimit_override >= 0)
228 cvar_set("timelimit", ftos(timelimit_override));
229 if(leadlimit_override >= 0)
230 cvar_set("leadlimit", ftos(leadlimit_override));
231 if(qualifying_override >= 0)
232 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
237 // we need to find out the correct value for g_race_qualifying
238 if(autocvar_g_campaign)
240 g_race_qualifying = 1;
242 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
244 g_race_qualifying = 2;
245 race_fraglimit = autocvar_fraglimit;
246 race_leadlimit = autocvar_leadlimit;
247 race_timelimit = autocvar_timelimit;
248 cvar_set("fraglimit", "0");
249 cvar_set("leadlimit", "0");
250 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
253 g_race_qualifying = 0;
258 if(g_race_qualifying)
259 independent_players = 1;
264 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
267 string GetClientVersionMessage() {
269 if (self.version_mismatch) {
270 if(self.version < autocvar_gameversion) {
271 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
273 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
276 versionmsg = "^2client version and server version are compatible.^8";
281 string getwelcomemessage(void)
283 string s, modifications, motd;
286 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
287 modifications = ret_string;
290 modifications = strcat(modifications, ", MinstaGib");
293 if(g_weaponarena_random)
294 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
296 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
298 if(autocvar_g_start_weapon_laser == 0)
299 modifications = strcat(modifications, ", No start weapons");
300 if(autocvar_sv_gravity < 800)
301 modifications = strcat(modifications, ", Low gravity");
302 if(g_cloaked && !g_cts)
303 modifications = strcat(modifications, ", Cloaked");
305 modifications = strcat(modifications, ", Hook");
307 modifications = strcat(modifications, ", Midair");
309 modifications = strcat(modifications, ", Piñata");
310 if(g_weapon_stay && !g_cts)
311 modifications = strcat(modifications, ", Weapons stay");
313 modifications = strcat(modifications, ", Blood loss");
315 modifications = strcat(modifications, ", Jet pack");
316 if(autocvar_g_powerups == 0)
317 modifications = strcat(modifications, ", No powerups");
318 if(autocvar_g_powerups > 0)
319 modifications = strcat(modifications, ", Powerups");
320 modifications = substring(modifications, 2, strlen(modifications) - 2);
322 string versionmessage;
323 versionmessage = GetClientVersionMessage();
325 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
326 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
328 if(modifications != "")
329 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
331 if (g_grappling_hook)
332 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
334 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
336 if(cache_lastmutatormsg)
337 strunzone(cache_lastmutatormsg);
339 strunzone(cache_mutatormsg);
340 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
341 cache_mutatormsg = strzone(cache_lastmutatormsg);
344 if (cache_mutatormsg != "") {
345 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
348 motd = autocvar_sv_motd;
350 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
355 void SetPlayerColors(entity pl, float _color)
359 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
361 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
362 pl.clientcolors = 16*cl + cl;*/
365 pants = _color & 0x0F;
366 shirt = _color & 0xF0;
370 setcolor(pl, 16*pants + pants);
372 setcolor(pl, shirt + pants);
376 void SetPlayerTeam(entity pl, float t, float s, float noprint)
381 _color = FL_TEAM_4 - 1;
383 _color = FL_TEAM_3 - 1;
385 _color = FL_TEAM_2 - 1;
387 _color = FL_TEAM_1 - 1;
389 SetPlayerColors(pl,_color);
392 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
395 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
400 // set c1...c4 to show what teams are allowed
401 void CheckAllowedTeams (entity for_whom)
407 c1 = c2 = c3 = c4 = -1;
408 cb1 = cb2 = cb3 = cb4 = 0;
410 teament_name = string_null;
413 // onslaught is special
414 head = findchain(classname, "onslaught_generator");
417 if (head.team == FL_TEAM_1) c1 = 0;
418 if (head.team == FL_TEAM_2) c2 = 0;
419 if (head.team == FL_TEAM_3) c3 = 0;
420 if (head.team == FL_TEAM_4) c4 = 0;
424 else if(g_domination)
425 teament_name = "dom_team";
427 teament_name = "ctf_team";
429 teament_name = "tdm_team";
431 teament_name = "nexball_team";
433 c1 = c2 = 0; // Assault always has 2 teams
436 // cover anything else by treating it like tdm with no teams spawned
443 MUTATOR_CALLHOOK(GetTeamCount);
447 c1 = c2 = c3 = c4 = 0;
454 // find out what teams are allowed if necessary
457 head = find(world, classname, teament_name);
460 if(!(g_domination && head.netname == ""))
462 if(head.team == FL_TEAM_1)
464 else if(head.team == FL_TEAM_2)
466 else if(head.team == FL_TEAM_3)
468 else if(head.team == FL_TEAM_4)
471 head = find(head, classname, teament_name);
475 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
477 if(autocvar_bot_vs_human && for_whom)
479 if(autocvar_bot_vs_human > 0)
482 if(clienttype(for_whom) == CLIENTTYPE_BOT)
490 if(clienttype(for_whom) == CLIENTTYPE_BOT)
497 // if player has a forced team, ONLY allow that one
498 if(self.team_forced == FL_TEAM_1 && c1 >= 0)
500 else if(self.team_forced == FL_TEAM_2 && c2 >= 0)
502 else if(self.team_forced == FL_TEAM_3 && c3 >= 0)
504 else if(self.team_forced == FL_TEAM_4 && c4 >= 0)
508 float PlayerValue(entity p)
511 // FIXME: it always returns 1...
514 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
515 // teams that are allowed will now have their player counts stored in c1...c4
516 void GetTeamCounts(entity ignore)
520 // now count how many players are on each team already
522 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
523 // also remember the lowest-scoring player
525 FOR_EACH_CLIENT(head)
528 if(head.classname == "player")
530 else if(head.team_forced > 0)
531 t = head.team_forced; // reserve the spot
534 if(head != ignore)// && head.netname != "")
536 value = PlayerValue(head);
537 if(clienttype(head) == CLIENTTYPE_BOT)
576 // if the player who has a forced team has not joined yet, reserve the spot
577 if(autocvar_g_campaign)
579 switch(autocvar_g_campaign_forceteam)
581 case 1: if(c1 == cb1) ++c1; break;
582 case 2: if(c2 == cb2) ++c2; break;
583 case 3: if(c3 == cb3) ++c3; break;
584 case 4: if(c4 == cb4) ++c4; break;
589 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
591 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
593 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
597 case 1: ca = c1; cba = cb1; sa = team1_score; break;
598 case 2: ca = c2; cba = cb2; sa = team2_score; break;
599 case 3: ca = c3; cba = cb3; sa = team3_score; break;
600 case 4: ca = c4; cba = cb4; sa = team4_score; break;
604 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
605 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
606 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
607 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
618 if(clienttype(e) == CLIENTTYPE_REAL)
627 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
643 // the more we're at the end of the match, the more take scores into account
644 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
651 // returns # of smallest team (1, 2, 3, 4)
652 // NOTE: Assumes CheckAllowedTeams has already been called!
653 float FindSmallestTeam(entity pl, float ignore_pl)
658 // find out what teams are available
659 //CheckAllowedTeams();
661 // make sure there are at least 2 teams to join
663 totalteams = totalteams + 1;
665 totalteams = totalteams + 1;
667 totalteams = totalteams + 1;
669 totalteams = totalteams + 1;
671 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
676 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
677 return 1; // special case for campaign and player joining
678 else if(g_domination)
679 error("Too few teams available for domination\n");
681 error("Too few teams available for ctf\n");
683 error("Too few teams available for key hunt\n");
685 error("Too few teams available for freeze tag\n");
687 error("Too few teams available for team deathmatch\n");
690 // count how many players are in each team
694 GetTeamCounts(world);
696 RandomSelection_Init();
699 if(TeamSmallerEqThanTeam(2, t, pl))
701 if(TeamSmallerEqThanTeam(3, t, pl))
703 if(TeamSmallerEqThanTeam(4, t, pl))
706 // now t is the minimum, or A minimum!
707 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
708 RandomSelection_Add(world, 1, string_null, 1, 1);
709 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
710 RandomSelection_Add(world, 2, string_null, 1, 1);
711 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
712 RandomSelection_Add(world, 3, string_null, 1, 1);
713 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
714 RandomSelection_Add(world, 4, string_null, 1, 1);
716 return RandomSelection_chosen_float;
719 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
721 float smallest, selectedteam;
723 // don't join a team if we're not playing a team game
727 // find out what teams are available
728 CheckAllowedTeams(pl);
730 // if we don't care what team he ends up on, put him on whatever team he entered as.
731 // if he's not on a valid team, then let other code put him on the smallest team
734 if( c1 >= 0 && pl.team == FL_TEAM_1)
735 selectedteam = pl.team;
736 else if(c2 >= 0 && pl.team == FL_TEAM_2)
737 selectedteam = pl.team;
738 else if(c3 >= 0 && pl.team == FL_TEAM_3)
739 selectedteam = pl.team;
740 else if(c4 >= 0 && pl.team == FL_TEAM_4)
741 selectedteam = pl.team;
747 if(!only_return_best)
749 SetPlayerColors(pl, selectedteam - 1);
751 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
752 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
753 LogTeamchange(pl.playerid, pl.team, 99);
757 // otherwise end up on the smallest team (handled below)
760 smallest = FindSmallestTeam(pl, TRUE);
762 if(!only_return_best && !pl.bot_forced_team)
764 TeamchangeFrags(self);
767 SetPlayerColors(pl, FL_TEAM_1 - 1);
769 else if(smallest == 2)
771 SetPlayerColors(pl, FL_TEAM_2 - 1);
773 else if(smallest == 3)
775 SetPlayerColors(pl, FL_TEAM_3 - 1);
777 else if(smallest == 4)
779 SetPlayerColors(pl, FL_TEAM_4 - 1);
783 error("smallest team: invalid team\n");
786 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
788 if(pl.deadflag == DEAD_NO)
789 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
795 //void() ctf_playerchanged;
796 void SV_ChangeTeam(float _color)
798 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
800 // in normal deathmatch we can just apply the color and we're done
802 SetPlayerColors(self, _color);
806 scolor = self.clientcolors & 0x0F;
807 dcolor = _color & 0x0F;
809 if(scolor == FL_TEAM_1 - 1)
811 else if(scolor == FL_TEAM_2 - 1)
813 else if(scolor == FL_TEAM_3 - 1)
815 else // if(scolor == FL_TEAM_4 - 1)
817 if(dcolor == FL_TEAM_1 - 1)
819 else if(dcolor == FL_TEAM_2 - 1)
821 else if(dcolor == FL_TEAM_3 - 1)
823 else // if(dcolor == FL_TEAM_4 - 1)
826 CheckAllowedTeams(self);
828 if(dteam == 1 && c1 < 0) dteam = 4;
829 if(dteam == 4 && c4 < 0) dteam = 3;
830 if(dteam == 3 && c3 < 0) dteam = 2;
831 if(dteam == 2 && c2 < 0) dteam = 1;
833 // not changing teams
836 //bprint("same team change\n");
837 SetPlayerTeam(self, dteam, steam, TRUE);
841 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
842 sprint(self, "Team changes not allowed\n");
843 return; // changing teams is not allowed
846 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
847 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
850 if(!TeamSmallerEqThanTeam(dteam, steam, self))
852 sprint(self, "Cannot change to a larger/better/shinier team\n");
857 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
859 if(self.classname == "player" && steam != dteam)
861 // reduce frags during a team change
862 TeamchangeFrags(self);
865 SetPlayerTeam(self, dteam, steam, FALSE);
867 if(self.classname == "player" && steam != dteam)
869 // kill player when changing teams
870 if(self.deadflag == DEAD_NO)
871 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
875 void ShufflePlayerOutOfTeam (float source_team)
877 float smallestteam, smallestteam_count, steam;
878 float lowest_bot_score, lowest_player_score;
879 entity head, lowest_bot, lowest_player, selected;
882 smallestteam_count = 999999999;
884 if(c1 >= 0 && c1 < smallestteam_count)
887 smallestteam_count = c1;
889 if(c2 >= 0 && c2 < smallestteam_count)
892 smallestteam_count = c2;
894 if(c3 >= 0 && c3 < smallestteam_count)
897 smallestteam_count = c3;
899 if(c4 >= 0 && c4 < smallestteam_count)
902 smallestteam_count = c4;
907 bprint("warning: no smallest team\n");
913 else if(source_team == 2)
915 else if(source_team == 3)
917 else // if(source_team == 4)
921 lowest_bot_score = 999999999;
922 lowest_player = world;
923 lowest_player_score = 999999999;
925 // find the lowest-scoring player & bot of that team
926 FOR_EACH_PLAYER(head)
928 if(head.team == steam)
932 if(head.totalfrags < lowest_bot_score)
935 lowest_bot_score = head.totalfrags;
940 if(head.totalfrags < lowest_player_score)
942 lowest_player = head;
943 lowest_player_score = head.totalfrags;
949 // prefers to move a bot...
950 if(lowest_bot != world)
951 selected = lowest_bot;
952 // but it will move a player if it has to
954 selected = lowest_player;
955 // don't do anything if it couldn't find anyone
958 bprint("warning: couldn't find a player to move from team\n");
962 // smallest team gains a member
963 if(smallestteam == 1)
967 else if(smallestteam == 2)
971 else if(smallestteam == 3)
975 else if(smallestteam == 4)
981 bprint("warning: destination team invalid\n");
984 // source team loses a member
989 else if(source_team == 2)
993 else if(source_team == 3)
997 else if(source_team == 4)
1003 bprint("warning: source team invalid\n");
1007 // move the player to the new team
1008 TeamchangeFrags(selected);
1009 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1011 if(selected.deadflag == DEAD_NO)
1012 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1013 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", Team_ColoredFullName(selected.team)));
1016 // code from here on is just to support maps that don't have team entities
1017 void tdm_spawnteam (string teamname, float teamcolor)
1021 e.classname = "tdm_team";
1022 e.netname = teamname;
1027 // spawn some default teams if the map is not set up for tdm
1028 void tdm_spawnteams()
1032 numteams = autocvar_g_tdm_teams_override;
1034 numteams = autocvar_g_tdm_teams;
1035 numteams = bound(2, numteams, 4);
1037 tdm_spawnteam("Red", FL_TEAM_1-1);
1038 tdm_spawnteam("Blue", FL_TEAM_2-1);
1040 tdm_spawnteam("Yellow", FL_TEAM_3-1);
1042 tdm_spawnteam("Pink", FL_TEAM_4-1);
1045 void tdm_delayedinit()
1047 // if no teams are found, spawn defaults
1048 if (find(world, classname, "tdm_team") == world)
1054 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);