00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "beamaction.h"
00023 #include "beameffector.h"
00024 #include <soccer/soccerbase/soccerbase.h>
00025 #include <soccer/agentstate/agentstate.h>
00026 #include <cmath>
00027
00028 using namespace boost;
00029 using namespace oxygen;
00030 using namespace salt;
00031 using namespace std;
00032
00033 BeamEffector::BeamEffector() : oxygen::Effector()
00034 {
00035 }
00036
00037 BeamEffector::~BeamEffector()
00038 {
00039 }
00040
00041 #ifdef __APPLE_CC__
00042 bool
00043 isfinite( float f )
00044 {
00045 return f == f && f != f * 0.5f;
00046 }
00047 #endif
00048
00049 bool
00050 BeamEffector::Realize(boost::shared_ptr<ActionObject> action)
00051 {
00052 if (
00053 (mBody.get() == 0) ||
00054 (mGameState.get() == 0) ||
00055 (mAgentState.get() == 0)
00056 )
00057 {
00058 return false;
00059 }
00060
00061 shared_ptr<BeamAction> beamAction =
00062 shared_dynamic_cast<BeamAction>(action);
00063
00064 if (beamAction.get() == 0)
00065 {
00066 GetLog()->Error()
00067 << "ERROR: (BeamEffector) cannot realize an unknown ActionObject\n";
00068 return false;
00069 }
00070
00071
00072 if (mGameState->GetPlayMode() == PM_BeforeKickOff)
00073 {
00074 Vector3f pos = beamAction->GetPosition();
00075
00076
00077 if (
00078 (! isfinite(pos[0])) ||
00079 (! isfinite(pos[1])) ||
00080 (! isfinite(pos[2]))
00081 )
00082 {
00083 return false;
00084 }
00085
00086
00087 float minX = -mFieldLength/2 + mAgentRadius;
00088 pos[0] = std::max<float>(pos[0],minX);
00089 pos[0] = std::min<float>(pos[0],0.0f);
00090
00091 float minY = -mFieldWidth/2 + mAgentRadius;
00092 float maxY = mFieldWidth/2 - mAgentRadius;
00093 pos[1] = std::max<float>(minY,pos[1]);
00094 pos[1] = std::min<float>(maxY,pos[1]);
00095
00096 pos[2] = mAgentRadius;
00097
00098
00099
00100
00101 pos = SoccerBase::FlipView
00102 (
00103 pos,
00104 mAgentState->GetTeamIndex()
00105 );
00106
00107 mBody->SetPosition(pos);
00108 mBody->SetVelocity(Vector3f(0,0,0));
00109 mBody->SetAngularVelocity(Vector3f(0,0,0));
00110 }
00111
00112 return true;
00113 }
00114
00115 shared_ptr<ActionObject>
00116 BeamEffector::GetActionObject(const Predicate& predicate)
00117 {
00118 if (predicate.name != GetPredicate())
00119 {
00120 GetLog()->Error() << "ERROR: (BeamEffector) invalid predicate"
00121 << predicate.name << "\n";
00122 return shared_ptr<ActionObject>();
00123 }
00124
00125 Vector3f pos;
00126 if (! predicate.GetValue(predicate.begin(), pos))
00127 {
00128 GetLog()->Error()
00129 << "ERROR: (BeamEffector) Vector3f parameter expected\n";
00130 return shared_ptr<ActionObject>(new ActionObject(GetPredicate()));
00131 }
00132
00133 return shared_ptr<ActionObject>(new BeamAction(GetPredicate(),pos));
00134 }
00135
00136 void
00137 BeamEffector::OnLink()
00138 {
00139 SoccerBase::GetBody(*this,mBody);
00140 SoccerBase::GetGameState(*this, mGameState);
00141 SoccerBase::GetAgentState(*this,mAgentState);
00142
00143 mFieldWidth = 64.0;
00144 SoccerBase::GetSoccerVar(*this,"FieldWidth",mFieldWidth);
00145
00146 mFieldLength = 100.0;
00147 SoccerBase::GetSoccerVar(*this,"FieldLength",mFieldLength);
00148
00149 mAgentRadius = 0.22;
00150 SoccerBase::GetSoccerVar(*this,"AgentRadius",mAgentRadius);
00151 }
00152
00153 void
00154 BeamEffector::OnUnlink()
00155 {
00156 mBody.reset();
00157 mGameState.reset();
00158 mAgentState.reset();
00159 }
00160