Exemplo n.º 1
0
void IBattle::SaveOptionsPreset( const wxString& name )
{
  m_preset = FixPresetName(name);
  if (m_preset == _T("")) m_preset = name; //new preset

  for ( int i = 0; i < (int)OptionsWrapper::LastOption; i++)
  {
    if ( (OptionsWrapper::GameOption)i != OptionsWrapper::PrivateOptions )
    {
      sett().SetHostingPreset( m_preset, (OptionsWrapper::GameOption)i, CustomBattleOptions().getOptionsMap( (OptionsWrapper::GameOption)i ) );
    }
    else
    {
      std::map<wxString,wxString> opts;
      opts[_T("mapname")] = GetHostMapName();
      unsigned int validrectcount = 0;
      if ( s2l (CustomBattleOptions().getSingleValue( _T("startpostype"), OptionsWrapper::EngineOption ) ) == ST_Choose )
      {
        unsigned int boxcount = GetLastRectIdx();
        for ( unsigned int boxnum = 0; boxnum <= boxcount; boxnum++ )
        {
          BattleStartRect rect = GetStartRect( boxnum );
          if ( rect.IsOk() )
          {
            opts[_T("rect_") + TowxString(validrectcount) + _T("_ally")] = TowxString( rect.ally + 1 );
            opts[_T("rect_") + TowxString(validrectcount) + _T("_left")] = TowxString( rect.left );
            opts[_T("rect_") + TowxString(validrectcount) + _T("_top")] = TowxString( rect.top );
            opts[_T("rect_") + TowxString(validrectcount) + _T("_bottom")] = TowxString( rect.bottom );
            opts[_T("rect_") + TowxString(validrectcount) + _T("_right")] = TowxString( rect.right );
            validrectcount++;
          }
        }
      }
      opts[_T("numrects")] = TowxString( validrectcount );

      wxString restrictionsstring;
	  for ( std::map<wxString, int>::const_iterator itor = m_restricted_units.begin(); itor != m_restricted_units.end(); ++itor )
      {
        restrictionsstring << itor->first << _T('=') << TowxString(itor->second) << _T('\t');
      }
      opts[_T("restrictions")] = restrictionsstring;

      sett().SetHostingPreset( m_preset, (OptionsWrapper::GameOption)i, opts );
    }
  }
  sett().SaveSettings();
  ui().ReloadPresetList();
}
Exemplo n.º 2
0
void SinglePlayerBattle::SendHostInfo( HostInfo update )
{
  if ( (update & HI_StartType) != 0 ) m_sptab.UpdateMinimap();
  if ( (update & HI_Restrictions) != 0 ) m_sptab.ReloadRestrictions();
  if ( (update & HI_Map_Changed) != 0 )
  {
	LoadMap();
    m_sptab.ReloadMapOptContrls();
	Update(  wxFormat(_T("%d_%s") ) % LSL::OptionsWrapper::PrivateOptions % _T("mapname") );
  }
  if ( (update & HI_Mod_Changed) != 0 )
  {
    RemoveUnfittingBots();
	LoadMod();
    wxString presetname = sett().GetModDefaultPresetName( GetHostModName() );
    if ( !presetname.IsEmpty() )
    {
      LoadOptionsPreset( presetname );
      SendHostInfo( HI_Send_All_opts );
    }
    m_sptab.ReloadModOptContrls();
	Update(  wxFormat(_T("%d_%s") ) % LSL::OptionsWrapper::PrivateOptions % _T("modname") );
  }
  if ( (update & HI_Send_All_opts) != 0 )
  {
    for ( int i = 0; i < (int)LSL::OptionsWrapper::LastOption; i++)
    {
      for (const auto pair : CustomBattleOptions().getOptionsMap( (LSL::OptionsWrapper::GameOption)i))
      {
        Update(  wxFormat(_T("%d_%s") ) % i % pair.first );
      }
    }
  }
}
void SinglePlayerBattle::SendHostInfo( HostInfo update )
{
  if ( (update & HI_StartType) != 0 ) m_sptab.UpdateMinimap();
  if ( (update & HI_Restrictions) != 0 ) m_sptab.ReloadRestrictions();
  if ( (update & HI_Map_Changed) != 0 )
  {
	LoadMap();
    m_sptab.ReloadMapOptContrls();
	Update(  wxFormat(_T("%d_%s") ) % OptionsWrapper::PrivateOptions % _T("mapname") );
  }
  if ( (update & HI_Mod_Changed) != 0 )
  {
    RemoveUnfittingBots();
	LoadMod();
    wxString presetname = sett().GetModDefaultPresetName( GetHostModName() );
    if ( !presetname.IsEmpty() )
    {
      LoadOptionsPreset( presetname );
      SendHostInfo( HI_Send_All_opts );
    }
    m_sptab.ReloadModOptContrls();
	Update(  wxFormat(_T("%d_%s") ) % OptionsWrapper::PrivateOptions % _T("modname") );
  }
  if ( (update & HI_Send_All_opts) != 0 )
  {
    for ( int i = 0; i < (int)OptionsWrapper::LastOption; i++)
    {
      const std::map<wxString,wxString>& options = CustomBattleOptions().getOptionsMap( (OptionsWrapper::GameOption)i );
      for ( std::map<wxString,wxString>::const_iterator itor = options.begin(); itor != options.end(); ++itor )
      {
		Update(  wxFormat(_T("%d_%s") ) % i % itor->first );
      }
    }
  }
}
SinglePlayerBattle::SinglePlayerBattle( MainSinglePlayerTab& msptab ):
  m_sptab(msptab),
  m_me( User( usync().IsLoaded() ? usync().GetDefaultNick() : _T("invalid") ) )
{
	OnUserAdded( m_me );
	m_me.BattleStatus().side = sett().GetBattleLastSideSel( GetHostModName() );
	m_me.BattleStatus().colour = sett().GetBattleLastColour();
	CustomBattleOptions().setSingleOption( _T("startpostype"), wxFormat(_T("%d") ) % ST_Pick, OptionsWrapper::EngineOption );
}
Exemplo n.º 5
0
void IBattle::LoadScriptMMOpts( const LSL::TDF::PDataList& node )
{
	if ( !node.ok() ) return;
	LSL::OptionsWrapper& opts = CustomBattleOptions();
	auto options = opts.getOptionsMap(LSL::OptionsWrapper::EngineOption);
for (const auto i : options) {
		opts.setSingleOption( i.first, node->GetString( i.first, i.second));
	}
}
SinglePlayerBattle::SinglePlayerBattle(MainSinglePlayerTab& msptab)
    : m_sptab(msptab)
    , m_me(STD_STRING(cfg().ReadString("/Spring/DefaultName")))
{
	OnUserAdded(m_me);
	m_me.BattleStatus().side = sett().GetBattleLastSideSel(TowxString(GetHostGameName()));
	m_me.BattleStatus().colour = wxColourTolsl(sett().GetBattleLastColour());
	CustomBattleOptions().setSingleOption("startpostype", LSL::Util::ToIntString(ST_Pick), LSL::Enum::EngineOption);
}
Exemplo n.º 7
0
bool IBattle::LoadOptionsPreset( const std::string& name )
{
	const std::string preset = FixPresetName(name);
	if (preset.empty()) return false; //preset not found
	m_preset = preset;

	for ( unsigned int i = 0; i < LSL::OptionsWrapper::LastOption; i++) {
		std::map<wxString,wxString> options = sett().GetHostingPreset( TowxString(m_preset), i );
		if ( (LSL::OptionsWrapper::GameOption)i != LSL::OptionsWrapper::PrivateOptions ) {
			for ( std::map<wxString,wxString>::const_iterator itor = options.begin(); itor != options.end(); ++itor ) {
				wxLogWarning( itor->first + _T(" ::: ") + itor->second );
				CustomBattleOptions().setSingleOption( STD_STRING(itor->first),
								       STD_STRING(itor->second),
								       (LSL::OptionsWrapper::GameOption)i );
			}
		} else {
			if ( !options[_T("mapname")].IsEmpty() ) {
				if (LSL::usync().MapExists(STD_STRING(options[_T("mapname")]))) {
					SetLocalMap( STD_STRING(options[_T("mapname")]) );
					SendHostInfo( HI_Map );
				} else if ( !ui().OnPresetRequiringMap( options[_T("mapname")] ) ) {
					//user didn't want to download the missing map, so set to empty to not have it tried to be loaded again
					options[_T("mapname")] = wxEmptyString;
					sett().SetHostingPreset( TowxString(m_preset), i, options );
				}
			}

			for( unsigned int j = 0; j <= GetLastRectIdx(); ++j ) {
				if ( GetStartRect( j ).IsOk() )
					RemoveStartRect(j); // remove all rects that might come from map presets
			}
			SendHostInfo( IBattle::HI_StartRects );

			unsigned int rectcount = s2l( options[_T("numrects")] );
			for ( unsigned int loadrect = 0; loadrect < rectcount; loadrect++) {
				int ally = s2l(options[_T("rect_") + TowxString(loadrect) + _T("_ally")]);
				if ( ally == 0 ) continue;
				AddStartRect( ally - 1, s2l(options[_T("rect_") + TowxString(loadrect) + _T("_left")]), s2l(options[_T("rect_") + TowxString(loadrect) + _T("_top")]), s2l(options[_T("rect_") + TowxString(loadrect) + _T("_right")]), s2l(options[_T("rect_") + TowxString(loadrect) + _T("_bottom")]) );
			}
			SendHostInfo( HI_StartRects );

			wxStringTokenizer tkr( options[_T("restrictions")], _T('\t') );
			m_restricted_units.clear();
			while( tkr.HasMoreTokens() ) {
				wxString unitinfo = tkr.GetNextToken();
				RestrictUnit( STD_STRING(unitinfo.BeforeLast(_T('='))), s2l( unitinfo.AfterLast(_T('=')) ) );
			}
			SendHostInfo( HI_Restrictions );
			Update( wxFormat( _T("%d_restrictions") ) % LSL::OptionsWrapper::PrivateOptions );

		}
	}
	SendHostInfo( HI_Send_All_opts );
	ui().ReloadPresetList();
	return true;
}
Exemplo n.º 8
0
SinglePlayerBattle::SinglePlayerBattle( MainSinglePlayerTab& msptab ):
  m_sptab(msptab),
  m_me( User(sett().GetDefaultNick()))
{
	OnUserAdded( m_me );
	m_me.BattleStatus().side = sett().GetBattleLastSideSel( GetHostModName() );
	m_me.BattleStatus().colour = sett().GetBattleLastColour();
    CustomBattleOptions().setSingleOption( "startpostype", LSL::Util::ToString(ST_Pick), LSL::OptionsWrapper::EngineOption );
	ConnectGlobalEvent(this, GlobalEvent::OnUnitsyncReloaded, wxObjectEventFunction(&SinglePlayerBattle::OnUnitsyncReloaded));
}
Exemplo n.º 9
0
void IBattle::LoadScriptMMOpts( const std::string& sectionname, const LSL::TDF::PDataList& node )
{
	if ( !node.ok() ) return;
	LSL::TDF::PDataList section ( node->Find(sectionname) );
	if ( !section.ok() ) return;
	LSL::OptionsWrapper& opts = CustomBattleOptions();
	for ( LSL::TDF::PNode n = section->First(); n != section->Last(); n = section->Next( n ) ) {
		if ( !n.ok() ) continue;
		opts.setSingleOption( n->Name(), section->GetString( n->Name() ) );
	}
}
Exemplo n.º 10
0
void IBattle::LoadScriptMMOpts( const SL::PDataList& node )
{
	if ( !node.ok() ) return;
    OptionsWrapper& opts = CustomBattleOptions();
    typedef std::map<wxString,wxString> optMap;
    optMap options = opts.getOptionsMap(OptionsWrapper::EngineOption);
    for ( optMap::const_iterator i = options.begin(); i != options.end(); ++i)
    {
        opts.setSingleOption( i->first, node->GetString( i->first, i->second ) );
    }
}
Exemplo n.º 11
0
const LSL::UnitsyncMod& IBattle::LoadMod()
{
	assert(!m_host_mod.name.empty());
	if ( !m_mod_loaded ) {
		try {
			ASSERT_EXCEPTION( ModExists(), _T("Mod does not exist.") );
			m_local_mod = LSL::usync().GetMod( m_host_mod.name );
			bool options_loaded = CustomBattleOptions().loadOptions( LSL::OptionsWrapper::ModOption, m_host_mod.name );
			ASSERT_EXCEPTION( options_loaded, _T("couldn't load the mod options") );
			m_mod_loaded = true;
		} catch (...) {}
	}
	return m_local_mod;
}
Exemplo n.º 12
0
const LSL::UnitsyncMap& IBattle::LoadMap()
{
	if (( !m_map_loaded ) && (!m_host_map.name.empty())) {
		try {
			ASSERT_EXCEPTION( MapExists(), _T("Map does not exist: ") + TowxString(m_host_map.name) );
			m_local_map = LSL::usync().GetMap( m_host_map.name );
			bool options_loaded = CustomBattleOptions().loadOptions( LSL::OptionsWrapper::MapOption, m_host_map.name );
			ASSERT_EXCEPTION( options_loaded, _T("couldn't load the map options") );
			m_map_loaded = true;

		} catch (...) {}
	}
	return m_local_map;
}
Exemplo n.º 13
0
const UnitSyncMod& IBattle::LoadMod()
{
  if ( !m_mod_loaded )
   {
    try {
      ASSERT_EXCEPTION( m_mod_exists, _T("Mod does not exist.") );
      m_local_mod = usync().GetMod( m_host_mod.name );
	  bool options_loaded = CustomBattleOptions().loadOptions( OptionsWrapper::ModOption, m_host_mod.name );
	  ASSERT_EXCEPTION( options_loaded, _T("couldn't load the mod options") );
      m_mod_loaded = true;
    } catch (...) {}
  }
  return m_local_mod;
}
Exemplo n.º 14
0
void Battle::LoadMapDefaults( const wxString& mapname )
{
	CustomBattleOptions().setSingleOption( _T("startpostype"), sett().GetMapLastStartPosType( mapname ), OptionsWrapper::EngineOption );
	SendHostInfo( wxString::Format( _T("%d_startpostype"), OptionsWrapper::EngineOption ) );

	for( unsigned int i = 0; i <= GetLastRectIdx(); ++i ) if ( GetStartRect( i ).IsOk() ) RemoveStartRect(i); // remove all rects
	SendHostInfo( IBattle::HI_StartRects );

	std::vector<Settings::SettStartBox> savedrects = sett().GetMapLastRectPreset( mapname );
	for ( std::vector<Settings::SettStartBox>::iterator itor = savedrects.begin(); itor != savedrects.end(); itor++ )
	{
		AddStartRect( itor->ally, itor->topx, itor->topy, itor->bottomx, itor->bottomy );
	}
	SendHostInfo( IBattle::HI_StartRects );
}
Exemplo n.º 15
0
const UnitSyncMap& IBattle::LoadMap()
{

  if ( !m_map_loaded ) {
    try {
      ASSERT_EXCEPTION( m_map_exists, _T("Map does not exist.") );
      m_local_map = usync().GetMapEx( m_host_map.name );
	  bool options_loaded = CustomBattleOptions().loadOptions( OptionsWrapper::MapOption, m_host_map.name );
	  ASSERT_EXCEPTION( options_loaded, _T("couldn't load the map options") );
      m_map_loaded = true;

    } catch (...) {}
  }
  return m_local_map;
}
Exemplo n.º 16
0
void Battle::SaveMapDefaults()
{
    // save map preset
		wxString mapname = LoadMap().name;
		wxString startpostype = CustomBattleOptions().getSingleValue( _T("startpostype"), OptionsWrapper::EngineOption );
		sett().SetMapLastStartPosType( mapname, startpostype);
		std::vector<Settings::SettStartBox> rects;
		for( unsigned int i = 0; i <= GetLastRectIdx(); ++i )
		{
			 BattleStartRect rect = GetStartRect( i );
			 if ( rect.IsOk() )
			 {
				 Settings::SettStartBox box;
				 box.ally = rect.ally;
				 box.topx = rect.left;
				 box.topy = rect.top;
				 box.bottomx = rect.right;
				 box.bottomy = rect.bottom;
				 rects.push_back( box );
			 }
		}
		sett().SetMapLastRectPreset( mapname, rects );
}
Exemplo n.º 17
0
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 );
        }
    }
}