ConnectionsPlacer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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), theirPath(theirArea);
  115. ourPath.connect(zone.freePaths());
  116. ourPath = ourPath.search(guardPos, true, costFunction);
  117. theirPath.connect(otherZone->freePaths());
  118. theirPath = theirPath.search(guardPos, true, costFunction);
  119. if(ourPath.valid() && theirPath.valid())
  120. {
  121. zone.connectPath(ourPath);
  122. otherZone->connectPath(theirPath);
  123. if(monsterType)
  124. {
  125. rmg::Object monster(*monsterType);
  126. monster.setPosition(guardPos);
  127. manager.placeObject(monster, false, true);
  128. }
  129. else
  130. {
  131. zone.areaPossible().erase(guardPos);
  132. zone.freePaths().add(guardPos);
  133. map.setOccupied(guardPos, ETileType::FREE);
  134. }
  135. assert(zone.getModificator<RoadPlacer>());
  136. zone.getModificator<RoadPlacer>()->addRoadNode(guardPos);
  137. assert(otherZone->getModificator<RoadPlacer>());
  138. otherZone->getModificator<RoadPlacer>()->addRoadNode(borderPos);
  139. assert(otherZone->getModificator<ConnectionsPlacer>());
  140. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  141. success = true;
  142. }
  143. }
  144. }
  145. //2. connect via water
  146. bool waterMode = map.getMapGenOptions().getWaterContent() != EWaterContent::NONE;
  147. if(waterMode && zone.isUnderground() == otherZone->isUnderground())
  148. {
  149. if(generator.getZoneWater() && generator.getZoneWater()->getModificator<WaterProxy>())
  150. {
  151. if(generator.getZoneWater()->getModificator<WaterProxy>()->waterKeepConnection(connection.getZoneA(), connection.getZoneB()))
  152. {
  153. assert(otherZone->getModificator<ConnectionsPlacer>());
  154. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  155. success = true;
  156. }
  157. }
  158. }
  159. if(success)
  160. dCompleted.push_back(connection);
  161. }
  162. void ConnectionsPlacer::selfSideIndirectConnection(const rmg::ZoneConnection & connection)
  163. {
  164. bool success = false;
  165. auto otherZoneId = (connection.getZoneA() == zone.getId() ? connection.getZoneB() : connection.getZoneA());
  166. auto & otherZone = map.getZones().at(otherZoneId);
  167. //3. place subterrain gates
  168. if(zone.isUnderground() != otherZone->isUnderground())
  169. {
  170. int3 zShift(0, 0, zone.getPos().z - otherZone->getPos().z);
  171. auto commonArea = zone.areaPossible() * (otherZone->areaPossible() + zShift);
  172. if(!commonArea.empty())
  173. {
  174. assert(zone.getModificator<ObjectManager>());
  175. auto & manager = *zone.getModificator<ObjectManager>();
  176. assert(otherZone->getModificator<ObjectManager>());
  177. auto & managerOther = *otherZone->getModificator<ObjectManager>();
  178. auto factory = VLC->objtypeh->getHandlerFor(Obj::SUBTERRANEAN_GATE, 0);
  179. auto gate1 = factory->create();
  180. auto gate2 = factory->create();
  181. rmg::Object rmgGate1(*gate1), rmgGate2(*gate2);
  182. rmgGate1.setTemplate(zone.getTerrainType());
  183. rmgGate2.setTemplate(otherZone->getTerrainType());
  184. bool guarded1 = manager.addGuard(rmgGate1, connection.getGuardStrength(), true);
  185. bool guarded2 = managerOther.addGuard(rmgGate2, connection.getGuardStrength(), true);
  186. int minDist = 3;
  187. rmg::Path path2(otherZone->area());
  188. rmg::Path path1 = manager.placeAndConnectObject(commonArea, rmgGate1, [this, minDist, &path2, &rmgGate1, &zShift, guarded2, &managerOther, &rmgGate2 ](const int3 & tile)
  189. {
  190. auto ti = map.getTile(tile);
  191. float dist = ti.getNearestObjectDistance();
  192. if(dist < minDist)
  193. return -1.f;
  194. rmg::Area toPlace(rmgGate1.getArea() + rmgGate1.getAccessibleArea());
  195. toPlace.translate(-zShift);
  196. path2 = managerOther.placeAndConnectObject(toPlace, rmgGate2, minDist, guarded2, true, ObjectManager::OptimizeType::NONE);
  197. return path2.valid() ? 1.f : -1.f;
  198. }, guarded1, true, ObjectManager::OptimizeType::NONE);
  199. if(path1.valid() && path2.valid())
  200. {
  201. zone.connectPath(path1);
  202. otherZone->connectPath(path2);
  203. manager.placeObject(rmgGate1, guarded1, true);
  204. managerOther.placeObject(rmgGate2, guarded2, true);
  205. assert(otherZone->getModificator<ConnectionsPlacer>());
  206. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  207. success = true;
  208. }
  209. }
  210. }
  211. //4. place monoliths/portals
  212. if(!success)
  213. {
  214. auto factory = VLC->objtypeh->getHandlerFor(Obj::MONOLITH_TWO_WAY, generator.getNextMonlithIndex());
  215. auto teleport1 = factory->create();
  216. auto teleport2 = factory->create();
  217. zone.getModificator<ObjectManager>()->addRequiredObject(teleport1, connection.getGuardStrength());
  218. otherZone->getModificator<ObjectManager>()->addRequiredObject(teleport2, connection.getGuardStrength());
  219. assert(otherZone->getModificator<ConnectionsPlacer>());
  220. otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
  221. success = true;
  222. }
  223. if(success)
  224. dCompleted.push_back(connection);
  225. }
  226. void ConnectionsPlacer::collectNeighbourZones()
  227. {
  228. auto border = zone.area().getBorderOutside();
  229. for(auto & i : border)
  230. {
  231. if(!map.isOnMap(i))
  232. continue;
  233. auto zid = map.getZoneID(i);
  234. assert(zid != zone.getId());
  235. dNeighbourZones[zid].insert(i);
  236. }
  237. }
  238. VCMI_LIB_NAMESPACE_END