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";
55 void runematch_init();
59 void LogTeamchange(float player_id, float team_number, float type)
61 if(!autocvar_sv_eventlog)
67 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
70 void default_delayedinit()
72 if(!scores_initialized)
76 void ActivateTeamplay()
78 serverflags |= SERVERFLAG_TEAMPLAY;
82 void InitGameplayMode()
84 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
86 qualifying_override = -1;
90 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
95 MapInfo_LoadMapSettings(mapname);
97 serverflags &~= SERVERFLAG_TEAMPLAY;
99 if not(cvar_value_issafe(world.fog))
101 print("The current map contains a potentially harmful fog setting, ignored\n");
102 world.fog = string_null;
104 if(MapInfo_Map_fog != "")
105 if(MapInfo_Map_fog == "none")
106 world.fog = string_null;
108 world.fog = strzone(MapInfo_Map_fog);
109 clientstuff = strzone(MapInfo_Map_clientstuff);
111 MapInfo_ClearTemps();
113 // set both here, gamemode can override it later
114 timelimit_override = autocvar_timelimit_override;
115 fraglimit_override = autocvar_fraglimit_override;
116 leadlimit_override = autocvar_leadlimit_override;
117 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
127 if(autocvar_g_tdm_team_spawns)
128 have_team_spawns = -1; // request team spawns
134 fraglimit_override = autocvar_g_domination_point_limit;
135 leadlimit_override = autocvar_g_domination_point_leadlimit;
136 MUTATOR_ADD(gamemode_domination);
137 have_team_spawns = -1; // request team spawns
143 fraglimit_override = autocvar_capturelimit_override;
144 leadlimit_override = autocvar_captureleadlimit_override;
145 MUTATOR_ADD(gamemode_ctf);
146 have_team_spawns = -1; // request team spawns
151 // ActivateTeamplay();
152 fraglimit_override = autocvar_g_runematch_point_limit;
153 leadlimit_override = autocvar_g_runematch_point_leadlimit;
159 fraglimit_override = autocvar_g_lms_lives_override;
160 leadlimit_override = 0; // not supported by LMS
161 if(fraglimit_override == 0)
162 fraglimit_override = -1;
163 MUTATOR_ADD(gamemode_lms);
168 fraglimit_override = autocvar_g_arena_point_limit;
169 leadlimit_override = autocvar_g_arena_point_leadlimit;
170 maxspawned = autocvar_g_arena_maxspawned;
173 arena_roundbased = autocvar_g_arena_roundbased;
179 fraglimit_override = autocvar_g_ca_point_limit;
180 leadlimit_override = autocvar_g_ca_point_leadlimit;
181 precache_sound("ctf/red_capture.wav");
182 precache_sound("ctf/blue_capture.wav");
187 fraglimit_override = autocvar_g_keyhunt_point_limit;
188 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
189 MUTATOR_ADD(gamemode_keyhunt);
195 fraglimit_override = autocvar_g_freezetag_point_limit;
196 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
197 MUTATOR_ADD(gamemode_freezetag);
203 ScoreRules_assault();
204 have_team_spawns = -1; // request team spawns
210 have_team_spawns = -1; // request team spawns
211 MUTATOR_ADD(gamemode_onslaught);
217 if(autocvar_g_race_teams)
220 race_teams = bound(2, autocvar_g_race_teams, 4);
221 have_team_spawns = -1; // request team spawns
226 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
227 fraglimit_override = autocvar_g_race_laps_limit;
228 leadlimit_override = 0; // currently not supported by race
233 g_race_qualifying = 1;
234 fraglimit_override = 0;
235 leadlimit_override = 0;
240 fraglimit_override = autocvar_g_nexball_goallimit;
241 leadlimit_override = autocvar_g_nexball_goalleadlimit;
243 have_team_spawns = -1; // request team spawns
244 MUTATOR_ADD(gamemode_nexball);
249 MUTATOR_ADD(gamemode_keepaway);
255 cache_mutatormsg = strzone("");
256 cache_lastmutatormsg = strzone("");
258 // enforce the server's universal frag/time limits
259 if(!autocvar_g_campaign)
261 if(fraglimit_override >= 0)
262 cvar_set("fraglimit", ftos(fraglimit_override));
263 if(timelimit_override >= 0)
264 cvar_set("timelimit", ftos(timelimit_override));
265 if(leadlimit_override >= 0)
266 cvar_set("leadlimit", ftos(leadlimit_override));
267 if(qualifying_override >= 0)
268 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
273 // we need to find out the correct value for g_race_qualifying
274 if(autocvar_g_campaign)
276 g_race_qualifying = 1;
278 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
280 g_race_qualifying = 2;
281 race_fraglimit = autocvar_fraglimit;
282 race_leadlimit = autocvar_leadlimit;
283 race_timelimit = autocvar_timelimit;
284 cvar_set("fraglimit", "0");
285 cvar_set("leadlimit", "0");
286 cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
289 g_race_qualifying = 0;
294 if(g_race_qualifying)
295 independent_players = 1;
300 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
303 string GetClientVersionMessage() {
305 if (self.version_mismatch) {
306 if(self.version < autocvar_gameversion) {
307 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
309 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
312 versionmsg = "^2client version and server version are compatible.^8";
317 string getwelcomemessage(void)
319 string s, modifications, motd;
322 MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
323 modifications = ret_string;
326 modifications = strcat(modifications, ", MinstaGib");
329 if(g_weaponarena_random)
330 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
332 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
334 if(autocvar_g_start_weapon_laser == 0)
335 modifications = strcat(modifications, ", No start weapons");
336 if(autocvar_sv_gravity < 800)
337 modifications = strcat(modifications, ", Low gravity");
338 if(g_cloaked && !g_cts)
339 modifications = strcat(modifications, ", Cloaked");
341 modifications = strcat(modifications, ", Hook");
343 modifications = strcat(modifications, ", Midair");
345 modifications = strcat(modifications, ", Piñata");
346 if(g_weapon_stay && !g_cts)
347 modifications = strcat(modifications, ", Weapons stay");
349 modifications = strcat(modifications, ", Blood loss");
351 modifications = strcat(modifications, ", Jet pack");
352 if(autocvar_g_powerups == 0)
353 modifications = strcat(modifications, ", No powerups");
354 if(autocvar_g_powerups > 0)
355 modifications = strcat(modifications, ", Powerups");
356 modifications = substring(modifications, 2, strlen(modifications) - 2);
358 string versionmessage;
359 versionmessage = GetClientVersionMessage();
361 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
362 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
364 if(modifications != "")
365 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
367 if (g_grappling_hook)
368 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
370 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
372 if(cache_lastmutatormsg)
373 strunzone(cache_lastmutatormsg);
375 strunzone(cache_mutatormsg);
376 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
377 cache_mutatormsg = strzone(cache_lastmutatormsg);
380 if (cache_mutatormsg != "") {
381 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
384 motd = autocvar_sv_motd;
386 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
391 void SetPlayerColors(entity pl, float _color)
395 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
397 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
398 pl.clientcolors = 16*cl + cl;*/
401 pants = _color & 0x0F;
402 shirt = _color & 0xF0;
406 setcolor(pl, 16*pants + pants);
408 setcolor(pl, shirt + pants);
412 void SetPlayerTeam(entity pl, float t, float s, float noprint)
417 _color = COLOR_TEAM4 - 1;
419 _color = COLOR_TEAM3 - 1;
421 _color = COLOR_TEAM2 - 1;
423 _color = COLOR_TEAM1 - 1;
425 SetPlayerColors(pl,_color);
428 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
431 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
436 // set c1...c4 to show what teams are allowed
437 void CheckAllowedTeams (entity for_whom)
443 c1 = c2 = c3 = c4 = -1;
444 cb1 = cb2 = cb3 = cb4 = 0;
446 teament_name = string_null;
449 // onslaught is special
450 head = findchain(classname, "onslaught_generator");
453 if (head.team == COLOR_TEAM1) c1 = 0;
454 if (head.team == COLOR_TEAM2) c2 = 0;
455 if (head.team == COLOR_TEAM3) c3 = 0;
456 if (head.team == COLOR_TEAM4) c4 = 0;
460 else if(g_domination)
461 teament_name = "dom_team";
463 teament_name = "ctf_team";
465 teament_name = "tdm_team";
467 teament_name = "nexball_team";
469 c1 = c2 = 0; // Assault always has 2 teams
472 // cover anything else by treating it like tdm with no teams spawned
479 MUTATOR_CALLHOOK(GetTeamCount);
483 c1 = c2 = c3 = c4 = 0;
490 // find out what teams are allowed if necessary
493 head = find(world, classname, teament_name);
496 if(!(g_domination && head.netname == ""))
498 if(head.team == COLOR_TEAM1)
500 else if(head.team == COLOR_TEAM2)
502 else if(head.team == COLOR_TEAM3)
504 else if(head.team == COLOR_TEAM4)
507 head = find(head, classname, teament_name);
511 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
513 if(autocvar_bot_vs_human && for_whom)
515 if(autocvar_bot_vs_human > 0)
518 if(clienttype(for_whom) == CLIENTTYPE_BOT)
526 if(clienttype(for_whom) == CLIENTTYPE_BOT)
533 // if player has a forced team, ONLY allow that one
534 if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
536 else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
538 else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
540 else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
544 float PlayerValue(entity p)
547 // FIXME: it always returns 1...
550 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
551 // teams that are allowed will now have their player counts stored in c1...c4
552 void GetTeamCounts(entity ignore)
556 // now count how many players are on each team already
558 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
559 // also remember the lowest-scoring player
561 FOR_EACH_CLIENT(head)
564 if(head.classname == "player")
566 else if(head.team_forced > 0)
567 t = head.team_forced; // reserve the spot
570 if(head != ignore)// && head.netname != "")
572 value = PlayerValue(head);
573 if(clienttype(head) == CLIENTTYPE_BOT)
612 // if the player who has a forced team has not joined yet, reserve the spot
613 if(autocvar_g_campaign)
615 switch(autocvar_g_campaign_forceteam)
617 case 1: if(c1 == cb1) ++c1; break;
618 case 2: if(c2 == cb2) ++c2; break;
619 case 3: if(c3 == cb3) ++c3; break;
620 case 4: if(c4 == cb4) ++c4; break;
625 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
627 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
629 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
633 case 1: ca = c1; cba = cb1; sa = team1_score; break;
634 case 2: ca = c2; cba = cb2; sa = team2_score; break;
635 case 3: ca = c3; cba = cb3; sa = team3_score; break;
636 case 4: ca = c4; cba = cb4; sa = team4_score; break;
640 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
641 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
642 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
643 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
654 if(clienttype(e) == CLIENTTYPE_REAL)
663 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
679 // the more we're at the end of the match, the more take scores into account
680 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
687 // returns # of smallest team (1, 2, 3, 4)
688 // NOTE: Assumes CheckAllowedTeams has already been called!
689 float FindSmallestTeam(entity pl, float ignore_pl)
694 // find out what teams are available
695 //CheckAllowedTeams();
697 // make sure there are at least 2 teams to join
699 totalteams = totalteams + 1;
701 totalteams = totalteams + 1;
703 totalteams = totalteams + 1;
705 totalteams = totalteams + 1;
707 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
712 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
713 return 1; // special case for campaign and player joining
714 else if(g_domination)
715 error("Too few teams available for domination\n");
717 error("Too few teams available for ctf\n");
719 error("Too few teams available for key hunt\n");
721 error("Too few teams available for freeze tag\n");
723 error("Too few teams available for team deathmatch\n");
726 // count how many players are in each team
730 GetTeamCounts(world);
732 RandomSelection_Init();
735 if(TeamSmallerEqThanTeam(2, t, pl))
737 if(TeamSmallerEqThanTeam(3, t, pl))
739 if(TeamSmallerEqThanTeam(4, t, pl))
742 // now t is the minimum, or A minimum!
743 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
744 RandomSelection_Add(world, 1, string_null, 1, 1);
745 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
746 RandomSelection_Add(world, 2, string_null, 1, 1);
747 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
748 RandomSelection_Add(world, 3, string_null, 1, 1);
749 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
750 RandomSelection_Add(world, 4, string_null, 1, 1);
752 return RandomSelection_chosen_float;
755 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
757 float smallest, selectedteam;
759 // don't join a team if we're not playing a team game
763 // find out what teams are available
764 CheckAllowedTeams(pl);
766 // if we don't care what team he ends up on, put him on whatever team he entered as.
767 // if he's not on a valid team, then let other code put him on the smallest team
770 if( c1 >= 0 && pl.team == COLOR_TEAM1)
771 selectedteam = pl.team;
772 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
773 selectedteam = pl.team;
774 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
775 selectedteam = pl.team;
776 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
777 selectedteam = pl.team;
783 if(!only_return_best)
785 SetPlayerColors(pl, selectedteam - 1);
787 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
788 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
789 LogTeamchange(pl.playerid, pl.team, 99);
793 // otherwise end up on the smallest team (handled below)
796 smallest = FindSmallestTeam(pl, TRUE);
798 if(!only_return_best && !pl.bot_forced_team)
800 TeamchangeFrags(self);
803 SetPlayerColors(pl, COLOR_TEAM1 - 1);
805 else if(smallest == 2)
807 SetPlayerColors(pl, COLOR_TEAM2 - 1);
809 else if(smallest == 3)
811 SetPlayerColors(pl, COLOR_TEAM3 - 1);
813 else if(smallest == 4)
815 SetPlayerColors(pl, COLOR_TEAM4 - 1);
819 error("smallest team: invalid team\n");
822 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
824 if(pl.deadflag == DEAD_NO)
825 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
831 //void() ctf_playerchanged;
832 void SV_ChangeTeam(float _color)
834 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
836 // in normal deathmatch we can just apply the color and we're done
838 SetPlayerColors(self, _color);
842 scolor = self.clientcolors & 0x0F;
843 dcolor = _color & 0x0F;
845 if(scolor == COLOR_TEAM1 - 1)
847 else if(scolor == COLOR_TEAM2 - 1)
849 else if(scolor == COLOR_TEAM3 - 1)
851 else // if(scolor == COLOR_TEAM4 - 1)
853 if(dcolor == COLOR_TEAM1 - 1)
855 else if(dcolor == COLOR_TEAM2 - 1)
857 else if(dcolor == COLOR_TEAM3 - 1)
859 else // if(dcolor == COLOR_TEAM4 - 1)
862 CheckAllowedTeams(self);
864 if(dteam == 1 && c1 < 0) dteam = 4;
865 if(dteam == 4 && c4 < 0) dteam = 3;
866 if(dteam == 3 && c3 < 0) dteam = 2;
867 if(dteam == 2 && c2 < 0) dteam = 1;
869 // not changing teams
872 //bprint("same team change\n");
873 SetPlayerTeam(self, dteam, steam, TRUE);
877 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
878 sprint(self, "Team changes not allowed\n");
879 return; // changing teams is not allowed
882 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
883 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
886 if(!TeamSmallerEqThanTeam(dteam, steam, self))
888 sprint(self, "Cannot change to a larger/better/shinier team\n");
893 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
895 if(self.classname == "player" && steam != dteam)
897 // reduce frags during a team change
898 TeamchangeFrags(self);
901 SetPlayerTeam(self, dteam, steam, FALSE);
903 if(self.classname == "player" && steam != dteam)
905 // kill player when changing teams
906 if(self.deadflag == DEAD_NO)
907 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
911 void ShufflePlayerOutOfTeam (float source_team)
913 float smallestteam, smallestteam_count, steam;
914 float lowest_bot_score, lowest_player_score;
915 entity head, lowest_bot, lowest_player, selected;
918 smallestteam_count = 999999999;
920 if(c1 >= 0 && c1 < smallestteam_count)
923 smallestteam_count = c1;
925 if(c2 >= 0 && c2 < smallestteam_count)
928 smallestteam_count = c2;
930 if(c3 >= 0 && c3 < smallestteam_count)
933 smallestteam_count = c3;
935 if(c4 >= 0 && c4 < smallestteam_count)
938 smallestteam_count = c4;
943 bprint("warning: no smallest team\n");
949 else if(source_team == 2)
951 else if(source_team == 3)
953 else // if(source_team == 4)
957 lowest_bot_score = 999999999;
958 lowest_player = world;
959 lowest_player_score = 999999999;
961 // find the lowest-scoring player & bot of that team
962 FOR_EACH_PLAYER(head)
964 if(head.team == steam)
968 if(head.totalfrags < lowest_bot_score)
971 lowest_bot_score = head.totalfrags;
976 if(head.totalfrags < lowest_player_score)
978 lowest_player = head;
979 lowest_player_score = head.totalfrags;
985 // prefers to move a bot...
986 if(lowest_bot != world)
987 selected = lowest_bot;
988 // but it will move a player if it has to
990 selected = lowest_player;
991 // don't do anything if it couldn't find anyone
994 bprint("warning: couldn't find a player to move from team\n");
998 // smallest team gains a member
999 if(smallestteam == 1)
1003 else if(smallestteam == 2)
1007 else if(smallestteam == 3)
1011 else if(smallestteam == 4)
1017 bprint("warning: destination team invalid\n");
1020 // source team loses a member
1021 if(source_team == 1)
1025 else if(source_team == 2)
1029 else if(source_team == 3)
1033 else if(source_team == 4)
1039 bprint("warning: source team invalid\n");
1043 // move the player to the new team
1044 TeamchangeFrags(selected);
1045 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1047 if(selected.deadflag == DEAD_NO)
1048 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1049 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1052 // code from here on is just to support maps that don't have team entities
1053 void tdm_spawnteam (string teamname, float teamcolor)
1057 e.classname = "tdm_team";
1058 e.netname = teamname;
1063 // spawn some default teams if the map is not set up for tdm
1064 void tdm_spawnteams()
1068 numteams = autocvar_g_tdm_teams_override;
1070 numteams = autocvar_g_tdm_teams;
1071 numteams = bound(2, numteams, 4);
1073 tdm_spawnteam("Red", COLOR_TEAM1-1);
1074 tdm_spawnteam("Blue", COLOR_TEAM2-1);
1076 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1078 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1081 void tdm_delayedinit()
1083 // if no teams are found, spawn defaults
1084 if (find(world, classname, "tdm_team") == world)
1090 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);