3 #include "cl_client.qh"
6 #include "scores_rules.qh"
10 #include "command/vote.qh"
12 #include "mutators/all.qh"
14 #include "../common/deathtypes/all.qh"
15 #include "../common/gamemodes/all.qh"
16 #include "../common/teams.qh"
18 void TeamchangeFrags(entity e)
23 void LogTeamchange(float player_id, float team_number, float type)
25 if(!autocvar_sv_eventlog)
31 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
34 void default_delayedinit(entity this)
36 if(!scores_initialized)
40 void ActivateTeamplay()
42 serverflags |= SERVERFLAG_TEAMPLAY;
44 cvar_set("teamplay", "2"); // DP needs this for sending proper getstatus replies.
47 void InitGameplayMode()
51 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
56 MapInfo_LoadMapSettings(mapname);
57 serverflags &= ~SERVERFLAG_TEAMPLAY;
59 cvar_set("teamplay", "0"); // DP needs this for sending proper getstatus replies.
61 if (!cvar_value_issafe(world.fog))
63 LOG_INFO("The current map contains a potentially harmful fog setting, ignored\n");
64 world.fog = string_null;
66 if(MapInfo_Map_fog != "")
67 if(MapInfo_Map_fog == "none")
68 world.fog = string_null;
70 world.fog = strzone(MapInfo_Map_fog);
71 clientstuff = strzone(MapInfo_Map_clientstuff);
75 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
77 cache_mutatormsg = strzone("");
78 cache_lastmutatormsg = strzone("");
80 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
83 string GetClientVersionMessage()
86 if (self.version_mismatch) {
87 if(self.version < autocvar_gameversion) {
88 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
90 versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
93 versionmsg = "^2client version and server version are compatible.^8";
98 string getwelcomemessage()
100 string s, modifications, motd;
102 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
103 modifications = ret_string;
107 if(g_weaponarena_random)
108 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
110 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
112 else if(cvar("g_balance_blaster_weaponstart") == 0)
113 modifications = strcat(modifications, ", No start weapons");
114 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
115 modifications = strcat(modifications, ", Low gravity");
116 if(g_weapon_stay && !g_cts)
117 modifications = strcat(modifications, ", Weapons stay");
119 modifications = strcat(modifications, ", Jet pack");
120 if(autocvar_g_powerups == 0)
121 modifications = strcat(modifications, ", No powerups");
122 if(autocvar_g_powerups > 0)
123 modifications = strcat(modifications, ", Powerups");
124 modifications = substring(modifications, 2, strlen(modifications) - 2);
126 string versionmessage;
127 versionmessage = GetClientVersionMessage();
129 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
130 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
132 if(modifications != "")
133 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
135 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
137 if(cache_lastmutatormsg)
138 strunzone(cache_lastmutatormsg);
140 strunzone(cache_mutatormsg);
141 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
142 cache_mutatormsg = strzone(cache_lastmutatormsg);
145 if (cache_mutatormsg != "") {
146 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
149 string mutator_msg = "";
150 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
151 mutator_msg = ret_string;
153 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
155 motd = autocvar_sv_motd;
157 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
162 void SetPlayerColors(entity pl, float _color)
166 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
168 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
169 pl.clientcolors = 16*cl + cl;*/
172 pants = _color & 0x0F;
173 shirt = _color & 0xF0;
177 setcolor(pl, 16*pants + pants);
179 setcolor(pl, shirt + pants);
183 void SetPlayerTeam(entity pl, float t, float s, float noprint)
188 _color = NUM_TEAM_4 - 1;
190 _color = NUM_TEAM_3 - 1;
192 _color = NUM_TEAM_2 - 1;
194 _color = NUM_TEAM_1 - 1;
196 SetPlayerColors(pl,_color);
199 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
202 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
207 // set c1...c4 to show what teams are allowed
208 void CheckAllowedTeams (entity for_whom)
212 c1 = c2 = c3 = c4 = -1;
213 cb1 = cb2 = cb3 = cb4 = 0;
215 string teament_name = string_null;
217 bool mutator_returnvalue = MUTATOR_CALLHOOK(GetTeamCount, dm, teament_name);
218 teament_name = ret_string;
221 if(!mutator_returnvalue)
224 c1 = c2 = c3 = c4 = 0;
231 // find out what teams are allowed if necessary
234 entity head = find(world, classname, teament_name);
239 case NUM_TEAM_1: c1 = 0; break;
240 case NUM_TEAM_2: c2 = 0; break;
241 case NUM_TEAM_3: c3 = 0; break;
242 case NUM_TEAM_4: c4 = 0; break;
245 head = find(head, classname, teament_name);
249 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
251 if(autocvar_bot_vs_human && for_whom)
253 if(autocvar_bot_vs_human > 0)
256 if(IS_BOT_CLIENT(for_whom))
264 if(IS_BOT_CLIENT(for_whom))
271 // if player has a forced team, ONLY allow that one
272 if(self.team_forced == NUM_TEAM_1 && c1 >= 0)
274 else if(self.team_forced == NUM_TEAM_2 && c2 >= 0)
276 else if(self.team_forced == NUM_TEAM_3 && c3 >= 0)
278 else if(self.team_forced == NUM_TEAM_4 && c4 >= 0)
282 float PlayerValue(entity p)
285 // FIXME: it always returns 1...
288 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
289 // teams that are allowed will now have their player counts stored in c1...c4
290 void GetTeamCounts(entity ignore)
293 // now count how many players are on each team already
295 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
296 // also remember the lowest-scoring player
298 FOREACH_CLIENT(true, LAMBDA(
300 if(IS_PLAYER(it) || it.caplayer)
302 else if(it.team_forced > 0)
303 t = it.team_forced; // reserve the spot
306 if(it != ignore)// && it.netname != "")
308 value = PlayerValue(it);
309 if(IS_BOT_CLIENT(it))
348 // if the player who has a forced team has not joined yet, reserve the spot
349 if(autocvar_g_campaign)
351 switch(autocvar_g_campaign_forceteam)
353 case 1: if(c1 == cb1) ++c1; break;
354 case 2: if(c2 == cb2) ++c2; break;
355 case 3: if(c3 == cb3) ++c3; break;
356 case 4: if(c4 == cb4) ++c4; break;
361 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
363 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
365 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
369 case 1: ca = c1; cba = cb1; sa = team1_score; break;
370 case 2: ca = c2; cba = cb2; sa = team2_score; break;
371 case 3: ca = c3; cba = cb3; sa = team3_score; break;
372 case 4: ca = c4; cba = cb4; sa = team4_score; break;
376 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
377 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
378 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
379 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
390 if(IS_REAL_CLIENT(e))
399 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
415 // the more we're at the end of the match, the more take scores into account
416 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
423 // returns # of smallest team (1, 2, 3, 4)
424 // NOTE: Assumes CheckAllowedTeams has already been called!
425 float FindSmallestTeam(entity pl, float ignore_pl)
430 // find out what teams are available
431 //CheckAllowedTeams();
433 // make sure there are at least 2 teams to join
435 totalteams = totalteams + 1;
437 totalteams = totalteams + 1;
439 totalteams = totalteams + 1;
441 totalteams = totalteams + 1;
443 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
448 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
449 return 1; // special case for campaign and player joining
451 error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
454 // count how many players are in each team
458 GetTeamCounts(world);
460 RandomSelection_Init();
463 if(TeamSmallerEqThanTeam(2, t, pl))
465 if(TeamSmallerEqThanTeam(3, t, pl))
467 if(TeamSmallerEqThanTeam(4, t, pl))
470 // now t is the minimum, or A minimum!
471 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
472 RandomSelection_Add(world, 1, string_null, 1, 1);
473 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
474 RandomSelection_Add(world, 2, string_null, 1, 1);
475 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
476 RandomSelection_Add(world, 3, string_null, 1, 1);
477 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
478 RandomSelection_Add(world, 4, string_null, 1, 1);
480 return RandomSelection_chosen_float;
483 int JoinBestTeam(entity pl, bool only_return_best, bool forcebestteam)
485 float smallest, selectedteam;
487 // don't join a team if we're not playing a team game
491 // find out what teams are available
492 CheckAllowedTeams(pl);
494 // if we don't care what team he ends up on, put him on whatever team he entered as.
495 // if he's not on a valid team, then let other code put him on the smallest team
498 if( c1 >= 0 && pl.team == NUM_TEAM_1)
499 selectedteam = pl.team;
500 else if(c2 >= 0 && pl.team == NUM_TEAM_2)
501 selectedteam = pl.team;
502 else if(c3 >= 0 && pl.team == NUM_TEAM_3)
503 selectedteam = pl.team;
504 else if(c4 >= 0 && pl.team == NUM_TEAM_4)
505 selectedteam = pl.team;
511 if(!only_return_best)
513 SetPlayerColors(pl, selectedteam - 1);
515 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
516 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
517 LogTeamchange(pl.playerid, pl.team, 99);
521 // otherwise end up on the smallest team (handled below)
524 smallest = FindSmallestTeam(pl, true);
526 if(!only_return_best && !pl.bot_forced_team)
528 TeamchangeFrags(self);
531 SetPlayerColors(pl, NUM_TEAM_1 - 1);
533 else if(smallest == 2)
535 SetPlayerColors(pl, NUM_TEAM_2 - 1);
537 else if(smallest == 3)
539 SetPlayerColors(pl, NUM_TEAM_3 - 1);
541 else if(smallest == 4)
543 SetPlayerColors(pl, NUM_TEAM_4 - 1);
547 error("smallest team: invalid team\n");
550 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
553 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE.m_id, pl.origin, '0 0 0');
559 //void() ctf_playerchanged;
560 void SV_ChangeTeam(float _color)
562 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
564 // in normal deathmatch we can just apply the color and we're done
566 SetPlayerColors(self, _color);
570 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
571 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_CONNECTING, self.netname);
578 scolor = self.clientcolors & 0x0F;
579 dcolor = _color & 0x0F;
581 if(scolor == NUM_TEAM_1 - 1)
583 else if(scolor == NUM_TEAM_2 - 1)
585 else if(scolor == NUM_TEAM_3 - 1)
587 else // if(scolor == NUM_TEAM_4 - 1)
589 if(dcolor == NUM_TEAM_1 - 1)
591 else if(dcolor == NUM_TEAM_2 - 1)
593 else if(dcolor == NUM_TEAM_3 - 1)
595 else // if(dcolor == NUM_TEAM_4 - 1)
598 CheckAllowedTeams(self);
600 if(dteam == 1 && c1 < 0) dteam = 4;
601 if(dteam == 4 && c4 < 0) dteam = 3;
602 if(dteam == 3 && c3 < 0) dteam = 2;
603 if(dteam == 2 && c2 < 0) dteam = 1;
605 // not changing teams
608 //bprint("same team change\n");
609 SetPlayerTeam(self, dteam, steam, true);
613 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
614 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
615 return; // changing teams is not allowed
618 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
619 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
622 if(!TeamSmallerEqThanTeam(dteam, steam, self))
624 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
629 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
631 if(IS_PLAYER(self) && steam != dteam)
633 // reduce frags during a team change
634 TeamchangeFrags(self);
637 MUTATOR_CALLHOOK(Player_ChangeTeam, self, steam, dteam);
639 SetPlayerTeam(self, dteam, steam, !IS_CLIENT(self));
641 if(IS_PLAYER(self) && steam != dteam)
643 // kill player when changing teams
645 Damage(self, self, self, 100000, DEATH_TEAMCHANGE.m_id, self.origin, '0 0 0');
649 void ShufflePlayerOutOfTeam (float source_team)
651 float smallestteam, smallestteam_count, steam;
652 float lowest_bot_score, lowest_player_score;
653 entity lowest_bot, lowest_player, selected;
656 smallestteam_count = 999999999;
658 if(c1 >= 0 && c1 < smallestteam_count)
661 smallestteam_count = c1;
663 if(c2 >= 0 && c2 < smallestteam_count)
666 smallestteam_count = c2;
668 if(c3 >= 0 && c3 < smallestteam_count)
671 smallestteam_count = c3;
673 if(c4 >= 0 && c4 < smallestteam_count)
676 smallestteam_count = c4;
681 bprint("warning: no smallest team\n");
687 else if(source_team == 2)
689 else if(source_team == 3)
691 else // if(source_team == 4)
695 lowest_bot_score = 999999999;
696 lowest_player = world;
697 lowest_player_score = 999999999;
699 // find the lowest-scoring player & bot of that team
700 FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, LAMBDA(
703 if(it.totalfrags < lowest_bot_score)
706 lowest_bot_score = it.totalfrags;
711 if(it.totalfrags < lowest_player_score)
714 lowest_player_score = it.totalfrags;
719 // prefers to move a bot...
720 if(lowest_bot != world)
721 selected = lowest_bot;
722 // but it will move a player if it has to
724 selected = lowest_player;
725 // don't do anything if it couldn't find anyone
728 bprint("warning: couldn't find a player to move from team\n");
732 // smallest team gains a member
733 if(smallestteam == 1)
737 else if(smallestteam == 2)
741 else if(smallestteam == 3)
745 else if(smallestteam == 4)
751 bprint("warning: destination team invalid\n");
754 // source team loses a member
759 else if(source_team == 2)
763 else if(source_team == 3)
767 else if(source_team == 4)
773 bprint("warning: source team invalid\n");
777 // move the player to the new team
778 TeamchangeFrags(selected);
779 SetPlayerTeam(selected, smallestteam, source_team, false);
781 if(!IS_DEAD(selected))
782 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
783 Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);