soccerruleaspect.cpp
来自「ROBOCUP 仿真3D server 源码」· C++ 代码 · 共 1,158 行 · 第 1/3 页
CPP
1,158 行
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soccerruleaspect.cpp,v 1.26 2007/02/06 22:49:31 heshamebrahimi Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "soccerruleaspect.h"#include <salt/random.h>#include <zeitgeist/logserver/logserver.h>#include <oxygen/agentaspect/agentaspect.h>#include <oxygen/physicsserver/body.h>#include <oxygen/sceneserver/scene.h>#include <oxygen/gamecontrolserver/gamecontrolserver.h>#include <soccer/soccerbase/soccerbase.h>#include <soccer/gamestateaspect/gamestateaspect.h>#include <soccer/ballstateaspect/ballstateaspect.h>#include <soccer/agentstate/agentstate.h>using namespace oxygen;using namespace boost;using namespace std;using salt::Vector2f;using salt::Vector3f;SoccerRuleAspect::SoccerRuleAspect() : mBallRadius(0.111), mGoalPauseTime(3), mKickInPauseTime(1), mHalfTime(2.25 * 60), mFreeKickDist(9.15), mFreeKickMoveDist(15.15), mAutomaticKickOff(false), mWaitBeforeKickOff(1.0), mSingleHalfTime(false), mSayMsgSize(20), mAudioCutDist(50.0), mUseOffside(true), mFirstCollidingAgent(true), mNotOffside(false), mLastModeWasPlayOn(false){}SoccerRuleAspect::~SoccerRuleAspect(){}voidSoccerRuleAspect::MoveBall(const Vector3f& pos){ mBallBody->SetPosition(pos); mBallBody->SetVelocity(Vector3f(0,0,0)); mBallBody->SetAngularVelocity(Vector3f(0,0,0));}voidSoccerRuleAspect::MoveAgent(shared_ptr<Body> agent_body, const Vector3f& pos){ agent_body->SetPosition(pos); agent_body->SetVelocity(Vector3f(0,0,0)); agent_body->SetAngularVelocity(Vector3f(0,0,0));}voidSoccerRuleAspect::ClearPlayers(const salt::Vector3f& pos, float radius, float min_dist, TTeamIndex idx){ if (idx == TI_NONE || mBallState.get() == 0) return; std::list<boost::shared_ptr<AgentState> > agent_states; if (! SoccerBase::GetAgentStates(*mBallState, agent_states, idx)) return; salt::BoundingSphere sphere(pos, radius); boost::shared_ptr<oxygen::Transform> transform_parent; boost::shared_ptr<oxygen::Body> agent_body; std::list<boost::shared_ptr<AgentState> >::const_iterator i; for (i = agent_states.begin(); i != agent_states.end(); ++i) { SoccerBase::GetTransformParent(*(*i), transform_parent); // call GetAgentBody with matching AgentAspect SoccerBase::GetAgentBody(transform_parent, agent_body); // if agent is too close, move it away Vector3f new_pos = agent_body->GetPosition(); if (sphere.Contains(new_pos)) { float dist = salt::UniformRNG<>(min_dist, min_dist + 2.0)(); if (idx == TI_LEFT) { if (pos[0] - dist < -mFieldLength/2.0) { new_pos[1] = pos[1] < 0 ? pos[1] + dist : pos[1] - dist; } else { new_pos[0] = pos[0] - dist; } } else { if (pos[0] + dist > mFieldLength/2.0) { new_pos[1] = pos[1] < 0 ? pos[1] + dist : pos[1] - dist; } else { new_pos[0] = pos[0] + dist; } } new_pos[2] = 1.0; MoveAgent(agent_body, new_pos); } }}voidSoccerRuleAspect::ClearPlayers(const salt::AABB2& box, float min_dist, TTeamIndex idx){ if (idx == TI_NONE || mBallState.get() == 0) return; std::list<boost::shared_ptr<AgentState> > agent_states; if (! SoccerBase::GetAgentStates(*mBallState, agent_states, idx)) return; boost::shared_ptr<oxygen::Transform> transform_parent; boost::shared_ptr<oxygen::Body> agent_body; std::list<boost::shared_ptr<AgentState> >::const_iterator i; for (i = agent_states.begin(); i != agent_states.end(); ++i) { SoccerBase::GetTransformParent(*(*i), transform_parent); // call GetAgentBody with matching AgentAspect SoccerBase::GetAgentBody(transform_parent, agent_body); // if agent is too close, move it away Vector3f new_pos = agent_body->GetPosition(); if (box.Contains(Vector2f(new_pos[0], new_pos[1]))) { if (idx == TI_LEFT) { new_pos[0] = box.minVec[0] - salt::UniformRNG<>(min_dist, min_dist + 2.0)(); } else { new_pos[0] = box.maxVec[0] + salt::UniformRNG<>(min_dist, min_dist + 2.0)(); } new_pos[2] = 1.0; MoveAgent(agent_body, new_pos); } }}voidSoccerRuleAspect::DropBall(){ DropBall(mBallBody->GetPosition());}voidSoccerRuleAspect::DropBall(Vector3f pos){ salt::Vector2f ball_pos(pos.x(), pos.y()); // we do not drop the ball within the penalty area if (mLeftPenaltyArea.Contains(ball_pos)) { pos[0] = mLeftPenaltyArea.maxVec[0]; pos[1] = pos.y() < 0 ? mLeftPenaltyArea.minVec[1] : mLeftPenaltyArea.maxVec[1]; } else if (mRightPenaltyArea.Contains(ball_pos)) { pos[0] = mRightPenaltyArea.minVec[0]; pos[1] = pos.y() < 0 ? mRightPenaltyArea.minVec[1] : mRightPenaltyArea.maxVec[1]; } MoveBall(pos); ClearPlayers(pos, mFreeKickDist, mFreeKickMoveDist, TI_LEFT); ClearPlayers(pos, mFreeKickDist, mFreeKickMoveDist, TI_RIGHT); mGameState->SetPlayMode(PM_PlayOn);}voidSoccerRuleAspect::UpdateBeforeKickOff(){ // before the game starts the ball should stay in the middle of // the playing field Vector3f pos(0,0,mBallRadius); MoveBall(pos); ClearPlayers(mRightHalf, 1.0, TI_LEFT); ClearPlayers(mLeftHalf, 1.0, TI_RIGHT); // // TODO: this has to be tested (compiles and no crashes at least) mInOffsideLeftPlayers.clear(); mInOffsideRightPlayers.clear(); if (mAutomaticKickOff && mGameState->GetModeTime() > mWaitBeforeKickOff) { mGameState->KickOff(); }}voidSoccerRuleAspect::UpdateKickOff(TTeamIndex idx){ ClearPlayers(mRightHalf, 1.0, TI_LEFT); ClearPlayers(mLeftHalf, 1.0, TI_RIGHT); ClearPlayers(Vector3f(0,0,0), 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) { // Drop the ball at its current position. // This should always be (0,0) during kickoff. DropBall(mBallBody->GetPosition()); return; } // after the first agent touches the ball move to PM_PLAYON shared_ptr<AgentAspect> agent; TTime time; if (! mBallState->GetLastCollidingAgent(agent,time)) { return; } if (time > mGameState->GetLastModeChange()) { mGameState->SetPlayMode(PM_PlayOn); }}voidSoccerRuleAspect::UpdateKickIn(TTeamIndex idx){ // 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)) { GetLog()->Error() << "ERROR: (SoccerRuleAspect) " << "no agent collided yet\n"; return; } TTime lastChange = mGameState->GetLastModeChange(); if (time > lastChange) { mGameState->SetPlayMode(PM_PlayOn); GetLog()->Error() << "ERROR: (SoccerRuleAspect) " << "Set Playmode to playon\n"; } else { // move the ball back on the ground where it left the playing // field MoveBall(mFreeKickPos); }}voidSoccerRuleAspect::UpdateGoalKick(TTeamIndex idx){ // 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); }}voidSoccerRuleAspect::UpdateCornerKick(TTeamIndex idx){ // 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);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?