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 // ActivateTeamplay();
153 fraglimit_override = autocvar_g_runematch_point_limit;
154 leadlimit_override = autocvar_g_runematch_point_leadlimit;
160 fraglimit_override = autocvar_g_lms_lives_override;
161 leadlimit_override = 0; // not supported by LMS
162 if(fraglimit_override == 0)
163 fraglimit_override = -1;
164 lms_lowest_lives = 9999;
171 fraglimit_override = autocvar_g_arena_point_limit;
172 leadlimit_override = autocvar_g_arena_point_leadlimit;
173 maxspawned = autocvar_g_arena_maxspawned;
176 arena_roundbased = autocvar_g_arena_roundbased;
182 fraglimit_override = autocvar_g_ca_point_limit;
183 leadlimit_override = autocvar_g_ca_point_leadlimit;
184 precache_sound("ctf/red_capture.wav");
185 precache_sound("ctf/blue_capture.wav");
190 fraglimit_override = autocvar_g_keyhunt_point_limit;
191 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
192 MUTATOR_ADD(gamemode_keyhunt);
198 fraglimit_override = autocvar_g_freezetag_point_limit;
199 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
200 MUTATOR_ADD(gamemode_freezetag);
206 ScoreRules_assault();
207 have_team_spawns = -1; // request team spawns
213 have_team_spawns = -1; // request team spawns
214 MUTATOR_ADD(gamemode_onslaught);
220 if(autocvar_g_race_teams)
223 race_teams = bound(2, autocvar_g_race_teams, 4);
224 have_team_spawns = -1; // request team spawns
229 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
230 fraglimit_override = autocvar_g_race_laps_limit;
231 leadlimit_override = 0; // currently not supported by race
236 g_race_qualifying = 1;
237 fraglimit_override = 0;
238 leadlimit_override = 0;
243 fraglimit_override = autocvar_g_nexball_goallimit;
244 leadlimit_override = autocvar_g_nexball_goalleadlimit;
246 have_team_spawns = -1; // request team spawns
247 MUTATOR_ADD(gamemode_nexball);
252 MUTATOR_ADD(gamemode_keepaway);
258 cache_mutatormsg = strzone("");
259 cache_lastmutatormsg = strzone("");
261 // enforce the server's universal frag/time limits
262 if(!autocvar_g_campaign)
264 if(fraglimit_override >= 0)
265 cvar_set("fraglimit", ftos(fraglimit_override));
266 if(timelimit_override >= 0)
267 cvar_set("timelimit", ftos(timelimit_override));
268 if(leadlimit_override >= 0)
269 cvar_set("leadlimit", ftos(leadlimit_override));
270 if(qualifying_override >= 0)
271 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
276 // we need to find out the correct value for g_race_qualifying
277 if(autocvar_g_campaign)
279 g_race_qualifying = 1;
281 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
283 g_race_qualifying = 2;
284 race_fraglimit = autocvar_fraglimit;
285 race_leadlimit = autocvar_leadlimit;
286 race_timelimit = autocvar_timelimit;
287 cvar_set("fraglimit", "0");
288 cvar_set("leadlimit", "0");
289 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
292 g_race_qualifying = 0;
297 if(g_race_qualifying)
298 independent_players = 1;
303 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
306 string GetClientVersionMessage() {
308 if (self.version_mismatch) {
309 if(self.version < autocvar_gameversion) {
310 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
312 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
315 versionmsg = "^2client version and server version are compatible.^8";
320 string getwelcomemessage(void)
322 string s, modifications, motd;
325 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
326 modifications = ret_string;
330 if(g_weaponarena_random)
331 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
333 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
335 if(autocvar_g_start_weapon_laser == 0)
336 modifications = strcat(modifications, ", No start weapons");
337 if(autocvar_sv_gravity < 800)
338 modifications = strcat(modifications, ", Low gravity");
339 if(g_cloaked && !g_cts)
340 modifications = strcat(modifications, ", Cloaked");
342 modifications = strcat(modifications, ", Hook");
344 modifications = strcat(modifications, ", Midair");
346 modifications = strcat(modifications, ", Piñata");
347 if(g_weapon_stay && !g_cts)
348 modifications = strcat(modifications, ", Weapons stay");
350 modifications = strcat(modifications, ", Blood loss");
352 modifications = strcat(modifications, ", Jet pack");
353 if(autocvar_g_powerups == 0)
354 modifications = strcat(modifications, ", No powerups");
355 if(autocvar_g_powerups > 0)
356 modifications = strcat(modifications, ", Powerups");
357 modifications = substring(modifications, 2, strlen(modifications) - 2);
359 string versionmessage;
360 versionmessage = GetClientVersionMessage();
362 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
363 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
365 if(modifications != "")
366 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
368 if (g_grappling_hook)
369 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
371 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
373 if(cache_lastmutatormsg)
374 strunzone(cache_lastmutatormsg);
376 strunzone(cache_mutatormsg);
377 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
378 cache_mutatormsg = strzone(cache_lastmutatormsg);
381 if (cache_mutatormsg != "") {
382 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
385 motd = autocvar_sv_motd;
387 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
392 void SetPlayerColors(entity pl, float _color)
396 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
398 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
399 pl.clientcolors = 16*cl + cl;*/
402 pants = _color & 0x0F;
403 shirt = _color & 0xF0;
407 setcolor(pl, 16*pants + pants);
409 setcolor(pl, shirt + pants);
413 void SetPlayerTeam(entity pl, float t, float s, float noprint)
418 _color = COLOR_TEAM4 - 1;
420 _color = COLOR_TEAM3 - 1;
422 _color = COLOR_TEAM2 - 1;
424 _color = COLOR_TEAM1 - 1;
426 SetPlayerColors(pl,_color);
429 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
432 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
437 // set c1...c4 to show what teams are allowed
438 void CheckAllowedTeams (entity for_whom)
444 c1 = c2 = c3 = c4 = -1;
445 cb1 = cb2 = cb3 = cb4 = 0;
447 teament_name = string_null;
450 // onslaught is special
451 head = findchain(classname, "onslaught_generator");
454 if (head.team == COLOR_TEAM1) c1 = 0;
455 if (head.team == COLOR_TEAM2) c2 = 0;
456 if (head.team == COLOR_TEAM3) c3 = 0;
457 if (head.team == COLOR_TEAM4) c4 = 0;
461 else if(g_domination)
462 teament_name = "dom_team";
464 teament_name = "ctf_team";
466 teament_name = "tdm_team";
468 teament_name = "nexball_team";
470 c1 = c2 = 0; // Assault always has 2 teams
473 // cover anything else by treating it like tdm with no teams spawned
480 MUTATOR_CALLHOOK(GetTeamCount);
484 c1 = c2 = c3 = c4 = 0;
491 // find out what teams are allowed if necessary
494 head = find(world, classname, teament_name);
497 if(!(g_domination && head.netname == ""))
499 if(head.team == COLOR_TEAM1)
501 else if(head.team == COLOR_TEAM2)
503 else if(head.team == COLOR_TEAM3)
505 else if(head.team == COLOR_TEAM4)
508 head = find(head, classname, teament_name);
512 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
514 if(autocvar_bot_vs_human && for_whom)
516 if(autocvar_bot_vs_human > 0)
519 if(clienttype(for_whom) == CLIENTTYPE_BOT)
527 if(clienttype(for_whom) == CLIENTTYPE_BOT)
534 // if player has a forced team, ONLY allow that one
535 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
537 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
539 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
541 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
545 float PlayerValue(entity p)
548 // FIXME: it always returns 1...
551 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
552 // teams that are allowed will now have their player counts stored in c1...c4
553 void GetTeamCounts(entity ignore)
557 // now count how many players are on each team already
559 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
560 // also remember the lowest-scoring player
562 FOR_EACH_CLIENT(head)
565 if(head.classname == "player")
567 else if(head.team_forced > 0)
568 t = head.team_forced; // reserve the spot
571 if(head != ignore)// && head.netname != "")
573 value = PlayerValue(head);
574 if(clienttype(head) == CLIENTTYPE_BOT)
613 // if the player who has a forced team has not joined yet, reserve the spot
614 if(autocvar_g_campaign)
616 switch(autocvar_g_campaign_forceteam)
618 case 1: if(c1 == cb1) ++c1; break;
619 case 2: if(c2 == cb2) ++c2; break;
620 case 3: if(c3 == cb3) ++c3; break;
621 case 4: if(c4 == cb4) ++c4; break;
626 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
628 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
630 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
634 case 1: ca = c1; cba = cb1; sa = team1_score; break;
635 case 2: ca = c2; cba = cb2; sa = team2_score; break;
636 case 3: ca = c3; cba = cb3; sa = team3_score; break;
637 case 4: ca = c4; cba = cb4; sa = team4_score; break;
641 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
642 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
643 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
644 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
655 if(clienttype(e) == CLIENTTYPE_REAL)
664 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
680 // the more we're at the end of the match, the more take scores into account
681 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
688 // returns # of smallest team (1, 2, 3, 4)
689 // NOTE: Assumes CheckAllowedTeams has already been called!
690 float FindSmallestTeam(entity pl, float ignore_pl)
695 // find out what teams are available
696 //CheckAllowedTeams();
698 // make sure there are at least 2 teams to join
700 totalteams = totalteams + 1;
702 totalteams = totalteams + 1;
704 totalteams = totalteams + 1;
706 totalteams = totalteams + 1;
708 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
713 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
714 return 1; // special case for campaign and player joining
715 else if(g_domination)
716 error("Too few teams available for domination\n");
718 error("Too few teams available for ctf\n");
720 error("Too few teams available for key hunt\n");
722 error("Too few teams available for freeze tag\n");
724 error("Too few teams available for team deathmatch\n");
727 // count how many players are in each team
731 GetTeamCounts(world);
733 RandomSelection_Init();
736 if(TeamSmallerEqThanTeam(2, t, pl))
738 if(TeamSmallerEqThanTeam(3, t, pl))
740 if(TeamSmallerEqThanTeam(4, t, pl))
743 // now t is the minimum, or A minimum!
744 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
745 RandomSelection_Add(world, 1, string_null, 1, 1);
746 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
747 RandomSelection_Add(world, 2, string_null, 1, 1);
748 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
749 RandomSelection_Add(world, 3, string_null, 1, 1);
750 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
751 RandomSelection_Add(world, 4, string_null, 1, 1);
753 return RandomSelection_chosen_float;
756 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
758 float smallest, selectedteam;
760 // don't join a team if we're not playing a team game
764 // find out what teams are available
765 CheckAllowedTeams(pl);
767 // if we don't care what team he ends up on, put him on whatever team he entered as.
768 // if he's not on a valid team, then let other code put him on the smallest team
771 if( c1 >= 0 && pl.team == COLOR_TEAM1)
772 selectedteam = pl.team;
773 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
774 selectedteam = pl.team;
775 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
776 selectedteam = pl.team;
777 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
778 selectedteam = pl.team;
784 if(!only_return_best)
786 SetPlayerColors(pl, selectedteam - 1);
788 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
789 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
790 LogTeamchange(pl.playerid, pl.team, 99);
794 // otherwise end up on the smallest team (handled below)
797 smallest = FindSmallestTeam(pl, TRUE);
799 if(!only_return_best && !pl.bot_forced_team)
801 TeamchangeFrags(self);
804 SetPlayerColors(pl, COLOR_TEAM1 - 1);
806 else if(smallest == 2)
808 SetPlayerColors(pl, COLOR_TEAM2 - 1);
810 else if(smallest == 3)
812 SetPlayerColors(pl, COLOR_TEAM3 - 1);
814 else if(smallest == 4)
816 SetPlayerColors(pl, COLOR_TEAM4 - 1);
820 error("smallest team: invalid team\n");
823 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
825 if(pl.deadflag == DEAD_NO)
826 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
832 //void() ctf_playerchanged;
833 void SV_ChangeTeam(float _color)
835 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
837 // in normal deathmatch we can just apply the color and we're done
839 SetPlayerColors(self, _color);
843 scolor = self.clientcolors & 0x0F;
844 dcolor = _color & 0x0F;
846 if(scolor == COLOR_TEAM1 - 1)
848 else if(scolor == COLOR_TEAM2 - 1)
850 else if(scolor == COLOR_TEAM3 - 1)
852 else // if(scolor == COLOR_TEAM4 - 1)
854 if(dcolor == COLOR_TEAM1 - 1)
856 else if(dcolor == COLOR_TEAM2 - 1)
858 else if(dcolor == COLOR_TEAM3 - 1)
860 else // if(dcolor == COLOR_TEAM4 - 1)
863 CheckAllowedTeams(self);
865 if(dteam == 1 && c1 < 0) dteam = 4;
866 if(dteam == 4 && c4 < 0) dteam = 3;
867 if(dteam == 3 && c3 < 0) dteam = 2;
868 if(dteam == 2 && c2 < 0) dteam = 1;
870 // not changing teams
873 //bprint("same team change\n");
874 SetPlayerTeam(self, dteam, steam, TRUE);
878 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
879 sprint(self, "Team changes not allowed\n");
880 return; // changing teams is not allowed
883 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
884 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
887 if(!TeamSmallerEqThanTeam(dteam, steam, self))
889 sprint(self, "Cannot change to a larger/better/shinier team\n");
894 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
896 if(self.classname == "player" && steam != dteam)
898 // reduce frags during a team change
899 TeamchangeFrags(self);
902 SetPlayerTeam(self, dteam, steam, FALSE);
904 if(self.classname == "player" && steam != dteam)
906 // kill player when changing teams
907 if(self.deadflag == DEAD_NO)
908 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
912 void ShufflePlayerOutOfTeam (float source_team)
914 float smallestteam, smallestteam_count, steam;
915 float lowest_bot_score, lowest_player_score;
916 entity head, lowest_bot, lowest_player, selected;
919 smallestteam_count = 999999999;
921 if(c1 >= 0 && c1 < smallestteam_count)
924 smallestteam_count = c1;
926 if(c2 >= 0 && c2 < smallestteam_count)
929 smallestteam_count = c2;
931 if(c3 >= 0 && c3 < smallestteam_count)
934 smallestteam_count = c3;
936 if(c4 >= 0 && c4 < smallestteam_count)
939 smallestteam_count = c4;
944 bprint("warning: no smallest team\n");
950 else if(source_team == 2)
952 else if(source_team == 3)
954 else // if(source_team == 4)
958 lowest_bot_score = 999999999;
959 lowest_player = world;
960 lowest_player_score = 999999999;
962 // find the lowest-scoring player & bot of that team
963 FOR_EACH_PLAYER(head)
965 if(head.team == steam)
969 if(head.totalfrags < lowest_bot_score)
972 lowest_bot_score = head.totalfrags;
977 if(head.totalfrags < lowest_player_score)
979 lowest_player = head;
980 lowest_player_score = head.totalfrags;
986 // prefers to move a bot...
987 if(lowest_bot != world)
988 selected = lowest_bot;
989 // but it will move a player if it has to
991 selected = lowest_player;
992 // don't do anything if it couldn't find anyone
995 bprint("warning: couldn't find a player to move from team\n");
999 // smallest team gains a member
1000 if(smallestteam == 1)
1004 else if(smallestteam == 2)
1008 else if(smallestteam == 3)
1012 else if(smallestteam == 4)
1018 bprint("warning: destination team invalid\n");
1021 // source team loses a member
1022 if(source_team == 1)
1026 else if(source_team == 2)
1030 else if(source_team == 3)
1034 else if(source_team == 4)
1040 bprint("warning: source team invalid\n");
1044 // move the player to the new team
1045 TeamchangeFrags(selected);
1046 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1048 if(selected.deadflag == DEAD_NO)
1049 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1050 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1053 // code from here on is just to support maps that don't have team entities
1054 void tdm_spawnteam (string teamname, float teamcolor)
1058 e.classname = "tdm_team";
1059 e.netname = teamname;
1064 // spawn some default teams if the map is not set up for tdm
1065 void tdm_spawnteams()
1069 numteams = autocvar_g_tdm_teams_override;
1071 numteams = autocvar_g_tdm_teams;
1072 numteams = bound(2, numteams, 4);
1074 tdm_spawnteam("Red", COLOR_TEAM1-1);
1075 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1077 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1079 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1082 void tdm_delayedinit()
1084 // if no teams are found, spawn defaults
1085 if (find(world, classname, "tdm_team") == world)
1091 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);