diff --git a/data/cdata/scripts/mp/team_balance.gsc b/data/cdata/scripts/mp/team_balance.gsc index bf9685da..37e9b798 100644 --- a/data/cdata/scripts/mp/team_balance.gsc +++ b/data/cdata/scripts/mp/team_balance.gsc @@ -1,11 +1,71 @@ -init() +main() { - // define the auto balance string in the game array (referenced in gsc dump, but not defined past IW6?) precachestring(&"MP_AUTOBALANCE_NOW"); - game["strings"]["autobalance"] = &"MP_AUTOBALANCE_NOW"; - // define onteamselection callback function used in balanceteams() - level.onteamselection = ::set_team; + // use player's score to balance instead player's time on team + replacefunc(maps\mp\gametypes\_teams::balanceteams, ::balance_teams_stub); +} + +balance_teams_stub() +{ + iprintlnbold(&"MP_AUTOBALANCE_NOW"); + + allied_players = get_valid_team_array("allies"); + axis_players = get_valid_team_array("axis"); + while (is_team_bigger_than(allied_players, axis_players) || is_team_bigger_than(axis_players, allied_players)) + { + if (is_team_bigger_than(allied_players, axis_players)) + { + handle_lowest_score_player(allied_players, "axis"); + } + else if (is_team_bigger_than(axis_players, team)) + { + handle_lowest_score_player(axis_players, "allies"); + } + + // refresh array for loop + allied_players = get_valid_team_array("allies"); + axis_players = get_valid_team_array("axis"); + } +} + +get_valid_team_array(team) +{ + team_array = []; + players = level.players; + for (i = 0; i < players.size; i++) + { + if (!isdefined(players[i].pers["score"])) // was teamTime + continue; + + if (isdefined(players[i].pers["team"]) && players[i].pers["team"] == team) + team_array[team_array.size] = players[i]; + } + return team_arary; +} + +is_team_bigger_than(team_one, team_two) +{ + return (team_one.size > (team_two.size + 1)); +} + +handle_lowest_score_player(team, new_team) +{ + lowest_score_player = undefined; + + // move the player that has the lowest score (highest teamTime value) + for (i = 0; i < team.size; i++) + { + if (isdefined(team[j].dont_auto_balance)) + continue; + + if (!isdefined(lowest_score_player)) + lowest_score_player = team[j]; + else if (team[j].pers["score"] < lowest_score_player.pers["score"]) + lowest_score_player = team[j]; + } + + lowest_score_player set_team(new_team); } set_team(team) @@ -24,6 +84,4 @@ set_team(team) self maps\mp\gametypes\_menus::addtoteam(team); self maps\mp\gametypes\_menus::endrespawnnotify(); - - self maps\mp\gametypes\_teams::updateteamtime(); // defines pers["teamTime"] for balancing }