ConnectionsPlacer.cpp 8.6 KB

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