1 float COLOR_TEAM1 = 5; // red
2 float COLOR_TEAM2 = 14; // blue
3 float COLOR_TEAM3 = 4; // green
4 float COLOR_TEAM4 = 13; // yellow
6 #define COLOR_TEAM_RED COLOR_TEAM1
7 #define COLOR_TEAM_BLUE COLOR_TEAM2
8 #define COLOR_TEAM_GREEN COLOR_TEAM3
9 #define COLOR_TEAM_YELLOW COLOR_TEAM4
11 float GAME_DEATHMATCH = 1;
12 float GAME_TEAM_DEATHMATCH = 2;
13 float GAME_DOMINATION = 3;
15 float GAME_RUNEMATCH = 5;
19 // client counts for each team
21 // # of bots on those teams
22 float cb1, cb2, cb3, cb4;
25 float g_domination, g_ctf, g_tdm;
27 float audit_teams_time;
30 string TeamName(float t)
32 // fixme: Search for team entities and get their .netname's!
33 if(t == COLOR_TEAM_RED)
35 if(t == COLOR_TEAM_BLUE)
37 if(t == COLOR_TEAM_GREEN)
39 if(t == COLOR_TEAM_YELLOW)
41 return "Neutral Team";
43 string ColoredTeamName(float t)
45 // fixme: Search for team entities and get their .netname's!
46 if(t == COLOR_TEAM_RED)
47 return "^1Red Team^7";
48 if(t == COLOR_TEAM_BLUE)
49 return "^4Blue Team^7";
50 if(t == COLOR_TEAM_GREEN)
51 return "^2Green Team^7";
52 if(t == COLOR_TEAM_YELLOW)
53 return "^3Yellow Team^7";
54 return "Neutral Team";
56 string TeamNoName(float t)
58 // fixme: Search for team entities and get their .netname's!
67 return "Neutral Team";
72 void runematch_init();
78 cvar_set("g_tdm", "0");
79 cvar_set("g_domination", "0");
80 cvar_set("g_ctf", "0");
81 cvar_set("g_runematch", "0");
82 cvar_set("g_lms", "0");
83 cvar_set("teamplay", "0");
86 cvar_set("exit_cfg", "");
89 void ActivateTeamplay()
91 float teamplay_default;
92 teamplay_default = cvar("teamplay_default");
95 cvar_set("teamplay", ftos(teamplay_default));
97 cvar_set("teamplay", "3");
100 string gamemode_name;
103 void InitGameplayMode()
105 float fraglimit_override, timelimit_override;
107 game = cvar ("gamecfg"); // load game options
109 // game cvars get reset before map changes
110 // then map's cfg sets them as desired
112 // FIXME: also set a message or game mode name to print to players when the join
114 // set both here, gamemode can override it later
115 timelimit_override = cvar("timelimit_override");
116 fraglimit_override = cvar("fraglimit_override");
118 if(game == GAME_DOMINATION || cvar("g_domination"))
120 game = GAME_DOMINATION;
121 cvar_set("g_domination", "1");
125 fraglimit_override = cvar("g_domination_point_limit");
127 gamemode_name = "Domination";
130 else if(game == GAME_CTF || cvar("g_ctf"))
133 cvar_set("g_ctf", "1");
137 fraglimit_override = cvar("g_ctf_capture_limit");
139 gamemode_name = "Capture the Flag";
142 else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
144 game = GAME_RUNEMATCH;
145 cvar_set("g_runematch", "1");
147 if(cvar("deathmatch_force_teamplay"))
150 fraglimit_override = cvar("g_runematch_point_limit");
152 gamemode_name = "Rune Match";
158 else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
160 if(!cvar("deathmatch"))
161 cvar_set("deathmatch", "1");
164 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
166 game = GAME_TEAM_DEATHMATCH;
167 gamemode_name = "Team Deathmatch";
173 game = GAME_DEATHMATCH;
174 gamemode_name = "Deathmatch";
178 fraglimit_override = cvar("fraglimit_override");
180 else if(game == GAME_LMS || cvar("g_lms"))
183 cvar_set("g_lms", "1");
184 fraglimit_override = cvar("fraglimit_override");
185 gamemode_name = "Last Man Standing";
187 lms_lowest_lives = 999;
191 // we can only assume...
192 gamemode_name = "Deathmatch";
195 /* else if(game == GAME_TEAM_DEATHMATCH)
197 if(!cvar("deathmatch"))
198 cvar_set("deathmatch", "1");
200 //if(!cvar("teamplay"))
201 // cvar_set("teamplay", "3");
204 fraglimit_override = cvar("fraglimit_override");
207 // enforce the server's universal frag/time limits
208 if(fraglimit_override >= 0)
209 cvar_set("fraglimit", ftos(fraglimit_override));
210 if(timelimit_override >= 0)
211 cvar_set("timelimit", ftos(timelimit_override));
213 if (game == GAME_DOMINATION)//cvar("g_domination"))
215 else if (game == GAME_CTF)//cvar("g_ctf"))
217 else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
219 else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
222 // those mutators rule each other out
223 if(cvar("g_minstagib"))
225 cvar_set("g_instagib", "0");
226 cvar_set("g_rocketarena", "0");
228 if(cvar("g_instagib"))
230 cvar_set("g_minstagib", "0");
231 cvar_set("g_rocketarena", "0");
233 if(cvar("g_rocketarena"))
235 cvar_set("g_instagib", "0");
236 cvar_set("g_minstagib", "0");
240 void PrintWelcomeMessage(entity pl)
242 string s, grap_msg, temp, mutator;
244 /*if(self.welcomemessage_time < time)
246 if(self.welcomemessage_time2 > time)
248 self.welcomemessage_time2 = time + 0.8; */
250 if(self.welcomemessage_time2 > time) return;
251 self.welcomemessage_time2 = time + 1.0;
253 if(cvar("g_minstagib"))
254 mutator = "^2Minstagib ^1";
255 else if(cvar("g_instagib"))
256 mutator = "^2Instagib ^1";
257 else if(cvar("g_rocketarena"))
258 mutator = "^2Rocketarena ^1";
262 if(cvar("g_grappling_hook"))
263 grap_msg = strzone("\n\nBind a key to ^1+hook^8 to use the grappling hook\n");
265 s = strcat(s, "\n\nThis is Nexuiz ", cvar_string("g_nexuizversion"), "\n", self.versionmessage);
266 s = strcat(s, "^8\n\nMatch type is ^1", mutator, gamemode_name, "^8\n");
268 if(self.classname == "observer" || self.classname == "spectator") {
269 s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
276 "\n\n\n^8Welcome, ", pl.netname, "^8\n",
279 temp = strzone(temp);
282 if (cvar_string("g_mutatormsg") != "") {
283 s = strcat(s, "\n\nSpecial gameplay tips: ", cvar_string("g_mutatormsg"));
286 if (cvar_string("sv_motd") != "") {
287 s = strcat(s, "\n\nMOTD: ", cvar_string("sv_motd"));
297 void SetPlayerColors(entity pl, float color)
301 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
303 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
304 pl.clientcolors = 16*cl + cl;*/
307 setcolor(pl, 16*color + color);
310 shirt = pl.clientcolors & 0xF0;
311 setcolor(pl, shirt + color);
316 void SetPlayerTeam(entity pl, float t, float s, float noprint)
320 color = COLOR_TEAM4 - 1;
322 color = COLOR_TEAM3 - 1;
324 color = COLOR_TEAM2 - 1;
326 color = COLOR_TEAM1 - 1;
329 SetPlayerColors(pl,color);
332 if(!noprint && t != s)
334 //bprint(strcat(pl.netname, " has changed to ", TeamNoName(t), "\n"));
335 bprint(strcat(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n"));
344 // set c1...c4 to show what teams are allowed
345 void CheckAllowedTeams ()
354 c1 = c2 = c3 = c4 = -1;
355 cb1 = cb2 = cb3 = cb4 = 0;
358 teament_name = "dom_team";
360 teament_name = "ctf_team";
362 teament_name = "tdm_team";
365 // cover anything else by treating it like tdm with no teams spawned
366 dm = cvar("g_tdm_teams");
368 error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
372 c1 = c2 = c3 = c4 = 0;
385 // first find out what teams are allowed
386 head = find(world, classname, teament_name);
389 if(!(g_domination && head.netname == ""))
391 if(head.team == COLOR_TEAM1)
395 if(head.team == COLOR_TEAM2)
399 if(head.team == COLOR_TEAM3)
403 if(head.team == COLOR_TEAM4)
408 head = find(head, classname, teament_name);
412 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
413 // teams that are allowed will now have their player counts stored in c1...c4
414 void GetTeamCounts(entity ignore)
417 // now count how many players are on each team already
419 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
420 // also remember the lowest-scoring player
422 head = find(world, classname, "player");
425 if(head != ignore)// && head.netname != "")
427 if(head.team == COLOR_TEAM1)
435 if(head.team == COLOR_TEAM2)
443 if(head.team == COLOR_TEAM3)
451 if(head.team == COLOR_TEAM4)
460 head = find(head, classname, "player");
464 // returns # of smallest team (1, 2, 3, 4)
465 // NOTE: Assumes CheckAllowedTeams has already been called!
466 float FindSmallestTeam(entity pl, float ignore_pl)
468 float totalteams, smallestteam, smallestteam_count, balance_type;
471 // find out what teams are available
472 //CheckAllowedTeams();
474 // make sure there are at least 2 teams to join
476 totalteams = totalteams + 1;
478 totalteams = totalteams + 1;
480 totalteams = totalteams + 1;
482 totalteams = totalteams + 1;
487 error("Too few teams available for domination\n");
489 error("Too few teams available for ctf\n");
491 error("Too few teams available for team deathmatch\n");
495 // count how many players are in each team
497 GetTeamCounts(world);
503 // c1...c4 now have counts of each team
504 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
507 smallestteam_count = 999;
509 // 2 gives priority to what team you're already on, 1 goes in order
510 // 2 doesn't seem to work though...
513 if(balance_type == 1)
515 if(c1 >= 0 && c1 < smallestteam_count)
518 smallestteam_count = c1;
520 if(c2 >= 0 && c2 < smallestteam_count)
523 smallestteam_count = c2;
525 if(c3 >= 0 && c3 < smallestteam_count)
528 smallestteam_count = c3;
530 if(c4 >= 0 && c4 < smallestteam_count)
533 smallestteam_count = c4;
538 if(c1 >= 0 && (c1 < smallestteam_count ||
539 (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
542 smallestteam_count = c1;
544 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
545 (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
548 smallestteam_count = c2;
550 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
551 (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
554 smallestteam_count = c3;
556 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
557 (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
560 smallestteam_count = c4;
567 float JoinBestTeam(entity pl, float only_return_best)
569 float smallest, selectedteam;
571 g_domination = cvar("g_domination");
572 g_ctf = cvar("g_ctf");
574 // don't join a team if we're not playing a team game
575 if(!cvar("teamplay") && !g_domination && !g_ctf)
578 // find out what teams are available
581 // if we don't care what team he ends up on, put him on whatever team he entered as.
582 // if he's not on a valid team, then let other code put him on the smallest team
583 if(!cvar("balance_teams") && !cvar("force_balance"))
585 if( c1 >= 0 && pl.team == COLOR_TEAM1)
586 selectedteam = pl.team;
587 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
588 selectedteam = pl.team;
589 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
590 selectedteam = pl.team;
591 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
592 selectedteam = pl.team;
597 if(!only_return_best)
598 SetPlayerColors(pl, selectedteam - 1);
601 // otherwise end up on the smallest team (handled below)
604 smallest = FindSmallestTeam(pl, TRUE);
607 if(!only_return_best)
611 SetPlayerColors(pl, COLOR_TEAM1 - 1);
613 else if(smallest == 2)
615 SetPlayerColors(pl, COLOR_TEAM2 - 1);
617 else if(smallest == 3)
619 SetPlayerColors(pl, COLOR_TEAM3 - 1);
621 else if(smallest == 4)
623 SetPlayerColors(pl, COLOR_TEAM4 - 1);
626 error("smallest team: invalid team\n");
633 void SV_ChangeTeam(float color)
635 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
637 scolor = self.clientcolors & 0x0F;
638 dcolor = color & 0x0F;
640 // store shirt color in .clientcolors
641 // this will get overwritten in teamplay modes later
642 setcolor(self, color & 0xF0 + scolor);
644 if(scolor == COLOR_TEAM1 - 1)
646 else if(scolor == COLOR_TEAM2 - 1)
648 else if(scolor == COLOR_TEAM3 - 1)
650 else if(scolor == COLOR_TEAM4 - 1)
652 if(dcolor == COLOR_TEAM1 - 1)
654 else if(dcolor == COLOR_TEAM2 - 1)
656 else if(dcolor == COLOR_TEAM3 - 1)
658 else if(dcolor == COLOR_TEAM4 - 1)
661 // not changing teams
664 //bprint("same team change\n");
665 SetPlayerTeam(self, dteam, steam, TRUE);
671 if(cvar("g_changeteam_banned"))
673 sprint(self, "Team changes not allowed\n");
674 return; // changing teams is not allowed
677 if(cvar("g_balance_teams_prevent_imbalance"))
679 // only allow changing to a smaller or equal size team
681 // find out what teams are available
683 // count how many players on each team
684 GetTeamCounts(world);
687 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
692 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
697 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
702 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
709 sprint(self, "Cannot change to an invalid team\n");
715 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
717 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
719 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
721 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
724 if(scount) // started at a valid, nonempty team
726 // check if we're trying to change to a larger team that doens't have bots to swap with
727 if(dcount >= scount && dbotcount <= 0)
729 sprint(self, "Cannot change to a larger team\n");
730 return; // can't change to a larger team
736 // reduce frags during a team change
737 self.frags = floor(self.frags * (cvar("g_changeteam_fragtransfer") / 100));
739 // bprint(strcat("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n"));
741 SetPlayerTeam(self, dteam, steam, FALSE);
745 void ShufflePlayerOutOfTeam (float source_team)
747 float smallestteam, smallestteam_count, steam;
748 float lowest_bot_score, lowest_player_score;
749 entity head, lowest_bot, lowest_player, selected;
752 smallestteam_count = 999;
754 if(c1 >= 0 && c1 < smallestteam_count)
757 smallestteam_count = c1;
759 if(c2 >= 0 && c2 < smallestteam_count)
762 smallestteam_count = c2;
764 if(c3 >= 0 && c3 < smallestteam_count)
767 smallestteam_count = c3;
769 if(c4 >= 0 && c4 < smallestteam_count)
772 smallestteam_count = c4;
777 bprint("warning: no smallest team\n");
783 else if(source_team == 2)
785 else if(source_team == 3)
787 else if(source_team == 4)
791 lowest_bot_score = 9999;
792 lowest_player = world;
793 lowest_player_score = 9999;
795 // find the lowest-scoring player & bot of that team
796 head = find(world, classname, "player");
799 if(head.team == steam)
803 if(head.frags < lowest_bot_score)
806 lowest_bot_score = head.frags;
811 if(head.frags < lowest_player_score)
813 lowest_player = head;
814 lowest_player_score = head.frags;
818 head = find(head, classname, "player");
821 // prefers to move a bot...
822 if(lowest_bot != world)
823 selected = lowest_bot;
824 // but it will move a player if it has to
826 selected = lowest_player;
827 // don't do anything if it couldn't find anyone
830 bprint("warning: couldn't find a player to move from team\n");
834 // smallest team gains a member
835 if(smallestteam == 1)
839 else if(smallestteam == 2)
843 else if(smallestteam == 3)
847 else if(smallestteam == 4)
853 bprint("warning: destination team invalid\n");
856 // source team loses a member
861 else if(source_team == 2)
865 else if(source_team == 3)
869 else if(source_team == 4)
875 bprint("warning: source team invalid\n");
879 // move the player to the new team
880 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
883 // part of g_balance_teams_force
884 // occasionally perform an audit of the teams to make
885 // sure they're more or less balanced in player count.
888 float numplayers, numteams, average;
889 if(!cvar("g_balance_teams_force"))
891 if(!cvar("teamplay"))
894 if(audit_teams_time > time)
897 audit_teams_time = time + 4 + random();
899 // bprint("Auditing teams\n");
902 GetTeamCounts(world);
905 numteams = numplayers = 0;
908 numteams = numteams + 1;
909 numplayers = numplayers + c1;
913 numteams = numteams + 1;
914 numplayers = numplayers + c2;
918 numteams = numteams + 1;
919 numplayers = numplayers + c3;
923 numteams = numteams + 1;
924 numplayers = numplayers + c4;
928 return; // no players to move around
930 return; // don't bother shuffling if for some reason there aren't any teams
932 average = ceil(numplayers / numteams);
935 return; // that's weird...
937 if(c1 && c1 > average)
939 bprint("Rebalancing Teams\n");
940 //bprint("Shuffle from team 1\n");
941 ShufflePlayerOutOfTeam(1);
943 if(c2 && c2 > average)
945 bprint("Rebalancing Teams\n");
946 //bprint("Shuffle from team 2\n");
947 ShufflePlayerOutOfTeam(2);
949 if(c3 && c3 > average)
951 bprint("Rebalancing Teams\n");
952 //bprint("Shuffle from team 3\n");
953 ShufflePlayerOutOfTeam(3);
955 if(c4 && c4 > average)
957 bprint("Rebalancing Teams\n");
958 //bprint("Shuffle from team 4\n");
959 ShufflePlayerOutOfTeam(4);
962 // if teams are still unbalanced, balance them further in the next audit,
963 // which will happen sooner (keep doing rapid audits until things are in order)
964 audit_teams_time = time + 0.7 + random()*0.3;
969 /*void(entity e, float first) UpdateTeamScore =
974 WriteByte (MSG_ALL, SVC_UPDATENAME);
975 WriteByte (MSG_ALL, clientno);
976 WriteString (MSG_ALL, e.netname);
978 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
979 WriteByte (MSG_ALL, clientno);
980 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
983 WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
984 WriteByte (MSG_ALL, clientno);
985 WriteShort (MSG_ALL, e.frags + 10000);
996 self.classname = "tdm_team";
997 self.team = self.cnt + 1;
1000 // code from here on is just to support maps that don't have team entities
1001 void tdm_spawnteam (string teamname, float teamcolor)
1003 local entity oldself;
1006 self.classname = "tdm_team";
1007 self.netname = teamname;
1008 self.cnt = teamcolor;
1015 // spawn some default teams if the map is not set up for tdm
1016 void() tdm_spawnteams =
1020 numteams = cvar("g_tdm_teams");
1022 tdm_spawnteam("Red", 4);
1023 tdm_spawnteam("Blue", 13);
1026 void() tdm_delayedinit =
1028 self.think = SUB_Remove;
1029 self.nextthink = time;
1030 // if no teams are found, spawn defaults
1031 if (find(world, classname, "tdm_team") == world)
1039 e.think = tdm_delayedinit;
1040 e.nextthink = time + 0.1;