00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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;
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 }