model.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by the FIFE team                              *
00003  *   http://www.fifengine.net                                              *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "util/structures/purge.h"
00031 #include "util/log/logger.h"
00032 #include "model/metamodel/ipather.h"
00033 #include "model/metamodel/object.h"
00034 #include "model/metamodel/grids/cellgrid.h"
00035 #include "structures/map.h"
00036 #include "structures/layer.h"
00037 #include "structures/instance.h"
00038 #include "util/base/exception.h"
00039 #include "view/rendererbase.h"
00040 #include "video/renderbackend.h"
00041 
00042 #include "model.h"
00043 
00044 namespace FIFE {
00045     static Logger _log(LM_MODEL);
00046 
00047     Model::Model(RenderBackend* renderbackend, const std::vector<RendererBase*>& renderers)
00048     :   FifeClass(),
00049         m_last_namespace(NULL),
00050         m_timeprovider(NULL),
00051         m_renderbackend(renderbackend),
00052         m_renderers(renderers){
00053     }
00054 
00055     Model::~Model() {
00056         purge(m_maps);
00057         for(std::list<namespace_t>::iterator nspace = m_namespaces.begin(); nspace != m_namespaces.end(); ++nspace)
00058             purge_map(nspace->second);
00059         purge(m_pathers);
00060         purge(m_created_grids);
00061         purge(m_adopted_grids);
00062     }
00063 
00064     Map* Model::createMap(const std::string& identifier) {
00065         std::list<Map*>::const_iterator it = m_maps.begin();
00066         for(; it != m_maps.end(); ++it) {
00067             if(identifier == (*it)->getId()) {
00068                 throw NameClash(identifier);
00069             }
00070         }
00071 
00072         Map* map = new Map(identifier, m_renderbackend, m_renderers, &m_timeprovider);
00073         m_maps.push_back(map);
00074         return map;
00075     }
00076 
00077     void Model::adoptPather(IPather* pather) {
00078         m_pathers.push_back(pather);
00079     }
00080 
00081     IPather* Model::getPather(const std::string& pathername) {
00082         std::vector<IPather*>::const_iterator it = m_pathers.begin();
00083         for(; it != m_pathers.end(); ++it) {
00084             if ((*it)->getName() == pathername) {
00085                 return *it;
00086             }
00087         }
00088         FL_WARN(_log, "No pather of requested type \"" + pathername + "\" found.");
00089         return NULL;
00090     }
00091 
00092     void Model::adoptCellGrid(CellGrid* grid) {
00093         m_adopted_grids.push_back(grid);
00094     }
00095 
00096     CellGrid* Model::getCellGrid(const std::string& gridtype) {
00097         std::vector<CellGrid*>::const_iterator it = m_adopted_grids.begin();
00098         for(; it != m_adopted_grids.end(); ++it) {
00099             if ((*it)->getType() == gridtype) {
00100                 CellGrid* newcg = (*it)->clone();
00101                 m_created_grids.push_back(newcg);
00102                 return newcg;
00103             }
00104         }
00105         FL_WARN(_log, "No cellgrid of requested type \"" + gridtype + "\" found.");
00106         return NULL;
00107     }
00108 
00109 
00110     Map* Model::getMap(const std::string& identifier) const {
00111         std::list<Map*>::const_iterator it = m_maps.begin();
00112         for(; it != m_maps.end(); ++it) {
00113             if((*it)->getId() == identifier)
00114                 return *it;
00115         }
00116 
00117         throw NotFound(std::string("Tried to get non-existent map: ") + identifier + ".");
00118     }
00119 
00120     void Model::deleteMap(Map* map) {
00121         std::list<Map*>::iterator it = m_maps.begin();
00122         for(; it != m_maps.end(); ++it) {
00123             if(*it == map) {
00124                 delete *it;
00125                 m_maps.erase(it);
00126                 return ;
00127             }
00128         }
00129     }
00130 
00131     uint32_t Model::getMapCount() const {
00132         return m_maps.size();
00133     }
00134 
00135     void Model::deleteMaps() {
00136         purge(m_maps);
00137         m_maps.clear();
00138     }
00139 
00140     std::list<std::string> Model::getNamespaces() const {
00141         std::list<std::string> namespace_list;
00142         std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
00143         for(; nspace != m_namespaces.end(); ++nspace) {
00144             namespace_list.push_back(nspace->first);
00145         }
00146         return namespace_list;
00147     }
00148 
00149     Object* Model::createObject(const std::string& identifier, const std::string& name_space, Object* parent) {
00150         // Find or create namespace
00151         namespace_t* nspace = selectNamespace(name_space);
00152         if(!nspace) {
00153             m_namespaces.push_back(namespace_t(name_space,objectmap_t()));
00154             nspace = selectNamespace(name_space);
00155         }
00156 
00157         // Check for nameclashes
00158         objectmap_t::const_iterator it = nspace->second.find(identifier);
00159         if( it != nspace->second.end() ) {
00160             throw NameClash(identifier);
00161         }
00162 
00163         // Finally insert & create
00164         Object* object = new Object(identifier, name_space, parent);
00165         nspace->second[identifier] = object;
00166         return object;
00167     }
00168 
00169     bool Model::deleteObject(Object* object) {
00170         // WARNING: This code has obviously not been tested (thoroughly).
00171 
00172         // Check if any instances exist. If yes - bail out.
00173         std::list<Layer*>::const_iterator jt;
00174         std::vector<Instance*>::const_iterator kt;
00175         for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
00176             for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
00177                 for(kt = (*jt)->getInstances().begin(); kt != (*jt)->getInstances().end(); ++kt) {
00178                     Object* o = (*kt)->getObject();
00179                     if(o == object) {
00180                         return false;
00181                     }
00182                 }
00183             }
00184         }
00185 
00186         // Check if the namespace exists
00187         namespace_t* nspace = selectNamespace(object->getNamespace());
00188         if(!nspace)
00189             return true;
00190 
00191         // If yes - delete+erase object.
00192         objectmap_t::iterator it = nspace->second.find(object->getId());
00193         if( it != nspace->second.end()) {
00194             delete it->second;
00195             nspace->second.erase(it);
00196         }
00197 
00198         return true;
00199     }
00200 
00201     bool Model::deleteObjects() {
00202         // If we have layers with instances - bail out.
00203         std::list<Layer*>::const_iterator jt;
00204         for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
00205             for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
00206                 if((*jt)->hasInstances())
00207                     return false;
00208             }
00209         }
00210 
00211         // Otherwise delete every object in every namespace
00212         std::list<namespace_t>::iterator nspace = m_namespaces.begin();
00213         while(nspace != m_namespaces.end()) {
00214             objectmap_t::iterator it = nspace->second.begin();
00215             for(; it != nspace->second.end(); ++it) {
00216                 delete it->second;
00217             }
00218             nspace = m_namespaces.erase(nspace);
00219         }
00220         m_last_namespace = 0;
00221         return true;
00222     }
00223 
00224     Object* Model::getObject(const std::string& id, const std::string& name_space) {
00225         namespace_t* nspace = selectNamespace(name_space);
00226         if(nspace) {
00227             objectmap_t::iterator it = nspace->second.find(id);
00228             if( it !=  nspace->second.end() )
00229                 return it->second;
00230         }
00231         return NULL;
00232     }
00233 
00234     std::list<Object*> Model::getObjects(const std::string& name_space) const {
00235         std::list<Object*> object_list;
00236         const namespace_t* nspace = selectNamespace(name_space);
00237         if(nspace) {
00238             objectmap_t::const_iterator it = nspace->second.begin();
00239             for(; it != nspace->second.end(); ++it )
00240                 object_list.push_back(it->second);
00241         }
00242 
00243         return object_list;
00244     }
00245 
00246     const Model::namespace_t* Model::selectNamespace(const std::string& name_space) const {
00247         std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
00248         for(; nspace != m_namespaces.end(); ++nspace) {
00249             if( nspace->first == name_space ) {
00250                 return &(*nspace);
00251             }
00252         }
00253 
00254         return NULL;
00255     }
00256 
00257     Model::namespace_t* Model::selectNamespace(const std::string& name_space) {
00258         if( m_last_namespace && m_last_namespace->first == name_space )
00259             return m_last_namespace;
00260         std::list<namespace_t>::iterator nspace = m_namespaces.begin();
00261         for(; nspace != m_namespaces.end(); ++nspace) {
00262             if( nspace->first == name_space ) {
00263                 m_last_namespace = &(*nspace);
00264                 return m_last_namespace;
00265             }
00266         }
00267         m_last_namespace = 0;
00268         return NULL;
00269     }
00270 
00271     void Model::update() {
00272         std::list<Map*>::iterator it = m_maps.begin();
00273         for(; it != m_maps.end(); ++it) {
00274             (*it)->update();
00275         }
00276         std::vector<IPather*>::iterator jt = m_pathers.begin();
00277         for(; jt != m_pathers.end(); ++jt) {
00278             (*jt)->update();
00279         }
00280     }
00281 
00282 } //FIFE
00283