Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

gamecontrolserver.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2004 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: gamecontrolserver.cpp,v 1.18 2004/06/11 07:57:47 fruit Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include <sstream>
00023 #include "gamecontrolserver.h"
00024 #include "baseparser.h"
00025 #include <oxygen/agentaspect/agentaspect.h>
00026 #include <oxygen/sceneserver/sceneserver.h>
00027 #include <oxygen/sceneserver/scene.h>
00028 #include <oxygen/controlaspect/controlaspect.h>
00029 #include <zeitgeist/logserver/logserver.h>
00030 #include <zeitgeist/scriptserver/scriptserver.h>
00031 #include <zeitgeist/corecontext.h>
00032 
00033 using namespace oxygen;
00034 using namespace zeitgeist;
00035 using namespace boost;
00036 using namespace std;
00037 
00038 GameControlServer::GameControlServer() : zeitgeist::Node()
00039 {
00040     mExit = false;
00041 }
00042 
00043 GameControlServer::~GameControlServer()
00044 {
00045 }
00046 
00047 bool
00048 GameControlServer::InitParser(const std::string& parserName)
00049 {
00050     mParser = shared_dynamic_cast<BaseParser>(GetCore()->New(parserName));
00051 
00052     if (mParser.get() == 0)
00053     {
00054         GetLog()->Error() << "ERROR: (GameControlServer::InitParser) Unable to create "
00055                           << parserName << "\n";
00056         return false;
00057     }
00058 
00059     return true;
00060 }
00061 
00062 void
00063 GameControlServer::InitEffector(const std::string& effectorName)
00064 {
00065    mCreateEffector = effectorName;
00066 }
00067 
00068 bool
00069 GameControlServer::InitControlAspect(const string& aspectName)
00070 {
00071     shared_ptr<ControlAspect> aspect
00072         = shared_dynamic_cast<ControlAspect>(GetCore()->New(aspectName));
00073 
00074     if (aspect.get() == 0)
00075     {
00076         GetLog()->Error() << "ERROR: (GameControlServer::InitControlAspect) "
00077                           << "Unable to create " << aspectName << "\n";
00078         return false;
00079     }
00080 
00081     aspect->SetName(aspectName);
00082     AddChildReference(aspect);
00083 
00084     return true;
00085 }
00086 
00087 shared_ptr<BaseParser>
00088 GameControlServer::GetParser()
00089 {
00090     return mParser;
00091 }
00092 
00093 shared_ptr<Scene>
00094 GameControlServer::GetActiveScene()
00095 {
00096     shared_ptr<SceneServer> sceneServer =
00097         shared_dynamic_cast<SceneServer>(GetCore()->Get("/sys/server/scene"));
00098 
00099     if (sceneServer.get() == 0)
00100     {
00101         GetLog()->Error()
00102             << "ERROR: (GameControlServer) SceneServer not found.\n";
00103         return shared_ptr<Scene>();
00104     }
00105 
00106     shared_ptr<Scene> scene = sceneServer->GetActiveScene();
00107 
00108     if (scene.get() == 0)
00109     {
00110         GetLog()->Error()
00111             << "ERROR: (GameControlServer) SceneServer "
00112             << "reports no active scene\n";
00113     }
00114 
00115     return scene;
00116 }
00117 
00118 bool
00119 GameControlServer::AgentConnect(int id)
00120 {
00121     // for map::insert(Elem), the test here is not required. map::insert does not
00122     // overwrite existing elements. The test is required to report if the agent
00123     // was already connected or not.
00124     if (mAgentMap.find(id) != mAgentMap.end())
00125     {
00126         return false;
00127     }
00128 
00129     GetLog()->Normal()
00130         << "(GameControlServer) a new agent connected (id: " << id << ")\n";
00131 
00132     shared_ptr<Scene> scene = GetActiveScene();
00133     if (scene.get() == 0)
00134     {
00135         GetLog()->Error()
00136             << "(GameControlServer) ERROR: Got no active scene from the "
00137             << "SceneServer to create the AgentAspect in.\n";
00138         return false;
00139     }
00140 
00141     // create a new AgentAspect for the ID in the scene and add it to
00142     // our map of AgentAspects
00143     shared_ptr<AgentAspect> aspect = shared_dynamic_cast<AgentAspect>
00144         (GetCore()->New("oxygen/AgentAspect"));
00145 
00146     if (aspect.get() == 0)
00147     {
00148         GetLog()->Error()
00149             << "ERROR: (GameControlServer) cannot create new AgentAspect\n";
00150         return false;
00151     }
00152 
00153     stringstream name;
00154     name << "AgentAspect" << id;
00155     aspect->SetName(name.str());
00156 
00157     scene->AddChildReference(aspect);
00158     mAgentMap[id] = aspect;
00159 
00160     // mark the scene as modified
00161     scene->SetModified(true);
00162 
00163     return aspect->Init(mCreateEffector);
00164 }
00165 
00166 bool GameControlServer::AgentDisappear(int id)
00167 {
00168     TAgentMap::iterator iter = mAgentMap.find(id);
00169 
00170     if (iter == mAgentMap.end())
00171     {
00172         GetLog()->Error()
00173             << "ERROR: (GameControlServer) AgentDisappear called for "
00174             << "unknown agent id " << id << "\n";
00175         return false;
00176     }
00177 
00178     // remove the AgentAspect from the Scene and our map. The
00179     // AgentAspect does all the necessary cleanup
00180     shared_ptr<Scene> scene = GetActiveScene();
00181     if (scene.get() != 0)
00182     {
00183         (*iter).second->UnlinkChildren();
00184         (*iter).second->Unlink();
00185 
00186         // mark the scene as modified
00187         scene->SetModified(true);
00188     } else
00189         {
00190             GetLog()->Error()
00191                 << "ERROR: (GameControlServer) failed to remove AgentAspect "
00192                 << "for agent id " << id << "\n";
00193         }
00194 
00195     mAgentMap.erase(id);
00196 
00197     GetLog()->Debug() << "(GameControlServer) An agent disconnected (id: "
00198                       << id << ")\n";
00199 
00200     return true;
00201 }
00202 
00203 float
00204 GameControlServer::GetSenseInterval(int /*id*/)
00205 {
00206     // the real thing should query the AgentAspect corresponding to
00207     // the agent.
00208     return 0.2;
00209 }
00210 
00211 float
00212 GameControlServer::GetSenseLatency(int /*id*/)
00213 {
00214     // the real thing should query the AgentAspect corresponding to
00215     // the agent
00216     return 0.1;
00217 }
00218 
00219 float
00220 GameControlServer::GetActionLatency(int /*id*/)
00221 {
00222     // the real thing should query the AgentAspect corresponding to
00223     // the agent.
00224     return 0.1;
00225 }
00226 
00227 shared_ptr<ActionObject::TList>
00228 GameControlServer::Parse(int id, const string& str) const
00229 {
00230     TAgentMap::const_iterator iter = mAgentMap.find(id);
00231 
00232     if (iter == mAgentMap.end())
00233     {
00234         GetLog()->Error()
00235             << "ERROR: (GameControlServer::Parse) Parse "
00236             << "called with unknown agent id "
00237             << id << "\n";
00238         return shared_ptr<ActionObject::TList>();
00239     }
00240 
00241     if (mParser.get() == 0)
00242     {
00243         GetLog()->Error()
00244             << "ERROR: (GameControlServer::Parse) No parser registered.\n";
00245         return shared_ptr<ActionObject::TList>();
00246     }
00247 
00248     // use the parser to create a PredicateList
00249     shared_ptr<PredicateList> predicates(mParser->Parse(str));
00250 
00251     // construct an ActionList using the registered effectors
00252     shared_ptr<ActionObject::TList> actionList(new ActionObject::TList());
00253 
00254     // update the map of effectors below the agentaspect
00255     shared_ptr<AgentAspect> aspect = (*iter).second;
00256     aspect->UpdateEffectorMap();
00257 
00258     for
00259         (
00260             PredicateList::TList::const_iterator iter = predicates->begin();
00261             iter != predicates->end();
00262             ++iter
00263             )
00264     {
00265         const Predicate& predicate = (*iter);
00266 
00267         shared_ptr<Effector> effector = aspect->GetEffector(predicate.name);
00268         if (effector.get() == 0)
00269         {
00270             GetLog()->Warning()
00271                 << "(GameControlServer::Parse) No effector"
00272                 << " registered for predicate "
00273                 << predicate.name << "\n";
00274             continue;
00275         }
00276 
00277         shared_ptr<ActionObject> action(effector->GetActionObject(predicate));
00278 
00279         if (action.get() == 0)
00280         {
00281             continue;
00282         }
00283 
00284         actionList->push_back(action);
00285     }
00286 
00287     return actionList;
00288 }
00289 
00290 shared_ptr<AgentAspect>
00291 GameControlServer::GetAgentAspect(int id)
00292 {
00293     TAgentMap::iterator iter = mAgentMap.find(id);
00294     if (iter == mAgentMap.end())
00295     {
00296         return shared_ptr<AgentAspect>();
00297     }
00298 
00299     return (*iter).second;
00300 }
00301 
00302 void
00303 GameControlServer::Update(float deltaTime)
00304 {
00305     // build list of ControlAspects, NOT searching recursively
00306     TLeafList control;
00307     ListChildrenSupportingClass<ControlAspect>(control,false);
00308 
00309     // update all ControlAspects found
00310     for (
00311         TLeafList::iterator iter = control.begin();
00312         iter != control.end();
00313         ++iter
00314         )
00315     {
00316         shared_ptr<ControlAspect> aspect =
00317             shared_static_cast<ControlAspect>(*iter);
00318 
00319         aspect->Update(deltaTime);
00320     }
00321 }
00322 
00323 void
00324 GameControlServer::Quit()
00325 {
00326     mExit = true;
00327 }
00328 
00329 bool
00330 GameControlServer::IsFinished() const
00331 {
00332     return mExit;
00333 }

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4