# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

fsm_script.cpp

Go to the documentation of this file.
00001 #include "nel/ai/logic/fsm_script.h"
00002 #include "nel/ai/agent/msg_action.h"
00003 #include "nel/ai/agent/agent.h"
00004 #include "nel/ai/agent/agent_script.h"
00005 #include "nel/ai/agent/object_type.h"
00006 #include "nel/ai/script/codage.h"
00007 //#include "nel/ai/logic/interpret_object_operator.h"
00008 #include "nel/ai/script/interpret_message_action.h"
00009 
00010 namespace NLAIAGENT
00011 {
00012         static CGroupType listBidon;
00013 
00015         // Succes and failure messages declaration
00016 /*
00017         NLAISCRIPT::COperandSimpleListOr *CFsmScript::ParamIdSuccessMsg = NULL;
00018         NLAISCRIPT::CParam *CFsmScript::ParamSuccessMsg = NULL;
00019         NLAISCRIPT::COperandSimpleListOr *CFsmScript::ParamIdFailureMsg = NULL;
00020         NLAISCRIPT::CParam *CFsmScript::ParamFailureMsg = NULL;
00021 
00022         void CFsmScript::initClass()
00023         {
00024                 CFsmScript::ParamIdSuccessMsg = new NLAISCRIPT::COperandSimpleListOr(2,
00025                                                                                                                           new NLAIC::CIdentType(NLAIAGENT::CSuccessMsg::IdSuccessMsg),
00026                                                                                                                           new NLAIC::CIdentType(NLAISCRIPT::CSuccessMsgClass::IdSuccessMsgClass)        );              
00027                 CFsmScript::ParamSuccessMsg = new NLAISCRIPT::CParam(1,ParamIdSuccessMsg);              
00028                 CFsmScript::ParamIdFailureMsg = new NLAISCRIPT::COperandSimpleListOr(2,
00029                                                                                                                                   new NLAIC::CIdentType(NLAIAGENT::CFailureMsg::IdFailureMsg),
00030                                                                                                                                   new NLAIC::CIdentType(NLAISCRIPT::CFailureMsgClass::IdFailureMsgClass)        );
00031 
00032                 CFsmScript::ParamFailureMsg = new NLAISCRIPT::CParam(1,ParamIdFailureMsg);
00033         }
00034         void CFsmScript::releaseClass()
00035         {               
00036                 CFsmScript::ParamSuccessMsg->release();         
00037                 CFsmScript::ParamFailureMsg->release();
00038         }
00039 */
00041 
00042 
00043         CFsmScript::CFsmScript(const CFsmScript &a) : CActorScript(a)
00044         {
00045 /*              std::vector<IBasicAgent *>::const_iterator it_c = a._ActiveChilds.begin();
00046                 while ( it_c != a._ActiveChilds.end() )
00047                 {
00048                         _ActiveChilds.push_back( (IBasicAgent *) (*it_c)->clone() );
00049                         it_c++;
00050                 }
00051                 */
00052         }
00053 
00054         CFsmScript::CFsmScript(IAgentManager *manager, 
00055                                                            IBasicAgent *father,
00056                                                            std::list<IObjectIA *> &components,  
00057                                                            NLAISCRIPT::CFsmClass *actor_class )
00058         : CActorScript(manager, father, components, actor_class )
00059         {       
00060         }       
00061 
00062         CFsmScript::CFsmScript(IAgentManager *manager, bool stay_alive) : CActorScript( manager, stay_alive )
00063         {
00064         }
00065 
00066         CFsmScript::~CFsmScript()
00067         {
00068 /*              ParamIdSuccessMsg->release();
00069                 ParamSuccessMsg->release();
00070                 ParamIdFailureMsg->release();
00071                 ParamFailureMsg->release();
00072                 */
00073         }
00074 
00075         const NLAIC::IBasicType *CFsmScript::clone() const
00076         {               
00077                 CFsmScript *m = new CFsmScript(*this);
00078                 return m;
00079         }               
00080 
00081         const NLAIC::IBasicType *CFsmScript::newInstance() const
00082         {       
00083                 CFsmScript *instance;
00084                 if ( _AgentClass )
00085                 {
00086                         instance = (CFsmScript *) _AgentClass->buildNewInstance();
00087                 }
00088                 else 
00089                 {                       
00090                         instance = new CFsmScript(NULL);
00091                 }
00092                 return instance;
00093         }
00094         
00095         void CFsmScript::getDebugString(std::string &t) const
00096         {
00097                 if ( _AgentClass )
00098                 {
00099                         std::string buf;
00100                         _AgentClass->getDebugString(buf);
00101                         t += buf;
00102                 }
00103                 else
00104                         t += "<CFsmScript>";
00105         }
00106 
00107         bool CFsmScript::isEqual(const IBasicObjectIA &a) const
00108         {
00109                 return true;
00110         }
00111 
00112         const NLAIC::CIdentType &CFsmScript::getType() const
00113         {               
00114                 return IdFsmScript;
00115         }
00116 
00117         void CFsmScript::save(NLMISC::IStream &os)
00118         {
00119                 CAgentScript::save(os);
00120                 // TODO
00121         }
00122 
00123         void CFsmScript::load(NLMISC::IStream &is)
00124         {
00125                 CAgentScript::load(is);
00126                 // TODO
00127         }
00128 
00129         sint32 CFsmScript::getMethodIndexSize() const
00130         {
00131                 return CAgentScript::getBaseMethodCount();
00132         }
00133 
00134 /*      IObjectIA::CProcessResult CFsmScript::runMethodBase(int index,int heritance, IObjectIA *params)
00135         {               
00136                 IObjectIA::CProcessResult r;
00137 */
00138 /*              if ( index == fid_activate )
00139                 {
00140                         activate();
00141                         IObjectIA::CProcessResult r;
00142                         r.ResultState =  NLAIAGENT::processIdle;
00143                         r.Result = NULL;
00144                 }
00145 */
00146 /*              return CAgentScript::runMethodeBase(heritance,index,params);
00147         }
00148 
00149 */
00150 
00151         IObjectIA::CProcessResult CFsmScript::runMethodBase(int index,IObjectIA *params)
00152         {       
00153 
00154 
00155                 int i = index - IAgent::getMethodIndexSize();
00156 
00157                 if ( i < getBaseMethodCount() )
00158                         return CAgentScript::runMethodBase(index, params);
00159 
00160                 switch ( i )
00161                 {
00162                         case 1:
00163                                 onSuccess(params);
00164                                 break;
00165                         case 2:
00166                                 onFailure(params);
00167                                 break;
00168                 }
00169 
00170                 IObjectIA::CProcessResult r;
00171 
00172 #ifdef NL_DEBUG
00173                 std::string buf;
00174                 getDebugString(buf);
00175 #endif
00176 
00177                 return r;
00178         }
00179 
00180         int CFsmScript::getBaseMethodCount() const
00181         {
00182                 return CAgentScript::getBaseMethodCount();
00183         }
00184 
00185 
00186         tQueue CFsmScript::isMember(const IVarName *className,const IVarName *name,const IObjectIA &param) const
00187         {               
00188 #ifdef NL_DEBUG
00189                 const char *dbg_func_name = name->getString();
00190 #endif
00191 
00192                 tQueue result = CAgentScript::isMember( className, name, param);
00193 
00194                 static NLAIAGENT::CStringVarName run_tell("RunTell");
00195                 static NLAIAGENT::CStringVarName get_active("getActive");
00196 
00197                 if ( result.size() )
00198                         return result;
00199 
00200                 // Processes succes and failure functions
00201                 if ( *name ==  run_tell)
00202                 {
00203                         double d;
00204                         d = ((NLAISCRIPT::CParam &)*ParamSuccessMsg).eval((NLAISCRIPT::CParam &)param);
00205                         if ( d >= 0.0 )
00206                         {
00207                                 NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00208                                 result.push( NLAIAGENT::CIdMethod(  IAgent::getMethodIndexSize() + 1, 0.0,NULL, r_type ) );
00209                         }
00210 
00211                         d = ((NLAISCRIPT::CParam &)*ParamFailureMsg).eval((NLAISCRIPT::CParam &)param);
00212                         if ( d >= 0.0 )
00213                         {
00214                                 NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00215                                 result.push( NLAIAGENT::CIdMethod(  IAgent::getMethodIndexSize() + 2, 0.0,NULL, r_type ) );
00216                         }
00217                 }
00218 
00219                 // Returns a list of active states
00220                 if ( *name == get_active )
00221                 {
00222                         // Todo
00223                 }
00224 
00225                 return result;
00226         }
00227 
00228         const IObjectIA::CProcessResult &CFsmScript::run()
00229         {
00230                 return CAgentScript::run();
00231         }
00232 
00233         void CFsmScript::activate(IBasicAgent *child)
00234         {
00235 //              _ActiveChilds.push_back(child);
00236         }
00237 
00238         void CFsmScript::unactivate(IBasicAgent *child)
00239         {
00240 /*              std::vector<IBasicAgent *>::iterator it_c = _ActiveChilds.begin();
00241                 while ( it_c != _ActiveChilds.end() )
00242                 {
00243                         if ( child == *it_c )
00244                         {
00245                                 _ActiveChilds.erase( it_c );
00246                                 return;
00247                         }
00248                         it_c++;
00249                 }
00250                 */
00251         }
00252 
00253         void CFsmScript::runChildren()
00254         {
00255 #ifdef NL_DEBUG 
00256         const char *classBase = (const char *)getType();
00257 #endif
00258 /*              std::vector<IBasicAgent *>::iterator it_c = _ActiveChilds.begin();
00259                 while ( it_c != _ActiveChilds.end() )
00260                 {
00261                         IBasicAgent *child = *it_c;
00262                         (*it_c)->run();
00263                         it_c++;
00264                 }
00265                 */
00266 
00267                 // Activation des fils
00268                 CAgentScript::runChildren();
00269         }
00270 
00271 /*
00272         void CFsmScript::onSuccess( IObjectIA *)
00273         {
00274                 // Envoi d'un message succès au père
00275         }
00276 
00277         void CFsmScript::onFailure( IObjectIA *)
00278         {
00279                 // Envoi d'un message echec au père
00280         }
00281         */
00282 
00283         void CFsmScript::setTopLevel(NLAIAGENT::CAgentScript *tl)
00284         {
00285                 _TopLevel = tl;
00286 
00287                 for (int i = 0; i < _NbComponents; i++ )
00288                 {
00289                         if ( _Components[i]->isClassInheritedFrom( NLAIAGENT::CStringVarName("Actor") ) != -1 )
00290                         {
00291                                 if ( _TopLevel )
00292                                         ( (CActorScript *)_Components[i] )->setTopLevel( _TopLevel );
00293                                 else
00294                                         ( (CActorScript *)_Components[i] )->setTopLevel( this );
00295                         }
00296                 }
00297         }
00298 }