# 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 #include "nel/ai/agent/actor_script.h"
00002 #include "nel/ai/agent/agent_script.h"
00003 #include "nel/ai/agent/object_type.h"
00004 #include "nel/ai/script/codage.h"
00005 
00006 namespace NLAIAGENT
00007 {
00008         static CGroupType listBidon;
00009 
00010         CActorScript::CActorScript(const CActorScript &a) : CAgentScript(a)
00011         {
00012                 _IsActivated = a._IsActivated;
00013                 _OnActivateIndex = -1;
00014         }
00015 
00016         CActorScript::CActorScript(IAgentManager *manager, 
00017                                                            IBasicAgent *father,
00018                                                            std::list<IObjectIA *> &components,  
00019                                                            NLAISCRIPT::CActorClass *actor_class )
00020         : CAgentScript(manager, father, components, actor_class )
00021         {       
00022                 _IsActivated = false;
00023         }       
00024 
00025         CActorScript::CActorScript(IAgentManager *manager, bool stay_alive) : CAgentScript( manager )
00026         {
00027                 _IsActivated = false;
00028         }
00029 
00030         CActorScript::~CActorScript()
00031         {
00032         }
00033 
00035         bool CActorScript::isActivated()
00036         {
00037                 return _IsActivated;
00038         }
00039 
00041         void CActorScript::activate()
00042         {
00043                 if ( !_IsActivated )
00044                 {
00045                         onActivate();
00046                         _IsActivated = true;
00047                 }
00048         }
00049 
00051         void CActorScript::unActivate()
00052         {
00053                 if ( _IsActivated )
00054                 {
00055                         onUnActivate();
00056                         _IsActivated = false;
00057                 }
00058         }
00059 
00063         void CActorScript::switchActor(CActorScript *receiver, bool stay_active)
00064         {
00065                 receiver->activate();
00066 
00067                 if ( !stay_active )
00068                         unActivate();
00069         }
00070                 
00074         void CActorScript::switchActor(std::vector<CActorScript *> &actors, bool stay_active)
00075         {
00076                 std::vector<CActorScript *>::iterator it_act = actors.begin();
00077                 while ( it_act != actors.end() )
00078                 {
00079                         ( *it_act )->activate();
00080                         it_act++;
00081                 }
00082                 // TODO: Envoi de message "activate" 
00083                 if ( !stay_active )
00084                         unActivate();
00085         }
00086 
00090         void CActorScript::switchActor(std::vector<CComponentHandle *> &handles, bool stay_active)
00091         {
00092                 std::vector<CComponentHandle *>::iterator it_handle = handles.begin();
00093                 while ( it_handle != handles.end() )
00094                 {
00095                         ( (CActorScript *)( *it_handle )->getValue() )->activate();
00096                         it_handle++;
00097                 }
00098                 // TODO: Envoi de message "activate" 
00099                 if ( !stay_active )
00100                         unActivate();
00101         }
00102 
00103 
00105         void CActorScript::onActivate()
00106         {
00107                 // Default behaviour: do nothing
00108         }
00109 
00111         void CActorScript::onUnActivate()
00112         {
00113                 // default behaviour: do nothing
00114         }
00115 
00116         const NLAIC::IBasicType *CActorScript::clone() const
00117         {               
00118                 CActorScript *m = new CActorScript(*this);
00119                 return m;
00120         }               
00121 
00122         const NLAIC::IBasicType *CActorScript::newInstance() const
00123         {       
00124                 CActorScript *instance;
00125                 if ( _AgentClass )
00126                 {
00127                         instance = (CActorScript *) _AgentClass->buildNewInstance();
00128                 }
00129                 else 
00130                 {                       
00131                         instance = new CActorScript(NULL);
00132                 }
00133                 return instance;
00134         }
00135         
00136         void CActorScript::getDebugString(char *t) const
00137         {
00138                 strcpy(t,"CActorScript ");
00139                 if ( _IsActivated )
00140                         strcat(t, "<active>");
00141                 else
00142                         strcat(t, "<idle>");
00143         }
00144 
00145         bool CActorScript::isEqual(const IBasicObjectIA &a) const
00146         {
00147                 return true;
00148         }
00149 
00150         IObjectIA::CProcessResult CActorScript::sendMessage(IObjectIA *m)
00151         {
00152                 return CAgentScript::sendMessage(m);
00153         }
00154 
00155         void CActorScript::processMessages()
00156         {
00157                 if ( _IsActivated )
00158                         CAgentScript::processMessages();
00159         }
00160 
00161         const IObjectIA::CProcessResult &CActorScript::run()
00162         {
00163                 if ( _IsActivated )
00164                 {
00165                         return CAgentScript::run();
00166                 }
00167                 else
00168                         return IObjectIA::ProcessRun;
00169         }
00170 
00171 /*      IObjectIA *CActorScript::run(const IMessageBase &msg)
00172         {
00173                 return CAgentScript::run( msg );
00174         }
00175 */
00176         const NLAIC::CIdentType &CActorScript::getType() const
00177         {               
00178                 return IdActorScript;
00179         }
00180 
00181         void CActorScript::save(NLMISC::IStream &os)
00182         {
00183                 CAgentScript::save(os);
00184                 sint32 b = (_IsActivated == false);
00185                 os.serial( b );
00186         }
00187 
00188         void CActorScript::load(NLMISC::IStream &is)
00189         {
00190                 CAgentScript::load(is);
00191                 sint32 b;
00192                 is.serial( b );
00193                 _IsActivated =  !b ;
00194         }
00195 
00196         sint32 CActorScript::getMethodIndexSize() const
00197         {
00198 
00199                 return CAgentScript::getBaseMethodCount() + fid_switch;
00200         }
00201 
00202 //      virtual IObjectIA::CProcessResult runMethodBase(int heritance, int index,IObjectIA *);
00203 
00204 
00205         IObjectIA::CProcessResult CActorScript::runMethodBase(int index,int heritance, IObjectIA *params)
00206         {               
00207                 IObjectIA::CProcessResult r;
00208 
00209                 if ( index == fid_activate )
00210                 {
00211                         activate();
00212                         IObjectIA::CProcessResult r;
00213                         r.ResultState =  NLAIAGENT::processIdle;
00214                         r.Result = NULL;
00215                 }
00216 
00217                 if ( index == fid_onActivate )
00218                 {
00219                         onActivate();
00220                         IObjectIA::CProcessResult r;
00221                         r.ResultState =  NLAIAGENT::processIdle;
00222                         r.Result = NULL;
00223                 }
00224 
00225                 if ( index == fid_unActivate )
00226                 {
00227                         unActivate();
00228                         IObjectIA::CProcessResult r;
00229                         r.ResultState =  NLAIAGENT::processIdle;
00230                         r.Result = NULL;
00231                 }
00232 
00233                 if ( index == fid_onUnActivate )
00234                 {
00235                         onUnActivate();
00236                         IObjectIA::CProcessResult r;
00237                         r.ResultState =  NLAIAGENT::processIdle;
00238                         r.Result = NULL;
00239                 }
00240 
00241                 if ( index == fid_switch )
00242                 {
00243                         std::vector<CStringType *> handles;
00244                         if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00245                         {
00246                                 IBaseGroupType *fw = (IBaseGroupType *) ( ((NLAIAGENT::IBaseGroupType *)params) )->getFront();
00247                                 ( ((NLAIAGENT::IBaseGroupType *)params))->popFront();
00248                                 while ( fw->size() )
00249                                 {
00250                                         handles.push_back( (CStringType *) fw->getFront() );
00251                                         fw->popFront();
00252                                 }
00253                                 std::vector<CComponentHandle *> switched;
00254                                 for ( int i = 0; i < (int) handles.size(); i++)
00255                                         switched.push_back( new CComponentHandle(  handles[ i ]->getStr() , (IAgent *) getParent() ) );
00256 
00257                                 switchActor( switched, false );
00258                         }
00259                         IObjectIA::CProcessResult r;
00260                         r.ResultState =  NLAIAGENT::processIdle;
00261                         r.Result = NULL;
00262                 }
00263                 return CAgentScript::runMethodeMember(heritance,index,params);
00264         }
00265 
00266 
00267 
00268         IObjectIA::CProcessResult CActorScript::runMethodBase(int index,IObjectIA *params)
00269         {       
00270 
00271                 index = index - IAgent::getMethodIndexSize();
00272 /*
00273 
00274                 if ( index < getBaseMethodCount() )
00275                         return CAgentScript::runMethodeMember(index, params);
00276 */
00277                 IObjectIA::CProcessResult r;
00278 
00279                 char buf[1024];
00280                 getDebugString(buf);
00281 
00282 //              index = index - getBaseMethodCount();
00283                 
00284                 if ( index == fid_activate )
00285                 {
00286                         activate();
00287                         IObjectIA::CProcessResult r;
00288                         r.ResultState =  NLAIAGENT::processIdle;
00289                         r.Result = NULL;
00290                 }
00291 
00292                 if ( index == fid_onActivate )
00293                 {
00294                         onActivate();
00295                         IObjectIA::CProcessResult r;
00296                         r.ResultState =  NLAIAGENT::processIdle;
00297                         r.Result = NULL;
00298                 }
00299 
00300                 if ( index == fid_unActivate )
00301                 {
00302                         unActivate();
00303                         IObjectIA::CProcessResult r;
00304                         r.ResultState =  NLAIAGENT::processIdle;
00305                         r.Result = NULL;
00306                 }
00307 
00308                 if ( index == fid_onUnActivate )
00309                 {
00310                         onUnActivate();
00311                         IObjectIA::CProcessResult r;
00312                         r.ResultState =  NLAIAGENT::processIdle;
00313                         r.Result = NULL;
00314                 }
00315 
00316                 if ( index == fid_switch )
00317                 {
00318                         std::vector<CStringType *> handles;
00319                         if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00320                         {
00321 #ifdef _DEBUG
00322                                 const char *dbg_param_type = (const char *) params->getType();
00323                                 char dbg_param_string[1024 * 8];
00324                                 params->getDebugString(dbg_param_string);
00325 #endif
00326                                 const IObjectIA *fw = ( ((NLAIAGENT::IBaseGroupType *)params) )->getFront();
00327 #ifdef _DEBUG
00328                                 const char *dbg_param_front_type = (const char *) fw->getType();
00329 #endif
00330 
00331                                 ( ((NLAIAGENT::IBaseGroupType *)params))->popFront();
00332 //                              while ( fw->size() )
00333 //                              {
00334                                         handles.push_back( (CStringType *) fw);
00335 //                                      fw->popFront();
00336 //                              }
00337 
00338                                 std::vector<CComponentHandle *> switched;
00339                                 for ( int i = 0; i < (int) handles.size(); i++)
00340                                         switched.push_back( new CComponentHandle( handles[ i ]->getStr(), (IAgent *) getParent() ) );
00341                                 switchActor( switched, false );
00342                         }
00343                         IObjectIA::CProcessResult r;
00344                         r.ResultState =  NLAIAGENT::processIdle;
00345                         r.Result = NULL;
00346                 }
00347                 return r;
00348         }
00349 
00350         int CActorScript::getBaseMethodCount() const
00351         {
00352                 return CAgentScript::getBaseMethodCount() + fid_last;
00353         }
00354 
00355 
00356         tQueue CActorScript::isMember(const IVarName *className,const IVarName *name,const IObjectIA &param) const
00357         {               
00358 
00359                 const char *txt = name->getString();
00360 
00361                 tQueue result = CAgentScript::isMember( className, name, param);
00362 
00363                 if ( result.size() )
00364                         return result;
00365 
00366                 if ( *name == CStringVarName("activate") )
00367                 {
00368                         NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00369                         result.push( NLAIAGENT::CIdMethod(  IAgent::getMethodIndexSize() + fid_activate, 0.0,NULL, r_type ) );
00370                 }
00371 
00372                 if ( *name == CStringVarName("onActivate") )
00373                 {
00374                         NLAIAGENT::CObjectType *r_type = new NLAIAGENT::CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00375                         result.push( NLAIAGENT::CIdMethod( IAgent::getMethodIndexSize() + fid_onActivate , 0.0,NULL, r_type ) );
00376                 }
00377 
00378 
00379                 if ( *name == CStringVarName("unActivate") )
00380                 {
00381                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00382                         result.push( NLAIAGENT::CIdMethod( IAgent::getMethodIndexSize() + fid_onUnActivate, 0.0,NULL, r_type ) );
00383                 }
00384 
00385                 if ( *name == CStringVarName("onUnActivate") )
00386                 {
00387                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00388                         result.push( NLAIAGENT::CIdMethod( IAgent::getMethodIndexSize() + fid_unActivate, 0.0,NULL, r_type ) );
00389                 }
00390 
00391                 if ( *name == CStringVarName("switch") )
00392                 {
00393                         CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00394                         result.push( NLAIAGENT::CIdMethod( IAgent::getMethodIndexSize() + fid_switch, 0.0, NULL, r_type ) );
00395                 }
00396 
00397                 if(_AgentClass != NULL)
00398                 {
00399                         tQueue r = _AgentClass->isMember(className, name, param);
00400                         if(r.size() != 0) return r;
00401                 }
00402                 return result;
00403         }
00404 }