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;
14 // client counts for each team
16 // # of bots on those teams
17 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 "^2Green 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 lms_lowest_lives = 999;
186 // we can only assume...
187 gamemode_name = "Deathmatch";
190 /* else if(game == GAME_TEAM_DEATHMATCH)
192 if(!cvar("deathmatch"))
193 cvar_set("deathmatch", "1");
195 //if(!cvar("teamplay"))
196 // cvar_set("teamplay", "3");
199 fraglimit_override = cvar("fraglimit_override");
202 // enforce the server's universal frag/time limits
203 if(fraglimit_override >= 0)
204 cvar_set("fraglimit", ftos(fraglimit_override));
205 if(timelimit_override >= 0)
206 cvar_set("timelimit", ftos(timelimit_override));
208 if (game == GAME_DOMINATION)//cvar("g_domination"))
210 else if (game == GAME_CTF)//cvar("g_ctf"))
212 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
214 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
217 // those mutators rule each other out
218 if(cvar("g_minstagib"))
220 cvar_set("g_instagib", "0");
221 cvar_set("g_rocketarena", "0");
223 if(cvar("g_instagib"))
225 cvar_set("g_minstagib", "0");
226 cvar_set("g_rocketarena", "0");
228 if(cvar("g_rocketarena"))
230 cvar_set("g_instagib", "0");
231 cvar_set("g_minstagib", "0");
235 void PrintWelcomeMessage(entity pl)
237 string s, grap_msg, temp, temp2;
240 if(self.welcomemessage_time < time)
242 if(self.welcomemessage_time2 > time)
244 self.welcomemessage_time2 = time + 0.8;
250 if(cvar("g_minstagib"))
251 temp2 = strcat("^2Minstagib ^1", gamemode_name);
253 temp2 = gamemode_name;
255 if(cvar("g_grappling_hook"))
256 grap_msg = strzone("\n\nBind a key to ^1+hook^8 to use the grappling hook\n");
258 s = strcat("\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", self.versionmessage, "^8\n\nMatch type is ^1", temp2, "^8\n");
262 "\n\n\n^8Welcome, ", pl.netname, "^8\n",
265 temp = strzone(temp);
270 "You are on ", ColoredTeamName(pl.team), "^8\n\n",
271 "Go to ^1Menu->Options->Player^8 to change your name, model & team\n",
278 "Go to ^1Menu->Options->Player^8 to change your name & model\n",
283 if (cvar_string("g_mutatormsg") != "") {
284 s = strcat(s, "\n\nSpecial gameplay tips: ", cvar_string("g_mutatormsg"));
287 if (cvar_string("sv_motd") != "") {
288 s = strcat(s, "\n\nMOTD: ", cvar_string("sv_motd"));
293 if(cvar("g_minstagib"))
294 temp2 = strcat("Minstagib ", gamemode_name);
296 if(cvar("g_grappling_hook"))
297 grap_msg = strzone("\n\nBind a key to +hook to use the grappling hook\n");
299 s = strcat("Match type is ", temp2, "\n");
303 "\n\n\nWelcome, ", pl.netname, "\n",
306 temp = strzone(temp);
311 "You are on ", TeamName(pl.team), "\n\n",
312 "Go to Menu->Options->Player to\nchange your name, model & team\n",
319 "Go to Menu->Options->Player to change your name & model\n",
332 void SetPlayerColors(entity pl, float color)
336 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
338 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
339 pl.clientcolors = 16*cl + cl;*/
342 setcolor(pl, 16*color + color);
345 shirt = (pl.clientcolors & 240) / 16;
346 setcolor(pl, 16*shirt + color);
350 void SetPlayerTeam(entity pl, float t, float s, float noprint)
356 color = COLOR_TEAM4 - 1;
358 color = COLOR_TEAM3 - 1;
360 color = COLOR_TEAM2 - 1;
362 color = COLOR_TEAM1 - 1;
365 SetPlayerColors(pl,color);
368 if(!noprint && t != s)
370 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
371 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
380 // set c1...c4 to show what teams are allowed
381 void CheckAllowedTeams ()
390 c1 = c2 = c3 = c4 = -1;
391 cb1 = cb2 = cb3 = cb4 = 0;
394 teament_name = "dom_team";
396 teament_name = "ctf_team";
398 teament_name = "tdm_team";
401 // cover anything else by treating it like tdm with no teams spawned
402 dm = cvar("g_tdm_teams");
404 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
408 c1 = c2 = c3 = c4 = 0;
421 // first find out what teams are allowed
422 head = find(world, classname, teament_name);
425 if(!(g_domination && head.netname == ""))
427 if(head.team == COLOR_TEAM1)
431 if(head.team == COLOR_TEAM2)
435 if(head.team == COLOR_TEAM3)
439 if(head.team == COLOR_TEAM4)
444 head = find(head, classname, teament_name);
448 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
449 // teams that are allowed will now have their player counts stored in c1...c4
450 void GetTeamCounts(entity ignore)
453 // now count how many players are on each team already
455 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
456 // also remember the lowest-scoring player
458 head = find(world, classname, "player");
461 if(head != ignore)// && head.netname != "")
463 if(head.team == COLOR_TEAM1)
471 if(head.team == COLOR_TEAM2)
479 if(head.team == COLOR_TEAM3)
487 if(head.team == COLOR_TEAM4)
496 head = find(head, classname, "player");
500 // returns # of smallest team (1, 2, 3, 4)
501 // NOTE: Assumes CheckAllowedTeams has already been called!
502 float FindSmallestTeam(entity pl, float ignore_pl)
504 float totalteams, smallestteam, smallestteam_count, balance_type;
507 // find out what teams are available
508 //CheckAllowedTeams();
510 // make sure there are at least 2 teams to join
512 totalteams = totalteams + 1;
514 totalteams = totalteams + 1;
516 totalteams = totalteams + 1;
518 totalteams = totalteams + 1;
523 error("Too few teams available for domination\n");
525 error("Too few teams available for ctf\n");
527 error("Too few teams available for team deathmatch\n");
531 // count how many players are in each team
533 GetTeamCounts(world);
539 // c1...c4 now have counts of each team
540 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
543 smallestteam_count = 999;
545 // 2 gives priority to what team you're already on, 1 goes in order
546 // 2 doesn't seem to work though...
549 if(balance_type == 1)
551 if(c1 >= 0 && c1 < smallestteam_count)
554 smallestteam_count = c1;
556 if(c2 >= 0 && c2 < smallestteam_count)
559 smallestteam_count = c2;
561 if(c3 >= 0 && c3 < smallestteam_count)
564 smallestteam_count = c3;
566 if(c4 >= 0 && c4 < smallestteam_count)
569 smallestteam_count = c4;
574 if(c1 >= 0 && (c1 < smallestteam_count ||
575 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
578 smallestteam_count = c1;
580 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
581 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
584 smallestteam_count = c2;
586 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
587 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
590 smallestteam_count = c3;
592 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
593 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
596 smallestteam_count = c4;
603 float JoinBestTeam(entity pl, float only_return_best)
605 float smallest, selectedteam;
607 g_domination = cvar("g_domination");
608 g_ctf = cvar("g_ctf");
610 // don't join a team if we're not playing a team game
611 if(!cvar("teamplay") && !g_domination && !g_ctf)
614 // find out what teams are available
617 // if we don't care what team he ends up on, put him on whatever team he entered as.
618 // if he's not on a valid team, then let other code put him on the smallest team
619 if(!cvar("balance_teams") && !cvar("force_balance"))
621 if( c1 >= 0 && pl.team == COLOR_TEAM1)
622 selectedteam = pl.team;
623 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
624 selectedteam = pl.team;
625 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
626 selectedteam = pl.team;
627 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
628 selectedteam = pl.team;
633 if(!only_return_best)
634 SetPlayerColors(pl, selectedteam - 1);
637 // otherwise end up on the smallest team (handled below)
640 smallest = FindSmallestTeam(pl, TRUE);
643 if(!only_return_best)
647 SetPlayerColors(pl, COLOR_TEAM1 - 1);
649 else if(smallest == 2)
651 SetPlayerColors(pl, COLOR_TEAM2 - 1);
653 else if(smallest == 3)
655 SetPlayerColors(pl, COLOR_TEAM3 - 1);
657 else if(smallest == 4)
659 SetPlayerColors(pl, COLOR_TEAM4 - 1);
662 error("smallest team: invalid team\n");
669 void SV_ChangeTeam(float color)
671 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
673 scolor = self.clientcolors & 15;
676 if(scolor == COLOR_TEAM1 - 1)
678 else if(scolor == COLOR_TEAM2 - 1)
680 else if(scolor == COLOR_TEAM3 - 1)
682 else if(scolor == COLOR_TEAM4 - 1)
684 if(dcolor == COLOR_TEAM1 - 1)
686 else if(dcolor == COLOR_TEAM2 - 1)
688 else if(dcolor == COLOR_TEAM3 - 1)
690 else if(dcolor == COLOR_TEAM4 - 1)
693 // not changing teams
696 //bprint("same team change\n");
697 SetPlayerTeam(self, dteam, steam, TRUE);
703 if(cvar("g_changeteam_banned"))
705 sprint(self, "Team changes not allowed\n");
706 return; // changing teams is not allowed
709 if(cvar("g_balance_teams_prevent_imbalance"))
711 // only allow changing to a smaller or equal size team
713 // find out what teams are available
715 // count how many players on each team
716 GetTeamCounts(world);
719 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
724 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
729 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
734 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
741 sprint(self, "Cannot change to an invalid team\n");
747 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
749 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
751 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
753 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
756 if(scount) // started at a valid, nonempty team
758 // check if we're trying to change to a larger team that doens't have bots to swap with
759 if(dcount >= scount && dbotcount <= 0)
761 sprint(self, "Cannot change to a larger team\n");
762 return; // can't change to a larger team
768 // reduce frags during a team change
769 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
771 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
773 SetPlayerTeam(self, dteam, steam, FALSE);
777 void ShufflePlayerOutOfTeam (float source_team)
779 float smallestteam, smallestteam_count, steam;
780 float lowest_bot_score, lowest_player_score;
781 entity head, lowest_bot, lowest_player, selected;
784 smallestteam_count = 999;
786 if(c1 >= 0 && c1 < smallestteam_count)
789 smallestteam_count = c1;
791 if(c2 >= 0 && c2 < smallestteam_count)
794 smallestteam_count = c2;
796 if(c3 >= 0 && c3 < smallestteam_count)
799 smallestteam_count = c3;
801 if(c4 >= 0 && c4 < smallestteam_count)
804 smallestteam_count = c4;
809 bprint("warning: no smallest team\n");
815 else if(source_team == 2)
817 else if(source_team == 3)
819 else if(source_team == 4)
823 lowest_bot_score = 9999;
824 lowest_player = world;
825 lowest_player_score = 9999;
827 // find the lowest-scoring player & bot of that team
828 head = find(world, classname, "player");
831 if(head.team == steam)
835 if(head.frags < lowest_bot_score)
838 lowest_bot_score = head.frags;
843 if(head.frags < lowest_player_score)
845 lowest_player = head;
846 lowest_player_score = head.frags;
850 head = find(head, classname, "player");
853 // prefers to move a bot...
854 if(lowest_bot != world)
855 selected = lowest_bot;
856 // but it will move a player if it has to
858 selected = lowest_player;
859 // don't do anything if it couldn't find anyone
862 bprint("warning: couldn't find a player to move from team\n");
866 // smallest team gains a member
867 if(smallestteam == 1)
871 else if(smallestteam == 2)
875 else if(smallestteam == 3)
879 else if(smallestteam == 4)
885 bprint("warning: destination team invalid\n");
888 // source team loses a member
893 else if(source_team == 2)
897 else if(source_team == 3)
901 else if(source_team == 4)
907 bprint("warning: source team invalid\n");
911 // move the player to the new team
912 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
915 // part of g_balance_teams_force
916 // occasionally perform an audit of the teams to make
917 // sure they're more or less balanced in player count.
920 float numplayers, numteams, average;
921 if(!cvar("g_balance_teams_force"))
923 if(!cvar("teamplay"))
926 if(audit_teams_time > time)
929 audit_teams_time = time + 4 + random();
931 // bprint("Auditing teams\n");
934 GetTeamCounts(world);
937 numteams = numplayers = 0;
940 numteams = numteams + 1;
941 numplayers = numplayers + c1;
945 numteams = numteams + 1;
946 numplayers = numplayers + c2;
950 numteams = numteams + 1;
951 numplayers = numplayers + c3;
955 numteams = numteams + 1;
956 numplayers = numplayers + c4;
960 return; // no players to move around
962 return; // don't bother shuffling if for some reason there aren't any teams
964 average = ceil(numplayers / numteams);
967 return; // that's weird...
969 if(c1 && c1 > average)
971 bprint("Rebalancing Teams\n");
972 //bprint("Shuffle from team 1\n");
973 ShufflePlayerOutOfTeam(1);
975 if(c2 && c2 > average)
977 bprint("Rebalancing Teams\n");
978 //bprint("Shuffle from team 2\n");
979 ShufflePlayerOutOfTeam(2);
981 if(c3 && c3 > average)
983 bprint("Rebalancing Teams\n");
984 //bprint("Shuffle from team 3\n");
985 ShufflePlayerOutOfTeam(3);
987 if(c4 && c4 > average)
989 bprint("Rebalancing Teams\n");
990 //bprint("Shuffle from team 4\n");
991 ShufflePlayerOutOfTeam(4);
994 // if teams are still unbalanced, balance them further in the next audit,
995 // which will happen sooner (keep doing rapid audits until things are in order)
996 audit_teams_time = time + 0.7 + random()*0.3;
1001 /*void(entity e, float first) UpdateTeamScore =
1006 WriteByte (MSG_ALL, SVC_UPDATENAME);
1007 WriteByte (MSG_ALL, clientno);
1008 WriteString (MSG_ALL, e.netname);
1010 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1011 WriteByte (MSG_ALL, clientno);
1012 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1015 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1016 WriteByte (MSG_ALL, clientno);
1017 WriteShort (MSG_ALL, e.frags + 10000);
1028 self.classname = "tdm_team";
1029 self.team = self.cnt + 1;
1032 // code from here on is just to support maps that don't have team entities
1033 void tdm_spawnteam (string teamname, float teamcolor)
1035 local entity oldself;
1038 self.classname = "tdm_team";
1039 self.netname = teamname;
1040 self.cnt = teamcolor;
1047 // spawn some default teams if the map is not set up for tdm
1048 void() tdm_spawnteams =
1052 numteams = cvar("g_tdm_teams");
1054 tdm_spawnteam("Red", 4);
1055 tdm_spawnteam("Blue", 13);
1058 void() tdm_delayedinit =
1060 self.think = SUB_Remove;
1061 self.nextthink = time;
1062 // if no teams are found, spawn defaults
1063 if (find(world, classname, "tdm_team") == world)
1071 e.think = tdm_delayedinit;
1072 e.nextthink = time + 0.1;