# 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  

message_script.cpp

Go to the documentation of this file.
00001 
00006 /* Copyright, 2000 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */
00024 #include "nel/ai/agent/message_script.h"
00025 #include "nel/ai/script/interpret_object_message.h"
00026 #include "nel/ai/script/codage.h"
00027 #include "nel/ai/script/type_def.h"
00028 #include "nel/ai/script/object_unknown.h"
00029 #include "nel/ai/agent/msg.h"
00030 #include "nel/ai/e/ai_exception.h"
00031 
00032 namespace NLAIAGENT
00033 {
00034         static CGroupType listBidon;
00035 
00036         CMessageScript::CMessageScript(const CMessageScript &m):IMessageBase(m)
00037         {
00038                 _MessageClass = m._MessageClass;
00039                 /*if ( _MessageClass )
00040                         _MessageClass->incRef();*/
00041                 //CVectorGroupType *x = new CVectorGroupType(m.size());
00042         }
00043         
00044         CMessageScript::CMessageScript(NLAISCRIPT::CMessageClass *b) : _MessageClass( b )
00045         {
00046                 /*if ( _MessageClass )
00047                         _MessageClass->incRef();*/
00048         }
00049 
00050         CMessageScript::CMessageScript( std::list<IObjectIA *> &components, NLAISCRIPT::CMessageClass *message_class ): _MessageClass( message_class )
00051         {       
00052                 /*if ( _MessageClass )
00053                         _MessageClass->incRef();*/
00054 
00055                 // Creates the static components array
00056                 CVectorGroupType *x = new CVectorGroupType(components.size());
00057                 std::list<IObjectIA *>::iterator it_c = components.begin();             
00058                 sint32 i = 0;
00059                 while ( it_c != components.end() )
00060                 {                       
00061                         x->setObject(i,*it_c);
00062                         it_c++;                                 
00063                         i ++;
00064                 }
00065                 setMessageGroup(x);
00066                 setGroup(CMessageGroup::msgScriptingGroup);
00067         }       
00068 
00069         CMessageScript::~CMessageScript()
00070         {
00071 /*#ifdef NL_DEBUG
00072                 const char *name = (const char *)getType();
00073 #endif*/
00074                 /*if ( _MessageClass )
00075                         _MessageClass->release();               */
00076         }
00077 
00078         sint32 CMessageScript::getMethodIndexSize() const
00079         {
00080                 return IMessageBase::getMethodIndexSize() + _MessageClass->getMethodIndexSize();
00081         }
00082 
00083         sint32 CMessageScript::getBaseMethodCount() const
00084         {
00085                 return IMessageBase::getMethodIndexSize();
00086         }
00087 
00088 
00089         NLAISCRIPT::IOpCode *CMessageScript::getMethode(sint32 inheritance,sint32 index)
00090         {
00091 #ifdef NL_DEBUG
00092                 if ( index >= _MessageClass->getMethodIndexSize())
00093                 {
00094                         throw NLAIE::CExceptionIndexError();
00095                 }
00096 
00097                 if ( inheritance >= _MessageClass->sizeVTable())
00098                 {
00099                         throw NLAIE::CExceptionIndexError();
00100                 }
00101 #endif
00102                 return (NLAISCRIPT::IOpCode *)_MessageClass->getBrancheCode(inheritance,index).getCode();
00103         }
00104 
00105         NLAISCRIPT::IOpCode *CMessageScript::getMethode(sint32 index)
00106         {
00107 #ifdef NL_DEBUG
00108                 if ( index >= _MessageClass->getMethodIndexSize())
00109                 {
00110                         throw NLAIE::CExceptionIndexError();
00111                 }
00112 #endif
00113                 return (NLAISCRIPT::IOpCode *)_MessageClass->getBrancheCode(index).getCode();
00114         }
00115 
00116 
00117         IObjectIA::CProcessResult CMessageScript::runMethodeMember(sint32 inheritance, sint32 index, IObjectIA *c)
00118         {
00119                 if(c->getType() != NLAISCRIPT::CCodeContext::IdCodeContext)
00120                 {
00121                         return IMessageBase::runMethodeMember(inheritance,index, c);
00122                 }
00123 
00124                 NLAISCRIPT::IOpCode *opPtr = NULL;
00125                 NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*c;
00126 
00127                 sint32 i = index - getBaseMethodCount();
00128                 if(i < 0)
00129                 {
00130                         /*if (context.ContextDebug.Active)
00131                         {
00132                                 context.ContextDebug.Param.push_back(&listBidon);
00133                                 listBidon.incRef();
00134                         }*/
00135 
00136                         IObjectIA::CProcessResult r = IMessageBase::runMethodeMember(index,(IObjectIA *)context.Param.back());
00137                         if(r.Result != NULL)
00138                         {
00139                                 context.Stack++;
00140                                 context.Stack[(int)context.Stack] = r.Result;
00141                         }
00142                         r.Result = NULL;
00143                         return r;
00144                 }
00145                 else
00146                 {
00147                         opPtr = getMethode( inheritance, i);
00148                 }
00149                 
00150                 IObjectIA::CProcessResult r;
00151 
00152                 if(opPtr)
00153                 {
00154                         NLAISCRIPT::IOpCode &op = *opPtr;
00155                         NLAISCRIPT::CCodeBrancheRun *opTmp = context.Code;
00156                         sint32 ip = (uint32)*context.Code;
00157                         context.Code = (NLAISCRIPT::CCodeBrancheRun *)&op;              
00158                         *context.Code = 0;
00159 
00160                         /*TProcessStatement k = IObjectIA::ProcessIdle;
00161 
00162                         while(k != IObjectIA::ProcessEnd)
00163                         {
00164                                 k = op.run(context);    
00165                         }*/
00166                         
00167                         r = ((NLAISCRIPT::ICodeBranche *)opPtr)->run(context);
00168 
00169                         // If we are in Debug Mode
00170                         if (context.ContextDebug.Active)
00171                         {
00172                                 context.ContextDebug.callStackPop();
00173                         }
00174                         
00175                         *context.Code = ip;
00176                         context.Code = opTmp;
00177                 }
00178                 else
00179                 {
00180                         throw NLAIE::CExceptionUnReference("CMessageScript::runMethodeMember(sint32 , sint32, IObjectIA *) because code is null");
00181                 }
00182                 /*IObjectIA::CProcessResult r;
00183                 r.Result = NULL;
00184                 r.ResultState = k;*/
00185                 return r;
00186         }
00187 
00188         IObjectIA::CProcessResult CMessageScript::runMethodeMember(sint32 index,IObjectIA *c)
00189         {
00190                 if(c->getType() != NLAISCRIPT::CCodeContext::IdCodeContext)
00191                 {
00192                         return IMessageBase::runMethodeMember(index, c);
00193                 }
00194 
00195                 NLAISCRIPT::IOpCode *opPtr = NULL;
00196                 NLAISCRIPT::CCodeContext &context = (NLAISCRIPT::CCodeContext &)*c;
00197 
00198                 sint32 i = index - getBaseMethodCount();
00199                 if(i < 0)
00200                 {
00201                         /*if (context.ContextDebug.Active)
00202                         {
00203                                 context.ContextDebug.Param.push_back(&listBidon);
00204                                 listBidon.incRef();
00205                         }*/
00206 
00207                         IObjectIA::CProcessResult r = IMessageBase::runMethodeMember(index,(IObjectIA *)context.Param.back());
00208                         if(r.Result != NULL)
00209                         {
00210                                 context.Stack++;
00211                                 context.Stack[(int)context.Stack] = r.Result;
00212                         }
00213                         r.Result = NULL;
00214                         return r;
00215                 }
00216                 else
00217                 {
00218                         opPtr = getMethode(i);
00219                 }
00220 
00221                 IObjectIA::CProcessResult r;
00222 
00223                 if(opPtr)
00224                 {
00225                         NLAISCRIPT::IOpCode &op = *opPtr;
00226                         NLAISCRIPT::CCodeBrancheRun *opTmp = context.Code;
00227                         sint32 ip = (uint32)*context.Code;
00228                         context.Code = (NLAISCRIPT::CCodeBrancheRun *)&op;              
00229                         *context.Code = 0;
00230 
00231                         /*:TProcessStatement k = IObjectIA::ProcessIdle;
00232 
00233                         while(k != IObjectIA::ProcessEnd)
00234                         {
00235                                 k = op.run(context);    
00236                         }*/             
00237 
00238                         r = ((NLAISCRIPT::ICodeBranche *)opPtr)->run(context);
00239 
00240                         // If we are in Debug Mode
00241                         if (context.ContextDebug.Active)
00242                         {
00243                                 context.ContextDebug.callStackPop();
00244                         }
00245 
00246                         *context.Code = ip;
00247                         context.Code = opTmp;
00248                 }
00249                 else
00250                 {
00251                         throw NLAIE::CExceptionUnReference("CMessageScript::runMethodeMember(sint32, IObjectIA *) because code is null");
00252                 }
00253                 /*IObjectIA::CProcessResult r;
00254                 r.Result = NULL;
00255                 r.ResultState = k;*/
00256                 return r;
00257         }       
00258 
00259         sint32 CMessageScript::isClassInheritedFrom(const IVarName &class_name) const
00260         {
00261                 return _MessageClass->isClassInheritedFrom( class_name );
00262         }       
00263 
00264         bool CMessageScript::setStaticMember(sint32 index,IObjectIA *op)
00265         {
00266 #ifdef NL_DEBUG
00267                 if ( index >= size() )
00268                 {
00269                         throw NLAIE::CExceptionIndexError();
00270                 }
00271 #endif
00272                 CVectorGroupType *a = (CVectorGroupType *)getMessageGroup();
00273                 IObjectIA *b = (IObjectIA *)(*a)[ index ];
00274                 a->setObject(index, op);
00275                 if(op != b) b->release();
00276                 else return true;
00277                 return false;
00278         }
00279 
00280         const IObjectIA *CMessageScript::getStaticMember(sint32 index) const
00281         {
00282 #ifdef NL_DEBUG
00283                 if ( index >= size() )
00284                 {
00285                         throw NLAIE::CExceptionIndexError();
00286                 }
00287 #endif
00288                 return (*this)[ index ];                
00289         }
00290 
00291         sint32 CMessageScript::getStaticMemberIndex(const IVarName &name) const
00292         {
00293                 return _MessageClass->getStaticMemberIndex(name);
00294         }
00295 
00296         sint32 CMessageScript::getStaticMemberSize() const
00297         {
00298                 return 0;
00299         }
00300 
00301         tQueue CMessageScript::isMember(const IVarName *className,const IVarName *methodName,const IObjectIA &param) const
00302         {       
00303 
00304                 tQueue result = IMessageBase::isMember(className, methodName, param);
00305                 
00306                 if ( result.size()  )
00307                 {
00308                         return result;
00309                 }
00310 
00311                 if( *methodName == CStringVarName("send") )
00312                 {
00313                         tQueue r;
00314                         NLAISCRIPT::COperandVoid typeR;
00315                         NLAISCRIPT::CObjectUnknown *t = new NLAISCRIPT::CObjectUnknown((NLAISCRIPT::IOpType *)typeR.clone());                   
00316                         r.push(CIdMethod(0,0.0,NULL,t));
00317                         return r;
00318                 }
00319 
00320                 return result;
00321         }
00322 
00323         void CMessageScript::save(NLMISC::IStream &os)
00324         {
00325                 IMessageBase::save(os);
00326         }
00327 
00328         void CMessageScript::load(NLMISC::IStream &is)
00329         {
00330                 IMessageBase::load(is);
00331         }
00332 
00333         const NLAIC::IBasicType *CMessageScript::clone() const
00334         {               
00335                 CMessageScript *cl = new CMessageScript( *this );
00336 
00337                 return cl;
00338         }
00339 
00340         const NLAIC::IBasicType *CMessageScript::newInstance() const
00341         {
00342                 CMessageScript *instance;
00343                 if ( _MessageClass )
00344                 {
00345                         instance = (CMessageScript *) _MessageClass->buildNewInstance();
00346                 }
00347                 else 
00348                 {
00349                         instance = new CMessageScript();
00350                 }
00351                 return instance;
00352         }
00353 
00354         void CMessageScript::getDebugString(std::string &t) const
00355         {
00356                 if ( _MessageClass )
00357                         t += NLAIC::stringGetBuild("<%s> (scripted)\n   -StaticComponents:\n",(const char *)_MessageClass->getType());
00358                 else
00359                         t += NLAIC::stringGetBuild("<undefined_class> (scripted)\n   -StaticComponents:\n");
00360                 
00361                 for (sint32 i = 0; i < size(); i++ )
00362                 {
00363                         std::string buf;
00364                         t += "     ";
00365                         if ( _MessageClass->getComponent(i) )
00366                         {
00367                                 t += _MessageClass->getComponentName(i);
00368                         }
00369                         else
00370                                 t += "<unnamed>";
00371 
00372                         t += "     ";
00373                         (*this)[i]->getDebugString(buf);
00374                         t += buf;
00375                         t += "\n";
00376 
00377                 }
00378         }
00379 
00380         bool CMessageScript::isEqual(const IBasicObjectIA &a) const
00381         {
00382                 return IMessageBase::isEqual(a);
00383         }
00384 
00385         const NLAIC::CIdentType &CMessageScript::getType() const
00386         {
00387                 if ( _MessageClass ) 
00388                         return _MessageClass->getType();
00389                 else
00390                         return IdMessageScript;
00391         }       
00392 
00393         const IObjectIA::CProcessResult &CMessageScript::run()
00394         {
00395                 return IObjectIA::ProcessRun;
00396         }
00397 }