00001 /****************************************************************************** 00002 * Wormux is a convivial mass murder game. 00003 * Copyright (C) 2001-2004 Lawrence Azzoug. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00018 ****************************************************************************** 00019 *****************************************************************************/ 00020 00021 #include "body_list.h" 00022 //----------------------------------------------------------------------------- 00023 #include <string> 00024 #include <iostream> 00025 #include "../game/config.h" // DATADIR 00026 #include "../tool/i18n.h" 00027 #include "../tool/xml_document.h" 00028 00029 const std::string CONFIG_FN = "config.xml"; 00030 //----------------------------------------------------------------------------- 00031 BodyList body_list; 00032 //----------------------------------------------------------------------------- 00033 00034 00035 BodyList::BodyList() 00036 { 00037 } 00038 00039 void BodyList::FreeMem() 00040 { 00041 // The bodies member variable are freed from here, because the playing bodies only contains 00042 // to member/movement/clothes of the bodies in the body_list 00043 00044 std::map<std::string, Body*>::iterator it = list.begin(); 00045 while(it != list.end()) 00046 { 00047 // Clean the movements list 00048 std::map<std::string, Movement*>::iterator it3 = it->second->mvt_lst.begin(); 00049 while(it3 != it->second->mvt_lst.end()) 00050 { 00051 delete it3->second; 00052 it3++; 00053 } 00054 00055 delete it->second; 00056 it++; 00057 } 00058 list.clear(); 00059 } 00060 00061 void BodyList::Load (const std::string &name) 00062 { 00063 std::string fn = Config::GetInstance()->GetDataDir() + PATH_SEPARATOR + "body" + PATH_SEPARATOR + name + PATH_SEPARATOR + CONFIG_FN; 00064 00065 XmlReader doc; 00066 if (!doc.Load(fn)) { 00067 std::cerr << "Unable to find file " << fn << std::endl; 00068 return; 00069 } 00070 00071 Profile *res = resource_manager.LoadXMLProfile( fn, true); 00072 00073 Body* body = new Body(doc.GetRoot(), res); 00074 list[name] = body; 00075 00076 resource_manager.UnLoadXMLProfile( res); 00077 } 00078 00079 Body* BodyList::GetBody(const std::string &name) 00080 { 00081 if(list[name] == NULL) 00082 Load(name); 00083 00084 if(list[name] == NULL) 00085 { 00086 std::cerr << "Unable to load body \"" << name << "\"" << std::endl; 00087 return NULL; 00088 } 00089 return new Body(list[name]); 00090 }