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

📄 soccerruleaspect.cpp

📁 robocup rcssserver 运行防真机器人足球比赛所用的服务器端
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#if 1    // do nothing for the duration of mKickInPauseTime    if (mGameState->GetModeTime() < mKickInPauseTime)    {        return;    }    // move away opponent team    ClearPlayers(idx == TI_LEFT ? mLeftPenaltyArea : mRightPenaltyArea,                 1.0, SoccerBase::OpponentTeam(idx));    // if no player touched the ball for mDropBallTime, we move away    // all players and set the play mode to play on    if (mDropBallTime > 0 &&        mGameState->GetModeTime() > mDropBallTime)    {        DropBall(mFreeKickPos);        return;    }    // after the first agent touches the ball, we do nothing until    // the ball leaves the penalty area.    shared_ptr<AgentAspect> agent;    TTime time;    if (! mBallState->GetLastCollidingAgent(agent,time))    {        return;    }    TTime lastChange = mGameState->GetLastModeChange();    // if the team with the goal kick touched the ball and the ball is    // outside the penalty area, we switch to play on.    if (time > lastChange)    {        Vector2f pos(mBallBody->GetPosition().x(),                     mBallBody->GetPosition().y());        if ((idx == TI_RIGHT && !mRightPenaltyArea.Contains(pos)) ||            (idx == TI_LEFT && !mLeftPenaltyArea.Contains(pos)) ||            (idx == TI_NONE))        {            // we have to handle the case where the team with the goal kick            // scores a self goal, because the penalty areas contain the goal.//            if (!mBallState->GetBallOnField())                mGameState->SetPlayMode(PM_PlayOn);        }        return;    }    // the ball was not touched yet    else    {        // move the ball back on the free kick position        MoveBall(mFreeKickPos);    }#endif}voidSoccerRuleAspect::UpdateCornerKick(TTeamIndex idx){#if 1    // do nothing for the duration of mKickInPauseTime    if (mGameState->GetModeTime() < mKickInPauseTime)    {        return;    }    // move away opponent team    ClearPlayers(mFreeKickPos, mFreeKickDist, mFreeKickMoveDist,                 SoccerBase::OpponentTeam(idx));    // if no player touched the ball for mDropBallTime, we move away    // all players and set the play mode to play on    if (mDropBallTime > 0 &&        mGameState->GetModeTime() > mDropBallTime)    {        DropBall(mFreeKickPos);        return;    }    // after the first agent touches the ball move to PM_PLAY_ON. the    // time when the agent last touches the ball must be after the    // change to the KickIn mode    shared_ptr<AgentAspect> agent;    TTime time;    if (! mBallState->GetLastCollidingAgent(agent,time))    {        return;    }    TTime lastChange = mGameState->GetLastModeChange();    if (time > lastChange)    {        mGameState->SetPlayMode(PM_PlayOn);    } else    {        // move the ball back on the ground where it left the playing        // field        MoveBall(mFreeKickPos);    }#endif}boolSoccerRuleAspect::CheckBallLeftField(){//     cerr << "CheckBallLeftField\n";    if (mBallState->GetBallOnField())    {        // update freekickpos        mFreeKickPos = mBallState->GetLastValidBallPosition();        return false;    }    // get the team of the last agent touching the ball and set the    // correct kick in playmode    shared_ptr<AgentAspect> agent;    shared_ptr<AgentState> agentState;    TTime time;    if (mBallState->GetLastCollidingAgent(agent,time) &&        SoccerBase::GetAgentState(agent,agentState))    {        /* Possibilities are:           - Ball left field on the long sides: kick in for the other team           - Ball left field on the short sides:             - corner kick if the a team kicked the ball over its own side             - goal kick if the a team kicked the ball over opponent side           - [ The ball left the field through a hole in the floor or ceiling               or some aliens have taken it away =:-]               In this case put the ball back to the last valid position and               we hope nobody sees.               (this should not happen. really.  But it CAN happen depending               on the ODE parameters you choose or if somebody is going to               simulate the first contact (stardate 50893.5)).        */        Vector3f ball_pos = mBallBody->GetPosition();        bool last_touch_left = (agentState->GetTeamIndex() == TI_LEFT);        bool ball_left = (ball_pos[0] < 0);        bool ball_up = (ball_pos[1] < 0);        // handle corner / goal kick        if (salt::gAbs(ball_pos.x()) >= mFieldLength / 2)        {            // check goal kick right team            if (last_touch_left && !ball_left)            {                mFreeKickPos[0] = mFieldLength / 2 - 5.0;                mFreeKickPos[1] = 0.0;                mFreeKickPos[2] = mBallRadius;                mGameState->SetPlayMode(PM_GOAL_KICK_RIGHT);            }            // check goal kick left team            else if (!last_touch_left && ball_left)            {                mFreeKickPos[0] = -mFieldLength / 2 + 5.0;                mFreeKickPos[1] = 0.0;                mFreeKickPos[2] = mBallRadius;                mGameState->SetPlayMode(PM_GOAL_KICK_LEFT);            }            // check corner kick right team            else if (last_touch_left && ball_left)            {                mFreeKickPos[0] = -mFieldLength / 2 + 0.05;                mFreeKickPos[1] = ball_pos[1] > 0 ?                    mFieldWidth / 2 - 0.05 : -mFieldWidth / 2 + 0.05;                mFreeKickPos[2] = mBallRadius;                mGameState->SetPlayMode(PM_CORNER_KICK_RIGHT);            }            // check corner kick left team            else            {                mFreeKickPos[0] = mFieldLength / 2 - 0.05;                mFreeKickPos[1] = ball_pos[1] > 0 ?                    mFieldWidth / 2 - 0.05 : -mFieldWidth / 2 + 0.05;                mFreeKickPos[2] = mBallRadius;                mGameState->SetPlayMode(PM_CORNER_KICK_LEFT);            }        }        // handle kick in        else if (salt::gAbs(ball_pos.y()) >= mFieldWidth / 2)        {            mFreeKickPos = mBallState->GetLastValidBallPosition();            mFreeKickPos[1] = mFreeKickPos[1] > 0 ?                mFieldWidth / 2 : -mFieldWidth / 2;            mFreeKickPos[2] = mBallRadius;            mGameState->SetPlayMode((agentState->GetTeamIndex() == TI_LEFT) ?                                    PM_KickIn_Right : PM_KickIn_Left);        }        // we've got a situation here        else {            // this will stop the ball, but better than losing it away.            MoveBall(mBallState->GetLastValidBallPosition());            // no need to change the game state, we just don't know what            // has been going on.            return false;        }    }    return true;}boolSoccerRuleAspect::CheckGoal(){    // check if the ball is in one of the goals    TTeamIndex idx = mBallState->GetGoalState();    if (idx == TI_NONE)    {        return false;    }    // score the lucky team    mGameState->ScoreTeam((idx == TI_LEFT) ? TI_RIGHT : TI_LEFT);    mGameState->SetPlayMode((idx == TI_LEFT) ? PM_Goal_Right : PM_Goal_Left);    return true;}voidSoccerRuleAspect::UpdatePlayOn(){    // check if the ball is in one of the goals    if (CheckGoal())    {        return;    }    // check if the ball is otherwise not on the playing field    if (CheckBallLeftField())    {        return;    }#if 0    // check if the players are in offside    if (mUseOffside && CheckOffside())    {        return;    }#endif    // other checks go here...}voidSoccerRuleAspect::UpdateGoal(){    // check if the pause time after the goal has elapsed    if (mGameState->GetModeTime() < mGoalPauseTime)    {        return;    }    // put the ball back in the middle of the playing field    Vector3f pos(0,0,mBallRadius);    MoveBall(pos);    // kick off for the opposite team    // Original//     mGameState->SetPlayMode(//         mGameState->GetPlayMode() == PM_Goal_Left ?//         PM_KickOff_Right : PM_KickOff_Left//         );    // kick off for the opposite team    mGameState->KickOff(        mGameState->GetPlayMode() == PM_Goal_Left ?        TI_RIGHT : TI_LEFT        );}voidSoccerRuleAspect::UpdateGameOver(){    // wait for 10 seconds to finish    if (mGameState->GetModeTime() < 9)    {        return;    }    mGameState->Finish();    if (mGameState->GetModeTime() >= 10)    {        boost::shared_ptr<GameControlServer> gameControlServer =            shared_dynamic_cast<GameControlServer>(GetCore()->Get("/sys/server/gamecontrol"));        gameControlServer->Quit();    }}voidSoccerRuleAspect::CheckTime(){    TTime now = mGameState->GetTime();    TGameHalf half = mGameState->GetGameHalf();    if ((half == GH_FIRST) && (now >= mHalfTime))    {        if (mSingleHalfTime)        {            // we want to play only one half of the match            mGameState->SetPlayMode(PM_GameOver);        } else {            // the first game half is over            mGameState->SetPlayMode(PM_BeforeKickOff);            mGameState->SetGameHalf(GH_SECOND);        }    }    else if ((half == GH_SECOND) && (now >= 2 * mHalfTime))    {        // the game is over        mGameState->SetPlayMode(PM_GameOver);    }}voidSoccerRuleAspect::Update(float deltaTime){    if (        (mGameState.get() == 0) ||        (mBallState.get() == 0) ||        (mBallBody.get() == 0)        )    {        return;    }    CheckTime();    TPlayMode playMode = mGameState->GetPlayMode();    static bool updated = false;    mLastModeWasPlayOn = false;    switch (playMode)    {    case PM_BeforeKickOff:        // At the beginning of the match, we update the member variables        // with the values from the ruby script (once). At this point in time,        // the ruby script has definitely been processed.        if (! updated)        {            UpdateCachedInternal();            updated = true;        }        // Below is the check we do during before kick off mode.        UpdateBeforeKickOff();        break;    case PM_PlayOn:        UpdatePlayOn();        mLastModeWasPlayOn = true;        break;    case PM_KickOff_Left:        UpdateKickOff(TI_LEFT);        break;    case PM_KickOff_Right:        UpdateKickOff(TI_RIGHT);        break;    case PM_FREE_KICK_LEFT:        UpdateFreeKick(TI_LEFT);        break;    case PM_FREE_KICK_RIGHT:        UpdateFreeKick(TI_RIGHT);        break;    case PM_KickIn_Left:        UpdateKickIn(TI_LEFT);        break;    case PM_KickIn_Right:        UpdateKickIn(TI_RIGHT);        break;    case PM_GOAL_KICK_LEFT:        UpdateGoalKick(TI_LEFT);        break;    case PM_GOAL_KICK_RIGHT:        UpdateGoalKick(TI_RIGHT);        break;    case PM_CORNER_KICK_LEFT:        UpdateCornerKick(TI_LEFT);        break;    case PM_CORNER_KICK_RIGHT:        UpdateCornerKick(TI_RIGHT);        break;    case PM_Goal_Left:    case PM_Goal_Right:        UpdateGoal();        break;    case PM_OFFSIDE_LEFT:        UpdateOffside(TI_LEFT);        break;    case PM_OFFSIDE_RIGHT:        UpdateOffside(TI_RIGHT);        break;    case PM_GameOver:        UpdateGameOver();        break;    default:        GetLog()->Error()            << "ERROR: (SoccerRuleAspect) unknown play mode "            << playMode << "\n";        break;    }}voidSoccerRuleAspect::OnLink(){    SoccerControlAspect::OnLink();    mGameState = shared_dynamic_cast<GameStateAspect>        (GetControlAspect("GameStateAspect"));    if (mGameState.get() == NULL)    {        GetLog()->Error() << "(SoccerRuleAspect) ERROR: could not get GameStateAspect\n";    }    mBallState = shared_dynamic_cast<BallStateAspect>        (GetControlAspect("BallStateAspect"));    if (mBallState.get() == NULL)    {        GetLog()->Error() << "(SoccerRuleAspect) ERROR: could not get BallStateAspect\n";    }    SoccerBase::GetBallBody(*this,mBallBody);}voidSoccerRuleAspect::OnUnlink(){    SoccerControlAspect::OnUnlink();    mGameState.reset();    mBallState.reset();    mBallBody.reset();}voidSoccerRuleAspect::UpdateCachedInternal(){    SoccerBase::GetSoccerVar(*this,"BallRadius",mBallRadius);    SoccerBase::GetSoccerVar(*this,"RuleGoalPauseTime",mGoalPauseTime);    SoccerBase::GetSoccerVar(*this,"RuleKickInPauseTime",mKickInPauseTime);    SoccerBase::GetSoccerVar(*this,"RuleHalfTime",mHalfTime);    SoccerBase::GetSoccerVar(*this,"RuleDropBallTime",mDropBallTime);    SoccerBase::GetSoccerVar(*this,"FieldLength",mFieldLength);    SoccerBase::GetSoccerVar(*this,"FieldWidth",mFieldWidth);    SoccerBase::GetSoccerVar(*this,"GoalWidth",mGoalWidth);    SoccerBase::GetSoccerVar(*this,"FreeKickDistance",mFreeKickDist);    SoccerBase::GetSoccerVar(*this,"AutomaticKickOff",mAutomaticKickOff);    SoccerBase::GetSoccerVar(*this,"WaitBeforeKickOff",mWaitBeforeKickOff);    SoccerBase::GetSoccerVar(*this,"SingleHalfTime",mSingleHalfTime);    SoccerBase::GetSoccerVar(*this,"UseOffside",mUseOffside);    // set up bounding boxes for halfs and goal areas    // the right and the left half are intentionally oversized towards the sides and    // the end of the field so that no opponents sneak up from behind.    mRightHalf = salt::AABB2(Vector2f(0, -mFieldWidth/2.0 - 10.0),                             Vector2f(mFieldLength/2.0 + 10, mFieldWidth/2.0 + 10.0));    mLeftHalf = salt::AABB2(Vector2f(0, -mFieldWidth/2.0 - 10.0),

⌨️ 快捷键说明

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