CRmgTemplate.cpp 4.4 KB

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