Zone.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Zone.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 "float3.h"
  13. #include "../int3.h"
  14. #include "CRmgTemplate.h"
  15. #include "RmgArea.h"
  16. #include "RmgPath.h"
  17. #include "RmgObject.h"
  18. #include "modificators/Modificator.h"
  19. //uncomment to generate dumps afger every step of map generation
  20. //#define RMG_DUMP
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class RmgMap;
  23. class CMapGenerator;
  24. class Modificator;
  25. extern const std::function<bool(const int3 &)> AREA_NO_FILTER;
  26. typedef std::list<std::shared_ptr<Modificator>> TModificators;
  27. template<typename T>
  28. class ThreadSafeProxy
  29. {
  30. public:
  31. ThreadSafeProxy(T& resource, std::recursive_mutex& mutex)
  32. : resourceRef(resource), lock(mutex) {}
  33. T* operator->() { return &resourceRef; }
  34. const T* operator->() const { return &resourceRef; }
  35. T& operator*() { return resourceRef; }
  36. const T& operator*() const { return resourceRef; }
  37. T& get() {return resourceRef;}
  38. const T& get() const {return resourceRef;}
  39. T operator+(const T & other)
  40. {
  41. return resourceRef + other;
  42. }
  43. template <typename U>
  44. T operator+(ThreadSafeProxy<U> & other)
  45. {
  46. return get() + other.get();
  47. }
  48. template <typename U>
  49. T operator+(ThreadSafeProxy<U> && other)
  50. {
  51. return get() + other.get();
  52. }
  53. private:
  54. T& resourceRef;
  55. std::lock_guard<std::recursive_mutex> lock;
  56. };
  57. class Zone : public rmg::ZoneOptions
  58. {
  59. public:
  60. Zone(RmgMap & map, CMapGenerator & generator, vstd::RNG & rand);
  61. Zone(const Zone &) = delete;
  62. ~Zone();
  63. void setOptions(const rmg::ZoneOptions & options);
  64. bool isUnderground() const;
  65. float3 getCenter() const;
  66. void setCenter(const float3 &f);
  67. int3 getPos() const;
  68. void setPos(const int3 &pos);
  69. void moveToCenterOfMass();
  70. ThreadSafeProxy<rmg::Area> area();
  71. ThreadSafeProxy<const rmg::Area> area() const;
  72. ThreadSafeProxy<rmg::Area> areaPossible();
  73. ThreadSafeProxy<const rmg::Area> areaPossible() const;
  74. ThreadSafeProxy<rmg::Area> freePaths();
  75. ThreadSafeProxy<const rmg::Area> freePaths() const;
  76. ThreadSafeProxy<rmg::Area> areaUsed();
  77. ThreadSafeProxy<const rmg::Area> areaUsed() const;
  78. rmg::Area areaForRoads() const;
  79. void initFreeTiles();
  80. void clearTiles();
  81. void fractalize();
  82. FactionID getTownType() const;
  83. void setTownType(FactionID town);
  84. TerrainId getTerrainType() const;
  85. void setTerrainType(TerrainId terrain);
  86. void connectPath(const rmg::Path & path);
  87. rmg::Path searchPath(const rmg::Area & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter = AREA_NO_FILTER) const;
  88. rmg::Path searchPath(const int3 & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter = AREA_NO_FILTER) const;
  89. rmg::Path searchPath(const rmg::Area & src, bool onlyStraight, const rmg::Area & searchArea) const;
  90. TModificators getModificators();
  91. template<class T>
  92. T* getModificator()
  93. {
  94. for(auto & m : modificators)
  95. if(auto * mm = dynamic_cast<T*>(m.get()))
  96. return mm;
  97. return nullptr;
  98. }
  99. template<class T>
  100. void addModificator()
  101. {
  102. modificators.emplace_back(new T(*this, map, generator));
  103. }
  104. void initModificators();
  105. vstd::RNG & getRand();
  106. public:
  107. mutable std::recursive_mutex areaMutex;
  108. using Lock = std::unique_lock<std::recursive_mutex>;
  109. protected:
  110. CMapGenerator & generator;
  111. std::unique_ptr<vstd::RNG> rand;
  112. RmgMap & map;
  113. TModificators modificators;
  114. bool finished;
  115. //placement info
  116. int3 pos;
  117. float3 center;
  118. rmg::Area dArea; //irregular area assigned to zone
  119. rmg::Area dAreaPossible;
  120. rmg::Area dAreaFree; //core paths of free tiles that all other objects will be linked to
  121. rmg::Area dAreaUsed;
  122. std::vector<int3> possibleQuestArtifactPos;
  123. //template info
  124. FactionID townType;
  125. TerrainId terrainType;
  126. };
  127. VCMI_LIB_NAMESPACE_END