LandscapeDefinitionsBase.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D 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
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <landscapedef/LandscapeDefinitionsBase.h>
00022 #include <common/Defines.h>
00023 #include <string.h>
00024 #include <stdlib.h>
00025 #include <time.h>
00026 
00027 bool LandscapeDefinitionsEntry::readXML(XMLNode *node)
00028 {
00029         if (!node->getNamedChild("name", name)) return false;
00030         if (!node->getNamedChild("weight", weight)) return false;
00031         if (!node->getNamedChild("description", description)) return false;
00032         if (!node->getNamedChild("picture", picture)) return false;
00033 
00034         XMLNode *tex, *defn, *tmp;
00035         if (!node->getNamedChild("defn", defn)) return false;
00036         while (defn->getNamedChild("item", tmp, false, true))
00037         {
00038                 const char *landscapeDefnFile = tmp->getContent();
00039                 defns.push_back(landscapeDefnFile);
00040         }
00041         if (!node->getNamedChild("tex", tex)) return false;
00042         while (tex->getNamedChild("item", tmp, false, true))
00043         {
00044                 const char *landscapeTexFile = tmp->getContent();
00045                 texs.push_back(landscapeTexFile);
00046         }
00047 
00048         DIALOG_ASSERT(!texs.empty() && !defns.empty());
00049         return node->failChildren();
00050 }
00051 
00052 LandscapeDefinitionsBase::LandscapeDefinitionsBase()
00053 {
00054 }
00055 
00056 LandscapeDefinitionsBase::~LandscapeDefinitionsBase()
00057 {
00058 }
00059 
00060 void LandscapeDefinitionsBase::clearLandscapeDefinitions()
00061 {
00062         entries_.clear();
00063 }
00064 
00065 bool LandscapeDefinitionsBase::readLandscapeDefinitions()
00066 {
00067         // Load landscape definition file
00068         XMLFile file;
00069         if (!file.readFile(S3D::getDataFile("data/landscapes.xml")) ||
00070                 !file.getRootNode())
00071         {
00072                 S3D::dialogMessage("Scorched Landscape", S3D::formatStringBuffer(
00073                                           "Failed to parse \"data/landscapes.xml\"\n%s", 
00074                                           file.getParserError()));
00075                 return false;
00076         }
00077 
00078         // Itterate all of the landscapes in the file
00079         std::list<XMLNode *>::iterator childrenItor;
00080                 std::list<XMLNode *> &children = file.getRootNode()->getChildren();
00081         for (childrenItor = children.begin();
00082                 childrenItor != children.end();
00083                 childrenItor++)
00084         {
00085                 LandscapeDefinitionsEntry newDefn;
00086                 if (!newDefn.readXML(*childrenItor)) return false;
00087                 entries_.push_back(newDefn);
00088         }
00089         return true;
00090 }
00091 
00092 bool LandscapeDefinitionsBase::landscapeEnabled(OptionsGame &context, 
00093                                                                                         const char *name)
00094 {
00095         std::string landscapes = context.getLandscapes();
00096         if (landscapes.empty()) return true; // Default un-initialized state
00097 
00098         char *token = strtok((char *) landscapes.c_str(), ":");
00099         while(token != 0)
00100         {
00101                 if (0 == strcmp(token, name)) return true;
00102                 token = strtok(0, ":");
00103         }
00104         return false;
00105 }
00106 
00107 LandscapeDefinitionsEntry *LandscapeDefinitionsBase::getLandscapeByName(
00108         const char *name)
00109 {
00110         std::list<LandscapeDefinitionsEntry>::iterator itor;
00111         for (itor = entries_.begin();
00112                 itor != entries_.end();
00113                 itor++)
00114         {
00115                 LandscapeDefinitionsEntry &result = *itor;
00116                 if (0 == strcmp(name, result.name.c_str()))
00117                 {
00118                         return &result;
00119                 }
00120         }
00121         return 0;
00122 }

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3