00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "nel/ai/agent/agent.h"
00025 #include "nel/ai/agent/main_agent_script.h"
00026 #include "nel/ai/e/ai_exception.h"
00027
00028 namespace NLAIAGENT
00029 {
00030
00031 const sint StackMemSize = 256;
00032 CMainAgentScript::CMainAgentScript(const CMainAgentScript &a): IMainAgent(a)
00033 {
00034 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00035 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00036
00037 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,a._CodeContext->InputOutput);
00038 }
00039
00040 CMainAgentScript::CMainAgentScript(IAgentManager *main,NLAIC::IIO *io):IMainAgent (main)
00041 {
00042 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00043 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00044 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,io);
00045 }
00046
00047 CMainAgentScript::CMainAgentScript(IAgentManager *a,NLAIC::IIO *io, IBasicAgent *b, std::list<IObjectIA *> &v, NLAISCRIPT::CAgentClass *c):IMainAgent(a,b,v,c)
00048 {
00049 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00050 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00051 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,io);
00052 }
00053
00054 CMainAgentScript::CMainAgentScript(NLAIC::IIO *io):IMainAgent (NULL)
00055 {
00056 _Stack = new NLAISCRIPT::CStackPointer(StackMemSize);
00057 _Heap = new NLAISCRIPT::CStackPointer(StackMemSize);
00058 _CodeContext = new NLAISCRIPT::CCodeContext(*_Stack,*_Heap,NULL,this,io);
00059 }
00060
00061 CMainAgentScript::~CMainAgentScript()
00062 {
00063 Kill();
00064 _CodeContext->release();
00065 delete _Stack;
00066 delete _Heap;
00067 }
00068
00069 const IObjectIA *CMainAgentScript::getAgentContext() const
00070 {
00071 return _CodeContext;
00072 }
00073
00074 int CMainAgentScript::getBaseMethodCount() const
00075 {
00076 return CAgentScript::getBaseMethodCount();
00077 }
00078
00079 IObjectIA::CProcessResult CMainAgentScript::sendMessage(IObjectIA *m)
00080 {
00081 return CAgentScript::sendMessage(m);
00082 }
00083
00084 const NLAIC::IBasicType *CMainAgentScript::clone() const
00085 {
00086 NLAIC::IBasicType *x = new CMainAgentScript(*this);
00087 return x;
00088 }
00089
00090 const NLAIC::IBasicType *CMainAgentScript::newInstance() const
00091 {
00092 NLAIC::IBasicType *x = new CMainAgentScript(_CodeContext->InputOutput);
00093 return x;
00094 }
00095
00096 IMessageBase *CMainAgentScript::runExec(const IMessageBase &m)
00097 {
00098 if(m.getGroup().getId() == 1)
00099 {
00100 _CodeContext->Self = this;
00101
00102 CConstIteratorContener i = m.getConstIterator();
00103 NLAISCRIPT::CCodeBrancheRun *o = (NLAISCRIPT::CCodeBrancheRun *)i++;
00104 NLAISCRIPT::CStackPointer stack;
00105 NLAISCRIPT::CStackPointer heap;
00106 _CodeContext->InputOutput->incRef();
00107 NLAISCRIPT::CCodeContext codeContext(stack,heap,NULL,this,_CodeContext->InputOutput);
00108 codeContext.Code = o;
00109 (void)o->run(codeContext);
00110 heap -= (int)heap;
00111 stack -= (int)stack;
00112 }
00113 return NULL;
00114 }
00115
00116 void CMainAgentScript::processMessages()
00117 {
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 try
00135 {
00136 CAgentScript::processMessages();
00137 }
00138 catch(NLAIE::IException &e)
00139 {
00140 const char *w = e.what();
00141 _CodeContext->InputOutput->Echo("\n\n%s\n\n",(char *)w);
00142 }
00143 }
00144
00145 const IObjectIA::CProcessResult &CMainAgentScript::run()
00146 {
00147 runChildren();
00148
00149
00150 getMail()->run();
00151
00152 if(getFactoryClass() != NULL && getFactoryClass()->getRunMethod() >= 0)
00153 {
00154 runMethodeMember(getFactoryClass()->getRunMethod(),_CodeContext);
00155 }
00156
00157 processMessages();
00158
00159 if(haveActivity() && getState().ResultState == processIdle) runActivity();
00160
00161 return getState();
00162 }
00163
00164 IObjectIA::CProcessResult CMainAgentScript::addDynamicAgent(IBaseGroupType *g)
00165 {
00166 CIteratorContener i = g->getIterator();
00167 i++;
00168 IBasicAgent *o = (IBasicAgent *)i++;
00169
00170 uint b = NLAIC::CTypeOfObject::tInterpret | NLAIC::CTypeOfObject::tAgent;
00171 const NLAIC::CTypeOfObject &t = o->getType();
00172 if((t.getValue() & b) == b)
00173 {
00174 ((CAgentScript *)o)->setAgentManager(this);
00175 }
00176
00177 return CAgentScript::addDynamicAgent(g);
00178 }
00179 }