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";
56 void runematch_init();
60 void LogTeamchange(float player_id, float team_number, float type)
62 if(!autocvar_sv_eventlog)
68 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
71 void default_delayedinit()
73 if(!scores_initialized)
77 void ActivateTeamplay()
79 serverflags |= SERVERFLAG_TEAMPLAY;
83 void InitGameplayMode()
85 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
87 qualifying_override = -1;
91 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
96 MapInfo_LoadMapSettings(mapname);
98 serverflags &~= SERVERFLAG_TEAMPLAY;
100 if not(cvar_value_issafe(world.fog))
102 print("The current map contains a potentially harmful fog setting, ignored\n");
103 world.fog = string_null;
105 if(MapInfo_Map_fog != "")
106 if(MapInfo_Map_fog == "none")
107 world.fog = string_null;
109 world.fog = strzone(MapInfo_Map_fog);
110 clientstuff = strzone(MapInfo_Map_clientstuff);
112 MapInfo_ClearTemps();
114 // set both here, gamemode can override it later
115 timelimit_override = autocvar_timelimit_override;
116 fraglimit_override = autocvar_fraglimit_override;
117 leadlimit_override = autocvar_leadlimit_override;
118 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
128 if(autocvar_g_tdm_team_spawns)
129 have_team_spawns = -1; // request team spawns
135 fraglimit_override = autocvar_g_domination_point_limit;
136 leadlimit_override = autocvar_g_domination_point_leadlimit;
138 have_team_spawns = -1; // request team spawns
144 fraglimit_override = autocvar_capturelimit_override;
145 leadlimit_override = autocvar_captureleadlimit_override;
146 MUTATOR_ADD(gamemode_ctf);
147 have_team_spawns = -1; // request team spawns
152 fraglimit_override = 0; // no limits in TD - it's a survival mode
153 leadlimit_override = 0;
154 MUTATOR_ADD(gamemode_td);
159 // ActivateTeamplay();
160 fraglimit_override = autocvar_g_runematch_point_limit;
161 leadlimit_override = autocvar_g_runematch_point_leadlimit;
167 fraglimit_override = autocvar_g_lms_lives_override;
168 leadlimit_override = 0; // not supported by LMS
169 if(fraglimit_override == 0)
170 fraglimit_override = -1;
171 lms_lowest_lives = 9999;
178 fraglimit_override = autocvar_g_arena_point_limit;
179 leadlimit_override = autocvar_g_arena_point_leadlimit;
180 maxspawned = autocvar_g_arena_maxspawned;
183 arena_roundbased = autocvar_g_arena_roundbased;
189 fraglimit_override = autocvar_g_ca_point_limit;
190 leadlimit_override = autocvar_g_ca_point_leadlimit;
191 precache_sound("ctf/red_capture.wav");
192 precache_sound("ctf/blue_capture.wav");
197 fraglimit_override = autocvar_g_keyhunt_point_limit;
198 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
199 MUTATOR_ADD(gamemode_keyhunt);
205 fraglimit_override = autocvar_g_freezetag_point_limit;
206 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
207 MUTATOR_ADD(gamemode_freezetag);
213 ScoreRules_assault();
214 have_team_spawns = -1; // request team spawns
220 have_team_spawns = -1; // request team spawns
221 MUTATOR_ADD(gamemode_onslaught);
227 if(autocvar_g_race_teams)
230 race_teams = bound(2, autocvar_g_race_teams, 4);
231 have_team_spawns = -1; // request team spawns
236 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
237 fraglimit_override = autocvar_g_race_laps_limit;
238 leadlimit_override = 0; // currently not supported by race
243 g_race_qualifying = 1;
244 fraglimit_override = 0;
245 leadlimit_override = 0;
250 fraglimit_override = autocvar_g_nexball_goallimit;
251 leadlimit_override = autocvar_g_nexball_goalleadlimit;
253 have_team_spawns = -1; // request team spawns
254 MUTATOR_ADD(gamemode_nexball);
259 MUTATOR_ADD(gamemode_keepaway);
265 cache_mutatormsg = strzone("");
266 cache_lastmutatormsg = strzone("");
268 // enforce the server's universal frag/time limits
269 if(!autocvar_g_campaign)
271 if(fraglimit_override >= 0)
272 cvar_set("fraglimit", ftos(fraglimit_override));
273 if(timelimit_override >= 0)
274 cvar_set("timelimit", ftos(timelimit_override));
275 if(leadlimit_override >= 0)
276 cvar_set("leadlimit", ftos(leadlimit_override));
277 if(qualifying_override >= 0)
278 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
283 // we need to find out the correct value for g_race_qualifying
284 if(autocvar_g_campaign)
286 g_race_qualifying = 1;
288 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
290 g_race_qualifying = 2;
291 race_fraglimit = autocvar_fraglimit;
292 race_leadlimit = autocvar_leadlimit;
293 race_timelimit = autocvar_timelimit;
294 cvar_set("fraglimit", "0");
295 cvar_set("leadlimit", "0");
296 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
299 g_race_qualifying = 0;
304 if(g_race_qualifying)
305 independent_players = 1;
310 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
313 string GetClientVersionMessage() {
315 if (self.version_mismatch) {
316 if(self.version < autocvar_gameversion) {
317 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
319 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
322 versionmsg = "^2client version and server version are compatible.^8";
327 string getwelcomemessage(void)
329 string s, modifications, motd;
332 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
333 modifications = ret_string;
336 modifications = strcat(modifications, ", MinstaGib");
339 if(g_weaponarena_random)
340 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
342 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
344 if(autocvar_g_start_weapon_laser == 0)
345 modifications = strcat(modifications, ", No start weapons");
346 if(autocvar_sv_gravity < 800)
347 modifications = strcat(modifications, ", Low gravity");
348 if(g_cloaked && !g_cts)
349 modifications = strcat(modifications, ", Cloaked");
351 modifications = strcat(modifications, ", Hook");
353 modifications = strcat(modifications, ", Midair");
355 modifications = strcat(modifications, ", Piñata");
356 if(g_weapon_stay && !g_cts)
357 modifications = strcat(modifications, ", Weapons stay");
359 modifications = strcat(modifications, ", Blood loss");
361 modifications = strcat(modifications, ", Jet pack");
362 if(autocvar_g_powerups == 0)
363 modifications = strcat(modifications, ", No powerups");
364 if(autocvar_g_powerups > 0)
365 modifications = strcat(modifications, ", Powerups");
366 modifications = substring(modifications, 2, strlen(modifications) - 2);
368 string versionmessage;
369 versionmessage = GetClientVersionMessage();
371 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
372 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
374 if(modifications != "")
375 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
377 if (g_grappling_hook)
378 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
380 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
382 if(cache_lastmutatormsg)
383 strunzone(cache_lastmutatormsg);
385 strunzone(cache_mutatormsg);
386 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
387 cache_mutatormsg = strzone(cache_lastmutatormsg);
390 if (cache_mutatormsg != "") {
391 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
394 motd = autocvar_sv_motd;
396 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
401 void SetPlayerColors(entity pl, float _color)
405 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
407 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
408 pl.clientcolors = 16*cl + cl;*/
411 pants = _color & 0x0F;
412 shirt = _color & 0xF0;
416 setcolor(pl, 16*pants + pants);
418 setcolor(pl, shirt + pants);
422 void SetPlayerTeam(entity pl, float t, float s, float noprint)
427 _color = COLOR_TEAM4 - 1;
429 _color = COLOR_TEAM3 - 1;
431 _color = COLOR_TEAM2 - 1;
433 _color = COLOR_TEAM1 - 1;
435 SetPlayerColors(pl,_color);
438 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
441 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
446 // set c1...c4 to show what teams are allowed
447 void CheckAllowedTeams (entity for_whom)
453 c1 = c2 = c3 = c4 = -1;
454 cb1 = cb2 = cb3 = cb4 = 0;
456 teament_name = string_null;
459 // onslaught is special
460 head = findchain(classname, "onslaught_generator");
463 if (head.team == COLOR_TEAM1) c1 = 0;
464 if (head.team == COLOR_TEAM2) c2 = 0;
465 if (head.team == COLOR_TEAM3) c3 = 0;
466 if (head.team == COLOR_TEAM4) c4 = 0;
470 else if(g_domination)
471 teament_name = "dom_team";
473 teament_name = "ctf_team";
475 teament_name = "tdm_team";
477 teament_name = "nexball_team";
479 c1 = c2 = 0; // Assault always has 2 teams
482 // cover anything else by treating it like tdm with no teams spawned
489 MUTATOR_CALLHOOK(GetTeamCount);
493 c1 = c2 = c3 = c4 = 0;
500 // find out what teams are allowed if necessary
503 head = find(world, classname, teament_name);
506 if(!(g_domination && head.netname == ""))
508 if(head.team == COLOR_TEAM1)
510 else if(head.team == COLOR_TEAM2)
512 else if(head.team == COLOR_TEAM3)
514 else if(head.team == COLOR_TEAM4)
517 head = find(head, classname, teament_name);
521 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
523 if(autocvar_bot_vs_human && for_whom)
525 if(autocvar_bot_vs_human > 0)
528 if(clienttype(for_whom) == CLIENTTYPE_BOT)
536 if(clienttype(for_whom) == CLIENTTYPE_BOT)
543 // if player has a forced team, ONLY allow that one
544 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
546 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
548 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
550 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
554 float PlayerValue(entity p)
557 // FIXME: it always returns 1...
560 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
561 // teams that are allowed will now have their player counts stored in c1...c4
562 void GetTeamCounts(entity ignore)
566 // now count how many players are on each team already
568 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
569 // also remember the lowest-scoring player
571 FOR_EACH_CLIENT(head)
574 if(head.classname == "player")
576 else if(head.team_forced > 0)
577 t = head.team_forced; // reserve the spot
580 if(head != ignore)// && head.netname != "")
582 value = PlayerValue(head);
583 if(clienttype(head) == CLIENTTYPE_BOT)
622 // if the player who has a forced team has not joined yet, reserve the spot
623 if(autocvar_g_campaign)
625 switch(autocvar_g_campaign_forceteam)
627 case 1: if(c1 == cb1) ++c1; break;
628 case 2: if(c2 == cb2) ++c2; break;
629 case 3: if(c3 == cb3) ++c3; break;
630 case 4: if(c4 == cb4) ++c4; break;
635 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
637 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
639 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
643 case 1: ca = c1; cba = cb1; sa = team1_score; break;
644 case 2: ca = c2; cba = cb2; sa = team2_score; break;
645 case 3: ca = c3; cba = cb3; sa = team3_score; break;
646 case 4: ca = c4; cba = cb4; sa = team4_score; break;
650 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
651 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
652 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
653 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
664 if(clienttype(e) == CLIENTTYPE_REAL)
673 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
689 // the more we're at the end of the match, the more take scores into account
690 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
697 // returns # of smallest team (1, 2, 3, 4)
698 // NOTE: Assumes CheckAllowedTeams has already been called!
699 float FindSmallestTeam(entity pl, float ignore_pl)
704 // find out what teams are available
705 //CheckAllowedTeams();
707 // make sure there are at least 2 teams to join
709 totalteams = totalteams + 1;
711 totalteams = totalteams + 1;
713 totalteams = totalteams + 1;
715 totalteams = totalteams + 1;
717 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
722 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
723 return 1; // special case for campaign and player joining
724 else if(g_domination)
725 error("Too few teams available for domination\n");
727 error("Too few teams available for ctf\n");
729 error("Too few teams available for key hunt\n");
731 error("Too few teams available for freeze tag\n");
733 error("Too few teams available for team deathmatch\n");
736 // count how many players are in each team
740 GetTeamCounts(world);
742 RandomSelection_Init();
745 if(TeamSmallerEqThanTeam(2, t, pl))
747 if(TeamSmallerEqThanTeam(3, t, pl))
749 if(TeamSmallerEqThanTeam(4, t, pl))
752 // now t is the minimum, or A minimum!
753 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
754 RandomSelection_Add(world, 1, string_null, 1, 1);
755 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
756 RandomSelection_Add(world, 2, string_null, 1, 1);
757 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
758 RandomSelection_Add(world, 3, string_null, 1, 1);
759 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
760 RandomSelection_Add(world, 4, string_null, 1, 1);
762 return RandomSelection_chosen_float;
765 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
767 float smallest, selectedteam;
769 // don't join a team if we're not playing a team game
773 // find out what teams are available
774 CheckAllowedTeams(pl);
776 // if we don't care what team he ends up on, put him on whatever team he entered as.
777 // if he's not on a valid team, then let other code put him on the smallest team
780 if( c1 >= 0 && pl.team == COLOR_TEAM1)
781 selectedteam = pl.team;
782 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
783 selectedteam = pl.team;
784 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
785 selectedteam = pl.team;
786 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
787 selectedteam = pl.team;
793 if(!only_return_best)
795 SetPlayerColors(pl, selectedteam - 1);
797 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
798 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
799 LogTeamchange(pl.playerid, pl.team, 99);
803 // otherwise end up on the smallest team (handled below)
806 smallest = FindSmallestTeam(pl, TRUE);
808 if(!only_return_best && !pl.bot_forced_team)
810 TeamchangeFrags(self);
813 SetPlayerColors(pl, COLOR_TEAM1 - 1);
815 else if(smallest == 2)
817 SetPlayerColors(pl, COLOR_TEAM2 - 1);
819 else if(smallest == 3)
821 SetPlayerColors(pl, COLOR_TEAM3 - 1);
823 else if(smallest == 4)
825 SetPlayerColors(pl, COLOR_TEAM4 - 1);
829 error("smallest team: invalid team\n");
832 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
834 if(pl.deadflag == DEAD_NO)
835 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
841 //void() ctf_playerchanged;
842 void SV_ChangeTeam(float _color)
844 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
846 // in normal deathmatch we can just apply the color and we're done
848 SetPlayerColors(self, _color);
852 scolor = self.clientcolors & 0x0F;
853 dcolor = _color & 0x0F;
855 if(scolor == COLOR_TEAM1 - 1)
857 else if(scolor == COLOR_TEAM2 - 1)
859 else if(scolor == COLOR_TEAM3 - 1)
861 else // if(scolor == COLOR_TEAM4 - 1)
863 if(dcolor == COLOR_TEAM1 - 1)
865 else if(dcolor == COLOR_TEAM2 - 1)
867 else if(dcolor == COLOR_TEAM3 - 1)
869 else // if(dcolor == COLOR_TEAM4 - 1)
872 CheckAllowedTeams(self);
874 if(dteam == 1 && c1 < 0) dteam = 4;
875 if(dteam == 4 && c4 < 0) dteam = 3;
876 if(dteam == 3 && c3 < 0) dteam = 2;
877 if(dteam == 2 && c2 < 0) dteam = 1;
879 // not changing teams
882 //bprint("same team change\n");
883 SetPlayerTeam(self, dteam, steam, TRUE);
887 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
888 sprint(self, "Team changes not allowed\n");
889 return; // changing teams is not allowed
892 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
893 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
896 if(!TeamSmallerEqThanTeam(dteam, steam, self))
898 sprint(self, "Cannot change to a larger/better/shinier team\n");
903 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
905 if(self.classname == "player" && steam != dteam)
907 // reduce frags during a team change
908 TeamchangeFrags(self);
911 SetPlayerTeam(self, dteam, steam, FALSE);
913 if(self.classname == "player" && steam != dteam)
915 // kill player when changing teams
916 if(self.deadflag == DEAD_NO)
917 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
921 void ShufflePlayerOutOfTeam (float source_team)
923 float smallestteam, smallestteam_count, steam;
924 float lowest_bot_score, lowest_player_score;
925 entity head, lowest_bot, lowest_player, selected;
928 smallestteam_count = 999999999;
930 if(c1 >= 0 && c1 < smallestteam_count)
933 smallestteam_count = c1;
935 if(c2 >= 0 && c2 < smallestteam_count)
938 smallestteam_count = c2;
940 if(c3 >= 0 && c3 < smallestteam_count)
943 smallestteam_count = c3;
945 if(c4 >= 0 && c4 < smallestteam_count)
948 smallestteam_count = c4;
953 bprint("warning: no smallest team\n");
959 else if(source_team == 2)
961 else if(source_team == 3)
963 else // if(source_team == 4)
967 lowest_bot_score = 999999999;
968 lowest_player = world;
969 lowest_player_score = 999999999;
971 // find the lowest-scoring player & bot of that team
972 FOR_EACH_PLAYER(head)
974 if(head.team == steam)
978 if(head.totalfrags < lowest_bot_score)
981 lowest_bot_score = head.totalfrags;
986 if(head.totalfrags < lowest_player_score)
988 lowest_player = head;
989 lowest_player_score = head.totalfrags;
995 // prefers to move a bot...
996 if(lowest_bot != world)
997 selected = lowest_bot;
998 // but it will move a player if it has to
1000 selected = lowest_player;
1001 // don't do anything if it couldn't find anyone
1004 bprint("warning: couldn't find a player to move from team\n");
1008 // smallest team gains a member
1009 if(smallestteam == 1)
1013 else if(smallestteam == 2)
1017 else if(smallestteam == 3)
1021 else if(smallestteam == 4)
1027 bprint("warning: destination team invalid\n");
1030 // source team loses a member
1031 if(source_team == 1)
1035 else if(source_team == 2)
1039 else if(source_team == 3)
1043 else if(source_team == 4)
1049 bprint("warning: source team invalid\n");
1053 // move the player to the new team
1054 TeamchangeFrags(selected);
1055 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1057 if(selected.deadflag == DEAD_NO)
1058 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1059 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1062 // code from here on is just to support maps that don't have team entities
1063 void tdm_spawnteam (string teamname, float teamcolor)
1067 e.classname = "tdm_team";
1068 e.netname = teamname;
1073 // spawn some default teams if the map is not set up for tdm
1074 void tdm_spawnteams()
1078 numteams = autocvar_g_tdm_teams_override;
1080 numteams = autocvar_g_tdm_teams;
1081 numteams = bound(2, numteams, 4);
1083 tdm_spawnteam("Red", COLOR_TEAM1-1);
1084 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1086 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1088 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1091 void tdm_delayedinit()
1093 // if no teams are found, spawn defaults
1094 if (find(world, classname, "tdm_team") == world)
1100 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);