⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 field.c

📁 在LINUX下运行的仿真机器人服务器源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* -*- Mode: C++ -*- *Header: *File: field.C *Author: Noda Itsuki *Date: 1996/02/14 *EndHeader: *//* *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: */#ifdef HAVE_CONFIG_H#include "config.h"#endif#ifdef HAVE_SSTREAM#include <sstream>#else#include <strstream>#endif#include <cmath>#include <cstdio>#include <cstdlib>#include <unistd.h>#include <string>#include <strings.h>#include <csignal>#include <cctype>#include <sys/time.h>#include <iostream>#include <netinet/in.h>#include <cerrno>#include "utility.h"#include "param.h"#include "types.h"#include "object.h"#include "field.h"#include "clangmsg.h"#include "random.h"#include "serializer.h"#include "referee.h"#include "xpmholder.h"#include "landmarkreader.h"#include "monitor.h"#include "serverparam.h"const std::string Stadium::DEF_TEXT_NAME = "incomplete";const std::string Stadium::DEF_TEXT_SUFFIX = ".rcl";const std::string Stadium::DEF_GAME_NAME = "incomplete";const std::string Stadium::DEF_GAME_SUFFIX = ".rcg";const std::string Stadium::DEF_KAWAY_NAME = "incomplete";const std::string Stadium::DEF_KAWAY_SUFFIX = ".kwy";extern Stadium Std;namespace{  void sigHandle ( int )  {    std::cout << "Server Killed. Exiting...\n";    closeLogs ();    Std.saveResults();    Std.exit( EXIT_FAILURE );  }}Stadium::Stadium ()  : team_l ( NULL ),    team_r ( NULL ),    m_indirect( false ),    M_last_playon_start( 0 ),    M_last_back_passer( 0 ),    M_free_kicker( NULL ),    M_free_kicker_dashed( false ),    M_referees(),    M_caught_ball( NULL ),    M_game_over_wait( 0 ), // can't use serverparam yet,    M_left_child( 0 ),    M_right_child( 0 ),    m_no_offsides( false ){    time_t tmp_time = ::time( NULL );    tm* tmp_local_time = localtime( &tmp_time );    if( tmp_local_time == NULL )    {        std::cerr << __FILE__ << ":" << __LINE__                  << ": Error getting time: "                  << strerror( errno ) << std::endl;        exit( EXIT_FAILURE );    }    m_real_time = *tmp_local_time;  M_referees.push_back( new FreeKickRef( *this ) );  M_referees.push_back( new TouchRef( *this ) );  M_referees.push_back( new CatchRef( *this ) );  M_referees.push_back( new KeepawayRef( *this ) );  M_referees.push_back( new PenaltyRef( *this ) );}Stadium::~Stadium(){  for( std::list< Referee* >::iterator i = M_referees.begin();       i != M_referees.end(); ++i )    delete *i;  M_referees.clear();}/* *=================================================================== *Part: Create Stadium Window *=================================================================== */void Stadium::init( int argc, const char** argv ){  M_game_over_wait = ServerParam::instance().gameOverWait();    m_argc = argc;    m_argv = argv;    // we create the result savers now, so that if there are any    // errors creating them, it will be reported before    // the game starts, not after it has finished.    std::list< const char* > savers = rcss::ResultSaver::factory().list();    for( std::list< const char* >::iterator i = savers.begin();         i != savers.end(); ++i )    {	rcss::ResultSaver::Creator creator;	if( rcss::ResultSaver::factory().getCreator( creator, *i ) )	{            rcss::ResultSaver::Ptr saver = creator();            std::cout << saver->getName() << ": Ready\n";            m_savers.push_back( saver );        }	else	{	    std::cerr << *i << ": error loading" << std::endl;         }    }    if( ServerParam::instance().CoachMode        && !ServerParam::instance().CwRMode )    {        for( std::list< Referee* >::iterator i = M_referees.begin();             i != M_referees.end(); ++i )            delete *i;        M_referees.clear();    }  struct sigaction sig_action ;  sig_action.sa_handler = &sigHandle ;  sig_action.sa_flags = 0; // [2000/11/20.frehberg.cs.tu-berlin.de]  if ( sigaction ( SIGINT, &sig_action , NULL ) != 0 )    {        std::cerr << __FILE__ << ": " << __LINE__                  << ": could not set signal handler: "                  << strerror( errno ) << std::endl;      exit ( EXIT_FAILURE );    }    landmark_reader = new LandmarkReader ( this,                                            ServerParam::instance().landmarkFile () );    if ( landmark_reader == NULL )      {        perror ( "Can not create landmark reader" );        exit ( EXIT_FAILURE );      }    if ( PlayerParam::instance().randomSeed() == 0 )      rcss::random::DefaultRNG::instance();    else      rcss::random::DefaultRNG::instance( PlayerParam::instance().randomSeed() );    player_types = new HetroPlayer* [ PlayerParam::instance().playerTypes () ];    if ( player_types == NULL )		{				perror ( "Can not create hetro_player array" );				exit ( EXIT_FAILURE );		}        player_types[0] = new HetroPlayer( 0 );//      std::cout << *(player_types[ 0 ]) << std::endl;    for( int i = 1; i < PlayerParam::instance().playerTypes (); i++ )		{				player_types[i] = new HetroPlayer();//  				std::cout << *(player_types[i]) << std::endl;		}        if ( ServerParam::instance().textLogging () ) 		{				text_log_name = ServerParam::instance().textLogDir ();        if ( text_log_name [ text_log_name.size () -1 ] != '/' )          text_log_name += '/';				if ( ServerParam::instance().textLogFixed () )						text_log_name += ServerParam::instance().textLogFixedName ();				else						text_log_name += Stadium::DEF_TEXT_NAME;				text_log_name += Stadium::DEF_TEXT_SUFFIX;				if ( ServerParam::instance().textLogCompression () > 0 )				{						text_log_name += std::string ( ".gz" );						gz_text_log.open ( text_log_name.c_str (), 															 ServerParam::instance().textLogCompression () );				}				else				    text_log.open ( text_log_name.c_str () );					if ( !text_log_open () )                    std::cerr << __FILE__ << ": " << __LINE__ 								 << ": can't open log_file " << text_log_name << std::endl ;		}    if ( ServerParam::instance().gameLogging () ) 				OpenForWrite() ;    if ( ServerParam::instance().kaway &&	 ServerParam::instance().kawayLogging () )       {	kaway_log_name = ServerParam::instance().kawayLogDir ();        if ( kaway_log_name [ kaway_log_name.size () -1 ] != '/' )          kaway_log_name += '/';		if ( ServerParam::instance().kawayLogFixed () )	  kaway_log_name += ServerParam::instance().kawayLogFixedName ();	else	  kaway_log_name += Stadium::DEF_KAWAY_NAME;	kaway_log_name += Stadium::DEF_KAWAY_SUFFIX;		kaway_log.open ( kaway_log_name.c_str () );		if ( !kaway_log.is_open () )	  std::cerr << __FILE__ << ": " << __LINE__ 		    << ": can't open keepaway_log_file " << kaway_log_name << std::endl ;      }    if( M_player_socket.bind( rcss::net::Addr( ServerParam::instance().playerPort() ) )	&& M_offline_coach_socket.bind( rcss::net::Addr( ServerParam::instance().offlineCoachPort() ) )	&& M_online_coach_socket.bind( rcss::net::Addr( ServerParam::instance().onlineCoachPort() ) ) )    {	if( M_player_socket.setNonBlocking() == -1             || M_offline_coach_socket.setNonBlocking() == -1            || M_online_coach_socket.setNonBlocking() == -1 )        {            std::cerr << "error setting sockets non-blocking: "                      << strerror( errno ) << std::endl;            this->exit ( EXIT_FAILURE );        }    }    else    {        std::cerr << "error initializing sockets: "		  << strerror( errno ) << std::endl;	this->exit ( EXIT_FAILURE );    }    weather.init(this) ;    assign() ;    assignBall() ;    set_ball(PVector(0.0,0.0),LEFT) ;    //this->startp = FALSE ;    this->startp = 0;    player = new Player*[MAX_PLAYER * 2] ;    olcoach = new OnlineCoach*[2];    initPlayer() ;    initCoach(this) ;    initOnlineCoach(this) ;    //last_touch = NULL; //player[0];    time = 0;    team_l->point = ServerParam::instance().start_goal_l;    team_r->point = ServerParam::instance().start_goal_r;    change_play_mode(PM_BeforeKickOff) ;    for ( int i = 0 ; i <= MAX_PLAYER * 2; i++)      dinfo.body.show.pos[i].enable = htons(FALSE) ;    dinfo.mode = htons(SHOW_MODE) ;    dinfo.body.show.team[0].name[0] = NULLCHAR ;    dinfo.body.show.team[1].name[0] = NULLCHAR ;    dinfo.body.show.time = htons((unsigned short)-1) ;    minfo.mode = htons(MSG_MODE) ;    for ( int i = 0 ; i < MAX_PLAYER * 2; i++)      dinfo2.body.show.pos[i].mode = htons(FALSE) ;    dinfo2.mode = htons(SHOW_MODE) ;    dinfo2.body.show.team[0].name[0] = NULLCHAR ;    dinfo2.body.show.team[1].name[0] = NULLCHAR ;    dinfo2.body.show.time = htons((unsigned short)-1) ;    randomize() ;      M_kick_off_wait = ServerParam::instance().kickOffWait();    M_connect_wait= ServerParam::instance().connectWait();}void Stadium::clean (){  if ( player_types != NULL )    {      for ( int i = 0; i < PlayerParam::instance().playerTypes (); i++ )        {          if ( player_types[i] != NULL )            {              delete player_types[i];              player_types[i] = NULL;            }        }      delete[] player_types;      player_types = NULL;    }}void Stadium::reset_player_comm_flag(){	int i ;	for (i = 0 ; i < MAX_PLAYER * 2; i++)    {      if( player[i]->getTackleCycles() > 0 )        player[i]->decrementTackleCycles();      if( player[i]->getTackleCycles() == 0 )        player[i]->command_done = FALSE;      player[i]->turn_neck_done = FALSE ;    }}void Stadium::startTeams(){   if( mode != PM_PlayOn )    {        if( M_left_child == 0             && !ServerParam::instance().teamLeftStart().empty() )        {            M_left_child = startTeam( ServerParam::instance().teamLeftStart() );        }                if( M_right_child == 0             && !ServerParam::instance().teamRightStart().empty()             && teamConnected( LEFT ) ) // the right team cannot connect            // until the left team has            M_right_child = startTeam( ServerParam::instance().teamRightStart() );    }}intStadium::startTeam( const std::string& start ){  if( start.empty() )    return 0;    std::cout << "Starting \"/bin/sh -c "        << start.c_str() << "\"\n";  int pid = fork();  if( pid == -1 )    {      std::cerr << PACKAGE << "-" << VERSION            << ": Error: Could not fork to start team: "           << strerror( errno ) << std::endl;      this->exit( EXIT_FAILURE );    }  if( pid == 0 )    {      execlp( "/bin/sh", "sh", "-c", start.c_str(), (char *)NULL );      std::cerr << PACKAGE << "-" << VERSION            << ": Error: Could not execute \"/bin/sh -c "            << start.c_str() << "\": "           << strerror( errno ) << std::endl;      this->exit( EXIT_FAILURE );    }  return pid;}boolStadium::teamConnected( Side side ){    if( side == LEFT )    {        return team_l->enable;    }    else if( side == RIGHT )    {        return team_r->enable;    }    else        return team_l->enable && team_r->enable;}/*//pfr:SYNCHvoid Stadium::MainSynchLoop() {  struct itimerval itv ;  struct sigaction alarm_action ;  const int max_alrms_wait = 25;  char* think_command = "(think)";  int cycles_missed = 0; //number of cycles where someone missed  const int max_cycles_missed = 20;    timeval tp_new, tp_old;  bool shutdown = false;  

⌨️ 快捷键说明

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