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(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
83 string GetClientVersionMessage(entity this)
86 if (this.version_mismatch) {
87 if(this.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(entity this)
100 string s, modifications, motd;
102 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
103 modifications = M_ARGV(0, 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 = GetClientVersionMessage(this);
128 s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
129 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
131 if(modifications != "")
132 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
134 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
136 if(cache_lastmutatormsg)
137 strunzone(cache_lastmutatormsg);
139 strunzone(cache_mutatormsg);
140 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
141 cache_mutatormsg = strzone(cache_lastmutatormsg);
144 if (cache_mutatormsg != "") {
145 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
148 string mutator_msg = "";
149 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
150 mutator_msg = M_ARGV(0, string);
152 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
154 motd = autocvar_sv_motd;
156 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
161 void SetPlayerColors(entity pl, float _color)
165 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
167 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
168 pl.clientcolors = 16*cl + cl;*/
171 pants = _color & 0x0F;
172 shirt = _color & 0xF0;
176 setcolor(pl, 16*pants + pants);
178 setcolor(pl, shirt + pants);
182 void SetPlayerTeam(entity pl, float t, float s, float noprint)
187 _color = NUM_TEAM_4 - 1;
189 _color = NUM_TEAM_3 - 1;
191 _color = NUM_TEAM_2 - 1;
193 _color = NUM_TEAM_1 - 1;
195 SetPlayerColors(pl,_color);
198 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
201 bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
206 // set c1...c4 to show what teams are allowed
207 void CheckAllowedTeams (entity for_whom)
211 c1 = c2 = c3 = c4 = -1;
212 cb1 = cb2 = cb3 = cb4 = 0;
214 string teament_name = string_null;
216 bool mutator_returnvalue = MUTATOR_CALLHOOK(GetTeamCount, dm, teament_name);
217 dm = M_ARGV(0, float);
218 teament_name = M_ARGV(1, string);
220 if(!mutator_returnvalue)
223 c1 = c2 = c3 = c4 = 0;
230 // find out what teams are allowed if necessary
233 entity head = find(NULL, classname, teament_name);
238 case NUM_TEAM_1: c1 = 0; break;
239 case NUM_TEAM_2: c2 = 0; break;
240 case NUM_TEAM_3: c3 = 0; break;
241 case NUM_TEAM_4: c4 = 0; break;
244 head = find(head, classname, teament_name);
248 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
250 if(autocvar_bot_vs_human && for_whom)
252 if(autocvar_bot_vs_human > 0)
255 if(IS_BOT_CLIENT(for_whom))
263 if(IS_BOT_CLIENT(for_whom))
273 // if player has a forced team, ONLY allow that one
274 if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
276 else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
278 else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
280 else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
284 float PlayerValue(entity p)
287 // FIXME: it always returns 1...
290 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
291 // teams that are allowed will now have their player counts stored in c1...c4
292 void GetTeamCounts(entity ignore)
295 // now count how many players are on each team already
297 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
298 // also remember the lowest-scoring player
300 FOREACH_CLIENT(true, LAMBDA(
302 if(IS_PLAYER(it) || it.caplayer)
304 else if(it.team_forced > 0)
305 t = it.team_forced; // reserve the spot
308 if(it != ignore)// && it.netname != "")
310 value = PlayerValue(it);
311 if(IS_BOT_CLIENT(it))
350 // if the player who has a forced team has not joined yet, reserve the spot
351 if(autocvar_g_campaign)
353 switch(autocvar_g_campaign_forceteam)
355 case 1: if(c1 == cb1) ++c1; break;
356 case 2: if(c2 == cb2) ++c2; break;
357 case 3: if(c3 == cb3) ++c3; break;
358 case 4: if(c4 == cb4) ++c4; break;
363 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
365 // we assume that CheckAllowedTeams and GetTeamCounts have already been called
367 float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
371 case 1: ca = c1; cba = cb1; sa = team1_score; break;
372 case 2: ca = c2; cba = cb2; sa = team2_score; break;
373 case 3: ca = c3; cba = cb3; sa = team3_score; break;
374 case 4: ca = c4; cba = cb4; sa = team4_score; break;
378 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
379 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
380 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
381 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
392 if(IS_REAL_CLIENT(e))
401 // keep teams alive (teams of size 0 always count as smaller, ignoring score)
417 // the more we're at the end of the match, the more take scores into account
418 f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
425 // returns # of smallest team (1, 2, 3, 4)
426 // NOTE: Assumes CheckAllowedTeams has already been called!
427 float FindSmallestTeam(entity pl, float ignore_pl)
432 // find out what teams are available
433 //CheckAllowedTeams();
435 // make sure there are at least 2 teams to join
437 totalteams = totalteams + 1;
439 totalteams = totalteams + 1;
441 totalteams = totalteams + 1;
443 totalteams = totalteams + 1;
445 if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
450 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
451 return 1; // special case for campaign and player joining
453 error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
456 // count how many players are in each team
462 RandomSelection_Init();
465 if(TeamSmallerEqThanTeam(2, t, pl))
467 if(TeamSmallerEqThanTeam(3, t, pl))
469 if(TeamSmallerEqThanTeam(4, t, pl))
472 // now t is the minimum, or A minimum!
473 if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
474 RandomSelection_Add(NULL, 1, string_null, 1, 1);
475 if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
476 RandomSelection_Add(NULL, 2, string_null, 1, 1);
477 if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
478 RandomSelection_Add(NULL, 3, string_null, 1, 1);
479 if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
480 RandomSelection_Add(NULL, 4, string_null, 1, 1);
482 return RandomSelection_chosen_float;
485 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
487 float smallest, selectedteam;
489 // don't join a team if we're not playing a team game
493 // find out what teams are available
494 CheckAllowedTeams(this);
496 // if we don't care what team he ends up on, put him on whatever team he entered as.
497 // if he's not on a valid team, then let other code put him on the smallest team
500 if( c1 >= 0 && this.team == NUM_TEAM_1)
501 selectedteam = this.team;
502 else if(c2 >= 0 && this.team == NUM_TEAM_2)
503 selectedteam = this.team;
504 else if(c3 >= 0 && this.team == NUM_TEAM_3)
505 selectedteam = this.team;
506 else if(c4 >= 0 && this.team == NUM_TEAM_4)
507 selectedteam = this.team;
513 if(!only_return_best)
515 SetPlayerColors(this, selectedteam - 1);
517 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
518 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
519 LogTeamchange(this.playerid, this.team, 99);
523 // otherwise end up on the smallest team (handled below)
526 smallest = FindSmallestTeam(this, true);
528 if(!only_return_best && !this.bot_forced_team)
530 TeamchangeFrags(this);
533 SetPlayerColors(this, NUM_TEAM_1 - 1);
535 else if(smallest == 2)
537 SetPlayerColors(this, NUM_TEAM_2 - 1);
539 else if(smallest == 3)
541 SetPlayerColors(this, NUM_TEAM_3 - 1);
543 else if(smallest == 4)
545 SetPlayerColors(this, NUM_TEAM_4 - 1);
549 error("smallest team: invalid team\n");
552 LogTeamchange(this.playerid, this.team, 2); // log auto join
555 Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
561 //void() ctf_playerchanged;
562 void SV_ChangeTeam(entity this, float _color)
564 float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
566 // in normal deathmatch we can just apply the color and we're done
568 SetPlayerColors(this, _color);
572 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
573 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
580 scolor = this.clientcolors & 0x0F;
581 dcolor = _color & 0x0F;
583 if(scolor == NUM_TEAM_1 - 1)
585 else if(scolor == NUM_TEAM_2 - 1)
587 else if(scolor == NUM_TEAM_3 - 1)
589 else // if(scolor == NUM_TEAM_4 - 1)
591 if(dcolor == NUM_TEAM_1 - 1)
593 else if(dcolor == NUM_TEAM_2 - 1)
595 else if(dcolor == NUM_TEAM_3 - 1)
597 else // if(dcolor == NUM_TEAM_4 - 1)
600 CheckAllowedTeams(this);
602 if(dteam == 1 && c1 < 0) dteam = 4;
603 if(dteam == 4 && c4 < 0) dteam = 3;
604 if(dteam == 3 && c3 < 0) dteam = 2;
605 if(dteam == 2 && c2 < 0) dteam = 1;
607 // not changing teams
610 //bprint("same team change\n");
611 SetPlayerTeam(this, dteam, steam, true);
615 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && this.wasplayer)) {
616 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
617 return; // changing teams is not allowed
620 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
621 if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
624 if(!TeamSmallerEqThanTeam(dteam, steam, this))
626 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
631 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
633 if(IS_PLAYER(this) && steam != dteam)
635 // reduce frags during a team change
636 TeamchangeFrags(this);
639 MUTATOR_CALLHOOK(Player_ChangeTeam, this, steam, dteam);
641 SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
643 if(IS_PLAYER(this) && steam != dteam)
645 // kill player when changing teams
647 Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
651 void ShufflePlayerOutOfTeam (float source_team)
653 float smallestteam, smallestteam_count, steam;
654 float lowest_bot_score, lowest_player_score;
655 entity lowest_bot, lowest_player, selected;
658 smallestteam_count = 999999999;
660 if(c1 >= 0 && c1 < smallestteam_count)
663 smallestteam_count = c1;
665 if(c2 >= 0 && c2 < smallestteam_count)
668 smallestteam_count = c2;
670 if(c3 >= 0 && c3 < smallestteam_count)
673 smallestteam_count = c3;
675 if(c4 >= 0 && c4 < smallestteam_count)
678 smallestteam_count = c4;
683 bprint("warning: no smallest team\n");
689 else if(source_team == 2)
691 else if(source_team == 3)
693 else // if(source_team == 4)
697 lowest_bot_score = 999999999;
698 lowest_player = NULL;
699 lowest_player_score = 999999999;
701 // find the lowest-scoring player & bot of that team
702 FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, LAMBDA(
705 if(it.totalfrags < lowest_bot_score)
708 lowest_bot_score = it.totalfrags;
713 if(it.totalfrags < lowest_player_score)
716 lowest_player_score = it.totalfrags;
721 // prefers to move a bot...
722 if(lowest_bot != NULL)
723 selected = lowest_bot;
724 // but it will move a player if it has to
726 selected = lowest_player;
727 // don't do anything if it couldn't find anyone
730 bprint("warning: couldn't find a player to move from team\n");
734 // smallest team gains a member
735 if(smallestteam == 1)
739 else if(smallestteam == 2)
743 else if(smallestteam == 3)
747 else if(smallestteam == 4)
753 bprint("warning: destination team invalid\n");
756 // source team loses a member
761 else if(source_team == 2)
765 else if(source_team == 3)
769 else if(source_team == 4)
775 bprint("warning: source team invalid\n");
779 // move the player to the new team
780 TeamchangeFrags(selected);
781 SetPlayerTeam(selected, smallestteam, source_team, false);
783 if(!IS_DEAD(selected))
784 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
785 Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);