ConnectionsPlacer.cpp 9.8 KB

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