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

soccerbase.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) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: soccerbase.cpp,v 1.12 2005/12/13 20:55:09 rollmark 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 "soccerbase.h"
00023 #include <oxygen/physicsserver/body.h>
00024 #include <oxygen/physicsserver/spherecollider.h>
00025 #include <oxygen/agentaspect/perceptor.h>
00026 #include <oxygen/sceneserver/sceneserver.h>
00027 #include <oxygen/sceneserver/scene.h>
00028 #include <oxygen/sceneserver/transform.h>
00029 #include <oxygen/controlaspect/controlaspect.h>
00030 #include <soccer/gamestateaspect/gamestateaspect.h>
00031 #include <soccer/soccerruleaspect/soccerruleaspect.h>
00032 #include <soccer/agentstate/agentstate.h>
00033 #include <soccer/ball/ball.h>
00034 
00035 using namespace boost;
00036 using namespace zeitgeist;
00037 using namespace oxygen;
00038 using namespace std;
00039 
00040 bool
00041 SoccerBase::GetSceneServer(const Leaf& base,
00042                            shared_ptr<SceneServer>& scene_server)
00043 {
00044     scene_server = shared_static_cast<SceneServer>
00045         (base.GetCore()->Get("/sys/server/scene"));
00046 
00047     if (scene_server.get() == 0)
00048     {
00049         base.GetLog()->Error()
00050             << "Error: (SoccerBase: " << base.GetName()
00051             << ") scene server not found.\n";
00052         return false;
00053     }
00054 
00055     return true;
00056 }
00057 
00058 bool
00059 SoccerBase::GetTransformParent(const Leaf& base,
00060                                shared_ptr<Transform>& transform_parent)
00061 {
00062     transform_parent = shared_dynamic_cast<Transform>
00063         (make_shared(base.GetParentSupportingClass("Transform")));
00064 
00065     if (transform_parent.get() == 0)
00066     {
00067         base.GetLog()->Error()
00068             << "Error: (SoccerBase: " << base.GetName()
00069             << ") parent node is not derived from TransformNode\n";
00070         return false;
00071     }
00072     return true;
00073 }
00074 
00075 bool
00076 SoccerBase::GetAgentState(const shared_ptr<Transform> transform,
00077                           shared_ptr<AgentState>& agent_state)
00078 {
00079     agent_state =
00080         shared_dynamic_cast<AgentState>(transform->GetChild("AgentState"));
00081 
00082     if (agent_state.get() == 0)
00083     {
00084         return false;
00085     }
00086 
00087     return true;
00088 }
00089 
00090 bool
00091 SoccerBase::GetAgentBody(const shared_ptr<Transform> transform,
00092                           shared_ptr<Body>& agent_body)
00093 
00094 {
00095     agent_body = shared_dynamic_cast<Body>
00096         (transform->GetChildSupportingClass("Body", true));
00097 
00098     if (agent_body.get() == 0)
00099     {
00100         transform->GetLog()->Error()
00101             << "(SoccerBase) ERROR: " << transform->GetName()
00102             << ") node has no Body child\n";
00103         return false;
00104     }
00105 
00106     return true;
00107 }
00108 
00109 bool
00110 SoccerBase::GetAgentBody(const Leaf& base, TTeamIndex idx,
00111                          int unum, shared_ptr<Body>& agent_body)
00112 {
00113     shared_ptr<AgentState> agentState;
00114     shared_ptr<Transform>  parent;
00115 
00116     // get matching AgentState
00117     if (!GetAgentState(base, idx, unum, agentState))
00118         return false;
00119 
00120     // get AgentAspect
00121     if (!GetTransformParent(*agentState, parent))
00122         return false;
00123 
00124     // call GetAgentBody with matching AgentAspect
00125     return GetAgentBody(parent, agent_body);
00126 }
00127 
00128 bool
00129 SoccerBase::GetAgentState(const Leaf& base,
00130                           shared_ptr<AgentState>& agent_state)
00131 {
00132     shared_ptr<Transform> parent;
00133     if (! GetTransformParent(base,parent))
00134     {
00135         return false;
00136     }
00137 
00138     return GetAgentState(parent,agent_state);
00139 }
00140 
00141 bool
00142 SoccerBase::GetAgentState(const Leaf& base, TTeamIndex idx,
00143                           int unum, shared_ptr<AgentState>& agentState)
00144 {
00145     // get the active scene
00146     shared_ptr<Scene> activeScene;
00147 
00148     if (GetActiveScene(base, activeScene))
00149     {
00150         Leaf::TLeafList leafList;
00151 
00152         // get a list of all the agent aspects
00153         activeScene->GetChildrenOfClass("AgentAspect", leafList);
00154 
00155         if (leafList.size() == 0)
00156         {
00157             base.GetLog()->Error()
00158                 << "ERROR: (SoccerBase) active scene doesn't have "
00159                 << "children of type AgentAspect\n";
00160 
00161             return false;
00162         }
00163 
00164         Leaf::TLeafList::iterator iter = leafList.begin();
00165 
00166         // search through the list to find an agent state
00167         // with matching team index and unum
00168         for (
00169               iter;
00170               iter != leafList.end();
00171               ++iter
00172              )
00173         {
00174             shared_ptr<Transform> agentAspect =
00175              shared_dynamic_cast<Transform>(*iter);
00176 
00177             if (GetAgentState(agentAspect, agentState) &&
00178                 (agentState->GetTeamIndex() == idx) &&
00179                 (agentState->GetUniformNumber() == unum))
00180             {
00181                 return true;
00182             }
00183         }
00184     }
00185     return false;
00186 }
00187 
00188 bool
00189 SoccerBase::GetAgentStates(const zeitgeist::Leaf& base,
00190                            TAgentStateList& agentStates,
00191                            TTeamIndex idx)
00192 {
00193     // get the active scene
00194     shared_ptr<Scene> activeScene;
00195 
00196     if (GetActiveScene(base, activeScene))
00197     {
00198         Leaf::TLeafList leafList;
00199 
00200         // get a list of all the agent aspects
00201         activeScene->GetChildrenOfClass("AgentAspect", leafList);
00202 
00203         if (leafList.size() == 0)
00204         {
00205             base.GetLog()->Error()
00206                 << "ERROR: (SoccerBase) active scene doesn't have "
00207                 << "children of type AgentAspect\n";
00208 
00209             return false;
00210         }
00211 
00212         shared_ptr<AgentState> agentState;
00213         Leaf::TLeafList::iterator iter = leafList.begin();
00214         // search through the list to find an agent state
00215         // with matching team index
00216         for (iter;
00217              iter != leafList.end();
00218              ++iter
00219             )
00220         {
00221             shared_ptr<Transform> agentAspect =
00222                 shared_dynamic_cast<Transform>(*iter);
00223 
00224             if (agentAspect.get() == 0) continue;
00225 
00226             if (GetAgentState(agentAspect, agentState) &&
00227                 ((agentState->GetTeamIndex() == idx) ||
00228                  (idx == TI_NONE)))
00229             {
00230                 agentStates.push_back(agentState);
00231             }
00232         }
00233         return true;
00234     }
00235     return false;
00236 }
00237 
00238 bool
00239 SoccerBase::GetGameState(const Leaf& base,
00240                          shared_ptr<GameStateAspect>& game_state)
00241 {
00242     game_state = shared_dynamic_cast<GameStateAspect>
00243         (base.GetCore()->Get("/sys/server/gamecontrol/GameStateAspect"));
00244 
00245     if (game_state.get() == 0)
00246     {
00247         base.GetLog()->Error()
00248             << "Error: (SoccerBase: " << base.GetName()
00249             << ") found no GameStateAspect\n";
00250         return false;
00251     }
00252 
00253     return true;
00254 }
00255 
00256 bool
00257 SoccerBase::GetSoccerRuleAspect(const Leaf& base,
00258                                 shared_ptr<SoccerRuleAspect>& soccer_rule_aspect)
00259 {
00260     soccer_rule_aspect = shared_dynamic_cast<SoccerRuleAspect>
00261         (base.GetCore()->Get("/sys/server/gamecontrol/SoccerRuleAspect"));
00262 
00263     if (soccer_rule_aspect.get() == 0)
00264     {
00265         base.GetLog()->Error()
00266             << "Error: (SoccerBase: " << base.GetName()
00267             << ") found no SoccerRuleAspect\n";
00268         return false;
00269     }
00270 
00271     return true;
00272 }
00273 
00274 bool
00275 SoccerBase::GetActiveScene(const Leaf& base,
00276                            shared_ptr<Scene>& active_scene)
00277 {
00278     shared_ptr<SceneServer> sceneServer;
00279     if (! GetSceneServer(base,sceneServer))
00280         {
00281             return false;
00282         }
00283 
00284     active_scene = sceneServer->GetActiveScene();
00285     if (active_scene.get() == 0)
00286     {
00287         base.GetLog()->Error()
00288             << "ERROR: (SoccerBase: " << base.GetName()
00289             << ") SceneServer reports no active scene\n";
00290         return false;
00291     }
00292     return true;
00293 }
00294 
00295 bool
00296 SoccerBase::GetBody(const Leaf& base, shared_ptr<Body>& body)
00297 {
00298     shared_ptr<Transform> parent;
00299     if (! GetTransformParent(base,parent))
00300     {
00301         return false;
00302     }
00303 
00304   body = shared_dynamic_cast<Body>(parent->GetChildOfClass("Body"));
00305 
00306   if (body.get() == 0)
00307     {
00308       base.GetLog()->Error()
00309           << "ERROR: (SoccerBase: " << base.GetName()
00310           << ") parent node has no Body child.";
00311       return false;
00312     }
00313 
00314   return true;
00315 }
00316 
00317 bool
00318 SoccerBase::GetBall(const Leaf& base, shared_ptr<Ball>& ball)
00319 {
00320     shared_ptr<Scene> scene;
00321     if (! GetActiveScene(base,scene))
00322         {
00323             return false;
00324         }
00325 
00326     ball = shared_dynamic_cast<Ball>
00327         (base.GetCore()->Get(scene->GetFullPath() + "Ball"));
00328 
00329     if (ball.get() == 0)
00330         {
00331             base.GetLog()->Error()
00332                 << "ERROR: (SoccerBase: " << base.GetName()
00333                 << ") found no ball node\n";
00334             return false;
00335         }
00336 
00337     return true;
00338 }
00339 
00340 bool
00341 SoccerBase::GetBallBody(const Leaf& base, shared_ptr<Body>& body)
00342 {
00343     shared_ptr<Scene> scene;
00344     if (! GetActiveScene(base,scene))
00345         {
00346             return false;
00347         }
00348 
00349    body = shared_dynamic_cast<Body>
00350         (base.GetCore()->Get(scene->GetFullPath() + "Ball/physics"));
00351 
00352    if (body.get() == 0)
00353        {
00354             base.GetLog()->Error()
00355                 << "ERROR: (SoccerBase: " << base.GetName()
00356                 << ") found no ball body node\n";
00357             return false;
00358        }
00359 
00360    return true;
00361 }
00362 
00363 bool
00364 SoccerBase::GetBallCollider(const zeitgeist::Leaf& base,
00365                 boost::shared_ptr<oxygen::SphereCollider>& sphere)
00366 {
00367     shared_ptr<Scene> scene;
00368     if (! GetActiveScene(base,scene))
00369         {
00370             return false;
00371         }
00372 
00373    sphere = shared_dynamic_cast<SphereCollider>
00374         (base.GetCore()->Get(scene->GetFullPath() + "Ball/geometry"));
00375 
00376    if (sphere.get() == 0)
00377        {
00378             base.GetLog()->Error()
00379                 << "ERROR: (SoccerBase: " << base.GetName()
00380                 << ") Ball got no SphereCollider node\n";
00381             return false;
00382        }
00383 
00384    return true;
00385 }
00386 
00387 salt::Vector3f
00388 SoccerBase::FlipView(const salt::Vector3f& pos, TTeamIndex ti)
00389 {
00390     salt::Vector3f newPos;
00391     switch (ti)
00392     {
00393     case TI_RIGHT:
00394         newPos[0] = -pos[0];
00395         newPos[1] = -pos[1];
00396         newPos[2] = pos[2];
00397         break;
00398     case TI_NONE:
00399     case TI_LEFT:
00400         newPos = pos;
00401         break;
00402     }
00403     return newPos;
00404 }
00405 
00406 TTeamIndex
00407 SoccerBase::OpponentTeam(TTeamIndex ti)
00408 {
00409     switch (ti)
00410     {
00411     case TI_RIGHT:
00412         return TI_LEFT;
00413     case TI_LEFT:
00414         return TI_RIGHT;
00415     default:
00416         return TI_NONE;
00417     }
00418 }
00419 
00420 string
00421 SoccerBase::PlayMode2Str(const TPlayMode mode)
00422 {
00423     switch (mode)
00424     {
00425     case PM_BeforeKickOff:
00426         return STR_PM_BeforeKickOff;
00427 
00428     case PM_KickOff_Left:
00429         return STR_PM_KickOff_Left;
00430 
00431     case PM_KickOff_Right:
00432         return STR_PM_KickOff_Right;
00433 
00434     case PM_PlayOn:
00435         return STR_PM_PlayOn;
00436 
00437     case PM_KickIn_Left:
00438         return STR_PM_KickIn_Left;
00439 
00440     case PM_KickIn_Right:
00441         return STR_PM_KickIn_Right;
00442 
00443     case PM_CORNER_KICK_LEFT:
00444         return STR_PM_CORNER_KICK_LEFT;
00445 
00446     case PM_CORNER_KICK_RIGHT:
00447         return STR_PM_CORNER_KICK_RIGHT;
00448 
00449     case PM_GOAL_KICK_LEFT:
00450         return STR_PM_GOAL_KICK_LEFT;
00451 
00452     case PM_GOAL_KICK_RIGHT:
00453         return STR_PM_GOAL_KICK_RIGHT;
00454 
00455     case PM_OFFSIDE_LEFT:
00456         return STR_PM_OFFSIDE_LEFT;
00457 
00458     case PM_OFFSIDE_RIGHT:
00459         return STR_PM_OFFSIDE_RIGHT;
00460 
00461     case PM_GameOver:
00462         return STR_PM_GameOver;
00463 
00464     case PM_Goal_Left:
00465         return STR_PM_Goal_Left;
00466 
00467     case PM_Goal_Right:
00468         return STR_PM_Goal_Right;
00469 
00470     case PM_FREE_KICK_LEFT:
00471         return STR_PM_FREE_KICK_LEFT;
00472 
00473     case PM_FREE_KICK_RIGHT:
00474         return STR_PM_FREE_KICK_RIGHT;
00475 
00476     default:
00477         return STR_PM_Unknown;
00478     };
00479 }
00480 
00481 shared_ptr<ControlAspect>
00482 SoccerBase::GetControlAspect(const zeitgeist::Leaf& base,const string& name)
00483 {
00484   static const string gcsPath = "/sys/server/gamecontrol/";
00485 
00486   shared_ptr<ControlAspect> aspect = shared_dynamic_cast<ControlAspect>
00487     (base.GetCore()->Get(gcsPath + name));
00488 
00489   if (aspect.get() == 0)
00490     {
00491         base.GetLog()->Error()
00492             << "ERROR: (SoccerBase: " << base.GetName()
00493             << ") found no ControlAspect " << name << "\n";
00494     }
00495 
00496   return aspect;
00497 }
00498 
00499 

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