1 float COLOR_TEAM1 = 5; // red
2 float COLOR_TEAM2 = 14; // blue
3 float COLOR_TEAM3 = 4; // green
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;
13 // client counts for each team
15 // # of bots on those teams
16 float cb1, cb2, cb3, cb4;
19 float g_domination, g_ctf, g_tdm;
21 float audit_teams_time;
24 string TeamName(float t)
26 // fixme: Search for team entities and get their .netname's!
35 return "Neutral Team";
37 string ColoredTeamName(float t)
39 // fixme: Search for team entities and get their .netname's!
41 return "^1Red Team^7";
43 return "^4Blue Team^7";
45 return "^2Green Team^7";
47 return "^3Yellow Team^7";
48 return "Neutral Team";
50 string TeamNoName(float t)
52 // fixme: Search for team entities and get their .netname's!
61 return "Neutral Team";
66 void runematch_init();
72 cvar_set("g_tdm", "0");
73 cvar_set("g_domination", "0");
74 cvar_set("g_ctf", "0");
75 cvar_set("g_runematch", "0");
76 cvar_set("teamplay", "0");
79 cvar_set("exit_cfg", "");
82 void ActivateTeamplay()
84 float teamplay_default;
85 teamplay_default = cvar("teamplay_default");
88 cvar_set("teamplay", ftos(teamplay_default));
90 cvar_set("teamplay", "3");
96 void InitGameplayMode()
98 float fraglimit_override, timelimit_override;
100 game = cvar ("gamecfg"); // load game options
102 // game cvars get reset before map changes
103 // then map's cfg sets them as desired
105 // FIXME: also set a message or game mode name to print to players when the join
107 // set both here, gamemode can override it later
108 timelimit_override = cvar("timelimit_override");
109 fraglimit_override = cvar("fraglimit_override");
111 if(game == GAME_DOMINATION || cvar("g_domination"))
113 game = GAME_DOMINATION;
114 cvar_set("g_domination", "1");
118 fraglimit_override = cvar("g_domination_point_limit");
120 gamemode_name = "Domination";
123 else if(game == GAME_CTF || cvar("g_ctf"))
126 cvar_set("g_ctf", "1");
130 fraglimit_override = cvar("g_ctf_capture_limit");
132 gamemode_name = "Capture the Flag";
135 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
137 game = GAME_RUNEMATCH;
138 cvar_set("g_runematch", "1");
140 if(cvar("deathmatch_force_teamplay"))
143 fraglimit_override = cvar("g_runematch_point_limit");
145 gamemode_name = "Rune Match";
151 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
153 if(!cvar("deathmatch"))
154 cvar_set("deathmatch", "1");
157 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
159 game = GAME_TEAM_DEATHMATCH;
160 gamemode_name = "Team Deathmatch";
166 game = GAME_DEATHMATCH;
167 gamemode_name = "Deathmatch";
171 fraglimit_override = cvar("fraglimit_override");
175 // we can only assume...
176 gamemode_name = "Deathmatch";
179 /* else if(game == GAME_TEAM_DEATHMATCH)
181 if(!cvar("deathmatch"))
182 cvar_set("deathmatch", "1");
184 //if(!cvar("teamplay"))
185 // cvar_set("teamplay", "3");
188 fraglimit_override = cvar("fraglimit_override");
191 // enforce the server's universal frag/time limits
192 if(fraglimit_override >= 0)
193 cvar_set("fraglimit", ftos(fraglimit_override));
194 if(timelimit_override >= 0)
195 cvar_set("timelimit", ftos(timelimit_override));
197 if (game == GAME_DOMINATION)//cvar("g_domination"))
199 else if (game == GAME_CTF)//cvar("g_ctf"))
201 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
203 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
206 // be backwards compatible
207 if (cvar("g_instagib")) {
208 cvar_set("g_start_weapon_laser", ftos(FALSE));
209 cvar_set("g_start_weapon_shotgun", ftos(FALSE));
210 cvar_set("g_start_weapon_uzi", ftos(FALSE));
211 cvar_set("g_start_weapon_grenadelauncher", ftos(FALSE));
212 cvar_set("g_start_weapon_electro", ftos(FALSE));
213 cvar_set("g_start_weapon_crylink", ftos(FALSE));
214 cvar_set("g_start_weapon_nex", ftos(TRUE));
215 cvar_set("g_start_weapon_hagar", ftos(FALSE));
216 cvar_set("g_start_weapon_rocketlauncher", ftos(FALSE));
217 cvar_set("g_pickup_items", ftos(FALSE));
218 cvar_set("g_use_ammunition", ftos(FALSE));
220 if (cvar("g_rocketarena")) {
221 cvar_set("g_start_weapon_laser", ftos(FALSE));
222 cvar_set("g_start_weapon_shotgun", ftos(FALSE));
223 cvar_set("g_start_weapon_uzi", ftos(FALSE));
224 cvar_set("g_start_weapon_grenadelauncher", ftos(FALSE));
225 cvar_set("g_start_weapon_electro", ftos(FALSE));
226 cvar_set("g_start_weapon_crylink", ftos(FALSE));
227 cvar_set("g_start_weapon_nex", ftos(FALSE));
228 cvar_set("g_start_weapon_hagar", ftos(FALSE));
229 cvar_set("g_start_weapon_rocketlauncher", ftos(TRUE));
230 cvar_set("g_pickup_items", ftos(FALSE));
231 cvar_set("g_use_ammunition", ftos(FALSE));
235 void PrintWelcomeMessage(entity pl)
237 string s, grap_msg, temp;
240 if(self.welcomemessage_time < time)
242 if(self.welcomemessage_time2 > time)
244 self.welcomemessage_time2 = time + 0.8;
249 if(cvar("g_grappling_hook"))
250 grap_msg = strzone("\n\nBind a key to ^1+hook^8 to use the grappling hook\n");
252 s = strcat("\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", self.versionmessage, "^8\n\nMatch type is ^1", gamemode_name, "^8\n");
256 "\n\n\n^8Welcome, ", pl.netname, "\n",
259 temp = strzone(temp);
264 "You are on ", ColoredTeamName(pl.team), "^8\n\n",
265 "Go to ^1Menu->Options->Player^8 to change your name, model & team\n",
272 "Go to ^1Menu->Options->Player^8 to change your name & model\n",
277 if (cvar_string("g_mutatormsg")) {
278 s = strcat(s, "\n\nSpecial gameplay tips: ", cvar_string("g_mutatormsg"));
281 if (cvar_string("sv_motd")) {
282 s = strcat(s, "\n\nMOTD: ", cvar_string("sv_motd"));
287 if(cvar("g_grappling_hook"))
288 grap_msg = strzone("\n\nBind a key to +hook to use the grappling hook\n");
290 s = strcat("Match type is ", gamemode_name, "\n");
294 "\n\n\nWelcome, ", pl.netname, "\n",
297 temp = strzone(temp);
302 "You are on ", TeamName(pl.team), "\n\n",
303 "Go to Menu->Options->Player to\nchange your name, model & team\n",
310 "Go to Menu->Options->Player to change your name & model\n",
323 void SetPlayerColors(entity pl, float color)
327 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
329 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
330 pl.clientcolors = 16*cl + cl;*/
332 setcolor(pl, 16*color + color);
335 void SetPlayerTeam(entity pl, float t, float s, float noprint)
339 color = COLOR_TEAM4 - 1;
341 color = COLOR_TEAM3 - 1;
343 color = COLOR_TEAM2 - 1;
345 color = COLOR_TEAM1 - 1;
346 setcolor(pl, 16*color + color);
348 if(!noprint && t != s)
350 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
351 bprint(strcat(pl.netname, " has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
360 // set c1...c4 to show what teams are allowed
361 void CheckAllowedTeams ()
370 c1 = c2 = c3 = c4 = -1;
371 cb1 = cb2 = cb3 = cb4 = 0;
374 teament_name = "dom_team";
376 teament_name = "ctf_team";
378 teament_name = "tdm_team";
381 // cover anything else by treating it like tdm with no teams spawned
382 dm = cvar("g_tdm_teams");
384 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
388 c1 = c2 = c3 = c4 = 0;
401 // first find out what teams are allowed
402 head = find(world, classname, teament_name);
405 if(!(g_domination && head.netname == ""))
407 if(head.team == COLOR_TEAM1)
411 if(head.team == COLOR_TEAM2)
415 if(head.team == COLOR_TEAM3)
419 if(head.team == COLOR_TEAM4)
424 head = find(head, classname, teament_name);
428 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
429 // teams that are allowed will now have their player counts stored in c1...c4
430 void GetTeamCounts(entity ignore)
433 // now count how many players are on each team already
435 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
436 // also remember the lowest-scoring player
438 head = find(world, classname, "player");
441 if(head != ignore)// && head.netname != "")
443 if(head.team == COLOR_TEAM1)
451 if(head.team == COLOR_TEAM2)
459 if(head.team == COLOR_TEAM3)
467 if(head.team == COLOR_TEAM4)
476 head = find(head, classname, "player");
480 // returns # of smallest team (1, 2, 3, 4)
481 // NOTE: Assumes CheckAllowedTeams has already been called!
482 float FindSmallestTeam(entity pl, float ignore_pl)
484 float totalteams, smallestteam, smallestteam_count, balance_type;
487 // find out what teams are available
488 //CheckAllowedTeams();
490 // make sure there are at least 2 teams to join
492 totalteams = totalteams + 1;
494 totalteams = totalteams + 1;
496 totalteams = totalteams + 1;
498 totalteams = totalteams + 1;
503 error("Too few teams available for domination\n");
505 error("Too few teams available for ctf\n");
507 error("Too few teams available for team deathmatch\n");
511 // count how many players are in each team
513 GetTeamCounts(world);
519 // c1...c4 now have counts of each team
520 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
523 smallestteam_count = 999;
525 // 2 gives priority to what team you're already on, 1 goes in order
526 // 2 doesn't seem to work though...
529 if(balance_type == 1)
531 if(c1 >= 0 && c1 < smallestteam_count)
534 smallestteam_count = c1;
536 if(c2 >= 0 && c2 < smallestteam_count)
539 smallestteam_count = c2;
541 if(c3 >= 0 && c3 < smallestteam_count)
544 smallestteam_count = c3;
546 if(c4 >= 0 && c4 < smallestteam_count)
549 smallestteam_count = c4;
554 if(c1 >= 0 && (c1 < smallestteam_count ||
555 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
558 smallestteam_count = c1;
560 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
561 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
564 smallestteam_count = c2;
566 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
567 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
570 smallestteam_count = c3;
572 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
573 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
576 smallestteam_count = c4;
583 float JoinBestTeam(entity pl, float only_return_best)
585 float smallest, selectedteam;
587 g_domination = cvar("g_domination");
588 g_ctf = cvar("g_ctf");
590 // don't join a team if we're not playing a team game
591 if(!cvar("teamplay") && !g_domination && !g_ctf)
594 // find out what teams are available
597 // if we don't care what team he ends up on, put him on whatever team he entered as.
598 // if he's not on a valid team, then let other code put him on the smallest team
599 if(!cvar("balance_teams") && !cvar("force_balance"))
601 if( c1 >= 0 && pl.team == COLOR_TEAM1)
602 selectedteam = pl.team;
603 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
604 selectedteam = pl.team;
605 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
606 selectedteam = pl.team;
607 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
608 selectedteam = pl.team;
613 if(!only_return_best)
614 SetPlayerColors(pl, selectedteam - 1);
617 // otherwise end up on the smallest team (handled below)
620 smallest = FindSmallestTeam(pl, TRUE);
623 if(!only_return_best)
627 SetPlayerColors(pl, COLOR_TEAM1 - 1);
629 else if(smallest == 2)
631 SetPlayerColors(pl, COLOR_TEAM2 - 1);
633 else if(smallest == 3)
635 SetPlayerColors(pl, COLOR_TEAM3 - 1);
637 else if(smallest == 4)
639 SetPlayerColors(pl, COLOR_TEAM4 - 1);
642 error("smallest team: invalid team\n");
649 void SV_ChangeTeam(float color)
651 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
653 scolor = self.clientcolors & 15;
656 if(scolor == COLOR_TEAM1 - 1)
658 else if(scolor == COLOR_TEAM2 - 1)
660 else if(scolor == COLOR_TEAM3 - 1)
662 else if(scolor == COLOR_TEAM4 - 1)
664 if(dcolor == COLOR_TEAM1 - 1)
666 else if(dcolor == COLOR_TEAM2 - 1)
668 else if(dcolor == COLOR_TEAM3 - 1)
670 else if(dcolor == COLOR_TEAM4 - 1)
673 // not changing teams
676 //setcolor(self, 16*color + color);
677 //bprint("same team change\n");
678 SetPlayerTeam(self, dteam, steam, TRUE);
684 if(cvar("g_changeteam_banned"))
686 sprint(self, "Team changes not allowed\n");
687 return; // changing teams is not allowed
690 if(cvar("g_balance_teams_prevent_imbalance"))
692 // only allow changing to a smaller or equal size team
694 // find out what teams are available
696 // count how many players on each team
697 GetTeamCounts(world);
700 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
705 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
710 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
715 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
722 sprint(self, "Cannot change to an invalid team\n");
728 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
730 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
732 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
734 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
737 if(scount) // started at a valid, nonempty team
739 // check if we're trying to change to a larger team that doens't have bots to swap with
740 if(dcount >= scount && dbotcount <= 0)
742 sprint(self, "Cannot change to a larger team\n");
743 return; // can't change to a larger team
749 // reduce frags during a team change
750 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
752 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
754 SetPlayerTeam(self, dteam, steam, FALSE);
755 //setcolor(self, 16*dcolor + dcolor);
759 void ShufflePlayerOutOfTeam (float source_team)
761 float smallestteam, smallestteam_count, steam;
762 float lowest_bot_score, lowest_player_score;
763 entity head, lowest_bot, lowest_player, selected;
766 smallestteam_count = 999;
768 if(c1 >= 0 && c1 < smallestteam_count)
771 smallestteam_count = c1;
773 if(c2 >= 0 && c2 < smallestteam_count)
776 smallestteam_count = c2;
778 if(c3 >= 0 && c3 < smallestteam_count)
781 smallestteam_count = c3;
783 if(c4 >= 0 && c4 < smallestteam_count)
786 smallestteam_count = c4;
791 bprint("warning: no smallest team\n");
797 else if(source_team == 2)
799 else if(source_team == 3)
801 else if(source_team == 4)
805 lowest_bot_score = 9999;
806 lowest_player = world;
807 lowest_player_score = 9999;
809 // find the lowest-scoring player & bot of that team
810 head = find(world, classname, "player");
813 if(head.team == steam)
817 if(head.frags < lowest_bot_score)
820 lowest_bot_score = head.frags;
825 if(head.frags < lowest_player_score)
827 lowest_player = head;
828 lowest_player_score = head.frags;
832 head = find(head, classname, "player");
835 // prefers to move a bot...
836 if(lowest_bot != world)
837 selected = lowest_bot;
838 // but it will move a player if it has to
840 selected = lowest_player;
841 // don't do anything if it couldn't find anyone
844 bprint("warning: couldn't find a player to move from team\n");
848 // smallest team gains a member
849 if(smallestteam == 1)
853 else if(smallestteam == 2)
857 else if(smallestteam == 3)
861 else if(smallestteam == 4)
867 bprint("warning: destination team invalid\n");
870 // source team loses a member
875 else if(source_team == 2)
879 else if(source_team == 3)
883 else if(source_team == 4)
889 bprint("warning: source team invalid\n");
893 // move the player to the new team
894 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
897 // part of g_balance_teams_force
898 // occasionally perform an audit of the teams to make
899 // sure they're more or less balanced in player count.
902 float numplayers, numteams, average;
903 if(!cvar("g_balance_teams_force"))
905 if(!cvar("teamplay"))
908 if(audit_teams_time > time)
911 audit_teams_time = time + 4 + random();
913 // bprint("Auditing teams\n");
916 GetTeamCounts(world);
919 numteams = numplayers = 0;
922 numteams = numteams + 1;
923 numplayers = numplayers + c1;
927 numteams = numteams + 1;
928 numplayers = numplayers + c2;
932 numteams = numteams + 1;
933 numplayers = numplayers + c3;
937 numteams = numteams + 1;
938 numplayers = numplayers + c4;
942 return; // no players to move around
944 return; // don't bother shuffling if for some reason there aren't any teams
946 average = ceil(numplayers / numteams);
949 return; // that's weird...
951 if(c1 && c1 > average)
953 bprint("Rebalancing Teams\n");
954 //bprint("Shuffle from team 1\n");
955 ShufflePlayerOutOfTeam(1);
957 if(c2 && c2 > average)
959 bprint("Rebalancing Teams\n");
960 //bprint("Shuffle from team 2\n");
961 ShufflePlayerOutOfTeam(2);
963 if(c3 && c3 > average)
965 bprint("Rebalancing Teams\n");
966 //bprint("Shuffle from team 3\n");
967 ShufflePlayerOutOfTeam(3);
969 if(c4 && c4 > average)
971 bprint("Rebalancing Teams\n");
972 //bprint("Shuffle from team 4\n");
973 ShufflePlayerOutOfTeam(4);
976 // if teams are still unbalanced, balance them further in the next audit,
977 // which will happen sooner (keep doing rapid audits until things are in order)
978 audit_teams_time = time + 0.7 + random()*0.3;
983 /*void(entity e, float first) UpdateTeamScore =
988 WriteByte (MSG_ALL, SVC_UPDATENAME);
989 WriteByte (MSG_ALL, clientno);
990 WriteString (MSG_ALL, e.netname);
992 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
993 WriteByte (MSG_ALL, clientno);
994 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
997 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
998 WriteByte (MSG_ALL, clientno);
999 WriteShort (MSG_ALL, e.frags + 10000);
1010 self.classname = "tdm_team";
1011 self.team = self.cnt + 1;
1014 // code from here on is just to support maps that don't have team entities
1015 void tdm_spawnteam (string teamname, float teamcolor)
1017 local entity oldself;
1020 self.classname = "tdm_team";
1021 self.netname = teamname;
1022 self.cnt = teamcolor;
1029 // spawn some default teams if the map is not set up for tdm
1030 void() tdm_spawnteams =
1034 numteams = cvar("g_tdm_teams");
1036 tdm_spawnteam("Red", 4);
1037 tdm_spawnteam("Blue", 13);
1040 void() tdm_delayedinit =
1042 self.think = SUB_Remove;
1043 self.nextthink = time;
1044 // if no teams are found, spawn defaults
1045 if (find(world, classname, "tdm_team") == world)
1053 e.think = tdm_delayedinit;
1054 e.nextthink = time + 0.1;