RockPlacer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * RockPlacer.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 "RockPlacer.h"
  12. #include "TreasurePlacer.h"
  13. #include "ObjectManager.h"
  14. #include "RoadPlacer.h"
  15. #include "RiverPlacer.h"
  16. #include "RmgMap.h"
  17. #include "CMapGenerator.h"
  18. #include "Functions.h"
  19. #include "../TerrainHandler.h"
  20. #include "../CRandomGenerator.h"
  21. #include "../mapping/CMapEditManager.h"
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. void RockPlacer::process()
  24. {
  25. rockTerrain = VLC->terrainTypeHandler->getById(zone.getTerrainType())->rockTerrain;
  26. assert(!VLC->terrainTypeHandler->getById(rockTerrain)->isPassable());
  27. accessibleArea = zone.freePaths() + zone.areaUsed();
  28. if(auto * m = zone.getModificator<ObjectManager>())
  29. accessibleArea.unite(m->getVisitableArea());
  30. //negative approach - create rock tiles first, then make sure all accessible tiles have no rock
  31. rockArea = zone.area().getSubarea([this](const int3 & t)
  32. {
  33. return map.shouldBeBlocked(t);
  34. });
  35. for(auto & z : map.getZones())
  36. {
  37. if(auto * m = z.second->getModificator<RockPlacer>())
  38. {
  39. if(m != this && !m->isFinished())
  40. return;
  41. }
  42. }
  43. processMap();
  44. }
  45. void RockPlacer::processMap()
  46. {
  47. //merge all areas
  48. for(auto & z : map.getZones())
  49. {
  50. if(auto * m = z.second->getModificator<RockPlacer>())
  51. {
  52. map.getEditManager()->getTerrainSelection().setSelection(m->rockArea.getTilesVector());
  53. map.getEditManager()->drawTerrain(m->rockTerrain, &generator.rand);
  54. }
  55. }
  56. for(auto & z : map.getZones())
  57. {
  58. if(auto * m = z.second->getModificator<RockPlacer>())
  59. {
  60. //now make sure all accessible tiles have no additional rock on them
  61. map.getEditManager()->getTerrainSelection().setSelection(m->accessibleArea.getTilesVector());
  62. map.getEditManager()->drawTerrain(z.second->getTerrainType(), &generator.rand);
  63. m->postProcess();
  64. }
  65. }
  66. }
  67. void RockPlacer::postProcess()
  68. {
  69. //finally mark rock tiles as occupied, spawn no obstacles there
  70. rockArea = zone.area().getSubarea([this](const int3 & t)
  71. {
  72. return !map.map().getTile(t).terType->isPassable();
  73. });
  74. zone.areaUsed().unite(rockArea);
  75. zone.areaPossible().subtract(rockArea);
  76. if(auto * m = zone.getModificator<RiverPlacer>())
  77. m->riverProhibit().unite(rockArea);
  78. if(auto * m = zone.getModificator<RoadPlacer>())
  79. m->areaIsolated().unite(rockArea);
  80. }
  81. void RockPlacer::init()
  82. {
  83. POSTFUNCTION_ALL(RoadPlacer);
  84. DEPENDENCY(TreasurePlacer);
  85. }
  86. char RockPlacer::dump(const int3 & t)
  87. {
  88. if(!map.map().getTile(t).terType->isPassable())
  89. {
  90. return zone.area().contains(t) ? 'R' : 'E';
  91. }
  92. return Modificator::dump(t);
  93. }
  94. VCMI_LIB_NAMESPACE_END