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\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",
279 if(cvar("g_grappling_hook"))
280 grap_msg = strzone("\n\nBind a key to +hook to use the grappling hook\n");
282 s = strcat("Match type is ", gamemode_name, "\n");
286 "\n\n\nWelcome, ", pl.netname, "\n",
289 temp = strzone(temp);
294 "You are on ", TeamName(pl.team), "\n\n",
295 "Go to Menu->Options->Player to\nchange your name, model & team\n",
302 "Go to Menu->Options->Player to change your name & model\n",
315 void SetPlayerColors(entity pl, float color)
319 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
321 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
322 pl.clientcolors = 16*cl + cl;*/
324 setcolor(pl, 16*color + color);
327 void SetPlayerTeam(entity pl, float t, float s, float noprint)
331 color = COLOR_TEAM4 - 1;
333 color = COLOR_TEAM3 - 1;
335 color = COLOR_TEAM2 - 1;
337 color = COLOR_TEAM1 - 1;
338 setcolor(pl, 16*color + color);
340 if(!noprint && t != s)
342 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
343 bprint(strcat(pl.netname, " has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
352 // set c1...c4 to show what teams are allowed
353 void CheckAllowedTeams ()
362 c1 = c2 = c3 = c4 = -1;
363 cb1 = cb2 = cb3 = cb4 = 0;
366 teament_name = "dom_team";
368 teament_name = "ctf_team";
370 teament_name = "tdm_team";
373 // cover anything else by treating it like tdm with no teams spawned
374 dm = cvar("g_tdm_teams");
376 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
380 c1 = c2 = c3 = c4 = 0;
393 // first find out what teams are allowed
394 head = find(world, classname, teament_name);
397 if(!(g_domination && head.netname == ""))
399 if(head.team == COLOR_TEAM1)
403 if(head.team == COLOR_TEAM2)
407 if(head.team == COLOR_TEAM3)
411 if(head.team == COLOR_TEAM4)
416 head = find(head, classname, teament_name);
420 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
421 // teams that are allowed will now have their player counts stored in c1...c4
422 void GetTeamCounts(entity ignore)
425 // now count how many players are on each team already
427 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
428 // also remember the lowest-scoring player
430 head = find(world, classname, "player");
433 if(head != ignore)// && head.netname != "")
435 if(head.team == COLOR_TEAM1)
443 if(head.team == COLOR_TEAM2)
451 if(head.team == COLOR_TEAM3)
459 if(head.team == COLOR_TEAM4)
468 head = find(head, classname, "player");
472 // returns # of smallest team (1, 2, 3, 4)
473 // NOTE: Assumes CheckAllowedTeams has already been called!
474 float FindSmallestTeam(entity pl, float ignore_pl)
476 float totalteams, smallestteam, smallestteam_count, balance_type;
479 // find out what teams are available
480 //CheckAllowedTeams();
482 // make sure there are at least 2 teams to join
484 totalteams = totalteams + 1;
486 totalteams = totalteams + 1;
488 totalteams = totalteams + 1;
490 totalteams = totalteams + 1;
495 error("Too few teams available for domination\n");
497 error("Too few teams available for ctf\n");
499 error("Too few teams available for team deathmatch\n");
503 // count how many players are in each team
505 GetTeamCounts(world);
511 // c1...c4 now have counts of each team
512 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
515 smallestteam_count = 999;
517 // 2 gives priority to what team you're already on, 1 goes in order
518 // 2 doesn't seem to work though...
521 if(balance_type == 1)
523 if(c1 >= 0 && c1 < smallestteam_count)
526 smallestteam_count = c1;
528 if(c2 >= 0 && c2 < smallestteam_count)
531 smallestteam_count = c2;
533 if(c3 >= 0 && c3 < smallestteam_count)
536 smallestteam_count = c3;
538 if(c4 >= 0 && c4 < smallestteam_count)
541 smallestteam_count = c4;
546 if(c1 >= 0 && (c1 < smallestteam_count ||
547 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
550 smallestteam_count = c1;
552 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
553 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
556 smallestteam_count = c2;
558 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
559 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
562 smallestteam_count = c3;
564 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
565 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
568 smallestteam_count = c4;
575 float JoinBestTeam(entity pl, float only_return_best)
577 float smallest, selectedteam;
579 g_domination = cvar("g_domination");
580 g_ctf = cvar("g_ctf");
582 // don't join a team if we're not playing a team game
583 if(!cvar("teamplay") && !g_domination && !g_ctf)
586 // find out what teams are available
589 // if we don't care what team he ends up on, put him on whatever team he entered as.
590 // if he's not on a valid team, then let other code put him on the smallest team
591 if(!cvar("balance_teams") && !cvar("force_balance"))
593 if( c1 >= 0 && pl.team == COLOR_TEAM1)
594 selectedteam = pl.team;
595 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
596 selectedteam = pl.team;
597 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
598 selectedteam = pl.team;
599 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
600 selectedteam = pl.team;
605 if(!only_return_best)
606 SetPlayerColors(pl, selectedteam - 1);
609 // otherwise end up on the smallest team (handled below)
612 smallest = FindSmallestTeam(pl, TRUE);
615 if(!only_return_best)
619 SetPlayerColors(pl, COLOR_TEAM1 - 1);
621 else if(smallest == 2)
623 SetPlayerColors(pl, COLOR_TEAM2 - 1);
625 else if(smallest == 3)
627 SetPlayerColors(pl, COLOR_TEAM3 - 1);
629 else if(smallest == 4)
631 SetPlayerColors(pl, COLOR_TEAM4 - 1);
634 error("smallest team: invalid team\n");
641 void SV_ChangeTeam(float color)
643 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
645 scolor = self.clientcolors & 15;
648 if(scolor == COLOR_TEAM1 - 1)
650 else if(scolor == COLOR_TEAM2 - 1)
652 else if(scolor == COLOR_TEAM3 - 1)
654 else if(scolor == COLOR_TEAM4 - 1)
656 if(dcolor == COLOR_TEAM1 - 1)
658 else if(dcolor == COLOR_TEAM2 - 1)
660 else if(dcolor == COLOR_TEAM3 - 1)
662 else if(dcolor == COLOR_TEAM4 - 1)
665 // not changing teams
668 //setcolor(self, 16*color + color);
669 //bprint("same team change\n");
670 SetPlayerTeam(self, dteam, steam, TRUE);
676 if(cvar("g_changeteam_banned"))
678 sprint(self, "Team changes not allowed\n");
679 return; // changing teams is not allowed
682 if(cvar("g_balance_teams_prevent_imbalance"))
684 // only allow changing to a smaller or equal size team
686 // find out what teams are available
688 // count how many players on each team
689 GetTeamCounts(world);
692 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
697 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
702 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
707 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
714 sprint(self, "Cannot change to an invalid team\n");
720 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
722 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
724 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
726 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
729 if(scount) // started at a valid, nonempty team
731 // check if we're trying to change to a larger team that doens't have bots to swap with
732 if(dcount >= scount && dbotcount <= 0)
734 sprint(self, "Cannot change to a larger team\n");
735 return; // can't change to a larger team
741 // reduce frags during a team change
742 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
744 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
746 SetPlayerTeam(self, dteam, steam, FALSE);
747 //setcolor(self, 16*dcolor + dcolor);
751 void ShufflePlayerOutOfTeam (float source_team)
753 float smallestteam, smallestteam_count, steam;
754 float lowest_bot_score, lowest_player_score;
755 entity head, lowest_bot, lowest_player, selected;
758 smallestteam_count = 999;
760 if(c1 >= 0 && c1 < smallestteam_count)
763 smallestteam_count = c1;
765 if(c2 >= 0 && c2 < smallestteam_count)
768 smallestteam_count = c2;
770 if(c3 >= 0 && c3 < smallestteam_count)
773 smallestteam_count = c3;
775 if(c4 >= 0 && c4 < smallestteam_count)
778 smallestteam_count = c4;
783 bprint("warning: no smallest team\n");
789 else if(source_team == 2)
791 else if(source_team == 3)
793 else if(source_team == 4)
797 lowest_bot_score = 9999;
798 lowest_player = world;
799 lowest_player_score = 9999;
801 // find the lowest-scoring player & bot of that team
802 head = find(world, classname, "player");
805 if(head.team == steam)
809 if(head.frags < lowest_bot_score)
812 lowest_bot_score = head.frags;
817 if(head.frags < lowest_player_score)
819 lowest_player = head;
820 lowest_player_score = head.frags;
824 head = find(head, classname, "player");
827 // prefers to move a bot...
828 if(lowest_bot != world)
829 selected = lowest_bot;
830 // but it will move a player if it has to
832 selected = lowest_player;
833 // don't do anything if it couldn't find anyone
836 bprint("warning: couldn't find a player to move from team\n");
840 // smallest team gains a member
841 if(smallestteam == 1)
845 else if(smallestteam == 2)
849 else if(smallestteam == 3)
853 else if(smallestteam == 4)
859 bprint("warning: destination team invalid\n");
862 // source team loses a member
867 else if(source_team == 2)
871 else if(source_team == 3)
875 else if(source_team == 4)
881 bprint("warning: source team invalid\n");
885 // move the player to the new team
886 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
889 // part of g_balance_teams_force
890 // occasionally perform an audit of the teams to make
891 // sure they're more or less balanced in player count.
894 float numplayers, numteams, average;
895 if(!cvar("g_balance_teams_force"))
897 if(!cvar("teamplay"))
900 if(audit_teams_time > time)
903 audit_teams_time = time + 4 + random();
905 // bprint("Auditing teams\n");
908 GetTeamCounts(world);
911 numteams = numplayers = 0;
914 numteams = numteams + 1;
915 numplayers = numplayers + c1;
919 numteams = numteams + 1;
920 numplayers = numplayers + c2;
924 numteams = numteams + 1;
925 numplayers = numplayers + c3;
929 numteams = numteams + 1;
930 numplayers = numplayers + c4;
934 return; // no players to move around
936 return; // don't bother shuffling if for some reason there aren't any teams
938 average = ceil(numplayers / numteams);
941 return; // that's weird...
943 if(c1 && c1 > average)
945 bprint("Rebalancing Teams\n");
946 //bprint("Shuffle from team 1\n");
947 ShufflePlayerOutOfTeam(1);
949 if(c2 && c2 > average)
951 bprint("Rebalancing Teams\n");
952 //bprint("Shuffle from team 2\n");
953 ShufflePlayerOutOfTeam(2);
955 if(c3 && c3 > average)
957 bprint("Rebalancing Teams\n");
958 //bprint("Shuffle from team 3\n");
959 ShufflePlayerOutOfTeam(3);
961 if(c4 && c4 > average)
963 bprint("Rebalancing Teams\n");
964 //bprint("Shuffle from team 4\n");
965 ShufflePlayerOutOfTeam(4);
968 // if teams are still unbalanced, balance them further in the next audit,
969 // which will happen sooner (keep doing rapid audits until things are in order)
970 audit_teams_time = time + 0.7 + random()*0.3;
975 /*void(entity e, float first) UpdateTeamScore =
980 WriteByte (MSG_ALL, SVC_UPDATENAME);
981 WriteByte (MSG_ALL, clientno);
982 WriteString (MSG_ALL, e.netname);
984 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
985 WriteByte (MSG_ALL, clientno);
986 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
989 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
990 WriteByte (MSG_ALL, clientno);
991 WriteShort (MSG_ALL, e.frags + 10000);
1002 self.classname = "tdm_team";
1003 self.team = self.cnt + 1;
1006 // code from here on is just to support maps that don't have team entities
1007 void tdm_spawnteam (string teamname, float teamcolor)
1009 local entity oldself;
1012 self.classname = "tdm_team";
1013 self.netname = teamname;
1014 self.cnt = teamcolor;
1021 // spawn some default teams if the map is not set up for tdm
1022 void() tdm_spawnteams =
1026 numteams = cvar("g_tdm_teams");
1028 tdm_spawnteam("Red", 4);
1029 tdm_spawnteam("Blue", 13);
1032 void() tdm_delayedinit =
1034 self.think = SUB_Remove;
1035 self.nextthink = time;
1036 // if no teams are found, spawn defaults
1037 if (find(world, classname, "tdm_team") == world)
1045 e.think = tdm_delayedinit;
1046 e.nextthink = time + 0.1;