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")) && !cvar("g_minstagib"))
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));
233 if (cvar("g_minstagib")) {
234 cvar_set("g_pickup_items", ftos(FALSE));
235 cvar_set("g_use_ammunition", ftos(TRUE));
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 if(cvar("g_grappling_hook"))
258 grap_msg = strzone("\n\nBind a key to ^1+hook^8 to use the grappling hook\n");
260 s = strcat("\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", self.versionmessage, "^8\n\nMatch type is ^1", temp2, "^8\n");
264 "\n\n\n^8Welcome, ", pl.netname, "\n",
267 temp = strzone(temp);
272 "You are on ", ColoredTeamName(pl.team), "^8\n\n",
273 "Go to ^1Menu->Options->Player^8 to change your name, model & team\n",
280 "Go to ^1Menu->Options->Player^8 to change your name & model\n",
285 if (cvar_string("g_mutatormsg") != "") {
286 s = strcat(s, "\n\nSpecial gameplay tips: ", cvar_string("g_mutatormsg"));
289 if (cvar_string("sv_motd") != "") {
290 s = strcat(s, "\n\nMOTD: ", cvar_string("sv_motd"));
295 if(cvar("g_minstagib"))
296 temp2 = strcat("Minstagib ", gamemode_name);
298 if(cvar("g_grappling_hook"))
299 grap_msg = strzone("\n\nBind a key to +hook to use the grappling hook\n");
301 s = strcat("Match type is ", temp2, "\n");
305 "\n\n\nWelcome, ", pl.netname, "\n",
308 temp = strzone(temp);
313 "You are on ", TeamName(pl.team), "\n\n",
314 "Go to Menu->Options->Player to\nchange your name, model & team\n",
321 "Go to Menu->Options->Player to change your name & model\n",
334 void SetPlayerColors(entity pl, float color)
338 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
340 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
341 pl.clientcolors = 16*cl + cl;*/
343 setcolor(pl, 16*color + color);
346 void SetPlayerTeam(entity pl, float t, float s, float noprint)
350 color = COLOR_TEAM4 - 1;
352 color = COLOR_TEAM3 - 1;
354 color = COLOR_TEAM2 - 1;
356 color = COLOR_TEAM1 - 1;
357 setcolor(pl, 16*color + color);
359 if(!noprint && t != s)
361 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
362 bprint(strcat(pl.netname, " has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
371 // set c1...c4 to show what teams are allowed
372 void CheckAllowedTeams ()
381 c1 = c2 = c3 = c4 = -1;
382 cb1 = cb2 = cb3 = cb4 = 0;
385 teament_name = "dom_team";
387 teament_name = "ctf_team";
389 teament_name = "tdm_team";
392 // cover anything else by treating it like tdm with no teams spawned
393 dm = cvar("g_tdm_teams");
395 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
399 c1 = c2 = c3 = c4 = 0;
412 // first find out what teams are allowed
413 head = find(world, classname, teament_name);
416 if(!(g_domination && head.netname == ""))
418 if(head.team == COLOR_TEAM1)
422 if(head.team == COLOR_TEAM2)
426 if(head.team == COLOR_TEAM3)
430 if(head.team == COLOR_TEAM4)
435 head = find(head, classname, teament_name);
439 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
440 // teams that are allowed will now have their player counts stored in c1...c4
441 void GetTeamCounts(entity ignore)
444 // now count how many players are on each team already
446 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
447 // also remember the lowest-scoring player
449 head = find(world, classname, "player");
452 if(head != ignore)// && head.netname != "")
454 if(head.team == COLOR_TEAM1)
462 if(head.team == COLOR_TEAM2)
470 if(head.team == COLOR_TEAM3)
478 if(head.team == COLOR_TEAM4)
487 head = find(head, classname, "player");
491 // returns # of smallest team (1, 2, 3, 4)
492 // NOTE: Assumes CheckAllowedTeams has already been called!
493 float FindSmallestTeam(entity pl, float ignore_pl)
495 float totalteams, smallestteam, smallestteam_count, balance_type;
498 // find out what teams are available
499 //CheckAllowedTeams();
501 // make sure there are at least 2 teams to join
503 totalteams = totalteams + 1;
505 totalteams = totalteams + 1;
507 totalteams = totalteams + 1;
509 totalteams = totalteams + 1;
514 error("Too few teams available for domination\n");
516 error("Too few teams available for ctf\n");
518 error("Too few teams available for team deathmatch\n");
522 // count how many players are in each team
524 GetTeamCounts(world);
530 // c1...c4 now have counts of each team
531 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
534 smallestteam_count = 999;
536 // 2 gives priority to what team you're already on, 1 goes in order
537 // 2 doesn't seem to work though...
540 if(balance_type == 1)
542 if(c1 >= 0 && c1 < smallestteam_count)
545 smallestteam_count = c1;
547 if(c2 >= 0 && c2 < smallestteam_count)
550 smallestteam_count = c2;
552 if(c3 >= 0 && c3 < smallestteam_count)
555 smallestteam_count = c3;
557 if(c4 >= 0 && c4 < smallestteam_count)
560 smallestteam_count = c4;
565 if(c1 >= 0 && (c1 < smallestteam_count ||
566 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
569 smallestteam_count = c1;
571 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
572 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
575 smallestteam_count = c2;
577 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
578 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
581 smallestteam_count = c3;
583 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
584 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
587 smallestteam_count = c4;
594 float JoinBestTeam(entity pl, float only_return_best)
596 float smallest, selectedteam;
598 g_domination = cvar("g_domination");
599 g_ctf = cvar("g_ctf");
601 // don't join a team if we're not playing a team game
602 if(!cvar("teamplay") && !g_domination && !g_ctf)
605 // find out what teams are available
608 // if we don't care what team he ends up on, put him on whatever team he entered as.
609 // if he's not on a valid team, then let other code put him on the smallest team
610 if(!cvar("balance_teams") && !cvar("force_balance"))
612 if( c1 >= 0 && pl.team == COLOR_TEAM1)
613 selectedteam = pl.team;
614 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
615 selectedteam = pl.team;
616 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
617 selectedteam = pl.team;
618 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
619 selectedteam = pl.team;
624 if(!only_return_best)
625 SetPlayerColors(pl, selectedteam - 1);
628 // otherwise end up on the smallest team (handled below)
631 smallest = FindSmallestTeam(pl, TRUE);
634 if(!only_return_best)
638 SetPlayerColors(pl, COLOR_TEAM1 - 1);
640 else if(smallest == 2)
642 SetPlayerColors(pl, COLOR_TEAM2 - 1);
644 else if(smallest == 3)
646 SetPlayerColors(pl, COLOR_TEAM3 - 1);
648 else if(smallest == 4)
650 SetPlayerColors(pl, COLOR_TEAM4 - 1);
653 error("smallest team: invalid team\n");
660 void SV_ChangeTeam(float color)
662 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
664 scolor = self.clientcolors & 15;
667 if(scolor == COLOR_TEAM1 - 1)
669 else if(scolor == COLOR_TEAM2 - 1)
671 else if(scolor == COLOR_TEAM3 - 1)
673 else if(scolor == COLOR_TEAM4 - 1)
675 if(dcolor == COLOR_TEAM1 - 1)
677 else if(dcolor == COLOR_TEAM2 - 1)
679 else if(dcolor == COLOR_TEAM3 - 1)
681 else if(dcolor == COLOR_TEAM4 - 1)
684 // not changing teams
687 //setcolor(self, 16*color + color);
688 //bprint("same team change\n");
689 SetPlayerTeam(self, dteam, steam, TRUE);
695 if(cvar("g_changeteam_banned"))
697 sprint(self, "Team changes not allowed\n");
698 return; // changing teams is not allowed
701 if(cvar("g_balance_teams_prevent_imbalance"))
703 // only allow changing to a smaller or equal size team
705 // find out what teams are available
707 // count how many players on each team
708 GetTeamCounts(world);
711 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
716 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
721 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
726 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
733 sprint(self, "Cannot change to an invalid team\n");
739 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
741 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
743 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
745 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
748 if(scount) // started at a valid, nonempty team
750 // check if we're trying to change to a larger team that doens't have bots to swap with
751 if(dcount >= scount && dbotcount <= 0)
753 sprint(self, "Cannot change to a larger team\n");
754 return; // can't change to a larger team
760 // reduce frags during a team change
761 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
763 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
765 SetPlayerTeam(self, dteam, steam, FALSE);
766 //setcolor(self, 16*dcolor + dcolor);
770 void ShufflePlayerOutOfTeam (float source_team)
772 float smallestteam, smallestteam_count, steam;
773 float lowest_bot_score, lowest_player_score;
774 entity head, lowest_bot, lowest_player, selected;
777 smallestteam_count = 999;
779 if(c1 >= 0 && c1 < smallestteam_count)
782 smallestteam_count = c1;
784 if(c2 >= 0 && c2 < smallestteam_count)
787 smallestteam_count = c2;
789 if(c3 >= 0 && c3 < smallestteam_count)
792 smallestteam_count = c3;
794 if(c4 >= 0 && c4 < smallestteam_count)
797 smallestteam_count = c4;
802 bprint("warning: no smallest team\n");
808 else if(source_team == 2)
810 else if(source_team == 3)
812 else if(source_team == 4)
816 lowest_bot_score = 9999;
817 lowest_player = world;
818 lowest_player_score = 9999;
820 // find the lowest-scoring player & bot of that team
821 head = find(world, classname, "player");
824 if(head.team == steam)
828 if(head.frags < lowest_bot_score)
831 lowest_bot_score = head.frags;
836 if(head.frags < lowest_player_score)
838 lowest_player = head;
839 lowest_player_score = head.frags;
843 head = find(head, classname, "player");
846 // prefers to move a bot...
847 if(lowest_bot != world)
848 selected = lowest_bot;
849 // but it will move a player if it has to
851 selected = lowest_player;
852 // don't do anything if it couldn't find anyone
855 bprint("warning: couldn't find a player to move from team\n");
859 // smallest team gains a member
860 if(smallestteam == 1)
864 else if(smallestteam == 2)
868 else if(smallestteam == 3)
872 else if(smallestteam == 4)
878 bprint("warning: destination team invalid\n");
881 // source team loses a member
886 else if(source_team == 2)
890 else if(source_team == 3)
894 else if(source_team == 4)
900 bprint("warning: source team invalid\n");
904 // move the player to the new team
905 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
908 // part of g_balance_teams_force
909 // occasionally perform an audit of the teams to make
910 // sure they're more or less balanced in player count.
913 float numplayers, numteams, average;
914 if(!cvar("g_balance_teams_force"))
916 if(!cvar("teamplay"))
919 if(audit_teams_time > time)
922 audit_teams_time = time + 4 + random();
924 // bprint("Auditing teams\n");
927 GetTeamCounts(world);
930 numteams = numplayers = 0;
933 numteams = numteams + 1;
934 numplayers = numplayers + c1;
938 numteams = numteams + 1;
939 numplayers = numplayers + c2;
943 numteams = numteams + 1;
944 numplayers = numplayers + c3;
948 numteams = numteams + 1;
949 numplayers = numplayers + c4;
953 return; // no players to move around
955 return; // don't bother shuffling if for some reason there aren't any teams
957 average = ceil(numplayers / numteams);
960 return; // that's weird...
962 if(c1 && c1 > average)
964 bprint("Rebalancing Teams\n");
965 //bprint("Shuffle from team 1\n");
966 ShufflePlayerOutOfTeam(1);
968 if(c2 && c2 > average)
970 bprint("Rebalancing Teams\n");
971 //bprint("Shuffle from team 2\n");
972 ShufflePlayerOutOfTeam(2);
974 if(c3 && c3 > average)
976 bprint("Rebalancing Teams\n");
977 //bprint("Shuffle from team 3\n");
978 ShufflePlayerOutOfTeam(3);
980 if(c4 && c4 > average)
982 bprint("Rebalancing Teams\n");
983 //bprint("Shuffle from team 4\n");
984 ShufflePlayerOutOfTeam(4);
987 // if teams are still unbalanced, balance them further in the next audit,
988 // which will happen sooner (keep doing rapid audits until things are in order)
989 audit_teams_time = time + 0.7 + random()*0.3;
994 /*void(entity e, float first) UpdateTeamScore =
999 WriteByte (MSG_ALL, SVC_UPDATENAME);
1000 WriteByte (MSG_ALL, clientno);
1001 WriteString (MSG_ALL, e.netname);
1003 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1004 WriteByte (MSG_ALL, clientno);
1005 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1008 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1009 WriteByte (MSG_ALL, clientno);
1010 WriteShort (MSG_ALL, e.frags + 10000);
1021 self.classname = "tdm_team";
1022 self.team = self.cnt + 1;
1025 // code from here on is just to support maps that don't have team entities
1026 void tdm_spawnteam (string teamname, float teamcolor)
1028 local entity oldself;
1031 self.classname = "tdm_team";
1032 self.netname = teamname;
1033 self.cnt = teamcolor;
1040 // spawn some default teams if the map is not set up for tdm
1041 void() tdm_spawnteams =
1045 numteams = cvar("g_tdm_teams");
1047 tdm_spawnteam("Red", 4);
1048 tdm_spawnteam("Blue", 13);
1051 void() tdm_delayedinit =
1053 self.think = SUB_Remove;
1054 self.nextthink = time;
1055 // if no teams are found, spawn defaults
1056 if (find(world, classname, "tdm_team") == world)
1064 e.think = tdm_delayedinit;
1065 e.nextthink = time + 0.1;