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 {
00008 CActor::CActor() : IAgent(NULL)
00009 {
00010 }
00011
00013 CActor::CActor(IAgent *father, bool activated) : IAgent( father )
00014 {
00015 _IsActivated = activated;
00016 }
00017
00019 CActor::CActor(const CActor &c) : IAgent( c )
00020 {
00021 _IsActivated = c._IsActivated;
00022 }
00023
00024 CActor::~CActor()
00025 {
00026
00027
00028 }
00029
00031 bool CActor::isActivated()
00032 {
00033 return _IsActivated;
00034 }
00035
00037 void CActor::activate()
00038 {
00039 if ( !_IsActivated )
00040 {
00041 onActivate();
00042 _IsActivated = true;
00043 }
00044 }
00045
00047 void CActor::unActivate()
00048 {
00049 if ( _IsActivated )
00050 {
00051 onUnActivate();
00052 _IsActivated = false;
00053 }
00054 }
00055
00059 void CActor::forwardActivity(CActor *receiver, bool stay_active)
00060 {
00061 receiver->activate();
00062
00063 if ( !stay_active )
00064 unActivate();
00065 }
00066
00070 void CActor::forwardActivity(std::vector<CActor *> &actors, bool stay_active)
00071 {
00072 std::vector<CActor *>::iterator it_act = actors.begin();
00073 while ( it_act != actors.end() )
00074 {
00075 ( *it_act )->activate();
00076 it_act++;
00077 }
00078
00079 if ( !stay_active )
00080 unActivate();
00081 }
00082
00084 void CActor::addTransition(NLAILOGIC::IBaseCond *cond, std::vector<CActor *> &outputs, bool stay_alive)
00085 {
00086
00087 }
00088
00089
00090
00091
00092
00093
00095 void CActor::onActivate()
00096 {
00097
00098 }
00100 void CActor::onUnActivate()
00101 {
00102
00103 }
00104
00105 const NLAIC::IBasicType *CActor::clone() const
00106 {
00107 CActor *m = new CActor(*this);
00108 return m;
00109 }
00110
00111 const NLAIC::IBasicType *CActor::newInstance() const
00112 {
00113 NLAIC::IBasicInterface *m;
00114 if ( getParent() != NULL )
00115 m = new CActor((IAgent *)getParent());
00116 else
00117 m = new CActor(NULL);
00118 return m;
00119 }
00120
00121 void CActor::getDebugString(std::string &t) const
00122 {
00123 t = "CActor ";
00124 if ( _IsActivated )
00125 t += "<active>";
00126 else
00127 t += "<idle>";
00128 }
00129
00130 bool CActor::isEqual(const IBasicObjectIA &a) const
00131 {
00132 return true;
00133 }
00134
00135 void CActor::processMessages()
00136 {
00137 IAgent::processMessages();
00138
00139
00140
00141
00142
00143
00144 }
00145
00146 const IObjectIA::CProcessResult &CActor::run()
00147 {
00148 if ( _IsActivated )
00149 {
00150 return IAgent::run();
00151 }
00152 else
00153 return IObjectIA::ProcessRun;
00154
00155 }
00156
00157
00158
00159
00160
00161
00162 const NLAIC::CIdentType &CActor::getType() const
00163 {
00164 return IdActor;
00165 }
00166
00167 void CActor::save(NLMISC::IStream &os)
00168 {
00169 IAgent::save(os);
00170 os.serial( (bool &) _IsActivated );
00171
00172 }
00173
00174 void CActor::load(NLMISC::IStream &is)
00175 {
00176 IAgent::load(is);
00177 }
00178
00179 tQueue CActor::isMember(const IVarName *className,const NLAIAGENT::IVarName *name,const IObjectIA ¶m) const
00180 {
00181 tQueue result;
00182
00183 result = IAgent::isMember( className, name, param );
00184
00185 if ( result.size() )
00186 return result;
00187
00188
00189
00190 if ( *name == CStringVarName("activate") )
00191 {
00192 CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00193 result.push( NLAIAGENT::CIdMethod( fid_activate + IAgent::getMethodIndexSize(), 0.0,NULL, r_type ) );
00194 }
00195
00196 if ( *name == CStringVarName("unActivate") )
00197 {
00198 CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00199 result.push( NLAIAGENT::CIdMethod( fid_unActivate + IAgent::getMethodIndexSize(), 0.0,NULL, r_type ) );
00200 }
00201
00202 if ( *name == CStringVarName("forwardActivity") )
00203 {
00204 CObjectType *r_type = new CObjectType( new NLAIC::CIdentType( NLAIC::CIdentType::VoidType ) );
00205 result.push( NLAIAGENT::CIdMethod( fid_forwardActivity + IAgent::getMethodIndexSize(), 0.0,NULL, r_type ) );
00206 }
00207
00208 return result;
00209 }
00210
00211
00212 IObjectIA::CProcessResult CActor::runMethodeMember(sint32 id, IObjectIA *params)
00213 {
00214 if ( id < IAgent::getMethodIndexSize() )
00215 return IAgent::runMethodeMember(id, params);
00216
00217 IObjectIA::CProcessResult r;
00218
00219 #ifdef NL_DEBUG
00220 std::string buf;
00221 getDebugString(buf);
00222 #endif
00223
00224 id = id - IAgent::getMethodIndexSize();
00225
00226 if ( id == fid_activate )
00227 {
00228 activate();
00229 IObjectIA::CProcessResult r;
00230 r.ResultState = NLAIAGENT::processIdle;
00231 r.Result = NULL;
00232 }
00233
00234 if ( id == fid_unActivate )
00235 {
00236 unActivate();
00237 IObjectIA::CProcessResult r;
00238 r.ResultState = NLAIAGENT::processIdle;
00239 r.Result = NULL;
00240 }
00241
00242 if ( id == fid_forwardActivity )
00243 {
00244 std::vector<CActor *> forwarded;
00245 if ( ( (NLAIAGENT::IBaseGroupType *) params)->size() )
00246 {
00247 IBaseGroupType *fw = (IBaseGroupType *) ( ((NLAIAGENT::IBaseGroupType *)params) )->getFront();
00248 ( ((NLAIAGENT::IBaseGroupType *)params))->popFront();
00249 while ( fw->size() )
00250 {
00251 forwarded.push_back( (CActor *) fw->getFront() );
00252 fw->popFront();
00253 }
00254 forwardActivity( forwarded, false );
00255 }
00256 IObjectIA::CProcessResult r;
00257 r.ResultState = NLAIAGENT::processIdle;
00258 r.Result = NULL;
00259 }
00260 return r;
00261 }
00262
00263 sint32 CActor::getMethodIndexSize() const
00264 {
00265 return IAgent::getMethodIndexSize() + 2;
00266 }
00267 }