1 float COLOR_TEAM1 = 5; // red
2 float COLOR_TEAM2 = 14; // blue
3 float COLOR_TEAM3 = 10; // pink
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 // client counts for each team
17 // # of bots on those teams
18 float cb1, cb2, cb3, cb4;
21 float g_domination, g_ctf, g_tdm;
23 float audit_teams_time;
26 string TeamName(float t)
28 // fixme: Search for team entities and get their .netname's!
37 return "Neutral Team";
39 string ColoredTeamName(float t)
41 // fixme: Search for team entities and get their .netname's!
43 return "^1Red Team^7";
45 return "^4Blue Team^7";
47 return "^6Pink Team^7";
49 return "^3Yellow Team^7";
50 return "Neutral Team";
52 string TeamNoName(float t)
54 // fixme: Search for team entities and get their .netname's!
63 return "Neutral Team";
68 void runematch_init();
74 cvar_set("g_tdm", "0");
75 cvar_set("g_domination", "0");
76 cvar_set("g_ctf", "0");
77 cvar_set("g_runematch", "0");
78 cvar_set("g_lms", "0");
79 cvar_set("teamplay", "0");
82 cvar_set("exit_cfg", "");
85 void ActivateTeamplay()
87 float teamplay_default;
88 teamplay_default = cvar("teamplay_default");
91 cvar_set("teamplay", ftos(teamplay_default));
93 cvar_set("teamplay", "3");
99 void InitGameplayMode()
101 float fraglimit_override, timelimit_override;
103 game = cvar ("gamecfg"); // load game options
105 // game cvars get reset before map changes
106 // then map's cfg sets them as desired
108 // FIXME: also set a message or game mode name to print to players when the join
110 // set both here, gamemode can override it later
111 timelimit_override = cvar("timelimit_override");
112 fraglimit_override = cvar("fraglimit_override");
114 if(game == GAME_DOMINATION || cvar("g_domination"))
116 game = GAME_DOMINATION;
117 cvar_set("g_domination", "1");
121 fraglimit_override = cvar("g_domination_point_limit");
123 gamemode_name = "Domination";
126 else if(game == GAME_CTF || cvar("g_ctf"))
129 cvar_set("g_ctf", "1");
133 fraglimit_override = cvar("g_ctf_capture_limit");
135 gamemode_name = "Capture the Flag";
138 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
140 game = GAME_RUNEMATCH;
141 cvar_set("g_runematch", "1");
143 if(cvar("deathmatch_force_teamplay"))
146 fraglimit_override = cvar("g_runematch_point_limit");
148 gamemode_name = "Rune Match";
154 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
156 if(!cvar("deathmatch"))
157 cvar_set("deathmatch", "1");
160 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
162 game = GAME_TEAM_DEATHMATCH;
163 gamemode_name = "Team Deathmatch";
169 game = GAME_DEATHMATCH;
170 gamemode_name = "Deathmatch";
174 fraglimit_override = cvar("fraglimit_override");
176 else if(game == GAME_LMS || cvar("g_lms"))
179 cvar_set("g_lms", "1");
180 fraglimit_override = cvar("fraglimit_override");
181 gamemode_name = "Last Man Standing";
183 cvar_set("teamplay", "0");
184 lms_lowest_lives = 999;
188 // we can only assume...
189 gamemode_name = "Deathmatch";
192 /* else if(game == GAME_TEAM_DEATHMATCH)
194 if(!cvar("deathmatch"))
195 cvar_set("deathmatch", "1");
197 //if(!cvar("teamplay"))
198 // cvar_set("teamplay", "3");
201 fraglimit_override = cvar("fraglimit_override");
204 // enforce the server's universal frag/time limits
205 if(fraglimit_override >= 0)
206 cvar_set("fraglimit", ftos(fraglimit_override));
207 if(timelimit_override >= 0)
208 cvar_set("timelimit", ftos(timelimit_override));
210 if (game == GAME_DOMINATION)//cvar("g_domination"))
212 else if (game == GAME_CTF)//cvar("g_ctf"))
214 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
216 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
219 // those mutators rule each other out
220 if(cvar("g_minstagib"))
222 cvar_set("g_instagib", "0");
223 cvar_set("g_rocketarena", "0");
225 if(cvar("g_instagib"))
227 cvar_set("g_minstagib", "0");
228 cvar_set("g_rocketarena", "0");
230 if(cvar("g_rocketarena"))
232 cvar_set("g_instagib", "0");
233 cvar_set("g_minstagib", "0");
237 void PrintWelcomeMessage(entity pl)
239 string s, grap_msg, temp, mutator;
241 /*if(self.welcomemessage_time < time)
243 if(self.welcomemessage_time2 > time)
245 self.welcomemessage_time2 = time + 0.8; */
247 if(self.welcomemessage_time2 > time) return;
248 self.welcomemessage_time2 = time + 1.0;
250 if(cvar("g_minstagib"))
251 mutator = "^2Minstagib ^1";
252 else if(cvar("g_instagib"))
253 mutator = "^2Instagib ^1";
254 else if(cvar("g_rocketarena"))
255 mutator = "^2Rocketarena ^1";
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(s, "\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", self.versionmessage);
263 s = strcat(s, "^8\n\nMatch type is ^1", mutator, gamemode_name, "^8\n");
265 if(self.classname == "observer" || self.classname == "spectator") {
266 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
273 "\n\n\n^8Welcome, ", pl.netname, "^8\n",
276 temp = strzone(temp);
279 if (cvar_string("g_mutatormsg") != "") {
280 s = strcat(s, "\n\nSpecial gameplay tips: ", cvar_string("g_mutatormsg"));
283 if (cvar_string("sv_motd") != "") {
284 s = strcat(s, "\n\nMOTD: ", cvar_string("sv_motd"));
294 void SetPlayerColors(entity pl, float _color)
298 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
300 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
301 pl.clientcolors = 16*cl + cl;*/
304 setcolor(pl, 16*_color + _color);
307 shirt = pl.clientcolors & 0xF0;
308 setcolor(pl, shirt + _color);
313 void SetPlayerTeam(entity pl, float t, float s, float noprint)
317 // remap invalid teams in dom & ctf
318 if(cvar("g_ctf") && t == 3)
320 else if(cvar("g_ctf") && t == 4)
322 else if(cvar("g_domination") && cvar("g_domination_default_teams") < 3)
329 else if(cvar("g_domination") && cvar("g_domination_default_teams") < 4)
336 _color = COLOR_TEAM4 - 1;
338 _color = COLOR_TEAM3 - 1;
340 _color = COLOR_TEAM2 - 1;
342 _color = COLOR_TEAM1 - 1;
344 // kill player when changing teams
345 if(teams_matter && pl.classname == "player" && pl.deadflag == DEAD_NO && pl.team != (_color + 1))
346 Damage(pl, pl, pl, 100000, DEATH_KILL, pl.origin, '0 0 0');
348 SetPlayerColors(pl,_color);
350 if(!noprint && t != s)
352 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
353 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
362 // set c1...c4 to show what teams are allowed
363 void CheckAllowedTeams ()
372 c1 = c2 = c3 = c4 = -1;
373 cb1 = cb2 = cb3 = cb4 = 0;
376 teament_name = "dom_team";
378 teament_name = "ctf_team";
380 teament_name = "tdm_team";
383 // cover anything else by treating it like tdm with no teams spawned
384 dm = cvar("g_tdm_teams");
386 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
390 c1 = c2 = c3 = c4 = 0;
403 // first find out what teams are allowed
404 head = find(world, classname, teament_name);
407 if(!(g_domination && head.netname == ""))
409 if(head.team == COLOR_TEAM1)
413 if(head.team == COLOR_TEAM2)
417 if(head.team == COLOR_TEAM3)
421 if(head.team == COLOR_TEAM4)
426 head = find(head, classname, teament_name);
430 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
431 // teams that are allowed will now have their player counts stored in c1...c4
432 void GetTeamCounts(entity ignore)
435 // now count how many players are on each team already
437 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
438 // also remember the lowest-scoring player
440 head = find(world, classname, "player");
443 if(head != ignore)// && head.netname != "")
445 if(head.team == COLOR_TEAM1)
453 if(head.team == COLOR_TEAM2)
461 if(head.team == COLOR_TEAM3)
469 if(head.team == COLOR_TEAM4)
478 head = find(head, classname, "player");
482 // returns # of smallest team (1, 2, 3, 4)
483 // NOTE: Assumes CheckAllowedTeams has already been called!
484 float FindSmallestTeam(entity pl, float ignore_pl)
486 float totalteams, smallestteam, smallestteam_count, balance_type;
489 // find out what teams are available
490 //CheckAllowedTeams();
492 // make sure there are at least 2 teams to join
494 totalteams = totalteams + 1;
496 totalteams = totalteams + 1;
498 totalteams = totalteams + 1;
500 totalteams = totalteams + 1;
505 error("Too few teams available for domination\n");
507 error("Too few teams available for ctf\n");
509 error("Too few teams available for team deathmatch\n");
513 // count how many players are in each team
515 GetTeamCounts(world);
521 // c1...c4 now have counts of each team
522 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
525 smallestteam_count = 999;
527 // 2 gives priority to what team you're already on, 1 goes in order
528 // 2 doesn't seem to work though...
531 if(balance_type == 1)
533 if(c1 >= 0 && c1 < smallestteam_count)
536 smallestteam_count = c1;
538 if(c2 >= 0 && c2 < smallestteam_count)
541 smallestteam_count = c2;
543 if(c3 >= 0 && c3 < smallestteam_count)
546 smallestteam_count = c3;
548 if(c4 >= 0 && c4 < smallestteam_count)
551 smallestteam_count = c4;
556 if(c1 >= 0 && (c1 < smallestteam_count ||
557 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
560 smallestteam_count = c1;
562 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
563 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
566 smallestteam_count = c2;
568 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
569 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
572 smallestteam_count = c3;
574 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
575 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
578 smallestteam_count = c4;
585 float JoinBestTeam(entity pl, float only_return_best)
587 float smallest, selectedteam;
589 g_domination = cvar("g_domination");
590 g_ctf = cvar("g_ctf");
592 // don't join a team if we're not playing a team game
593 if(!cvar("teamplay") && !g_domination && !g_ctf)
596 // find out what teams are available
599 // if we don't care what team he ends up on, put him on whatever team he entered as.
600 // if he's not on a valid team, then let other code put him on the smallest team
601 if(!cvar("balance_teams") && !cvar("force_balance"))
603 if( c1 >= 0 && pl.team == COLOR_TEAM1)
604 selectedteam = pl.team;
605 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
606 selectedteam = pl.team;
607 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
608 selectedteam = pl.team;
609 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
610 selectedteam = pl.team;
615 if(!only_return_best)
616 SetPlayerColors(pl, selectedteam - 1);
619 // otherwise end up on the smallest team (handled below)
622 smallest = FindSmallestTeam(pl, TRUE);
625 if(!only_return_best)
629 SetPlayerColors(pl, COLOR_TEAM1 - 1);
631 else if(smallest == 2)
633 SetPlayerColors(pl, COLOR_TEAM2 - 1);
635 else if(smallest == 3)
637 SetPlayerColors(pl, COLOR_TEAM3 - 1);
639 else if(smallest == 4)
641 SetPlayerColors(pl, COLOR_TEAM4 - 1);
645 error("smallest team: invalid team\n");
653 void SV_ChangeTeam(float _color)
655 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
657 scolor = self.clientcolors & 0x0F;
658 dcolor = _color & 0x0F;
660 // store shirt color in .clientcolors
661 // this will get overwritten in teamplay modes later
662 setcolor(self, _color & 0xF0 + scolor);
664 if(scolor == COLOR_TEAM1 - 1)
666 else if(scolor == COLOR_TEAM2 - 1)
668 else if(scolor == COLOR_TEAM3 - 1)
670 else if(scolor == COLOR_TEAM4 - 1)
672 if(dcolor == COLOR_TEAM1 - 1)
674 else if(dcolor == COLOR_TEAM2 - 1)
676 else if(dcolor == COLOR_TEAM3 - 1)
678 else if(dcolor == COLOR_TEAM4 - 1)
681 // not changing teams
684 //bprint("same team change\n");
685 SetPlayerTeam(self, dteam, steam, TRUE);
691 if(cvar("g_changeteam_banned"))
693 sprint(self, "Team changes not allowed\n");
694 return; // changing teams is not allowed
697 if(cvar("g_balance_teams_prevent_imbalance"))
699 // only allow changing to a smaller or equal size team
701 // find out what teams are available
703 // count how many players on each team
704 GetTeamCounts(world);
707 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
712 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
717 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
722 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
729 sprint(self, "Cannot change to an invalid team\n");
735 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
737 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
739 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
741 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
744 if(scount) // started at a valid, nonempty team
746 // check if we're trying to change to a larger team that doens't have bots to swap with
747 if(dcount >= scount && dbotcount <= 0)
749 sprint(self, "Cannot change to a larger team\n");
750 return; // can't change to a larger team
756 // reduce frags during a team change
757 if(teamplay && self.classname == "player" && self.team != (dcolor + 1))
758 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
760 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
762 SetPlayerTeam(self, dteam, steam, FALSE);
766 void ShufflePlayerOutOfTeam (float source_team)
768 float smallestteam, smallestteam_count, steam;
769 float lowest_bot_score, lowest_player_score;
770 entity head, lowest_bot, lowest_player, selected;
773 smallestteam_count = 999;
775 if(c1 >= 0 && c1 < smallestteam_count)
778 smallestteam_count = c1;
780 if(c2 >= 0 && c2 < smallestteam_count)
783 smallestteam_count = c2;
785 if(c3 >= 0 && c3 < smallestteam_count)
788 smallestteam_count = c3;
790 if(c4 >= 0 && c4 < smallestteam_count)
793 smallestteam_count = c4;
798 bprint("warning: no smallest team\n");
804 else if(source_team == 2)
806 else if(source_team == 3)
808 else if(source_team == 4)
812 lowest_bot_score = 9999;
813 lowest_player = world;
814 lowest_player_score = 9999;
816 // find the lowest-scoring player & bot of that team
817 head = find(world, classname, "player");
820 if(head.team == steam)
824 if(head.frags < lowest_bot_score)
827 lowest_bot_score = head.frags;
832 if(head.frags < lowest_player_score)
834 lowest_player = head;
835 lowest_player_score = head.frags;
839 head = find(head, classname, "player");
842 // prefers to move a bot...
843 if(lowest_bot != world)
844 selected = lowest_bot;
845 // but it will move a player if it has to
847 selected = lowest_player;
848 // don't do anything if it couldn't find anyone
851 bprint("warning: couldn't find a player to move from team\n");
855 // smallest team gains a member
856 if(smallestteam == 1)
860 else if(smallestteam == 2)
864 else if(smallestteam == 3)
868 else if(smallestteam == 4)
874 bprint("warning: destination team invalid\n");
877 // source team loses a member
882 else if(source_team == 2)
886 else if(source_team == 3)
890 else if(source_team == 4)
896 bprint("warning: source team invalid\n");
900 // move the player to the new team
901 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
904 // part of g_balance_teams_force
905 // occasionally perform an audit of the teams to make
906 // sure they're more or less balanced in player count.
909 float numplayers, numteams, average;
910 if(!cvar("g_balance_teams_force"))
912 if(!cvar("teamplay"))
915 if(audit_teams_time > time)
918 audit_teams_time = time + 4 + random();
920 // bprint("Auditing teams\n");
923 GetTeamCounts(world);
926 numteams = numplayers = 0;
929 numteams = numteams + 1;
930 numplayers = numplayers + c1;
934 numteams = numteams + 1;
935 numplayers = numplayers + c2;
939 numteams = numteams + 1;
940 numplayers = numplayers + c3;
944 numteams = numteams + 1;
945 numplayers = numplayers + c4;
949 return; // no players to move around
951 return; // don't bother shuffling if for some reason there aren't any teams
953 average = ceil(numplayers / numteams);
956 return; // that's weird...
958 if(c1 && c1 > average)
960 bprint("Rebalancing Teams\n");
961 //bprint("Shuffle from team 1\n");
962 ShufflePlayerOutOfTeam(1);
964 if(c2 && c2 > average)
966 bprint("Rebalancing Teams\n");
967 //bprint("Shuffle from team 2\n");
968 ShufflePlayerOutOfTeam(2);
970 if(c3 && c3 > average)
972 bprint("Rebalancing Teams\n");
973 //bprint("Shuffle from team 3\n");
974 ShufflePlayerOutOfTeam(3);
976 if(c4 && c4 > average)
978 bprint("Rebalancing Teams\n");
979 //bprint("Shuffle from team 4\n");
980 ShufflePlayerOutOfTeam(4);
983 // if teams are still unbalanced, balance them further in the next audit,
984 // which will happen sooner (keep doing rapid audits until things are in order)
985 audit_teams_time = time + 0.7 + random()*0.3;
990 /*void(entity e, float first) UpdateTeamScore =
995 WriteByte (MSG_ALL, SVC_UPDATENAME);
996 WriteByte (MSG_ALL, clientno);
997 WriteString (MSG_ALL, e.netname);
999 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1000 WriteByte (MSG_ALL, clientno);
1001 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1004 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1005 WriteByte (MSG_ALL, clientno);
1006 WriteShort (MSG_ALL, e.frags + 10000);
1017 self.classname = "tdm_team";
1018 self.team = self.cnt + 1;
1021 // code from here on is just to support maps that don't have team entities
1022 void tdm_spawnteam (string teamname, float teamcolor)
1024 local entity oldself;
1027 self.classname = "tdm_team";
1028 self.netname = teamname;
1029 self.cnt = teamcolor;
1036 // spawn some default teams if the map is not set up for tdm
1037 void() tdm_spawnteams =
1041 numteams = cvar("g_tdm_teams");
1043 tdm_spawnteam("Red", 4);
1044 tdm_spawnteam("Blue", 13);
1047 void() tdm_delayedinit =
1049 self.think = SUB_Remove;
1050 self.nextthink = time;
1051 // if no teams are found, spawn defaults
1052 if (find(world, classname, "tdm_team") == world)
1060 e.think = tdm_delayedinit;
1061 e.nextthink = time + 0.1;