CRmgTemplate.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * CRmgTemplate.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CRmgTemplate.h"
  12. #include "../mapping/CMap.h"
  13. #include "../VCMI_Lib.h"
  14. #include "../CTownHandler.h"
  15. namespace rmg
  16. {
  17. const std::set<ETerrainType> ZoneOptions::DEFAULT_TERRAIN_TYPES =
  18. {
  19. ETerrainType::DIRT,
  20. ETerrainType::SAND,
  21. ETerrainType::GRASS,
  22. ETerrainType::SNOW,
  23. ETerrainType::SWAMP,
  24. ETerrainType::ROUGH,
  25. ETerrainType::SUBTERRANEAN,
  26. ETerrainType::LAVA
  27. };
  28. ZoneOptions::CTownInfo::CTownInfo()
  29. : townCount(0),
  30. castleCount(0),
  31. townDensity(0),
  32. castleDensity(0)
  33. {
  34. }
  35. int ZoneOptions::CTownInfo::getTownCount() const
  36. {
  37. return townCount;
  38. }
  39. void ZoneOptions::CTownInfo::setTownCount(int value)
  40. {
  41. if(value < 0)
  42. throw std::runtime_error("Negative value for town count not allowed.");
  43. townCount = value;
  44. }
  45. int ZoneOptions::CTownInfo::getCastleCount() const
  46. {
  47. return castleCount;
  48. }
  49. void ZoneOptions::CTownInfo::setCastleCount(int value)
  50. {
  51. if(value < 0)
  52. throw std::runtime_error("Negative value for castle count not allowed.");
  53. castleCount = value;
  54. }
  55. int ZoneOptions::CTownInfo::getTownDensity() const
  56. {
  57. return townDensity;
  58. }
  59. void ZoneOptions::CTownInfo::setTownDensity(int value)
  60. {
  61. if(value < 0)
  62. throw std::runtime_error("Negative value for town density not allowed.");
  63. townDensity = value;
  64. }
  65. int ZoneOptions::CTownInfo::getCastleDensity() const
  66. {
  67. return castleDensity;
  68. }
  69. void ZoneOptions::CTownInfo::setCastleDensity(int value)
  70. {
  71. if(value < 0)
  72. throw std::runtime_error("Negative value for castle density not allowed.");
  73. castleDensity = value;
  74. }
  75. ZoneOptions::ZoneOptions()
  76. : id(0),
  77. type(ETemplateZoneType::PLAYER_START),
  78. size(1),
  79. owner(boost::none),
  80. playerTowns(),
  81. neutralTowns(),
  82. matchTerrainToTown(true),
  83. terrainTypes(DEFAULT_TERRAIN_TYPES),
  84. townsAreSameType(false),
  85. townTypes(),
  86. monsterTypes(),
  87. zoneMonsterStrength(EMonsterStrength::ZONE_NORMAL),
  88. mines(),
  89. treasureInfo(),
  90. connections()
  91. {
  92. }
  93. ZoneOptions & ZoneOptions::operator=(const ZoneOptions & other)
  94. {
  95. id = other.id;
  96. type = other.type;
  97. size = other.size;
  98. owner = other.owner;
  99. playerTowns = other.playerTowns;
  100. neutralTowns = other.neutralTowns;
  101. matchTerrainToTown = other.matchTerrainToTown;
  102. terrainTypes = other.terrainTypes;
  103. townsAreSameType = other.townsAreSameType;
  104. townTypes = other.townTypes;
  105. monsterTypes = other.monsterTypes;
  106. zoneMonsterStrength = other.zoneMonsterStrength;
  107. mines = other.mines;
  108. treasureInfo = other.treasureInfo;
  109. connections = other.connections;
  110. return *this;
  111. }
  112. TRmgTemplateZoneId ZoneOptions::getId() const
  113. {
  114. return id;
  115. }
  116. void ZoneOptions::setId(TRmgTemplateZoneId value)
  117. {
  118. if(value <= 0)
  119. throw std::runtime_error(boost::to_string(boost::format("Zone %d id should be greater than 0.") % id));
  120. id = value;
  121. }
  122. ETemplateZoneType::ETemplateZoneType ZoneOptions::getType() const
  123. {
  124. return type;
  125. }
  126. void ZoneOptions::setType(ETemplateZoneType::ETemplateZoneType value)
  127. {
  128. type = value;
  129. }
  130. int ZoneOptions::getSize() const
  131. {
  132. return size;
  133. }
  134. void ZoneOptions::setSize(int value)
  135. {
  136. if(value <= 0)
  137. throw std::runtime_error(boost::to_string(boost::format("Zone %d size needs to be greater than 0.") % id));
  138. size = value;
  139. }
  140. boost::optional<int> ZoneOptions::getOwner() const
  141. {
  142. return owner;
  143. }
  144. void ZoneOptions::setOwner(boost::optional<int> value)
  145. {
  146. if(value && !(*value >= 0 && *value <= PlayerColor::PLAYER_LIMIT_I))
  147. throw std::runtime_error(boost::to_string(boost::format ("Owner of zone %d has to be in range 0 to max player count.") % id));
  148. owner = value;
  149. }
  150. const ZoneOptions::CTownInfo & ZoneOptions::getPlayerTowns() const
  151. {
  152. return playerTowns;
  153. }
  154. void ZoneOptions::setPlayerTowns(const CTownInfo & value)
  155. {
  156. playerTowns = value;
  157. }
  158. const ZoneOptions::CTownInfo & ZoneOptions::getNeutralTowns() const
  159. {
  160. return neutralTowns;
  161. }
  162. void ZoneOptions::setNeutralTowns(const CTownInfo & value)
  163. {
  164. neutralTowns = value;
  165. }
  166. bool ZoneOptions::getMatchTerrainToTown() const
  167. {
  168. return matchTerrainToTown;
  169. }
  170. void ZoneOptions::setMatchTerrainToTown(bool value)
  171. {
  172. matchTerrainToTown = value;
  173. }
  174. const std::set<ETerrainType> & ZoneOptions::getTerrainTypes() const
  175. {
  176. return terrainTypes;
  177. }
  178. void ZoneOptions::setTerrainTypes(const std::set<ETerrainType> & value)
  179. {
  180. assert(value.find(ETerrainType::WRONG) == value.end() && value.find(ETerrainType::BORDER) == value.end() &&
  181. value.find(ETerrainType::WATER) == value.end() && value.find(ETerrainType::ROCK) == value.end());
  182. terrainTypes = value;
  183. }
  184. bool ZoneOptions::getTownsAreSameType() const
  185. {
  186. return townsAreSameType;
  187. }
  188. void ZoneOptions::setTownsAreSameType(bool value)
  189. {
  190. townsAreSameType = value;
  191. }
  192. std::set<TFaction> ZoneOptions::getDefaultTownTypes() const
  193. {
  194. std::set<TFaction> defaultTowns;
  195. auto towns = VLC->townh->getDefaultAllowed();
  196. for(int i = 0; i < towns.size(); ++i)
  197. {
  198. if(towns[i]) defaultTowns.insert(i);
  199. }
  200. return defaultTowns;
  201. }
  202. const std::set<TFaction> & ZoneOptions::getTownTypes() const
  203. {
  204. return townTypes;
  205. }
  206. void ZoneOptions::setTownTypes(const std::set<TFaction> & value)
  207. {
  208. townTypes = value;
  209. }
  210. void ZoneOptions::setMonsterTypes(const std::set<TFaction> & value)
  211. {
  212. monsterTypes = value;
  213. }
  214. void ZoneOptions::setMonsterStrength(EMonsterStrength::EMonsterStrength val)
  215. {
  216. assert (vstd::iswithin(val, EMonsterStrength::ZONE_WEAK, EMonsterStrength::ZONE_STRONG));
  217. zoneMonsterStrength = val;
  218. }
  219. void ZoneOptions::setMinesAmount(TResource res, ui16 amount)
  220. {
  221. mines[res] = amount;
  222. }
  223. std::map<TResource, ui16> ZoneOptions::getMinesInfo() const
  224. {
  225. return mines;
  226. }
  227. void ZoneOptions::addTreasureInfo(const CTreasureInfo & info)
  228. {
  229. treasureInfo.push_back(info);
  230. }
  231. const std::vector<CTreasureInfo> & ZoneOptions::getTreasureInfo() const
  232. {
  233. return treasureInfo;
  234. }
  235. void ZoneOptions::addConnection(TRmgTemplateZoneId otherZone)
  236. {
  237. connections.push_back (otherZone);
  238. }
  239. ZoneConnection::ZoneConnection()
  240. : zoneA(-1),
  241. zoneB(-1),
  242. guardStrength(0)
  243. {
  244. }
  245. TRmgTemplateZoneId ZoneConnection::getZoneA() const
  246. {
  247. return zoneA;
  248. }
  249. void ZoneConnection::setZoneA(TRmgTemplateZoneId value)
  250. {
  251. zoneA = value;
  252. }
  253. TRmgTemplateZoneId ZoneConnection::getZoneB() const
  254. {
  255. return zoneB;
  256. }
  257. void ZoneConnection::setZoneB(TRmgTemplateZoneId value)
  258. {
  259. zoneB = value;
  260. }
  261. int ZoneConnection::getGuardStrength() const
  262. {
  263. return guardStrength;
  264. }
  265. void ZoneConnection::setGuardStrength(int value)
  266. {
  267. if(value < 0) throw std::runtime_error("Negative value for guard strength not allowed.");
  268. guardStrength = value;
  269. }
  270. }
  271. using namespace rmg;//todo: remove
  272. CRmgTemplate::CSize::CSize() : width(CMapHeader::MAP_SIZE_MIDDLE), height(CMapHeader::MAP_SIZE_MIDDLE), under(true)
  273. {
  274. }
  275. CRmgTemplate::CSize::CSize(int width, int height, bool under) : under(under)
  276. {
  277. setWidth(width);
  278. setHeight(height);
  279. }
  280. int CRmgTemplate::CSize::getWidth() const
  281. {
  282. return width;
  283. }
  284. void CRmgTemplate::CSize::setWidth(int value)
  285. {
  286. if(value <= 0) throw std::runtime_error("Width > 0 failed.");
  287. width = value;
  288. }
  289. int CRmgTemplate::CSize::getHeight() const
  290. {
  291. return height;
  292. }
  293. void CRmgTemplate::CSize::setHeight(int value)
  294. {
  295. if(value <= 0) throw std::runtime_error("Height > 0 failed.");
  296. height = value;
  297. }
  298. bool CRmgTemplate::CSize::getUnder() const
  299. {
  300. return under;
  301. }
  302. void CRmgTemplate::CSize::setUnder(bool value)
  303. {
  304. under = value;
  305. }
  306. bool CRmgTemplate::CSize::operator<=(const CSize & value) const
  307. {
  308. if(width < value.width && height < value.height)
  309. {
  310. return true;
  311. }
  312. else if(width == value.width && height == value.height)
  313. {
  314. return under ? value.under : true;
  315. }
  316. else
  317. {
  318. return false;
  319. }
  320. }
  321. bool CRmgTemplate::CSize::operator>=(const CSize & value) const
  322. {
  323. if(width > value.width && height > value.height)
  324. {
  325. return true;
  326. }
  327. else if(width == value.width && height == value.height)
  328. {
  329. return under ? true : !value.under;
  330. }
  331. else
  332. {
  333. return false;
  334. }
  335. }
  336. CRmgTemplate::CRmgTemplate()
  337. {
  338. }
  339. CRmgTemplate::~CRmgTemplate()
  340. {
  341. for (auto & pair : zones) delete pair.second;
  342. }
  343. const std::string & CRmgTemplate::getName() const
  344. {
  345. return name;
  346. }
  347. void CRmgTemplate::setName(const std::string & value)
  348. {
  349. name = value;
  350. }
  351. const CRmgTemplate::CSize & CRmgTemplate::getMinSize() const
  352. {
  353. return minSize;
  354. }
  355. void CRmgTemplate::setMinSize(const CSize & value)
  356. {
  357. minSize = value;
  358. }
  359. const CRmgTemplate::CSize & CRmgTemplate::getMaxSize() const
  360. {
  361. return maxSize;
  362. }
  363. void CRmgTemplate::setMaxSize(const CSize & value)
  364. {
  365. maxSize = value;
  366. }
  367. const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getPlayers() const
  368. {
  369. return players;
  370. }
  371. void CRmgTemplate::setPlayers(const CPlayerCountRange & value)
  372. {
  373. players = value;
  374. }
  375. const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getCpuPlayers() const
  376. {
  377. return cpuPlayers;
  378. }
  379. void CRmgTemplate::setCpuPlayers(const CPlayerCountRange & value)
  380. {
  381. cpuPlayers = value;
  382. }
  383. const std::map<TRmgTemplateZoneId, ZoneOptions *> & CRmgTemplate::getZones() const
  384. {
  385. return zones;
  386. }
  387. void CRmgTemplate::setZones(const std::map<TRmgTemplateZoneId, ZoneOptions *> & value)
  388. {
  389. zones = value;
  390. }
  391. const std::list<ZoneConnection> & CRmgTemplate::getConnections() const
  392. {
  393. return connections;
  394. }
  395. void CRmgTemplate::setConnections(const std::list<ZoneConnection> & value)
  396. {
  397. connections = value;
  398. }
  399. void CRmgTemplate::validate() const
  400. {
  401. //TODO add some validation checks, throw on failure
  402. }
  403. void CRmgTemplate::CPlayerCountRange::addRange(int lower, int upper)
  404. {
  405. range.push_back(std::make_pair(lower, upper));
  406. }
  407. void CRmgTemplate::CPlayerCountRange::addNumber(int value)
  408. {
  409. range.push_back(std::make_pair(value, value));
  410. }
  411. bool CRmgTemplate::CPlayerCountRange::isInRange(int count) const
  412. {
  413. for(const auto & pair : range)
  414. {
  415. if(count >= pair.first && count <= pair.second) return true;
  416. }
  417. return false;
  418. }
  419. std::set<int> CRmgTemplate::CPlayerCountRange::getNumbers() const
  420. {
  421. std::set<int> numbers;
  422. for(const auto & pair : range)
  423. {
  424. for(int i = pair.first; i <= pair.second; ++i) numbers.insert(i);
  425. }
  426. return numbers;
  427. }