Zone.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "../CRandomGenerator.h"
  15. #include "CRmgTemplate.h"
  16. #include "RmgArea.h"
  17. #include "RmgPath.h"
  18. #include "RmgObject.h"
  19. #include "modificators/Modificator.h"
  20. //uncomment to generate dumps afger every step of map generation
  21. //#define RMG_DUMP
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. class RmgMap;
  24. class CMapGenerator;
  25. class Modificator;
  26. class CRandomGenerator;
  27. extern const std::function<bool(const int3 &)> AREA_NO_FILTER;
  28. typedef std::list<std::shared_ptr<Modificator>> TModificators;
  29. template<typename T>
  30. class ThreadSafeProxy
  31. {
  32. public:
  33. ThreadSafeProxy(T& resource, boost::recursive_mutex& mutex)
  34. : resourceRef(resource), lock(mutex) {}
  35. T* operator->() { return &resourceRef; }
  36. const T* operator->() const { return &resourceRef; }
  37. T& operator*() { return resourceRef; }
  38. const T& operator*() const { return resourceRef; }
  39. T& get() {return resourceRef;}
  40. const T& get() const {return resourceRef;}
  41. T operator+(const T & other)
  42. {
  43. return resourceRef + other;
  44. }
  45. template <typename U>
  46. T operator+(ThreadSafeProxy<U> & other)
  47. {
  48. return get() + other.get();
  49. }
  50. template <typename U>
  51. T operator+(ThreadSafeProxy<U> && other)
  52. {
  53. return get() + other.get();
  54. }
  55. private:
  56. T& resourceRef;
  57. std::lock_guard<boost::recursive_mutex> lock;
  58. };
  59. class Zone : public rmg::ZoneOptions
  60. {
  61. public:
  62. Zone(RmgMap & map, CMapGenerator & generator, CRandomGenerator & rand);
  63. Zone(const Zone &) = delete;
  64. void setOptions(const rmg::ZoneOptions & options);
  65. bool isUnderground() const;
  66. float3 getCenter() const;
  67. void setCenter(const float3 &f);
  68. int3 getPos() const;
  69. void setPos(const int3 &pos);
  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. 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. CRandomGenerator & getRand();
  105. public:
  106. mutable boost::recursive_mutex areaMutex;
  107. using Lock = boost::unique_lock<boost::recursive_mutex>;
  108. protected:
  109. CMapGenerator & generator;
  110. CRandomGenerator 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 assined 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