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";
57 void runematch_init();
61 void LogTeamchange(float player_id, float team_number, float type)
63 if(!autocvar_sv_eventlog)
69 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
72 void default_delayedinit()
74 if(!scores_initialized)
78 void ActivateTeamplay()
80 serverflags |= SERVERFLAG_TEAMPLAY;
84 void InitGameplayMode()
86 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
88 qualifying_override = -1;
92 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
97 MapInfo_LoadMapSettings(mapname);
99 serverflags &~= SERVERFLAG_TEAMPLAY;
101 if not(cvar_value_issafe(world.fog))
103 print("The current map contains a potentially harmful fog setting, ignored\n");
104 world.fog = string_null;
106 if(MapInfo_Map_fog != "")
107 if(MapInfo_Map_fog == "none")
108 world.fog = string_null;
110 world.fog = strzone(MapInfo_Map_fog);
111 clientstuff = strzone(MapInfo_Map_clientstuff);
113 MapInfo_ClearTemps();
115 // set both here, gamemode can override it later
116 timelimit_override = autocvar_timelimit_override;
117 fraglimit_override = autocvar_fraglimit_override;
118 leadlimit_override = autocvar_leadlimit_override;
119 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
129 if(autocvar_g_tdm_team_spawns)
130 have_team_spawns = -1; // request team spawns
136 fraglimit_override = autocvar_g_domination_point_limit;
137 leadlimit_override = autocvar_g_domination_point_leadlimit;
139 have_team_spawns = -1; // request team spawns
145 g_ctf_ignore_frags = autocvar_g_ctf_ignore_frags;
146 fraglimit_override = autocvar_capturelimit_override;
147 leadlimit_override = autocvar_captureleadlimit_override;
149 have_team_spawns = -1; // request team spawns
154 // ActivateTeamplay();
155 fraglimit_override = autocvar_g_runematch_point_limit;
156 leadlimit_override = autocvar_g_runematch_point_leadlimit;
162 fraglimit_override = autocvar_g_lms_lives_override;
163 leadlimit_override = 0; // not supported by LMS
164 if(fraglimit_override == 0)
165 fraglimit_override = -1;
166 lms_lowest_lives = 9999;
173 fraglimit_override = autocvar_g_arena_point_limit;
174 leadlimit_override = autocvar_g_arena_point_leadlimit;
175 maxspawned = autocvar_g_arena_maxspawned;
178 arena_roundbased = autocvar_g_arena_roundbased;
184 fraglimit_override = autocvar_g_ca_point_limit;
185 leadlimit_override = autocvar_g_ca_point_leadlimit;
186 precache_sound("ctf/red_capture.wav");
187 precache_sound("ctf/blue_capture.wav");
192 fraglimit_override = autocvar_g_keyhunt_point_limit;
193 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
194 MUTATOR_ADD(gamemode_keyhunt);
200 fraglimit_override = autocvar_g_freezetag_point_limit;
201 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
202 MUTATOR_ADD(gamemode_freezetag);
208 ScoreRules_assault();
209 have_team_spawns = -1; // request team spawns
215 have_team_spawns = -1; // request team spawns
216 MUTATOR_ADD(gamemode_onslaught);
222 if(autocvar_g_race_teams)
225 race_teams = bound(2, autocvar_g_race_teams, 4);
226 have_team_spawns = -1; // request team spawns
231 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
232 fraglimit_override = autocvar_g_race_laps_limit;
233 leadlimit_override = 0; // currently not supported by race
238 g_race_qualifying = 1;
239 fraglimit_override = 0;
240 leadlimit_override = 0;
245 fraglimit_override = autocvar_g_nexball_goallimit;
246 leadlimit_override = autocvar_g_nexball_goalleadlimit;
248 have_team_spawns = -1; // request team spawns
249 MUTATOR_ADD(gamemode_nexball);
254 MUTATOR_ADD(gamemode_keepaway);
260 cache_mutatormsg = strzone("");
261 cache_lastmutatormsg = strzone("");
263 // enforce the server's universal frag/time limits
264 if(!autocvar_g_campaign)
266 if(fraglimit_override >= 0)
267 cvar_set("fraglimit", ftos(fraglimit_override));
268 if(timelimit_override >= 0)
269 cvar_set("timelimit", ftos(timelimit_override));
270 if(leadlimit_override >= 0)
271 cvar_set("leadlimit", ftos(leadlimit_override));
272 if(qualifying_override >= 0)
273 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
278 // we need to find out the correct value for g_race_qualifying
279 if(autocvar_g_campaign)
281 g_race_qualifying = 1;
283 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
285 g_race_qualifying = 2;
286 race_fraglimit = autocvar_fraglimit;
287 race_leadlimit = autocvar_leadlimit;
288 race_timelimit = autocvar_timelimit;
289 cvar_set("fraglimit", "0");
290 cvar_set("leadlimit", "0");
291 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
294 g_race_qualifying = 0;
299 if(g_race_qualifying)
300 independent_players = 1;
305 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
308 string GetClientVersionMessage() {
310 if (self.version_mismatch) {
311 if(self.version < autocvar_gameversion) {
312 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
314 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
317 versionmsg = "^2client version and server version are compatible.^8";
322 string getwelcomemessage(void)
324 string s, modifications, motd;
327 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
328 modifications = ret_string;
331 modifications = strcat(modifications, ", MinstaGib");
334 if(g_weaponarena_random)
335 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
337 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
339 if(autocvar_g_start_weapon_laser == 0)
340 modifications = strcat(modifications, ", No start weapons");
341 if(autocvar_sv_gravity < 800)
342 modifications = strcat(modifications, ", Low gravity");
343 if(g_cloaked && !g_cts)
344 modifications = strcat(modifications, ", Cloaked");
346 modifications = strcat(modifications, ", Hook");
348 modifications = strcat(modifications, ", Midair");
350 modifications = strcat(modifications, ", Piñata");
351 if(g_weapon_stay && !g_cts)
352 modifications = strcat(modifications, ", Weapons stay");
354 modifications = strcat(modifications, ", Blood loss");
356 modifications = strcat(modifications, ", Jet pack");
357 if(autocvar_g_powerups == 0)
358 modifications = strcat(modifications, ", No powerups");
359 if(autocvar_g_powerups > 0)
360 modifications = strcat(modifications, ", Powerups");
361 modifications = substring(modifications, 2, strlen(modifications) - 2);
363 string versionmessage;
364 versionmessage = GetClientVersionMessage();
366 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
367 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
369 if(modifications != "")
370 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
372 if (g_grappling_hook)
373 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
375 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
377 if(cache_lastmutatormsg)
378 strunzone(cache_lastmutatormsg);
380 strunzone(cache_mutatormsg);
381 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
382 cache_mutatormsg = strzone(cache_lastmutatormsg);
385 if (cache_mutatormsg != "") {
386 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
389 motd = autocvar_sv_motd;
391 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
396 void SetPlayerColors(entity pl, float _color)
400 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
402 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
403 pl.clientcolors = 16*cl + cl;*/
406 pants = _color & 0x0F;
407 shirt = _color & 0xF0;
411 setcolor(pl, 16*pants + pants);
413 setcolor(pl, shirt + pants);
417 void SetPlayerTeam(entity pl, float t, float s, float noprint)
422 _color = COLOR_TEAM4 - 1;
424 _color = COLOR_TEAM3 - 1;
426 _color = COLOR_TEAM2 - 1;
428 _color = COLOR_TEAM1 - 1;
430 SetPlayerColors(pl,_color);
433 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
436 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
441 // set c1...c4 to show what teams are allowed
442 void CheckAllowedTeams (entity for_whom)
448 c1 = c2 = c3 = c4 = -1;
449 cb1 = cb2 = cb3 = cb4 = 0;
451 teament_name = string_null;
454 // onslaught is special
455 head = findchain(classname, "onslaught_generator");
458 if (head.team == COLOR_TEAM1) c1 = 0;
459 if (head.team == COLOR_TEAM2) c2 = 0;
460 if (head.team == COLOR_TEAM3) c3 = 0;
461 if (head.team == COLOR_TEAM4) c4 = 0;
465 else if(g_domination)
466 teament_name = "dom_team";
468 teament_name = "ctf_team";
470 teament_name = "tdm_team";
472 teament_name = "nexball_team";
474 c1 = c2 = 0; // Assault always has 2 teams
477 // cover anything else by treating it like tdm with no teams spawned
484 MUTATOR_CALLHOOK(GetTeamCount);
488 c1 = c2 = c3 = c4 = 0;
495 // find out what teams are allowed if necessary
498 head = find(world, classname, teament_name);
501 if(!(g_domination && head.netname == ""))
503 if(head.team == COLOR_TEAM1)
505 else if(head.team == COLOR_TEAM2)
507 else if(head.team == COLOR_TEAM3)
509 else if(head.team == COLOR_TEAM4)
512 head = find(head, classname, teament_name);
516 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
518 if(autocvar_bot_vs_human && for_whom)
520 if(autocvar_bot_vs_human > 0)
523 if(clienttype(for_whom) == CLIENTTYPE_BOT)
531 if(clienttype(for_whom) == CLIENTTYPE_BOT)
538 // if player has a forced team, ONLY allow that one
539 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
541 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
543 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
545 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
549 float PlayerValue(entity p)
552 // FIXME: it always returns 1...
555 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
556 // teams that are allowed will now have their player counts stored in c1...c4
557 void GetTeamCounts(entity ignore)
561 // now count how many players are on each team already
563 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
564 // also remember the lowest-scoring player
566 FOR_EACH_CLIENT(head)
569 if(head.classname == "player")
571 else if(head.team_forced > 0)
572 t = head.team_forced; // reserve the spot
575 if(head != ignore)// && head.netname != "")
577 value = PlayerValue(head);
578 if(clienttype(head) == CLIENTTYPE_BOT)
617 // if the player who has a forced team has not joined yet, reserve the spot
618 if(autocvar_g_campaign)
620 switch(autocvar_g_campaign_forceteam)
622 case 1: if(c1 == cb1) ++c1; break;
623 case 2: if(c2 == cb2) ++c2; break;
624 case 3: if(c3 == cb3) ++c3; break;
625 case 4: if(c4 == cb4) ++c4; break;
630 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
632 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
634 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
638 case 1: ca = c1; cba = cb1; sa = team1_score; break;
639 case 2: ca = c2; cba = cb2; sa = team2_score; break;
640 case 3: ca = c3; cba = cb3; sa = team3_score; break;
641 case 4: ca = c4; cba = cb4; sa = team4_score; break;
645 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
646 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
647 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
648 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
659 if(clienttype(e) == CLIENTTYPE_REAL)
668 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
684 // the more we're at the end of the match, the more take scores into account
685 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
692 // returns # of smallest team (1, 2, 3, 4)
693 // NOTE: Assumes CheckAllowedTeams has already been called!
694 float FindSmallestTeam(entity pl, float ignore_pl)
699 // find out what teams are available
700 //CheckAllowedTeams();
702 // make sure there are at least 2 teams to join
704 totalteams = totalteams + 1;
706 totalteams = totalteams + 1;
708 totalteams = totalteams + 1;
710 totalteams = totalteams + 1;
712 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
717 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
718 return 1; // special case for campaign and player joining
719 else if(g_domination)
720 error("Too few teams available for domination\n");
722 error("Too few teams available for ctf\n");
724 error("Too few teams available for key hunt\n");
726 error("Too few teams available for freeze tag\n");
728 error("Too few teams available for team deathmatch\n");
731 // count how many players are in each team
735 GetTeamCounts(world);
737 RandomSelection_Init();
740 if(TeamSmallerEqThanTeam(2, t, pl))
742 if(TeamSmallerEqThanTeam(3, t, pl))
744 if(TeamSmallerEqThanTeam(4, t, pl))
747 // now t is the minimum, or A minimum!
748 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
749 RandomSelection_Add(world, 1, string_null, 1, 1);
750 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
751 RandomSelection_Add(world, 2, string_null, 1, 1);
752 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
753 RandomSelection_Add(world, 3, string_null, 1, 1);
754 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
755 RandomSelection_Add(world, 4, string_null, 1, 1);
757 return RandomSelection_chosen_float;
760 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
762 float smallest, selectedteam;
764 // don't join a team if we're not playing a team game
768 // find out what teams are available
769 CheckAllowedTeams(pl);
771 // if we don't care what team he ends up on, put him on whatever team he entered as.
772 // if he's not on a valid team, then let other code put him on the smallest team
775 if( c1 >= 0 && pl.team == COLOR_TEAM1)
776 selectedteam = pl.team;
777 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
778 selectedteam = pl.team;
779 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
780 selectedteam = pl.team;
781 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
782 selectedteam = pl.team;
788 if(!only_return_best)
790 SetPlayerColors(pl, selectedteam - 1);
792 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
793 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
794 LogTeamchange(pl.playerid, pl.team, 99);
798 // otherwise end up on the smallest team (handled below)
801 smallest = FindSmallestTeam(pl, TRUE);
803 if(!only_return_best && !pl.bot_forced_team)
805 TeamchangeFrags(self);
808 SetPlayerColors(pl, COLOR_TEAM1 - 1);
810 else if(smallest == 2)
812 SetPlayerColors(pl, COLOR_TEAM2 - 1);
814 else if(smallest == 3)
816 SetPlayerColors(pl, COLOR_TEAM3 - 1);
818 else if(smallest == 4)
820 SetPlayerColors(pl, COLOR_TEAM4 - 1);
824 error("smallest team: invalid team\n");
827 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
829 if(pl.deadflag == DEAD_NO)
830 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
836 //void() ctf_playerchanged;
837 void SV_ChangeTeam(float _color)
839 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
841 // in normal deathmatch we can just apply the color and we're done
843 SetPlayerColors(self, _color);
847 scolor = self.clientcolors & 0x0F;
848 dcolor = _color & 0x0F;
850 if(scolor == COLOR_TEAM1 - 1)
852 else if(scolor == COLOR_TEAM2 - 1)
854 else if(scolor == COLOR_TEAM3 - 1)
856 else // if(scolor == COLOR_TEAM4 - 1)
858 if(dcolor == COLOR_TEAM1 - 1)
860 else if(dcolor == COLOR_TEAM2 - 1)
862 else if(dcolor == COLOR_TEAM3 - 1)
864 else // if(dcolor == COLOR_TEAM4 - 1)
867 CheckAllowedTeams(self);
869 if(dteam == 1 && c1 < 0) dteam = 4;
870 if(dteam == 4 && c4 < 0) dteam = 3;
871 if(dteam == 3 && c3 < 0) dteam = 2;
872 if(dteam == 2 && c2 < 0) dteam = 1;
874 // not changing teams
877 //bprint("same team change\n");
878 SetPlayerTeam(self, dteam, steam, TRUE);
882 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
883 sprint(self, "Team changes not allowed\n");
884 return; // changing teams is not allowed
887 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
888 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
891 if(!TeamSmallerEqThanTeam(dteam, steam, self))
893 sprint(self, "Cannot change to a larger/better/shinier team\n");
898 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
900 if(self.classname == "player" && steam != dteam)
902 // reduce frags during a team change
903 TeamchangeFrags(self);
906 SetPlayerTeam(self, dteam, steam, FALSE);
908 if(self.classname == "player" && steam != dteam)
910 // kill player when changing teams
911 if(self.deadflag == DEAD_NO)
912 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
914 //ctf_playerchanged();
917 void ShufflePlayerOutOfTeam (float source_team)
919 float smallestteam, smallestteam_count, steam;
920 float lowest_bot_score, lowest_player_score;
921 entity head, lowest_bot, lowest_player, selected;
924 smallestteam_count = 999999999;
926 if(c1 >= 0 && c1 < smallestteam_count)
929 smallestteam_count = c1;
931 if(c2 >= 0 && c2 < smallestteam_count)
934 smallestteam_count = c2;
936 if(c3 >= 0 && c3 < smallestteam_count)
939 smallestteam_count = c3;
941 if(c4 >= 0 && c4 < smallestteam_count)
944 smallestteam_count = c4;
949 bprint("warning: no smallest team\n");
955 else if(source_team == 2)
957 else if(source_team == 3)
959 else // if(source_team == 4)
963 lowest_bot_score = 999999999;
964 lowest_player = world;
965 lowest_player_score = 999999999;
967 // find the lowest-scoring player & bot of that team
968 FOR_EACH_PLAYER(head)
970 if(head.team == steam)
974 if(head.totalfrags < lowest_bot_score)
977 lowest_bot_score = head.totalfrags;
982 if(head.totalfrags < lowest_player_score)
984 lowest_player = head;
985 lowest_player_score = head.totalfrags;
991 // prefers to move a bot...
992 if(lowest_bot != world)
993 selected = lowest_bot;
994 // but it will move a player if it has to
996 selected = lowest_player;
997 // don't do anything if it couldn't find anyone
1000 bprint("warning: couldn't find a player to move from team\n");
1004 // smallest team gains a member
1005 if(smallestteam == 1)
1009 else if(smallestteam == 2)
1013 else if(smallestteam == 3)
1017 else if(smallestteam == 4)
1023 bprint("warning: destination team invalid\n");
1026 // source team loses a member
1027 if(source_team == 1)
1031 else if(source_team == 2)
1035 else if(source_team == 3)
1039 else if(source_team == 4)
1045 bprint("warning: source team invalid\n");
1049 // move the player to the new team
1050 TeamchangeFrags(selected);
1051 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1053 if(selected.deadflag == DEAD_NO)
1054 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1055 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1058 // code from here on is just to support maps that don't have team entities
1059 void tdm_spawnteam (string teamname, float teamcolor)
1063 e.classname = "tdm_team";
1064 e.netname = teamname;
1069 // spawn some default teams if the map is not set up for tdm
1070 void tdm_spawnteams()
1074 numteams = autocvar_g_tdm_teams_override;
1076 numteams = autocvar_g_tdm_teams;
1077 numteams = bound(2, numteams, 4);
1079 tdm_spawnteam("Red", COLOR_TEAM1-1);
1080 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1082 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1084 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1087 void tdm_delayedinit()
1089 // if no teams are found, spawn defaults
1090 if (find(world, classname, "tdm_team") == world)
1096 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);