referee.cpp
来自「2009 ROBOCUP 仿真2DSERVER 源码」· C++ 代码 · 共 2,262 行 · 第 1/5 页
CPP
2,262 行
|| pm == PM_OffSide_Right || pm == PM_OffSide_Left || pm == PM_Back_Pass_Right || pm == PM_Back_Pass_Left || pm == PM_Free_Kick_Fault_Right || pm == PM_Free_Kick_Fault_Left || pm == PM_CatchFault_Right || pm == PM_CatchFault_Left ) { return; } const ServerParam & param = ServerParam::instance(); /* if a value of half_time is negative, then ignore time. */ if ( param.halfTime() > 0 ) { int normal_time = param.halfTime() * param.nrNormalHalfs(); int maximum_time = normal_time + param.extraHalfTime() * param.nrExtraHalfs(); int extra_count = s_half_time_count - param.nrNormalHalfs(); /* check for penalty shoot-outs, half_time and extra_time. */ if ( M_stadium.time() >= maximum_time ) { if ( param.penaltyShootOuts() && M_stadium.teamLeft().point() == M_stadium.teamRight().point() ) { return; // handled by PenaltyRef } else { M_stadium.sendRefereeAudio( "time_up" ); M_stadium.change_play_mode( PM_TimeOver ); return; } } else if ( M_stadium.time() >= normal_time ) { if ( ! M_stadium.teamLeft().enabled() || ! M_stadium.teamRight().enabled() ) { M_stadium.sendRefereeAudio( "time_up_without_a_team" ); M_stadium.change_play_mode( PM_TimeOver ); return; } else if ( M_stadium.teamLeft().point() != M_stadium.teamRight().point() ) { M_stadium.sendRefereeAudio( "time_up" ); M_stadium.change_play_mode( PM_TimeOver ); return; } else if ( M_stadium.time() >= ( normal_time + ( param.extraHalfTime() * ( extra_count + 1 ) ) ) ) { ++s_half_time_count; M_stadium.sendRefereeAudio( "time_extended" ); Side kick_off_side = ( s_half_time_count % 2 == 0 ? LEFT : RIGHT ); M_stadium.setHalfTime( kick_off_side, s_half_time_count ); placePlayersInTheirField(); return; } } // if not in overtime, check whether halfTime() cycles have been passed else if ( M_stadium.time() >= param.halfTime() * ( s_half_time_count + 1 ) ) { ++s_half_time_count; Side kick_off_side = ( s_half_time_count % 2 == 0 ? LEFT : RIGHT ); M_stadium.sendRefereeAudio( "half_time" ); M_stadium.setHalfTime( kick_off_side, s_half_time_count ); placePlayersInTheirField(); return; } }// if ( ServerParam::instance().CoachMode && M_stadium.check_ball_pos() != BPI_InField )// {// // freeze the ball// M_stadium.ball->moveTo( M_stadium.ball->pos(),// 0.0,// PVector( 0.0, 0.0 ),// PVector( 0.0, 0.0 ) );// }}voidReferee::placePlayersInTheirField(){ static const RArea fld_l( PVector( - ServerParam::PITCH_LENGTH/4, 0.0 ), PVector( ServerParam::PITCH_LENGTH/2, ServerParam::PITCH_WIDTH ) ); static const RArea fld_r( PVector( ServerParam::PITCH_LENGTH/4, 0.0), PVector( ServerParam::PITCH_LENGTH/2, ServerParam::PITCH_WIDTH ) ); const bool kick_off_offside ( ServerParam::instance().kickOffOffside() && ( M_stadium.playmode() == PM_KickOff_Left || M_stadium.playmode() == PM_KickOff_Right ) ); const Stadium::PlayerCont::iterator end = M_stadium.remotePlayers().end(); for ( Stadium::PlayerCont::iterator it = M_stadium.remotePlayers().begin(); it != end; ++it ) { if ( (*it)->state() == DISABLE ) continue; switch ( (*it)->side() ) { case LEFT: if ( (*it)->pos().x > 0 ) { if ( kick_off_offside ) { (*it)->moveTo( PVector( -(*it)->size(), (*it)->pos().y ) ); } else { (*it)->moveTo( fld_l.randomize() ); } } break; case RIGHT: if ( (*it)->pos().x < 0 ) { if ( kick_off_offside ) { (*it)->moveTo( PVector( (*it)->size(), (*it)->pos().y ) ); } else { (*it)->moveTo( fld_r.randomize() ); } } break; case NEUTRAL: default: break; } if ( (*it)->side() != M_stadium.kickOffSide() ) { CArea expand_c( PVector( 0.0, 0.0 ), ServerParam::KICK_OFF_CLEAR_DISTANCE + (*it)->size() ); if ( expand_c.inArea( (*it)->pos() ) ) { (*it)->moveTo( expand_c.nearestEdge( (*it)->pos() ) ); } } }}voidReferee::clearPlayersFromBall( const Side side ){ // I would really prefer if we did not teleport players around the field. In // my mind players should be given time to move away from the ball and given // yellow cards if they repeatedly fail to stay clear. Two yellows and your // out of the game. const PlayMode pm = M_stadium.playmode(); const double clear_dist = ( ( pm == PM_Back_Pass_Left || pm == PM_Back_Pass_Right ) ? ServerParam::GOAL_AREA_LENGTH : ServerParam::KICK_OFF_CLEAR_DISTANCE ); const bool indirect = ( pm == PM_IndFreeKick_Left || pm == PM_IndFreeKick_Right ); const double goal_half_width = ServerParam::instance().goalWidth()*0.5; const Stadium::PlayerCont::iterator end = M_stadium.players().end(); for ( int loop = 0; loop < 10; ++loop ) { bool exist = false; for ( Stadium::PlayerCont::iterator it = M_stadium.players().begin(); it != end; ++it ) { if ( (*it)->state() == DISABLE ) { continue; } if ( side == NEUTRAL || (*it)->side() == side ) { if ( indirect && std::fabs( (*it)->pos().x ) >= ServerParam::PITCH_LENGTH*0.5 && std::fabs( (*it)->pos().y ) <= goal_half_width ) { // defender is allowed to stand on the goal line. continue; } CArea clear_area( M_stadium.ball().pos(), clear_dist + (*it)->size() ); if ( clear_area.inArea( (*it)->pos() ) ) { PVector new_pos = clear_area.nearestEdge( (*it)->pos() ); if ( indirect && std::fabs( new_pos.x ) > ServerParam::PITCH_LENGTH*0.5 && std::fabs( new_pos.y ) < goal_half_width ) { double tangent = ( new_pos.y - M_stadium.ball().pos().y ) / ( new_pos.x - M_stadium.ball().pos().x ); new_pos.x = ServerParam::PITCH_LENGTH*0.5 * ( new_pos.x > 0.0 ? 1.0 : -1.0 ); new_pos.y = M_stadium.ball().pos().y + tangent * ( new_pos.x - M_stadium.ball().pos().x ); } else if ( std::fabs( new_pos.x ) > ( ServerParam::PITCH_LENGTH*0.5 + ServerParam::PITCH_MARGIN ) && std::fabs( new_pos.x ) > ( ServerParam::PITCH_WIDTH*0.5 + ServerParam::PITCH_MARGIN ) ) { new_pos -= M_stadium.ball().pos(); new_pos.rotate( 180.0 ); new_pos += M_stadium.ball().pos(); } (*it)->place( new_pos ); exist = true; } } } if ( exist ) { M_stadium.collisions(); } else { break; } }}//**********// BallStuckRef//**********voidBallStuckRef::analyse(){ if ( ServerParam::instance().ballStuckArea() <= 0.0 || ServerParam::instance().dropTime() <= 0 ) { return; } if ( M_stadium.playmode() != PM_PlayOn ) { M_last_ball_pos = M_stadium.ball().pos(); M_counter = 0; return; } if ( M_stadium.ball().pos().distance( M_last_ball_pos ) <= ServerParam::instance().ballStuckArea() ) { ++M_counter; if ( M_counter >= ServerParam::instance().dropTime() ) { M_last_ball_pos = M_stadium.ball().pos(); M_counter = 0; awardDropBall( M_stadium.ball().pos() ); } } else { M_last_ball_pos = M_stadium.ball().pos(); M_counter = 0; }}//**********// OffsideRef//**********const int OffsideRef::AFTER_OFFSIDE_WAIT = 30;voidOffsideRef::kickTaken( const Player & kicker ){ if ( ! ServerParam::instance().useOffside() ) { return; } M_last_kicker = &kicker; setOffsideMark( kicker );}voidOffsideRef::ballTouched( const Player & player ){ if ( ! ServerParam::instance().useOffside() ) { return; } M_last_kicker = &player; setOffsideMark( player );}voidOffsideRef::analyse(){ if ( ! ServerParam::instance().useOffside() ) { return; } if ( isPenaltyShootOut( M_stadium.playmode() ) ) { return; } if ( M_stadium.playmode() == PM_BeforeKickOff || M_stadium.playmode() == PM_KickOff_Left || M_stadium.playmode() == PM_KickOff_Right ) { if ( ServerParam::instance().kickOffOffside() ) { placePlayersInTheirField(); } return; } if ( M_stadium.playmode() == PM_OffSide_Left ) { clearPlayersFromBall( LEFT ); checkPlayerAfterOffside(); if ( ++M_after_offside_time > AFTER_OFFSIDE_WAIT ) { M_stadium.change_play_mode( PM_FreeKick_Right ); } return; } if ( M_stadium.playmode() == PM_OffSide_Right ) { clearPlayersFromBall( RIGHT ); checkPlayerAfterOffside(); if ( ++M_after_offside_time > AFTER_OFFSIDE_WAIT ) { M_stadium.change_play_mode( PM_FreeKick_Left ); } return; } if ( M_stadium.playmode() != PM_PlayOn ) { const Stadium::PlayerCont::iterator end = M_stadium.players().end(); for ( Stadium::PlayerCont::iterator p = M_stadium.players().begin(); p != end; ++p ) { (*p)->clearOffsideMark(); } return; } double dist = ServerParam::instance().offsideActiveArea(); { 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)->hasOffsideMark() ) { double tmp = (*p)->pos().distance( M_stadium.ball().pos() ); if ( tmp < dist ) { dist = tmp; M_offside_pos = (*p)->offsidePos(); } } } } if ( dist != ServerParam::instance().offsideActiveArea() ) { callOffside(); }}voidOffsideRef::playModeChange( PlayMode pm ){ if ( pm != PM_PlayOn ) { const Stadium::PlayerCont::const_iterator end = M_stadium.players().end(); for ( Stadium::PlayerCont::const_iterator p = M_stadium.players().begin(); p != end; ++p ) { (*p)->clearOffsideMark(); } }}voidOffsideRef::setOffsideMark( const Player & kicker ){ if ( kicker.hasOffsideMark() ) { M_offside_pos = kicker.offsidePos(); callOffside(); return; } const Stadium::PlayerCont::const_iterator end = M_stadium.players().end(); for ( Stadium::PlayerCont::const_iterator p = M_stadium.players().begin(); p != end; ++p ) { (*p)->clearOffsideMark(); } //if (m_no_offsides ) if ( M_stadium.playmode() == PM_GoalKick_Left || M_stadium.playmode() == PM_GoalKick_Right || M_stadium.playmode() == PM_KickIn_Left || M_stadium.playmode() == PM_KickIn_Right || M_stadium.playmode() == PM_CornerKick_Left || M_stadium.playmode() == PM_CornerKick_Right ) { return; } double fast = 0; double second = 0; double offside_line;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?