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/agent.h" 00025 00026 namespace NLAIAGENT 00027 { 00028 00029 00030 IAgentComposite::IAgentComposite(const IAgentComposite &a): IBasicAgent(a),_SizeChild(0) 00031 { 00032 std::list<IBasicAgent *>::const_iterator i = a._AgentList.begin(); 00033 while(i != a._AgentList.end()) 00034 { 00035 IBasicAgent *b = ( *i++ ); 00036 addChild( b ); 00037 } 00038 } 00039 00040 IAgentComposite::IAgentComposite(IBasicAgent *parent): IBasicAgent(parent != NULL ? (const IWordNumRef *) *parent:NULL),_SizeChild(0) 00041 { 00042 if(parent) parent->addChild(this); 00043 } 00044 00045 IAgentComposite::IAgentComposite(IBasicAgent *parent,IMailBox *m): IBasicAgent(parent != NULL ? (const IWordNumRef *) *parent:NULL,m),_SizeChild(0) 00046 { 00047 if(parent) parent->addChild(this); 00048 } 00049 00050 IAgentComposite::~IAgentComposite() 00051 { 00052 /*std::list<IBasicAgent *>::iterator i = _AgentList.begin(); 00053 while(i != _AgentList.end()) 00054 { 00055 IBasicAgent *b = (*i++); 00056 }*/ 00057 deleteListe(); 00058 } 00059 00060 void IAgentComposite::onKill(IConnectIA *a) 00061 { 00062 //removeChild((const IBasicAgent *)a); 00063 IBasicAgent::onKill(a); 00064 } 00065 00066 // Ajoute un fils à l'agent. 00067 std::list<IBasicAgent *>::iterator IAgentComposite::addChild(IBasicAgent *p) 00068 { 00069 p->incRef(); 00070 _AgentList.push_front((IBasicAgent *)p); 00071 // Donne au fils accès à la boite aux lettres du père 00072 p->getMail()->addMailBox( this->getMail() ); 00073 _SizeChild ++; 00074 00075 return _AgentList.begin(); 00076 } 00077 00078 void IAgentComposite::cpyChild(const IBasicAgent &p) 00079 { 00080 _AgentList.push_front((IBasicAgent *)p.clone()); 00081 _SizeChild ++; 00082 } 00083 00084 void IAgentComposite::removeChild(std::list<IBasicAgent *>::iterator &iter) 00085 { 00086 _AgentList.erase(iter); 00087 _SizeChild --; 00088 } 00089 00090 void IAgentComposite::removeChild(const IBasicAgent &p) 00091 { 00092 std::list<IBasicAgent *>::iterator i = _AgentList.begin(); 00093 while(i != _AgentList.end()) 00094 { 00095 if(*((*i)) == p) 00096 { 00097 (*i)->release(); 00098 _AgentList.erase(i); 00099 // Supprime chez l'ancien fils la boite au lettre du père 00100 p.getMail()->removeMailBox( this->getMail() ); 00101 _SizeChild --; 00102 return; 00103 } 00104 i++; 00105 } 00106 } 00107 00108 void IAgentComposite::removeChild(const IBasicAgent *p) 00109 { 00110 std::list<IBasicAgent *>::iterator i = _AgentList.begin(); 00111 while(i != _AgentList.end()) 00112 { 00113 IBasicAgent *c = *i; 00114 if(c == p) 00115 { 00116 _AgentList.erase(i); 00117 p->getMail()->removeMailBox( this->getMail() ); 00118 c->release(); 00119 _SizeChild --; 00120 // Supprime chez l'ancien fils la boite au lettre du père 00121 break; 00122 } 00123 i++; 00124 } 00125 } 00126 void IAgentComposite::deleteListe() 00127 { 00128 std::list<IBasicAgent *>::iterator i = _AgentList.begin(); 00129 while(i != _AgentList.end()) 00130 { 00131 IBasicAgent *b = (*i++); 00132 b->setParent(NULL); 00133 b->release(); 00134 } 00135 _SizeChild = 0; 00136 } 00137 00138 void IAgentComposite::save(NLMISC::IStream &os) 00139 { 00140 IBasicAgent::save(os); 00141 00142 sint32 size = _AgentList.size(); 00143 os.serial(size); 00144 std::list<IBasicAgent *>::const_iterator i = _AgentList.begin(); 00145 while(i != _AgentList.end()) 00146 { 00147 IBasicAgent &a = *(*i++); 00148 os.serial( (NLAIC::CIdentType &) a.getType() ); 00149 a.save(os); 00150 } 00151 } 00152 00153 void IAgentComposite::load(NLMISC::IStream &is) 00154 { 00155 deleteListe(); 00156 IBasicAgent::load(is); 00157 NLAIC::CIdentTypeAlloc id; 00158 sint32 i; 00159 00160 is.serial(i); 00161 _SizeChild = 0; 00162 while(i--) 00163 { 00164 is.serial( id ); 00165 IBasicAgent &a = *((IBasicAgent *)id.allocClass()); 00166 a.load(is); 00167 addChild(&a); 00168 } 00169 } 00170 }