# 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.cpp

Go to the documentation of this file.
00001 #include "nel/ai/agent/actor.h"
00002 #include "nel/ai/agent/agent_script.h"
00003 #include "nel/ai/agent/object_type.h"
00004 
00005 namespace NLAIAGENT
00006 {
00007 
00008 
00010         CActor::CActor() : IAgent(NULL)
00011         {
00012         }
00013 
00015         CActor::CActor(IAgent *father, bool activated) : IAgent( father )
00016         {
00017                 _IsActivated = activated;
00018         }
00019 
00021         CActor::CActor(const CActor &c) : IAgent( c )
00022         {
00023                 _IsActivated = c._IsActivated;
00024         }
00025 
00026         CActor::~CActor()
00027         {
00028 //              for (int i = 0; i < c._Transitions.size(); i++ )
00029                 //      _Transitions[i]->release();
00030         }
00031 
00033         bool CActor::isActivated()
00034         {
00035                 return _IsActivated;
00036         }
00037 
00039         void CActor::activate()
00040         {
00041                 if ( !_IsActivated )
00042                 {
00043                         onActivate();
00044                         _IsActivated = true;
00045                 }
00046         }
00047 
00049         void CActor::unActivate()
00050         {
00051                 if ( _IsActivated )
00052                 {
00053                         onUnActivate();
00054                         _IsActivated = false;
00055                 }
00056         }
00057 
00061         void CActor::forwardActivity(CActor *receiver, bool stay_active)
00062         {
00063                 receiver->activate();
00064 
00065                 if ( !stay_active )
00066                         unActivate();
00067         }
00068                 
00072         void CActor::forwardActivity(std::vector<CActor *> &actors, bool stay_active)
00073         {
00074                 std::vector<CActor *>::iterator it_act = actors.begin();
00075                 while ( it_act != actors.end() )
00076                 {
00077                         ( *it_act )->activate();
00078                         it_act++;
00079                 }
00080                 // TODO: Envoi de message "activate" 
00081                 if ( !stay_active )
00082                         unActivate();
00083         }
00084 
00086         void CActor::addTransition(NLAILOGIC::IBaseCond *cond, std::vector<CActor *> &outputs, bool stay_alive)
00087         {
00088 
00089         }
00090 
00091 //      void CActor::addTransition(CTransition *trans)
00092 //      {
00093 //              _Transitions.push_back( trans );
00094 //      }
00095 
00097         void CActor::onActivate()
00098         {
00099                 // Default behaviour: do nothing
00100         }
00102         void CActor::onUnActivate()
00103         {
00104                 // default behaviour: do nothing
00105         }
00106 
00107         const NLAIC::IBasicType *CActor::clone() const
00108         {               
00109                 CActor *m = new CActor(*this);
00110                 return m;
00111         }               
00112 
00113         const NLAIC::IBasicType *CActor::newInstance() const
00114         {       
00115                 NLAIC::IBasicInterface *m;
00116                 if ( getParent() != NULL ) 
00117                         m = new CActor((IAgent *)getParent());
00118                 else 
00119                         m = new CActor(NULL);
00120                 return m;
00121         }
00122         
00123         void CActor::getDebugString(char *t) const
00124         {
00125                 strcpy(t,"CActor ");
00126                 if ( _IsActivated )
00127                         strcat(t, "<active>");
00128                 else
00129                         strcat(t, "<idle>");
00130         }
00131 
00132         bool CActor::isEqual(const IBasicObjectIA &a) const
00133         {
00134                 return true;
00135         }
00136 
00137         void CActor::processMessages()
00138         {
00139                 IAgent::processMessages();
00140 /*              while(getMail()->getMessageCount())
00141                 {
00142                         const IMessageBase &msg = getMail()->getMessage();                              
00143                         run( msg );
00144                         getMail()->popMessage();
00145                 }*/
00146         }
00147 
00148         const IObjectIA::CProcessResult &CActor::run()
00149         {
00150                 if ( _IsActivated )
00151                 {
00152                         return IAgent::run();
00153                 }
00154                 else
00155                         return IObjectIA::ProcessRun;
00156                 /*
00157                 setState(processBuzzy,NULL); 
00158 
00159                 runChildren();          // Gestion des fils
00160                 getMail()->run();       // Execution de la boite aux lettres
00161 
00162                 processMessages();      // Traitement de ses propres messages
00163 
00164                 setState(processIdle,NULL);
00165                 return getState();  
00166                 */
00167         }
00168 
00169         /*IObjectIA *CActor::run(const IMessageBase &msg)
00170         {
00171                 return IAgent::run( msg );
00172         }*/
00173 
00174         const NLAIC::CIdentType &CActor::getType() const
00175         {               
00176                 return IdActor;
00177         }
00178 
00179         void CActor::save(NLMISC::IStream &os)
00180         {
00181                 IAgent::save(os);
00182                 os.serial( (bool &) _IsActivated );
00183                 
00184         }
00185 
00186         void CActor::load(NLMISC::IStream &is)
00187         {
00188                 IAgent::load(is);
00189         }
00190 
00191         tQueue CActor::isMember(const IVarName *className,const NLAIAGENT::IVarName *name,const IObjectIA &param) const
00192         {
00193                 tQueue result;
00194 
00195                 result = IAgent::isMember( className, name, param );
00196 
00197                 if ( result.size() )
00198                         return result;
00199 
00200 //              if(className != NULL) 
00201 //              {
00202                         if ( *name == CStringVarName("activate") )
00203                         {
00204                                 CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00205                                 result.push( NLAIAGENT::CIdMethod( fid_activate + IAgent::getMethodIndexSize(), 0.0,NULL, r_type ) );
00206                         }
00207 
00208                         if ( *name == CStringVarName("unActivate") )
00209                         {
00210                                 CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00211                                 result.push( NLAIAGENT::CIdMethod( fid_unActivate + IAgent::getMethodIndexSize(), 0.0,NULL, r_type ) );
00212                         }
00213 
00214                         if ( *name == CStringVarName("forwardActivity") )
00215                         {
00216                                 CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00217                                 result.push( NLAIAGENT::CIdMethod( fid_forwardActivity + IAgent::getMethodIndexSize(), 0.0,NULL, r_type ) );
00218                         }
00219 //              }
00220                 return result;
00221         }
00222 
00223         // Executes a method from its index id and with its parameters
00224         IObjectIA::CProcessResult CActor::runMethodeMember(sint32 id, IObjectIA *params)
00225         {
00226                 if ( id < IAgent::getMethodIndexSize() )
00227                         return IAgent::runMethodeMember(id, params);
00228 
00229                 IObjectIA::CProcessResult r;
00230 
00231                 char buf[1024];
00232                 getDebugString(buf);
00233 
00234                 id = id - IAgent::getMethodIndexSize();
00235                 
00236                 if ( id == fid_activate )
00237                 {
00238                         activate();
00239                         IObjectIA::CProcessResult r;
00240                         r.ResultState =  NLAIAGENT::processIdle;
00241                         r.Result = NULL;
00242                 }
00243 
00244                 if ( id == fid_unActivate )
00245                 {
00246                         unActivate();
00247                         IObjectIA::CProcessResult r;
00248                         r.ResultState =  NLAIAGENT::processIdle;
00249                         r.Result = NULL;
00250                 }
00251 
00252                 if ( id == fid_forwardActivity )
00253                 {
00254                         std::vector<CActor *> forwarded;
00255                         if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00256                         {
00257                                 IBaseGroupType *fw = (IBaseGroupType *) ( ((NLAIAGENT::IBaseGroupType *)params) )->getFront();
00258                                 ( ((NLAIAGENT::IBaseGroupType *)params))->popFront();
00259                                 while ( fw->size() )
00260                                 {
00261                                         forwarded.push_back( (CActor *) fw->getFront() );
00262                                         fw->popFront();
00263                                 }
00264                                 forwardActivity( forwarded, false );
00265                         }
00266                         IObjectIA::CProcessResult r;
00267                         r.ResultState =  NLAIAGENT::processIdle;
00268                         r.Result = NULL;
00269                 }
00270                 return r;
00271         }
00272 
00273         sint32 CActor::getMethodIndexSize() const
00274         {
00275                 return IAgent::getMethodIndexSize() + 2;
00276         }
00277 }