ObstaclePlacer.cpp 4.1 KB

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