referee.cpp

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

CPP
2,262 行
字号
         && crossGoalLine( LEFT, M_prev_ball_pos )         && ! isPenaltyShootOut( M_stadium.playmode() ) )    {        M_stadium.score( RIGHT );        announceGoal( M_stadium.teamRight() );        M_after_goal_time = 0;        M_stadium.set_ball( LEFT, M_stadium.ball().pos() );        if ( ServerParam::instance().halfTime() >= 0             && M_stadium.time()             >= ServerParam::instance().halfTime()             * ServerParam::instance().nrNormalHalfs() )        {            M_stadium.change_play_mode( PM_TimeOver );        }        else        {            M_stadium.change_play_mode( PM_AfterGoal_Right );        }        return true;    }    else if ( ( ! M_stadium.ballCatcher()                || M_stadium.ballCatcher()->side() == RIGHT )              && crossGoalLine( RIGHT, M_prev_ball_pos )              && ! isPenaltyShootOut( M_stadium.playmode() )  )    {        M_stadium.score( LEFT );        announceGoal( M_stadium.teamLeft() );        M_after_goal_time = 0;        M_stadium.set_ball( RIGHT, M_stadium.ball().pos() );        if ( ServerParam::instance().halfTime() >= 0             && M_stadium.time()             >= ServerParam::instance().halfTime()             * ServerParam::instance().nrNormalHalfs() )        {            M_stadium.change_play_mode( PM_TimeOver );        }        else        {            M_stadium.change_play_mode( PM_AfterGoal_Left );        }        return true;    }    return false;}voidTouchRef::announceGoal( const Team & team ){#ifdef HAVE_SSTREAM    std::ostringstream msg;    msg << "goal_" << SideStr( team.side() ) << "_" << team.point();    M_stadium.sendRefereeAudio( msg.str().c_str() );#else    std::ostrstream msg;    msg << "goal_" << SideStr( team.side() ) << "_" << team.point() << std::ends;    M_stadium.say( msg.str() );    msg.freeze( false );#endif}boolTouchRef::indirectFreeKick( const PlayMode pm ){    switch( pm ) {    case PM_IndFreeKick_Right:    case PM_IndFreeKick_Left:        return true;    default:        return false;    }}//************// KeepawayRef//************voidKeepawayRef::analyse(){    if( ServerParam::instance().keepAwayMode() )    {        if ( M_stadium.playmode() == PM_PlayOn )        {            if( !ballInKeepawayArea() )            {                logEpisode( "o" );                M_stadium.sendRefereeAudio( trainingMsg );                resetField();            }            else if( M_take_time >= TURNOVER_TIME )            {                logEpisode( "t" );                M_stadium.sendRefereeAudio( trainingMsg );                resetField();            }            else            {                bool keeperPoss = false;                const Stadium::PlayerCont::const_iterator end = M_stadium.players().end();                for ( Stadium::PlayerCont::const_iterator p = M_stadium.players().begin();                      p != end;                      ++p )                {                    PVector ppos = (*p)->pos();                    if ( (*p)->state() != DISABLE                         && ppos.distance( M_stadium.ball().pos() )                         < ServerParam::instance().kickableArea() )                    {                        if ( (*p)->side() == LEFT )                        {                            keeperPoss = true;                        }                        else if ( (*p)->side() == RIGHT )                        {                            keeperPoss = false;                            M_take_time++;                            break;                        }                    }                }                if ( keeperPoss )                    M_take_time = 0;            }        }        else if ( ServerParam::instance().kawayStart() >= 0 )        {            if ( difftime( time( NULL ), M_start_time ) >                 ServerParam::instance().kawayStart() )                M_stadium.change_play_mode( PM_PlayOn );        }    }}voidKeepawayRef::kickTaken( const Player & ){}voidKeepawayRef::ballTouched( const Player & ){}voidKeepawayRef::playModeChange( PlayMode pm ){    if ( ServerParam::instance().keepAwayMode() )    {        if ( pm == PM_PlayOn && M_episode == 0 )        {            M_episode = 1;            const Stadium::PlayerCont::const_iterator end = M_stadium.players().end();            for ( Stadium::PlayerCont::const_iterator p = M_stadium.players().begin();                  p != end;                  ++p )            {                if ( (*p)->state() != DISABLE )                {                    if ( (*p)->side() == LEFT )                        M_keepers++;                    else if ( (*p)->side() == RIGHT )                        M_takers++;                }            }            logHeader();            resetField();        }    }}boolKeepawayRef::ballInKeepawayArea(){    PVector ball_pos = M_stadium.ball().pos();    return ( std::fabs( ball_pos.x ) < ServerParam::instance().keepAwayLength() * 0.5 &&             std::fabs( ball_pos.y ) < ServerParam::instance().keepAwayWidth() * 0.5 );}voidKeepawayRef::logHeader(){    if ( M_stadium.logger().kawayLog() )    {        M_stadium.logger().kawayLog() << "# Keepers: " << M_keepers << '\n'                                      << "# Takers:  " << M_takers << '\n'                                      << "# Region:  " << ServerParam::instance().keepAwayLength()                                      << " x " << ServerParam::instance().keepAwayWidth()                                      << '\n'                                      << "#\n"                                      << "# Description of Fields:\n"                                      << "# 1) Episode number\n"                                      << "# 2) Start time in simulator steps (100ms)\n"                                      << "# 3) End time in simulator steps (100ms)\n"                                      << "# 4) Duration in simulator steps (100ms)\n"                                      << "# 5) (o)ut of bounds / (t)aken away\n"                                      << "#\n"                                      << std::flush;    }}voidKeepawayRef::logEpisode( const char *endCond ){    if ( M_stadium.logger().kawayLog() )    {        M_stadium.logger().kawayLog() << M_episode++ << "\t"                                      << M_time << "\t"                                      << M_stadium.time() << "\t"                                      << M_stadium.time() - M_time << "\t"                                      << endCond                                      << std::endl;    }    M_time = M_stadium.time();}voidKeepawayRef::resetField(){    int keeper_pos = irand( M_keepers );    //int keeper_pos = boost::uniform_smallint<>( 0, M_keepers - 1 )( rcss::random::DefaultRNG::instance() );    const Stadium::PlayerCont::iterator end = M_stadium.players().end();    for ( Stadium::PlayerCont::iterator p = M_stadium.players().begin();          p != end;          ++p )    {        if ( (*p)->state() != DISABLE )        {            double x, y;            if ( (*p)->side() == LEFT )            {                switch( keeper_pos ) {                case 0:                    x = -ServerParam::instance().keepAwayLength() * 0.5 + drand( 0, 3 );                    y = -ServerParam::instance().keepAwayWidth() * 0.5 + drand( 0, 3 );                    break;                case 1:                    x = ServerParam::instance().keepAwayLength() * 0.5 - drand( 0, 3 );                    y = -ServerParam::instance().keepAwayWidth() * 0.5 + drand( 0, 3 );                    break;                case 2:                    x = ServerParam::instance().keepAwayLength() * 0.5 - drand( 0, 3 );                    y = ServerParam::instance().keepAwayWidth() * 0.5 - drand( 0, 3 );                    break;                default:                    x = drand( -1, 1 ); y = drand( -1, 1 );                    break;                }                (*p)->place( PVector( x, y ) );                keeper_pos = ( keeper_pos + 1 ) % M_keepers;            }            else if ( (*p)->side() == RIGHT )            {                x = -ServerParam::instance().keepAwayLength() * 0.5 + drand( 0, 3 );                y = ServerParam::instance().keepAwayWidth() * 0.5 - drand( 0, 3 );                (*p)->place( PVector( x, y ) );            }        }    }    M_stadium.set_ball( NEUTRAL,                        PVector( -ServerParam::instance().keepAwayLength() * 0.5 + 4.0,                                 -ServerParam::instance().keepAwayWidth() * 0.5 + 4.0 ) );    M_take_time = 0;}//************// CatchRef//************const int CatchRef::AFTER_BACKPASS_WAIT = 30;const int CatchRef::AFTER_CATCH_FAULT_WAIT = 30;voidCatchRef::kickTaken( const Player & kicker ){//     if ( ! kicker.isGoalie() )    {        if ( kicker.side() == LEFT )        {            M_team_l_touched = true;        }        else if ( kicker.side() == RIGHT )        {            M_team_r_touched = true;        }        if ( M_team_l_touched && M_team_r_touched )        {            M_last_back_passer = NULL;        }        else if ( kicker.isGoalie() )        {            if ( M_last_back_passer                 && M_last_back_passer->side() != kicker.side() )            {            }            else            {                M_last_back_passer = &kicker;                M_last_back_passer_time = M_stadium.time();            }        }        else if ( M_last_back_passer != &kicker )        {            M_last_back_passer = &kicker;            M_last_back_passer_time = M_stadium.time();        }    }//     else if ( M_last_back_passer != NULL//               && M_last_back_passer->team() != kicker.team() )//     {//         M_last_back_passer = NULL;//         // The else if above is to handle rare situations where a player from team//         // A kicks the ball, the goalie from team B kicks it, and then the goalie//         // from team A cacthes it.  This should not be concidered a back pass and//         // the else if make sure of that.//     }}voidCatchRef::ballTouched( const Player & player ){    // If ball is not kicked, back pass violation is never taken.//    if ( ! player.isGoalie() )    {        if ( player.side() == LEFT )        {            M_team_l_touched = true;        }        else if ( player.side() == RIGHT )        {            M_team_r_touched = true;        }        if ( M_team_l_touched && M_team_r_touched )        {            M_last_back_passer = NULL;        }    }}voidCatchRef::ballCaught( const Player & catcher ){    if ( isPenaltyShootOut( M_stadium.playmode() ) )    {        return;    }    // check handling violation    if ( M_stadium.playmode() != PM_AfterGoal_Left         && M_stadium.playmode() != PM_AfterGoal_Right         && M_stadium.playmode() != PM_TimeOver         && ! inPenaltyArea( catcher.side(), M_stadium.ball().pos() ) )    {        callCatchFault( catcher.side(), M_stadium.ball().pos() );        return;    }    // check backpass violation    if ( M_stadium.playmode() != PM_AfterGoal_Left         && M_stadium.playmode() != PM_AfterGoal_Right         && M_stadium.playmode() != PM_TimeOver         && M_stadium.time() != M_last_back_passer_time         && M_last_back_passer != NULL         //&& M_last_back_passer != &catcher         && M_last_back_passer->team() == catcher.team()         && ServerParam::instance().backPasses() )    {        //M_last_back_passer->alive |= BACK_PASS;        M_stadium.setPlayerState( M_last_back_passer->side(),                                  M_last_back_passer->unum(),                                  BACK_PASS );        callBackPass( catcher.side() );        return;    }    M_last_back_passer = NULL;    awardFreeKick( catcher.side(), M_stadium.ball().pos() );}voidCatchRef::analyse(){    M_team_l_touched = false;    M_team_r_touched = false;    if ( isPenaltyShootOut( M_stadium.playmode() ) )    {        return;    }    if ( M_stadium.playmode() == PM_Back_Pass_Left )    {        clearPlayersFromBall( LEFT );        if ( ++M_after_back_pass_time > AFTER_BACKPASS_WAIT )        {            //M_stadium.change_play_mode( PM_FreeKick_Right );            M_stadium.change_play_mode( PM_IndFreeKick_Right );        }        return;    }    if ( M_stadium.playmode() == PM_Back_Pass_Right )    {        clearPlayersFromBall( RIGHT );        if ( ++M_after_back_pass_time > AFTER_BACKPASS_WAIT )        {            //M_stadium.change_play_mode( PM_FreeKick_Left );            M_stadium.change_play_mode( PM_IndFreeKick_Left );        }        return;    }    if ( M_stadium.playmode() == PM_CatchFault_Left )    {        clearPlayersFromBall( LEFT );        if( ++M_after_catch_fault_time > AFTER_CATCH_FAULT_WAIT )        {            //M_stadium.change_play_mode( PM_IndFreeKick_Right );            M_stadium.change_play_mode( PM_FreeKick_Right );        }        return;    }    if ( M_stadium.playmode() == PM_CatchFault_Right )    {        clearPlayersFromBall( RIGHT );        if( ++M_after_catch_fault_time > AFTER_CATCH_FAULT_WAIT )        {            //M_stadium.change

⌨️ 快捷键说明

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