00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "nel/ai/logic/goal_path.h"
00025 #include "nel/ai/agent/object_type.h"
00026 #include "nel/ai/agent/msg_goal.h"
00027
00028
00029 namespace NLAILOGIC
00030 {
00031 CGoalPath::CGoalPath(NLAIAGENT::IAgentManager *manager) : CActorScript( manager, true )
00032 {
00033 }
00034
00035 CGoalPath::CGoalPath(NLAIAGENT::IAgentManager *manager,
00036 NLAIAGENT::IBasicAgent *father,
00037 std::list<NLAIAGENT::IObjectIA *> &components,
00038 NLAISCRIPT::CAgentClass *actor_class )
00039 : CActorScript(manager, father, components, actor_class )
00040 {
00041 }
00042
00043
00044 void CGoalPath::addGoal( CGoal *goal, bool action, int on_success, int on_failure )
00045 {
00046 _Goals.push_back( goal );
00047 _JmpNext.push_back( action );
00048 _OnSucces.push_back( on_success );
00049 _OnFailure.push_back( on_failure );
00050 }
00051
00052 void CGoalPath::success()
00053 {
00054 if ( _CurrentState < ( int(_Goals.size()) - 1 ) )
00055 {
00056 _CurrentState++;
00057 NLAIAGENT::IMessageBase *msg = new NLAIAGENT::CGoalMsg((NLAIAGENT::IBasicAgent *)NULL);
00058 msg->setPerformatif(NLAIAGENT::IMessageBase::PAchieve);
00059 CGoal *g = _Goals[ _CurrentState ];
00060 g->setSender( this );
00061 msg->set(0, g );
00062 msg->setSender( this );
00063 msg->setReceiver( _Father);
00064 _Father->sendMessage(msg);
00065 }
00066 else
00067 {
00068
00069 CActorScript::success();
00070 }
00071 }
00072
00073 void CGoalPath::failure()
00074 {
00075 if ( _CurrentState < ( int(_Goals.size()) - 1 ) )
00076 {
00077
00078 NLAIAGENT::IMessageBase *msg = new NLAIAGENT::CGoalMsg((NLAIAGENT::IBasicAgent *)NULL);
00079 msg->setPerformatif(NLAIAGENT::IMessageBase::PAchieve);
00080 CGoal *g = _Goals[ _CurrentState ];
00081 g->setSender( this );
00082 msg->set(0, g );
00083 msg->setSender( this );
00084 msg->setReceiver( _Father);
00085 _Father->sendMessage(msg);
00086
00087 }
00088 else
00089 {
00090
00091 CActorScript::failure();
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102 NLAIAGENT::tQueue CGoalPath::getPrivateMember(const NLAIAGENT::IVarName *className,const NLAIAGENT::IVarName *funcName,const NLAIAGENT::IObjectIA ¶ms) const
00103 {
00104
00105 #ifdef NL_DEBUG
00106 std::string nameP;
00107 std::string nameM;
00108 funcName->getDebugString(nameM);
00109 params.getDebugString(nameP);
00110 const char *dbg_class_name = (const char *) getType();
00111 #endif
00112 static NLAIAGENT::CStringVarName constructor_name("Constructor");
00113 static NLAIAGENT::CStringVarName add_goal_name("AddGoal");
00114
00115 NLAIAGENT::tQueue r;
00116 if(className == NULL)
00117 {
00118 if( (*funcName) == constructor_name )
00119 {
00120 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CGoal::IdGoal ) );
00121 r.push( NLAIAGENT::CIdMethod( 0 + CActorScript::getMethodIndexSize(), 0.0, NULL, c) );
00122 }
00123
00124 if( (*funcName) == add_goal_name )
00125 {
00126 NLAIAGENT::CObjectType *c = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( CGoal::IdGoal ) );
00127 r.push( NLAIAGENT::CIdMethod( 1 + CActorScript::getMethodIndexSize(), 0.0, NULL, c) );
00128 }
00129 }
00130
00131 if ( r.empty() )
00132 return CActorScript::getPrivateMember(className, funcName, params);
00133 else
00134 return r;
00135 }
00136
00137
00138 NLAIAGENT::IObjectIA::CProcessResult CGoalPath::runMethodBase(int heritance, int index, NLAIAGENT::IObjectIA *)
00139 {
00140 return IObjectIA::CProcessResult();
00141 }
00142
00143 NLAIAGENT::IObjectIA::CProcessResult CGoalPath::runMethodBase(int index, NLAIAGENT::IObjectIA *p)
00144 {
00145 NLAIAGENT::IBaseGroupType *param = (NLAIAGENT::IBaseGroupType *)p;
00146
00147 switch(index - CActorScript::getMethodIndexSize())
00148 {
00149 case 0:
00150 {
00151 #ifdef NL_DEBUG
00152
00153 #endif
00154 _Father = (NLAIAGENT::CProxyAgentMail *) param->getFront();
00155 param->popFront();
00156
00157 while ( param->size() )
00158 {
00159 _Goals.push_back( (NLAILOGIC::CGoal *) param->getFront()->clone() );
00160 param->popFront();
00161 }
00162 _CurrentState = 0;
00163 return IObjectIA::CProcessResult();
00164 }
00165 break;
00166
00167 case 1:
00168 {
00169
00170 while ( param->size() )
00171 {
00172 _Goals.push_back( (NLAILOGIC::CGoal *) param->getFront()->clone() );
00173 param->popFront();
00174 }
00175 return IObjectIA::CProcessResult();
00176 }
00177 break;
00178
00179 }
00180
00181 return CActorScript::runMethodBase( index, p );
00182 }
00183
00184 sint32 CGoalPath::getMethodIndexSize() const
00185 {
00186 return CActorScript::getMethodIndexSize() + 2;
00187 }
00188
00189 const NLAIC::IBasicType *CGoalPath::clone() const
00190 {
00191 NLAIC::IBasicInterface *m = new CGoalPath( *this );
00192 return m;
00193 }
00194
00195 const NLAIC::IBasicType *CGoalPath::newInstance() const
00196 {
00197 return clone();
00198 }
00199
00200 const NLAIC::CIdentType &CGoalPath::getType() const
00201 {
00202 return IdGoalPath;
00203 }
00204
00205 int CGoalPath::getBaseMethodCount() const
00206 {
00207 return CActorScript::getBaseMethodCount() + 2;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 void CGoalPath::onActivate()
00238 {
00239 _CurrentState = 0;
00240 if ( _Father != NULL )
00241 {
00242 NLAIAGENT::IMessageBase *msg = new NLAIAGENT::CGoalMsg((NLAIAGENT::IBasicAgent *)NULL);
00243 msg->setPerformatif(NLAIAGENT::IMessageBase::PAchieve);
00244 CGoal *g = _Goals[ _CurrentState ];
00245 g->setSender( this );
00246 msg->set(0, g );
00247 msg->setSender( this );
00248 msg->setReceiver( _Father);
00249 _Father->sendMessage(msg);
00250 }
00251 }
00252
00253 void CGoalPath::onUnActivate()
00254 {
00255 _CurrentState = 0;
00256 if ( _Father != NULL )
00257 {
00258 NLAIAGENT::IMessageBase *msg = new NLAIAGENT::CCancelGoalMsg((NLAIAGENT::IBasicAgent *)NULL);
00259 msg->setPerformatif(NLAIAGENT::IMessageBase::PAchieve);
00260 msg->set(0, _Goals[ _CurrentState ] );
00261 msg->setSender( this );
00262 msg->setReceiver( _Father);
00263 _Father->sendMessage(msg);
00264 }
00265 }
00266 }