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;
15 // FIXME: get rid of this and use proper parameters instead
18 // client counts for each team
20 // # of bots on those teams
21 float cb1, cb2, cb3, cb4;
24 float g_domination, g_ctf, g_tdm;
26 float audit_teams_time;
29 string TeamName(float t)
31 // fixme: Search for team entities and get their .netname's!
40 return "Neutral Team";
42 string ColoredTeamName(float t)
44 // fixme: Search for team entities and get their .netname's!
46 return "^1Red Team^7";
48 return "^4Blue Team^7";
50 return "^2Green Team^7";
52 return "^3Yellow Team^7";
53 return "Neutral Team";
55 string TeamNoName(float t)
57 // fixme: Search for team entities and get their .netname's!
66 return "Neutral Team";
71 void runematch_init();
77 cvar_set("g_tdm", "0");
78 cvar_set("g_domination", "0");
79 cvar_set("g_ctf", "0");
80 cvar_set("g_runematch", "0");
81 cvar_set("g_lms", "0");
82 cvar_set("teamplay", "0");
85 cvar_set("exit_cfg", "");
88 void ActivateTeamplay()
90 float teamplay_default;
91 teamplay_default = cvar("teamplay_default");
94 cvar_set("teamplay", ftos(teamplay_default));
96 cvar_set("teamplay", "3");
102 void InitGameplayMode()
104 float fraglimit_override, timelimit_override;
106 game = cvar ("gamecfg"); // load game options
108 // game cvars get reset before map changes
109 // then map's cfg sets them as desired
111 // FIXME: also set a message or game mode name to print to players when the join
113 // set both here, gamemode can override it later
114 timelimit_override = cvar("timelimit_override");
115 fraglimit_override = cvar("fraglimit_override");
117 if(game == GAME_DOMINATION || cvar("g_domination"))
119 game = GAME_DOMINATION;
120 cvar_set("g_domination", "1");
124 fraglimit_override = cvar("g_domination_point_limit");
126 gamemode_name = "Domination";
129 else if(game == GAME_CTF || cvar("g_ctf"))
132 cvar_set("g_ctf", "1");
136 fraglimit_override = cvar("g_ctf_capture_limit");
138 gamemode_name = "Capture the Flag";
141 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
143 game = GAME_RUNEMATCH;
144 cvar_set("g_runematch", "1");
146 if(cvar("deathmatch_force_teamplay"))
149 fraglimit_override = cvar("g_runematch_point_limit");
151 gamemode_name = "Rune Match";
157 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
159 if(!cvar("deathmatch"))
160 cvar_set("deathmatch", "1");
163 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
165 game = GAME_TEAM_DEATHMATCH;
166 gamemode_name = "Team Deathmatch";
172 game = GAME_DEATHMATCH;
173 gamemode_name = "Deathmatch";
177 fraglimit_override = cvar("fraglimit_override");
179 else if(game == GAME_LMS || cvar("g_lms"))
182 cvar_set("g_lms", "1");
183 fraglimit_override = cvar("fraglimit_override");
184 gamemode_name = "Last Man Standing";
186 lms_lowest_lives = 999;
190 // we can only assume...
191 gamemode_name = "Deathmatch";
194 /* else if(game == GAME_TEAM_DEATHMATCH)
196 if(!cvar("deathmatch"))
197 cvar_set("deathmatch", "1");
199 //if(!cvar("teamplay"))
200 // cvar_set("teamplay", "3");
203 fraglimit_override = cvar("fraglimit_override");
206 // enforce the server's universal frag/time limits
207 if(fraglimit_override >= 0)
208 cvar_set("fraglimit", ftos(fraglimit_override));
209 if(timelimit_override >= 0)
210 cvar_set("timelimit", ftos(timelimit_override));
212 if (game == GAME_DOMINATION)//cvar("g_domination"))
214 else if (game == GAME_CTF)//cvar("g_ctf"))
216 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
218 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
221 // those mutators rule each other out
222 if(cvar("g_minstagib"))
224 cvar_set("g_instagib", "0");
225 cvar_set("g_rocketarena", "0");
227 if(cvar("g_instagib"))
229 cvar_set("g_minstagib", "0");
230 cvar_set("g_rocketarena", "0");
232 if(cvar("g_rocketarena"))
234 cvar_set("g_instagib", "0");
235 cvar_set("g_minstagib", "0");
239 void PrintWelcomeMessage(entity pl)
241 string s, grap_msg, temp, temp2;
244 if(self.welcomemessage_time < time)
246 if(self.welcomemessage_time2 > time)
248 self.welcomemessage_time2 = time + 0.8;
254 if(cvar("g_minstagib"))
255 temp2 = strcat("^2Minstagib ^1", gamemode_name);
257 temp2 = gamemode_name;
259 if(cvar("g_grappling_hook"))
260 grap_msg = strzone("\n\nBind a key to ^1+hook^8 to use the grappling hook\n");
262 s = strcat("\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", self.versionmessage, "^8\n\nMatch type is ^1", temp2, "^8\n");
266 "\n\n\n^8Welcome, ", pl.netname, "^8\n",
269 temp = strzone(temp);
274 "You are on ", ColoredTeamName(pl.team), "^8\n\n",
275 "Go to ^1Menu->Options->Player^8 to change your name, model & team\n",
282 "Go to ^1Menu->Options->Player^8 to change your name & model\n",
287 if (cvar_string("g_mutatormsg") != "") {
288 s = strcat(s, "\n\nSpecial gameplay tips: ", cvar_string("g_mutatormsg"));
291 if (cvar_string("sv_motd") != "") {
292 s = strcat(s, "\n\nMOTD: ", cvar_string("sv_motd"));
297 if(cvar("g_minstagib"))
298 temp2 = strcat("Minstagib ", gamemode_name);
300 if(cvar("g_grappling_hook"))
301 grap_msg = strzone("\n\nBind a key to +hook to use the grappling hook\n");
303 s = strcat("Match type is ", temp2, "\n");
307 "\n\n\nWelcome, ", pl.netname, "\n",
310 temp = strzone(temp);
315 "You are on ", TeamName(pl.team), "\n\n",
316 "Go to Menu->Options->Player to\nchange your name, model & team\n",
323 "Go to Menu->Options->Player to change your name & model\n",
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 setcolor(pl, 16*color + color);
349 //shirt = pl.clientcolors & 0xF0;
350 setcolor(pl, shirtcolor * 16 + color);
351 /*bprint("CLIENTCOLORS");
352 bprint(ftos(pl.clientcolors));
357 void SetPlayerTeam(entity pl, float t, float s, float noprint)
363 color = COLOR_TEAM4 - 1;
365 color = COLOR_TEAM3 - 1;
367 color = COLOR_TEAM2 - 1;
369 color = COLOR_TEAM1 - 1;
372 SetPlayerColors(pl,color);
375 if(!noprint && t != s)
377 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
378 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
387 // set c1...c4 to show what teams are allowed
388 void CheckAllowedTeams ()
397 c1 = c2 = c3 = c4 = -1;
398 cb1 = cb2 = cb3 = cb4 = 0;
401 teament_name = "dom_team";
403 teament_name = "ctf_team";
405 teament_name = "tdm_team";
408 // cover anything else by treating it like tdm with no teams spawned
409 dm = cvar("g_tdm_teams");
411 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
415 c1 = c2 = c3 = c4 = 0;
428 // first find out what teams are allowed
429 head = find(world, classname, teament_name);
432 if(!(g_domination && head.netname == ""))
434 if(head.team == COLOR_TEAM1)
438 if(head.team == COLOR_TEAM2)
442 if(head.team == COLOR_TEAM3)
446 if(head.team == COLOR_TEAM4)
451 head = find(head, classname, teament_name);
455 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
456 // teams that are allowed will now have their player counts stored in c1...c4
457 void GetTeamCounts(entity ignore)
460 // now count how many players are on each team already
462 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
463 // also remember the lowest-scoring player
465 head = find(world, classname, "player");
468 if(head != ignore)// && head.netname != "")
470 if(head.team == COLOR_TEAM1)
478 if(head.team == COLOR_TEAM2)
486 if(head.team == COLOR_TEAM3)
494 if(head.team == COLOR_TEAM4)
503 head = find(head, classname, "player");
507 // returns # of smallest team (1, 2, 3, 4)
508 // NOTE: Assumes CheckAllowedTeams has already been called!
509 float FindSmallestTeam(entity pl, float ignore_pl)
511 float totalteams, smallestteam, smallestteam_count, balance_type;
514 // find out what teams are available
515 //CheckAllowedTeams();
517 // make sure there are at least 2 teams to join
519 totalteams = totalteams + 1;
521 totalteams = totalteams + 1;
523 totalteams = totalteams + 1;
525 totalteams = totalteams + 1;
530 error("Too few teams available for domination\n");
532 error("Too few teams available for ctf\n");
534 error("Too few teams available for team deathmatch\n");
538 // count how many players are in each team
540 GetTeamCounts(world);
546 // c1...c4 now have counts of each team
547 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
550 smallestteam_count = 999;
552 // 2 gives priority to what team you're already on, 1 goes in order
553 // 2 doesn't seem to work though...
556 if(balance_type == 1)
558 if(c1 >= 0 && c1 < smallestteam_count)
561 smallestteam_count = c1;
563 if(c2 >= 0 && c2 < smallestteam_count)
566 smallestteam_count = c2;
568 if(c3 >= 0 && c3 < smallestteam_count)
571 smallestteam_count = c3;
573 if(c4 >= 0 && c4 < smallestteam_count)
576 smallestteam_count = c4;
581 if(c1 >= 0 && (c1 < smallestteam_count ||
582 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
585 smallestteam_count = c1;
587 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
588 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
591 smallestteam_count = c2;
593 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
594 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
597 smallestteam_count = c3;
599 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
600 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
603 smallestteam_count = c4;
610 float JoinBestTeam(entity pl, float only_return_best)
612 float smallest, selectedteam;
614 g_domination = cvar("g_domination");
615 g_ctf = cvar("g_ctf");
617 // don't join a team if we're not playing a team game
618 if(!cvar("teamplay") && !g_domination && !g_ctf)
621 // find out what teams are available
624 // if we don't care what team he ends up on, put him on whatever team he entered as.
625 // if he's not on a valid team, then let other code put him on the smallest team
626 if(!cvar("balance_teams") && !cvar("force_balance"))
628 if( c1 >= 0 && pl.team == COLOR_TEAM1)
629 selectedteam = pl.team;
630 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
631 selectedteam = pl.team;
632 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
633 selectedteam = pl.team;
634 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
635 selectedteam = pl.team;
640 if(!only_return_best)
641 SetPlayerColors(pl, selectedteam - 1);
644 // otherwise end up on the smallest team (handled below)
647 smallest = FindSmallestTeam(pl, TRUE);
650 if(!only_return_best)
654 SetPlayerColors(pl, COLOR_TEAM1 - 1);
656 else if(smallest == 2)
658 SetPlayerColors(pl, COLOR_TEAM2 - 1);
660 else if(smallest == 3)
662 SetPlayerColors(pl, COLOR_TEAM3 - 1);
664 else if(smallest == 4)
666 SetPlayerColors(pl, COLOR_TEAM4 - 1);
669 error("smallest team: invalid team\n");
676 void SV_ChangeTeam(float color)
678 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
680 shirtcolor = (color & 240)/16;
682 scolor = self.clientcolors & 15;
685 if(scolor == COLOR_TEAM1 - 1)
687 else if(scolor == COLOR_TEAM2 - 1)
689 else if(scolor == COLOR_TEAM3 - 1)
691 else if(scolor == COLOR_TEAM4 - 1)
693 if(dcolor == COLOR_TEAM1 - 1)
695 else if(dcolor == COLOR_TEAM2 - 1)
697 else if(dcolor == COLOR_TEAM3 - 1)
699 else if(dcolor == COLOR_TEAM4 - 1)
702 // not changing teams
705 //bprint("same team change\n");
706 SetPlayerTeam(self, dteam, steam, TRUE);
712 if(cvar("g_changeteam_banned"))
714 sprint(self, "Team changes not allowed\n");
715 return; // changing teams is not allowed
718 if(cvar("g_balance_teams_prevent_imbalance"))
720 // only allow changing to a smaller or equal size team
722 // find out what teams are available
724 // count how many players on each team
725 GetTeamCounts(world);
728 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
733 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
738 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
743 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
750 sprint(self, "Cannot change to an invalid team\n");
756 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
758 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
760 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
762 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
765 if(scount) // started at a valid, nonempty team
767 // check if we're trying to change to a larger team that doens't have bots to swap with
768 if(dcount >= scount && dbotcount <= 0)
770 sprint(self, "Cannot change to a larger team\n");
771 return; // can't change to a larger team
777 // reduce frags during a team change
778 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
780 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
782 SetPlayerTeam(self, dteam, steam, FALSE);
786 void ShufflePlayerOutOfTeam (float source_team)
788 float smallestteam, smallestteam_count, steam;
789 float lowest_bot_score, lowest_player_score;
790 entity head, lowest_bot, lowest_player, selected;
793 smallestteam_count = 999;
795 if(c1 >= 0 && c1 < smallestteam_count)
798 smallestteam_count = c1;
800 if(c2 >= 0 && c2 < smallestteam_count)
803 smallestteam_count = c2;
805 if(c3 >= 0 && c3 < smallestteam_count)
808 smallestteam_count = c3;
810 if(c4 >= 0 && c4 < smallestteam_count)
813 smallestteam_count = c4;
818 bprint("warning: no smallest team\n");
824 else if(source_team == 2)
826 else if(source_team == 3)
828 else if(source_team == 4)
832 lowest_bot_score = 9999;
833 lowest_player = world;
834 lowest_player_score = 9999;
836 // find the lowest-scoring player & bot of that team
837 head = find(world, classname, "player");
840 if(head.team == steam)
844 if(head.frags < lowest_bot_score)
847 lowest_bot_score = head.frags;
852 if(head.frags < lowest_player_score)
854 lowest_player = head;
855 lowest_player_score = head.frags;
859 head = find(head, classname, "player");
862 // prefers to move a bot...
863 if(lowest_bot != world)
864 selected = lowest_bot;
865 // but it will move a player if it has to
867 selected = lowest_player;
868 // don't do anything if it couldn't find anyone
871 bprint("warning: couldn't find a player to move from team\n");
875 // smallest team gains a member
876 if(smallestteam == 1)
880 else if(smallestteam == 2)
884 else if(smallestteam == 3)
888 else if(smallestteam == 4)
894 bprint("warning: destination team invalid\n");
897 // source team loses a member
902 else if(source_team == 2)
906 else if(source_team == 3)
910 else if(source_team == 4)
916 bprint("warning: source team invalid\n");
920 // move the player to the new team
921 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
924 // part of g_balance_teams_force
925 // occasionally perform an audit of the teams to make
926 // sure they're more or less balanced in player count.
929 float numplayers, numteams, average;
930 if(!cvar("g_balance_teams_force"))
932 if(!cvar("teamplay"))
935 if(audit_teams_time > time)
938 audit_teams_time = time + 4 + random();
940 // bprint("Auditing teams\n");
943 GetTeamCounts(world);
946 numteams = numplayers = 0;
949 numteams = numteams + 1;
950 numplayers = numplayers + c1;
954 numteams = numteams + 1;
955 numplayers = numplayers + c2;
959 numteams = numteams + 1;
960 numplayers = numplayers + c3;
964 numteams = numteams + 1;
965 numplayers = numplayers + c4;
969 return; // no players to move around
971 return; // don't bother shuffling if for some reason there aren't any teams
973 average = ceil(numplayers / numteams);
976 return; // that's weird...
978 if(c1 && c1 > average)
980 bprint("Rebalancing Teams\n");
981 //bprint("Shuffle from team 1\n");
982 ShufflePlayerOutOfTeam(1);
984 if(c2 && c2 > average)
986 bprint("Rebalancing Teams\n");
987 //bprint("Shuffle from team 2\n");
988 ShufflePlayerOutOfTeam(2);
990 if(c3 && c3 > average)
992 bprint("Rebalancing Teams\n");
993 //bprint("Shuffle from team 3\n");
994 ShufflePlayerOutOfTeam(3);
996 if(c4 && c4 > average)
998 bprint("Rebalancing Teams\n");
999 //bprint("Shuffle from team 4\n");
1000 ShufflePlayerOutOfTeam(4);
1003 // if teams are still unbalanced, balance them further in the next audit,
1004 // which will happen sooner (keep doing rapid audits until things are in order)
1005 audit_teams_time = time + 0.7 + random()*0.3;
1010 /*void(entity e, float first) UpdateTeamScore =
1015 WriteByte (MSG_ALL, SVC_UPDATENAME);
1016 WriteByte (MSG_ALL, clientno);
1017 WriteString (MSG_ALL, e.netname);
1019 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1020 WriteByte (MSG_ALL, clientno);
1021 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1024 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1025 WriteByte (MSG_ALL, clientno);
1026 WriteShort (MSG_ALL, e.frags + 10000);
1037 self.classname = "tdm_team";
1038 self.team = self.cnt + 1;
1041 // code from here on is just to support maps that don't have team entities
1042 void tdm_spawnteam (string teamname, float teamcolor)
1044 local entity oldself;
1047 self.classname = "tdm_team";
1048 self.netname = teamname;
1049 self.cnt = teamcolor;
1056 // spawn some default teams if the map is not set up for tdm
1057 void() tdm_spawnteams =
1061 numteams = cvar("g_tdm_teams");
1063 tdm_spawnteam("Red", 4);
1064 tdm_spawnteam("Blue", 13);
1067 void() tdm_delayedinit =
1069 self.think = SUB_Remove;
1070 self.nextthink = time;
1071 // if no teams are found, spawn defaults
1072 if (find(world, classname, "tdm_team") == world)
1080 e.think = tdm_delayedinit;
1081 e.nextthink = time + 0.1;