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;
104 game = cvar ("gamecfg"); // load game options
106 // game cvars get reset before map changes
107 // then map's cfg sets them as desired
109 // FIXME: also set a message or game mode name to print to players when the join
111 // set both here, gamemode can override it later
112 timelimit_override = cvar("timelimit_override");
113 fraglimit_override = cvar("fraglimit_override");
115 if(game == GAME_DOMINATION || cvar("g_domination"))
117 game = GAME_DOMINATION;
118 cvar_set("g_domination", "1");
122 fraglimit_override = cvar("g_domination_point_limit");
124 gamemode_name = "Domination";
127 else if(game == GAME_CTF || cvar("g_ctf"))
130 cvar_set("g_ctf", "1");
134 fraglimit_override = cvar("g_ctf_capture_limit");
136 gamemode_name = "Capture the Flag";
139 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
141 game = GAME_RUNEMATCH;
142 cvar_set("g_runematch", "1");
144 if(cvar("deathmatch_force_teamplay"))
147 fraglimit_override = cvar("g_runematch_point_limit");
149 gamemode_name = "Rune Match";
155 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
157 if(!cvar("deathmatch"))
158 cvar_set("deathmatch", "1");
161 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
163 game = GAME_TEAM_DEATHMATCH;
164 gamemode_name = "Team Deathmatch";
167 cvar_set("g_tdm", "1");
171 game = GAME_DEATHMATCH;
172 gamemode_name = "Deathmatch";
176 fraglimit_override = cvar("fraglimit_override");
178 else if(game == GAME_LMS || cvar("g_lms"))
181 cvar_set("g_lms", "1");
182 fraglimit_override = cvar("g_lms_lives_override");
183 if(fraglimit_override == 0)
184 fraglimit_override = -1;
185 gamemode_name = "Last Man Standing";
187 cvar_set("teamplay", "0");
188 lms_lowest_lives = 999;
192 // we can only assume...
193 gamemode_name = "Deathmatch";
196 /* else if(game == GAME_TEAM_DEATHMATCH)
198 if(!cvar("deathmatch"))
199 cvar_set("deathmatch", "1");
201 //if(!cvar("teamplay"))
202 // cvar_set("teamplay", "3");
205 fraglimit_override = cvar("fraglimit_override");
208 // enforce the server's universal frag/time limits
209 if(fraglimit_override >= 0)
210 cvar_set("fraglimit", ftos(fraglimit_override));
211 if(timelimit_override >= 0)
212 cvar_set("timelimit", ftos(timelimit_override));
214 if (game == GAME_DOMINATION)//cvar("g_domination"))
216 else if (game == GAME_CTF)//cvar("g_ctf"))
218 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
220 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
223 // those mutators rule each other out
224 if(cvar("g_minstagib"))
226 cvar_set("g_instagib", "0");
227 cvar_set("g_rocketarena", "0");
229 if(cvar("g_instagib"))
231 cvar_set("g_minstagib", "0");
232 cvar_set("g_rocketarena", "0");
234 if(cvar("g_rocketarena"))
236 cvar_set("g_instagib", "0");
237 cvar_set("g_minstagib", "0");
241 string GetClientVersionMessage(float v) {
242 local string versionmsg;
244 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";
245 // either that or someone wants to be funny
246 } else if (v != cvar("g_nexuizversion_major")) {
247 if(v < cvar("g_nexuizversion_major")) {
248 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
250 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
253 versionmsg = "^2client version and server version are compatible.^8";
255 return strzone(versionmsg);
260 void PrintWelcomeMessage(entity pl)
262 string s, mutator, modifications;
264 /*if(self.welcomemessage_time < time)
266 if(self.welcomemessage_time2 > time)
268 self.welcomemessage_time2 = time + 0.8; */
270 if(self.welcomemessage_time2 > time) return;
271 self.welcomemessage_time2 = time + 1.0;
273 if(cvar("g_minstagib"))
274 mutator = "^2Minstagib ^1";
275 else if(cvar("g_instagib"))
276 mutator = "^2Instagib ^1";
277 else if(cvar("g_rocketarena"))
278 mutator = "^2Rocketarena ^1";
280 if(cvar("g_midair")) {
281 // to protect against unheedingly made changes
283 modifications = strcat(modifications, ", ");
285 modifications = "midair";
287 if(cvar("g_vampire")) {
289 modifications = strcat(modifications, ", ");
291 modifications = strcat(modifications, "vampire");
293 if(cvar("g_homing_missile")) {
295 modifications = strcat(modifications, ", ");
297 modifications = strcat(modifications, "homing missiles");
300 local string versionmessage;
301 versionmessage = GetClientVersionMessage(self.version);
303 s = strcat(s, "\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
304 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
306 if(modifications != "")
307 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
309 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("g_nexuizversion_major")) {
310 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
316 if (cvar("g_grappling_hook"))
317 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
319 if (cvar_string("g_mutatormsg") != "") {
320 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("g_mutatormsg"));
323 if (cvar_string("sv_motd") != "") {
324 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("sv_motd"));
336 void SetPlayerColors(entity pl, float _color)
340 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
342 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
343 pl.clientcolors = 16*cl + cl;*/
346 pants = _color & 0x0F;
347 shirt = _color & 0xF0;
351 setcolor(pl, 16*pants + pants);
353 setcolor(pl, shirt + pants);
357 void SetPlayerTeam(entity pl, float t, float s, float noprint)
362 _color = COLOR_TEAM4 - 1;
364 _color = COLOR_TEAM3 - 1;
366 _color = COLOR_TEAM2 - 1;
368 _color = COLOR_TEAM1 - 1;
370 SetPlayerColors(pl,_color);
372 if(!noprint && t != s)
374 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
375 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
384 // set c1...c4 to show what teams are allowed
385 void CheckAllowedTeams ()
394 c1 = c2 = c3 = c4 = -1;
395 cb1 = cb2 = cb3 = cb4 = 0;
398 teament_name = "dom_team";
400 teament_name = "ctf_team";
402 teament_name = "tdm_team";
405 // cover anything else by treating it like tdm with no teams spawned
406 dm = cvar("g_tdm_teams");
408 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
412 c1 = c2 = c3 = c4 = 0;
425 // first find out what teams are allowed
426 head = find(world, classname, teament_name);
429 if(!(g_domination && head.netname == ""))
431 if(head.team == COLOR_TEAM1)
435 if(head.team == COLOR_TEAM2)
439 if(head.team == COLOR_TEAM3)
443 if(head.team == COLOR_TEAM4)
448 head = find(head, classname, teament_name);
452 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
453 // teams that are allowed will now have their player counts stored in c1...c4
454 void GetTeamCounts(entity ignore)
457 // now count how many players are on each team already
459 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
460 // also remember the lowest-scoring player
462 head = find(world, classname, "player");
465 if(head != ignore)// && head.netname != "")
467 if(head.team == COLOR_TEAM1)
475 if(head.team == COLOR_TEAM2)
483 if(head.team == COLOR_TEAM3)
491 if(head.team == COLOR_TEAM4)
500 head = find(head, classname, "player");
504 // returns # of smallest team (1, 2, 3, 4)
505 // NOTE: Assumes CheckAllowedTeams has already been called!
506 float FindSmallestTeam(entity pl, float ignore_pl)
508 float totalteams, smallestteam, smallestteam_count, balance_type;
511 // find out what teams are available
512 //CheckAllowedTeams();
514 // make sure there are at least 2 teams to join
516 totalteams = totalteams + 1;
518 totalteams = totalteams + 1;
520 totalteams = totalteams + 1;
522 totalteams = totalteams + 1;
527 error("Too few teams available for domination\n");
529 error("Too few teams available for ctf\n");
531 error("Too few teams available for team deathmatch\n");
535 // count how many players are in each team
537 GetTeamCounts(world);
543 // c1...c4 now have counts of each team
544 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
547 smallestteam_count = 999;
549 // 2 gives priority to what team you're already on, 1 goes in order
550 // 2 doesn't seem to work though...
553 if(balance_type == 1)
555 if(c1 >= 0 && c1 < smallestteam_count)
558 smallestteam_count = c1;
560 if(c2 >= 0 && c2 < smallestteam_count)
563 smallestteam_count = c2;
565 if(c3 >= 0 && c3 < smallestteam_count)
568 smallestteam_count = c3;
570 if(c4 >= 0 && c4 < smallestteam_count)
573 smallestteam_count = c4;
578 if(c1 >= 0 && (c1 < smallestteam_count ||
579 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
582 smallestteam_count = c1;
584 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
585 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
588 smallestteam_count = c2;
590 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
591 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
594 smallestteam_count = c3;
596 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
597 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
600 smallestteam_count = c4;
607 float JoinBestTeam(entity pl, float only_return_best)
609 float smallest, selectedteam;
611 g_domination = cvar("g_domination");
612 g_ctf = cvar("g_ctf");
614 // don't join a team if we're not playing a team game
615 if(!cvar("teamplay") && !g_domination && !g_ctf)
618 // find out what teams are available
621 // if we don't care what team he ends up on, put him on whatever team he entered as.
622 // if he's not on a valid team, then let other code put him on the smallest team
623 if(!cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
625 if( c1 >= 0 && pl.team == COLOR_TEAM1)
626 selectedteam = pl.team;
627 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
628 selectedteam = pl.team;
629 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
630 selectedteam = pl.team;
631 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
632 selectedteam = pl.team;
637 if(!only_return_best)
638 SetPlayerColors(pl, selectedteam - 1);
641 // otherwise end up on the smallest team (handled below)
644 smallest = FindSmallestTeam(pl, TRUE);
647 if(!only_return_best)
651 SetPlayerColors(pl, COLOR_TEAM1 - 1);
653 else if(smallest == 2)
655 SetPlayerColors(pl, COLOR_TEAM2 - 1);
657 else if(smallest == 3)
659 SetPlayerColors(pl, COLOR_TEAM3 - 1);
661 else if(smallest == 4)
663 SetPlayerColors(pl, COLOR_TEAM4 - 1);
667 error("smallest team: invalid team\n");
675 void SV_ChangeTeam(float _color)
677 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
679 // in normal deathmatch we can just apply the color and we're done
680 if(!cvar("teamplay")) {
681 SetPlayerColors(self, _color);
685 scolor = self.clientcolors & 0x0F;
686 dcolor = _color & 0x0F;
688 if(scolor == COLOR_TEAM1 - 1)
690 else if(scolor == COLOR_TEAM2 - 1)
692 else if(scolor == COLOR_TEAM3 - 1)
694 else if(scolor == COLOR_TEAM4 - 1)
696 if(dcolor == COLOR_TEAM1 - 1)
698 else if(dcolor == COLOR_TEAM2 - 1)
700 else if(dcolor == COLOR_TEAM3 - 1)
702 else if(dcolor == COLOR_TEAM4 - 1)
705 // remap invalid teams in dom & ctf
706 if(cvar("g_ctf") && dteam == 3)
708 else if(cvar("g_ctf") && dteam == 4)
710 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
717 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
723 // not changing teams
726 //bprint("same team change\n");
727 SetPlayerTeam(self, dteam, steam, TRUE);
733 if(cvar("g_changeteam_banned"))
735 sprint(self, "Team changes not allowed\n");
736 return; // changing teams is not allowed
739 if(cvar("g_balance_teams_prevent_imbalance"))
741 // only allow changing to a smaller or equal size team
743 // find out what teams are available
745 // count how many players on each team
746 GetTeamCounts(world);
749 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
754 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
759 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
764 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
771 sprint(self, "Cannot change to an invalid team\n");
777 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
779 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
781 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
783 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
786 if(scount) // started at a valid, nonempty team
788 // check if we're trying to change to a larger team that doens't have bots to swap with
789 if(dcount >= scount && dbotcount <= 0)
791 sprint(self, "Cannot change to a larger team\n");
792 return; // can't change to a larger team
798 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
800 SetPlayerTeam(self, dteam, steam, FALSE);
801 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
803 // kill player when changing teams
804 if(self.deadflag == DEAD_NO)
805 // self.event_damage(self, self, 10000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
807 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
808 // reduce frags during a team change
809 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
814 void ShufflePlayerOutOfTeam (float source_team)
816 float smallestteam, smallestteam_count, steam;
817 float lowest_bot_score, lowest_player_score;
818 entity head, lowest_bot, lowest_player, selected;
822 smallestteam_count = 999;
824 if(c1 >= 0 && c1 < smallestteam_count)
827 smallestteam_count = c1;
829 if(c2 >= 0 && c2 < smallestteam_count)
832 smallestteam_count = c2;
834 if(c3 >= 0 && c3 < smallestteam_count)
837 smallestteam_count = c3;
839 if(c4 >= 0 && c4 < smallestteam_count)
842 smallestteam_count = c4;
847 bprint("warning: no smallest team\n");
853 else if(source_team == 2)
855 else if(source_team == 3)
857 else if(source_team == 4)
861 lowest_bot_score = 9999;
862 lowest_player = world;
863 lowest_player_score = 9999;
865 // find the lowest-scoring player & bot of that team
866 head = find(world, classname, "player");
869 if(head.team == steam)
873 if(head.frags < lowest_bot_score)
876 lowest_bot_score = head.frags;
881 if(head.frags < lowest_player_score)
883 lowest_player = head;
884 lowest_player_score = head.frags;
888 head = find(head, classname, "player");
891 // prefers to move a bot...
892 if(lowest_bot != world)
893 selected = lowest_bot;
894 // but it will move a player if it has to
896 selected = lowest_player;
897 // don't do anything if it couldn't find anyone
900 bprint("warning: couldn't find a player to move from team\n");
904 // smallest team gains a member
905 if(smallestteam == 1)
909 else if(smallestteam == 2)
913 else if(smallestteam == 3)
917 else if(smallestteam == 4)
923 bprint("warning: destination team invalid\n");
926 // source team loses a member
931 else if(source_team == 2)
935 else if(source_team == 3)
939 else if(source_team == 4)
945 bprint("warning: source team invalid\n");
949 // move the player to the new team
950 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
952 if(selected.deadflag == DEAD_NO)
953 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
954 m = "You have been moved into a different team to improve team balance\nYou are now on: ";
955 if (selected.team == 5)
956 m = strcat(m, "^1Red Team");
957 else if (selected.team == 14)
958 m = strcat(m, "^4Blue Team");
959 else if (selected.team == 10)
960 m = strcat(m, "^6Pink Team");
961 else if (selected.team == 13)
962 m = strcat(m, "^3Yellow Team");
963 centerprint(selected, m);
966 // part of g_balance_teams_force
967 // occasionally perform an audit of the teams to make
968 // sure they're more or less balanced in player count.
971 float numplayers, numteams, average;
972 if(!cvar("g_balance_teams_force"))
974 if(!cvar("teamplay"))
977 if(audit_teams_time > time)
980 audit_teams_time = time + 4 + random();
982 // bprint("Auditing teams\n");
985 GetTeamCounts(world);
988 numteams = numplayers = 0;
991 numteams = numteams + 1;
992 numplayers = numplayers + c1;
996 numteams = numteams + 1;
997 numplayers = numplayers + c2;
1001 numteams = numteams + 1;
1002 numplayers = numplayers + c3;
1006 numteams = numteams + 1;
1007 numplayers = numplayers + c4;
1011 return; // no players to move around
1013 return; // don't bother shuffling if for some reason there aren't any teams
1015 average = ceil(numplayers / numteams);
1018 return; // that's weird...
1020 if(c1 && c1 > average)
1022 bprint("Rebalancing Teams\n");
1023 //bprint("Shuffle from team 1\n");
1024 ShufflePlayerOutOfTeam(1);
1026 if(c2 && c2 > average)
1028 bprint("Rebalancing Teams\n");
1029 //bprint("Shuffle from team 2\n");
1030 ShufflePlayerOutOfTeam(2);
1032 if(c3 && c3 > average)
1034 bprint("Rebalancing Teams\n");
1035 //bprint("Shuffle from team 3\n");
1036 ShufflePlayerOutOfTeam(3);
1038 if(c4 && c4 > average)
1040 bprint("Rebalancing Teams\n");
1041 //bprint("Shuffle from team 4\n");
1042 ShufflePlayerOutOfTeam(4);
1045 // if teams are still unbalanced, balance them further in the next audit,
1046 // which will happen sooner (keep doing rapid audits until things are in order)
1047 audit_teams_time = time + 0.7 + random()*0.3;
1052 /*void(entity e, float first) UpdateTeamScore =
1057 WriteByte (MSG_ALL, SVC_UPDATENAME);
1058 WriteByte (MSG_ALL, clientno);
1059 WriteString (MSG_ALL, e.netname);
1061 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1062 WriteByte (MSG_ALL, clientno);
1063 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1066 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1067 WriteByte (MSG_ALL, clientno);
1068 WriteShort (MSG_ALL, e.frags + 10000);
1074 // code from here on is just to support maps that don't have team entities
1075 void tdm_spawnteam (string teamname, float teamcolor)
1079 e.classname = "tdm_team";
1080 e.netname = teamname;
1085 // spawn some default teams if the map is not set up for tdm
1086 void() tdm_spawnteams =
1090 numteams = cvar("g_tdm_teams");
1092 tdm_spawnteam("Red", 4);
1093 tdm_spawnteam("Blue", 13);
1096 void() tdm_delayedinit =
1098 self.think = SUB_Remove;
1099 self.nextthink = time;
1100 // if no teams are found, spawn defaults
1101 if (find(world, classname, "tdm_team") == world)
1109 e.think = tdm_delayedinit;
1110 e.nextthink = time + 0.1;