CZonePlacer.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*
  2. * CZonePlacer.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 "CZonePlacer.h"
  12. #include "../TerrainHandler.h"
  13. #include "../entities/faction/CFaction.h"
  14. #include "../entities/faction/CTownHandler.h"
  15. #include "../mapping/CMap.h"
  16. #include "../mapping/CMapEditManager.h"
  17. #include "../GameLibrary.h"
  18. #include "CMapGenOptions.h"
  19. #include "CRmgTemplate.h"
  20. #include "RmgMap.h"
  21. #include "Zone.h"
  22. #include "Functions.h"
  23. #include "PenroseTiling.h"
  24. #include <vstd/RNG.h>
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. //#define ZONE_PLACEMENT_LOG true
  27. CZonePlacer::CZonePlacer(RmgMap & map)
  28. : width(0), height(0), mapSize(0),
  29. gravityConstant(1e-3f),
  30. stiffnessConstant(3e-3f),
  31. stifness(0),
  32. stiffnessIncreaseFactor(1.03f),
  33. bestTotalDistance(1e10),
  34. bestTotalOverlap(1e10),
  35. map(map)
  36. {
  37. }
  38. int3 CZonePlacer::cords(const float3 & f) const
  39. {
  40. return int3(static_cast<si32>(std::max(0.f, (f.x * map.width()) - 1)), static_cast<si32>(std::max(0.f, (f.y * map.height() - 1))), f.z);
  41. }
  42. float CZonePlacer::getDistance (float distance) const
  43. {
  44. return (distance ? distance * distance : 1e-6f);
  45. }
  46. void CZonePlacer::findPathsBetweenZones()
  47. {
  48. auto zones = map.getZones();
  49. std::set<std::shared_ptr<Zone>> zonesToCheck;
  50. // Iterate through each pair of nodes in the graph
  51. for (const auto& zone : zones)
  52. {
  53. int start = zone.first;
  54. distancesBetweenZones[start][start] = 0; // Distance from a node to itself is 0
  55. std::queue<int> q;
  56. std::map<int, bool> visited;
  57. visited[start] = true;
  58. q.push(start);
  59. // Perform Breadth-First Search from the starting node
  60. while (!q.empty())
  61. {
  62. int current = q.front();
  63. q.pop();
  64. const auto& currentZone = zones.at(current);
  65. const auto& connectedZoneIds = currentZone->getConnections();
  66. for (auto & connection : connectedZoneIds)
  67. {
  68. switch (connection.getConnectionType())
  69. {
  70. //Do not consider virtual connections for graph distance
  71. case rmg::EConnectionType::REPULSIVE:
  72. case rmg::EConnectionType::FORCE_PORTAL:
  73. continue;
  74. }
  75. auto neighbor = connection.getOtherZoneId(current);
  76. if (current == neighbor)
  77. {
  78. //Do not consider self-connections
  79. continue;
  80. }
  81. if (!visited[neighbor])
  82. {
  83. visited[neighbor] = true;
  84. q.push(neighbor);
  85. distancesBetweenZones[start][neighbor] = distancesBetweenZones[start][current] + 1;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. void CZonePlacer::placeOnGrid(vstd::RNG* rand)
  92. {
  93. auto zones = map.getZones();
  94. assert(zones.size());
  95. //Make sure there are at least as many grid fields as the number of zones
  96. size_t gridSize = std::ceil(std::sqrt(zones.size()));
  97. typedef boost::multi_array<std::shared_ptr<Zone>, 2> GridType;
  98. GridType grid(boost::extents[gridSize][gridSize]);
  99. TZoneVector zonesVector(zones.begin(), zones.end());
  100. //Place first zone
  101. auto firstZone = zonesVector[0].second;
  102. size_t x = 0;
  103. size_t y = 0;
  104. auto getRandomEdge = [rand, gridSize](size_t& x, size_t& y)
  105. {
  106. switch (rand->nextInt(0, 3) % 4)
  107. {
  108. case 0:
  109. x = 0;
  110. y = gridSize / 2;
  111. break;
  112. case 1:
  113. x = gridSize - 1;
  114. y = gridSize / 2;
  115. break;
  116. case 2:
  117. x = gridSize / 2;
  118. y = 0;
  119. break;
  120. case 3:
  121. x = gridSize / 2;
  122. y = gridSize - 1;
  123. break;
  124. }
  125. };
  126. switch (firstZone->getType())
  127. {
  128. case ETemplateZoneType::PLAYER_START:
  129. case ETemplateZoneType::CPU_START:
  130. if (firstZone->getConnectedZoneIds().size() > 2)
  131. {
  132. getRandomEdge(x, y);
  133. }
  134. else
  135. {
  136. //Random corner
  137. if (rand->nextInt(0, 1) == 1)
  138. {
  139. x = 0;
  140. }
  141. else
  142. {
  143. x = gridSize - 1;
  144. }
  145. if (rand->nextInt(0, 1) == 1)
  146. {
  147. y = 0;
  148. }
  149. else
  150. {
  151. y = gridSize - 1;
  152. }
  153. }
  154. break;
  155. case ETemplateZoneType::TREASURE:
  156. if (gridSize & 1) //odd
  157. {
  158. x = y = (gridSize / 2);
  159. }
  160. else
  161. {
  162. //One of 4 squares in the middle
  163. x = (gridSize / 2) - 1 + rand->nextInt(0, 1);
  164. y = (gridSize / 2) - 1 + rand->nextInt(0, 1);
  165. }
  166. break;
  167. case ETemplateZoneType::JUNCTION:
  168. getRandomEdge(x, y);
  169. break;
  170. }
  171. grid[x][y] = firstZone;
  172. //Ignore z placement for simplicity
  173. for (size_t i = 1; i < zones.size(); i++)
  174. {
  175. auto zone = zonesVector[i].second;
  176. auto connectedZoneIds = zone->getConnectedZoneIds();
  177. float maxDistance = -1000.0;
  178. int3 mostDistantPlace;
  179. //Iterate over free positions
  180. for (size_t freeX = 0; freeX < gridSize; ++freeX)
  181. {
  182. for (size_t freeY = 0; freeY < gridSize; ++freeY)
  183. {
  184. if (!grid[freeX][freeY])
  185. {
  186. //There is free space left here
  187. int3 potentialPos(freeX, freeY, 0);
  188. //Compute distance to every existing zone
  189. float distance = 0;
  190. for (size_t existingX = 0; existingX < gridSize; ++existingX)
  191. {
  192. for (size_t existingY = 0; existingY < gridSize; ++existingY)
  193. {
  194. auto existingZone = grid[existingX][existingY];
  195. if (existingZone)
  196. {
  197. //There is already zone here
  198. float localDistance = 0.0f;
  199. auto graphDistance = distancesBetweenZones[zone->getId()][existingZone->getId()];
  200. if (graphDistance > 1)
  201. {
  202. //No direct connection
  203. localDistance = potentialPos.dist2d(int3(existingX, existingY, 0)) * graphDistance;
  204. }
  205. else
  206. {
  207. //Has direct connection - place as close as possible
  208. localDistance = -potentialPos.dist2d(int3(existingX, existingY, 0));
  209. }
  210. localDistance *= scaleForceBetweenZones(zone, existingZone);
  211. distance += localDistance;
  212. }
  213. }
  214. }
  215. if (distance > maxDistance)
  216. {
  217. maxDistance = distance;
  218. mostDistantPlace = potentialPos;
  219. }
  220. }
  221. }
  222. }
  223. //Place in a free slot
  224. grid[mostDistantPlace.x][mostDistantPlace.y] = zone;
  225. }
  226. //TODO: toggle with a flag
  227. #ifdef ZONE_PLACEMENT_LOG
  228. logGlobal->trace("Initial zone grid:");
  229. for (size_t x = 0; x < gridSize; ++x)
  230. {
  231. std::string s;
  232. for (size_t y = 0; y < gridSize; ++y)
  233. {
  234. if (grid[x][y])
  235. {
  236. s += (boost::format("%3d ") % grid[x][y]->getId()).str();
  237. }
  238. else
  239. {
  240. s += " -- ";
  241. }
  242. }
  243. logGlobal->trace(s);
  244. }
  245. #endif
  246. //Set initial position for zones - random position in square centered around (x, y)
  247. for (size_t x = 0; x < gridSize; ++x)
  248. {
  249. for (size_t y = 0; y < gridSize; ++y)
  250. {
  251. auto zone = grid[x][y];
  252. if (zone)
  253. {
  254. //i.e. for grid size 5 we get range (0.25 - 4.75)
  255. auto targetX = rand->nextDouble(x + 0.25f, x + 0.75f);
  256. vstd::abetween(targetX, 0.5, gridSize - 0.5);
  257. auto targetY = rand->nextDouble(y + 0.25f, y + 0.75f);
  258. vstd::abetween(targetY, 0.5, gridSize - 0.5);
  259. zone->setCenter(float3(targetX / gridSize, targetY / gridSize, zone->getPos().z));
  260. }
  261. }
  262. }
  263. }
  264. float CZonePlacer::scaleForceBetweenZones(const std::shared_ptr<Zone> zoneA, const std::shared_ptr<Zone> zoneB) const
  265. {
  266. if (zoneA->getOwner() && zoneB->getOwner()) //Players participate in game
  267. {
  268. int firstPlayer = zoneA->getOwner().value();
  269. int secondPlayer = zoneB->getOwner().value();
  270. //Players with lower indexes (especially 1 and 2) will be placed further apart
  271. return (1.0f + (2.0f / (firstPlayer * secondPlayer)));
  272. }
  273. else
  274. {
  275. return 1;
  276. }
  277. }
  278. void CZonePlacer::placeZones(vstd::RNG * rand)
  279. {
  280. logGlobal->info("Starting zone placement");
  281. width = map.getMapGenOptions().getWidth();
  282. height = map.getMapGenOptions().getHeight();
  283. auto zones = map.getZones();
  284. vstd::erase_if(zones, [](const std::pair<TRmgTemplateZoneId, std::shared_ptr<Zone>> & pr)
  285. {
  286. return pr.second->getType() == ETemplateZoneType::WATER;
  287. });
  288. bool underground = map.getMapGenOptions().getHasTwoLevels();
  289. findPathsBetweenZones();
  290. placeOnGrid(rand);
  291. /*
  292. Fruchterman-Reingold algorithm
  293. Let's assume we try to fit N circular zones with radius = size on a map
  294. Connected zones attract, intersecting zones and map boundaries push back
  295. */
  296. TZoneVector zonesVector(zones.begin(), zones.end());
  297. assert (zonesVector.size());
  298. RandomGeneratorUtil::randomShuffle(zonesVector, *rand);
  299. //0. set zone sizes and surface / underground level
  300. prepareZones(zones, zonesVector, underground, rand);
  301. std::map<std::shared_ptr<Zone>, float3> bestSolution;
  302. TForceVector forces;
  303. TForceVector totalForces; // both attraction and pushback, overcomplicated?
  304. TDistanceVector distances;
  305. TDistanceVector overlaps;
  306. auto evaluateSolution = [this, zones, &distances, &overlaps, &bestSolution]() -> bool
  307. {
  308. bool improvement = false;
  309. float totalDistance = 0;
  310. float totalOverlap = 0;
  311. for (const auto& zone : distances) //find most misplaced zone
  312. {
  313. totalDistance += zone.second;
  314. float overlap = overlaps[zone.first];
  315. totalOverlap += overlap;
  316. }
  317. //check fitness function
  318. if ((totalDistance + 1) * (totalOverlap + 1) < (bestTotalDistance + 1) * (bestTotalOverlap + 1))
  319. {
  320. //multiplication is better for auto-scaling, but stops working if one factor is 0
  321. improvement = true;
  322. }
  323. //Save best solution
  324. if (improvement)
  325. {
  326. bestTotalDistance = totalDistance;
  327. bestTotalOverlap = totalOverlap;
  328. for (const auto& zone : zones)
  329. bestSolution[zone.second] = zone.second->getCenter();
  330. }
  331. #ifdef ZONE_PLACEMENT_LOG
  332. logGlobal->trace("Total distance between zones after this iteration: %2.4f, Total overlap: %2.4f, Improved: %s", totalDistance, totalOverlap , improvement);
  333. #endif
  334. return improvement;
  335. };
  336. //Start with low stiffness. Bigger graphs need more time and more flexibility
  337. for (stifness = stiffnessConstant / zones.size(); stifness <= stiffnessConstant;)
  338. {
  339. //1. attract connected zones
  340. attractConnectedZones(zones, forces, distances);
  341. for(const auto & zone : forces)
  342. {
  343. zone.first->setCenter (zone.first->getCenter() + zone.second);
  344. totalForces[zone.first] = zone.second; //override
  345. }
  346. //2. separate overlapping zones
  347. separateOverlappingZones(zones, forces, overlaps);
  348. for(const auto & zone : forces)
  349. {
  350. zone.first->setCenter (zone.first->getCenter() + zone.second);
  351. totalForces[zone.first] += zone.second; //accumulate
  352. }
  353. bool improved = evaluateSolution();
  354. if (!improved)
  355. {
  356. //3. now perform drastic movement of zone that is completely not linked
  357. //TODO: Don't do this is fitness was improved
  358. moveOneZone(zones, totalForces, distances, overlaps);
  359. improved |= evaluateSolution();
  360. }
  361. if (!improved)
  362. {
  363. //Only cool down if we didn't see any improvement
  364. stifness *= stiffnessIncreaseFactor;
  365. }
  366. }
  367. logGlobal->trace("Best fitness reached: total distance %2.4f, total overlap %2.4f", bestTotalDistance, bestTotalOverlap);
  368. for(const auto & zone : zones) //finalize zone positions
  369. {
  370. zone.second->setPos (cords (bestSolution[zone.second]));
  371. #ifdef ZONE_PLACEMENT_LOG
  372. logGlobal->trace("Placed zone %d at relative position %s and coordinates %s", zone.first, zone.second->getCenter().toString(), zone.second->getPos().toString());
  373. #endif
  374. }
  375. }
  376. void CZonePlacer::prepareZones(TZoneMap &zones, TZoneVector &zonesVector, const bool underground, vstd::RNG * rand)
  377. {
  378. std::vector<float> totalSize = { 0, 0 }; //make sure that sum of zone sizes on surface and uderground match size of the map
  379. int zonesOnLevel[2] = { 0, 0 };
  380. //even distribution for surface / underground zones. Surface zones always have priority.
  381. TZoneVector zonesToPlace;
  382. std::map<TRmgTemplateZoneId, int> levels;
  383. //first pass - determine fixed surface for zones
  384. for(const auto & zone : zonesVector)
  385. {
  386. if (!underground) //this step is ignored
  387. zonesToPlace.push_back(zone);
  388. else //place players depending on their factions
  389. {
  390. if(std::optional<int> owner = zone.second->getOwner())
  391. {
  392. auto player = PlayerColor(*owner - 1);
  393. auto playerSettings = map.getMapGenOptions().getPlayersSettings();
  394. FactionID faction = FactionID::RANDOM;
  395. if (playerSettings.size() > player)
  396. {
  397. faction = std::next(playerSettings.begin(), player)->second.getStartingTown();
  398. }
  399. else
  400. {
  401. logGlobal->trace("Player %d (starting zone %d) does not participate in game", player.getNum(), zone.first);
  402. }
  403. if (faction == FactionID::RANDOM) //TODO: check this after a town has already been randomized
  404. zonesToPlace.push_back(zone);
  405. else
  406. {
  407. auto & tt = (*LIBRARY->townh)[faction]->nativeTerrain;
  408. if(tt == ETerrainId::NONE)
  409. {
  410. //any / random
  411. zonesToPlace.push_back(zone);
  412. }
  413. else
  414. {
  415. const auto & terrainType = LIBRARY->terrainTypeHandler->getById(tt);
  416. if(terrainType->isUnderground() && !terrainType->isSurface())
  417. {
  418. //underground only
  419. zonesOnLevel[1]++;
  420. levels[zone.first] = 1;
  421. }
  422. else
  423. {
  424. //surface
  425. zonesOnLevel[0]++;
  426. levels[zone.first] = 0;
  427. }
  428. }
  429. }
  430. }
  431. else //no starting zone or no underground altogether
  432. {
  433. zonesToPlace.push_back(zone);
  434. }
  435. }
  436. }
  437. for(const auto & zone : zonesToPlace)
  438. {
  439. if (underground) //only then consider underground zones
  440. {
  441. int level = 0;
  442. if (zonesOnLevel[1] < zonesOnLevel[0]) //only if there are less underground zones
  443. level = 1;
  444. else
  445. level = 0;
  446. levels[zone.first] = level;
  447. zonesOnLevel[level]++;
  448. }
  449. else
  450. levels[zone.first] = 0;
  451. }
  452. for(const auto & zone : zonesVector)
  453. {
  454. int level = levels[zone.first];
  455. totalSize[level] += (zone.second->getSize() * zone.second->getSize());
  456. float3 center = zone.second->getCenter();
  457. center.z = level;
  458. zone.second->setCenter(center);
  459. }
  460. /*
  461. prescale zones
  462. formula: sum((prescaler*n)^2)*pi = WH
  463. prescaler = sqrt((WH)/(sum(n^2)*pi))
  464. */
  465. std::vector<float> prescaler = { 0, 0 };
  466. for (int i = 0; i < 2; i++)
  467. prescaler[i] = std::sqrt((width * height) / (totalSize[i] * PI_CONSTANT));
  468. mapSize = static_cast<float>(sqrt(width * height));
  469. for(const auto & zone : zones)
  470. {
  471. zone.second->setSize(static_cast<int>(zone.second->getSize() * prescaler[zone.second->getCenter().z]));
  472. }
  473. }
  474. void CZonePlacer::attractConnectedZones(TZoneMap & zones, TForceVector & forces, TDistanceVector & distances) const
  475. {
  476. for(const auto & zone : zones)
  477. {
  478. float3 forceVector(0, 0, 0);
  479. float3 pos = zone.second->getCenter();
  480. float totalDistance = 0;
  481. for (const auto & connection : zone.second->getConnections())
  482. {
  483. switch (connection.getConnectionType())
  484. {
  485. //Do not consider virtual connections for graph distance
  486. case rmg::EConnectionType::REPULSIVE:
  487. case rmg::EConnectionType::FORCE_PORTAL:
  488. continue;
  489. }
  490. if (connection.getZoneA() == connection.getZoneB())
  491. {
  492. //Do not consider self-connections
  493. continue;
  494. }
  495. auto otherZone = zones[connection.getOtherZoneId(zone.second->getId())];
  496. float3 otherZoneCenter = otherZone->getCenter();
  497. auto distance = static_cast<float>(pos.dist2d(otherZoneCenter));
  498. forceVector += (otherZoneCenter - pos) * distance * gravityConstant * scaleForceBetweenZones(zone.second, otherZone); //positive value
  499. //Attract zone centers always
  500. float minDistance = 0;
  501. if (pos.z != otherZoneCenter.z)
  502. minDistance = 0; //zones on different levels can overlap completely
  503. else
  504. minDistance = (zone.second->getSize() + otherZone->getSize()) / mapSize; //scale down to (0,1) coordinates
  505. if (distance > minDistance)
  506. totalDistance += (distance - minDistance);
  507. }
  508. distances[zone.second] = totalDistance;
  509. forceVector.z = 0; //operator - doesn't preserve z coordinate :/
  510. forces[zone.second] = forceVector;
  511. }
  512. }
  513. void CZonePlacer::separateOverlappingZones(TZoneMap &zones, TForceVector &forces, TDistanceVector &overlaps)
  514. {
  515. for(const auto & zone : zones)
  516. {
  517. float3 forceVector(0, 0, 0);
  518. float3 pos = zone.second->getCenter();
  519. float overlap = 0;
  520. //separate overlapping zones
  521. for(const auto & otherZone : zones)
  522. {
  523. float3 otherZoneCenter = otherZone.second->getCenter();
  524. //zones on different levels don't push away
  525. if (zone == otherZone || pos.z != otherZoneCenter.z)
  526. continue;
  527. auto distance = static_cast<float>(pos.dist2d(otherZoneCenter));
  528. float minDistance = (zone.second->getSize() + otherZone.second->getSize()) / mapSize;
  529. if (distance < minDistance)
  530. {
  531. float3 localForce = (((otherZoneCenter - pos)*(minDistance / (distance ? distance : 1e-3f))) / getDistance(distance)) * stifness;
  532. //negative value
  533. localForce *= scaleForceBetweenZones(zone.second, otherZone.second);
  534. forceVector -= localForce * (distancesBetweenZones[zone.second->getId()][otherZone.second->getId()] / 2.0f);
  535. overlap += (minDistance - distance); //overlapping of small zones hurts us more
  536. }
  537. }
  538. //move zones away from boundaries
  539. //do not scale boundary distance - zones tend to get squashed
  540. float size = zone.second->getSize() / mapSize;
  541. auto pushAwayFromBoundary = [&forceVector, pos, size, &overlap, this](float x, float y)
  542. {
  543. float3 boundary = float3(x, y, pos.z);
  544. auto distance = static_cast<float>(pos.dist2d(boundary));
  545. overlap += std::max<float>(0, distance - size); //check if we're closer to map boundary than value of zone size
  546. forceVector -= (boundary - pos) * (size - distance) / this->getDistance(distance) * this->stifness; //negative value
  547. };
  548. if (pos.x < size)
  549. {
  550. pushAwayFromBoundary(0, pos.y);
  551. }
  552. if (pos.x > 1 - size)
  553. {
  554. pushAwayFromBoundary(1, pos.y);
  555. }
  556. if (pos.y < size)
  557. {
  558. pushAwayFromBoundary(pos.x, 0);
  559. }
  560. if (pos.y > 1 - size)
  561. {
  562. pushAwayFromBoundary(pos.x, 1);
  563. }
  564. //Always move repulsive zones away, no matter their distance
  565. //TODO: Consider z plane?
  566. for (auto& connection : zone.second->getConnections())
  567. {
  568. if (connection.getConnectionType() == rmg::EConnectionType::REPULSIVE)
  569. {
  570. auto & otherZone = zones[connection.getOtherZoneId(zone.second->getId())];
  571. float3 otherZoneCenter = otherZone->getCenter();
  572. //TODO: Roll into lambda?
  573. auto distance = static_cast<float>(pos.dist2d(otherZoneCenter));
  574. float minDistance = (zone.second->getSize() + otherZone->getSize()) / mapSize;
  575. float3 localForce = (((otherZoneCenter - pos)*(minDistance / (distance ? distance : 1e-3f))) / getDistance(distance)) * stifness;
  576. localForce *= (distancesBetweenZones[zone.second->getId()][otherZone->getId()]);
  577. forceVector -= localForce * scaleForceBetweenZones(zone.second, otherZone);
  578. }
  579. }
  580. overlaps[zone.second] = overlap;
  581. forceVector.z = 0; //operator - doesn't preserve z coordinate :/
  582. forces[zone.second] = forceVector;
  583. }
  584. }
  585. void CZonePlacer::moveOneZone(TZoneMap& zones, TForceVector& totalForces, TDistanceVector& distances, TDistanceVector& overlaps)
  586. {
  587. //The more zones, the greater total distance expected
  588. //Also, higher stiffness make expected movement lower
  589. const int maxDistanceMovementRatio = zones.size() * zones.size() * (stiffnessConstant / stifness);
  590. typedef std::pair<float, std::shared_ptr<Zone>> Misplacement;
  591. std::vector<Misplacement> misplacedZones;
  592. float totalDistance = 0;
  593. float totalOverlap = 0;
  594. for (const auto& zone : distances) //find most misplaced zone
  595. {
  596. if (vstd::contains(lastSwappedZones, zone.first->getId()))
  597. {
  598. continue;
  599. }
  600. totalDistance += zone.second;
  601. float overlap = overlaps[zone.first];
  602. totalOverlap += overlap;
  603. //if distance to actual movement is long, the zone is misplaced
  604. float ratio = (zone.second + overlap) / static_cast<float>(totalForces[zone.first].mag());
  605. if (ratio > maxDistanceMovementRatio)
  606. {
  607. misplacedZones.emplace_back(std::make_pair(ratio, zone.first));
  608. }
  609. }
  610. if (misplacedZones.empty())
  611. return;
  612. boost::sort(misplacedZones, [](const Misplacement& lhs, Misplacement& rhs)
  613. {
  614. return lhs.first > rhs.first; //Largest displacement first
  615. });
  616. #ifdef ZONE_PLACEMENT_LOG
  617. logGlobal->trace("Worst misplacement/movement ratio: %3.2f", misplacedZones.front().first);
  618. #endif
  619. if (misplacedZones.size() >= 2)
  620. {
  621. //Swap 2 misplaced zones
  622. auto firstZone = misplacedZones.front().second;
  623. std::shared_ptr<Zone> secondZone;
  624. std::set<TRmgTemplateZoneId> connectedZones;
  625. for (const auto& connection : firstZone->getConnections())
  626. {
  627. switch (connection.getConnectionType())
  628. {
  629. //Do not consider virtual connections for graph distance
  630. case rmg::EConnectionType::REPULSIVE:
  631. case rmg::EConnectionType::FORCE_PORTAL:
  632. continue;
  633. }
  634. if (connection.getZoneA() == connection.getZoneB())
  635. {
  636. //Do not consider self-connections
  637. continue;
  638. }
  639. connectedZones.insert(connection.getOtherZoneId(firstZone->getId()));
  640. }
  641. auto level = firstZone->getCenter().z;
  642. for (size_t i = 1; i < misplacedZones.size(); i++)
  643. {
  644. //Only swap zones on the same level
  645. //Don't swap zones that should be connected (Jebus)
  646. if (misplacedZones[i].second->getCenter().z == level &&
  647. !vstd::contains(connectedZones, misplacedZones[i].second->getId()))
  648. {
  649. secondZone = misplacedZones[i].second;
  650. break;
  651. }
  652. }
  653. if (secondZone)
  654. {
  655. #ifdef ZONE_PLACEMENT_LOG
  656. logGlobal->trace("Swapping two misplaced zones %d and %d", firstZone->getId(), secondZone->getId());
  657. #endif
  658. auto firstCenter = firstZone->getCenter();
  659. auto secondCenter = secondZone->getCenter();
  660. firstZone->setCenter(secondCenter);
  661. secondZone->setCenter(firstCenter);
  662. lastSwappedZones.insert(firstZone->getId());
  663. lastSwappedZones.insert(secondZone->getId());
  664. return;
  665. }
  666. }
  667. lastSwappedZones.clear(); //If we didn't swap zones in this iteration, we can do it in the next
  668. //find most distant zone that should be attracted and move inside it
  669. std::shared_ptr<Zone> targetZone;
  670. auto misplacedZone = misplacedZones.front().second;
  671. float3 ourCenter = misplacedZone->getCenter();
  672. if ((totalDistance / (bestTotalDistance + 1)) > (totalOverlap / (bestTotalOverlap + 1)))
  673. {
  674. //Move one zone towards most distant zone to reduce distance
  675. float maxDistance = 0;
  676. for (const auto & con : misplacedZone->getConnections())
  677. {
  678. if (con.getConnectionType() == rmg::EConnectionType::REPULSIVE)
  679. {
  680. continue;
  681. }
  682. auto otherZone = zones[con.getOtherZoneId(misplacedZone->getId())];
  683. float distance = static_cast<float>(otherZone->getCenter().dist2dSQ(ourCenter));
  684. if (distance > maxDistance)
  685. {
  686. maxDistance = distance;
  687. targetZone = otherZone;
  688. }
  689. }
  690. if (targetZone)
  691. {
  692. float3 vec = targetZone->getCenter() - ourCenter;
  693. float newDistanceBetweenZones = (std::max(misplacedZone->getSize(), targetZone->getSize())) / mapSize;
  694. #ifdef ZONE_PLACEMENT_LOG
  695. logGlobal->trace("Trying to move zone %d %s towards %d %s. Direction is %s", misplacedZone->getId(), ourCenter.toString(), targetZone->getId(), targetZone->getCenter().toString(), vec.toString());
  696. #endif
  697. misplacedZone->setCenter(targetZone->getCenter() - vec.unitVector() * newDistanceBetweenZones); //zones should now overlap by half size
  698. }
  699. }
  700. else
  701. {
  702. //Move misplaced zone away from overlapping zone
  703. float maxOverlap = 0;
  704. for(const auto & otherZone : zones)
  705. {
  706. float3 otherZoneCenter = otherZone.second->getCenter();
  707. if (otherZone.second == misplacedZone || otherZoneCenter.z != ourCenter.z)
  708. continue;
  709. auto distance = static_cast<float>(otherZoneCenter.dist2dSQ(ourCenter));
  710. if (distance > maxOverlap)
  711. {
  712. maxOverlap = distance;
  713. targetZone = otherZone.second;
  714. }
  715. }
  716. if (targetZone)
  717. {
  718. float3 vec = ourCenter - targetZone->getCenter();
  719. float newDistanceBetweenZones = (misplacedZone->getSize() + targetZone->getSize()) / mapSize;
  720. #ifdef ZONE_PLACEMENT_LOG
  721. logGlobal->trace("Trying to move zone %d %s away from %d %s. Direction is %s", misplacedZone->getId(), ourCenter.toString(), targetZone->getId(), targetZone->getCenter().toString(), vec.toString());
  722. #endif
  723. misplacedZone->setCenter(targetZone->getCenter() + vec.unitVector() * newDistanceBetweenZones); //zones should now be just separated
  724. }
  725. }
  726. //Don't swap that zone in next iteration
  727. lastSwappedZones.insert(misplacedZone->getId());
  728. }
  729. float CZonePlacer::metric (const int3 &A, const int3 &B) const
  730. {
  731. return A.dist2dSQ(B);
  732. }
  733. void CZonePlacer::assignZones(vstd::RNG * rand)
  734. {
  735. logGlobal->info("Starting zone colouring");
  736. auto width = map.getMapGenOptions().getWidth();
  737. auto height = map.getMapGenOptions().getHeight();
  738. auto zones = map.getZones();
  739. vstd::erase_if(zones, [](const std::pair<TRmgTemplateZoneId, std::shared_ptr<Zone>> & pr)
  740. {
  741. return pr.second->getType() == ETemplateZoneType::WATER;
  742. });
  743. using Dpair = std::pair<std::shared_ptr<Zone>, float>;
  744. std::vector <Dpair> distances;
  745. distances.reserve(zones.size());
  746. //now place zones correctly and assign tiles to each zone
  747. auto compareByDistance = [](const Dpair & lhs, const Dpair & rhs) -> bool
  748. {
  749. //bigger zones have smaller distance
  750. return lhs.second / lhs.first->getSize() < rhs.second / rhs.first->getSize();
  751. };
  752. auto simpleCompareByDistance = [](const Dpair & lhs, const Dpair & rhs) -> bool
  753. {
  754. //bigger zones have smaller distance
  755. return lhs.second < rhs.second;
  756. };
  757. int levels = map.levels();
  758. // Find current center of mass for each zone. Move zone to that center to balance zones sizes
  759. std::vector<RmgMap::Zones> zonesOnLevel;
  760. for(int level = 0; level < levels; level++)
  761. {
  762. zonesOnLevel.push_back(map.getZonesOnLevel(level));
  763. }
  764. int3 pos;
  765. for(pos.z = 0; pos.z < levels; pos.z++)
  766. {
  767. for(pos.x = 0; pos.x < width; pos.x++)
  768. {
  769. for(pos.y = 0; pos.y < height; pos.y++)
  770. {
  771. distances.clear();
  772. for(const auto & zone : zonesOnLevel[pos.z])
  773. {
  774. distances.emplace_back(zone.second, static_cast<float>(pos.dist2dSQ(zone.second->getPos())));
  775. }
  776. boost::min_element(distances, compareByDistance)->first->area()->add(pos); //closest tile belongs to zone
  777. }
  778. }
  779. }
  780. for(const auto & zone : zones)
  781. {
  782. if(zone.second->area()->empty())
  783. throw rmgException("Empty zone is generated, probably RMG template is inappropriate for map size");
  784. zone.second->moveToCenterOfMass();
  785. }
  786. for(const auto & zone : zones)
  787. zone.second->clearTiles(); //now populate them again
  788. PenroseTiling penrose;
  789. for (int level = 0; level < levels; level++)
  790. {
  791. //Create different tiling for each level
  792. auto vertices = penrose.generatePenroseTiling(zonesOnLevel[level].size(), rand);
  793. // Assign zones to closest Penrose vertex
  794. std::map<std::shared_ptr<Zone>, std::set<int3>> vertexMapping;
  795. for (const auto & vertex : vertices)
  796. {
  797. distances.clear();
  798. for(const auto & zone : zonesOnLevel[level])
  799. {
  800. distances.emplace_back(zone.second, zone.second->getCenter().dist2dSQ(float3(vertex.x(), vertex.y(), level)));
  801. }
  802. auto closestZone = boost::min_element(distances, compareByDistance)->first;
  803. vertexMapping[closestZone].insert(int3(vertex.x() * width, vertex.y() * height, level)); //Closest vertex belongs to zone
  804. }
  805. //Assign actual tiles to each zone
  806. pos.z = level;
  807. for (pos.x = 0; pos.x < width; pos.x++)
  808. {
  809. for (pos.y = 0; pos.y < height; pos.y++)
  810. {
  811. distances.clear();
  812. for(const auto & zoneVertex : vertexMapping)
  813. {
  814. auto zone = zoneVertex.first;
  815. for (const auto & vertex : zoneVertex.second)
  816. {
  817. distances.emplace_back(zone, metric(pos, vertex));
  818. }
  819. }
  820. //Tile closest to vertex belongs to zone
  821. auto closestZone = boost::min_element(distances, simpleCompareByDistance)->first;
  822. closestZone->area()->add(pos);
  823. map.setZoneID(pos, closestZone->getId());
  824. }
  825. }
  826. for(const auto & zone : zonesOnLevel[level])
  827. {
  828. if(zone.second->area()->empty())
  829. {
  830. // FIXME: Some vertices are duplicated, but it's not a source of problem
  831. logGlobal->error("Zone %d at %s is empty, dumping Penrose tiling", zone.second->getId(), zone.second->getCenter().toString());
  832. for (const auto & vertex : vertices)
  833. {
  834. logGlobal->warn("Penrose Vertex: %s", vertex.toString());
  835. }
  836. throw rmgException("Empty zone after Penrose tiling");
  837. }
  838. }
  839. }
  840. //set position (town position) to center of mass of irregular zone
  841. for(const auto & zone : zones)
  842. {
  843. zone.second->moveToCenterOfMass();
  844. //TODO: similar for islands
  845. #define CREATE_FULL_UNDERGROUND true //consider linking this with water amount
  846. if (zone.second->isUnderground())
  847. {
  848. if (!CREATE_FULL_UNDERGROUND)
  849. {
  850. auto discardTiles = collectDistantTiles(*zone.second, zone.second->getSize() + 1.f);
  851. for(const auto & t : discardTiles)
  852. zone.second->area()->erase(t);
  853. }
  854. //make sure that terrain inside zone is not a rock
  855. auto v = zone.second->area()->getTilesVector();
  856. map.getMapProxy()->drawTerrain(*rand, v, ETerrainId::SUBTERRANEAN);
  857. }
  858. }
  859. logGlobal->info("Finished zone colouring");
  860. }
  861. void CZonePlacer::RemoveRoadsForWideConnections()
  862. {
  863. auto zones = map.getZones();
  864. for(auto & zonePtr : zones)
  865. {
  866. for(auto & connection : zonePtr.second->getConnections())
  867. {
  868. if(connection.getConnectionType() == rmg::EConnectionType::WIDE)
  869. {
  870. zonePtr.second->setRoadOption(connection.getId(), rmg::ERoadOption::ROAD_FALSE);
  871. }
  872. }
  873. }
  874. }
  875. TRmgTemplateZoneId findSet(std::map<TRmgTemplateZoneId, TRmgTemplateZoneId> & parent, TRmgTemplateZoneId x)
  876. {
  877. if(parent[x] != x)
  878. parent[x] = findSet(parent, parent[x]);
  879. return parent[x];
  880. }
  881. void unionSets(std::map<TRmgTemplateZoneId, TRmgTemplateZoneId> & parent, TRmgTemplateZoneId x, TRmgTemplateZoneId y)
  882. {
  883. TRmgTemplateZoneId rx = findSet(parent, x);
  884. TRmgTemplateZoneId ry = findSet(parent, y);
  885. if(rx != ry)
  886. parent[rx] = ry;
  887. }
  888. /*
  889. Random road generation requirements:
  890. - Every town should be connected via road
  891. - There should be exactly one road betwen any two towns (connected MST)
  892. - This excludes cases when there are multiple road connetions betwween two zones
  893. - Road cannot end in a zone without town
  894. - Wide connections should have no road
  895. */
  896. void CZonePlacer::dropRandomRoads(vstd::RNG * rand)
  897. {
  898. logGlobal->info("Starting road randomization");
  899. auto zones = map.getZones();
  900. // Helper lambda to set road option for all instances of a connection
  901. auto setRoadOptionForConnection = [&zones](int connectionId, rmg::ERoadOption roadOption)
  902. {
  903. // Update all instances of this connection (A→B and B→A) to have the same road option
  904. for(auto & zonePtr : zones)
  905. {
  906. for(auto & connection : zonePtr.second->getConnections())
  907. {
  908. if(connection.getId() == connectionId)
  909. {
  910. zonePtr.second->setRoadOption(connectionId, roadOption);
  911. }
  912. }
  913. }
  914. };
  915. // Identify zones with towns
  916. std::set<TRmgTemplateZoneId> zonesWithTowns;
  917. for(const auto & zone : zones)
  918. {
  919. if(zone.second->getPlayerTowns().getTownCount() ||
  920. zone.second->getPlayerTowns().getCastleCount() ||
  921. zone.second->getNeutralTowns().getTownCount() ||
  922. zone.second->getNeutralTowns().getCastleCount())
  923. {
  924. zonesWithTowns.insert(zone.first);
  925. }
  926. }
  927. logGlobal->info("Found %d zones with towns", zonesWithTowns.size());
  928. if(zonesWithTowns.empty())
  929. {
  930. // No towns, no roads needed - mark all RANDOM roads as FALSE
  931. for(auto & zonePtr : zones)
  932. {
  933. for(auto & connection : zonePtr.second->getConnections())
  934. {
  935. if(connection.getRoadOption() == rmg::ERoadOption::ROAD_RANDOM)
  936. {
  937. setRoadOptionForConnection(connection.getId(), rmg::ERoadOption::ROAD_FALSE);
  938. }
  939. }
  940. }
  941. return;
  942. }
  943. // Track direct connections between zones (both TRUE and RANDOM)
  944. std::map<TRmgTemplateZoneId, std::map<TRmgTemplateZoneId, bool>> directConnections;
  945. // First pass: find all TRUE connections
  946. for(auto & zonePtr : zones)
  947. {
  948. for(auto & connection : zonePtr.second->getConnections())
  949. {
  950. auto zoneA = connection.getZoneA();
  951. auto zoneB = connection.getZoneB();
  952. if(connection.getRoadOption() == rmg::ERoadOption::ROAD_TRUE)
  953. {
  954. // Mark that these zones are directly connected by a TRUE road
  955. directConnections[zoneA][zoneB] = true;
  956. directConnections[zoneB][zoneA] = true;
  957. }
  958. }
  959. }
  960. // Initialize Union-Find for MST tracking
  961. std::map<TRmgTemplateZoneId, TRmgTemplateZoneId> parent;
  962. for(auto townZone : zonesWithTowns)
  963. {
  964. parent[townZone] = townZone;
  965. }
  966. // Add all TRUE roads connecting town zones to MST
  967. for(auto & zonePtr : zones)
  968. {
  969. for(auto & connection : zonePtr.second->getConnections())
  970. {
  971. if(connection.getRoadOption() == rmg::ERoadOption::ROAD_TRUE)
  972. {
  973. auto zoneA = connection.getZoneA();
  974. auto zoneB = connection.getZoneB();
  975. // If both zones have towns, add to MST
  976. if(vstd::contains(zonesWithTowns, zoneA) && vstd::contains(zonesWithTowns, zoneB))
  977. {
  978. unionSets(parent, zoneA, zoneB);
  979. }
  980. }
  981. }
  982. }
  983. // Process all paths through true roads (BFS)
  984. for(auto townZone : zonesWithTowns)
  985. {
  986. std::queue<TRmgTemplateZoneId> q;
  987. std::set<TRmgTemplateZoneId> visited;
  988. q.push(townZone);
  989. visited.insert(townZone);
  990. while(!q.empty())
  991. {
  992. auto current = q.front();
  993. q.pop();
  994. for(auto & otherZone : directConnections[current])
  995. {
  996. if(otherZone.second && !vstd::contains(visited, otherZone.first))
  997. {
  998. visited.insert(otherZone.first);
  999. q.push(otherZone.first);
  1000. // If this is another town, update MST
  1001. if(vstd::contains(zonesWithTowns, otherZone.first))
  1002. {
  1003. unionSets(parent, townZone, otherZone.first);
  1004. }
  1005. }
  1006. }
  1007. }
  1008. }
  1009. // Process RANDOM connections
  1010. // First, ensure each town has at least one connection
  1011. for(auto townZone : zonesWithTowns)
  1012. {
  1013. // Skip if already has a TRUE road
  1014. if(!directConnections[townZone].empty())
  1015. continue;
  1016. // Find a random connection to upgrade to TRUE
  1017. for(auto & zonePtr : zones)
  1018. {
  1019. for(auto & connection : zonePtr.second->getConnections())
  1020. {
  1021. if(connection.getRoadOption() == rmg::ERoadOption::ROAD_RANDOM &&
  1022. (connection.getZoneA() == townZone || connection.getZoneB() == townZone))
  1023. {
  1024. auto zoneA = connection.getZoneA();
  1025. auto zoneB = connection.getZoneB();
  1026. // Don't upgrade if these zones are already directly connected by a TRUE road
  1027. if(vstd::contains(directConnections[zoneA], zoneB) && directConnections[zoneA][zoneB])
  1028. continue;
  1029. // Upgrade to TRUE
  1030. setRoadOptionForConnection(connection.getId(), rmg::ERoadOption::ROAD_TRUE);
  1031. directConnections[zoneA][zoneB] = true;
  1032. directConnections[zoneB][zoneA] = true;
  1033. auto otherZone = (zoneA == townZone) ? zoneB : zoneA;
  1034. logGlobal->info("Setting RANDOM road to TRUE for connection %d to ensure town zone %d has at least one connection (to zone %d)",
  1035. connection.getId(), townZone, otherZone);
  1036. break;
  1037. }
  1038. }
  1039. // Stop if we've found a connection
  1040. if(!directConnections[townZone].empty())
  1041. break;
  1042. }
  1043. }
  1044. // Process remaining RANDOM roads - prioritize town connectivity
  1045. // First collect all RANDOM roads
  1046. std::vector<std::pair<int, std::pair<TRmgTemplateZoneId, TRmgTemplateZoneId>>> randomRoads;
  1047. for(auto & zonePtr : zones)
  1048. {
  1049. for(auto & connection : zonePtr.second->getConnections())
  1050. {
  1051. if(connection.getRoadOption() == rmg::ERoadOption::ROAD_RANDOM)
  1052. {
  1053. auto id = connection.getId();
  1054. auto zoneA = connection.getZoneA();
  1055. auto zoneB = connection.getZoneB();
  1056. // Skip if these zones are already directly connected by a TRUE road
  1057. if(vstd::contains(directConnections[zoneA], zoneB) && directConnections[zoneA][zoneB])
  1058. {
  1059. setRoadOptionForConnection(id, rmg::ERoadOption::ROAD_FALSE);
  1060. logGlobal->info("Setting RANDOM road to FALSE for connection %d - duplicate of TRUE road between zones %d and %d",
  1061. id, zoneA, zoneB);
  1062. continue;
  1063. }
  1064. randomRoads.push_back(std::make_pair(id, std::make_pair(zoneA, zoneB)));
  1065. }
  1066. }
  1067. }
  1068. RandomGeneratorUtil::randomShuffle(randomRoads, *rand);
  1069. // Process random roads - first connect town zones
  1070. for(auto& road : randomRoads)
  1071. {
  1072. auto id = road.first;
  1073. auto zoneA = road.second.first;
  1074. auto zoneB = road.second.second;
  1075. bool setToTrue = false;
  1076. // If both zones have towns, check if they're already connected in the MST
  1077. if(vstd::contains(zonesWithTowns, zoneA) && vstd::contains(zonesWithTowns, zoneB))
  1078. {
  1079. if(findSet(parent, zoneA) != findSet(parent, zoneB))
  1080. {
  1081. // Not connected, add this road to MST
  1082. unionSets(parent, zoneA, zoneB);
  1083. setToTrue = true;
  1084. logGlobal->info("Setting RANDOM road to TRUE for connection %d between town zones %d and %d",
  1085. id, zoneA, zoneB);
  1086. }
  1087. }
  1088. // If one zone has a town and one doesn't
  1089. else if(vstd::contains(zonesWithTowns, zoneA) || vstd::contains(zonesWithTowns, zoneB))
  1090. {
  1091. TRmgTemplateZoneId townZone = vstd::contains(zonesWithTowns, zoneA) ? zoneA : zoneB;
  1092. // Check if this town already has at least one TRUE connection
  1093. if(directConnections[townZone].empty())
  1094. {
  1095. setToTrue = true;
  1096. logGlobal->info("Setting RANDOM road to TRUE for connection %d - only connection for town zone %d",
  1097. id, townZone);
  1098. }
  1099. }
  1100. // Update all zones with this connection
  1101. setRoadOptionForConnection(id, setToTrue ? rmg::ERoadOption::ROAD_TRUE : rmg::ERoadOption::ROAD_FALSE);
  1102. if(setToTrue)
  1103. {
  1104. directConnections[zoneA][zoneB] = true;
  1105. directConnections[zoneB][zoneA] = true;
  1106. }
  1107. }
  1108. logGlobal->info("Finished road generation - created minimal spanning tree connecting all towns");
  1109. }
  1110. const TDistanceMap& CZonePlacer::getDistanceMap()
  1111. {
  1112. return distancesBetweenZones;
  1113. }
  1114. VCMI_LIB_NAMESPACE_END