ConnectionsPlacer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * ConnectionsPlacer.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 "ConnectionsPlacer.h"
  12. #include "CMapGenerator.h"
  13. #include "RmgMap.h"
  14. #include "../TerrainHandler.h"
  15. #include "../mapping/CMap.h"
  16. #include "../mapping/CMapEditManager.h"
  17. #include "../mapObjects/CObjectClassesHandler.h"
  18. #include "RmgPath.h"
  19. #include "RmgObject.h"
  20. #include "ObjectManager.h"
  21. #include "Functions.h"
  22. #include "RoadPlacer.h"
  23. #include "TileInfo.h"
  24. #include "WaterAdopter.h"
  25. #include "WaterProxy.h"
  26. #include "TownPlacer.h"
  27. VCMI_LIB_NAMESPACE_BEGIN
  28. void ConnectionsPlacer::process()
  29. {
  30. collectNeighbourZones();
  31. for(auto & c : dConnections)
  32. {
  33. if(c.getZoneA() != zone.getId() && c.getZoneB() != zone.getId())
  34. continue;
  35. if(vstd::contains(dCompleted, c))
  36. continue;
  37. selfSideDirectConnection(c);
  38. }
  39. createBorder(map, zone);
  40. for(auto & c : dConnections)
  41. {
  42. if(c.getZoneA() != zone.getId() && c.getZoneB() != zone.getId())
  43. continue;
  44. if(vstd::contains(dCompleted, c))
  45. continue;
  46. selfSideIndirectConnection(c);
  47. }
  48. }
  49. void ConnectionsPlacer::init()
  50. {
  51. DEPENDENCY(WaterAdopter);
  52. DEPENDENCY(TownPlacer);
  53. POSTFUNCTION(RoadPlacer);
  54. POSTFUNCTION(ObjectManager);
  55. for(auto c : map.getMapGenOptions().getMapTemplate()->getConnections())
  56. addConnection(c);
  57. }
  58. void ConnectionsPlacer::addConnection(const rmg::ZoneConnection& connection)
  59. {
  60. dConnections.push_back(connection);
  61. }
  62. void ConnectionsPlacer::otherSideConnection(const rmg::ZoneConnection & connection)
  63. {
  64. dCompleted.push_back(connection);
  65. }
  66. void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & connection)
  67. {
  68. bool success = false;
  69. auto otherZoneId = (connection.getZoneA() == zone.getId() ? connection.getZoneB() : connection.getZoneA());
  70. auto & otherZone = map.getZones().at(otherZoneId);
  71. //1. Try to make direct connection
  72. //Do if it's not prohibited by terrain settings
  73. const auto * ourTerrain = VLC->terrainTypeHandler->getById(zone.getTerrainType());
  74. const auto * otherTerrain = VLC->terrainTypeHandler->getById(otherZone->getTerrainType());
  75. bool directProhibited = vstd::contains(ourTerrain->prohibitTransitions, otherZone->getTerrainType())
  76. || vstd::contains(otherTerrain->prohibitTransitions, zone.getTerrainType());
  77. auto directConnectionIterator = dNeighbourZones.find(otherZoneId);
  78. if(!directProhibited && directConnectionIterator != dNeighbourZones.end())
  79. {
  80. int3 guardPos(-1, -1, -1);
  81. int3 borderPos;
  82. while(!directConnectionIterator->second.empty())
  83. {
  84. borderPos = *RandomGeneratorUtil::nextItem(directConnectionIterator->second, generator.rand);
  85. guardPos = zone.areaPossible().nearest(borderPos);
  86. assert(borderPos != guardPos);
  87. auto safetyGap = rmg::Area({guardPos});
  88. safetyGap.unite(safetyGap.getBorderOutside());
  89. safetyGap.intersect(zone.areaPossible());
  90. if(!safetyGap.empty())
  91. {
  92. safetyGap.intersect(otherZone->areaPossible());
  93. if(safetyGap.empty())
  94. break; //successfull position
  95. }
  96. //failed position
  97. directConnectionIterator->second.erase(borderPos);
  98. guardPos = int3(-1, -1, -1);
  99. }
  100. if(guardPos.valid())
  101. {
  102. assert(zone.getModificator<ObjectManager>());
  103. auto & manager = *zone.getModificator<ObjectManager>();
  104. auto * monsterType = manager.chooseGuard(connection.getGuardStrength(), true);
  105. rmg::Area border(zone.getArea().getBorder());
  106. border.unite(otherZone->getArea().getBorder());
  107. auto costFunction = [&border](const int3 & s, const int3 & d)
  108. {
  109. return 1.f / (1.f + border.distanceSqr(d));
  110. };
  111. auto ourArea = zone.areaPossible() + zone.freePaths();
  112. auto theirArea = otherZone->areaPossible() + otherZone->freePaths();
  113. theirArea.add(guardPos);
  114. rmg::Path ourPath(ourArea);
  115. rmg::Path theirPath(theirArea);
  116. ourPath.connect(zone.freePaths());
  117. ourPath = ourPath.search(guardPos, true, costFunction);
  118. theirPath.connect(otherZone->freePaths());
  119. theirPath = theirPath.search(guardPos, true, costFunction);
  120. if(ourPath.valid() && theirPath.valid())
  121. {
  122. zone.connectPath(ourPath);
  123. otherZone->connectPath(theirPath);
  124. if(monsterType)
  125. {
  126. rmg::Object monster(*monsterType);
  127. monster.setPosition(guardPos);
  128. manager.placeObject(monster, false, true);
  129. }
  130. else
  131. {
  132. zone.areaPossible().erase(guardPos);
  133. zone.freePaths().add(guardPos);
  134. map.setOccupied(guardPos, ETileType::FREE);
  135. }
  136. assert(zone.getModificator<RoadPlacer>());
  137. zone.getModificator<RoadPlacer>()->addRoadNode(guardPos);
  138. assert(otherZone->getModificator<RoadPlacer>());
  139. otherZone->getModificator<RoadPlacer>()->addRoadNode(borderPos);
  140. assert(otherZone->getModificator<ConnectionsPlacer>());
  141. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  142. success = true;
  143. }
  144. }
  145. }
  146. //2. connect via water
  147. bool waterMode = map.getMapGenOptions().getWaterContent() != EWaterContent::NONE;
  148. if(waterMode && zone.isUnderground() == otherZone->isUnderground())
  149. {
  150. if(generator.getZoneWater() && generator.getZoneWater()->getModificator<WaterProxy>())
  151. {
  152. if(generator.getZoneWater()->getModificator<WaterProxy>()->waterKeepConnection(connection.getZoneA(), connection.getZoneB()))
  153. {
  154. assert(otherZone->getModificator<ConnectionsPlacer>());
  155. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  156. success = true;
  157. }
  158. }
  159. }
  160. if(success)
  161. dCompleted.push_back(connection);
  162. }
  163. void ConnectionsPlacer::selfSideIndirectConnection(const rmg::ZoneConnection & connection)
  164. {
  165. bool success = false;
  166. auto otherZoneId = (connection.getZoneA() == zone.getId() ? connection.getZoneB() : connection.getZoneA());
  167. auto & otherZone = map.getZones().at(otherZoneId);
  168. //3. place subterrain gates
  169. if(zone.isUnderground() != otherZone->isUnderground())
  170. {
  171. int3 zShift(0, 0, zone.getPos().z - otherZone->getPos().z);
  172. auto commonArea = zone.areaPossible() * (otherZone->areaPossible() + zShift);
  173. if(!commonArea.empty())
  174. {
  175. assert(zone.getModificator<ObjectManager>());
  176. auto & manager = *zone.getModificator<ObjectManager>();
  177. assert(otherZone->getModificator<ObjectManager>());
  178. auto & managerOther = *otherZone->getModificator<ObjectManager>();
  179. auto factory = VLC->objtypeh->getHandlerFor(Obj::SUBTERRANEAN_GATE, 0);
  180. auto * gate1 = factory->create();
  181. auto * gate2 = factory->create();
  182. rmg::Object rmgGate1(*gate1);
  183. rmg::Object rmgGate2(*gate2);
  184. rmgGate1.setTemplate(zone.getTerrainType());
  185. rmgGate2.setTemplate(otherZone->getTerrainType());
  186. bool guarded1 = manager.addGuard(rmgGate1, connection.getGuardStrength(), true);
  187. bool guarded2 = managerOther.addGuard(rmgGate2, connection.getGuardStrength(), true);
  188. int minDist = 3;
  189. rmg::Path path2(otherZone->area());
  190. rmg::Path path1 = manager.placeAndConnectObject(commonArea, rmgGate1, [this, minDist, &path2, &rmgGate1, &zShift, guarded2, &managerOther, &rmgGate2 ](const int3 & tile)
  191. {
  192. auto ti = map.getTile(tile);
  193. float dist = ti.getNearestObjectDistance();
  194. if(dist < minDist)
  195. return -1.f;
  196. rmg::Area toPlace(rmgGate1.getArea() + rmgGate1.getAccessibleArea());
  197. toPlace.translate(-zShift);
  198. path2 = managerOther.placeAndConnectObject(toPlace, rmgGate2, minDist, guarded2, true, ObjectManager::OptimizeType::NONE);
  199. return path2.valid() ? 1.f : -1.f;
  200. }, guarded1, true, ObjectManager::OptimizeType::NONE);
  201. if(path1.valid() && path2.valid())
  202. {
  203. zone.connectPath(path1);
  204. otherZone->connectPath(path2);
  205. manager.placeObject(rmgGate1, guarded1, true);
  206. managerOther.placeObject(rmgGate2, guarded2, true);
  207. assert(otherZone->getModificator<ConnectionsPlacer>());
  208. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  209. success = true;
  210. }
  211. }
  212. }
  213. //4. place monoliths/portals
  214. if(!success)
  215. {
  216. auto factory = VLC->objtypeh->getHandlerFor(Obj::MONOLITH_TWO_WAY, generator.getNextMonlithIndex());
  217. auto * teleport1 = factory->create();
  218. auto * teleport2 = factory->create();
  219. zone.getModificator<ObjectManager>()->addRequiredObject(teleport1, connection.getGuardStrength());
  220. otherZone->getModificator<ObjectManager>()->addRequiredObject(teleport2, connection.getGuardStrength());
  221. assert(otherZone->getModificator<ConnectionsPlacer>());
  222. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  223. success = true;
  224. }
  225. if(success)
  226. dCompleted.push_back(connection);
  227. }
  228. void ConnectionsPlacer::collectNeighbourZones()
  229. {
  230. auto border = zone.area().getBorderOutside();
  231. for(const auto & i : border)
  232. {
  233. if(!map.isOnMap(i))
  234. continue;
  235. auto zid = map.getZoneID(i);
  236. assert(zid != zone.getId());
  237. dNeighbourZones[zid].insert(i);
  238. }
  239. }
  240. VCMI_LIB_NAMESPACE_END