Zone.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. ThreadSafeProxy<rmg::Area> area();
  70. ThreadSafeProxy<const rmg::Area> area() const;
  71. ThreadSafeProxy<rmg::Area> areaPossible();
  72. ThreadSafeProxy<const rmg::Area> areaPossible() const;
  73. ThreadSafeProxy<rmg::Area> freePaths();
  74. ThreadSafeProxy<const rmg::Area> freePaths() const;
  75. ThreadSafeProxy<rmg::Area> areaUsed();
  76. ThreadSafeProxy<const rmg::Area> areaUsed() const;
  77. rmg::Area areaForRoads() const;
  78. void initFreeTiles();
  79. void clearTiles();
  80. void fractalize();
  81. FactionID getTownType() const;
  82. void setTownType(FactionID town);
  83. TerrainId getTerrainType() const;
  84. void setTerrainType(TerrainId terrain);
  85. void connectPath(const rmg::Path & path);
  86. rmg::Path searchPath(const rmg::Area & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter = AREA_NO_FILTER) const;
  87. rmg::Path searchPath(const int3 & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter = AREA_NO_FILTER) const;
  88. rmg::Path searchPath(const rmg::Area & src, bool onlyStraight, const rmg::Area & searchArea) const;
  89. TModificators getModificators();
  90. template<class T>
  91. T* getModificator()
  92. {
  93. for(auto & m : modificators)
  94. if(auto * mm = dynamic_cast<T*>(m.get()))
  95. return mm;
  96. return nullptr;
  97. }
  98. template<class T>
  99. void addModificator()
  100. {
  101. modificators.emplace_back(new T(*this, map, generator));
  102. }
  103. void initModificators();
  104. vstd::RNG & getRand();
  105. public:
  106. mutable std::recursive_mutex areaMutex;
  107. using Lock = boost::unique_lock<std::recursive_mutex>;
  108. protected:
  109. CMapGenerator & generator;
  110. std::unique_ptr<vstd::RNG> rand;
  111. RmgMap & map;
  112. TModificators modificators;
  113. bool finished;
  114. //placement info
  115. int3 pos;
  116. float3 center;
  117. rmg::Area dArea; //irregular area assigned to zone
  118. rmg::Area dAreaPossible;
  119. rmg::Area dAreaFree; //core paths of free tiles that all other objects will be linked to
  120. rmg::Area dAreaUsed;
  121. std::vector<int3> possibleQuestArtifactPos;
  122. //template info
  123. FactionID townType;
  124. TerrainId terrainType;
  125. };
  126. VCMI_LIB_NAMESPACE_END