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 timelimit_override = cvar("timelimit_override");
109 if(game == GAME_DOMINATION || cvar("g_domination"))
111 game = GAME_DOMINATION;
112 cvar_set("g_domination", "1");
116 fraglimit_override = cvar("g_domination_point_limit");
118 gamemode_name = "Domination";
121 else if(game == GAME_CTF || cvar("g_ctf"))
124 cvar_set("g_ctf", "1");
128 fraglimit_override = cvar("g_ctf_capture_limit");
130 gamemode_name = "Capture the Flag";
133 else if(game == GAME_RUNEMATCH || cvar("g_runematch"))
135 game = GAME_RUNEMATCH;
136 cvar_set("g_runematch", "1");
138 if(cvar("deathmatch_force_teamplay"))
141 fraglimit_override = cvar("g_runematch_point_limit");
143 gamemode_name = "Rune Match";
149 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
151 if(!cvar("deathmatch"))
152 cvar_set("deathmatch", "1");
155 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
157 game = GAME_TEAM_DEATHMATCH;
158 gamemode_name = "Team Deathmatch";
164 game = GAME_DEATHMATCH;
165 gamemode_name = "Deathmatch";
169 fraglimit_override = cvar("fraglimit_override");
173 // we can only assume...
174 gamemode_name = "Deathmatch";
177 /* else if(game == GAME_TEAM_DEATHMATCH)
179 if(!cvar("deathmatch"))
180 cvar_set("deathmatch", "1");
182 //if(!cvar("teamplay"))
183 // cvar_set("teamplay", "3");
186 fraglimit_override = cvar("fraglimit_override");
189 // enforce the server's universal frag/time limits
190 if(fraglimit_override)
191 cvar_set("fraglimit", ftos(fraglimit_override));
192 if(timelimit_override)
193 cvar_set("timelimit", ftos(timelimit_override));
195 if (game == GAME_DOMINATION)//cvar("g_domination"))
197 else if (game == GAME_CTF)//cvar("g_ctf"))
199 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
201 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
205 void PrintWelcomeMessage(entity pl)
207 string s, grap_msg, temp;
210 if(self.welcomemessage_time < time)
212 if(self.welcomemessage_time2 > time)
214 self.welcomemessage_time2 = time + 0.8;
219 if(cvar("g_grappling_hook"))
220 grap_msg = strzone("\n\nBind a key to ^1+hook^8 to use the grappling hook\n");
222 s = strcat("Match type is ^1", gamemode_name, "^8\n");
226 "\n\n\n^8Welcome, ", pl.netname, "\n",
229 temp = strzone(temp);
234 "You are on ", ColoredTeamName(pl.team), "^8\n\n",
235 "Go to ^1Menu->Options->Player^8 to change your name, model & team\n",
242 "Go to ^1Menu->Options->Player^8 to change your name & model\n",
249 if(cvar("g_grappling_hook"))
250 grap_msg = strzone("\n\nBind a key to +hook to use the grappling hook\n");
252 s = strcat("Match type is ", gamemode_name, "\n");
256 "\n\n\nWelcome, ", pl.netname, "\n",
259 temp = strzone(temp);
264 "You are on ", TeamName(pl.team), "\n\n",
265 "Go to Menu->Options->Player to\nchange your name, model & team\n",
272 "Go to Menu->Options->Player to change your name & model\n",
285 void SetPlayerColors(entity pl, float color)
289 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
291 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
292 pl.clientcolors = 16*cl + cl;*/
294 setcolor(pl, 16*color + color);
297 void SetPlayerTeam(entity pl, float t, float s, float noprint)
301 color = COLOR_TEAM4 - 1;
303 color = COLOR_TEAM3 - 1;
305 color = COLOR_TEAM2 - 1;
307 color = COLOR_TEAM1 - 1;
308 setcolor(pl, 16*color + color);
310 if(!noprint && t != s)
312 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
313 bprint(strcat(pl.netname, " has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
322 // set c1...c4 to show what teams are allowed
323 void CheckAllowedTeams ()
332 c1 = c2 = c3 = c4 = -1;
333 cb1 = cb2 = cb3 = cb4 = 0;
336 teament_name = "dom_team";
338 teament_name = "ctf_team";
340 teament_name = "tdm_team";
343 // cover anything else by treating it like tdm with no teams spawned
344 dm = cvar("g_tdm_teams");
346 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
350 c1 = c2 = c3 = c4 = 0;
363 // first find out what teams are allowed
364 head = find(world, classname, teament_name);
367 if(!(g_domination && head.netname == ""))
369 if(head.team == COLOR_TEAM1)
373 if(head.team == COLOR_TEAM2)
377 if(head.team == COLOR_TEAM3)
381 if(head.team == COLOR_TEAM4)
386 head = find(head, classname, teament_name);
390 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
391 // teams that are allowed will now have their player counts stored in c1...c4
392 void GetTeamCounts(entity ignore)
395 // now count how many players are on each team already
397 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
398 // also remember the lowest-scoring player
400 head = find(world, classname, "player");
403 if(head != ignore)// && head.netname != "")
405 if(head.team == COLOR_TEAM1)
413 if(head.team == COLOR_TEAM2)
421 if(head.team == COLOR_TEAM3)
429 if(head.team == COLOR_TEAM4)
438 head = find(head, classname, "player");
442 // returns # of smallest team (1, 2, 3, 4)
443 // NOTE: Assumes CheckAllowedTeams has already been called!
444 float FindSmallestTeam(entity pl, float ignore_pl)
446 float totalteams, smallestteam, smallestteam_count, balance_type;
449 // find out what teams are available
450 //CheckAllowedTeams();
452 // make sure there are at least 2 teams to join
454 totalteams = totalteams + 1;
456 totalteams = totalteams + 1;
458 totalteams = totalteams + 1;
460 totalteams = totalteams + 1;
465 error("Too few teams available for domination\n");
467 error("Too few teams available for ctf\n");
469 error("Too few teams available for team deathmatch\n");
473 // count how many players are in each team
475 GetTeamCounts(world);
481 // c1...c4 now have counts of each team
482 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
485 smallestteam_count = 999;
487 // 2 gives priority to what team you're already on, 1 goes in order
488 // 2 doesn't seem to work though...
491 if(balance_type == 1)
493 if(c1 >= 0 && c1 < smallestteam_count)
496 smallestteam_count = c1;
498 if(c2 >= 0 && c2 < smallestteam_count)
501 smallestteam_count = c2;
503 if(c3 >= 0 && c3 < smallestteam_count)
506 smallestteam_count = c3;
508 if(c4 >= 0 && c4 < smallestteam_count)
511 smallestteam_count = c4;
516 if(c1 >= 0 && (c1 < smallestteam_count ||
517 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
520 smallestteam_count = c1;
522 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
523 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
526 smallestteam_count = c2;
528 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
529 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
532 smallestteam_count = c3;
534 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
535 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
538 smallestteam_count = c4;
545 float JoinBestTeam(entity pl, float only_return_best)
547 float smallest, selectedteam;
549 g_domination = cvar("g_domination");
550 g_ctf = cvar("g_ctf");
552 // don't join a team if we're not playing a team game
553 if(!cvar("teamplay") && !g_domination && !g_ctf)
556 // find out what teams are available
559 // if we don't care what team he ends up on, put him on whatever team he entered as.
560 // if he's not on a valid team, then let other code put him on the smallest team
561 if(!cvar("balance_teams") && !cvar("force_balance"))
563 if( c1 >= 0 && pl.team == COLOR_TEAM1)
564 selectedteam = pl.team;
565 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
566 selectedteam = pl.team;
567 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
568 selectedteam = pl.team;
569 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
570 selectedteam = pl.team;
575 if(!only_return_best)
576 SetPlayerColors(pl, selectedteam - 1);
579 // otherwise end up on the smallest team (handled below)
582 smallest = FindSmallestTeam(pl, TRUE);
585 if(!only_return_best)
589 SetPlayerColors(pl, COLOR_TEAM1 - 1);
591 else if(smallest == 2)
593 SetPlayerColors(pl, COLOR_TEAM2 - 1);
595 else if(smallest == 3)
597 SetPlayerColors(pl, COLOR_TEAM3 - 1);
599 else if(smallest == 4)
601 SetPlayerColors(pl, COLOR_TEAM4 - 1);
604 error("smallest team: invalid team\n");
611 void SV_ChangeTeam(float color)
613 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
615 scolor = self.clientcolors & 15;
618 if(scolor == COLOR_TEAM1 - 1)
620 else if(scolor == COLOR_TEAM2 - 1)
622 else if(scolor == COLOR_TEAM3 - 1)
624 else if(scolor == COLOR_TEAM4 - 1)
626 if(dcolor == COLOR_TEAM1 - 1)
628 else if(dcolor == COLOR_TEAM2 - 1)
630 else if(dcolor == COLOR_TEAM3 - 1)
632 else if(dcolor == COLOR_TEAM4 - 1)
635 // not changing teams
638 //setcolor(self, 16*color + color);
639 //bprint("same team change\n");
640 SetPlayerTeam(self, dteam, steam, TRUE);
646 if(cvar("g_changeteam_banned"))
648 sprint(self, "Team changes not allowed\n");
649 return; // changing teams is not allowed
652 if(cvar("g_balance_teams_prevent_imbalance"))
654 // only allow changing to a smaller or equal size team
656 // find out what teams are available
658 // count how many players on each team
659 GetTeamCounts(world);
662 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
667 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
672 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
677 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
684 sprint(self, "Cannot change to an invalid team\n");
690 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
692 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
694 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
696 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
699 if(scount) // started at a valid, nonempty team
701 // check if we're trying to change to a larger team that doens't have bots to swap with
702 if(dcount >= scount && dbotcount <= 0)
704 sprint(self, "Cannot change to a larger team\n");
705 return; // can't change to a larger team
711 // reduce frags during a team change
712 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
714 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
716 SetPlayerTeam(self, dteam, steam, FALSE);
717 //setcolor(self, 16*dcolor + dcolor);
721 void ShufflePlayerOutOfTeam (float source_team)
723 float smallestteam, smallestteam_count, steam;
724 float lowest_bot_score, lowest_player_score;
725 entity head, lowest_bot, lowest_player, selected;
728 smallestteam_count = 999;
730 if(c1 >= 0 && c1 < smallestteam_count)
733 smallestteam_count = c1;
735 if(c2 >= 0 && c2 < smallestteam_count)
738 smallestteam_count = c2;
740 if(c3 >= 0 && c3 < smallestteam_count)
743 smallestteam_count = c3;
745 if(c4 >= 0 && c4 < smallestteam_count)
748 smallestteam_count = c4;
753 bprint("warning: no smallest team\n");
759 else if(source_team == 2)
761 else if(source_team == 3)
763 else if(source_team == 4)
767 lowest_bot_score = 9999;
768 lowest_player = world;
769 lowest_player_score = 9999;
771 // find the lowest-scoring player & bot of that team
772 head = find(world, classname, "player");
775 if(head.team == steam)
779 if(head.frags < lowest_bot_score)
782 lowest_bot_score = head.frags;
787 if(head.frags < lowest_player_score)
789 lowest_player = head;
790 lowest_player_score = head.frags;
794 head = find(head, classname, "player");
797 // prefers to move a bot...
798 if(lowest_bot != world)
799 selected = lowest_bot;
800 // but it will move a player if it has to
802 selected = lowest_player;
803 // don't do anything if it couldn't find anyone
806 bprint("warning: couldn't find a player to move from team\n");
810 // smallest team gains a member
811 if(smallestteam == 1)
815 else if(smallestteam == 2)
819 else if(smallestteam == 3)
823 else if(smallestteam == 4)
829 bprint("warning: destination team invalid\n");
832 // source team loses a member
837 else if(source_team == 2)
841 else if(source_team == 3)
845 else if(source_team == 4)
851 bprint("warning: source team invalid\n");
855 // move the player to the new team
856 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
859 // part of g_balance_teams_force
860 // occasionally perform an audit of the teams to make
861 // sure they're more or less balanced in player count.
864 float numplayers, numteams, average;
865 if(!cvar("g_balance_teams_force"))
867 if(!cvar("teamplay"))
870 if(audit_teams_time > time)
873 audit_teams_time = time + 4 + random();
875 // bprint("Auditing teams\n");
878 GetTeamCounts(world);
881 numteams = numplayers = 0;
884 numteams = numteams + 1;
885 numplayers = numplayers + c1;
889 numteams = numteams + 1;
890 numplayers = numplayers + c2;
894 numteams = numteams + 1;
895 numplayers = numplayers + c3;
899 numteams = numteams + 1;
900 numplayers = numplayers + c4;
904 return; // no players to move around
906 return; // don't bother shuffling if for some reason there aren't any teams
908 average = ceil(numplayers / numteams);
911 return; // that's weird...
913 if(c1 && c1 > average)
915 bprint("Rebalancing Teams\n");
916 //bprint("Shuffle from team 1\n");
917 ShufflePlayerOutOfTeam(1);
919 if(c2 && c2 > average)
921 bprint("Rebalancing Teams\n");
922 //bprint("Shuffle from team 2\n");
923 ShufflePlayerOutOfTeam(2);
925 if(c3 && c3 > average)
927 bprint("Rebalancing Teams\n");
928 //bprint("Shuffle from team 3\n");
929 ShufflePlayerOutOfTeam(3);
931 if(c4 && c4 > average)
933 bprint("Rebalancing Teams\n");
934 //bprint("Shuffle from team 4\n");
935 ShufflePlayerOutOfTeam(4);
938 // if teams are still unbalanced, balance them further in the next audit,
939 // which will happen sooner (keep doing rapid audits until things are in order)
940 audit_teams_time = time + 0.7 + random()*0.3;
945 /*void(entity e, float first) UpdateTeamScore =
950 WriteByte (MSG_ALL, SVC_UPDATENAME);
951 WriteByte (MSG_ALL, clientno);
952 WriteString (MSG_ALL, e.netname);
954 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
955 WriteByte (MSG_ALL, clientno);
956 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
959 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
960 WriteByte (MSG_ALL, clientno);
961 WriteShort (MSG_ALL, e.frags + 10000);
972 self.classname = "tdm_team";
973 self.team = self.cnt + 1;
976 // code from here on is just to support maps that don't have team entities
977 void tdm_spawnteam (string teamname, float teamcolor)
979 local entity oldself;
982 self.classname = "tdm_team";
983 self.netname = teamname;
984 self.cnt = teamcolor;
991 // spawn some default teams if the map is not set up for tdm
992 void() tdm_spawnteams =
996 numteams = cvar("g_tdm_teams");
998 tdm_spawnteam("Red", 4);
999 tdm_spawnteam("Blue", 13);
1002 void() tdm_delayedinit =
1004 self.think = SUB_Remove;
1005 self.nextthink = time;
1006 // if no teams are found, spawn defaults
1007 if (find(world, classname, "tdm_team") == world)
1015 e.think = tdm_delayedinit;
1016 e.nextthink = time + 0.1;