コード例 #1
0
ファイル: battle.cpp プロジェクト: tvo/springlobby
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 );
        }
    }
}
コード例 #2
0
unsigned int LegacyCentrality::GetCentralityClass(const evt::Event &event, std::vector<int> *centralityClass){
  bool isMC = event.GetSimEvent().HasMainVertex();
  if (fCentralityEdges.empty()){
    double defaultCentrality[4] = {0.05, 0.1, 0.15, 0.2};
    fCentralityEdges.assign(defaultCentrality, defaultCentrality + 4);
  }
  
  if (!fCentralityGraph8s0l) Init(event);
  
  //                        0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22
  double weights8s0l[45] = {0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  //                         23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44
                            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  
  //                        0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22
  double weights8s2l[45] = {0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  //                         23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44
                            0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  
  //                        0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22
  double weights8s4l[45] = {0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0,
  //                         23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44
                            0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  
  //                          0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22
  double weights16s12l[45] = {0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
  //                         23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44
                            1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  
  double moduleEnergy[45];
  double psdEnergy[4] = {0, 0, 0, 0};
  if (!isMC){
    for (int i = 0; i < 45; ++i){
      moduleEnergy[i] = 0;
      if (event.GetRecEvent().GetPSD().HasModule(i)){
        evt::rec::PSDModule module = event.GetRecEvent().GetPSD().GetModule(i);
        moduleEnergy[i] = module.GetEnergy();
      }
    }
  } else {
    std::vector< evt::Index<evt::sim::VertexTrack> > spectatorList;
    GetSpectators(event, spectatorList);
    PSDSimulation(event, moduleEnergy, spectatorList);
  }
  
  for (int i = 0; i < 45; ++i){
    psdEnergy[0] += moduleEnergy[i] * weights8s0l[i];
    psdEnergy[1] += moduleEnergy[i] * weights8s2l[i];
    psdEnergy[2] += moduleEnergy[i] * weights8s4l[i];
    psdEnergy[3] += moduleEnergy[i] * weights16s12l[i];
  }
  
  double cent8s0l   = fCentralityGraph8s0l->Eval(psdEnergy[0]);
  double cent8s2l   = fCentralityGraph8s2l->Eval(psdEnergy[1]);
  double cent8s4l   = fCentralityGraph8s4l->Eval(psdEnergy[2]);
  double cent16s12l = fCentralityGraph16s12l->Eval(psdEnergy[3]);
  
  unsigned int centralityClass8s0l = 1;
  unsigned int centralityClass8s2l = 1;
  unsigned int centralityClass8s4l = 1;
  unsigned int centralityClass16s12l = 1;
  
  bool done8s0l = false;
  bool done8s2l = false;
  bool done8s4l = false;
  bool done16s12l = false;
  
  for(std::vector<double>::iterator it  = fCentralityEdges.begin();
                                    it != fCentralityEdges.end();
                                  ++it){
    if (cent8s0l < *it) done8s0l = true;
    if (cent8s2l < *it) done8s2l = true;
    if (cent8s4l < *it) done8s4l = true;
    if (cent16s12l < *it) done16s12l = true;
    if (!done8s0l) centralityClass8s0l++;
    if (!done8s2l) centralityClass8s2l++;
    if (!done8s4l) centralityClass8s4l++;
    if (!done16s12l) centralityClass16s12l++;
  }
  
  if (centralityClass){
    centralityClass->push_back(centralityClass8s0l);
    centralityClass->push_back(centralityClass8s2l);
    centralityClass->push_back(centralityClass8s4l);
    centralityClass->push_back(centralityClass16s12l);
  }
  
  return centralityClass16s12l;
}