CRmgTemplate.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "CRmgTemplateZone.h"
  13. #include "../mapping/CMap.h"
  14. CRmgTemplateZoneConnection::CRmgTemplateZoneConnection() : zoneA(nullptr), zoneB(nullptr), guardStrength(0)
  15. {
  16. }
  17. CRmgTemplateZone * CRmgTemplateZoneConnection::getZoneA() const
  18. {
  19. return zoneA;
  20. }
  21. void CRmgTemplateZoneConnection::setZoneA(CRmgTemplateZone * value)
  22. {
  23. zoneA = value;
  24. }
  25. CRmgTemplateZone * CRmgTemplateZoneConnection::getZoneB() const
  26. {
  27. return zoneB;
  28. }
  29. void CRmgTemplateZoneConnection::setZoneB(CRmgTemplateZone * value)
  30. {
  31. zoneB = value;
  32. }
  33. int CRmgTemplateZoneConnection::getGuardStrength() const
  34. {
  35. return guardStrength;
  36. }
  37. void CRmgTemplateZoneConnection::setGuardStrength(int value)
  38. {
  39. if(value < 0) throw std::runtime_error("Negative value for guard strength not allowed.");
  40. guardStrength = value;
  41. }
  42. CRmgTemplate::CSize::CSize() : width(CMapHeader::MAP_SIZE_MIDDLE), height(CMapHeader::MAP_SIZE_MIDDLE), under(true)
  43. {
  44. }
  45. CRmgTemplate::CSize::CSize(int width, int height, bool under) : under(under)
  46. {
  47. setWidth(width);
  48. setHeight(height);
  49. }
  50. int CRmgTemplate::CSize::getWidth() const
  51. {
  52. return width;
  53. }
  54. void CRmgTemplate::CSize::setWidth(int value)
  55. {
  56. if(value <= 0) throw std::runtime_error("Width > 0 failed.");
  57. width = value;
  58. }
  59. int CRmgTemplate::CSize::getHeight() const
  60. {
  61. return height;
  62. }
  63. void CRmgTemplate::CSize::setHeight(int value)
  64. {
  65. if(value <= 0) throw std::runtime_error("Height > 0 failed.");
  66. height = value;
  67. }
  68. bool CRmgTemplate::CSize::getUnder() const
  69. {
  70. return under;
  71. }
  72. void CRmgTemplate::CSize::setUnder(bool value)
  73. {
  74. under = value;
  75. }
  76. bool CRmgTemplate::CSize::operator<=(const CSize & value) const
  77. {
  78. if(width < value.width && height < value.height)
  79. {
  80. return true;
  81. }
  82. else if(width == value.width && height == value.height)
  83. {
  84. return under ? value.under : true;
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90. }
  91. bool CRmgTemplate::CSize::operator>=(const CSize & value) const
  92. {
  93. if(width > value.width && height > value.height)
  94. {
  95. return true;
  96. }
  97. else if(width == value.width && height == value.height)
  98. {
  99. return under ? true : !value.under;
  100. }
  101. else
  102. {
  103. return false;
  104. }
  105. }
  106. CRmgTemplate::CRmgTemplate()
  107. {
  108. }
  109. CRmgTemplate::~CRmgTemplate()
  110. {
  111. for (auto & pair : zones) delete pair.second;
  112. }
  113. const std::string & CRmgTemplate::getName() const
  114. {
  115. return name;
  116. }
  117. void CRmgTemplate::setName(const std::string & value)
  118. {
  119. name = value;
  120. }
  121. const CRmgTemplate::CSize & CRmgTemplate::getMinSize() const
  122. {
  123. return minSize;
  124. }
  125. void CRmgTemplate::setMinSize(const CSize & value)
  126. {
  127. minSize = value;
  128. }
  129. const CRmgTemplate::CSize & CRmgTemplate::getMaxSize() const
  130. {
  131. return maxSize;
  132. }
  133. void CRmgTemplate::setMaxSize(const CSize & value)
  134. {
  135. maxSize = value;
  136. }
  137. const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getPlayers() const
  138. {
  139. return players;
  140. }
  141. void CRmgTemplate::setPlayers(const CPlayerCountRange & value)
  142. {
  143. players = value;
  144. }
  145. const CRmgTemplate::CPlayerCountRange & CRmgTemplate::getCpuPlayers() const
  146. {
  147. return cpuPlayers;
  148. }
  149. void CRmgTemplate::setCpuPlayers(const CPlayerCountRange & value)
  150. {
  151. cpuPlayers = value;
  152. }
  153. const std::map<TRmgTemplateZoneId, CRmgTemplateZone *> & CRmgTemplate::getZones() const
  154. {
  155. return zones;
  156. }
  157. void CRmgTemplate::setZones(const std::map<TRmgTemplateZoneId, CRmgTemplateZone *> & value)
  158. {
  159. zones = value;
  160. }
  161. const std::list<CRmgTemplateZoneConnection> & CRmgTemplate::getConnections() const
  162. {
  163. return connections;
  164. }
  165. void CRmgTemplate::setConnections(const std::list<CRmgTemplateZoneConnection> & value)
  166. {
  167. connections = value;
  168. }
  169. void CRmgTemplate::validate() const
  170. {
  171. //TODO add some validation checks, throw on failure
  172. }
  173. void CRmgTemplate::CPlayerCountRange::addRange(int lower, int upper)
  174. {
  175. range.push_back(std::make_pair(lower, upper));
  176. }
  177. void CRmgTemplate::CPlayerCountRange::addNumber(int value)
  178. {
  179. range.push_back(std::make_pair(value, value));
  180. }
  181. bool CRmgTemplate::CPlayerCountRange::isInRange(int count) const
  182. {
  183. for(const auto & pair : range)
  184. {
  185. if(count >= pair.first && count <= pair.second) return true;
  186. }
  187. return false;
  188. }
  189. std::set<int> CRmgTemplate::CPlayerCountRange::getNumbers() const
  190. {
  191. std::set<int> numbers;
  192. for(const auto & pair : range)
  193. {
  194. for(int i = pair.first; i <= pair.second; ++i) numbers.insert(i);
  195. }
  196. return numbers;
  197. }