ConnectionsPlacer.cpp 8.6 KB

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