1 string cache_mutatormsg;
\r
2 string cache_lastmutatormsg;
\r
4 // client counts for each team
\r
5 float c1, c2, c3, c4;
\r
6 // # of bots on those teams
\r
7 float cb1, cb2, cb3, cb4;
\r
9 float audit_teams_time;
\r
11 float IsTeamBalanceForced()
\r
13 if(intermission_running)
\r
14 return 0; // no rebalancing whatsoever please
\r
17 if(cvar("g_campaign"))
\r
19 if(!cvar("g_balance_teams_force"))
\r
24 void TeamchangeFrags(entity e)
\r
26 PlayerScore_Clear(e);
\r
29 vector TeamColor(float teem)
\r
34 return '1 0.0625 0.0625';
\r
36 return '0.0625 0.0625 1';
\r
38 return '1 1 0.0625';
\r
40 return '1 0.0625 1';
\r
46 string TeamName(float t)
\r
48 return strcat(Team_ColorName(t), " Team");
\r
50 string ColoredTeamName(float t)
\r
52 return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
\r
54 string TeamNoName(float t)
\r
56 // fixme: Search for team entities and get their .netname's!
\r
62 return "Yellow Team";
\r
65 return "Neutral Team";
\r
73 void LogTeamchange(float player_id, float team_number, float type)
\r
75 if(!cvar("sv_eventlog"))
\r
81 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
\r
84 void WriteGameCvars()
\r
86 cvar_set("g_dm", ftos(g_dm));
\r
87 cvar_set("g_tdm", ftos(g_tdm));
\r
88 cvar_set("g_domination", ftos(g_domination));
\r
89 cvar_set("g_ctf", ftos(g_ctf));
\r
90 cvar_set("g_lms", ftos(g_lms));
\r
91 cvar_set("g_arena", ftos(g_arena));
\r
92 cvar_set("g_ca", ftos(g_ca));
\r
93 cvar_set("g_keyhunt", ftos(g_keyhunt));
\r
94 cvar_set("g_assault", ftos(g_assault));
\r
95 cvar_set("g_onslaught", ftos(g_onslaught));
\r
96 cvar_set("g_race", ftos(g_race));
\r
97 cvar_set("g_cts", ftos(g_cts));
\r
98 cvar_set("g_rpg", ftos(g_rpg));
\r
101 void ReadGameCvars()
\r
108 prev = cvar("gamecfg");
\r
109 for(i = 0; i < 2; ++i)
\r
111 found += (g_dm = (!found && (prev != GAME_DEATHMATCH) && cvar("g_dm")));
\r
112 found += (g_tdm = (!found && (prev != GAME_TEAM_DEATHMATCH) && cvar("g_tdm")));
\r
113 found += (g_domination = (!found && (prev != GAME_DOMINATION) && cvar("g_domination")));
\r
114 found += (g_ctf = (!found && (prev != GAME_CTF) && cvar("g_ctf")));
\r
115 found += (g_lms = (!found && (prev != GAME_LMS) && cvar("g_lms")));
\r
116 found += (g_arena = (!found && (prev != GAME_ARENA) && cvar("g_arena")));
\r
117 found += (g_ca = (!found && (prev != GAME_CA) && cvar("g_ca")));
\r
118 found += (g_keyhunt = (!found && (prev != GAME_KEYHUNT) && cvar("g_keyhunt")));
\r
119 found += (g_assault = (!found && (prev != GAME_ASSAULT) && cvar("g_assault")));
\r
120 found += (g_onslaught = (!found && (prev != GAME_ONSLAUGHT) && cvar("g_onslaught")));
\r
121 found += (g_race = (!found && (prev != GAME_RACE) && cvar("g_race")));
\r
122 found += (g_cts = (!found && (prev != GAME_CTS) && cvar("g_cts")));
\r
123 found += (g_rpg = (!found && (prev != GAME_RPG) && cvar("g_rpg")));
\r
128 prev = -1; // second attempt takes place WITHOUT prev set
\r
134 if(g_dm && cvar("deathmatch_force_teamplay"))
\r
143 void default_delayedinit()
\r
145 if(!scores_initialized)
\r
146 ScoreRules_generic();
\r
149 void ActivateTeamplay()
\r
151 float teamplay_default;
\r
152 teamplay_default = cvar("teamplay_default");
\r
154 if(teamplay_default)
\r
155 teamplay = teamplay_default;
\r
158 cvar_set("teamplay", ftos(teamplay));
\r
163 void InitGameplayMode()
\r
165 float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
\r
167 qualifying_override = -1;
\r
172 cvar_set("teamplay", "0");
\r
174 // make sure only ONE type is selected
\r
178 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
\r
180 world.mins = mi_min;
\r
181 world.maxs = mi_max;
\r
183 MapInfo_LoadMapSettings(mapname);
\r
185 if not(cvar_value_issafe(world.fog))
\r
187 print("The current map contains a potentially harmful fog setting, ignored\n");
\r
188 world.fog = string_null;
\r
190 if(MapInfo_Map_fog != "")
\r
191 if(MapInfo_Map_fog == "none")
\r
192 world.fog = string_null;
\r
194 world.fog = strzone(MapInfo_Map_fog);
\r
195 clientstuff = strzone(MapInfo_Map_clientstuff);
\r
197 MapInfo_ClearTemps();
\r
199 // in case mapinfo switched the type
\r
202 // set both here, gamemode can override it later
\r
203 timelimit_override = cvar("timelimit_override");
\r
204 fraglimit_override = cvar("fraglimit_override");
\r
205 leadlimit_override = cvar("leadlimit_override");
\r
209 game = GAME_DEATHMATCH;
\r
210 gamemode_name = "Deathmatch";
\r
215 game = GAME_TEAM_DEATHMATCH;
\r
216 gamemode_name = "Team Deathmatch";
\r
217 ActivateTeamplay();
\r
219 if(cvar("g_tdm_team_spawns"))
\r
220 have_team_spawns = -1; // request team spawns
\r
225 game = GAME_DOMINATION;
\r
226 gamemode_name = "Domination";
\r
227 ActivateTeamplay();
\r
228 fraglimit_override = cvar("g_domination_point_limit");
\r
229 leadlimit_override = cvar("g_domination_point_leadlimit");
\r
231 have_team_spawns = -1; // request team spawns
\r
237 gamemode_name = "Capture the Flag";
\r
238 ActivateTeamplay();
\r
239 if(cvar("g_campaign"))
\r
240 g_ctf_win_mode = 2;
\r
242 g_ctf_win_mode = cvar("g_ctf_win_mode");
\r
243 g_ctf_ignore_frags = cvar("g_ctf_ignore_frags");
\r
244 if(g_ctf_win_mode == 2)
\r
246 fraglimit_override = cvar("g_ctf_capture_limit");
\r
247 leadlimit_override = cvar("g_ctf_capture_leadlimit");
\r
251 fraglimit_override = cvar("capturelimit_override");
\r
252 leadlimit_override = cvar("captureleadlimit_override");
\r
255 have_team_spawns = -1; // request team spawns
\r
261 gamemode_name = "Last Man Standing";
\r
262 fraglimit_override = cvar("g_lms_lives_override");
\r
263 leadlimit_override = 0; // not supported by LMS
\r
264 if(fraglimit_override == 0)
\r
265 fraglimit_override = -1;
\r
266 lms_lowest_lives = 9999;
\r
267 lms_next_place = 0;
\r
274 gamemode_name = "Arena";
\r
275 fraglimit_override = cvar("g_arena_point_limit");
\r
276 leadlimit_override = cvar("g_arena_point_leadlimit");
\r
277 maxspawned = cvar("g_arena_maxspawned");
\r
280 arena_roundbased = cvar("g_arena_roundbased");
\r
286 gamemode_name = "Clan Arena";
\r
287 ActivateTeamplay();
\r
288 fraglimit_override = cvar("g_ca_point_limit");
\r
289 leadlimit_override = cvar("g_ca_point_leadlimit");
\r
290 precache_sound("ctf/red_capture.wav");
\r
291 precache_sound("ctf/blue_capture.wav");
\r
295 game = GAME_KEYHUNT;
\r
296 gamemode_name = "Key Hunt";
\r
297 ActivateTeamplay();
\r
298 fraglimit_override = cvar("g_keyhunt_point_limit");
\r
299 leadlimit_override = cvar("g_keyhunt_point_leadlimit");
\r
305 game = GAME_ASSAULT;
\r
306 gamemode_name = "Assault";
\r
307 ActivateTeamplay();
\r
308 ScoreRules_assault();
\r
309 have_team_spawns = -1; // request team spawns
\r
314 game = GAME_ONSLAUGHT;
\r
315 gamemode_name = "Onslaught";
\r
316 ActivateTeamplay();
\r
317 have_team_spawns = -1; // request team spawns
\r
323 gamemode_name = "Race";
\r
325 if(cvar("g_race_teams"))
\r
327 ActivateTeamplay();
\r
328 race_teams = bound(2, cvar("g_race_teams"), 4);
\r
329 have_team_spawns = -1; // request team spawns
\r
334 qualifying_override = cvar("g_race_qualifying_timelimit_override");
\r
335 fraglimit_override = cvar("g_race_laps_limit");
\r
336 leadlimit_override = 0; // currently not supported by race
\r
342 gamemode_name = "CTS";
\r
343 g_race_qualifying = 1;
\r
344 fraglimit_override = 0;
\r
345 leadlimit_override = 0;
\r
351 gamemode_name = "Role Play";
\r
352 fraglimit_override = 0;
\r
353 leadlimit_override = 0;
\r
360 // save it (for the next startup)
\r
361 cvar_set("gamecfg", ftos(game));
\r
363 cache_mutatormsg = strzone("");
\r
364 cache_lastmutatormsg = strzone("");
\r
366 // enforce the server's universal frag/time limits
\r
367 if(!cvar("g_campaign"))
\r
369 if(fraglimit_override >= 0)
\r
370 cvar_set("fraglimit", ftos(fraglimit_override));
\r
371 if(timelimit_override >= 0)
\r
372 cvar_set("timelimit", ftos(timelimit_override));
\r
373 if(leadlimit_override >= 0)
\r
374 cvar_set("leadlimit", ftos(leadlimit_override));
\r
375 if(qualifying_override >= 0)
\r
376 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
\r
381 // we need to find out the correct value for g_race_qualifying
\r
382 if(cvar("g_campaign"))
\r
384 g_race_qualifying = 1;
\r
386 else if(!cvar("g_campaign") && cvar("g_race_qualifying_timelimit") > 0)
\r
388 g_race_qualifying = 2;
\r
389 race_fraglimit = cvar("fraglimit");
\r
390 race_leadlimit = cvar("leadlimit");
\r
391 race_timelimit = cvar("timelimit");
\r
392 cvar_set("fraglimit", "0");
\r
393 cvar_set("leadlimit", "0");
\r
394 cvar_set("timelimit", cvar_string("g_race_qualifying_timelimit"));
\r
397 g_race_qualifying = 0;
\r
400 if(g_race || g_cts)
\r
402 if(g_race_qualifying)
\r
403 independent_players = 1;
\r
408 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
\r
411 string GetClientVersionMessage() {
\r
412 local string versionmsg;
\r
413 if (self.version_mismatch) {
\r
414 if(self.version < cvar("gameversion")) {
\r
415 versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
\r
417 versionmsg = "^3This server is using an outdated Voretournament version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
\r
420 versionmsg = "^2client version and server version are compatible.^8";
\r
426 void PrintWelcomeMessage(entity pl)
\r
428 string s, modifications, motd;
\r
430 if(self.cvar_scr_centertime == 0) return;
\r
432 if(cvar("g_campaign"))
\r
434 if(self.classname == "player" && !self.BUTTON_INFO)
\r
439 if((time - self.jointime) > cvar("welcome_message_time") && !self.BUTTON_INFO)
\r
443 if( !(timeoutStatus >= 1) ) { //really print the WelcomeMessage to the player every frame when timeout-seconds are shown or the game is restarted, to make sure that the shown number is accurate
\r
444 if(self.welcomemessage_time > time) return;
\r
445 self.welcomemessage_time = time + max(0.5, self.cvar_scr_centertime * 0.6);
\r
448 if(cvar("g_campaign"))
\r
450 centerprint(pl, campaign_message);
\r
454 //TODO GreEn`mArine: make the timeout-messages clientside as well (just like the ready restart countdown)!
\r
455 if(!self.BUTTON_INFO)
\r
457 // TODO get rid of this too
\r
458 local string specString;
\r
459 specString = NEWLINES;
\r
460 //if(time < game_starttime) //also show the countdown when being a spectator
\r
461 // specString = strcat(specString, "\n\n^1Game starts in ", ftos(ceil(game_starttime - time)), " seconds^7");
\r
463 if (timeoutStatus != 0)
\r
464 specString = strcat(specString, "\n\n", getTimeoutText(1));
\r
467 if(self.classname == "player")
\r
471 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
\r
475 modifications = "";
\r
476 if(cvar("sv_gravity") < 800)
\r
477 modifications = strcat(modifications, ", Low gravity");
\r
479 modifications = strcat(modifications, ", Cloaked");
\r
481 modifications = strcat(modifications, ", Midair");
\r
483 modifications = strcat(modifications, ", Vampire");
\r
485 modifications = strcat(modifications, ", Weapons stay");
\r
486 if(g_bloodloss > 0)
\r
487 modifications = strcat(modifications, ", Bloodloss");
\r
489 modifications = strcat(modifications, ", Jet pack");
\r
490 if(!cvar("g_start_weapon_grabber"))
\r
491 modifications = strcat(modifications, ", No start weapon");
\r
492 if(cvar("g_balance_vore_weight_gravity") < 0)
\r
493 modifications = strcat(modifications, ", Lighten");
\r
494 if(cvar("g_balance_vore_digestion_damage") >= 1000)
\r
495 modifications = strcat(modifications, ", InstaDigestion");
\r
496 modifications = substring(modifications, 2, strlen(modifications) - 2);
\r
498 local string versionmessage;
\r
499 versionmessage = GetClientVersionMessage();
\r
501 s = strcat(s, NEWLINES, "This is Voretournament ", cvar_string("g_voretournamentversion"), "\n", versionmessage);
\r
502 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
\r
504 if(modifications != "")
\r
505 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
\r
507 if(timeoutStatus != 0)
\r
508 s = strcat(s, "\n\n", getTimeoutText(1));
\r
510 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
\r
512 if(cache_lastmutatormsg)
\r
513 strunzone(cache_lastmutatormsg);
\r
514 if(cache_mutatormsg)
\r
515 strunzone(cache_mutatormsg);
\r
516 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
\r
517 cache_mutatormsg = strzone(cache_lastmutatormsg);
\r
520 if (cache_mutatormsg != "") {
\r
521 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
\r
524 motd = cvar_string("sv_motd");
\r
526 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
\r
528 s = strcat(s, "\n");
\r
530 centerprint(pl, s);
\r
534 void SetPlayerColors(entity pl, float _color)
\r
538 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
\r
540 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
\r
541 pl.clientcolors = 16*cl + cl;*/
\r
543 float pants, shirt;
\r
544 pants = _color & 0x0F;
\r
545 shirt = _color & 0xF0;
\r
549 setcolor(pl, 16*pants + pants);
\r
551 setcolor(pl, shirt + pants);
\r
555 void SetPlayerTeam(entity pl, float t, float s, float noprint)
\r
560 _color = COLOR_TEAM4 - 1;
\r
562 _color = COLOR_TEAM3 - 1;
\r
564 _color = COLOR_TEAM2 - 1;
\r
566 _color = COLOR_TEAM1 - 1;
\r
568 SetPlayerColors(pl,_color);
\r
571 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
\r
574 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
\r
579 // set c1...c4 to show what teams are allowed
\r
580 void CheckAllowedTeams (entity for_whom)
\r
584 string teament_name;
\r
586 c1 = c2 = c3 = c4 = -1;
\r
587 cb1 = cb2 = cb3 = cb4 = 0;
\r
591 // onslaught is special
\r
592 head = findchain(classname, "onslaught_generator");
\r
595 if (head.team == COLOR_TEAM1) c1 = 0;
\r
596 if (head.team == COLOR_TEAM2) c2 = 0;
\r
597 if (head.team == COLOR_TEAM3) c3 = 0;
\r
598 if (head.team == COLOR_TEAM4) c4 = 0;
\r
602 else if(g_domination)
\r
603 teament_name = "dom_team";
\r
605 teament_name = "ctf_team";
\r
607 teament_name = "tdm_team";
\r
609 c1 = c2 = 0; // Assault always has 2 teams
\r
612 // cover anything else by treating it like tdm with no teams spawned
\r
621 c1 = c2 = c3 = c4 = 0;
\r
628 // find out what teams are allowed if necessary
\r
631 head = find(world, classname, teament_name);
\r
634 if(!(g_domination && head.netname == ""))
\r
636 if(head.team == COLOR_TEAM1)
\r
638 else if(head.team == COLOR_TEAM2)
\r
640 else if(head.team == COLOR_TEAM3)
\r
642 else if(head.team == COLOR_TEAM4)
\r
645 head = find(head, classname, teament_name);
\r
649 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
\r
651 if(cvar("bot_vs_human") && for_whom)
\r
653 if(cvar("bot_vs_human") > 0)
\r
655 // bots are all blue
\r
656 if(clienttype(for_whom) == CLIENTTYPE_BOT)
\r
663 // bots are all red
\r
664 if(clienttype(for_whom) == CLIENTTYPE_BOT)
\r
672 float PlayerValue(entity p)
\r
674 if(IsTeamBalanceForced() == 1)
\r
679 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
\r
680 // teams that are allowed will now have their player counts stored in c1...c4
\r
681 void GetTeamCounts(entity ignore)
\r
684 float value, bvalue;
\r
685 // now count how many players are on each team already
\r
687 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
\r
688 // also remember the lowest-scoring player
\r
690 FOR_EACH_PLAYER(head)
\r
692 if(head != ignore)// && head.netname != "")
\r
694 value = PlayerValue(head);
\r
695 if(clienttype(head) == CLIENTTYPE_BOT)
\r
699 if(head.team == COLOR_TEAM1)
\r
704 cb1 = cb1 + bvalue;
\r
707 if(head.team == COLOR_TEAM2)
\r
712 cb2 = cb2 + bvalue;
\r
715 if(head.team == COLOR_TEAM3)
\r
720 cb3 = cb3 + bvalue;
\r
723 if(head.team == COLOR_TEAM4)
\r
728 cb4 = cb4 + bvalue;
\r
735 // returns # of smallest team (1, 2, 3, 4)
\r
736 // NOTE: Assumes CheckAllowedTeams has already been called!
\r
737 float FindSmallestTeam(entity pl, float ignore_pl)
\r
739 float totalteams, balance_type, maxc;
\r
742 // find out what teams are available
\r
743 //CheckAllowedTeams();
\r
745 // make sure there are at least 2 teams to join
\r
747 totalteams = totalteams + 1;
\r
749 totalteams = totalteams + 1;
\r
751 totalteams = totalteams + 1;
\r
753 totalteams = totalteams + 1;
\r
755 if(cvar("bot_vs_human"))
\r
758 if(totalteams <= 1)
\r
761 error("Too few teams available for domination\n");
\r
763 error("Too few teams available for ctf\n");
\r
765 error("Too few teams available for key hunt\n");
\r
767 error("Too few teams available for team deathmatch\n");
\r
770 // count how many players are in each team
\r
774 GetTeamCounts(world);
\r
776 // c1...c4 now have counts of each team
\r
777 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
\r
779 // 2 gives priority to what team you're already on, 1 goes in order
\r
780 // 2 doesn't seem to work though...
\r
783 if(bots_would_leave)
\r
784 //if(pl.classname != "player")
\r
785 if(clienttype(pl) != CLIENTTYPE_BOT)
\r
787 c1 -= cb1 * 255.0/256.0;
\r
788 c2 -= cb2 * 255.0/256.0;
\r
789 c3 -= cb3 * 255.0/256.0;
\r
790 c4 -= cb4 * 255.0/256.0;
\r
792 maxc = max4(c1, c2, c3, c4);
\r
794 RandomSelection_Init();
\r
795 if(balance_type == 1)
\r
797 // 1: use team count, then score (note: can only use 8 significant bits of score)
\r
798 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score) / 256.0);
\r
799 if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score) / 256.0);
\r
800 if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score) / 256.0);
\r
801 if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score) / 256.0);
\r
803 else if(balance_type == 2)
\r
805 // 1: use team count, if equal prefer own team
\r
806 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM1) / 512.0);
\r
807 if(c2 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM2) / 512.0);
\r
808 if(c3 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM3) / 512.0);
\r
809 if(c4 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM4) / 512.0);
\r
811 else if(balance_type == 3)
\r
813 // 1: use team count, then score, if equal prefer own team (probably fails due to float accuracy problems)
\r
814 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score + 0.5 * (self.team == COLOR_TEAM1)) / 256.0);
\r
815 if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score + 0.5 * (self.team == COLOR_TEAM2)) / 256.0);
\r
816 if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score + 0.5 * (self.team == COLOR_TEAM3)) / 256.0);
\r
817 if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score + 0.5 * (self.team == COLOR_TEAM4)) / 256.0);
\r
819 return RandomSelection_chosen_float;
\r
822 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
\r
824 float smallest, selectedteam;
\r
826 // don't join a team if we're not playing a team game
\r
830 // find out what teams are available
\r
831 CheckAllowedTeams(pl);
\r
833 // if we don't care what team he ends up on, put him on whatever team he entered as.
\r
834 // if he's not on a valid team, then let other code put him on the smallest team
\r
837 if( c1 >= 0 && pl.team == COLOR_TEAM1)
\r
838 selectedteam = pl.team;
\r
839 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
\r
840 selectedteam = pl.team;
\r
841 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
\r
842 selectedteam = pl.team;
\r
843 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
\r
844 selectedteam = pl.team;
\r
848 if(selectedteam > 0)
\r
850 if(!only_return_best)
\r
852 SetPlayerColors(pl, selectedteam - 1);
\r
854 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
\r
855 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
\r
856 LogTeamchange(pl.playerid, pl.team, 99);
\r
858 return selectedteam;
\r
860 // otherwise end up on the smallest team (handled below)
\r
863 smallest = FindSmallestTeam(pl, TRUE);
\r
865 if(!only_return_best)
\r
867 TeamchangeFrags(self);
\r
870 SetPlayerColors(pl, COLOR_TEAM1 - 1);
\r
872 else if(smallest == 2)
\r
874 SetPlayerColors(pl, COLOR_TEAM2 - 1);
\r
876 else if(smallest == 3)
\r
878 SetPlayerColors(pl, COLOR_TEAM3 - 1);
\r
880 else if(smallest == 4)
\r
882 SetPlayerColors(pl, COLOR_TEAM4 - 1);
\r
886 error("smallest team: invalid team\n");
\r
889 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
\r
891 if(pl.deadflag == DEAD_NO)
\r
892 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
\r
898 //void() ctf_playerchanged;
\r
899 void SV_ChangeTeam(float _color)
\r
901 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
\r
903 // in normal deathmatch we can just apply the color and we're done
\r
904 if(!teams_matter) {
\r
905 SetPlayerColors(self, _color);
\r
909 scolor = self.clientcolors & 0x0F;
\r
910 dcolor = _color & 0x0F;
\r
912 if(scolor == COLOR_TEAM1 - 1)
\r
914 else if(scolor == COLOR_TEAM2 - 1)
\r
916 else if(scolor == COLOR_TEAM3 - 1)
\r
918 else // if(scolor == COLOR_TEAM4 - 1)
\r
920 if(dcolor == COLOR_TEAM1 - 1)
\r
922 else if(dcolor == COLOR_TEAM2 - 1)
\r
924 else if(dcolor == COLOR_TEAM3 - 1)
\r
926 else // if(dcolor == COLOR_TEAM4 - 1)
\r
929 CheckAllowedTeams(self);
\r
931 if(dteam == 1 && c1 < 0) dteam = 4;
\r
932 if(dteam == 4 && c4 < 0) dteam = 3;
\r
933 if(dteam == 3 && c3 < 0) dteam = 2;
\r
934 if(dteam == 2 && c2 < 0) dteam = 1;
\r
936 // not changing teams
\r
937 if(scolor == dcolor)
\r
939 //bprint("same team change\n");
\r
940 SetPlayerTeam(self, dteam, steam, TRUE);
\r
944 if((cvar("g_campaign")) || (cvar("g_changeteam_banned") && self.wasplayer)) {
\r
945 sprint(self, "Team changes not allowed\n");
\r
946 return; // changing teams is not allowed
\r
949 if(cvar("g_balance_teams_prevent_imbalance"))
\r
951 // only allow changing to a smaller or equal size team
\r
953 // find out what teams are available
\r
954 //CheckAllowedTeams();
\r
955 // count how many players on each team
\r
956 GetTeamCounts(world);
\r
958 // get desired team
\r
959 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
\r
964 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
\r
969 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
\r
974 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
\r
981 sprint(self, "Cannot change to an invalid team\n");
\r
986 // get starting team
\r
987 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
\r
989 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
\r
991 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
\r
993 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
\r
996 if(scount) // started at a valid, nonempty team
\r
998 // check if we're trying to change to a larger team that doens't have bots to swap with
\r
999 if(dcount >= scount && dbotcount <= 0)
\r
1001 sprint(self, "Cannot change to a larger team\n");
\r
1002 return; // can't change to a larger team
\r
1007 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
\r
1009 if(self.classname == "player" && steam != dteam)
\r
1011 // reduce frags during a team change
\r
1012 TeamchangeFrags(self);
\r
1015 SetPlayerTeam(self, dteam, steam, FALSE);
\r
1017 if(self.classname == "player" && steam != dteam)
\r
1019 // kill player when changing teams
\r
1020 if(self.deadflag == DEAD_NO)
\r
1021 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
\r
1023 //ctf_playerchanged();
\r
1026 void ShufflePlayerOutOfTeam (float source_team)
\r
1028 float smallestteam, smallestteam_count, steam;
\r
1029 float lowest_bot_score, lowest_player_score;
\r
1030 entity head, lowest_bot, lowest_player, selected;
\r
1033 smallestteam_count = 999999999;
\r
1035 if(c1 >= 0 && c1 < smallestteam_count)
\r
1038 smallestteam_count = c1;
\r
1040 if(c2 >= 0 && c2 < smallestteam_count)
\r
1043 smallestteam_count = c2;
\r
1045 if(c3 >= 0 && c3 < smallestteam_count)
\r
1048 smallestteam_count = c3;
\r
1050 if(c4 >= 0 && c4 < smallestteam_count)
\r
1053 smallestteam_count = c4;
\r
1058 bprint("warning: no smallest team\n");
\r
1062 if(source_team == 1)
\r
1063 steam = COLOR_TEAM1;
\r
1064 else if(source_team == 2)
\r
1065 steam = COLOR_TEAM2;
\r
1066 else if(source_team == 3)
\r
1067 steam = COLOR_TEAM3;
\r
1068 else if(source_team == 4)
\r
1069 steam = COLOR_TEAM4;
\r
1071 lowest_bot = world;
\r
1072 lowest_bot_score = 999999999;
\r
1073 lowest_player = world;
\r
1074 lowest_player_score = 999999999;
\r
1076 // find the lowest-scoring player & bot of that team
\r
1077 FOR_EACH_PLAYER(head)
\r
1079 if(head.team == steam)
\r
1083 if(head.totalfrags < lowest_bot_score)
\r
1085 lowest_bot = head;
\r
1086 lowest_bot_score = head.totalfrags;
\r
1091 if(head.totalfrags < lowest_player_score)
\r
1093 lowest_player = head;
\r
1094 lowest_player_score = head.totalfrags;
\r
1100 // prefers to move a bot...
\r
1101 if(lowest_bot != world)
\r
1102 selected = lowest_bot;
\r
1103 // but it will move a player if it has to
\r
1105 selected = lowest_player;
\r
1106 // don't do anything if it couldn't find anyone
\r
1109 bprint("warning: couldn't find a player to move from team\n");
\r
1113 // smallest team gains a member
\r
1114 if(smallestteam == 1)
\r
1118 else if(smallestteam == 2)
\r
1122 else if(smallestteam == 3)
\r
1126 else if(smallestteam == 4)
\r
1132 bprint("warning: destination team invalid\n");
\r
1135 // source team loses a member
\r
1136 if(source_team == 1)
\r
1140 else if(source_team == 2)
\r
1144 else if(source_team == 3)
\r
1148 else if(source_team == 4)
\r
1154 bprint("warning: source team invalid\n");
\r
1158 // move the player to the new team
\r
1159 TeamchangeFrags(selected);
\r
1160 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
\r
1162 if(selected.deadflag == DEAD_NO)
\r
1163 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
\r
1164 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
\r
1167 void CauseRebalance(float source_team, float howmany_toomany)
\r
1169 if(IsTeamBalanceForced() == 1)
\r
1171 bprint("Rebalancing Teams\n");
\r
1172 ShufflePlayerOutOfTeam(source_team);
\r
1176 // part of g_balance_teams_force
\r
1177 // occasionally perform an audit of the teams to make
\r
1178 // sure they're more or less balanced in player count.
\r
1181 float numplayers, numteams, smallest, toomany;
\r
1183 balance = IsTeamBalanceForced();
\r
1187 if(audit_teams_time > time)
\r
1190 audit_teams_time = time + 4 + random();
\r
1192 // bprint("Auditing teams\n");
\r
1194 CheckAllowedTeams(world);
\r
1195 GetTeamCounts(world);
\r
1198 numteams = numplayers = smallest = 0;
\r
1201 numteams = numteams + 1;
\r
1202 numplayers = numplayers + c1;
\r
1207 numteams = numteams + 1;
\r
1208 numplayers = numplayers + c2;
\r
1214 numteams = numteams + 1;
\r
1215 numplayers = numplayers + c3;
\r
1221 numteams = numteams + 1;
\r
1222 numplayers = numplayers + c4;
\r
1227 if(numplayers <= 0)
\r
1228 return; // no players to move around
\r
1230 return; // don't bother shuffling if for some reason there aren't any teams
\r
1232 toomany = smallest + 1;
\r
1234 if(c1 && c1 > toomany)
\r
1235 CauseRebalance(1, c1 - toomany);
\r
1236 if(c2 && c2 > toomany)
\r
1237 CauseRebalance(2, c2 - toomany);
\r
1238 if(c3 && c3 > toomany)
\r
1239 CauseRebalance(3, c3 - toomany);
\r
1240 if(c4 && c4 > toomany)
\r
1241 CauseRebalance(4, c4 - toomany);
\r
1243 // if teams are still unbalanced, balance them further in the next audit,
\r
1244 // which will happen sooner (keep doing rapid audits until things are in order)
\r
1245 audit_teams_time = time + 0.7 + random()*0.3;
\r
1248 // code from here on is just to support maps that don't have team entities
\r
1249 void tdm_spawnteam (string teamname, float teamcolor)
\r
1253 e.classname = "tdm_team";
\r
1254 e.netname = teamname;
\r
1255 e.cnt = teamcolor;
\r
1256 e.team = e.cnt + 1;
\r
1259 // spawn some default teams if the map is not set up for tdm
\r
1260 void tdm_spawnteams()
\r
1264 numteams = cvar("g_tdm_teams_override");
\r
1266 numteams = cvar("g_tdm_teams");
\r
1267 numteams = bound(2, numteams, 4);
\r
1269 tdm_spawnteam("Red", COLOR_TEAM1-1);
\r
1270 tdm_spawnteam("Blue", COLOR_TEAM2-1);
\r
1272 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
\r
1274 tdm_spawnteam("Pink", COLOR_TEAM4-1);
\r
1277 void tdm_delayedinit()
\r
1279 // if no teams are found, spawn defaults
\r
1280 if (find(world, classname, "tdm_team") == world)
\r
1286 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);
\r