CZonePlacer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "../CRandomGenerator.h"
  12. #include "CZonePlacer.h"
  13. #include "CRmgTemplateZone.h"
  14. #include "CZoneGraphGenerator.h"
  15. class CRandomGenerator;
  16. CPlacedZone::CPlacedZone(const CRmgTemplateZone * zone) : zone(zone)
  17. {
  18. }
  19. CZonePlacer::CZonePlacer(CMapGenerator * Gen) : gen(Gen)
  20. {
  21. }
  22. CZonePlacer::~CZonePlacer()
  23. {
  24. }
  25. int3 CZonePlacer::cords (float3 f) const
  26. {
  27. return int3(std::max(0.f, (f.x * gen->map->width)-1), std::max(0.f, (f.y * gen->map->height-1)), f.z);
  28. }
  29. void CZonePlacer::placeZones(shared_ptr<CMapGenOptions> mapGenOptions, CRandomGenerator * rand)
  30. {
  31. //some relaxation-simmulated annealing algorithm
  32. const int iterations = 100;
  33. float temperature = 1e-2;;
  34. const float temperatureModifier = 0.99;
  35. logGlobal->infoStream() << "Starting zone placement";
  36. int width = mapGenOptions->getWidth();
  37. int height = mapGenOptions->getHeight();
  38. auto zones = gen->getZones();
  39. //TODO: consider underground zones
  40. /*
  41. let's assume we try to fit N circular zones with radius = size on a map
  42. formula: sum((prescaler*n)^2)*pi = WH
  43. prescaler = sqrt((WH)/(sum(n^2)*pi))
  44. */
  45. float totalSize = 0;
  46. for (auto zone : zones)
  47. {
  48. totalSize += (zone.second->getSize() * zone.second->getSize());
  49. zone.second->setCenter (float3(rand->nextDouble(0.2,0.8), rand->nextDouble(0.2,0.8), 0)); //start away from borders
  50. }
  51. //prescale zones
  52. float prescaler = sqrt ((width * height) / (totalSize * 3.14f));
  53. float mapSize = sqrt (width * height);
  54. for (auto zone : zones)
  55. {
  56. zone.second->setSize (zone.second->getSize() * prescaler);
  57. }
  58. //gravity-based algorithm. connected zones attract, intersceting zones and map boundaries push back
  59. auto getDistance = [](float distance) -> float
  60. {
  61. return (distance ? distance * distance : 1e-6);
  62. };
  63. std::map <CRmgTemplateZone *, float3> forces;
  64. for (int i = 0; i < iterations; ++i)
  65. {
  66. for (auto zone : zones)
  67. {
  68. float3 forceVector(0,0,0);
  69. float3 pos = zone.second->getCenter();
  70. //attract connected zones
  71. for (auto con : zone.second->getConnections())
  72. {
  73. auto otherZone = zones[con];
  74. float distance = pos.dist2d (otherZone->getCenter());
  75. float minDistance = (zone.second->getSize() + otherZone->getSize())/mapSize; //scale down to (0,1) coordinates
  76. if (distance > minDistance)
  77. {
  78. forceVector += (otherZone->getCenter() - pos) / getDistance(distance); //positive value
  79. }
  80. }
  81. //separate overlaping zones
  82. for (auto otherZone : zones)
  83. {
  84. if (zone == otherZone)
  85. continue;
  86. float distance = pos.dist2d (otherZone.second->getCenter());
  87. float minDistance = (zone.second->getSize() + otherZone.second->getSize())/mapSize;
  88. if (distance < minDistance)
  89. {
  90. forceVector -= (otherZone.second->getCenter() - pos) / getDistance(distance); //negative value
  91. }
  92. }
  93. //move zones away from boundaries
  94. float3 boundary(0,0,pos.z);
  95. float size = zone.second->getSize() / mapSize;
  96. if (pos.x < size)
  97. {
  98. boundary = float3 (0, pos.y, pos.z);
  99. float distance = pos.dist2d(boundary);
  100. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  101. }
  102. if (pos.x > 1-size)
  103. {
  104. boundary = float3 (1, pos.y, pos.z);
  105. float distance = pos.dist2d(boundary);
  106. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  107. }
  108. if (pos.y < size)
  109. {
  110. boundary = float3 (pos.x, 0, pos.z);
  111. float distance = pos.dist2d(boundary);
  112. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  113. }
  114. if (pos.y > 1-size)
  115. {
  116. boundary = float3 (pos.x, 1, pos.z);
  117. float distance = pos.dist2d(boundary);
  118. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  119. }
  120. forces[zone.second] = forceVector;
  121. }
  122. //update positions
  123. for (auto zone : forces)
  124. {
  125. zone.first->setCenter (zone.first->getCenter() + zone.second * temperature);
  126. }
  127. temperature *= temperatureModifier; //decrease temperature (needed?)
  128. }
  129. for (auto zone : zones) //finalize zone positions
  130. {
  131. zone.second->setPos(cords(zone.second->getCenter()));
  132. logGlobal->infoStream() << boost::format ("Placed zone %d at relative position %s and coordinates %s") % zone.first % zone.second->getCenter() % zone.second->getPos();
  133. }
  134. }
  135. float CZonePlacer::metric (int3 &A, int3 &B) const
  136. {
  137. /*
  138. Matlab code
  139. dx = abs(A(1) - B(1)); %distance must be symmetric
  140. dy = abs(A(2) - B(2));
  141. d = 0.01 * dx^3 + 0.1 * dx^2 + 1 * dx + ...
  142. 0.03 * dy^3 - 0.3 * dy^2 + 0.3 * dy;
  143. */
  144. float dx = abs(A.x - B.x) * scaleX;
  145. float dy = abs(A.y - B.y) * scaleY;
  146. //Horner scheme
  147. return dx * (1 + dx * (0.1 + dx * 0.01)) + dy * (0.2 + dy * (-0.2 + dy * 0.02));
  148. }
  149. void CZonePlacer::assignZones(shared_ptr<CMapGenOptions> mapGenOptions)
  150. {
  151. logGlobal->infoStream() << "Starting zone colouring";
  152. auto width = mapGenOptions->getWidth();
  153. auto height = mapGenOptions->getHeight();
  154. //scale to Medium map to ensure smooth results
  155. scaleX = 72.f / width;
  156. scaleY = 72.f / height;
  157. auto zones = gen->getZones();
  158. typedef std::pair<CRmgTemplateZone *, float> Dpair;
  159. std::vector <Dpair> distances;
  160. distances.reserve(zones.size());
  161. auto compareByDistance = [](const Dpair & lhs, const Dpair & rhs) -> bool
  162. {
  163. return lhs.second < rhs.second;
  164. };
  165. int levels = gen->map->twoLevel ? 2 : 1;
  166. for (int i=0; i<width; i++)
  167. {
  168. for(int j=0; j<height; j++)
  169. {
  170. for (int k = 0; k < levels; k++)
  171. {
  172. distances.clear();
  173. int3 pos(i, j, k);
  174. for (auto zone : zones)
  175. {
  176. distances.push_back (std::make_pair(zone.second, metric(pos, zone.second->getPos())));
  177. }
  178. boost::sort (distances, compareByDistance);
  179. distances.front().first->addTile(pos); //closest tile belongs to zone
  180. }
  181. }
  182. }
  183. logGlobal->infoStream() << "Finished zone colouring";
  184. }