void Battle::OnUserBattleStatusUpdated( User &user, UserBattleStatus status ) { if ( IsFounderMe() ) { if ( ( &user != &GetMe() ) && !status.IsBot() && ( m_opts.rankneeded != UserStatus::RANK_1 ) && !status.spectator ) { if ( m_opts.rankneeded > UserStatus::RANK_1 && user.GetStatus().rank < m_opts.rankneeded ) { DoAction( _T("Rank limit autospec: ") + user.GetNick() ); ForceSpectator( user, true ); } else if ( m_opts.rankneeded < UserStatus::RANK_1 && user.GetStatus().rank > ( -m_opts.rankneeded - 1 ) ) { DoAction( _T("Rank limit autospec: ") + user.GetNick() ); ForceSpectator( user, true ); } } UserBattleStatus previousstatus = user.BattleStatus(); if ( m_opts.lockexternalbalancechanges ) { if ( previousstatus.team != status.team ) { ForceTeam( user, previousstatus.team ); status.team = previousstatus.team; } if ( previousstatus.ally != status.ally ) { ForceAlly( user, previousstatus.ally ); status.ally = previousstatus.ally; } } } IBattle::OnUserBattleStatusUpdated( user, status ); if ( status.handicap != 0 ) { UiEvents::GetUiEventSender( UiEvents::OnBattleActionEvent ).SendEvent( UiEvents::OnBattleActionData( wxString(_T(" ")) , ( _T("Warning: user ") + user.GetNick() + _T(" got bonus ") ) << status.handicap ) ); } if ( IsFounderMe() ) { if ( ShouldAutoStart() ) { if ( sett().GetBattleLastAutoStartState() ) { if ( !spring().IsRunning() ) StartHostedBattle(); } } } if ( !GetMe().BattleStatus().spectator ) SetAutoUnspec(false); // we don't need auto unspec anymore ShouldAutoUnspec(); #ifndef SL_QT_MODE ui().OnUserBattleStatus( *this, user ); #endif }
void Battle::FixTeamIDs( BalanceType balance_type, bool support_clans, bool strong_clans, int numcontrolteams ) { wxLogMessage(_T("Autobalancing teams, type=%d, clans=%d, strong_clans=%d, numcontrolteams=%d"),balance_type, support_clans, strong_clans, numcontrolteams); //size_t i; //int num_alliances; std::vector<ControlTeam> control_teams; if ( numcontrolteams == 0 || numcontrolteams == -1 ) numcontrolteams = GetNumUsers() - GetSpectators(); // 0 or -1 -> use num players, will use comshare only if no available team slots IBattle::StartType position_type = (IBattle::StartType)s2l( CustomBattleOptions().getSingleValue( _T("startpostype"), OptionsWrapper::EngineOption ) ); if ( ( position_type == ST_Fixed ) || ( position_type == ST_Random ) ) // if fixed start pos type or random, use max teams = start pos count { try { int mapposcount = LoadMap().info.positions.size(); numcontrolteams = std::min( numcontrolteams, mapposcount ); } catch( assert_exception ) {} } if ( numcontrolteams >= (int)( GetNumUsers() - GetSpectators() ) ) // autobalance behaves weird when trying to put one player per team and i CBA to fix it, so i'll reuse the old code :P { // apparently tasserver doesnt like when i fix/force ids of everyone. std::set<int> allteams; size_t numusers = GetNumUsers(); for( size_t i = 0; i < numusers; ++i ) { User &user = GetUser(i); if( !user.BattleStatus().spectator ) allteams.insert( user.BattleStatus().team ); } std::set<int> teams; int t = 0; for( size_t i = 0; i < GetNumUsers(); ++i ) { User &user = GetUser(i); if( !user.BattleStatus().spectator ) { if( teams.count( user.BattleStatus().team ) ) { while( allteams.count(t) || teams.count( t ) ) t++; ForceTeam( GetUser(i), t ); teams.insert( t ); } else { teams.insert( user.BattleStatus().team ); } } } return; } for ( int i = 0; i < numcontrolteams; i++ ) control_teams.push_back( ControlTeam( i ) ); wxLogMessage(_T("number of teams: %u"), control_teams.size() ); std::vector<User*> players_sorted; players_sorted.reserve( GetNumUsers() ); int player_team_counter = 0; for ( size_t i = 0; i < GetNumUsers(); ++i ) // don't count spectators { if ( !GetUser(i).BattleStatus().spectator ) { players_sorted.push_back( &GetUser(i) ); // -- server fail? it doesnt work right. //ForceTeam(GetUser(i),player_team_counter); player_team_counter++; } } shuffle( players_sorted ); std::map<wxString, ControlTeam> clan_teams; if ( support_clans ) { for ( size_t i = 0; i < players_sorted.size(); ++i ) { wxString clan = players_sorted[i]->GetClan(); if ( !clan.empty() ) { clan_teams[clan].AddPlayer( players_sorted[i] ); } } }; if ( balance_type != balance_random ) std::sort( players_sorted.begin(), players_sorted.end(), PlayerRankCompareFunction ); if ( support_clans ) { std::map<wxString, ControlTeam>::iterator clan_it = clan_teams.begin(); while ( clan_it != clan_teams.end() ) { ControlTeam &clan = (*clan_it).second; // if clan is too small (only 1 clan member in battle) or too big, dont count it as clan if ( ( clan.players.size() < 2 ) || ( !strong_clans && ( clan.players.size() > ( ( players_sorted.size() + control_teams.size() -1 ) / control_teams.size() ) ) ) ) { wxLogMessage(_T("removing clan %s"),(*clan_it).first.c_str()); std::map<wxString, ControlTeam>::iterator next = clan_it; ++next; clan_teams.erase( clan_it ); clan_it = next; continue; } wxLogMessage( _T("Inserting clan %s"), (*clan_it).first.c_str() ); std::sort( control_teams.begin(), control_teams.end() ); float lowestrank = control_teams[0].ranksum; int rnd_k = 1; // number of alliances with rank equal to lowestrank while ( size_t( rnd_k ) < control_teams.size() ) { if ( fabs( control_teams[rnd_k].ranksum - lowestrank ) > 0.01 ) break; rnd_k++; } wxLogMessage(_T("number of lowestrank teams with same rank=%d"), rnd_k ); control_teams[my_random( rnd_k )].AddTeam( clan ); ++clan_it; } } for (size_t i = 0; i < players_sorted.size(); ++i ) { // skip clanners, those have been added already. if ( clan_teams.count( players_sorted[i]->GetClan() ) > 0 ) { wxLogMessage( _T("clanner already added, nick=%s"),players_sorted[i]->GetNick().c_str() ); continue; } // find teams with lowest ranksum // insert current user into random one out of them // since performance doesnt matter here, i simply sort teams, // then find how many teams in beginning have lowest ranksum // note that balance player ranks range from 1 to 1.1 now // i.e. them are quasi equal // so we're essentially adding to teams with smallest number of players, // the one with smallest ranksum. std::sort( control_teams.begin(), control_teams.end() ); float lowestrank = control_teams[0].ranksum; int rnd_k = 1; // number of alliances with rank equal to lowestrank while ( size_t( rnd_k ) < control_teams.size() ) { if ( fabs ( control_teams[rnd_k].ranksum - lowestrank ) > 0.01 ) break; rnd_k++; } wxLogMessage( _T("number of lowestrank teams with same rank=%d"), rnd_k ); control_teams[my_random( rnd_k )].AddPlayer( players_sorted[i] ); } for ( size_t i=0; i < control_teams.size(); ++i ) { for ( size_t j = 0; j < control_teams[i].players.size(); ++j ) { ASSERT_LOGIC( control_teams[i].players[j], _T("fail in Autobalance teams, NULL player") ); wxString msg = wxString::Format( _T("setting player %s to team and ally %d"), control_teams[i].players[j]->GetNick().c_str(), i ); wxLogMessage( _T("%s"), msg.c_str() ); ForceTeam( *control_teams[i].players[j], control_teams[i].teamnum ); ForceAlly( *control_teams[i].players[j], control_teams[i].teamnum ); } } }
void Battle::Autobalance( BalanceType balance_type, bool support_clans, bool strong_clans, int numallyteams ) { wxLogMessage(_T("Autobalancing alliances, type=%d, clans=%d, strong_clans=%d, numallyteams=%d"),balance_type, support_clans,strong_clans, numallyteams); //size_t i; //int num_alliances; std::vector<Alliance>alliances; if ( numallyteams == 0 || numallyteams == -1 ) // 0 or 1 -> use num start rects { int ally = 0; for ( unsigned int i = 0; i < GetNumRects(); ++i ) { BattleStartRect sr = GetStartRect(i); if ( sr.IsOk() ) { ally=i; alliances.push_back( Alliance( ally ) ); ally++; } } // make at least two alliances while ( alliances.size() < 2 ) { alliances.push_back( Alliance( ally ) ); ally++; } } else { for ( int i = 0; i < numallyteams; i++ ) alliances.push_back( Alliance( i ) ); } //for(i=0;i<alliances.size();++i)alliances[i].allynum=i; wxLogMessage( _T("number of alliances: %u"), alliances.size() ); std::vector<User*> players_sorted; players_sorted.reserve( GetNumUsers() ); for ( size_t i = 0; i < GetNumUsers(); ++i ) { User& usr = GetUser( i ); if ( !usr.BattleStatus().spectator ) { players_sorted.push_back( &usr ); } } // remove players in the same team so only one remains std::map< int, User*> dedupe_teams; for ( std::vector<User*>::iterator it = players_sorted.begin(); it != players_sorted.end(); it++ ) { dedupe_teams[(*it)->BattleStatus().team] = *it; } players_sorted.clear(); players_sorted.reserve( dedupe_teams.size() ); for ( std::map<int, User*>::iterator it = dedupe_teams.begin(); it != dedupe_teams.end(); it++ ) { players_sorted.push_back( it->second ); } shuffle( players_sorted ); std::map<wxString, Alliance> clan_alliances; if ( support_clans ) { for ( size_t i=0; i < players_sorted.size(); ++i ) { wxString clan = players_sorted[i]->GetClan(); if ( !clan.empty() ) { clan_alliances[clan].AddPlayer( players_sorted[i] ); } } }; if ( balance_type != balance_random ) std::sort( players_sorted.begin(), players_sorted.end(), PlayerRankCompareFunction ); if ( support_clans ) { std::map<wxString, Alliance>::iterator clan_it = clan_alliances.begin(); while ( clan_it != clan_alliances.end() ) { Alliance &clan = (*clan_it).second; // if clan is too small (only 1 clan member in battle) or too big, dont count it as clan if ( ( clan.players.size() < 2 ) || ( !strong_clans && ( clan.players.size() > ( ( players_sorted.size() + alliances.size() -1 ) / alliances.size() ) ) ) ) { wxLogMessage( _T("removing clan %s"), (*clan_it).first.c_str() ); std::map<wxString, Alliance>::iterator next = clan_it; ++next; clan_alliances.erase( clan_it ); clan_it = next; continue; } wxLogMessage( _T("Inserting clan %s"), (*clan_it).first.c_str() ); std::sort( alliances.begin(), alliances.end() ); float lowestrank = alliances[0].ranksum; int rnd_k = 1;// number of alliances with rank equal to lowestrank while ( size_t( rnd_k ) < alliances.size() ) { if ( fabs( alliances[rnd_k].ranksum - lowestrank ) > 0.01 ) break; rnd_k++; } wxLogMessage( _T("number of lowestrank alliances with same rank=%d"), rnd_k ); alliances[my_random( rnd_k )].AddAlliance( clan ); ++clan_it; } } for ( size_t i = 0; i < players_sorted.size(); ++i ) { // skip clanners, those have been added already. if ( clan_alliances.count( players_sorted[i]->GetClan() ) > 0 ) { wxLogMessage( _T("clanner already added, nick=%s"), players_sorted[i]->GetNick().c_str() ); continue; } // find alliances with lowest ranksum // insert current user into random one out of them // since performance doesnt matter here, i simply sort alliances, // then find how many alliances in beginning have lowest ranksum // note that balance player ranks range from 1 to 1.1 now // i.e. them are quasi equal // so we're essentially adding to alliance with smallest number of players, // the one with smallest ranksum. std::sort( alliances.begin(), alliances.end() ); float lowestrank = alliances[0].ranksum; int rnd_k = 1;// number of alliances with rank equal to lowestrank while ( size_t( rnd_k ) < alliances.size() ) { if ( fabs( alliances[rnd_k].ranksum - lowestrank ) > 0.01 ) break; rnd_k++; } wxLogMessage( _T("number of lowestrank alliances with same rank=%d"), rnd_k ); alliances[my_random( rnd_k )].AddPlayer( players_sorted[i] ); } UserList::user_map_t::size_type totalplayers = GetNumUsers(); for ( size_t i = 0; i < alliances.size(); ++i ) { for ( size_t j = 0; j < alliances[i].players.size(); ++j ) { ASSERT_LOGIC( alliances[i].players[j], _T("fail in Autobalance, NULL player") ); int balanceteam = alliances[i].players[j]->BattleStatus().team; wxLogMessage( _T("setting team %d to alliance %d"), balanceteam, i ); for ( size_t h = 0; h < totalplayers; h++ ) // change ally num of all players in the team { User& usr = GetUser( h ); if ( usr.BattleStatus().team == balanceteam ) ForceAlly( usr, alliances[i].allynum ); } } } }