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 void PrintWelcomeMessage(entity pl)
238 string s, grap_msg, temp, mutator;
240 /*if(self.welcomemessage_time < time)
242 if(self.welcomemessage_time2 > time)
244 self.welcomemessage_time2 = time + 0.8; */
246 if(self.welcomemessage_time2 > time) return;
247 self.welcomemessage_time2 = time + 1.0;
249 if(cvar("g_minstagib"))
250 mutator = "^2Minstagib ^1";
251 else if(cvar("g_instagib"))
252 mutator = "^2Instagib ^1";
253 else if(cvar("g_rocketarena"))
254 mutator = "^2Rocketarena ^1";
258 if(cvar("g_grappling_hook"))
259 grap_msg = strzone("\n\nBind a key to ^1+hook^8 to use the grappling hook\n");
261 s = strcat(s, "\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", self.versionmessage);
262 s = strcat(s, "^8\n\nMatch type is ^1", mutator, gamemode_name, "^8\n");
264 if(self.classname == "observer" || self.classname == "spectator") {
265 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
272 "\n\n\n^8Welcome, ", pl.netname, "^8\n",
275 temp = strzone(temp);
278 if (cvar_string("g_mutatormsg") != "") {
279 s = strcat(s, "\n\nSpecial gameplay tips: ", cvar_string("g_mutatormsg"));
282 if (cvar_string("sv_motd") != "") {
283 s = strcat(s, "\n\nMOTD: ", cvar_string("sv_motd"));
293 void SetPlayerColors(entity pl, float _color)
297 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
299 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
300 pl.clientcolors = 16*cl + cl;*/
303 pants = _color & 0x0F;
304 shirt = _color & 0xF0;
308 setcolor(pl, 16*pants + pants);
310 setcolor(pl, shirt + pants);
314 void SetPlayerTeam(entity pl, float t, float s, float noprint)
319 _color = COLOR_TEAM4 - 1;
321 _color = COLOR_TEAM3 - 1;
323 _color = COLOR_TEAM2 - 1;
325 _color = COLOR_TEAM1 - 1;
327 SetPlayerColors(pl,_color);
329 if(!noprint && t != s)
331 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
332 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
341 // set c1...c4 to show what teams are allowed
342 void CheckAllowedTeams ()
351 c1 = c2 = c3 = c4 = -1;
352 cb1 = cb2 = cb3 = cb4 = 0;
355 teament_name = "dom_team";
357 teament_name = "ctf_team";
359 teament_name = "tdm_team";
362 // cover anything else by treating it like tdm with no teams spawned
363 dm = cvar("g_tdm_teams");
365 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
369 c1 = c2 = c3 = c4 = 0;
382 // first find out what teams are allowed
383 head = find(world, classname, teament_name);
386 if(!(g_domination && head.netname == ""))
388 if(head.team == COLOR_TEAM1)
392 if(head.team == COLOR_TEAM2)
396 if(head.team == COLOR_TEAM3)
400 if(head.team == COLOR_TEAM4)
405 head = find(head, classname, teament_name);
409 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
410 // teams that are allowed will now have their player counts stored in c1...c4
411 void GetTeamCounts(entity ignore)
414 // now count how many players are on each team already
416 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
417 // also remember the lowest-scoring player
419 head = find(world, classname, "player");
422 if(head != ignore)// && head.netname != "")
424 if(head.team == COLOR_TEAM1)
432 if(head.team == COLOR_TEAM2)
440 if(head.team == COLOR_TEAM3)
448 if(head.team == COLOR_TEAM4)
457 head = find(head, classname, "player");
461 // returns # of smallest team (1, 2, 3, 4)
462 // NOTE: Assumes CheckAllowedTeams has already been called!
463 float FindSmallestTeam(entity pl, float ignore_pl)
465 float totalteams, smallestteam, smallestteam_count, balance_type;
468 // find out what teams are available
469 //CheckAllowedTeams();
471 // make sure there are at least 2 teams to join
473 totalteams = totalteams + 1;
475 totalteams = totalteams + 1;
477 totalteams = totalteams + 1;
479 totalteams = totalteams + 1;
484 error("Too few teams available for domination\n");
486 error("Too few teams available for ctf\n");
488 error("Too few teams available for team deathmatch\n");
492 // count how many players are in each team
494 GetTeamCounts(world);
500 // c1...c4 now have counts of each team
501 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
504 smallestteam_count = 999;
506 // 2 gives priority to what team you're already on, 1 goes in order
507 // 2 doesn't seem to work though...
510 if(balance_type == 1)
512 if(c1 >= 0 && c1 < smallestteam_count)
515 smallestteam_count = c1;
517 if(c2 >= 0 && c2 < smallestteam_count)
520 smallestteam_count = c2;
522 if(c3 >= 0 && c3 < smallestteam_count)
525 smallestteam_count = c3;
527 if(c4 >= 0 && c4 < smallestteam_count)
530 smallestteam_count = c4;
535 if(c1 >= 0 && (c1 < smallestteam_count ||
536 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
539 smallestteam_count = c1;
541 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
542 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
545 smallestteam_count = c2;
547 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
548 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
551 smallestteam_count = c3;
553 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
554 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
557 smallestteam_count = c4;
564 float JoinBestTeam(entity pl, float only_return_best)
566 float smallest, selectedteam;
568 g_domination = cvar("g_domination");
569 g_ctf = cvar("g_ctf");
571 // don't join a team if we're not playing a team game
572 if(!cvar("teamplay") && !g_domination && !g_ctf)
575 // find out what teams are available
578 // if we don't care what team he ends up on, put him on whatever team he entered as.
579 // if he's not on a valid team, then let other code put him on the smallest team
580 if(!cvar("balance_teams") && !cvar("force_balance"))
582 if( c1 >= 0 && pl.team == COLOR_TEAM1)
583 selectedteam = pl.team;
584 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
585 selectedteam = pl.team;
586 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
587 selectedteam = pl.team;
588 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
589 selectedteam = pl.team;
594 if(!only_return_best)
595 SetPlayerColors(pl, selectedteam - 1);
598 // otherwise end up on the smallest team (handled below)
601 smallest = FindSmallestTeam(pl, TRUE);
604 if(!only_return_best)
608 SetPlayerColors(pl, COLOR_TEAM1 - 1);
610 else if(smallest == 2)
612 SetPlayerColors(pl, COLOR_TEAM2 - 1);
614 else if(smallest == 3)
616 SetPlayerColors(pl, COLOR_TEAM3 - 1);
618 else if(smallest == 4)
620 SetPlayerColors(pl, COLOR_TEAM4 - 1);
624 error("smallest team: invalid team\n");
632 void SV_ChangeTeam(float _color)
634 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
636 // in normal deathmatch we can just apply the color and we're done
637 if(!cvar("teamplay")) {
638 SetPlayerColors(self, _color);
642 scolor = self.clientcolors & 0x0F;
643 dcolor = _color & 0x0F;
645 if(scolor == COLOR_TEAM1 - 1)
647 else if(scolor == COLOR_TEAM2 - 1)
649 else if(scolor == COLOR_TEAM3 - 1)
651 else if(scolor == COLOR_TEAM4 - 1)
653 if(dcolor == COLOR_TEAM1 - 1)
655 else if(dcolor == COLOR_TEAM2 - 1)
657 else if(dcolor == COLOR_TEAM3 - 1)
659 else if(dcolor == COLOR_TEAM4 - 1)
662 // remap invalid teams in dom & ctf
663 if(cvar("g_ctf") && dteam == 3)
665 else if(cvar("g_ctf") && dteam == 4)
667 else if(cvar("g_domination") && cvar("g_domination_default_teams") < 3)
674 else if(cvar("g_domination") && cvar("g_domination_default_teams") < 4)
680 // not changing teams
683 //bprint("same team change\n");
684 SetPlayerTeam(self, dteam, steam, TRUE);
690 if(self.classname == "player" && steam != dteam)
692 // kill player when changing teams
693 if(self.deadflag == DEAD_NO)
694 self.event_damage(self, self, 10000, DEATH_KILL, self.origin, '0 0 0');
696 // reduce frags during a team change
697 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
700 if(cvar("g_changeteam_banned"))
702 sprint(self, "Team changes not allowed\n");
703 return; // changing teams is not allowed
706 if(cvar("g_balance_teams_prevent_imbalance"))
708 // only allow changing to a smaller or equal size team
710 // find out what teams are available
712 // count how many players on each team
713 GetTeamCounts(world);
716 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
721 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
726 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
731 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
738 sprint(self, "Cannot change to an invalid team\n");
744 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
746 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
748 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
750 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
753 if(scount) // started at a valid, nonempty team
755 // check if we're trying to change to a larger team that doens't have bots to swap with
756 if(dcount >= scount && dbotcount <= 0)
758 sprint(self, "Cannot change to a larger team\n");
759 return; // can't change to a larger team
765 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
767 SetPlayerTeam(self, dteam, steam, FALSE);
771 void ShufflePlayerOutOfTeam (float source_team)
773 float smallestteam, smallestteam_count, steam;
774 float lowest_bot_score, lowest_player_score;
775 entity head, lowest_bot, lowest_player, selected;
778 smallestteam_count = 999;
780 if(c1 >= 0 && c1 < smallestteam_count)
783 smallestteam_count = c1;
785 if(c2 >= 0 && c2 < smallestteam_count)
788 smallestteam_count = c2;
790 if(c3 >= 0 && c3 < smallestteam_count)
793 smallestteam_count = c3;
795 if(c4 >= 0 && c4 < smallestteam_count)
798 smallestteam_count = c4;
803 bprint("warning: no smallest team\n");
809 else if(source_team == 2)
811 else if(source_team == 3)
813 else if(source_team == 4)
817 lowest_bot_score = 9999;
818 lowest_player = world;
819 lowest_player_score = 9999;
821 // find the lowest-scoring player & bot of that team
822 head = find(world, classname, "player");
825 if(head.team == steam)
829 if(head.frags < lowest_bot_score)
832 lowest_bot_score = head.frags;
837 if(head.frags < lowest_player_score)
839 lowest_player = head;
840 lowest_player_score = head.frags;
844 head = find(head, classname, "player");
847 // prefers to move a bot...
848 if(lowest_bot != world)
849 selected = lowest_bot;
850 // but it will move a player if it has to
852 selected = lowest_player;
853 // don't do anything if it couldn't find anyone
856 bprint("warning: couldn't find a player to move from team\n");
860 // smallest team gains a member
861 if(smallestteam == 1)
865 else if(smallestteam == 2)
869 else if(smallestteam == 3)
873 else if(smallestteam == 4)
879 bprint("warning: destination team invalid\n");
882 // source team loses a member
887 else if(source_team == 2)
891 else if(source_team == 3)
895 else if(source_team == 4)
901 bprint("warning: source team invalid\n");
905 // move the player to the new team
906 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
909 // part of g_balance_teams_force
910 // occasionally perform an audit of the teams to make
911 // sure they're more or less balanced in player count.
914 float numplayers, numteams, average;
915 if(!cvar("g_balance_teams_force"))
917 if(!cvar("teamplay"))
920 if(audit_teams_time > time)
923 audit_teams_time = time + 4 + random();
925 // bprint("Auditing teams\n");
928 GetTeamCounts(world);
931 numteams = numplayers = 0;
934 numteams = numteams + 1;
935 numplayers = numplayers + c1;
939 numteams = numteams + 1;
940 numplayers = numplayers + c2;
944 numteams = numteams + 1;
945 numplayers = numplayers + c3;
949 numteams = numteams + 1;
950 numplayers = numplayers + c4;
954 return; // no players to move around
956 return; // don't bother shuffling if for some reason there aren't any teams
958 average = ceil(numplayers / numteams);
961 return; // that's weird...
963 if(c1 && c1 > average)
965 bprint("Rebalancing Teams\n");
966 //bprint("Shuffle from team 1\n");
967 ShufflePlayerOutOfTeam(1);
969 if(c2 && c2 > average)
971 bprint("Rebalancing Teams\n");
972 //bprint("Shuffle from team 2\n");
973 ShufflePlayerOutOfTeam(2);
975 if(c3 && c3 > average)
977 bprint("Rebalancing Teams\n");
978 //bprint("Shuffle from team 3\n");
979 ShufflePlayerOutOfTeam(3);
981 if(c4 && c4 > average)
983 bprint("Rebalancing Teams\n");
984 //bprint("Shuffle from team 4\n");
985 ShufflePlayerOutOfTeam(4);
988 // if teams are still unbalanced, balance them further in the next audit,
989 // which will happen sooner (keep doing rapid audits until things are in order)
990 audit_teams_time = time + 0.7 + random()*0.3;
995 /*void(entity e, float first) UpdateTeamScore =
1000 WriteByte (MSG_ALL, SVC_UPDATENAME);
1001 WriteByte (MSG_ALL, clientno);
1002 WriteString (MSG_ALL, e.netname);
1004 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1005 WriteByte (MSG_ALL, clientno);
1006 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1009 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1010 WriteByte (MSG_ALL, clientno);
1011 WriteShort (MSG_ALL, e.frags + 10000);
1017 // code from here on is just to support maps that don't have team entities
1018 void tdm_spawnteam (string teamname, float teamcolor)
1022 e.classname = "tdm_team";
1023 e.netname = teamname;
1028 // spawn some default teams if the map is not set up for tdm
1029 void() tdm_spawnteams =
1033 numteams = cvar("g_tdm_teams");
1035 tdm_spawnteam("Red", 4);
1036 tdm_spawnteam("Blue", 13);
1039 void() tdm_delayedinit =
1041 self.think = SUB_Remove;
1042 self.nextthink = time;
1043 // if no teams are found, spawn defaults
1044 if (find(world, classname, "tdm_team") == world)
1052 e.think = tdm_delayedinit;
1053 e.nextthink = time + 0.1;