ObstaclePlacer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * ObstaclePlacer.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 "ObstaclePlacer.h"
  12. #include "ObjectManager.h"
  13. #include "TreasurePlacer.h"
  14. #include "RockFiller.h"
  15. #include "WaterRoutes.h"
  16. #include "WaterProxy.h"
  17. #include "RoadPlacer.h"
  18. #include "RiverPlacer.h"
  19. #include "../RmgMap.h"
  20. #include "../CMapGenerator.h"
  21. #include "../../CRandomGenerator.h"
  22. #include "../Functions.h"
  23. #include "../../mapping/CMapEditManager.h"
  24. #include "../../mapping/CMap.h"
  25. #include "../../mapping/ObstacleProxy.h"
  26. #include "../../mapObjects/CGObjectInstance.h"
  27. VCMI_LIB_NAMESPACE_BEGIN
  28. void ObstaclePlacer::process()
  29. {
  30. manager = zone.getModificator<ObjectManager>();
  31. if(!manager)
  32. return;
  33. collectPossibleObstacles(zone.getTerrainType());
  34. {
  35. //Zone::Lock lock(zone.areaMutex);
  36. auto area = zone.area();
  37. auto areaPossible = zone.areaPossible();
  38. auto areaUsed = zone.areaUsed();
  39. blockedArea = area->getSubarea([this](const int3& t)
  40. {
  41. return map.shouldBeBlocked(t);
  42. });
  43. blockedArea.subtract(*areaUsed);
  44. areaPossible->subtract(blockedArea);
  45. prohibitedArea = zone.freePaths() + areaUsed + manager->getVisitableArea();
  46. //Progressively block tiles, but make sure they don't seal any gap between blocks
  47. rmg::Area toBlock;
  48. do
  49. {
  50. toBlock.clear();
  51. for (const auto& tile : areaPossible->getTilesVector())
  52. {
  53. rmg::Area neighbors;
  54. rmg::Area t;
  55. t.add(tile);
  56. for (const auto& n : t.getBorderOutside())
  57. {
  58. //Area outside the map is also impassable
  59. if (!map.isOnMap(n) || map.shouldBeBlocked(n))
  60. {
  61. neighbors.add(n);
  62. }
  63. }
  64. if (neighbors.empty())
  65. {
  66. continue;
  67. }
  68. //Will only be added if it doesn't connect two disjointed blocks
  69. if (neighbors.connected(true)) //Do not block diagonal pass
  70. {
  71. toBlock.add(tile);
  72. }
  73. }
  74. areaPossible->subtract(toBlock);
  75. for (const auto& tile : toBlock.getTilesVector())
  76. {
  77. map.setOccupied(tile, ETileType::BLOCKED);
  78. }
  79. } while (!toBlock.empty());
  80. prohibitedArea.unite(areaPossible.get());
  81. }
  82. auto objs = createObstacles(zone.getRand(), map.mapInstance->cb);
  83. mapProxy->insertObjects(objs);
  84. }
  85. void ObstaclePlacer::init()
  86. {
  87. DEPENDENCY(ObjectManager);
  88. DEPENDENCY(TreasurePlacer);
  89. DEPENDENCY(RoadPlacer);
  90. if (zone.isUnderground())
  91. {
  92. DEPENDENCY(RockFiller);
  93. }
  94. else
  95. {
  96. DEPENDENCY(WaterRoutes);
  97. DEPENDENCY(WaterProxy);
  98. }
  99. }
  100. bool ObstaclePlacer::isInTheMap(const int3& tile)
  101. {
  102. return map.isOnMap(tile);
  103. }
  104. void ObstaclePlacer::placeObject(rmg::Object & object, std::set<CGObjectInstance*> &)
  105. {
  106. manager->placeObject(object, false, false);
  107. }
  108. std::pair<bool, bool> ObstaclePlacer::verifyCoverage(const int3 & t) const
  109. {
  110. return {map.shouldBeBlocked(t), zone.areaPossible()->contains(t)};
  111. }
  112. void ObstaclePlacer::postProcess(const rmg::Object & object)
  113. {
  114. //river processing
  115. riverManager = zone.getModificator<RiverPlacer>();
  116. if(riverManager)
  117. {
  118. const auto objTypeName = object.instances().front()->object().typeName;
  119. if(objTypeName == "mountain")
  120. riverManager->riverSource().unite(object.getArea());
  121. else if(objTypeName == "lake")
  122. riverManager->riverSink().unite(object.getArea());
  123. }
  124. }
  125. bool ObstaclePlacer::isProhibited(const rmg::Area & objArea) const
  126. {
  127. if(prohibitedArea.overlap(objArea))
  128. return true;
  129. if(!zone.area()->contains(objArea))
  130. return true;
  131. return false;
  132. }
  133. VCMI_LIB_NAMESPACE_END