From: Oliver O. <fr...@us...> - 2007-03-07 10:39:37
|
Update of /cvsroot/simspark/simspark/simulations/soccer/plugin/gamestateaspect In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv24718/gamestateaspect Added Files: Tag: projectx gamestateaspect.cpp gamestateaspect.h gamestateaspect_c.cpp gamestateitem.cpp gamestateitem.h gamestateitem_c.cpp Log Message: soccer plugins from rcssserver3D --- NEW FILE: gamestateitem.cpp --- /* -*- 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: gamestateitem.cpp,v 1.1.2.1 2007/03/07 10:39:32 fruit 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 "gamestateitem.h" #include "gamestateaspect.h" #include <soccer/soccerbase/soccerbase.h> using namespace oxygen; using namespace std; GameStateItem::GameStateItem() : MonitorItem() { ResetSentFlags(); } GameStateItem::~GameStateItem() { } void GameStateItem::ResetSentFlags() { mSentLeftTeamname = false; mSentRightTeamname = false; mLastHalf = GH_NONE; mLastLeftScore = -1; mLastRightScore = -1; mLastPlayMode = PM_NONE; mSentFlags = false; } void GameStateItem::PutFloatParam(const string& name, PredicateList& pList) { float value; if (! SoccerBase::GetSoccerVar(*this,name,value)) { return; } Predicate& pred = pList.AddPredicate(); pred.name = name; pred.parameter.AddValue(value); } void GameStateItem::GetInitialPredicates(PredicateList& pList) { ResetSentFlags(); // field geometry parameter PutFloatParam("FieldLength",pList); PutFloatParam("FieldWidth",pList); PutFloatParam("FieldHeight",pList); PutFloatParam("GoalWidth",pList); PutFloatParam("GoalDepth",pList); PutFloatParam("GoalHeight",pList); PutFloatParam("BorderSize",pList); PutFloatParam("FreeKickDistance",pList); PutFloatParam("WaitBeforeKickOff",pList); // agent parameter PutFloatParam("AgentMass",pList); PutFloatParam("AgentRadius",pList); PutFloatParam("AgentMaxSpeed",pList); // ball parameter PutFloatParam("BallRadius",pList); PutFloatParam("BallMass",pList); // soccer rule parameters PutFloatParam("RuleGoalPauseTime",pList); PutFloatParam("RuleKickInPauseTime",pList); PutFloatParam("RuleHalfTime",pList); // play modes Predicate& pred = pList.AddPredicate(); pred.name = "play_modes"; for (int i=0; i<PM_NONE; ++i) { pred.parameter.AddValue (SoccerBase::PlayMode2Str(static_cast<TPlayMode>(i))); } } void GameStateItem::GetPredicates(PredicateList& pList) { if (mGameState.get() == 0) { return; } Predicate& timePred = pList.AddPredicate(); timePred.name = "time"; timePred.parameter.AddValue(mGameState->GetTime()); if (! mSentLeftTeamname) { // team names string name = mGameState->GetTeamName(TI_LEFT); if (! name.empty()) { Predicate& teamPredLeft = pList.AddPredicate(); teamPredLeft.name = "team_left"; teamPredLeft.parameter.AddValue(name); mSentLeftTeamname = true; } } if (! mSentRightTeamname) { // team names string name = mGameState->GetTeamName(TI_RIGHT); if (! name.empty()) { Predicate& teamPredRight = pList.AddPredicate(); teamPredRight.name = "team_right"; teamPredRight.parameter.AddValue(name); mSentRightTeamname = true; } } // game half TGameHalf half = mGameState->GetGameHalf(); if (half != mLastHalf) { mLastHalf = half; Predicate& halfPred = pList.AddPredicate(); halfPred.name = "half"; halfPred.parameter.AddValue(static_cast<int>(half)); } // scores int left_score = mGameState->GetScore(TI_LEFT); if (left_score != mLastLeftScore) { mLastLeftScore = left_score; Predicate& scoreLeftPred = pList.AddPredicate(); scoreLeftPred.name = "score_left"; scoreLeftPred.parameter.AddValue(left_score); } int right_score = mGameState->GetScore(TI_RIGHT); if (right_score != mLastRightScore) { mLastRightScore = right_score; Predicate& scoreRightPred = pList.AddPredicate(); scoreRightPred.name = "score_right"; scoreRightPred.parameter.AddValue(right_score); } // gamestate TPlayMode play_mode = mGameState->GetPlayMode(); if (play_mode != mLastPlayMode) { mLastPlayMode = play_mode; Predicate& modePred = pList.AddPredicate(); modePred.name = "play_mode"; modePred.parameter.AddValue(static_cast<int>(play_mode)); } } void GameStateItem::OnLink() { SoccerBase::GetGameState(*this,mGameState); } void GameStateItem::OnUnlink() { mGameState.reset(); } --- NEW FILE: gamestateitem_c.cpp --- /* -*- 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: gamestateitem_c.cpp,v 1.1.2.1 2007/03/07 10:39:33 fruit 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 "gamestateitem.h" using namespace oxygen; void CLASS(GameStateItem)::DefineClass() { DEFINE_BASECLASS(MonitorItem); } --- NEW FILE: gamestateaspect_c.cpp --- /* -*- 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: gamestateaspect_c.cpp,v 1.1.2.1 2007/03/07 10:39:32 fruit 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 "gamestateaspect.h" using namespace oxygen; FUNCTION(GameStateAspect,kickOff) { obj->KickOff(); return true; } void CLASS(GameStateAspect)::DefineClass() { DEFINE_BASECLASS(SoccerControlAspect); DEFINE_FUNCTION(kickOff); } --- NEW FILE: gamestateitem.h --- /* -*- 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: gamestateitem.h,v 1.1.2.1 2007/03/07 10:39:32 fruit 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. */ #ifndef GAMESTATEITEM_H #define GAMESTATEITEM_H #include <oxygen/monitorserver/monitoritem.h> #include <soccer/soccerbase/soccerbase.h> class GameStateAspect; class GameStateItem : public oxygen::MonitorItem { public: GameStateItem(); virtual ~GameStateItem(); /** This function is called once for every MonitorSystem each time * a new client connects. It should append predicates to a list * that is sent using the active monitor */ virtual void GetInitialPredicates(oxygen::PredicateList& pList); /** This function will be called periodically to append predicates to a list that is sent using the active monitor */ virtual void GetPredicates(oxygen::PredicateList& pList); /** sets the reference to the GameStateAspect */ virtual void OnLink(); /** resets the reference to the GameStateAspect */ virtual void OnUnlink(); protected: void ResetSentFlags(); /** helper method that queries the ScriptServer for the float variable name and appends a predicate describing it to pList */ void PutFloatParam(const std::string& name, oxygen::PredicateList& pList); protected: boost::shared_ptr<GameStateAspect> mGameState; // flags for sent information //! flag if we already sent the left teamname bool mSentLeftTeamname; //! flag if we already sent the left teamname bool mSentRightTeamname; //! the last half sent out to monitors TGameHalf mLastHalf; //! the last left score sent out to monitors int mLastLeftScore; //! the last right score sent out to monitors int mLastRightScore; //! the last playmode sent out to monitors TPlayMode mLastPlayMode; //! flag if the monitors received field flags information bool mSentFlags; }; DECLARE_CLASS(GameStateItem) #endif // GAMESTATEITEM_H --- NEW FILE: gamestateaspect.cpp --- /* -*- 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: gamestateaspect.cpp,v 1.1.2.1 2007/03/07 10:39:31 fruit 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 "gamestateaspect.h" #include <zeitgeist/logserver/logserver.h> #include <soccer/soccerbase/soccerbase.h> #include <soccer/agentstate/agentstate.h> #include <salt/random.h> using namespace oxygen; using namespace boost; using namespace std; using namespace salt; GameStateAspect::GameStateAspect() : SoccerControlAspect() { mPlayMode = PM_BeforeKickOff; mTime = 0; mLeadTime = 0; mFupTime = 0; mLastModeChange = 0; mGameHalf = GH_FIRST; mScore[0] = 0; mScore[1] = 0; mMaxUnum[0] = 0; mMaxUnum[1] = 0; mLastKickOff = TI_NONE; //mSecondHalfKickOff = TI_NONE; mLeftInit = Vector3f(0,0,0); mRightInit = Vector3f(0,0,0); mFinished = false; } GameStateAspect::~GameStateAspect() { } void GameStateAspect::UpdateTime(float deltaTime) { switch (mPlayMode) { case PM_BeforeKickOff: mLeadTime += deltaTime; break; case PM_GameOver: mFupTime += deltaTime; break; default: mTime += deltaTime; } } void GameStateAspect::Update(float deltaTime) { UpdateTime(deltaTime); } TPlayMode GameStateAspect::GetPlayMode() const { return mPlayMode; } void GameStateAspect::SetPlayMode(TPlayMode mode) { if (mode == mPlayMode) { return; } GetLog()->Normal() << "(GameStateAspect) playmode changed to " << SoccerBase::PlayMode2Str(mode) << " at t=" << mTime << "\n"; mPlayMode = mode; mLastModeChange = mTime; mLeadTime = 0.0; mFupTime = 0.0; } // let the monitor handle who kicks off in 2nd half. void GameStateAspect::KickOff(TTeamIndex ti) { // throw a coin to determine which team kicks off if (ti == TI_NONE) { ti = (salt::UniformRNG<>(0,1)() <= 0.5) ? TI_LEFT : TI_RIGHT; } SetPlayMode((ti == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right); if (mLastKickOff == TI_NONE) mLastKickOff = ti; } // void // GameStateAspect::KickOff(TTeamIndex ti) // { // if (mGameHalf == GH_FIRST) // { // // throw a coin to determine which team kicks off // if (ti == TI_NONE) // { // ti = (salt::UniformRNG<>(0,1)() <= 0.5) ? TI_LEFT : TI_RIGHT; // } // SetPlayMode((ti == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right); // if (mLastKickOff == TI_NONE) // mLastKickOff = ti; // } // else // { // // in the second half, let the opposite team kick off // SetPlayMode((mLastKickOff == TI_LEFT) ? PM_KickOff_Right : PM_KickOff_Left); // } // } //--------------------------------------------- // void // GameStateAspect::KickOff(TTeamIndex ti) // { // if (mGameHalf == GH_FIRST) // { // // throw a coin to determine which team kicks off // if (ti == TI_NONE) // { // ti = (salt::UniformRNG<>(0,1)() <= 0.5) ? TI_LEFT : TI_RIGHT; // } // SetPlayMode((ti == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right); // mLastKickOff = ti; // if (mSecondHalfKickOff == TI_NONE) // { // //clog << "setting mSecondHalfKickOff\n"; // mSecondHalfKickOff = // (mLastKickOff == TI_LEFT) ? TI_RIGHT : TI_LEFT; // } // } // else // { // // in the second half, let the opposite team kick off // SetPlayMode((mSecondHalfKickOff == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right); // } // } //--------------------------------------------- TTime GameStateAspect::GetTime() const { return mTime; } TTime GameStateAspect::GetModeTime() const { switch (mPlayMode) { case PM_BeforeKickOff: return mLeadTime; case PM_GameOver: return mFupTime; default: return mTime - mLastModeChange; } } TTime GameStateAspect::GetLastModeChange() const { return mLastModeChange; } void GameStateAspect::SetTeamName(TTeamIndex idx, const std::string& name) { switch (idx) { case TI_LEFT: mTeamName[0] = name; break; case TI_RIGHT: mTeamName[1] = name; break; } return; } std::string GameStateAspect::GetTeamName(TTeamIndex idx) const { switch (idx) { case TI_LEFT: return mTeamName[0]; case TI_RIGHT: return mTeamName[1]; default: return ""; } } TTeamIndex GameStateAspect::GetTeamIndex(const std::string& teamName) { for (int i=0; i<=1; ++i) { if (mTeamName[i].empty()) { mTeamName[i] = teamName; return static_cast<TTeamIndex>(i + TI_LEFT); } if (mTeamName[i] == teamName) { return static_cast<TTeamIndex>(i + TI_LEFT); } } return TI_NONE; } bool GameStateAspect::InsertUnum(TTeamIndex idx, int unum) { int i; switch (idx) { case TI_LEFT: i = 0; break; case TI_RIGHT: i = 1; break; default: return false; } TUnumSet& set = mUnumSet[i]; if ( (set.size() >= 11) || (set.find(unum) != set.end()) ) { return false; } set.insert(unum); mMaxUnum[i] = std::max<int>(unum, mMaxUnum[i]); return true; } bool GameStateAspect::RequestUniform(shared_ptr<AgentState> agentState, std::string teamName, unsigned int unum) { if (agentState.get() == 0) { return false; } TTeamIndex idx = GetTeamIndex(teamName); if (idx == TI_NONE) { GetLog()->Error() << "ERROR: (GameStateAspect::RequestUniform) invalid teamname " << teamName << "\n"; return false; } if (unum == 0) { unum = RequestUniformNumber(idx); } if (! InsertUnum(idx,unum)) { GetLog()->Error() << "ERROR: (GameStateAspect::RequestUniform) cannot insert uniform" " number " << unum << " to team " << teamName << "\n"; return false; } agentState->SetUniformNumber(unum); agentState->SetTeamIndex(idx); //agentState->SetPerceptName(teamName, ObjectState::PT_Default); agentState->SetPerceptName(teamName, ObjectState::PT_Default, ObjectState::PT_Player ); agentState->SetPerceptName("player", ObjectState::PT_TooFar); GetLog()->Normal() << "(GameStateAspect) handed out uniform number " << unum << " for team " << teamName << "\n"; return true; } void GameStateAspect::SetGameHalf(TGameHalf half) { if ( (half != GH_FIRST) && (half != GH_SECOND) ) { return; } mGameHalf = half; } TGameHalf GameStateAspect::GetGameHalf() const { return mGameHalf; } void GameStateAspect::ScoreTeam(TTeamIndex idx) { switch (idx) { case TI_LEFT: ++mScore[0]; break; case TI_RIGHT: ++mScore[1]; break; } return; } int GameStateAspect::GetScore(TTeamIndex idx) const { switch (idx) { case TI_LEFT: return mScore[0]; case TI_RIGHT: return mScore[1]; default: return 0; } } Vector3f GameStateAspect::RequestInitPosition(const TTeamIndex ti) { if (ti == TI_NONE) { GetLog()->Debug() << "(GameStateAspect) RequestInitPosition called with " << "ti=TI_NONE\n"; return Vector3f(0,0,10); } salt::Vector3f& init = (ti ==TI_LEFT) ? mLeftInit : mRightInit; Vector3f pos = init; init[1] -= mAgentRadius * 3; return pos; } void GameStateAspect::OnLink() { // setup the initial starting positions for the agents float fieldWidth = 64.0; SoccerBase::GetSoccerVar(*this,"FieldWidth",fieldWidth); float fieldLength = 100.0; SoccerBase::GetSoccerVar(*this,"FieldLength",fieldLength); mAgentRadius = 0.22; SoccerBase::GetSoccerVar(*this,"AgentRadius",mAgentRadius); mLeftInit = Vector3f ( -fieldLength/2.0 + mAgentRadius*2, fieldWidth/2 - mAgentRadius*2, mAgentRadius ); mRightInit = Vector3f ( +fieldLength/2.0 - mAgentRadius*2, fieldWidth/2 - mAgentRadius*2, mAgentRadius ); } int GameStateAspect::RequestUniformNumber(TTeamIndex ti) const { switch (ti) { case TI_LEFT: return mMaxUnum[0] + 1; case TI_RIGHT: return mMaxUnum[1] + 1; default: return 0; } } --- NEW FILE: gamestateaspect.h --- /* -*- 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: gamestateaspect.h,v 1.1.2.1 2007/03/07 10:39:31 fruit 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. */ #ifndef GAMESTATEASPECT_H #define GAMESTATEASPECT_H #include <soccer/soccercontrolaspect/soccercontrolaspect.h> #include <set> class AgentState; class GameStateAspect : public SoccerControlAspect { protected: typedef std::set<int> TUnumSet; public: GameStateAspect(); virtual ~GameStateAspect(); /** called during the update of the GameControlServer to allow the ControlAspect to perform any necessary checks. */ virtual void Update(float deltaTime); /** Used by the SexpMonitor to check if the terminating message should be sent. \return true if the match is over */ bool IsFinished() const { return mFinished; } /** finish the simulation */ void Finish() { mFinished = true; } /** returns the current play mode */ TPlayMode GetPlayMode() const; /** sets the current play mode */ void SetPlayMode(TPlayMode mode); /** set the current play mode randomly to PM_KickOff_Left or PM_KickOff_Right. \param ti Team which should do the kick off (if TI_NONE, then do a coin toss) */ void KickOff(TTeamIndex ti = TI_NONE); /** returns the current game time */ TTime GetTime() const; /** returns the time passed since the last playmode change */ TTime GetModeTime() const; /** returns the time of the last playmode change */ TTime GetLastModeChange() const; /** sets the current game half */ void SetGameHalf(TGameHalf half); /** returns the current game half */ TGameHalf GetGameHalf() const; /** sets the name of a team */ void SetTeamName(TTeamIndex idx, const std::string& name); /** returns the name of a team */ std::string GetTeamName(TTeamIndex idx) const; /** called from the InitEffector to request a uniformn number and teamname */ bool RequestUniform(boost::shared_ptr<AgentState> agentState, std::string teamName, unsigned int unum); /** returns the next uniform number not taken for the given team */ int RequestUniformNumber(TTeamIndex ti) const; /** called from the InitEffector to request an initial position for an agent */ salt::Vector3f RequestInitPosition(const TTeamIndex ti); /** increments the score of a team */ void ScoreTeam(TTeamIndex idx); /** returns the score of a team */ int GetScore(TTeamIndex idx) const; protected: /** setup the init positions for the agents */ virtual void OnLink(); /** advances the game time */ void UpdateTime(float deltaTime); /** checks if the set of uniform numbers of given team already contains a uniform number unum and inserts it. */ bool InsertUnum(TTeamIndex idx, int unum); /** returns the team index corresponding to the given teamName. If the teamname does not exist and less than two teams are registered, the given team name is registered. */ TTeamIndex GetTeamIndex(const std::string& teamName); protected: /** the current play mode */ TPlayMode mPlayMode; /** the last time the play mode changed */ TTime mLastModeChange; /** the current game time */ TTime mTime; /** the current time before the match started */ TTime mLeadTime; /** the time in game state Game Over */ TTime mFupTime; /** the current half of the game */ TGameHalf mGameHalf; /** the team that had the last KickOff */ TTeamIndex mLastKickOff; // /** the team that has to start the second half */ // TTeamIndex mSecondHalfKickOff; /** the names of the two teams */ std::string mTeamName[2]; /** the set of uniform number for each team */ TUnumSet mUnumSet[2]; /** the maximal uniform number handed out for each team */ int mMaxUnum[2]; /** the scores of two teams */ int mScore[2]; /** the position where the last left player was put initially */ salt::Vector3f mLeftInit; /** the position where the last right player was put initially */ salt::Vector3f mRightInit; /** the radius of an agent */ float mAgentRadius; /** flag if the simulation should be stopped */ bool mFinished; }; DECLARE_CLASS(GameStateAspect); #endif // GAMESTATEASPECT_H |