playerparam.c

来自「在LINUX下运行的仿真机器人服务器源代码」· C语言 代码 · 共 459 行 · 第 1/2 页

C
459
字号
/* -*- Mode: C++ -*- *//* *Copyright:    Copyright (C) 1996-2000 Electrotechnical Laboratory.     	Itsuki Noda, Yasuo Kuniyoshi and Hitoshi Matsubara.    Copyright (C) 2000, 2001 RoboCup Soccer Server Maintainance Group.    	Patrick Riley, Tom Howard, Daniel Polani, Itsuki Noda,	Mikhail Prokopenko, Jan Wendler     This file is a part of SoccerServer.    This code is free software; you can redistribute it and/or    modify it under the terms of the GNU Lesser General Public    License as published by the Free Software Foundation; either    version 2.1 of the License, or (at your option) any later version.    This library is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    Lesser General Public License for more details.    You should have received a copy of the GNU Lesser General Public    License along with this library; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *EndCopyright: */#include <string>#include <iostream>#include "config.h"#include "playerparam.h"#include <rcssbase/conf/parser.hpp>#include <rcssbase/conf/builder.hpp>#include <rcssbase/conf/genericbuilder.hpp>#include <errno.h>PlayerParam::Printer::Printer( std::ostream& out, unsigned int version )    : M_out( out ),      M_version( version ){}        voidPlayerParam::Printer::operator()( const std::pair< const std::string, unsigned int > item ){            if( item.second <= M_version )            {		int ivalue; 		if( PlayerParam::instance().getInt( item.first, ivalue ) )		{                    M_out << "(" << item.first << " " << ivalue << ")";                    return;                }                		bool bvalue; 		if( PlayerParam::instance().getBool( item.first, bvalue ) )		{		    M_out << "(" << item.first << " " << bvalue << ")";                    return;                }                		double dvalue; 		if( PlayerParam::instance().getDoub( item.first, dvalue ) )		{                    M_out << "(" << item.first << " " << dvalue << ")";                    return;                }                                std::string svalue; 		if( PlayerParam::instance().getStr( item.first, svalue ) )		{                    M_out << "(" << item.first << " " << svalue << ")";                    return;                }            }}#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) || defined (__CYGWIN__)#  ifndef WIN32#    define WIN32#  endif#endif#ifdef WIN32const char PlayerParam::OLD_PLAYER_CONF[] = "~\\.rcssserver-player.conf";const char PlayerParam::PLAYER_CONF[] = "~\\.rcssserver\\player.conf";#elseconst char PlayerParam::OLD_PLAYER_CONF[] = "~/.rcssserver-player.conf";const char PlayerParam::PLAYER_CONF[] = "~/.rcssserver/player.conf";#endifconst int PlayerParam::DEFAULT_PLAYER_TYPES = 7;const int PlayerParam::DEFAULT_SUBS_MAX = 3;const int PlayerParam::DEFAULT_PT_MAX = 3;const double PlayerParam::DEFAULT_PLAYER_SPEED_MAX_DELTA_MIN = 0.0;const double PlayerParam::DEFAULT_PLAYER_SPEED_MAX_DELTA_MAX = 0.0;const double PlayerParam::DEFAULT_STAMINA_INC_MAX_DELTA_FACTOR = 0.0;    const double PlayerParam::DEFAULT_PLAYER_DECAY_DELTA_MIN = 0.0;const double PlayerParam::DEFAULT_PLAYER_DECAY_DELTA_MAX = 0.2;const double PlayerParam::DEFAULT_INERTIA_MOMENT_DELTA_FACTOR = 25.0;const double PlayerParam::DEFAULT_DASH_POWER_RATE_DELTA_MIN = 0.0;const double PlayerParam::DEFAULT_DASH_POWER_RATE_DELTA_MAX = 0.0;const double PlayerParam::DEFAULT_PLAYER_SIZE_DELTA_FACTOR = -100.0;const double PlayerParam::DEFAULT_KICKABLE_MARGIN_DELTA_MIN = 0.0;const double PlayerParam::DEFAULT_KICKABLE_MARGIN_DELTA_MAX = 0.2;const double PlayerParam::DEFAULT_KICK_RAND_DELTA_FACTOR = 0.5;const double PlayerParam::DEFAULT_EXTRA_STAMINA_DELTA_MIN = 0.0;const double PlayerParam::DEFAULT_EXTRA_STAMINA_DELTA_MAX = 100.0;const double PlayerParam::DEFAULT_EFFORT_MAX_DELTA_FACTOR = -0.002;const double PlayerParam::DEFAULT_EFFORT_MIN_DELTA_FACTOR = -0.002;const int    PlayerParam::DEFAULT_RANDOM_SEED = -1; //negative means generate a new seedconst double PlayerParam::DEFAULT_NEW_DASH_POWER_RATE_DELTA_MIN = 0.0;const double PlayerParam::DEFAULT_NEW_DASH_POWER_RATE_DELTA_MAX = 0.002;const double PlayerParam::DEFAULT_NEW_STAMINA_INC_MAX_DELTA_FACTOR = -10000.0;PlayerParam&PlayerParam::instance( rcss::conf::GenericBuilder* parent ){    static bool parent_set = false;    if( parent != NULL || parent_set )    {	static PlayerParam rval( parent );	parent_set = true;	return rval;    }    // hack to allow link testing to call instance without crashing    // do not used the return value in these situations    PlayerParam* rval = NULL;    return *rval;}PlayerParam&PlayerParam::instance(){ return PlayerParam::instance( NULL ); }boolPlayerParam::init( rcss::conf::GenericBuilder* parent ){    instance( parent );#ifndef WIN32    if( system( ( "ls " + tildeExpand( PlayerParam::OLD_PLAYER_CONF ) + " > /dev/null 2>&1" ).c_str() ) == 0 )    {        if( system( ( "ls " + tildeExpand( PlayerParam::PLAYER_CONF ) + " > /dev/null 2>&1" ).c_str() ) != 0 )        {            if( system( "which awk > /dev/null 2>&1" ) == 0 )            {                std::cout << "Trying to convert old configuration file '"                           << PlayerParam::OLD_PLAYER_CONF                          << "'\n";        char filename[] = "/tmp/rcssplayer-oldconf-XXXXXX";        int fd = mkstemp( filename );        if( fd != -1 )        {            close( fd );            std::string command = "awk '/^[ \\t]*$/ {} ";            command += "/^[^#]+[:]/ { gsub(/:/, \"=\" ); $1 = \"player::\" $1; } ";            command += "/^[ \\t]*[^#:=]+$/ { $1 = \"player::\" $1 \" = true\"; }";            command += "{ print; }' ";            command +=  tildeExpand( PlayerParam::OLD_PLAYER_CONF );            command += " > ";            command += filename;            if( system( command.c_str() ) == 0 )            {                std::cout << "Conversion successful\n";                instance().m_builder->parser()->parse( filename );            }            else            {                std::cout << "Conversion failed\n";            }        }        else        {            std::cout << "Conversion failed\n";        }    }        }    }#endif // not win32    if( !instance().m_builder->parser() )    {	std::cerr << __FILE__ << ": " << __LINE__ 		  << ": internal error: player param could not find configuration parser\n";	std::cerr << "Please submit a full bug report to sserver-bugs@lists.sf.net\n";	return false;    }    std::string conf_name = tildeExpand( PlayerParam::PLAYER_CONF );    std::ifstream conf( conf_name.c_str() );    if( !conf.is_open() )    {	instance().m_builder->creatingConfFile( conf_name );	std::ofstream new_conf( conf_name.c_str() );	if( new_conf.is_open() )	{	    instance().m_builder->createConfFile( new_conf );	    new_conf.close();	    instance().m_builder->createdConfFile( conf_name );	    return true;	}	else	{	    instance().m_builder->confCreationFailed( conf_name, errno );	    std::cerr << "could not create configuration file '" 		      << conf_name		      << "'\n";	    return false;	}    }    else    {	bool rval = instance().m_builder->parser()->parse( conf, conf_name );	conf.close();	if( !rval )	    std::cerr << "could not parse configuration file '" 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?