ObstaclePlacer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "../Functions.h"
  22. #include "../../entities/faction/CFaction.h"
  23. #include "../../mapping/CMapEditManager.h"
  24. #include "../../mapping/CMap.h"
  25. #include "../../mapping/ObstacleProxy.h"
  26. #include "../../mapObjects/CGObjectInstance.h"
  27. #include "../../mapObjects/ObstacleSetHandler.h"
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. void ObstaclePlacer::process()
  30. {
  31. manager = zone.getModificator<ObjectManager>();
  32. if(!manager)
  33. return;
  34. auto faction = zone.getTownType().toFaction();
  35. ObstacleSetFilter filter(ObstacleSet::EObstacleType::INVALID,
  36. zone.getTerrainType(),
  37. static_cast<EMapLevel>(zone.isUnderground()),
  38. faction->getId(),
  39. faction->alignment);
  40. if (!prepareBiome(filter, zone.getRand()))
  41. {
  42. logGlobal->warn("Failed to prepare biome, using all possible obstacles");
  43. // Use all if we fail to create proper biome
  44. collectPossibleObstacles(zone.getTerrainType());
  45. }
  46. else
  47. {
  48. logGlobal->info("Biome prepared successfully for zone %d", zone.getId());
  49. }
  50. {
  51. auto area = zone.area();
  52. auto areaPossible = zone.areaPossible();
  53. auto areaUsed = zone.areaUsed();
  54. blockedArea = area->getSubarea([this](const int3& t)
  55. {
  56. return map.shouldBeBlocked(t);
  57. });
  58. blockedArea.subtract(*areaUsed);
  59. areaPossible->subtract(blockedArea);
  60. prohibitedArea = zone.freePaths() + areaUsed + manager->getVisitableArea();
  61. if (auto * rp = zone.getModificator<RoadPlacer>())
  62. {
  63. prohibitedArea.unite(rp->getRoads());
  64. }
  65. //Progressively block tiles, but make sure they don't seal any gap between blocks
  66. rmg::Area toBlock;
  67. do
  68. {
  69. toBlock.clear();
  70. for (const auto& tile : areaPossible->getTilesVector())
  71. {
  72. rmg::Area neighbors;
  73. rmg::Area t;
  74. t.add(tile);
  75. for (const auto& n : t.getBorderOutside())
  76. {
  77. //Area outside the map is also impassable
  78. if (!map.isOnMap(n) || map.shouldBeBlocked(n))
  79. {
  80. neighbors.add(n);
  81. }
  82. }
  83. if (neighbors.empty())
  84. {
  85. continue;
  86. }
  87. //Will only be added if it doesn't connect two disjointed blocks
  88. if (neighbors.connected(true)) //Do not block diagonal pass
  89. {
  90. toBlock.add(tile);
  91. }
  92. }
  93. areaPossible->subtract(toBlock);
  94. for (const auto& tile : toBlock.getTilesVector())
  95. {
  96. map.setOccupied(tile, ETileType::BLOCKED);
  97. }
  98. } while (!toBlock.empty());
  99. prohibitedArea.unite(areaPossible.get());
  100. }
  101. auto objs = createObstacles(zone.getRand(), map.mapInstance->cb);
  102. mapProxy->insertObjects(objs);
  103. }
  104. void ObstaclePlacer::init()
  105. {
  106. DEPENDENCY(ObjectManager);
  107. DEPENDENCY(TreasurePlacer);
  108. DEPENDENCY(RoadPlacer);
  109. if (zone.isUnderground())
  110. {
  111. DEPENDENCY_ALL(RockFiller);
  112. }
  113. else
  114. {
  115. DEPENDENCY(WaterRoutes);
  116. DEPENDENCY(WaterProxy);
  117. }
  118. }
  119. bool ObstaclePlacer::isInTheMap(const int3& tile)
  120. {
  121. return map.isOnMap(tile);
  122. }
  123. void ObstaclePlacer::placeObject(rmg::Object & object, std::set<std::shared_ptr<CGObjectInstance>> &)
  124. {
  125. manager->placeObject(object, false, false);
  126. }
  127. std::pair<bool, bool> ObstaclePlacer::verifyCoverage(const int3 & t) const
  128. {
  129. return {map.shouldBeBlocked(t), zone.areaPossible()->contains(t)};
  130. }
  131. void ObstaclePlacer::postProcess(const rmg::Object & object)
  132. {
  133. //river processing
  134. riverManager = zone.getModificator<RiverPlacer>();
  135. if(riverManager)
  136. {
  137. const auto objTypeName = object.instances().front()->object().getTypeName();
  138. if(objTypeName == "mountain")
  139. riverManager->riverSource().unite(object.getArea());
  140. else if(objTypeName == "lake")
  141. riverManager->riverSink().unite(object.getArea());
  142. }
  143. }
  144. bool ObstaclePlacer::isProhibited(const rmg::Area & objArea) const
  145. {
  146. if(prohibitedArea.overlap(objArea))
  147. return true;
  148. if(!zone.area()->contains(objArea))
  149. return true;
  150. return false;
  151. }
  152. VCMI_LIB_NAMESPACE_END