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 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; version 2 of the License. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 #include "sayaction.h" 00022 #include "sayeffector.h" 00023 #include <zeitgeist/logserver/logserver.h> 00024 #include <oxygen/agentaspect/agentaspect.h> 00025 #include <soccer/agentstate/agentstate.h> 00026 #include <soccer/soccerbase/soccerbase.h> 00027 #include <soccer/soccerruleaspect/soccerruleaspect.h> 00028 00029 using namespace boost; 00030 using namespace oxygen; 00031 using namespace salt; 00032 using namespace std; 00033 00034 SayEffector::SayEffector() 00035 : oxygen::Effector() 00036 { 00037 } 00038 00039 SayEffector::~SayEffector() 00040 { 00041 } 00042 00043 bool 00044 SayEffector::Realize(shared_ptr<ActionObject> action) 00045 { 00046 if (mAgent.get() == 0) 00047 { 00048 GetLog()->Error() 00049 << "ERROR: (SayEffector) parent node is not derived from " 00050 << "BaseNode\n"; 00051 return false; 00052 } 00053 00054 if (mAgentState.get() == 0) 00055 { 00056 return false; 00057 } 00058 00059 shared_ptr<SayAction> sayAction = 00060 shared_dynamic_cast<SayAction>(action); 00061 00062 if (sayAction.get() == 0) 00063 { 00064 GetLog()->Error() 00065 << "ERROR: (SayEffector) cannot realize an unknown ActionObject\n"; 00066 return false; 00067 } 00068 00069 sayAction->GetMessage(mMessage); 00070 ifText=true; 00071 00072 mSoccerRule->Broadcast(mMessage, mAgent->GetWorldTransform().Pos(), 00073 mAgentState->GetUniformNumber(), mAgentState->GetTeamIndex()); 00074 00075 return true; 00076 } 00077 00078 string 00079 SayEffector::GetText() 00080 { 00081 ifText=false; 00082 return mMessage; 00083 } 00084 00085 bool 00086 SayEffector::IfText()const 00087 { 00088 return ifText; 00089 } 00090 00091 shared_ptr<ActionObject> 00092 SayEffector::GetActionObject(const Predicate& predicate) 00093 { 00094 if (predicate.name != GetPredicate()) 00095 { 00096 GetLog()->Error() << "ERROR: (SayEffector) invalid predicate" 00097 << predicate.name << "\n"; 00098 00099 // some error happened 00100 return shared_ptr<ActionObject>(); 00101 } 00102 00103 Predicate::Iterator iter = predicate.begin(); 00104 00105 std::string message; 00106 if (! predicate.AdvanceValue(iter, message)) 00107 { 00108 GetLog()->Error() 00109 << "ERROR: (SayEffector) said message expected\n"; 00110 00111 // some error happened 00112 return shared_ptr<ActionObject>(); 00113 } 00114 00115 // construct the SayAction object 00116 return shared_ptr<SayAction>(new SayAction(GetPredicate(), message)); 00117 00118 } 00119 00120 void 00121 SayEffector::OnLink() 00122 { 00123 SoccerBase::GetAgentState(*this,mAgentState); 00124 SoccerBase::GetSoccerRuleAspect(*this,mSoccerRule); 00125 00126 mAgent = shared_dynamic_cast<AgentAspect>(make_shared(GetParent())); 00127 00128 if (mAgent.get() == 0) 00129 { 00130 GetLog()->Error() 00131 << "ERROR: (SayEffector) parent node is not derived from " 00132 << "AgentAspect\n"; 00133 return; 00134 } 00135 } 00136 00137 void 00138 SayEffector::OnUnlink() 00139 { 00140 mAgent.reset(); 00141 mAgentState.reset(); 00142 mSoccerRule.reset(); 00143 }