# 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  

actor_script.cpp

Go to the documentation of this file.
00001 
00008 /* Copyright, 2000 Nevrax Ltd.
00009  *
00010  * This file is part of NEVRAX NEL.
00011  * NEVRAX NEL is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2, or (at your option)
00014  * any later version.
00015 
00016  * NEVRAX NEL is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019  * General Public License for more details. 
00020 
00021  * You should have received a copy of the GNU General Public License
00022  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00024  * MA 02111-1307, USA.
00025  */
00026 
00027 #include "nel/ai/agent/actor_script.h"
00028 #include "nel/ai/agent/agent_script.h"
00029 #include "nel/ai/agent/object_type.h"
00030 #include "nel/ai/script/codage.h"
00031 #include "nel/ai/script/interpret_object_agent.h"
00032 #include "nel/ai/script/interpret_fsm.h"
00033 #include "nel/ai/logic/fsm_seq_script.h"
00034 #include "nel/ai/agent/msg_action.h"
00035 #include "nel/ai/script/interpret_message_action.h"
00036 #include "nel/ai/logic/goal_path.h"
00037 
00038 namespace NLAIAGENT
00039 {
00040         static CGroupType listBidon;
00041 
00043         // Succes and failure messages declaration
00044 
00045         NLAISCRIPT::COperandSimpleListOr *CActorScript::ParamIdSuccessMsg = NULL;
00046         NLAISCRIPT::CParam *CActorScript::ParamSuccessMsg = NULL;
00047         NLAISCRIPT::COperandSimpleListOr *CActorScript::ParamIdFailureMsg = NULL;
00048         NLAISCRIPT::CParam *CActorScript::ParamFailureMsg = NULL;
00049 
00050         void CActorScript::initClass()
00051         {
00052                 CActorScript::ParamIdSuccessMsg = new NLAISCRIPT::COperandSimpleListOr(2,
00053                                                                                                                           new NLAIC::CIdentType(NLAIAGENT::CSuccessMsg::IdSuccessMsg),
00054                                                                                                                           new NLAIC::CIdentType(NLAISCRIPT::CSuccessMsgClass::IdSuccessMsgClass)        );              
00055                 CActorScript::ParamSuccessMsg = new NLAISCRIPT::CParam(1,ParamIdSuccessMsg);            
00056                 CActorScript::ParamIdFailureMsg = new NLAISCRIPT::COperandSimpleListOr(2,
00057                                                                                                                                   new NLAIC::CIdentType(NLAIAGENT::CFailureMsg::IdFailureMsg),
00058                                                                                                                                   new NLAIC::CIdentType(NLAISCRIPT::CFailureMsgClass::IdFailureMsgClass)        );
00059 
00060                 CActorScript::ParamFailureMsg = new NLAISCRIPT::CParam(1,ParamIdFailureMsg);
00061         }
00062 
00063         void CActorScript::releaseClass()
00064         {               
00065                 CActorScript::ParamSuccessMsg->release();               
00066                 CActorScript::ParamFailureMsg->release();
00067         }
00069 
00070 
00071         CActorScript::CActorScript(const CActorScript &a) : CAgentScript(a)
00072         {
00073                 _IsActivated = a._IsActivated;
00074                 _OnActivateIndex = a._OnActivateIndex;
00075                 _OnUnActivateIndex = a._OnUnActivateIndex;
00076                 _TopLevel = a._TopLevel;
00077                 _IsPaused = false;
00078                 _NbAnswers = a._NbAnswers;
00079         }
00080 
00081         CActorScript::CActorScript(IAgentManager *manager, 
00082                                                            IBasicAgent *father,
00083                                                            std::list<IObjectIA *> &components,  
00084                                                            NLAISCRIPT::CAgentClass *actor_class )
00085         : CAgentScript(manager, father, components, actor_class )
00086         {       
00087                 _IsActivated = false;
00088                 _OnActivateIndex = -1;
00089                 _OnUnActivateIndex = -1;
00090                 _TopLevel = NULL;
00091                 _IsPaused = false;
00092                 _NbAnswers = 1;
00093         }       
00094 
00095         CActorScript::CActorScript(IAgentManager *manager, bool stay_alive) : CAgentScript( manager )
00096         {
00097                 _IsActivated = false;
00098                 _OnActivateIndex = -1;
00099                 _OnUnActivateIndex = -1;
00100                 _TopLevel = NULL;
00101                 _IsPaused = false;
00102                 _NbAnswers = 1;
00103         }
00104 
00105         CActorScript::~CActorScript()
00106         {
00107                 while (  _Launched.size() )
00108                 {                       
00109 #ifdef NL_DEBUG
00110         nlinfo (" _Launched 0x0%0x", _Launched.front());
00111 #endif
00112                         //_Launched.front()->setParent(NULL);
00113                         //_Launched.front()->Kill();
00114                         _Launched.front()->release();
00115                         _Launched.pop_front();
00116                 }
00117         }
00118 
00120         bool CActorScript::isActivated()
00121         {
00122                 return _IsActivated;
00123         }
00124 
00126         void CActorScript::activate()
00127         {
00128                 if ( !_IsActivated )
00129                 {
00130                         CAgentScript *father = (CAgentScript *) getParent();
00131 
00132                         if (  father && ( ( CAgentScript *)father)->getClass()->isClassInheritedFrom( CStringVarName("Fsm") ) != -1 )
00133                         {
00134                                 ( (CFsmScript *)father)->activate( this );
00135                         }
00136 
00137                         // Looks for the function to call at the activation of the state
00138                         static CStringVarName activate_func_name("OnActivate");
00139                         tQueue r = _AgentClass->isMember( NULL, &activate_func_name, NLAISCRIPT::CParam() );
00140                         if ( !r.empty() )
00141                                 _OnActivateIndex = r.top().Index;
00142                         else
00143                                 onActivate();
00144                         _IsActivated = true;
00145                 }
00146         }
00147 
00149         void CActorScript::unActivate()
00150         {
00151                 if ( _IsActivated )
00152                 {
00153                         CAgentScript *father = (CAgentScript *) getParent();
00154                         if (  father && ( ( CAgentScript *)father)->getClass()->isClassInheritedFrom( CStringVarName("Fsm") ) != -1 )
00155                         {
00156                                 ( (CFsmScript *)father)->unactivate( this );
00157                         }
00158 
00159                         static CStringVarName unactivate_func_name("OnUnActivate");
00160                         tQueue r = _AgentClass->isMember( NULL, &unactivate_func_name, NLAISCRIPT::CParam() );
00161                         if ( !r.empty() )
00162                         {       
00163                                 _OnUnActivateIndex = r.top().Index;
00164                                 const NLAIAGENT::IAgentManager *manager = getAgentManager();
00165                                 if ( manager != NULL )
00166                                 {
00167                                         NLAISCRIPT::CCodeContext *context = (NLAISCRIPT::CCodeContext *) manager->getAgentContext();
00168                                         context->Self = this;
00169                                         runMethodeMember( _OnUnActivateIndex ,context);
00170                                         _OnActivateIndex = -1;
00171                                 }
00172                         }
00173                         // Destroys launched childs?
00174                         while ( _Launched.size() )
00175                         {
00176                                 ( (CActorScript *) _Launched.front() )->cancel();
00177                                 //_Launched.front()->Kill();
00178                                 /*if(_Launched.front()->getRef() >= 2)
00179                                                                                                 _Launched.front()->release();*/
00180                                 removeDynamic(_Launched.front());
00181                                 _Launched.front()->release();
00182                                 _Launched.pop_front();
00183                         }
00184                         onUnActivate();
00185                         _IsActivated = false;
00186                         _IsPaused = false;
00187                 }
00188         }
00189 
00191         void CActorScript::pause()
00192         {
00193                 if ( _IsActivated )
00194                 {
00195 
00196                         if ( !_IsPaused )
00197                         {
00198                                 // Looks for the function to call when the actor is paused
00199                                 static CStringVarName activate_func_name("OnPause");
00200                                 tQueue r = _AgentClass->isMember( NULL, &activate_func_name, NLAISCRIPT::CParam() );
00201                                 if ( !r.empty() )
00202                                 {
00203                                         if ( getAgentManager() != NULL )
00204                                         {
00205                                                 _OnPauseIndex = r.top().Index;
00206                                                 NLAISCRIPT::CCodeContext *context = (NLAISCRIPT::CCodeContext *) getAgentManager()->getAgentContext();
00207                                                 context->Self = this;
00208                                                 runMethodeMember( _OnPauseIndex ,context);
00209                                                 _OnPauseIndex = -1;
00210                                         }
00211                                 }
00212                                 else
00213                                         onPause();
00214                                 _IsPaused = true;
00215                         }
00216 
00217                         // Calls the launched actors Pause callbacks
00218                         std::list<IBasicAgent *>::iterator it_l = _Launched.begin();
00219                         while ( it_l != _Launched.end() )
00220                         {
00221                                 CActorScript *launched = (CActorScript *) *it_l;
00222                                 launched->pause();
00223                                 it_l++;
00224                         }
00225                 
00226                         int i;
00227                         for ( i = 0; i < _NbComponents; i++ )
00228                         {
00229                                 if ( ((const NLAIC::CTypeOfObject &)_Components[i]->getType()) & NLAIC::CTypeOfObject::tActor )
00230                                 {
00231                                         ( (CActorScript *) _Components[i] )->pause();
00232                                 }
00233                         }
00234                 }
00235         }
00236 
00237         void CActorScript::onPause()
00238         {
00239                 // Default: doesn't do anything.
00240         }
00241 
00243         void CActorScript::restart()
00244         {
00245                 if ( _IsActivated )
00246                 {
00247                         if ( _IsPaused )
00248                         {
00249                                 // Looks for the function to call when the actor is restarted
00250                                 static CStringVarName activate_func_name("OnRestart");
00251                                 tQueue r = _AgentClass->isMember( NULL, &activate_func_name, NLAISCRIPT::CParam() );
00252                                 if ( !r.empty() )
00253                                 {
00254                                         if ( getAgentManager() != NULL )
00255                                         {
00256                                                 _OnRestartIndex = r.top().Index;
00257                                                 NLAISCRIPT::CCodeContext *context = (NLAISCRIPT::CCodeContext *) getAgentManager()->getAgentContext();
00258                                                 context->Self = this;
00259                                                 runMethodeMember( _OnRestartIndex ,context);
00260                                                 _OnRestartIndex = -1;
00261                                         }
00262                                 }
00263                                 else
00264                                         onRestart();
00265                                 _IsPaused = false;
00266                         }
00267 
00268                         // Calls the launched actors Restart callbacks
00269                         std::list<IBasicAgent *>::iterator it_l = _Launched.begin();
00270                         while ( it_l != _Launched.end() )
00271                         {
00272                                 CActorScript *launched = (CActorScript *) *it_l;
00273                                 launched->restart();
00274                                 it_l++;
00275                         }
00276 
00277                         int i;
00278                         for ( i = 0; i < _NbComponents; i++ )
00279                         {
00280                                 if ( ((const NLAIC::CTypeOfObject &)_Components[i]->getType()) & NLAIC::CTypeOfObject::tActor )
00281                                 {
00282                                         ( (CActorScript *) _Components[i] )->restart();
00283                                 }
00284                         }
00285                 }
00286         }
00287 
00288         void CActorScript::onRestart()
00289         {
00290         }
00291 
00295         void CActorScript::switchActor(CActorScript *receiver, bool stay_active)
00296         {
00297                 receiver->activate();
00298 
00299                 if ( !stay_active && ( receiver != this ) )
00300                         unActivate();
00301         }
00302                 
00306         void CActorScript::switchActor(std::vector<CActorScript *> &actors, bool stay_active)
00307         {
00308                 std::vector<CActorScript *>::iterator it_act = actors.begin();
00309                 while ( it_act != actors.end() )
00310                 {
00311                         ( *it_act )->activate();
00312                         it_act++;
00313                 }
00314 
00315                 // TODO: Envoi de message "activate" 
00316                 if ( !stay_active )
00317                         unActivate();
00318         }
00319 
00323         void CActorScript::switchActor(std::vector<CComponentHandle *> &handles, bool stay_active)
00324         {
00325                 std::vector<CComponentHandle *>::iterator it_handle = handles.begin();
00326                 while ( it_handle != handles.end() )
00327                 {
00328                         CActorScript *actor = (CActorScript *)( *it_handle )->getValue();
00329                         if ( actor != NULL )
00330                                 actor->activate();
00331                         else
00332                         {
00333                                 const char *sw_name = (*it_handle)->getCompName()->getString();
00334                                 nlwarning("SWITCH: component %s not found.", sw_name);
00335                         }
00336                         it_handle++;
00337                 }
00338                 // TODO: Envoi de message "activate" 
00339                 if ( !stay_active )
00340                         unActivate();
00341         }
00342 
00343 
00345         void CActorScript::onActivate()
00346         {
00347                 // Default behaviour: do nothing
00348         }
00349 
00351         void CActorScript::onUnActivate()
00352         {
00353                 // default behaviour: do nothing
00354         }
00355 
00356         const NLAIC::IBasicType *CActorScript::clone() const
00357         {               
00358                 CActorScript *m = new CActorScript(*this);
00359                 return m;
00360         }               
00361 
00362         const NLAIC::IBasicType *CActorScript::newInstance() const
00363         {       
00364                 CActorScript *instance;
00365                 if ( _AgentClass )
00366                 {
00367                         instance = (CActorScript *) _AgentClass->buildNewInstance();
00368                 }
00369                 else 
00370                 {                       
00371                         instance = new CActorScript(NULL);
00372                 }
00373                 return instance;
00374         }
00375         
00376         void CActorScript::getDebugString(std::string &t) const
00377         {
00378                 t = "CActorScript ";
00379                 if ( _IsActivated )
00380                         t += "<active>";
00381                 else
00382                         t += "<idle>";
00383         }
00384 
00385         bool CActorScript::isEqual(const IBasicObjectIA &a) const
00386         {
00387                 return true;
00388         }
00389 
00390         IObjectIA::CProcessResult CActorScript::sendMessage(IObjectIA *m)
00391         {               
00392                 return CAgentScript::sendMessage(m);
00393         }
00394 
00395         void CActorScript::processMessages()
00396         {
00397                 //if ( _IsActivated )
00398                 CAgentScript::processMessages();
00399         }
00400 
00401         const IObjectIA::CProcessResult &CActorScript::run()
00402         {
00403                 if ( _IsActivated )
00404                 {
00405                         if ( _OnActivateIndex != -1 )
00406                         {
00407                                 if ( getAgentManager() != NULL )
00408                                 {
00409                                         NLAISCRIPT::CCodeContext *context = (NLAISCRIPT::CCodeContext *) getAgentManager()->getAgentContext();
00410                                         context->Self = this;
00411                                         CProcessResult r = runMethodeMember( _OnActivateIndex ,context);
00412                                         if ( r.Result != NULL )
00413                                                 r.Result->release();
00414                                         _OnActivateIndex = -1;
00415                                 }
00416                         }
00417                         return CAgentScript::run();
00418                 }
00419                 else
00420                 {
00421                         processMessages();
00422                         return IObjectIA::ProcessRun;
00423                 }
00424         }
00425 
00426         const NLAIC::CIdentType &CActorScript::getType() const
00427         {               
00428                 return IdActorScript;
00429         }
00430 
00431         void CActorScript::save(NLMISC::IStream &os)
00432         {
00433                 CAgentScript::save(os);
00434                 sint32 b = (_IsActivated == false);
00435                 os.serial( b );
00436         }
00437 
00438         void CActorScript::load(NLMISC::IStream &is)
00439         {
00440                 CAgentScript::load(is);
00441                 sint32 b;
00442                 is.serial( b );
00443                 _IsActivated =  !b ;
00444         }
00445 
00446         sint32 CActorScript::getMethodIndexSize() const
00447         {
00448                 return CAgentScript::getMethodIndexSize() + fid_last;
00449         }
00450 
00451 //      virtual IObjectIA::CProcessResult runMethodBase(int heritance, int index,IObjectIA *);
00452 
00453 
00454         IObjectIA::CProcessResult CActorScript::runMethodBase(int index,int heritance, IObjectIA *params)
00455         {               
00456                 IObjectIA::CProcessResult r;
00457 
00458                 switch ( index )
00459                 {
00460                         case fid_activate:
00461                                 activate();
00462                                 r.ResultState =  NLAIAGENT::processIdle;
00463                                 r.Result = NULL;
00464                                 break;
00465 
00466                         case fid_onActivate:
00467                                 onActivate();
00468                                 r.ResultState =  NLAIAGENT::processIdle;
00469                                 r.Result = NULL;
00470                                 break;
00471 
00472                         case fid_unActivate:
00473                                 unActivate();
00474                                 r.ResultState =  NLAIAGENT::processIdle;
00475                                 r.Result = NULL;
00476                                 break;
00477 
00478                         case fid_onUnActivate:
00479                                 onUnActivate();
00480                                 r.ResultState =  NLAIAGENT::processIdle;
00481                                 r.Result = NULL;
00482                                 break;
00483 
00484                         case fid_switch:
00485                                 {
00486                                         std::vector<CStringType *> handles;
00487                                         if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00488                                         {
00489                                                 IBaseGroupType *fw = (IBaseGroupType *) ( ((NLAIAGENT::IBaseGroupType *)params) )->getFront();
00490                                                 //( ((NLAIAGENT::IBaseGroupType *)params))->popFront();
00491                                                 while ( fw->size() )
00492                                                 {
00493                                                         handles.push_back( (CStringType *) fw->getFront() );
00494                                                         fw->popFront();
00495                                                 }
00496                                                 std::vector<CComponentHandle *> switched;
00497                                                 for ( int i = 0; i < (int) handles.size(); i++)
00498                                                         switched.push_back( new CComponentHandle(  handles[ i ]->getStr() , (IAgent *) getParent() ) );
00499 
00500                                                 switchActor( switched, false );
00501                                         }
00502                                         r.ResultState =  NLAIAGENT::processIdle;
00503                                         r.Result = NULL;
00504                                 }
00505                                 break;
00506                                 
00507                         case fid_launch:
00508                         
00509                                 if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00510                                 {
00511                                         /*IObjectIA *child = (IObjectIA *)((NLAIAGENT::IBaseGroupType *)params)->get();
00512                                         addDynamicAgent( (NLAIAGENT::IBaseGroupType *) params);                                 
00513                                         if ( child->isClassInheritedFrom( CStringVarName("Actor") ) != -1 )
00514                                         {
00515                                                 if ( _TopLevel )
00516                                                         ((CActorScript *)child)->setTopLevel( _TopLevel );
00517                                                 else
00518                                                         ((CActorScript *)child)->setTopLevel( this );
00519                                                 
00520                                                 ((CActorScript *)child)->activate();
00521                                         }
00522 
00523                                         _Launched.push_back( (NLAIAGENT::IAgent *) child );
00524                                         child->incRef();*/
00525                                         CIteratorContener i = ((NLAIAGENT::IBaseGroupType *) params)->getIterator();
00526                                         CStringType &s = (CStringType &)*i++;
00527                                         IBasicAgent *a = (IBasicAgent *)i++;
00528                                         Launch(s.getStr().getString(), a);
00529                                         a->incRef();
00530                                 }
00531                                 r.ResultState =  NLAIAGENT::processIdle;
00532                                 r.Result = NULL;
00533                                 return r;
00534                         
00535                         case fid_launch_goal:
00536                                 {
00537                                         NLAILOGIC::CGoalPath *goal_path = new NLAILOGIC::CGoalPath( this );
00538                                         goal_path->setFather( (CProxyAgentMail *) new CLocalAgentMail( this ) );
00539                                         // If the constructor() function is explicitely called and the object has already been initialised
00540                                         NLAIAGENT::IBaseGroupType *p = (NLAIAGENT::IBaseGroupType *) params;
00541                                         while ( p->size() )
00542                                         {
00543                                                 goal_path->addGoal( (NLAILOGIC::CGoal *) p->getFront()->clone() );
00544                                                 p->popFront();
00545                                         }
00546                                         Launch( "goal_path", goal_path );
00547                                         goal_path->incRef();
00548                                         return IObjectIA::CProcessResult();
00549                                 }
00550 
00551                         case fid_launched:
00552                                 {
00553                                         CVectorGroupType *result = new CVectorGroupType();
00554                                         std::list<IBasicAgent *>::iterator it_l = _Launched.begin();
00555 
00556                                         while ( it_l != _Launched.end() )
00557                                         {
00558                                                 result->push( new CLocalMailBox( (const NLAIAGENT::IWordNumRef *) **it_l ) );
00559                                                 it_l++;
00560                                         }
00561                                         r.ResultState = NLAIAGENT::processIdle;
00562                                         r.Result = result;
00563                                         return r;
00564                                 }
00565 
00566                         case fid_pause:
00567                                 pause();
00568                                 r.ResultState =  NLAIAGENT::processIdle;
00569                                 r.Result = NULL;
00570                                 return r;
00571 
00572                         case fid_restart:
00573                                 restart();
00574                                 r.ResultState =  NLAIAGENT::processIdle;
00575                                 r.Result = NULL;
00576                                 return r;
00577 
00578                         case fid_success:
00579                         case fid_msg_success:
00580                                 onSuccess(params);
00581                                 r.Result = new NLAIAGENT::CSuccessMsg();
00582                                 return r;
00583 
00584                         case fid_failure:
00585                         case fid_msg_failure:
00586                                 onFailure(params);
00587                                 r.Result = new NLAIAGENT::CFailureMsg();
00588                                 return r;
00589 
00590                         case fid_nb_answers:
00591                                 _NbAnswers = (uint32) ( (NLAIAGENT::DigitalType *) ( (NLAIAGENT::IBaseGroupType *) params )->get() )->getNumber();
00592                                 return r;
00593 
00594                         case fid_toplevel:
00595                                 r.Result = new CLocalAgentMail( (IBasicAgent *) getTopLevel() );                                
00596                                 return r;
00597 
00598                         case fid_owner:
00599                                 r.Result = new CLocalAgentMail( (IBasicAgent *) getTopLevel()->getParent() );                           
00600                                 return r;
00601 
00602                         case fid_isactive:
00603                                 r.Result = new NLAILOGIC::CBoolType( _IsActivated );
00604                                 return r;
00605                 }
00606                 return r;
00607         }
00608 
00609         void CActorScript::Launch(const std::string &name, NLAIAGENT::IBasicAgent *child)
00610         {
00611                 
00612                 CStringType stringName( name.c_str() );
00613                 addDynamicAgent( stringName , child );          
00614                 _NbAnswers++;
00615 
00616                 if ( child->isClassInheritedFrom( CStringVarName("Actor") ) != -1 )
00617                 {
00618                         if ( _TopLevel )
00619                                 ((CActorScript *)child)->setTopLevel( _TopLevel );
00620                         else
00621                                 ((CActorScript *)child)->setTopLevel( this );
00622                         
00623                         ((CActorScript *)child)->activate();
00624                 }
00625 
00626                 _Launched.push_back( (NLAIAGENT::IAgent *) child );             
00627         }
00628 
00629         
00630         IObjectIA::CProcessResult CActorScript::runMethodBase(int index,IObjectIA *params)
00631         {       
00632                 int i = index - CAgentScript::getMethodIndexSize();
00633 
00634 
00635                 IObjectIA::CProcessResult r;
00636                 std::vector<CStringType *> handles;
00637 
00638                 switch( i )
00639                 {
00640                 
00641                         case fid_activate:
00642                                 activate();
00643                                 r.ResultState =  NLAIAGENT::processIdle;
00644                                 r.Result = NULL;
00645                                 break;
00646 
00647                         case fid_onActivate:
00648                                 onActivate();
00649                                 r.ResultState =  NLAIAGENT::processIdle;
00650                                 r.Result = NULL;
00651                                 break;
00652 
00653                         case fid_unActivate:
00654                                 unActivate();
00655                                 r.ResultState =  NLAIAGENT::processIdle;
00656                                 r.Result = NULL;
00657                                 break;
00658                         
00659                         case fid_onUnActivate:
00660                                 onUnActivate();
00661                                 r.ResultState =  NLAIAGENT::processIdle;
00662                                 r.Result = NULL;
00663                                 return r;
00664                                 break;
00665 
00666                         case fid_switch:
00667                                 if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00668                                 {
00669                                         const IObjectIA *fw = ( ((NLAIAGENT::IBaseGroupType *)params) )->get();
00670 
00671                                         //( ((NLAIAGENT::IBaseGroupType *)params))->popFront();
00672 //                                      while ( fw->size() )
00673 //                                      {
00674                                                 handles.push_back( (CStringType *) fw);
00675 //                                              fw->popFront();
00676 //                                      }
00677 
00678                                         std::vector<CComponentHandle *> switched;
00679                                         int i;
00680                                         for ( i = 0; i < (int) handles.size(); i++)
00681                                                 switched.push_back( new CComponentHandle( handles[ i ]->getStr(), (IAgent *) getParent() ) );
00682                                         switchActor( switched, false );
00683                                         for ( i = 0; i < (int) switched.size(); i++)
00684                                                 delete switched[i];
00685                                 }
00686                                 r.ResultState =  NLAIAGENT::processIdle;
00687                                 r.Result = NULL;
00688                                 break;
00689 
00690                         case fid_launch:
00691                                 if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00692                                 {
00693                                         /*IObjectIA *child = (IObjectIA *)((NLAIAGENT::IBaseGroupType *)params)->get();
00694                                         addDynamicAgent( (NLAIAGENT::IBaseGroupType *) params);
00695                                         _NbAnswers++;
00696                                         if ( child->isClassInheritedFrom( CStringVarName("Actor") ) != -1 )
00697                                         {
00698                                                 if ( _TopLevel )
00699                                                         ((CActorScript *)child)->setTopLevel( _TopLevel );
00700                                                 else
00701                                                         ((CActorScript *)child)->setTopLevel( this );
00702                                                 
00703                                                 ((CActorScript *)child)->activate();
00704                                         }
00705                                         _Launched.push_back( (NLAIAGENT::IAgent *) child );
00706                                         child->incRef();*/
00707                                         
00708                                         CIteratorContener i = ((NLAIAGENT::IBaseGroupType *) params)->getIterator();
00709                                         CStringType &s = (CStringType &)*i++;
00710                                         IBasicAgent *a = (IBasicAgent *)i++;
00711                                         Launch(s.getStr().getString(), a);
00712                                         a->incRef();
00713 
00714                                 }
00715                                 r.ResultState =  NLAIAGENT::processIdle;
00716                                 r.Result = NULL;
00717                                 return r;
00718 
00719                         case fid_pause:
00720                                 pause();
00721                                 r.ResultState =  NLAIAGENT::processIdle;
00722                                 r.Result = NULL;
00723                                 return r;
00724                                 break;
00725 
00726                         case fid_restart:
00727                                 restart();
00728                                 r.ResultState =  NLAIAGENT::processIdle;
00729                                 r.Result = NULL;
00730                                 return r;
00731                                 break;
00732 
00733                         case fid_success:
00734                         case fid_msg_success:
00735                                 processSuccess(params);
00736                                 r.Result = new NLAIAGENT::CSuccessMsg();
00737                                 return r;
00738                                 break;
00739 
00740                         case fid_failure:
00741                         case fid_msg_failure:
00742                                 processFailure(params);
00743                                 r.Result = new NLAIAGENT::CFailureMsg();
00744                                 return r;
00745                                 break;
00746 
00747                         case fid_nb_answers:
00748                                 _NbAnswers = (uint32) ( (NLAIAGENT::DigitalType *) ( (NLAIAGENT::IBaseGroupType *) params )->get() )->getNumber();
00749                                 return r;
00750 
00751                         case fid_toplevel:
00752                                 r.Result = new CLocalAgentMail( (IBasicAgent *) getTopLevel() );                                
00753                                 return r;
00754                                 break;
00755 
00756                         case fid_owner:
00757                                 r.Result = new CLocalAgentMail( (IBasicAgent *) getTopLevel()->getParent() );                           
00758                                 return r;
00759                                 break;
00760 
00761                         case fid_isactive:
00762                                 r.Result = new NLAILOGIC::CBoolType( _IsActivated );
00763                                 return r;
00764                                 break;
00765                 }
00766                 return CAgentScript::runMethodBase(index, params);
00767         }
00768 
00769         int CActorScript::getBaseMethodCount() const
00770         {
00771                 return CAgentScript::getBaseMethodCount() + fid_last;
00772         }
00773 
00774         tQueue CActorScript::getPrivateMember(const IVarName *className,const IVarName *name,const IObjectIA &param) const
00775         {
00776 
00777                 tQueue result; 
00778 
00779                 static NLAIAGENT::CStringVarName activate_name("activate");
00780                 static NLAIAGENT::CStringVarName onactivate_name("onActivate");
00781                 static NLAIAGENT::CStringVarName unactivate_name("unActivate");
00782                 static NLAIAGENT::CStringVarName onunactivate_name("onUnActivate");
00783                 static NLAIAGENT::CStringVarName switch_name("switch");
00784                 static NLAIAGENT::CStringVarName launch_name("Launch");
00785                 static NLAIAGENT::CStringVarName launch_goal_name("LaunchGoals");
00786                 static NLAIAGENT::CStringVarName launched_name("Launched");
00787                 static NLAIAGENT::CStringVarName tell_name("RunTell");
00788                 static NLAIAGENT::CStringVarName toplevel_name("TopLevel");
00789                 static NLAIAGENT::CStringVarName owner_name("Owner");
00790                 static NLAIAGENT::CStringVarName success_name("Success");
00791                 static NLAIAGENT::CStringVarName failure_name("Failure");
00792                 static NLAIAGENT::CStringVarName pause_name("Pause");
00793                 static NLAIAGENT::CStringVarName restart_name("Restart");
00794                 static NLAIAGENT::CStringVarName isactive_name("IsActivated");
00795                 static NLAIAGENT::CStringVarName _NbAnswers_name("WaitFor");
00796 
00797 
00798                 if ( *name == activate_name )
00799                 {
00800                         NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00801                         result.push( NLAIAGENT::CIdMethod(  CAgentScript::getMethodIndexSize() + fid_activate, 0.0,NULL, r_type ) );
00802                 }
00803 
00804                 if ( *name == onactivate_name )
00805                 {
00806                         NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00807                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_onActivate , 0.0,NULL, r_type ) );
00808                 }
00809 
00810                 if ( *name == unactivate_name )
00811                 {
00812                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00813                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_onUnActivate, 0.0,NULL, r_type ) );
00814                 }
00815 
00816                 if ( *name == onunactivate_name )
00817                 {
00818                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00819                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_unActivate, 0.0,NULL, r_type ) );
00820                 }
00821 
00822                 if ( *name == switch_name )
00823                 {
00824                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00825                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_switch, 0.0, NULL, r_type ) );
00826                 }
00827 
00828                 if ( *name == launch_name )
00829                 {
00830                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00831                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_launch, 0.0, NULL, r_type ) );
00832                 }
00833 
00834                 if ( *name == launch_goal_name )
00835                 {
00836                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00837                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_launch_goal, 0.0, NULL, r_type ) );
00838                 }
00839 
00840                 if ( *name == launched_name )
00841                 {
00842                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIAGENT::CVectorGroupType::IdVectorGroupType ) );
00843                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_launched, 0.0, NULL, r_type ) );
00844                 }
00845 
00846                 // Processes succes and failure functions
00847                 if ( *name == tell_name )
00848                 {
00849                         double d;
00850                         d = ((NLAISCRIPT::CParam &)*ParamSuccessMsg).eval((NLAISCRIPT::CParam &)param);
00851                         if ( d >= 0.0 )
00852                         {
00853                                 NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00854                                 result.push( NLAIAGENT::CIdMethod(  CAgentScript::getMethodIndexSize() + fid_msg_success, 0.0,NULL, r_type ) );
00855                         }
00856 
00857                         d = ((NLAISCRIPT::CParam &)*ParamFailureMsg).eval((NLAISCRIPT::CParam &)param);
00858                         if ( d >= 0.0 )
00859                         {
00860                                 NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00861                                 result.push( NLAIAGENT::CIdMethod(  CAgentScript::getMethodIndexSize() + fid_msg_failure, 0.0,NULL, r_type ) );
00862                         }
00863                 }
00864 
00865                 if ( *name == toplevel_name )
00866                 {
00867                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00868                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_toplevel, 0.0, NULL, r_type ) );
00869                 }
00870 
00871                 if ( *name == _NbAnswers_name )
00872                 {
00873                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00874                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_nb_answers, 0.0, NULL, r_type ) );
00875                 }
00876 
00877 
00878                 if ( *name == owner_name )
00879                 {
00880                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00881                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_owner, 0.0, NULL, r_type ) );
00882                 }
00883 
00884                 if ( *name == success_name )
00885                 {
00886                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00887                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_success, 0.0, NULL, r_type ) );
00888                 }
00889 
00890                 if ( *name == failure_name )
00891                 {
00892                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00893                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_failure, 0.0, NULL, r_type ) );
00894                 }
00895 
00896                 if ( *name == pause_name )
00897                 {
00898                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00899                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_pause, 0.0, NULL, r_type ) );
00900                 }
00901 
00902                 if ( *name == restart_name )
00903                 {
00904                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00905                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_restart, 0.0, NULL, r_type ) );
00906                 }
00907 
00908                 if ( *name == isactive_name )
00909                 {
00910                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00911                         result.push( NLAIAGENT::CIdMethod( CAgentScript::getMethodIndexSize() + fid_isactive, 0.0, NULL, r_type ) );
00912                 }
00913 
00914                 if ( result.empty() )
00915                         return CAgentScript::getPrivateMember(className, name, param);
00916 
00917                 return result;
00918         }
00919 
00920 
00921         void CActorScript::cancel()
00922         {
00923                 unActivate();
00924         }
00925 
00926         float CActorScript::priority() const
00927         {
00928                 // Look at predecessors priorities
00929                 return 1.0;
00930         }
00931 
00932         void CActorScript::setTopLevel(CAgentScript *tl)
00933         {
00934                 _TopLevel = tl;
00935 
00936                 for (int i = 0; i < _NbComponents; i++ )
00937                 {
00938                         if ( _Components[i]->isClassInheritedFrom( NLAIAGENT::CStringVarName("Actor") ) != -1 )
00939                         {
00940                                 if ( _TopLevel )
00941                                         ( (CActorScript *)_Components[i] )->setTopLevel( _TopLevel );
00942                                 else
00943                                         ( (CActorScript *)_Components[i] )->setTopLevel( this );
00944                         }
00945                 }
00946         }
00947 
00948         const CAgentScript *CActorScript::getTopLevel() const
00949         {
00950                 return _TopLevel;
00951         }
00952 
00953         void CActorScript::processSuccess(NLAIAGENT::IObjectIA *param)
00954         {
00955 
00956 #ifdef NL_DEBUG
00957                 const char *dbg_type = (const char *) getType();
00958 #endif
00959 //              _NbAnswers--;
00960 //              if ( _NbAnswers < 1 )
00961 //              {                       
00962                         success();
00963 //              }
00964         }
00965 
00966         void CActorScript::success()
00967         {
00968                 static CStringVarName onsuccess_func_name("OnSuccess");
00969                 tQueue r = _AgentClass->isMember( NULL, &onsuccess_func_name, NLAISCRIPT::CParam() );
00970                 if ( !r.empty() )
00971                 {       
00972                         _OnSuccessIndex = r.top().Index;
00973                         const NLAIAGENT::IAgentManager *manager = getAgentManager();
00974                         if ( manager != NULL )
00975                         {
00976                                 NLAISCRIPT::CCodeContext *context = (NLAISCRIPT::CCodeContext *) manager->getAgentContext();
00977                                 context->Self = this;
00978                                 runMethodeMember( _OnSuccessIndex ,context);
00979                                 _OnSuccessIndex = -1;
00980                         }
00981                 }
00982                 onSuccess( NULL );
00983         }
00984 
00985         void CActorScript::failure()
00986         {
00987                 static CStringVarName onfailure_func_name("OnFailure");
00988                 tQueue r = _AgentClass->isMember( NULL, &onfailure_func_name, NLAISCRIPT::CParam() );
00989                 if ( !r.empty() )
00990                 {       
00991                         _OnFailureIndex = r.top().Index;
00992                         const NLAIAGENT::IAgentManager *manager = getAgentManager();
00993                         if ( manager != NULL )
00994                         {
00995                                 NLAISCRIPT::CCodeContext *context = (NLAISCRIPT::CCodeContext *) manager->getAgentContext();
00996                                 context->Self = this;
00997                                 runMethodeMember( _OnFailureIndex ,context);
00998                                 _OnFailureIndex = -1;
00999                         }
01000                 }
01001                 onFailure( NULL );
01002         }
01003 
01004         void CActorScript::processFailure(NLAIAGENT::IObjectIA *param)
01005         {
01006 
01007 
01008 #ifdef NL_DEBUG
01009                 const char *dbg_type = (const char *) getType();
01010 #endif
01011 
01012 //              _NbAnswers--;
01013 //              if ( _NbAnswers < 1 )
01014 //              {
01015                         param->incRef();
01016                         failure();
01017 //              }
01018         }
01019 
01020         IMessageBase *CActorScript::runTell(const IMessageBase &m)
01021         {
01022                 processSuccess( (NLAIAGENT::IObjectIA *) m.getMessageGroup() );
01023                 return new NLAIAGENT::CSuccessMsg();
01024         }
01025 }