1 /*
2 * Copyright 2010-2014 OpenXcom Developers.
3 *
4 * This file is part of OpenXcom.
5 *
6 * OpenXcom 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 3 of the License, or
9 * (at your option) any later version.
10 *
11 * OpenXcom 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
17 * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "RuleTerrain.h"
21 #include "MapBlock.h"
22 #include "MapDataSet.h"
23 #include "../Engine/RNG.h"
24 #include "Ruleset.h"
25
26 namespace OpenXcom
27 {
28
29 /**
30 * RuleTerrain construction.
31 */
RuleTerrain(const std::string & name)32 RuleTerrain::RuleTerrain(const std::string &name) : _name(name), _largeBlockLimit(0), _hemisphere(0)
33 {
34 }
35
36 /**
37 * Ruleterrain only holds mapblocks. Map datafiles are referenced.
38 */
~RuleTerrain()39 RuleTerrain::~RuleTerrain()
40 {
41 for (std::vector<MapBlock*>::iterator i = _mapBlocks.begin(); i != _mapBlocks.end(); ++i)
42 {
43 delete *i;
44 }
45 }
46
47 /**
48 * Loads the terrain from a YAML file.
49 * @param node YAML node.
50 * @param ruleset Ruleset for the terrain.
51 */
load(const YAML::Node & node,Ruleset * ruleset)52 void RuleTerrain::load(const YAML::Node &node, Ruleset *ruleset)
53 {
54 if (const YAML::Node &map = node["mapDataSets"])
55 {
56 _mapDataSets.clear();
57 for (YAML::const_iterator i = map.begin(); i != map.end(); ++i)
58 {
59 _mapDataSets.push_back(ruleset->getMapDataSet(i->as<std::string>()));
60 }
61 }
62 if (const YAML::Node &map = node["mapBlocks"])
63 {
64 _mapBlocks.clear();
65 for (YAML::const_iterator i = map.begin(); i != map.end(); ++i)
66 {
67 MapBlock *map = new MapBlock((*i)["name"].as<std::string>(), 0, 0, MT_DEFAULT);
68 map->load(*i);
69 _mapBlocks.push_back(map);
70 }
71 }
72 _name = node["name"].as<std::string>(_name);
73 _largeBlockLimit = node["largeBlockLimit"].as<int>(_largeBlockLimit);
74 _textures = node["textures"].as< std::vector<int> >(_textures);
75 _hemisphere = node["hemisphere"].as<int>(_hemisphere);
76 _roadTypeOdds = node["roadTypeOdds"].as< std::vector<int> >(_roadTypeOdds);
77
78 if (const YAML::Node &civs = node["civilianTypes"])
79 {
80 _civilianTypes = civs.as<std::vector<std::string> >(_civilianTypes);
81 }
82 else
83 {
84 _civilianTypes.push_back("MALE_CIVILIAN");
85 _civilianTypes.push_back("FEMALE_CIVILIAN");
86 }
87 }
88
89 /**
90 * Gets the array of mapblocks.
91 * @return Pointer to the array of mapblocks.
92 */
getMapBlocks()93 std::vector<MapBlock*> *RuleTerrain::getMapBlocks()
94 {
95 return &_mapBlocks;
96 }
97
98 /**
99 * Gets the array of mapdatafiles.
100 * @return Pointer to the array of mapdatafiles.
101 */
getMapDataSets()102 std::vector<MapDataSet*> *RuleTerrain::getMapDataSets()
103 {
104 return &_mapDataSets;
105 }
106
107 /**
108 * Gets the terrain name.
109 * @return The terrain name.
110 */
getName() const111 std::string RuleTerrain::getName() const
112 {
113 return _name;
114 }
115
116 /**
117 * Gets a random mapblock within the given constraints.
118 * @param maxsize The maximum size of the mapblock (10 or 20 or 999 - don't care).
119 * @param type Whether this must be a block of a certain type.
120 * @param force Whether to enforce the max size.
121 * @return Pointer to the mapblock.
122 */
getRandomMapBlock(int maxsize,MapBlockType type,bool force)123 MapBlock* RuleTerrain::getRandomMapBlock(int maxsize, MapBlockType type, bool force)
124 {
125 std::vector<MapBlock*> compliantMapBlocks;
126
127 for (std::vector<MapBlock*>::const_iterator i = _mapBlocks.begin(); i != _mapBlocks.end(); ++i)
128 {
129 if (((force && (*i)->getSizeX() == maxsize) ||
130 (!force && (*i)->getSizeX() <= maxsize)) &&
131 ((*i)->getType() == type || (*i)->getSubType() == type))
132 {
133 for (int j = 0; j != (*i)->getRemainingUses(); ++j)
134 {
135 compliantMapBlocks.push_back((*i));
136 }
137 }
138 }
139
140 if (compliantMapBlocks.empty()) return 0;
141
142 size_t n = RNG::generate(0, compliantMapBlocks.size() - 1);
143
144 if (type == MT_DEFAULT)
145 compliantMapBlocks[n]->markUsed();
146
147 return compliantMapBlocks[n];
148 }
149
150 /**
151 * Gets a mapblock with a given name.
152 * @param name The name of the mapblock.
153 * @return Pointer to mapblock.
154 */
getMapBlock(const std::string & name)155 MapBlock* RuleTerrain::getMapBlock(const std::string &name)
156 {
157 for (std::vector<MapBlock*>::const_iterator i = _mapBlocks.begin(); i != _mapBlocks.end(); ++i)
158 {
159 if((*i)->getName() == name)
160 return (*i);
161 }
162 return 0;
163 }
164
165 /**
166 * Gets a mapdata object.
167 * @param id The id in the terrain.
168 * @param mapDataSetID The id of the map data set.
169 * @return Pointer to MapData object.
170 */
getMapData(int * id,int * mapDataSetID) const171 MapData *RuleTerrain::getMapData(int *id, int *mapDataSetID) const
172 {
173 MapDataSet* mdf = 0;
174
175 for (std::vector<MapDataSet*>::const_iterator i = _mapDataSets.begin(); i != _mapDataSets.end(); ++i)
176 {
177 mdf = *i;
178 if (*id < mdf->getSize())
179 {
180 break;
181 }
182 *id -= mdf->getSize();
183 (*mapDataSetID)++;
184 }
185
186 return mdf->getObjects()->at(*id);
187 }
188
189 /**
190 * Gets the maximum amount of large blocks in this terrain.
191 * @return The maximum amount.
192 */
getLargeBlockLimit() const193 int RuleTerrain::getLargeBlockLimit() const
194 {
195 return _largeBlockLimit;
196 }
197
198 /**
199 * Resets the remaining uses of each mapblock.
200 */
resetMapBlocks()201 void RuleTerrain::resetMapBlocks()
202 {
203 for (std::vector<MapBlock*>::const_iterator i = _mapBlocks.begin(); i != _mapBlocks.end(); ++i)
204 {
205 (*i)->reset();
206 }
207 }
208
209 /**
210 * Gets the array of globe texture IDs this terrain is loaded on.
211 * @return Pointer to the array of texture IDs.
212 */
getTextures()213 std::vector<int> *RuleTerrain::getTextures()
214 {
215 return &_textures;
216 }
217
218 /**
219 * Gets the hemishpere this terrain occurs on.
220 * -1 = northern, 0 = either, 1 = southern.
221 * @return The hemisphere.
222 */
getHemisphere() const223 int RuleTerrain::getHemisphere() const
224 {
225 return _hemisphere;
226 }
227
228 /**
229 * Gets the list of civilian types to use on this terrain (default MALE_CIVILIAN and FEMALE_CIVILIAN)
230 * @return list of civilian types to use.
231 */
getCivilianTypes() const232 std::vector<std::string> RuleTerrain::getCivilianTypes() const
233 {
234 return _civilianTypes;
235 }
236
237 /**
238 * Gets the road type odds.
239 * @return The road type odds.
240 */
getRoadTypeOdds() const241 std::vector<int> RuleTerrain::getRoadTypeOdds() const
242 {
243 return _roadTypeOdds;
244 }
245 }
246