1 float COLOR_TEAM1 = 5; // red
2 float COLOR_TEAM2 = 14; // blue
3 float COLOR_TEAM3 = 10; // pink
4 float COLOR_TEAM4 = 13; // yellow
7 float GAME_DEATHMATCH = 1;
8 float GAME_TEAM_DEATHMATCH = 2;
9 float GAME_DOMINATION = 3;
11 float GAME_RUNEMATCH = 5;
15 // client counts for each team
17 // # of bots on those teams
18 float cb1, cb2, cb3, cb4;
20 float g_domination, g_ctf, g_tdm;
22 float audit_teams_time;
25 string TeamName(float t)
27 // fixme: Search for team entities and get their .netname's!
36 return "Neutral Team";
38 string ColoredTeamName(float t)
40 // fixme: Search for team entities and get their .netname's!
42 return "^1Red Team^7";
44 return "^4Blue Team^7";
46 return "^6Pink Team^7";
48 return "^3Yellow Team^7";
49 return "Neutral Team";
51 string TeamNoName(float t)
53 // fixme: Search for team entities and get their .netname's!
62 return "Neutral Team";
67 void runematch_init();
73 cvar_set("g_tdm", "0");
74 cvar_set("g_domination", "0");
75 cvar_set("g_ctf", "0");
76 cvar_set("g_runematch", "0");
77 cvar_set("g_lms", "0");
78 cvar_set("teamplay", "0");
81 cvar_set("exit_cfg", "");
84 void ActivateTeamplay()
86 float teamplay_default;
87 teamplay_default = cvar("teamplay_default");
90 cvar_set("teamplay", ftos(teamplay_default));
92 cvar_set("teamplay", "3");
98 void InitGameplayMode()
100 float fraglimit_override, timelimit_override;
102 game = cvar ("gamecfg"); // load game options
104 // game cvars get reset before map changes
105 // then map's cfg sets them as desired
107 // FIXME: also set a message or game mode name to print to players when the join
109 // set both here, gamemode can override it later
110 timelimit_override = cvar("timelimit_override");
111 fraglimit_override = cvar("fraglimit_override");
113 if(game == GAME_DOMINATION || cvar("g_domination"))
115 game = GAME_DOMINATION;
116 cvar_set("g_domination", "1");
120 fraglimit_override = cvar("g_domination_point_limit");
122 gamemode_name = "Domination";
125 else if(game == GAME_CTF || cvar("g_ctf"))
128 cvar_set("g_ctf", "1");
132 fraglimit_override = cvar("g_ctf_capture_limit");
134 gamemode_name = "Capture the Flag";
137 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
139 game = GAME_RUNEMATCH;
140 cvar_set("g_runematch", "1");
142 if(cvar("deathmatch_force_teamplay"))
145 fraglimit_override = cvar("g_runematch_point_limit");
147 gamemode_name = "Rune Match";
153 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
155 if(!cvar("deathmatch"))
156 cvar_set("deathmatch", "1");
159 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
161 game = GAME_TEAM_DEATHMATCH;
162 gamemode_name = "Team Deathmatch";
168 game = GAME_DEATHMATCH;
169 gamemode_name = "Deathmatch";
173 fraglimit_override = cvar("fraglimit_override");
175 else if(game == GAME_LMS || cvar("g_lms"))
178 cvar_set("g_lms", "1");
179 fraglimit_override = cvar("fraglimit_override");
180 gamemode_name = "Last Man Standing";
182 cvar_set("teamplay", "0");
183 lms_lowest_lives = 999;
187 // we can only assume...
188 gamemode_name = "Deathmatch";
191 /* else if(game == GAME_TEAM_DEATHMATCH)
193 if(!cvar("deathmatch"))
194 cvar_set("deathmatch", "1");
196 //if(!cvar("teamplay"))
197 // cvar_set("teamplay", "3");
200 fraglimit_override = cvar("fraglimit_override");
203 // enforce the server's universal frag/time limits
204 if(fraglimit_override >= 0)
205 cvar_set("fraglimit", ftos(fraglimit_override));
206 if(timelimit_override >= 0)
207 cvar_set("timelimit", ftos(timelimit_override));
209 if (game == GAME_DOMINATION)//cvar("g_domination"))
211 else if (game == GAME_CTF)//cvar("g_ctf"))
213 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
215 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
218 // those mutators rule each other out
219 if(cvar("g_minstagib"))
221 cvar_set("g_instagib", "0");
222 cvar_set("g_rocketarena", "0");
224 if(cvar("g_instagib"))
226 cvar_set("g_minstagib", "0");
227 cvar_set("g_rocketarena", "0");
229 if(cvar("g_rocketarena"))
231 cvar_set("g_instagib", "0");
232 cvar_set("g_minstagib", "0");
236 string GetClientVersionMessage(float v) {
237 local string versionmsg;
239 versionmsg = "^1client is too old to get versioninfo.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nUPDATE!!! (http://www.nexuiz.com)^8";
240 // either that or someone wants to be funny
241 } else if (v != cvar("g_nexuizversion_major")) {
242 if(v < cvar("g_nexuizversion_major")) {
243 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
245 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
248 versionmsg = "^2client version and server version are the same.^8";
250 return strzone(versionmsg);
255 void PrintWelcomeMessage(entity pl)
257 string s, mutator, modifications;
259 /*if(self.welcomemessage_time < time)
261 if(self.welcomemessage_time2 > time)
263 self.welcomemessage_time2 = time + 0.8; */
265 if(self.welcomemessage_time2 > time) return;
266 self.welcomemessage_time2 = time + 1.0;
268 if(cvar("g_minstagib"))
269 mutator = "^2Minstagib ^1";
270 else if(cvar("g_instagib"))
271 mutator = "^2Instagib ^1";
272 else if(cvar("g_rocketarena"))
273 mutator = "^2Rocketarena ^1";
275 if(cvar("g_midair")) {
276 // to protect against unheedingly made changes
278 modifications = strcat(modifications, ", ");
280 modifications = "midair";
282 if(cvar("g_vampire")) {
284 modifications = strcat(modifications, ", ");
286 modifications = strcat(modifications, "vampire");
288 if(cvar("g_homing_missile")) {
290 modifications = strcat(modifications, ", ");
292 modifications = strcat(modifications, "homing missiles");
295 local string versionmessage;
296 versionmessage = GetClientVersionMessage(self.version);
298 s = strcat(s, "\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
299 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
301 if(modifications != "")
302 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
304 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("g_nexuizversion_major")) {
305 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
311 if (cvar("g_grappling_hook"))
312 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
314 if (cvar_string("g_mutatormsg") != "") {
315 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("g_mutatormsg"));
318 if (cvar_string("sv_motd") != "") {
319 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("sv_motd"));
331 void SetPlayerColors(entity pl, float _color)
335 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
337 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
338 pl.clientcolors = 16*cl + cl;*/
341 pants = _color & 0x0F;
342 shirt = _color & 0xF0;
346 setcolor(pl, 16*pants + pants);
348 setcolor(pl, shirt + pants);
352 void SetPlayerTeam(entity pl, float t, float s, float noprint)
357 _color = COLOR_TEAM4 - 1;
359 _color = COLOR_TEAM3 - 1;
361 _color = COLOR_TEAM2 - 1;
363 _color = COLOR_TEAM1 - 1;
365 SetPlayerColors(pl,_color);
367 if(!noprint && t != s)
369 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
370 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
379 // set c1...c4 to show what teams are allowed
380 void CheckAllowedTeams ()
389 c1 = c2 = c3 = c4 = -1;
390 cb1 = cb2 = cb3 = cb4 = 0;
393 teament_name = "dom_team";
395 teament_name = "ctf_team";
397 teament_name = "tdm_team";
400 // cover anything else by treating it like tdm with no teams spawned
401 dm = cvar("g_tdm_teams");
403 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
407 c1 = c2 = c3 = c4 = 0;
420 // first find out what teams are allowed
421 head = find(world, classname, teament_name);
424 if(!(g_domination && head.netname == ""))
426 if(head.team == COLOR_TEAM1)
430 if(head.team == COLOR_TEAM2)
434 if(head.team == COLOR_TEAM3)
438 if(head.team == COLOR_TEAM4)
443 head = find(head, classname, teament_name);
447 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
448 // teams that are allowed will now have their player counts stored in c1...c4
449 void GetTeamCounts(entity ignore)
452 // now count how many players are on each team already
454 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
455 // also remember the lowest-scoring player
457 head = find(world, classname, "player");
460 if(head != ignore)// && head.netname != "")
462 if(head.team == COLOR_TEAM1)
470 if(head.team == COLOR_TEAM2)
478 if(head.team == COLOR_TEAM3)
486 if(head.team == COLOR_TEAM4)
495 head = find(head, classname, "player");
499 // returns # of smallest team (1, 2, 3, 4)
500 // NOTE: Assumes CheckAllowedTeams has already been called!
501 float FindSmallestTeam(entity pl, float ignore_pl)
503 float totalteams, smallestteam, smallestteam_count, balance_type;
506 // find out what teams are available
507 //CheckAllowedTeams();
509 // make sure there are at least 2 teams to join
511 totalteams = totalteams + 1;
513 totalteams = totalteams + 1;
515 totalteams = totalteams + 1;
517 totalteams = totalteams + 1;
522 error("Too few teams available for domination\n");
524 error("Too few teams available for ctf\n");
526 error("Too few teams available for team deathmatch\n");
530 // count how many players are in each team
532 GetTeamCounts(world);
538 // c1...c4 now have counts of each team
539 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
542 smallestteam_count = 999;
544 // 2 gives priority to what team you're already on, 1 goes in order
545 // 2 doesn't seem to work though...
548 if(balance_type == 1)
550 if(c1 >= 0 && c1 < smallestteam_count)
553 smallestteam_count = c1;
555 if(c2 >= 0 && c2 < smallestteam_count)
558 smallestteam_count = c2;
560 if(c3 >= 0 && c3 < smallestteam_count)
563 smallestteam_count = c3;
565 if(c4 >= 0 && c4 < smallestteam_count)
568 smallestteam_count = c4;
573 if(c1 >= 0 && (c1 < smallestteam_count ||
574 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
577 smallestteam_count = c1;
579 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
580 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
583 smallestteam_count = c2;
585 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
586 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
589 smallestteam_count = c3;
591 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
592 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
595 smallestteam_count = c4;
602 float JoinBestTeam(entity pl, float only_return_best)
604 float smallest, selectedteam;
606 g_domination = cvar("g_domination");
607 g_ctf = cvar("g_ctf");
609 // don't join a team if we're not playing a team game
610 if(!cvar("teamplay") && !g_domination && !g_ctf)
613 // find out what teams are available
616 // if we don't care what team he ends up on, put him on whatever team he entered as.
617 // if he's not on a valid team, then let other code put him on the smallest team
618 if(!cvar("balance_teams") && !cvar("force_balance"))
620 if( c1 >= 0 && pl.team == COLOR_TEAM1)
621 selectedteam = pl.team;
622 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
623 selectedteam = pl.team;
624 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
625 selectedteam = pl.team;
626 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
627 selectedteam = pl.team;
632 if(!only_return_best)
633 SetPlayerColors(pl, selectedteam - 1);
636 // otherwise end up on the smallest team (handled below)
639 smallest = FindSmallestTeam(pl, TRUE);
642 if(!only_return_best)
646 SetPlayerColors(pl, COLOR_TEAM1 - 1);
648 else if(smallest == 2)
650 SetPlayerColors(pl, COLOR_TEAM2 - 1);
652 else if(smallest == 3)
654 SetPlayerColors(pl, COLOR_TEAM3 - 1);
656 else if(smallest == 4)
658 SetPlayerColors(pl, COLOR_TEAM4 - 1);
662 error("smallest team: invalid team\n");
670 void SV_ChangeTeam(float _color)
672 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
674 // in normal deathmatch we can just apply the color and we're done
675 if(!cvar("teamplay")) {
676 SetPlayerColors(self, _color);
680 scolor = self.clientcolors & 0x0F;
681 dcolor = _color & 0x0F;
683 if(scolor == COLOR_TEAM1 - 1)
685 else if(scolor == COLOR_TEAM2 - 1)
687 else if(scolor == COLOR_TEAM3 - 1)
689 else if(scolor == COLOR_TEAM4 - 1)
691 if(dcolor == COLOR_TEAM1 - 1)
693 else if(dcolor == COLOR_TEAM2 - 1)
695 else if(dcolor == COLOR_TEAM3 - 1)
697 else if(dcolor == COLOR_TEAM4 - 1)
700 // remap invalid teams in dom & ctf
701 if(cvar("g_ctf") && dteam == 3)
703 else if(cvar("g_ctf") && dteam == 4)
705 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
712 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
718 // not changing teams
721 //bprint("same team change\n");
722 SetPlayerTeam(self, dteam, steam, TRUE);
728 if(self.classname == "player" && steam != dteam)
730 // kill player when changing teams
731 if(self.deadflag == DEAD_NO)
732 self.event_damage(self, self, 10000, DEATH_KILL, self.origin, '0 0 0');
734 // reduce frags during a team change
735 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
738 if(cvar("g_changeteam_banned"))
740 sprint(self, "Team changes not allowed\n");
741 return; // changing teams is not allowed
744 if(cvar("g_balance_teams_prevent_imbalance"))
746 // only allow changing to a smaller or equal size team
748 // find out what teams are available
750 // count how many players on each team
751 GetTeamCounts(world);
754 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
759 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
764 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
769 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
776 sprint(self, "Cannot change to an invalid team\n");
782 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
784 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
786 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
788 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
791 if(scount) // started at a valid, nonempty team
793 // check if we're trying to change to a larger team that doens't have bots to swap with
794 if(dcount >= scount && dbotcount <= 0)
796 sprint(self, "Cannot change to a larger team\n");
797 return; // can't change to a larger team
803 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
805 SetPlayerTeam(self, dteam, steam, FALSE);
809 void ShufflePlayerOutOfTeam (float source_team)
811 float smallestteam, smallestteam_count, steam;
812 float lowest_bot_score, lowest_player_score;
813 entity head, lowest_bot, lowest_player, selected;
816 smallestteam_count = 999;
818 if(c1 >= 0 && c1 < smallestteam_count)
821 smallestteam_count = c1;
823 if(c2 >= 0 && c2 < smallestteam_count)
826 smallestteam_count = c2;
828 if(c3 >= 0 && c3 < smallestteam_count)
831 smallestteam_count = c3;
833 if(c4 >= 0 && c4 < smallestteam_count)
836 smallestteam_count = c4;
841 bprint("warning: no smallest team\n");
847 else if(source_team == 2)
849 else if(source_team == 3)
851 else if(source_team == 4)
855 lowest_bot_score = 9999;
856 lowest_player = world;
857 lowest_player_score = 9999;
859 // find the lowest-scoring player & bot of that team
860 head = find(world, classname, "player");
863 if(head.team == steam)
867 if(head.frags < lowest_bot_score)
870 lowest_bot_score = head.frags;
875 if(head.frags < lowest_player_score)
877 lowest_player = head;
878 lowest_player_score = head.frags;
882 head = find(head, classname, "player");
885 // prefers to move a bot...
886 if(lowest_bot != world)
887 selected = lowest_bot;
888 // but it will move a player if it has to
890 selected = lowest_player;
891 // don't do anything if it couldn't find anyone
894 bprint("warning: couldn't find a player to move from team\n");
898 // smallest team gains a member
899 if(smallestteam == 1)
903 else if(smallestteam == 2)
907 else if(smallestteam == 3)
911 else if(smallestteam == 4)
917 bprint("warning: destination team invalid\n");
920 // source team loses a member
925 else if(source_team == 2)
929 else if(source_team == 3)
933 else if(source_team == 4)
939 bprint("warning: source team invalid\n");
943 // move the player to the new team
944 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
947 // part of g_balance_teams_force
948 // occasionally perform an audit of the teams to make
949 // sure they're more or less balanced in player count.
952 float numplayers, numteams, average;
953 if(!cvar("g_balance_teams_force"))
955 if(!cvar("teamplay"))
958 if(audit_teams_time > time)
961 audit_teams_time = time + 4 + random();
963 // bprint("Auditing teams\n");
966 GetTeamCounts(world);
969 numteams = numplayers = 0;
972 numteams = numteams + 1;
973 numplayers = numplayers + c1;
977 numteams = numteams + 1;
978 numplayers = numplayers + c2;
982 numteams = numteams + 1;
983 numplayers = numplayers + c3;
987 numteams = numteams + 1;
988 numplayers = numplayers + c4;
992 return; // no players to move around
994 return; // don't bother shuffling if for some reason there aren't any teams
996 average = ceil(numplayers / numteams);
999 return; // that's weird...
1001 if(c1 && c1 > average)
1003 bprint("Rebalancing Teams\n");
1004 //bprint("Shuffle from team 1\n");
1005 ShufflePlayerOutOfTeam(1);
1007 if(c2 && c2 > average)
1009 bprint("Rebalancing Teams\n");
1010 //bprint("Shuffle from team 2\n");
1011 ShufflePlayerOutOfTeam(2);
1013 if(c3 && c3 > average)
1015 bprint("Rebalancing Teams\n");
1016 //bprint("Shuffle from team 3\n");
1017 ShufflePlayerOutOfTeam(3);
1019 if(c4 && c4 > average)
1021 bprint("Rebalancing Teams\n");
1022 //bprint("Shuffle from team 4\n");
1023 ShufflePlayerOutOfTeam(4);
1026 // if teams are still unbalanced, balance them further in the next audit,
1027 // which will happen sooner (keep doing rapid audits until things are in order)
1028 audit_teams_time = time + 0.7 + random()*0.3;
1033 /*void(entity e, float first) UpdateTeamScore =
1038 WriteByte (MSG_ALL, SVC_UPDATENAME);
1039 WriteByte (MSG_ALL, clientno);
1040 WriteString (MSG_ALL, e.netname);
1042 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1043 WriteByte (MSG_ALL, clientno);
1044 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1047 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1048 WriteByte (MSG_ALL, clientno);
1049 WriteShort (MSG_ALL, e.frags + 10000);
1055 // code from here on is just to support maps that don't have team entities
1056 void tdm_spawnteam (string teamname, float teamcolor)
1060 e.classname = "tdm_team";
1061 e.netname = teamname;
1066 // spawn some default teams if the map is not set up for tdm
1067 void() tdm_spawnteams =
1071 numteams = cvar("g_tdm_teams");
1073 tdm_spawnteam("Red", 4);
1074 tdm_spawnteam("Blue", 13);
1077 void() tdm_delayedinit =
1079 self.think = SUB_Remove;
1080 self.nextthink = time;
1081 // if no teams are found, spawn defaults
1082 if (find(world, classname, "tdm_team") == world)
1090 e.think = tdm_delayedinit;
1091 e.nextthink = time + 0.1;