|
From: Markus R. <rol...@us...> - 2005-12-05 21:16:59
|
Update of /cvsroot/simspark/simspark/spark/oxygen/gamecontrolserver In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14301/gamecontrolserver Added Files: actionobject.h actionobject_c.cpp baseparser.h baseparser_c.cpp gamecontrolserver.cpp gamecontrolserver.h gamecontrolserver_c.cpp predicate.cpp predicate.h Log Message: --- NEW FILE: actionobject.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: actionobject.h,v 1.1 2005/12/05 21:16:49 rollmark 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 OXYGEN_ACTIONOBJECT_H #define OXYGEN_ACTIONOBJECT_H #include <zeitgeist/object.h> #include <zeitgeist/class.h> namespace oxygen { #if 0 } #endif class ActionObject : public zeitgeist::Object { public: typedef std::list<boost::shared_ptr<ActionObject> > TList; public: ActionObject(const std::string& predicate) : Object(), mPredicate(predicate) {} virtual ~ActionObject() {} //! returns the described predicate std::string GetPredicate() { return mPredicate; } protected: //! the predicate a derived ActionObject describes std::string mPredicate; }; DECLARE_ABSTRACTCLASS(ActionObject); } // namespace oxygen #endif // OXYGEN_ACTIONOBJECT_H --- NEW FILE: gamecontrolserver_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: gamecontrolserver_c.cpp,v 1.1 2005/12/05 21:16:49 rollmark 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 "gamecontrolserver.h" using namespace oxygen; using namespace std; FUNCTION(GameControlServer,initParser) { string inParserName; return( (in.GetSize() == 1) && (in.GetValue(in.begin(),inParserName)) && (obj->InitParser(inParserName)) ); } FUNCTION(GameControlServer,initEffector) { string inEffectorName; if ( (in.GetSize() != 1) || (! in.GetValue(in.begin(),inEffectorName)) ) { return false; } obj->InitEffector(inEffectorName); return true; } FUNCTION(GameControlServer,initControlAspect) { string inAspectName; return( (in.GetSize() == 1) && (in.GetValue(in.begin(),inAspectName)) && (obj->InitControlAspect(inAspectName)) ); } void CLASS(GameControlServer)::DefineClass() { DEFINE_BASECLASS(zeitgeist/Node); DEFINE_FUNCTION(initParser); DEFINE_FUNCTION(initEffector); DEFINE_FUNCTION(initControlAspect); } --- NEW FILE: predicate.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) 2004 RoboCup Soccer Server 3D Maintenance Group $Id: predicate.h,v 1.1 2005/12/05 21:16:49 rollmark 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 OXYGEN_PREDICATE_H #define OXYGEN_PREDICATE_H #include <list> #include <string> #include <functional> #include <boost/any.hpp> #include <salt/vector.h> #include <zeitgeist/logserver/logserver.h> namespace oxygen { /** \class Predicate encapsulates a predicate name together with its list of parameters. It is used as an internal representation for commands issued by agents and is interpreted by Effector classes. A BaseParser object is responsible to translate the received agent commands into a list of Predicates. An Effector is therefore independent from the command format used between an agent and the simulator. Predicates are further used to collect sensor data from the agents perceptors. A BaseParser is then used to translate these percepts into the format used to describe them to the corresponding agent. Again, the Perceptor is independent from the format used to transmit its data to the agent. */ class Predicate { public: /** \class Iterator encapsulates a ParameterList::TVectr::const_iterator together with a reference to the ParameterList the iterator belongs to. It tries to mimic the behaviour of an STL iterator as much as possible. This class is necessary because it is not sufficient for the FindParameter method to return a single iterator without the corresponding list as it is possible for list to be nested. A subsequent GetValue method would not be able to test if the end of a TParameterList is reached without knowing the list an iterator belongs to. */ struct Iterator { public: /** constructs an Iterator pointing to element i of list l */ Iterator(const zeitgeist::ParameterList* l, zeitgeist::ParameterList::TVector::const_iterator i); /** constructs an Iterator pointing to the first element of list l */ Iterator::Iterator(const zeitgeist::ParameterList* l); /** constructs an Iterator pointing to the first element of predicate::parameter */ Iterator(const Predicate& predicate); /** construct an Iterator pointing to Predicate::nullParamList. This empty static parameter list acts as a null element and avoids special cases for list==0 in the Iterator implementation. */ Iterator(); /** aeturns the element this Iterator points to */ const boost::any& operator * () const; /** advances this Iterator on element if possible */ void operator ++ (); /** constructs an Iterator pointing to the first element of the associated list */ Iterator begin() const; /** constructs an Iterator pointing to the end() element of the associated list */ Iterator end() const; /** \returns true if this Iterator and i are not identical */ bool operator != (const Iterator& i) const; /** \returns true if this Iterator and i are identical */ bool operator == (const Iterator& i) const; /** \returns a reference to the encapuslated iterator */ const zeitgeist::ParameterList::TVector::const_iterator& GetIterator() const; /** \returns a reference to the encapsulated iterator */ zeitgeist::ParameterList::TVector::const_iterator& GetIterator(); /** \returns a pointer to the associated list */ const zeitgeist::ParameterList* GetList() const; protected: /** the associated list */ const zeitgeist::ParameterList* list; /** the encapsulated iterator */ zeitgeist::ParameterList::TVector::const_iterator iter; }; public: const Iterator begin() const { return Iterator(¶meter,parameter.begin()); } /** find a parameter with a given name. For this method, we assume that parameters are represented by lists consisting of a name (a string) and (a number of) values. An example for the predicate init: init.name = "init"; init.parameter = [["teamname", "RoboLog"], ["unum", 1]]; The part after init is the list of parameters. To extract the teamname, do something like Predicate::iterator iter(myPredicate); if (FindParameter(iter, "teamname")) { GetValue(iter,name); } \param iter on success, iter will point to the first value of the parameter you are looking for. It will be unchanged otherwise. \param name Name of the parameter to find. \return true if parameter name was found. */ bool FindParameter(Iterator& iter, const std::string& name) const; /** finds and returns the first value of a parameter with a given name. This method tries to cover the most common use case of a ParameterList. It assumes that the parameters are a set lists consisting of name value pairs, e.g. for the predicate init init.name = "init"; init.parameter = [["teamname", "RoboLog"], ["unum", 1]]; For parameter entries with more than one value per parameter use the generic FindParameter/GetValue functions. \param name Name of the parameter to find. \param value reference to the value that will receive the parameter value on success \return true if parameter name was found. */ template<typename T> f_inline bool GetValue(const Iterator& iter, const std::string& name, T& value) const { Iterator tmp(iter); return AdvanceValue(tmp,name,value); } /** finds and returns the first value of a parameter with a given name. On success value contains the first parameter value and iter points to the second parameter value */ template<typename T> f_inline bool AdvanceValue(Iterator& iter, const std::string&name, T& value) const { if (! FindParameter(iter,name)) { return false; } return AdvanceValue(iter,value); } /** AdvanceValue tries to cast the parameter at iter to a value of type T. If successful it returns true, assigns the casted parameter to value and increments the iterator iter. Otherwise false is returned and value and iter are unchanged. */ template<typename T> f_inline bool AdvanceValue(Iterator& iter, T& value) const { return iter.GetList()->AdvanceValue(iter.GetIterator(),value); } /** GetValue has the same semantics as AdvanceValue except that it takes a const reference to iter that it does not increment. */ template<typename T> f_inline bool GetValue(const Iterator& iter, T& value) const { return iter.GetList()->GetValue(iter.GetIterator(),value); } template<typename T> f_inline bool GetAnyValue(const Iterator& iter, T& value) const { return iter.GetList()->GetAnyValue(iter.GetIterator(),value); } /** tries to inteprete the value iter points to as a ParameterList and points iter to it's first element. Returns true on success */ bool DescentList(Iterator& iter) const; public: /** the name of this predicate */ std::string name; /** the list of parameter values */ zeitgeist::ParameterList parameter; protected: static const zeitgeist::ParameterList nullParamList; }; /** A functional class to find a specific parameter for use as STL predicate. */ class ParameterName : public std::binary_function<boost::any,std::string,bool> { public: bool operator()(const boost::any& param, const std::string& pred) const; }; class PredicateList { public: typedef std::list<Predicate> TList; public: PredicateList(); virtual ~PredicateList(); /** returns an iterator pointing at the first contained Predicate */ TList::const_iterator begin() const; /** returns an iterator pointing at the last contained Predicate */ TList::const_iterator end() const; /** returns the number of values contained in the managed sequence */ int GetSize() const; /** removes all contained Predicates */ void Clear(); Predicate& AddPredicate(); protected: TList mList; }; } // namespace oxygen #endif // OXYGEN_PREDICATE_H --- NEW FILE: baseparser.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: baseparser.h,v 1.1 2005/12/05 21:16:49 rollmark 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 OXYGEN_BASEPARSER_H #define OXYGEN_BASEPARSER_H #include <zeitgeist/class.h> #include <zeitgeist/node.h> #include "predicate.h" namespace oxygen { /** \class BaseParser defines the interface for a parser and generator that is used with the GameControlServer. A BaseParser is responsible to parse a string into a list of predicates (see \class Predicate) and to assemble a string representing a given list of predicates. */ class BaseParser : public zeitgeist::Leaf { public: /** parses the \param input string into a list of Predicates */ virtual boost::shared_ptr<PredicateList> Parse(const std::string& input) = 0; /** generates a string representing the given \param input list of predicates */ virtual std::string Generate(boost::shared_ptr<PredicateList> input) = 0; }; DECLARE_ABSTRACTCLASS(BaseParser); } // namespace oxygen #endif // OXYGEN_BASEPARSER_H --- NEW FILE: predicate.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: predicate.cpp,v 1.1 2005/12/05 21:16:49 rollmark 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 "predicate.h" using namespace zeitgeist; using namespace oxygen; using namespace boost; using namespace std; /** The null element for Predicate::Iterator */ const zeitgeist::ParameterList Predicate::nullParamList; Predicate::Iterator::Iterator(const ParameterList* l, ParameterList::TVector::const_iterator i) : list(l),iter(i) {}; Predicate::Iterator::Iterator(const ParameterList* l) : list(l),iter(l->begin()) {} Predicate::Iterator::Iterator(const Predicate& predicate) : list(&predicate.parameter), iter(predicate.parameter.begin()) {} Predicate::Iterator::Iterator() : list(&nullParamList), iter(nullParamList.begin()) {}; const boost::any& Predicate::Iterator::operator * () const { return (*iter); } void Predicate::Iterator::operator ++ () { if (iter != list->end()) { ++iter; } } Predicate::Iterator Predicate::Iterator::begin() const { return Iterator(list, list->begin()); } Predicate::Iterator Predicate::Iterator::end() const { return Iterator(list, list->end()); } bool Predicate::Iterator::operator != (const Iterator& i) const { return ( (list != i.list) || (iter != i.iter) ); } bool Predicate::Iterator::operator == (const Iterator& i) const { return ( (list == i.list) && (iter == i.iter) ); } const ParameterList::TVector::const_iterator& Predicate::Iterator::GetIterator() const { return iter; } ParameterList::TVector::const_iterator& Predicate::Iterator::GetIterator() { return iter; } const ParameterList* Predicate::Iterator::GetList() const { return list; } /** implementation of class ParameterName */ bool ParameterName::operator()(const boost::any& param, const string& pred) const { try { // try get a ParameterList as an element const any* v = ¶m; const ParameterList* lst = any_cast<ParameterList>(v); if ( (lst == 0) || (lst->IsEmpty())) { return false; } string s; lst->GetValue(lst->begin(),s); return (pred == s); } catch(const boost::bad_any_cast &) { return false; } } bool Predicate::FindParameter(Iterator& iter, const string& name) const { const ParameterList* list = iter.GetList(); Iterator test ( list, find_if( list->begin(), list->end(), bind2nd(ParameterName(), name) ) ); if (test == test.end()) { return false; } // try to extract the first element as a parameter list const ParameterList* paramList = boost::any_cast<ParameterList>(&(*test)); if ( (paramList == 0) || (paramList->GetSize() < 2) ) { return false; } // return an iterator to the second element of the list, i.e. the // first parameter value iter = Iterator(paramList,paramList->begin()); ++iter; return true; } bool Predicate::DescentList(Iterator& iter) const { try { const any* v = &(*iter.GetIterator()); const ParameterList* l = any_cast<ParameterList>(v); iter = Iterator(l); return true; } catch(const std::bad_cast&) { return false; } } /** implementation of class PredicateList */ PredicateList::PredicateList() { } PredicateList::~PredicateList() { } PredicateList::TList::const_iterator PredicateList::begin() const { return mList.begin(); } PredicateList::TList::const_iterator PredicateList::end() const { return mList.end(); } Predicate& PredicateList::AddPredicate() { mList.push_back(Predicate()); return mList.back(); } int PredicateList::GetSize() const { return mList.size(); } void PredicateList::Clear() { mList.clear(); } --- NEW FILE: baseparser_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: baseparser_c.cpp,v 1.1 2005/12/05 21:16:49 rollmark 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 "baseparser.h" using namespace oxygen; void CLASS(BaseParser)::DefineClass() { DEFINE_BASECLASS(zeitgeist/Leaf); } --- NEW FILE: gamecontrolserver.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) 2004 RoboCup Soccer Server 3D Maintenance Group $Id: gamecontrolserver.cpp,v 1.1 2005/12/05 21:16:49 rollmark 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 <sstream> #include "gamecontrolserver.h" #include "baseparser.h" #include <oxygen/agentaspect/agentaspect.h> #include <oxygen/sceneserver/sceneserver.h> #include <oxygen/sceneserver/scene.h> #include <oxygen/controlaspect/controlaspect.h> #include <zeitgeist/logserver/logserver.h> #include <zeitgeist/scriptserver/scriptserver.h> #include <zeitgeist/corecontext.h> using namespace oxygen; using namespace zeitgeist; using namespace boost; using namespace std; GameControlServer::GameControlServer() : zeitgeist::Node() { mExit = false; } GameControlServer::~GameControlServer() { } bool GameControlServer::InitParser(const std::string& parserName) { mParser = shared_dynamic_cast<BaseParser>(GetCore()->New(parserName)); if (mParser.get() == 0) { GetLog()->Error() << "ERROR: (GameControlServer::InitParser) Unable to create " << parserName << "\n"; return false; } return true; } void GameControlServer::InitEffector(const std::string& effectorName) { mCreateEffector = effectorName; } bool GameControlServer::InitControlAspect(const string& aspectName) { shared_ptr<ControlAspect> aspect = shared_dynamic_cast<ControlAspect>(GetCore()->New(aspectName)); if (aspect.get() == 0) { GetLog()->Error() << "ERROR: (GameControlServer::InitControlAspect) " << "Unable to create " << aspectName << "\n"; return false; } aspect->SetName(aspectName); AddChildReference(aspect); return true; } shared_ptr<BaseParser> GameControlServer::GetParser() { return mParser; } shared_ptr<Scene> GameControlServer::GetActiveScene() { shared_ptr<SceneServer> sceneServer = shared_dynamic_cast<SceneServer>(GetCore()->Get("/sys/server/scene")); if (sceneServer.get() == 0) { GetLog()->Error() << "ERROR: (GameControlServer) SceneServer not found.\n"; return shared_ptr<Scene>(); } shared_ptr<Scene> scene = sceneServer->GetActiveScene(); if (scene.get() == 0) { GetLog()->Error() << "ERROR: (GameControlServer) SceneServer " << "reports no active scene\n"; } return scene; } bool GameControlServer::AgentConnect(int id) { // for map::insert(Elem), the test here is not required. map::insert does not // overwrite existing elements. The test is required to report if the agent // was already connected or not. if (mAgentMap.find(id) != mAgentMap.end()) { return false; } GetLog()->Normal() << "(GameControlServer) a new agent connected (id: " << id << ")\n"; shared_ptr<Scene> scene = GetActiveScene(); if (scene.get() == 0) { GetLog()->Error() << "(GameControlServer) ERROR: Got no active scene from the " << "SceneServer to create the AgentAspect in.\n"; return false; } // create a new AgentAspect for the ID in the scene and add it to // our map of AgentAspects shared_ptr<AgentAspect> aspect = shared_dynamic_cast<AgentAspect> (GetCore()->New("oxygen/AgentAspect")); if (aspect.get() == 0) { GetLog()->Error() << "ERROR: (GameControlServer) cannot create new AgentAspect\n"; return false; } stringstream name; name << "AgentAspect" << id; aspect->SetName(name.str()); scene->AddChildReference(aspect); mAgentMap[id] = aspect; // mark the scene as modified scene->SetModified(true); return aspect->Init(mCreateEffector); } bool GameControlServer::AgentDisappear(int id) { TAgentMap::iterator iter = mAgentMap.find(id); if (iter == mAgentMap.end()) { GetLog()->Error() << "ERROR: (GameControlServer) AgentDisappear called for " << "unknown agent id " << id << "\n"; return false; } // remove the AgentAspect from the Scene and our map. The // AgentAspect does all the necessary cleanup shared_ptr<Scene> scene = GetActiveScene(); if (scene.get() != 0) { (*iter).second->UnlinkChildren(); (*iter).second->Unlink(); // mark the scene as modified scene->SetModified(true); } else { GetLog()->Error() << "ERROR: (GameControlServer) failed to remove AgentAspect " << "for agent id " << id << "\n"; } mAgentMap.erase(id); GetLog()->Debug() << "(GameControlServer) An agent disconnected (id: " << id << ")\n"; return true; } float GameControlServer::GetSenseInterval(int /*id*/) { // the real thing should query the AgentAspect corresponding to // the agent. return 0.2; } float GameControlServer::GetSenseLatency(int /*id*/) { // the real thing should query the AgentAspect corresponding to // the agent return 0.1; } float GameControlServer::GetActionLatency(int /*id*/) { // the real thing should query the AgentAspect corresponding to // the agent. return 0.1; } shared_ptr<ActionObject::TList> GameControlServer::Parse(int id, const string& str) const { TAgentMap::const_iterator iter = mAgentMap.find(id); if (iter == mAgentMap.end()) { GetLog()->Error() << "ERROR: (GameControlServer::Parse) Parse " << "called with unknown agent id " << id << "\n"; return shared_ptr<ActionObject::TList>(); } if (mParser.get() == 0) { GetLog()->Error() << "ERROR: (GameControlServer::Parse) No parser registered.\n"; return shared_ptr<ActionObject::TList>(); } // use the parser to create a PredicateList shared_ptr<PredicateList> predicates(mParser->Parse(str)); // construct an ActionList using the registered effectors shared_ptr<ActionObject::TList> actionList(new ActionObject::TList()); // update the map of effectors below the agentaspect shared_ptr<AgentAspect> aspect = (*iter).second; aspect->UpdateEffectorMap(); for ( PredicateList::TList::const_iterator iter = predicates->begin(); iter != predicates->end(); ++iter ) { const Predicate& predicate = (*iter); shared_ptr<Effector> effector = aspect->GetEffector(predicate.name); if (effector.get() == 0) { GetLog()->Warning() << "(GameControlServer::Parse) No effector" << " registered for predicate " << predicate.name << "\n"; continue; } shared_ptr<ActionObject> action(effector->GetActionObject(predicate)); if (action.get() == 0) { continue; } actionList->push_back(action); } return actionList; } shared_ptr<AgentAspect> GameControlServer::GetAgentAspect(int id) { TAgentMap::iterator iter = mAgentMap.find(id); if (iter == mAgentMap.end()) { return shared_ptr<AgentAspect>(); } return (*iter).second; } void GameControlServer::Update(float deltaTime) { // build list of ControlAspects, NOT searching recursively TLeafList control; ListChildrenSupportingClass<ControlAspect>(control,false); // update all ControlAspects found for ( TLeafList::iterator iter = control.begin(); iter != control.end(); ++iter ) { shared_ptr<ControlAspect> aspect = shared_static_cast<ControlAspect>(*iter); aspect->Update(deltaTime); } } void GameControlServer::Quit() { mExit = true; } bool GameControlServer::IsFinished() const { return mExit; } --- NEW FILE: gamecontrolserver.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: gamecontrolserver.h,v 1.1 2005/12/05 21:16:49 rollmark 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 OXYGEN_GAMECONTROLSERVER_H #define OXYGEN_GAMECONTROLSERVER_H #include "actionobject.h" #include "baseparser.h" #include <zeitgeist/class.h> #include <zeitgeist/node.h> namespace oxygen { class AgentAspect; class Effector; class Scene; class GameControlServer : public zeitgeist::Node { public: GameControlServer(); ~GameControlServer(); /** creates a parser instance and registers it to the GameControlServer \param parserName the name of the parser */ bool InitParser(const std::string& parserName); /** sets effectorName as the effector class that is initially created with every new AgentAspect instance. \param effectorName the init effector of the agent */ void InitEffector(const std::string& effectorName); /** creates an instance of aspectName and registers it as a ControlAspect to the GameControlServer. \param aspectName */ bool InitControlAspect(const std::string& aspectName); /** returns the parser currently registered to the GameControlServer */ boost::shared_ptr<BaseParser> GetParser(); /** parses a command string using the registerd parser and uses the registered effectors of the agent identified by param \id to construct an ActionObject. This method must be const as it is called from SpadesServer::ParseAct, which is const to prevent it from modifying the world model. */ boost::shared_ptr<ActionObject::TList> Parse (int id, const std::string& str) const; /** notifies the GameControlServer that an agent has connected to the simulation. \param id should be a unique identifier for the new agent. It is not assigned by the GameControlServer. */ bool AgentConnect(int id); /** notifies the GameControlServer that an agent has disappeared from the simulation. \param id was the unique identifier of the disappeared agent. The GameControlServer uses the AgentAspect corresponding to id to clean up after the agent left. */ bool AgentDisappear(int id); /** returns the time between to queries of the agents perceptors in seconds */ float GetSenseInterval(int id); /** GetSenseLatency returns latency it takes to query the sensors of an agent in seconds */ float GetSenseLatency(int id); /** GetActionLatency returns the latency it takes to realize an agent action in seconds */ float GetActionLatency(int id); /** returns the AgentAspect for the given \param id */ boost::shared_ptr<AgentAspect> GetAgentAspect(int id); /** This method is used to notify the GameControlServer that the game has advanced deltaTime seconds. The GameControlServer will in turn update all registered GameControlAspects below it. */ void Update(float deltaTime); /** This method is used to indicate that the simulation is finished. The GameControlServer knows that the simulatio is finished if one of the GameControlAspects below indicate that the simulation is finished. The finished state is set during GameControlServer::Update(). \return true if the simulation should be stopped. */ bool IsFinished() const; /** This method is used to set the simulation into the 'finished' state. */ void Quit(); protected: /** helper method that queries the SceneServer for the currently active Scene */ boost::shared_ptr<Scene> GetActiveScene(); protected: typedef std::map<int, boost::shared_ptr<AgentAspect> > TAgentMap; /** a map from agent IDs to agent instances */ TAgentMap mAgentMap; /** the parser instance used */ boost::shared_ptr<BaseParser> mParser; /** the name of the initial effector class of each new agent has to construct all remaining parts */ std::string mCreateEffector; /** flag if the simulation is over */ bool mExit; }; DECLARE_CLASS(GameControlServer); } // namespace oxygen #endif // OXYGEN_GAMECONTROLSERVER_H --- NEW FILE: actionobject_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: actionobject_c.cpp,v 1.1 2005/12/05 21:16:49 rollmark 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 "actionobject.h" using namespace oxygen; void CLASS(ActionObject)::DefineClass() { DEFINE_BASECLASS(zeitgeist/Leaf); } |