field.cpp

来自「2009 ROBOCUP 仿真2DSERVER 源码」· C++ 代码 · 共 2,481 行 · 第 1/5 页

CPP
2,481
字号
/* -*- 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-2007 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#include "field.h"#include "audio.h"#include "clangmsg.h"#include "coach.h"#include "landmarkreader.h"#include "monitor.h"#include "object.h"#include "param.h"#include "player.h"#include "heteroplayer.h"#include "random.h"#include "referee.h"#include "serverparam.h"#include "team.h"#include "types.h"#include "utility.h"#include "xpmholder.h"#ifdef HAVE_SSTREAM#include <sstream>#else#include <strstream>#endif#include <vector>#include <string>#include <iostream>#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <csignal>#include <cctype>#include <cerrno>#include <sys/time.h>#include <netinet/in.h>/* *=================================================================== *Part: Field *=================================================================== */Field::Field()    : line_l( LINE_L_NAME, LINE_L_NAME_SHORT, "(Line)", "(L)",              PVector( - ServerParam::PITCH_LENGTH / 2.0, 0.0 ) ),      line_r( LINE_R_NAME, LINE_R_NAME_SHORT, "(Line)", "(L)",              PVector( + ServerParam::PITCH_LENGTH / 2.0, 0.0 ) ),      line_t( LINE_T_NAME, LINE_T_NAME_SHORT, "(Line)", "(L)",              PVector( - ServerParam::PITCH_WIDTH / 2.0, 0.0 ) ),      line_b( LINE_B_NAME, LINE_B_NAME_SHORT, "(Line)", "(L)",              PVector( + ServerParam::PITCH_WIDTH / 2.0, 0.0 ) ){}Field::~Field(){    M_goals.clear();    for ( std::vector< PObject * >::iterator i = M_landmarks.begin();          i != M_landmarks.end();          ++i )    {        delete *i;        *i = static_cast< PObject * >( 0 );    }    M_landmarks.clear();}voidField::addLandmark( PObject * new_obj ){    if ( new_obj )    {        if ( new_obj->closeName() == O_TYPE_GOAL_NAME )        {            M_goals.push_back( new_obj );        }        M_landmarks.push_back( new_obj );    }}Stadium::Stadium()    : M_alive( true ),      M_logger( *this ),      M_ball( NULL ),      M_players( MAX_PLAYER*2, static_cast< Player * >( 0 ) ),      M_coach( NULL ),      M_olcoaches( 2, static_cast< OnlineCoach * >( 0 ) ),      M_team_l( NULL ),      M_team_r( NULL ),      M_playmode( PM_BeforeKickOff ),      M_time( 0 ),      M_stoppage_time( 0 ),      M_ball_catcher( NULL ),      M_kick_off_side( LEFT ),      M_last_playon_start( 0 ),      M_game_over_wait( 0 ),      M_left_child( 0 ),      M_right_child( 0 ){    time_t tmp_time = std::time( NULL );    tm * tmp_local_time = std::localtime( &tmp_time );    if ( tmp_local_time == NULL )    {        std::cerr << __FILE__ << ":" << __LINE__                  << ": Error getting time: "                  << strerror( errno ) << std::endl;        //this->exit( EXIT_FAILURE );        disable();        return;    }    m_real_time = *tmp_local_time;    srand( tmp_time );    srandom( tmp_time );    rcss::random::DefaultRNG::instance( static_cast< rcss::random::DefaultRNG::result_type >( tmp_time ) );    // !!! registration order is very important !!!    // TODO: fix the dependencies between referees.    M_referees.push_back( new TimeRef( *this ) );    M_referees.push_back( new BallStuckRef( *this ) );    M_referees.push_back( new OffsideRef( *this ) );    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 ) );    M_movable_objects.reserve( MAX_PLAYER*2 + 1 );}Stadium::~Stadium(){    for ( std::list< ResultSaver * >::iterator i = m_savers.begin();          i != m_savers.end();          ++i )    {        delete *i;    }    m_savers.clear();    for ( std::list< Referee * >::iterator i = M_referees.begin();          i != M_referees.end();          ++i )    {        delete *i;    }    M_referees.clear();    for ( MonitorCont::iterator i = M_monitors.begin();          i != M_monitors.end();          ++i )    {        if ( !(*i)->connected() )        {            delete *i;            *i = static_cast< Monitor * >( 0 );        }    }    M_monitors.clear();    for ( OnlineCoachCont::iterator it = M_olcoaches.begin();          it != M_olcoaches.end();          ++it )    {        if ( *it )        {            delete *it;            *it = static_cast< OnlineCoach * >( 0 );        }    }    M_olcoaches.clear();    for ( PlayerCont::iterator it = M_players.begin();          it != M_players.end();          ++it )    {        if ( *it )        {            delete *it;            *it = static_cast< Player * >( 0 );        }    }    M_players.clear();    for ( std::vector< HeteroPlayer * >::iterator it = M_player_types.begin();          it != M_player_types.end();          ++it )    {        if ( *it != NULL )        {            delete *it;        }    }    M_player_types.clear();    delete M_team_l; M_team_l = NULL;    delete M_team_r; M_team_r = NULL;    delete M_coach; M_coach = NULL;    delete M_ball; M_ball = NULL;}/* *=================================================================== *Part: Create Stadium Window *=================================================================== */boolStadium::init(){    M_game_over_wait = ServerParam::instance().gameOverWait();    // 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 = ResultSaver::factory().list();    for ( std::list< const char * >::iterator i = savers.begin();          i != savers.end();          ++i )    {        ResultSaver::Creator creator;        if ( ResultSaver::factory().getCreator( creator, *i ) )        {            ResultSaver::Ptr saver = creator();            std::cout << saver->getName() << ": Ready\n";            m_savers.push_back( saver.release() );        }        else        {            std::cerr << *i << ": error loading" << std::endl;        }    }    if ( ServerParam::instance().coachMode()         && ! ServerParam::instance().coachWithRefereeMode() )    {        for ( std::list< Referee * >::iterator i = M_referees.begin();              i != M_referees.end();              ++i )        {            delete *i;        }        M_referees.clear();    }    {        LandmarkReader * reader            = new LandmarkReader( M_field, ServerParam::instance().landmarkFile() );        if ( ! reader )        {            perror( "Can not create landmark reader" );            disable();            return false;        }        delete reader;    }    M_player_types.push_back( new HeteroPlayer( 0 ) );    //std::cout << *(M_player_types[ 0 ]) << std::endl;    for ( int i = 1; i < PlayerParam::instance().playerTypes(); i++ )    {        M_player_types.push_back( new HeteroPlayer() );        //std::cout << *(M_player_types[i]) << std::endl;    }    if ( ! M_player_socket.bind( rcss::net::Addr( ServerParam::instance().playerPort() )  ) )    {        std::cerr << "Error initializing sockets: port=" << ServerParam::instance().playerPort()                  << ". " << strerror( errno ) << std::endl;        disable();        return false;    }    if ( ! M_offline_coach_socket.bind( rcss::net::Addr( ServerParam::instance().offlineCoachPort() ) ) )    {        std::cerr << "Error initializing sockets: port=" << ServerParam::instance().offlineCoachPort()                  << ". " << strerror( errno ) << std::endl;        disable();        return false;    }    if ( ! M_online_coach_socket.bind( rcss::net::Addr( ServerParam::instance().onlineCoachPort() ) ) )    {        std::cerr << "Error initializing sockets: port=" << ServerParam::instance().onlineCoachPort()                  << ". " << strerror( errno ) << std::endl;        disable();        return false;    }    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;        disable();        return false;    }    M_weather.init();    initObjects();    change_play_mode( PM_BeforeKickOff );    M_kick_off_wait = std::max( 0, ServerParam::instance().kickOffWait() );    M_connect_wait = std::max( 0, ServerParam::instance().connectWait() );    if ( ! M_logger.open() )    {        disable();        return false;    }    return true;}voidStadium::checkAutoMode(){    if ( ! ServerParam::instance().autoMode() )    {        return;    }    //    // check kick off for the auto mode    //    if ( playmode() == PM_BeforeKickOff         && time() < ( ( ServerParam::instance().halfTime()                         * ServerParam::instance().nrNormalHalfs() )                       + ( ServerParam::instance().extraHalfTime()                           * ServerParam::instance().nrExtraHalfs() ) )         )    {        if ( M_remote_players.size() == MAX_PLAYER*2 || time() > 0 )        {            if ( M_kick_off_wait == ServerParam::instance().kickOffWait() )            {                std::cout << "Waiting to kick off\n";            }            if ( M_kick_off_wait > 0 )            {                --M_kick_off_wait;            }        }        else        {            if ( M_connect_wait == ServerParam::instance().connectWait() )            {                std::cout << "Waiting for players to connect\n";            }            if ( M_connect_wait > 0 )            {                --M_connect_wait;            }        }        if ( M_kick_off_wait <= 0 || M_connect_wait <= 0 )        {            _Start( *this );        }    }    else    {        M_kick_off_wait = std::max( 0, ServerParam::instance().kickOffWait() );        M_connect_wait = std::max( 0, ServerParam::instance().connectWait() );    }    //    // check time over for the auto mode    //    if ( ServerParam::instance().autoMode() )    {        if ( playmode() == PM_TimeOver )        {            if( M_game_over_wait == ServerParam::instance().gameOverWait() )            {                std::cout << "Waiting after end of match\n";            }            if ( M_game_over_wait > 0 )            {                --M_game_over_wait;            }            if ( M_game_over_wait <= 0 )            {                finalize( "Game Over. Exiting..." );                return;            }        }        else        {            M_game_over_wait = ServerParam::instance().gameOverWait();        }    }}voidStadium::startTeams(){    if ( playmode() != 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 );        disable();

⌨️ 快捷键说明

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