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
357 // save it (for the next startup)
\r
358 cvar_set("gamecfg", ftos(game));
\r
360 cache_mutatormsg = strzone("");
\r
361 cache_lastmutatormsg = strzone("");
\r
363 // enforce the server's universal frag/time limits
\r
364 if(!cvar("g_campaign"))
\r
366 if(fraglimit_override >= 0)
\r
367 cvar_set("fraglimit", ftos(fraglimit_override));
\r
368 if(timelimit_override >= 0)
\r
369 cvar_set("timelimit", ftos(timelimit_override));
\r
370 if(leadlimit_override >= 0)
\r
371 cvar_set("leadlimit", ftos(leadlimit_override));
\r
372 if(qualifying_override >= 0)
\r
373 cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
\r
378 // we need to find out the correct value for g_race_qualifying
\r
379 if(cvar("g_campaign"))
\r
381 g_race_qualifying = 1;
\r
383 else if(!cvar("g_campaign") && cvar("g_race_qualifying_timelimit") > 0)
\r
385 g_race_qualifying = 2;
\r
386 race_fraglimit = cvar("fraglimit");
\r
387 race_leadlimit = cvar("leadlimit");
\r
388 race_timelimit = cvar("timelimit");
\r
389 cvar_set("fraglimit", "0");
\r
390 cvar_set("leadlimit", "0");
\r
391 cvar_set("timelimit", cvar_string("g_race_qualifying_timelimit"));
\r
394 g_race_qualifying = 0;
\r
397 if(g_race || g_cts)
\r
399 if(g_race_qualifying)
\r
400 independent_players = 1;
\r
405 InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
\r
408 string GetClientVersionMessage() {
\r
409 local string versionmsg;
\r
410 if (self.version_mismatch) {
\r
411 if(self.version < cvar("gameversion")) {
\r
412 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
414 versionmsg = "^3This server is using an outdated Voretournament version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
\r
417 versionmsg = "^2client version and server version are compatible.^8";
\r
423 void PrintWelcomeMessage(entity pl)
\r
425 string s, modifications, motd;
\r
427 if(self.cvar_scr_centertime == 0) return;
\r
429 if(cvar("g_campaign"))
\r
431 if(self.classname == "player" && !self.BUTTON_INFO)
\r
436 if((time - self.jointime) > cvar("welcome_message_time") && !self.BUTTON_INFO)
\r
440 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
441 if(self.welcomemessage_time > time) return;
\r
442 self.welcomemessage_time = time + max(0.5, self.cvar_scr_centertime * 0.6);
\r
445 if(cvar("g_campaign"))
\r
447 centerprint(pl, campaign_message);
\r
451 //TODO GreEn`mArine: make the timeout-messages clientside as well (just like the ready restart countdown)!
\r
452 if(!self.BUTTON_INFO)
\r
454 // TODO get rid of this too
\r
455 local string specString;
\r
456 specString = NEWLINES;
\r
457 //if(time < game_starttime) //also show the countdown when being a spectator
\r
458 // specString = strcat(specString, "\n\n^1Game starts in ", ftos(ceil(game_starttime - time)), " seconds^7");
\r
460 if (timeoutStatus != 0)
\r
461 specString = strcat(specString, "\n\n", getTimeoutText(1));
\r
464 if(self.classname == "player")
\r
468 return centerprint_atprio(self, CENTERPRIO_SPAM, specString);
\r
472 modifications = "";
\r
473 if(cvar("sv_gravity") < 800)
\r
474 modifications = strcat(modifications, ", Low gravity");
\r
476 modifications = strcat(modifications, ", Cloaked");
\r
478 modifications = strcat(modifications, ", Midair");
\r
480 modifications = strcat(modifications, ", Vampire");
\r
482 modifications = strcat(modifications, ", Weapons stay");
\r
483 if(g_bloodloss > 0)
\r
484 modifications = strcat(modifications, ", Bloodloss");
\r
486 modifications = strcat(modifications, ", Jet pack");
\r
487 if(cvar("g_balance_vore_weight_gravity") < 0)
\r
488 modifications = strcat(modifications, ", Lighten");
\r
489 if(cvar("g_balance_vore_digestion_damage") >= 1000)
\r
490 modifications = strcat(modifications, ", InstaDigestion");
\r
491 modifications = substring(modifications, 2, strlen(modifications) - 2);
\r
493 local string versionmessage;
\r
494 versionmessage = GetClientVersionMessage();
\r
496 s = strcat(s, NEWLINES, "This is Voretournament ", cvar_string("g_voretournamentversion"), "\n", versionmessage);
\r
497 s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
\r
499 if(modifications != "")
\r
500 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
\r
502 if(timeoutStatus != 0)
\r
503 s = strcat(s, "\n\n", getTimeoutText(1));
\r
505 if(cache_lastmutatormsg != cvar_string("g_mutatormsg"))
\r
507 if(cache_lastmutatormsg)
\r
508 strunzone(cache_lastmutatormsg);
\r
509 if(cache_mutatormsg)
\r
510 strunzone(cache_mutatormsg);
\r
511 cache_lastmutatormsg = strzone(cvar_string("g_mutatormsg"));
\r
512 cache_mutatormsg = strzone(cache_lastmutatormsg);
\r
515 if (cache_mutatormsg != "") {
\r
516 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
\r
519 motd = cvar_string("sv_motd");
\r
521 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
\r
523 s = strcat(s, "\n");
\r
525 centerprint(pl, s);
\r
529 void SetPlayerColors(entity pl, float _color)
\r
533 stuffcmd(pl, strcat("color ", s, " ", s, "\n") );
\r
535 //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
\r
536 pl.clientcolors = 16*cl + cl;*/
\r
538 float pants, shirt;
\r
539 pants = _color & 0x0F;
\r
540 shirt = _color & 0xF0;
\r
544 setcolor(pl, 16*pants + pants);
\r
546 setcolor(pl, shirt + pants);
\r
550 void SetPlayerTeam(entity pl, float t, float s, float noprint)
\r
555 _color = COLOR_TEAM4 - 1;
\r
557 _color = COLOR_TEAM3 - 1;
\r
559 _color = COLOR_TEAM2 - 1;
\r
561 _color = COLOR_TEAM1 - 1;
\r
563 SetPlayerColors(pl,_color);
\r
566 LogTeamchange(pl.playerid, pl.team, 3); // log manual team join
\r
569 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
\r
574 // set c1...c4 to show what teams are allowed
\r
575 void CheckAllowedTeams (entity for_whom)
\r
579 string teament_name;
\r
581 c1 = c2 = c3 = c4 = -1;
\r
582 cb1 = cb2 = cb3 = cb4 = 0;
\r
586 // onslaught is special
\r
587 head = findchain(classname, "onslaught_generator");
\r
590 if (head.team == COLOR_TEAM1) c1 = 0;
\r
591 if (head.team == COLOR_TEAM2) c2 = 0;
\r
592 if (head.team == COLOR_TEAM3) c3 = 0;
\r
593 if (head.team == COLOR_TEAM4) c4 = 0;
\r
597 else if(g_domination)
\r
598 teament_name = "dom_team";
\r
600 teament_name = "ctf_team";
\r
602 teament_name = "tdm_team";
\r
604 c1 = c2 = 0; // Assault always has 2 teams
\r
607 // cover anything else by treating it like tdm with no teams spawned
\r
616 c1 = c2 = c3 = c4 = 0;
\r
623 // find out what teams are allowed if necessary
\r
626 head = find(world, classname, teament_name);
\r
629 if(!(g_domination && head.netname == ""))
\r
631 if(head.team == COLOR_TEAM1)
\r
633 else if(head.team == COLOR_TEAM2)
\r
635 else if(head.team == COLOR_TEAM3)
\r
637 else if(head.team == COLOR_TEAM4)
\r
640 head = find(head, classname, teament_name);
\r
644 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
\r
646 if(cvar("bot_vs_human") && for_whom)
\r
648 if(cvar("bot_vs_human") > 0)
\r
650 // bots are all blue
\r
651 if(clienttype(for_whom) == CLIENTTYPE_BOT)
\r
658 // bots are all red
\r
659 if(clienttype(for_whom) == CLIENTTYPE_BOT)
\r
667 float PlayerValue(entity p)
\r
669 if(IsTeamBalanceForced() == 1)
\r
674 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
\r
675 // teams that are allowed will now have their player counts stored in c1...c4
\r
676 void GetTeamCounts(entity ignore)
\r
679 float value, bvalue;
\r
680 // now count how many players are on each team already
\r
682 // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
\r
683 // also remember the lowest-scoring player
\r
685 FOR_EACH_PLAYER(head)
\r
687 if(head != ignore)// && head.netname != "")
\r
689 value = PlayerValue(head);
\r
690 if(clienttype(head) == CLIENTTYPE_BOT)
\r
694 if(head.team == COLOR_TEAM1)
\r
699 cb1 = cb1 + bvalue;
\r
702 if(head.team == COLOR_TEAM2)
\r
707 cb2 = cb2 + bvalue;
\r
710 if(head.team == COLOR_TEAM3)
\r
715 cb3 = cb3 + bvalue;
\r
718 if(head.team == COLOR_TEAM4)
\r
723 cb4 = cb4 + bvalue;
\r
730 // returns # of smallest team (1, 2, 3, 4)
\r
731 // NOTE: Assumes CheckAllowedTeams has already been called!
\r
732 float FindSmallestTeam(entity pl, float ignore_pl)
\r
734 float totalteams, balance_type, maxc;
\r
737 // find out what teams are available
\r
738 //CheckAllowedTeams();
\r
740 // make sure there are at least 2 teams to join
\r
742 totalteams = totalteams + 1;
\r
744 totalteams = totalteams + 1;
\r
746 totalteams = totalteams + 1;
\r
748 totalteams = totalteams + 1;
\r
750 if(cvar("bot_vs_human"))
\r
753 if(totalteams <= 1)
\r
756 error("Too few teams available for domination\n");
\r
758 error("Too few teams available for ctf\n");
\r
760 error("Too few teams available for key hunt\n");
\r
762 error("Too few teams available for team deathmatch\n");
\r
765 // count how many players are in each team
\r
769 GetTeamCounts(world);
\r
771 // c1...c4 now have counts of each team
\r
772 // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
\r
774 // 2 gives priority to what team you're already on, 1 goes in order
\r
775 // 2 doesn't seem to work though...
\r
778 if(bots_would_leave)
\r
779 //if(pl.classname != "player")
\r
780 if(clienttype(pl) != CLIENTTYPE_BOT)
\r
782 c1 -= cb1 * 255.0/256.0;
\r
783 c2 -= cb2 * 255.0/256.0;
\r
784 c3 -= cb3 * 255.0/256.0;
\r
785 c4 -= cb4 * 255.0/256.0;
\r
787 maxc = max4(c1, c2, c3, c4);
\r
789 RandomSelection_Init();
\r
790 if(balance_type == 1)
\r
792 // 1: use team count, then score (note: can only use 8 significant bits of score)
\r
793 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score) / 256.0);
\r
794 if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score) / 256.0);
\r
795 if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score) / 256.0);
\r
796 if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score) / 256.0);
\r
798 else if(balance_type == 2)
\r
800 // 1: use team count, if equal prefer own team
\r
801 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM1) / 512.0);
\r
802 if(c2 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM2) / 512.0);
\r
803 if(c3 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM3) / 512.0);
\r
804 if(c4 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM4) / 512.0);
\r
806 else if(balance_type == 3)
\r
808 // 1: use team count, then score, if equal prefer own team (probably fails due to float accuracy problems)
\r
809 if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score + 0.5 * (self.team == COLOR_TEAM1)) / 256.0);
\r
810 if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score + 0.5 * (self.team == COLOR_TEAM2)) / 256.0);
\r
811 if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score + 0.5 * (self.team == COLOR_TEAM3)) / 256.0);
\r
812 if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score + 0.5 * (self.team == COLOR_TEAM4)) / 256.0);
\r
814 return RandomSelection_chosen_float;
\r
817 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
\r
819 float smallest, selectedteam;
\r
821 // don't join a team if we're not playing a team game
\r
825 // find out what teams are available
\r
826 CheckAllowedTeams(pl);
\r
828 // if we don't care what team he ends up on, put him on whatever team he entered as.
\r
829 // if he's not on a valid team, then let other code put him on the smallest team
\r
832 if( c1 >= 0 && pl.team == COLOR_TEAM1)
\r
833 selectedteam = pl.team;
\r
834 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
\r
835 selectedteam = pl.team;
\r
836 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
\r
837 selectedteam = pl.team;
\r
838 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
\r
839 selectedteam = pl.team;
\r
843 if(selectedteam > 0)
\r
845 if(!only_return_best)
\r
847 SetPlayerColors(pl, selectedteam - 1);
\r
849 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
\r
850 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
\r
851 LogTeamchange(pl.playerid, pl.team, 99);
\r
853 return selectedteam;
\r
855 // otherwise end up on the smallest team (handled below)
\r
858 smallest = FindSmallestTeam(pl, TRUE);
\r
860 if(!only_return_best)
\r
862 TeamchangeFrags(self);
\r
865 SetPlayerColors(pl, COLOR_TEAM1 - 1);
\r
867 else if(smallest == 2)
\r
869 SetPlayerColors(pl, COLOR_TEAM2 - 1);
\r
871 else if(smallest == 3)
\r
873 SetPlayerColors(pl, COLOR_TEAM3 - 1);
\r
875 else if(smallest == 4)
\r
877 SetPlayerColors(pl, COLOR_TEAM4 - 1);
\r
881 error("smallest team: invalid team\n");
\r
884 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
\r
886 if(pl.deadflag == DEAD_NO)
\r
887 Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
\r
893 //void() ctf_playerchanged;
\r
894 void SV_ChangeTeam(float _color)
\r
896 float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
\r
898 // in normal deathmatch we can just apply the color and we're done
\r
899 if(!teams_matter) {
\r
900 SetPlayerColors(self, _color);
\r
904 scolor = self.clientcolors & 0x0F;
\r
905 dcolor = _color & 0x0F;
\r
907 if(scolor == COLOR_TEAM1 - 1)
\r
909 else if(scolor == COLOR_TEAM2 - 1)
\r
911 else if(scolor == COLOR_TEAM3 - 1)
\r
913 else // if(scolor == COLOR_TEAM4 - 1)
\r
915 if(dcolor == COLOR_TEAM1 - 1)
\r
917 else if(dcolor == COLOR_TEAM2 - 1)
\r
919 else if(dcolor == COLOR_TEAM3 - 1)
\r
921 else // if(dcolor == COLOR_TEAM4 - 1)
\r
924 CheckAllowedTeams(self);
\r
926 if(dteam == 1 && c1 < 0) dteam = 4;
\r
927 if(dteam == 4 && c4 < 0) dteam = 3;
\r
928 if(dteam == 3 && c3 < 0) dteam = 2;
\r
929 if(dteam == 2 && c2 < 0) dteam = 1;
\r
931 // not changing teams
\r
932 if(scolor == dcolor)
\r
934 //bprint("same team change\n");
\r
935 SetPlayerTeam(self, dteam, steam, TRUE);
\r
939 if((cvar("g_campaign")) || (cvar("g_changeteam_banned") && self.wasplayer)) {
\r
940 sprint(self, "Team changes not allowed\n");
\r
941 return; // changing teams is not allowed
\r
944 if(cvar("g_balance_teams_prevent_imbalance"))
\r
946 // only allow changing to a smaller or equal size team
\r
948 // find out what teams are available
\r
949 //CheckAllowedTeams();
\r
950 // count how many players on each team
\r
951 GetTeamCounts(world);
\r
953 // get desired team
\r
954 if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
\r
959 else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
\r
964 else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
\r
969 else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
\r
976 sprint(self, "Cannot change to an invalid team\n");
\r
981 // get starting team
\r
982 if(steam == 1)//scolor == COLOR_TEAM1 - 1)
\r
984 else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
\r
986 else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
\r
988 else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
\r
991 if(scount) // started at a valid, nonempty team
\r
993 // check if we're trying to change to a larger team that doens't have bots to swap with
\r
994 if(dcount >= scount && dbotcount <= 0)
\r
996 sprint(self, "Cannot change to a larger team\n");
\r
997 return; // can't change to a larger team
\r
1002 // bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
\r
1004 if(self.classname == "player" && steam != dteam)
\r
1006 // reduce frags during a team change
\r
1007 TeamchangeFrags(self);
\r
1010 SetPlayerTeam(self, dteam, steam, FALSE);
\r
1012 if(self.classname == "player" && steam != dteam)
\r
1014 // kill player when changing teams
\r
1015 if(self.deadflag == DEAD_NO)
\r
1016 Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
\r
1018 //ctf_playerchanged();
\r
1021 void ShufflePlayerOutOfTeam (float source_team)
\r
1023 float smallestteam, smallestteam_count, steam;
\r
1024 float lowest_bot_score, lowest_player_score;
\r
1025 entity head, lowest_bot, lowest_player, selected;
\r
1028 smallestteam_count = 999999999;
\r
1030 if(c1 >= 0 && c1 < smallestteam_count)
\r
1033 smallestteam_count = c1;
\r
1035 if(c2 >= 0 && c2 < smallestteam_count)
\r
1038 smallestteam_count = c2;
\r
1040 if(c3 >= 0 && c3 < smallestteam_count)
\r
1043 smallestteam_count = c3;
\r
1045 if(c4 >= 0 && c4 < smallestteam_count)
\r
1048 smallestteam_count = c4;
\r
1053 bprint("warning: no smallest team\n");
\r
1057 if(source_team == 1)
\r
1058 steam = COLOR_TEAM1;
\r
1059 else if(source_team == 2)
\r
1060 steam = COLOR_TEAM2;
\r
1061 else if(source_team == 3)
\r
1062 steam = COLOR_TEAM3;
\r
1063 else if(source_team == 4)
\r
1064 steam = COLOR_TEAM4;
\r
1066 lowest_bot = world;
\r
1067 lowest_bot_score = 999999999;
\r
1068 lowest_player = world;
\r
1069 lowest_player_score = 999999999;
\r
1071 // find the lowest-scoring player & bot of that team
\r
1072 FOR_EACH_PLAYER(head)
\r
1074 if(head.team == steam)
\r
1078 if(head.totalfrags < lowest_bot_score)
\r
1080 lowest_bot = head;
\r
1081 lowest_bot_score = head.totalfrags;
\r
1086 if(head.totalfrags < lowest_player_score)
\r
1088 lowest_player = head;
\r
1089 lowest_player_score = head.totalfrags;
\r
1095 // prefers to move a bot...
\r
1096 if(lowest_bot != world)
\r
1097 selected = lowest_bot;
\r
1098 // but it will move a player if it has to
\r
1100 selected = lowest_player;
\r
1101 // don't do anything if it couldn't find anyone
\r
1104 bprint("warning: couldn't find a player to move from team\n");
\r
1108 // smallest team gains a member
\r
1109 if(smallestteam == 1)
\r
1113 else if(smallestteam == 2)
\r
1117 else if(smallestteam == 3)
\r
1121 else if(smallestteam == 4)
\r
1127 bprint("warning: destination team invalid\n");
\r
1130 // source team loses a member
\r
1131 if(source_team == 1)
\r
1135 else if(source_team == 2)
\r
1139 else if(source_team == 3)
\r
1143 else if(source_team == 4)
\r
1149 bprint("warning: source team invalid\n");
\r
1153 // move the player to the new team
\r
1154 TeamchangeFrags(selected);
\r
1155 SetPlayerTeam(selected, smallestteam, source_team, FALSE);
\r
1157 if(selected.deadflag == DEAD_NO)
\r
1158 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
\r
1159 centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
\r
1162 void CauseRebalance(float source_team, float howmany_toomany)
\r
1164 if(IsTeamBalanceForced() == 1)
\r
1166 bprint("Rebalancing Teams\n");
\r
1167 ShufflePlayerOutOfTeam(source_team);
\r
1171 // part of g_balance_teams_force
\r
1172 // occasionally perform an audit of the teams to make
\r
1173 // sure they're more or less balanced in player count.
\r
1176 float numplayers, numteams, smallest, toomany;
\r
1178 balance = IsTeamBalanceForced();
\r
1182 if(audit_teams_time > time)
\r
1185 audit_teams_time = time + 4 + random();
\r
1187 // bprint("Auditing teams\n");
\r
1189 CheckAllowedTeams(world);
\r
1190 GetTeamCounts(world);
\r
1193 numteams = numplayers = smallest = 0;
\r
1196 numteams = numteams + 1;
\r
1197 numplayers = numplayers + c1;
\r
1202 numteams = numteams + 1;
\r
1203 numplayers = numplayers + c2;
\r
1209 numteams = numteams + 1;
\r
1210 numplayers = numplayers + c3;
\r
1216 numteams = numteams + 1;
\r
1217 numplayers = numplayers + c4;
\r
1222 if(numplayers <= 0)
\r
1223 return; // no players to move around
\r
1225 return; // don't bother shuffling if for some reason there aren't any teams
\r
1227 toomany = smallest + 1;
\r
1229 if(c1 && c1 > toomany)
\r
1230 CauseRebalance(1, c1 - toomany);
\r
1231 if(c2 && c2 > toomany)
\r
1232 CauseRebalance(2, c2 - toomany);
\r
1233 if(c3 && c3 > toomany)
\r
1234 CauseRebalance(3, c3 - toomany);
\r
1235 if(c4 && c4 > toomany)
\r
1236 CauseRebalance(4, c4 - toomany);
\r
1238 // if teams are still unbalanced, balance them further in the next audit,
\r
1239 // which will happen sooner (keep doing rapid audits until things are in order)
\r
1240 audit_teams_time = time + 0.7 + random()*0.3;
\r
1243 // code from here on is just to support maps that don't have team entities
\r
1244 void tdm_spawnteam (string teamname, float teamcolor)
\r
1248 e.classname = "tdm_team";
\r
1249 e.netname = teamname;
\r
1250 e.cnt = teamcolor;
\r
1251 e.team = e.cnt + 1;
\r
1254 // spawn some default teams if the map is not set up for tdm
\r
1255 void tdm_spawnteams()
\r
1259 numteams = cvar("g_tdm_teams_override");
\r
1261 numteams = cvar("g_tdm_teams");
\r
1262 numteams = bound(2, numteams, 4);
\r
1264 tdm_spawnteam("Red", COLOR_TEAM1-1);
\r
1265 tdm_spawnteam("Blue", COLOR_TEAM2-1);
\r
1267 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
\r
1269 tdm_spawnteam("Pink", COLOR_TEAM4-1);
\r
1272 void tdm_delayedinit()
\r
1274 // if no teams are found, spawn defaults
\r
1275 if (find(world, classname, "tdm_team") == world)
\r
1281 InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);
\r