1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <landscapedef/LandscapeDefinitionsBase.h>
22 #include <common/Defines.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <time.h>
26 
readXML(XMLNode * node)27 bool LandscapeDefinitionsEntry::readXML(XMLNode *node)
28 {
29 	if (!node->getNamedChild("name", name)) return false;
30 	if (!node->getNamedChild("weight", weight)) return false;
31 	if (!node->getNamedChild("description", description)) return false;
32 	if (!node->getNamedChild("picture", picture)) return false;
33 
34 	XMLNode *tex, *defn, *tmp;
35 	if (!node->getNamedChild("defn", defn)) return false;
36 	while (defn->getNamedChild("item", tmp, false, true))
37 	{
38 		const char *landscapeDefnFile = tmp->getContent();
39 		defns.push_back(landscapeDefnFile);
40 	}
41 	if (!node->getNamedChild("tex", tex)) return false;
42 	while (tex->getNamedChild("item", tmp, false, true))
43 	{
44 		const char *landscapeTexFile = tmp->getContent();
45 		texs.push_back(landscapeTexFile);
46 	}
47 
48 	DIALOG_ASSERT(!texs.empty() && !defns.empty());
49 	return node->failChildren();
50 }
51 
LandscapeDefinitionsBase()52 LandscapeDefinitionsBase::LandscapeDefinitionsBase()
53 {
54 }
55 
~LandscapeDefinitionsBase()56 LandscapeDefinitionsBase::~LandscapeDefinitionsBase()
57 {
58 }
59 
clearLandscapeDefinitions()60 void LandscapeDefinitionsBase::clearLandscapeDefinitions()
61 {
62 	entries_.clear();
63 }
64 
readLandscapeDefinitions()65 bool LandscapeDefinitionsBase::readLandscapeDefinitions()
66 {
67 	// Load landscape definition file
68 	XMLFile file;
69 	if (!file.readFile(S3D::getModFile("data/landscapes.xml")) ||
70 		!file.getRootNode())
71 	{
72 		S3D::dialogMessage("Scorched Landscape", S3D::formatStringBuffer(
73 					  "Failed to parse \"data/landscapes.xml\"\n%s",
74 					  file.getParserError()));
75 		return false;
76 	}
77 
78 	// Itterate all of the landscapes in the file
79 	std::list<XMLNode *>::iterator childrenItor;
80 		std::list<XMLNode *> &children = file.getRootNode()->getChildren();
81 	for (childrenItor = children.begin();
82 		childrenItor != children.end();
83 		++childrenItor)
84 	{
85 		LandscapeDefinitionsEntry newDefn;
86 		if (!newDefn.readXML(*childrenItor)) return false;
87 		entries_.push_back(newDefn);
88 	}
89 	return true;
90 }
91 
landscapeEnabled(OptionsGame & context,const char * name)92 bool LandscapeDefinitionsBase::landscapeEnabled(OptionsGame &context,
93 											const char *name)
94 {
95 	std::string landscapes = context.getLandscapes();
96 	if (landscapes.empty()) return true; // Default un-initialized state
97 
98 	char *token = strtok((char *) landscapes.c_str(), ":");
99 	while(token != 0)
100 	{
101 		if (0 == strcmp(token, name)) return true;
102 		token = strtok(0, ":");
103 	}
104 	return false;
105 }
106 
getLandscapeByName(const char * name)107 LandscapeDefinitionsEntry *LandscapeDefinitionsBase::getLandscapeByName(
108 	const char *name)
109 {
110 	std::list<LandscapeDefinitionsEntry>::iterator itor;
111 	for (itor = entries_.begin();
112 		itor != entries_.end();
113 		++itor)
114 	{
115 		LandscapeDefinitionsEntry &result = *itor;
116 		if (0 == strcmp(name, result.name.c_str()))
117 		{
118 			return &result;
119 		}
120 	}
121 	return 0;
122 }
123