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");
240 registercvar("_motd", "");
241 registercvar("_mutatormsg", "");
242 cvar_set("_motd", linewrap(cvar_string("sv_motd"), 50));
243 cvar_set("_mutatormsg", linewrap(cvar_string("g_mutatormsg"), 50));
246 string GetClientVersionMessage(float v) {
247 local string versionmsg;
249 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";
250 // either that or someone wants to be funny
251 } else if (v != cvar("gameversion")) {
252 if(v < cvar("gameversion")) {
253 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
255 versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
258 versionmsg = "^2client version and server version are compatible.^8";
260 return strzone(versionmsg);
265 void PrintWelcomeMessage(entity pl)
267 string s, mutator, modifications;
269 /*if(self.welcomemessage_time < time)
271 if(self.welcomemessage_time2 > time)
273 self.welcomemessage_time2 = time + 0.8; */
275 if(self.welcomemessage_time2 > time) return;
276 self.welcomemessage_time2 = time + 1.0;
278 if(cvar("g_minstagib"))
279 mutator = "^2Minstagib ^1";
280 else if(cvar("g_instagib"))
281 mutator = "^2Instagib ^1";
282 else if(cvar("g_rocketarena"))
283 mutator = "^2Rocketarena ^1";
285 if(cvar("g_midair")) {
286 // to protect against unheedingly made changes
288 modifications = strcat(modifications, ", ");
290 modifications = "midair";
292 if(cvar("g_vampire")) {
294 modifications = strcat(modifications, ", ");
296 modifications = strcat(modifications, "vampire");
298 if(cvar("g_laserguided_missile")) {
300 modifications = strcat(modifications, ", ");
302 modifications = strcat(modifications, "laser-guided-missiles");
305 local string versionmessage;
306 versionmessage = GetClientVersionMessage(self.version);
308 s = strcat(s, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
309 s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
311 if(modifications != "")
312 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
314 if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
315 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
321 if (cvar("g_grappling_hook"))
322 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
324 if (cvar_string("_mutatormsg") != "") {
325 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
328 if (cvar_string("_motd") != "") {
329 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
341 void SetPlayerColors(entity pl, float _color)
345 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
347 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
348 pl.clientcolors = 16*cl + cl;*/
351 pants = _color & 0x0F;
352 shirt = _color & 0xF0;
356 setcolor(pl, 16*pants + pants);
358 setcolor(pl, shirt + pants);
362 void SetPlayerTeam(entity pl, float t, float s, float noprint)
367 _color = COLOR_TEAM4 - 1;
369 _color = COLOR_TEAM3 - 1;
371 _color = COLOR_TEAM2 - 1;
373 _color = COLOR_TEAM1 - 1;
375 SetPlayerColors(pl,_color);
377 if(!noprint && t != s)
379 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
380 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
389 // set c1...c4 to show what teams are allowed
390 void CheckAllowedTeams ()
399 c1 = c2 = c3 = c4 = -1;
400 cb1 = cb2 = cb3 = cb4 = 0;
403 teament_name = "dom_team";
405 teament_name = "ctf_team";
407 teament_name = "tdm_team";
410 // cover anything else by treating it like tdm with no teams spawned
411 dm = cvar("g_tdm_teams");
413 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
417 c1 = c2 = c3 = c4 = 0;
430 // first find out what teams are allowed
431 head = find(world, classname, teament_name);
434 if(!(g_domination && head.netname == ""))
436 if(head.team == COLOR_TEAM1)
440 if(head.team == COLOR_TEAM2)
444 if(head.team == COLOR_TEAM3)
448 if(head.team == COLOR_TEAM4)
453 head = find(head, classname, teament_name);
457 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
458 // teams that are allowed will now have their player counts stored in c1...c4
459 void GetTeamCounts(entity ignore)
462 // now count how many players are on each team already
464 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
465 // also remember the lowest-scoring player
467 head = find(world, classname, "player");
470 if(head != ignore)// && head.netname != "")
472 if(head.team == COLOR_TEAM1)
480 if(head.team == COLOR_TEAM2)
488 if(head.team == COLOR_TEAM3)
496 if(head.team == COLOR_TEAM4)
505 head = find(head, classname, "player");
509 // returns # of smallest team (1, 2, 3, 4)
510 // NOTE: Assumes CheckAllowedTeams has already been called!
511 float FindSmallestTeam(entity pl, float ignore_pl)
513 float totalteams, smallestteam, smallestteam_count, balance_type;
516 // find out what teams are available
517 //CheckAllowedTeams();
519 // make sure there are at least 2 teams to join
521 totalteams = totalteams + 1;
523 totalteams = totalteams + 1;
525 totalteams = totalteams + 1;
527 totalteams = totalteams + 1;
532 error("Too few teams available for domination\n");
534 error("Too few teams available for ctf\n");
536 error("Too few teams available for team deathmatch\n");
540 // count how many players are in each team
542 GetTeamCounts(world);
548 // c1...c4 now have counts of each team
549 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
552 smallestteam_count = 999;
554 // 2 gives priority to what team you're already on, 1 goes in order
555 // 2 doesn't seem to work though...
558 if(balance_type == 1)
560 if(c1 >= 0 && c1 < smallestteam_count)
563 smallestteam_count = c1;
565 if(c2 >= 0 && c2 < smallestteam_count)
568 smallestteam_count = c2;
570 if(c3 >= 0 && c3 < smallestteam_count)
573 smallestteam_count = c3;
575 if(c4 >= 0 && c4 < smallestteam_count)
578 smallestteam_count = c4;
583 if(c1 >= 0 && (c1 < smallestteam_count ||
584 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
587 smallestteam_count = c1;
589 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
590 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
593 smallestteam_count = c2;
595 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
596 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
599 smallestteam_count = c3;
601 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
602 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
605 smallestteam_count = c4;
612 float JoinBestTeam(entity pl, float only_return_best)
614 float smallest, selectedteam;
616 g_domination = cvar("g_domination");
617 g_ctf = cvar("g_ctf");
619 // don't join a team if we're not playing a team game
620 if(!cvar("teamplay") && !g_domination && !g_ctf)
623 // find out what teams are available
627 if(cvar("g_domination"))
629 if(cvar("g_domination_default_teams") < 3)
631 if(cvar("g_domination_default_teams") < 4)
635 // if we don't care what team he ends up on, put him on whatever team he entered as.
636 // if he's not on a valid team, then let other code put him on the smallest team
637 if(!cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
639 if( c1 >= 0 && pl.team == COLOR_TEAM1)
640 selectedteam = pl.team;
641 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
642 selectedteam = pl.team;
643 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
644 selectedteam = pl.team;
645 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
646 selectedteam = pl.team;
651 if(!only_return_best)
652 SetPlayerColors(pl, selectedteam - 1);
655 // otherwise end up on the smallest team (handled below)
658 smallest = FindSmallestTeam(pl, TRUE);
661 if(!only_return_best)
665 SetPlayerColors(pl, COLOR_TEAM1 - 1);
667 else if(smallest == 2)
669 SetPlayerColors(pl, COLOR_TEAM2 - 1);
671 else if(smallest == 3)
673 SetPlayerColors(pl, COLOR_TEAM3 - 1);
675 else if(smallest == 4)
677 SetPlayerColors(pl, COLOR_TEAM4 - 1);
681 error("smallest team: invalid team\n");
683 if(pl.deadflag == DEAD_NO)
684 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
691 void SV_ChangeTeam(float _color)
693 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
695 // in normal deathmatch we can just apply the color and we're done
696 if(!cvar("teamplay")) {
697 SetPlayerColors(self, _color);
701 scolor = self.clientcolors & 0x0F;
702 dcolor = _color & 0x0F;
704 if(scolor == COLOR_TEAM1 - 1)
706 else if(scolor == COLOR_TEAM2 - 1)
708 else if(scolor == COLOR_TEAM3 - 1)
710 else if(scolor == COLOR_TEAM4 - 1)
712 if(dcolor == COLOR_TEAM1 - 1)
714 else if(dcolor == COLOR_TEAM2 - 1)
716 else if(dcolor == COLOR_TEAM3 - 1)
718 else if(dcolor == COLOR_TEAM4 - 1)
721 // remap invalid teams in dom & ctf
722 if(cvar("g_ctf") && dteam == 3)
724 else if(cvar("g_ctf") && dteam == 4)
726 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
733 else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
739 // not changing teams
742 //bprint("same team change\n");
743 SetPlayerTeam(self, dteam, steam, TRUE);
749 if(cvar("g_changeteam_banned"))
751 sprint(self, "Team changes not allowed\n");
752 return; // changing teams is not allowed
755 if(cvar("g_balance_teams_prevent_imbalance"))
757 // only allow changing to a smaller or equal size team
759 // find out what teams are available
761 // count how many players on each team
762 GetTeamCounts(world);
765 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
770 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
775 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
780 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
787 sprint(self, "Cannot change to an invalid team\n");
793 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
795 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
797 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
799 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
802 if(scount) // started at a valid, nonempty team
804 // check if we're trying to change to a larger team that doens't have bots to swap with
805 if(dcount >= scount && dbotcount <= 0)
807 sprint(self, "Cannot change to a larger team\n");
808 return; // can't change to a larger team
814 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
816 SetPlayerTeam(self, dteam, steam, FALSE);
817 if(cvar("teamplay") && self.classname == "player" && steam != dteam)
819 // kill player when changing teams
820 if(self.deadflag == DEAD_NO)
821 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
822 // reduce frags during a team change
823 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
828 void ShufflePlayerOutOfTeam (float source_team)
830 float smallestteam, smallestteam_count, steam;
831 float lowest_bot_score, lowest_player_score;
832 entity head, lowest_bot, lowest_player, selected;
836 smallestteam_count = 999;
838 if(c1 >= 0 && c1 < smallestteam_count)
841 smallestteam_count = c1;
843 if(c2 >= 0 && c2 < smallestteam_count)
846 smallestteam_count = c2;
848 if(c3 >= 0 && c3 < smallestteam_count)
851 smallestteam_count = c3;
853 if(c4 >= 0 && c4 < smallestteam_count)
856 smallestteam_count = c4;
861 bprint("warning: no smallest team\n");
867 else if(source_team == 2)
869 else if(source_team == 3)
871 else if(source_team == 4)
875 lowest_bot_score = 9999;
876 lowest_player = world;
877 lowest_player_score = 9999;
879 // find the lowest-scoring player & bot of that team
880 head = find(world, classname, "player");
883 if(head.team == steam)
887 if(head.frags < lowest_bot_score)
890 lowest_bot_score = head.frags;
895 if(head.frags < lowest_player_score)
897 lowest_player = head;
898 lowest_player_score = head.frags;
902 head = find(head, classname, "player");
905 // prefers to move a bot...
906 if(lowest_bot != world)
907 selected = lowest_bot;
908 // but it will move a player if it has to
910 selected = lowest_player;
911 // don't do anything if it couldn't find anyone
914 bprint("warning: couldn't find a player to move from team\n");
918 // smallest team gains a member
919 if(smallestteam == 1)
923 else if(smallestteam == 2)
927 else if(smallestteam == 3)
931 else if(smallestteam == 4)
937 bprint("warning: destination team invalid\n");
940 // source team loses a member
945 else if(source_team == 2)
949 else if(source_team == 3)
953 else if(source_team == 4)
959 bprint("warning: source team invalid\n");
963 // move the player to the new team
964 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
966 if(selected.deadflag == DEAD_NO)
967 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
968 m = "You have been moved into a different team to improve team balance\nYou are now on: ";
969 if (selected.team == 5)
970 m = strcat(m, "^1Red Team");
971 else if (selected.team == 14)
972 m = strcat(m, "^4Blue Team");
973 else if (selected.team == 10)
974 m = strcat(m, "^6Pink Team");
975 else if (selected.team == 13)
976 m = strcat(m, "^3Yellow Team");
977 centerprint(selected, m);
980 // part of g_balance_teams_force
981 // occasionally perform an audit of the teams to make
982 // sure they're more or less balanced in player count.
985 float numplayers, numteams, average;
986 if(!cvar("g_balance_teams_force"))
988 if(!cvar("teamplay"))
991 if(audit_teams_time > time)
994 audit_teams_time = time + 4 + random();
996 // bprint("Auditing teams\n");
999 GetTeamCounts(world);
1002 numteams = numplayers = 0;
1005 numteams = numteams + 1;
1006 numplayers = numplayers + c1;
1010 numteams = numteams + 1;
1011 numplayers = numplayers + c2;
1015 numteams = numteams + 1;
1016 numplayers = numplayers + c3;
1020 numteams = numteams + 1;
1021 numplayers = numplayers + c4;
1025 return; // no players to move around
1027 return; // don't bother shuffling if for some reason there aren't any teams
1029 average = ceil(numplayers / numteams);
1032 return; // that's weird...
1034 if(c1 && c1 > average)
1036 bprint("Rebalancing Teams\n");
1037 //bprint("Shuffle from team 1\n");
1038 ShufflePlayerOutOfTeam(1);
1040 if(c2 && c2 > average)
1042 bprint("Rebalancing Teams\n");
1043 //bprint("Shuffle from team 2\n");
1044 ShufflePlayerOutOfTeam(2);
1046 if(c3 && c3 > average)
1048 bprint("Rebalancing Teams\n");
1049 //bprint("Shuffle from team 3\n");
1050 ShufflePlayerOutOfTeam(3);
1052 if(c4 && c4 > average)
1054 bprint("Rebalancing Teams\n");
1055 //bprint("Shuffle from team 4\n");
1056 ShufflePlayerOutOfTeam(4);
1059 // if teams are still unbalanced, balance them further in the next audit,
1060 // which will happen sooner (keep doing rapid audits until things are in order)
1061 audit_teams_time = time + 0.7 + random()*0.3;
1066 /*void(entity e, float first) UpdateTeamScore =
1071 WriteByte (MSG_ALL, SVC_UPDATENAME);
1072 WriteByte (MSG_ALL, clientno);
1073 WriteString (MSG_ALL, e.netname);
1075 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1076 WriteByte (MSG_ALL, clientno);
1077 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1080 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1081 WriteByte (MSG_ALL, clientno);
1082 WriteShort (MSG_ALL, e.frags + 10000);
1088 // code from here on is just to support maps that don't have team entities
1089 void tdm_spawnteam (string teamname, float teamcolor)
1093 e.classname = "tdm_team";
1094 e.netname = teamname;
1099 // spawn some default teams if the map is not set up for tdm
1100 void() tdm_spawnteams =
1104 numteams = cvar("g_tdm_teams");
1106 tdm_spawnteam("Red", 4);
1107 tdm_spawnteam("Blue", 13);
1110 void() tdm_delayedinit =
1112 self.think = SUB_Remove;
1113 self.nextthink = time;
1114 // if no teams are found, spawn defaults
1115 if (find(world, classname, "tdm_team") == world)
1123 e.think = tdm_delayedinit;
1124 e.nextthink = time + 0.1;