ConnectionsPlacer.cpp 9.2 KB

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