CRmgTemplateZone.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * CRmgTemplateZone.h, 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. #pragma once
  11. #include "../GameConstants.h"
  12. #include "CMapGenerator.h"
  13. #include "float3.h"
  14. #include "../int3.h"
  15. #include "../ResourceSet.h" //for TResource (?)
  16. #include "../mapObjects/ObjectTemplate.h"
  17. #include <boost/heap/priority_queue.hpp> //A*
  18. class CMapGenerator;
  19. class CTileInfo;
  20. class int3;
  21. class CGObjectInstance;
  22. class ObjectTemplate;
  23. class CRmgTemplateZoneConnection;
  24. namespace ETemplateZoneType
  25. {
  26. enum ETemplateZoneType
  27. {
  28. PLAYER_START,
  29. CPU_START,
  30. TREASURE,
  31. JUNCTION
  32. };
  33. }
  34. namespace EObjectPlacingResult
  35. {
  36. enum EObjectPlacingResult
  37. {
  38. SUCCESS,
  39. CANNOT_FIT,
  40. SEALED_OFF
  41. };
  42. }
  43. class DLL_LINKAGE CTileInfo
  44. {
  45. public:
  46. CTileInfo();
  47. float getNearestObjectDistance() const;
  48. void setNearestObjectDistance(float value);
  49. bool isBlocked() const;
  50. bool shouldBeBlocked() const;
  51. bool isPossible() const;
  52. bool isFree() const;
  53. bool isUsed() const;
  54. bool isRoad() const;
  55. void setOccupied(ETileType::ETileType value);
  56. ETerrainType getTerrainType() const;
  57. ETileType::ETileType getTileType() const;
  58. void setTerrainType(ETerrainType value);
  59. void setRoadType(ERoadType::ERoadType value);
  60. private:
  61. float nearestObjectDistance;
  62. ETileType::ETileType occupied;
  63. ETerrainType terrain;
  64. ERoadType::ERoadType roadType;
  65. };
  66. class DLL_LINKAGE CTreasureInfo
  67. {
  68. public:
  69. ui32 min;
  70. ui32 max;
  71. ui16 density;
  72. };
  73. struct DLL_LINKAGE ObjectInfo
  74. {
  75. ObjectTemplate templ;
  76. ui32 value;
  77. ui16 probability;
  78. ui32 maxPerZone;
  79. //ui32 maxPerMap; //unused
  80. std::function<CGObjectInstance *()> generateObject;
  81. void setTemplate (si32 type, si32 subtype, ETerrainType terrain);
  82. ObjectInfo();
  83. bool operator==(const ObjectInfo& oi) const { return (templ == oi.templ); }
  84. };
  85. struct DLL_LINKAGE CTreasurePileInfo
  86. {
  87. std::set<int3> visitableFromBottomPositions; //can be visited only from bottom or side
  88. std::set<int3> visitableFromTopPositions; //they can be visited from any direction
  89. std::set<int3> blockedPositions;
  90. std::set<int3> occupiedPositions; //blocked + visitable
  91. int3 nextTreasurePos;
  92. };
  93. /// The CRmgTemplateZone describes a zone in a template.
  94. class DLL_LINKAGE CRmgTemplateZone
  95. {
  96. public:
  97. class DLL_LINKAGE CTownInfo
  98. {
  99. public:
  100. CTownInfo();
  101. int getTownCount() const; /// Default: 0
  102. void setTownCount(int value);
  103. int getCastleCount() const; /// Default: 0
  104. void setCastleCount(int value);
  105. int getTownDensity() const; /// Default: 0
  106. void setTownDensity(int value);
  107. int getCastleDensity() const; /// Default: 0
  108. void setCastleDensity(int value);
  109. private:
  110. int townCount, castleCount, townDensity, castleDensity;
  111. };
  112. CRmgTemplateZone();
  113. void setGenPtr(CMapGenerator * Gen);
  114. TRmgTemplateZoneId getId() const; /// Default: 0
  115. void setId(TRmgTemplateZoneId value);
  116. ETemplateZoneType::ETemplateZoneType getType() const; /// Default: ETemplateZoneType::PLAYER_START
  117. void setType(ETemplateZoneType::ETemplateZoneType value);
  118. int getSize() const; /// Default: 1
  119. void setSize(int value);
  120. boost::optional<int> getOwner() const;
  121. void setOwner(boost::optional<int> value);
  122. const CTownInfo & getPlayerTowns() const;
  123. void setPlayerTowns(const CTownInfo & value);
  124. const CTownInfo & getNeutralTowns() const;
  125. void setNeutralTowns(const CTownInfo & value);
  126. bool getTownsAreSameType() const; /// Default: false
  127. void setTownsAreSameType(bool value);
  128. const std::set<TFaction> & getTownTypes() const; /// Default: all
  129. void setTownTypes(const std::set<TFaction> & value);
  130. void setMonsterTypes(const std::set<TFaction> & value);
  131. std::set<TFaction> getDefaultTownTypes() const;
  132. bool getMatchTerrainToTown() const; /// Default: true
  133. void setMatchTerrainToTown(bool value);
  134. const std::set<ETerrainType> & getTerrainTypes() const; /// Default: all
  135. void setTerrainTypes(const std::set<ETerrainType> & value);
  136. std::set<ETerrainType> getDefaultTerrainTypes() const;
  137. void setMinesAmount (TResource res, ui16 amount);
  138. std::map<TResource, ui16> getMinesInfo() const;
  139. void setMonsterStrength (EMonsterStrength::EMonsterStrength val);
  140. float3 getCenter() const;
  141. void setCenter(const float3 &f);
  142. int3 getPos() const;
  143. void setPos(const int3 &pos);
  144. bool isAccessibleFromAnywhere(ObjectTemplate &appearance, int3 &tile) const;
  145. int3 getAccessibleOffset(ObjectTemplate &appearance, int3 &tile) const;
  146. void addTile (const int3 &pos);
  147. void initFreeTiles ();
  148. std::set<int3> getTileInfo() const;
  149. std::set<int3> getPossibleTiles() const;
  150. void discardDistantTiles (float distance);
  151. void clearTiles();
  152. void addRequiredObject(CGObjectInstance * obj, si32 guardStrength=0);
  153. void addCloseObject(CGObjectInstance * obj, si32 guardStrength = 0);
  154. void addToConnectLater(const int3& src);
  155. bool addMonster(int3 &pos, si32 strength, bool clearSurroundingTiles = true, bool zoneGuard = false);
  156. bool createTreasurePile(int3 &pos, float minDistance, const CTreasureInfo& treasureInfo);
  157. bool fill ();
  158. bool placeMines ();
  159. void initTownType ();
  160. void paintZoneTerrain (ETerrainType terrainType);
  161. void randomizeTownType(); //helper function
  162. void initTerrainType ();
  163. void createBorder();
  164. void fractalize();
  165. void connectLater();
  166. EObjectPlacingResult::EObjectPlacingResult tryToPlaceObjectAndConnectToPath(CGObjectInstance *obj, int3 &pos); //return true if the position cna be connected
  167. bool createRequiredObjects();
  168. void createTreasures();
  169. void createObstacles1();
  170. void createObstacles2();
  171. bool crunchPath(const int3 &src, const int3 &dst, bool onlyStraight, std::set<int3>* clearedTiles = nullptr);
  172. bool connectPath(const int3& src, bool onlyStraight);
  173. bool connectWithCenter(const int3& src, bool onlyStraight);
  174. void updateDistances(const int3 & pos);
  175. std::vector<int3> getAccessibleOffsets (const CGObjectInstance* object);
  176. bool areAllTilesAvailable(CGObjectInstance* obj, int3& tile, std::set<int3>& tilesBlockedByObject) const;
  177. void addConnection(TRmgTemplateZoneId otherZone);
  178. void setQuestArtZone(CRmgTemplateZone * otherZone);
  179. std::vector<TRmgTemplateZoneId> getConnections() const;
  180. void addTreasureInfo(CTreasureInfo & info);
  181. std::vector<CTreasureInfo> getTreasureInfo();
  182. std::set<int3>* getFreePaths();
  183. ObjectInfo getRandomObject (CTreasurePileInfo &info, ui32 desiredValue, ui32 maxValue, ui32 currentValue);
  184. void placeSubterraneanGate(int3 pos, si32 guardStrength);
  185. void placeObject(CGObjectInstance* object, const int3 &pos, bool updateDistance = true);
  186. bool guardObject(CGObjectInstance* object, si32 str, bool zoneGuard = false, bool addToFreePaths = false);
  187. void placeAndGuardObject(CGObjectInstance* object, const int3 &pos, si32 str, bool zoneGuard = false);
  188. void addRoadNode(const int3 & node);
  189. void connectRoads(); //fills "roads" according to "roadNodes"
  190. //A* priority queue
  191. typedef std::pair<int3, float> TDistance;
  192. struct NodeComparer
  193. {
  194. bool operator()(const TDistance & lhs, const TDistance & rhs) const
  195. {
  196. return (rhs.second < lhs.second);
  197. }
  198. };
  199. boost::heap::priority_queue<TDistance, boost::heap::compare<NodeComparer>> createPiorityQueue();
  200. private:
  201. CMapGenerator * gen;
  202. //template info
  203. TRmgTemplateZoneId id;
  204. ETemplateZoneType::ETemplateZoneType type;
  205. int size;
  206. boost::optional<int> owner;
  207. CTownInfo playerTowns, neutralTowns;
  208. bool townsAreSameType;
  209. std::set<TFaction> townTypes;
  210. std::set<TFaction> monsterTypes;
  211. bool matchTerrainToTown;
  212. std::set<ETerrainType> terrainTypes;
  213. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  214. si32 townType;
  215. ETerrainType terrainType;
  216. CRmgTemplateZone * questArtZone; //artifacts required for Seer Huts will be placed here - or not if null
  217. EMonsterStrength::EMonsterStrength zoneMonsterStrength;
  218. std::vector<CTreasureInfo> treasureInfo;
  219. std::vector<ObjectInfo> possibleObjects;
  220. int minGuardedValue;
  221. //content info
  222. std::vector<std::pair<CGObjectInstance*, ui32>> requiredObjects;
  223. std::vector<std::pair<CGObjectInstance*, ui32>> closeObjects;
  224. std::vector<CGObjectInstance*> objects;
  225. //placement info
  226. int3 pos;
  227. float3 center;
  228. std::set<int3> tileinfo; //irregular area assined to zone
  229. std::set<int3> possibleTiles; //optimization purposes for treasure generation
  230. std::vector<TRmgTemplateZoneId> connections; //list of adjacent zones
  231. std::set<int3> freePaths; //core paths of free tiles that all other objects will be linked to
  232. std::set<int3> roadNodes; //tiles to be connected with roads
  233. std::set<int3> roads; //all tiles with roads
  234. std::set<int3> tilesToConnectLater; //will be connected after paths are fractalized
  235. bool createRoad(const int3 &src, const int3 &dst);
  236. void drawRoads(); //actually updates tiles
  237. bool pointIsIn(int x, int y);
  238. void addAllPossibleObjects (); //add objects, including zone-specific, to possibleObjects
  239. bool findPlaceForObject(CGObjectInstance* obj, si32 min_dist, int3 &pos);
  240. bool findPlaceForTreasurePile(float min_dist, int3 &pos, int value);
  241. bool canObstacleBePlacedHere(ObjectTemplate &temp, int3 &pos);
  242. void setTemplateForObject(CGObjectInstance* obj);
  243. void checkAndPlaceObject(CGObjectInstance* object, const int3 &pos);
  244. };