CZonePlacer.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 (const 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. bool underground = mapGenOptions->getHasTwoLevels();
  40. //TODO: consider underground zones
  41. /*
  42. let's assume we try to fit N circular zones with radius = size on a map
  43. formula: sum((prescaler*n)^2)*pi = WH
  44. prescaler = sqrt((WH)/(sum(n^2)*pi))
  45. */
  46. float totalSize = 0;
  47. for (auto zone : zones)
  48. {
  49. int level = 0;
  50. if (underground)
  51. level = rand->nextInt(0, 1);
  52. totalSize += (zone.second->getSize() * zone.second->getSize());
  53. zone.second->setCenter (float3(rand->nextDouble(0.2, 0.8), rand->nextDouble(0.2, 0.8), level)); //start away from borders
  54. }
  55. //prescale zones
  56. if (underground) //map is twice as big, so zones occupy only half of normal space
  57. totalSize /= 2;
  58. float prescaler = sqrt ((width * height) / (totalSize * 3.14f));
  59. float mapSize = sqrt (width * height);
  60. for (auto zone : zones)
  61. {
  62. zone.second->setSize (zone.second->getSize() * prescaler);
  63. }
  64. //gravity-based algorithm. connected zones attract, intersceting zones and map boundaries push back
  65. auto getDistance = [](float distance) -> float
  66. {
  67. return (distance ? distance * distance : 1e-6);
  68. };
  69. std::map <CRmgTemplateZone *, float3> forces;
  70. for (int i = 0; i < iterations; ++i)
  71. {
  72. for (auto zone : zones)
  73. {
  74. float3 forceVector(0,0,0);
  75. float3 pos = zone.second->getCenter();
  76. //attract connected zones
  77. for (auto con : zone.second->getConnections())
  78. {
  79. auto otherZone = zones[con];
  80. float distance = pos.dist2d (otherZone->getCenter());
  81. float minDistance = (zone.second->getSize() + otherZone->getSize())/mapSize; //scale down to (0,1) coordinates
  82. if (distance > minDistance)
  83. {
  84. forceVector += (otherZone->getCenter() - pos) / getDistance(distance); //positive value
  85. }
  86. }
  87. //separate overlaping zones
  88. for (auto otherZone : zones)
  89. {
  90. //zones on different levels don't push away
  91. if (zone == otherZone || pos.z != otherZone.second->getCenter().z)
  92. continue;
  93. float distance = pos.dist2d (otherZone.second->getCenter());
  94. float minDistance = (zone.second->getSize() + otherZone.second->getSize())/mapSize;
  95. if (distance < minDistance)
  96. {
  97. forceVector -= (otherZone.second->getCenter() - pos) / getDistance(distance); //negative value
  98. }
  99. }
  100. //move zones away from boundaries
  101. float3 boundary(0,0,pos.z);
  102. float size = zone.second->getSize() / mapSize;
  103. if (pos.x < size)
  104. {
  105. boundary = float3 (0, pos.y, pos.z);
  106. float distance = pos.dist2d(boundary);
  107. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  108. }
  109. if (pos.x > 1-size)
  110. {
  111. boundary = float3 (1, pos.y, pos.z);
  112. float distance = pos.dist2d(boundary);
  113. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  114. }
  115. if (pos.y < size)
  116. {
  117. boundary = float3 (pos.x, 0, pos.z);
  118. float distance = pos.dist2d(boundary);
  119. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  120. }
  121. if (pos.y > 1-size)
  122. {
  123. boundary = float3 (pos.x, 1, pos.z);
  124. float distance = pos.dist2d(boundary);
  125. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  126. }
  127. forceVector.z = 0; //operator - doesn't preserve z coordinate :/
  128. forces[zone.second] = forceVector;
  129. }
  130. //update positions
  131. for (auto zone : forces)
  132. {
  133. zone.first->setCenter (zone.first->getCenter() + zone.second * temperature);
  134. }
  135. temperature *= temperatureModifier; //decrease temperature (needed?)
  136. }
  137. for (auto zone : zones) //finalize zone positions
  138. {
  139. zone.second->setPos(cords(zone.second->getCenter()));
  140. logGlobal->infoStream() << boost::format ("Placed zone %d at relative position %s and coordinates %s") % zone.first % zone.second->getCenter() % zone.second->getPos();
  141. }
  142. }
  143. float CZonePlacer::metric (const int3 &A, const int3 &B) const
  144. {
  145. /*
  146. Matlab code
  147. dx = abs(A(1) - B(1)); %distance must be symmetric
  148. dy = abs(A(2) - B(2));
  149. d = 0.01 * dx^3 - 0.1618 * dx^2 + 1 * dx + ...
  150. 0.01618 * dy^3 + 0.1 * dy^2 + 0.168 * dy;
  151. */
  152. float dx = abs(A.x - B.x) * scaleX;
  153. float dy = abs(A.y - B.y) * scaleY;
  154. //Horner scheme
  155. return dx * (1 + dx * (0.1 + dx * 0.01)) + dy * (1.618 + dy * (-0.1618 + dy * 0.01618));
  156. }
  157. void CZonePlacer::assignZones(shared_ptr<CMapGenOptions> mapGenOptions)
  158. {
  159. logGlobal->infoStream() << "Starting zone colouring";
  160. auto width = mapGenOptions->getWidth();
  161. auto height = mapGenOptions->getHeight();
  162. //scale to Medium map to ensure smooth results
  163. scaleX = 72.f / width;
  164. scaleY = 72.f / height;
  165. auto zones = gen->getZones();
  166. typedef std::pair<CRmgTemplateZone *, float> Dpair;
  167. std::vector <Dpair> distances;
  168. distances.reserve(zones.size());
  169. auto compareByDistance = [](const Dpair & lhs, const Dpair & rhs) -> bool
  170. {
  171. return lhs.second < rhs.second;
  172. };
  173. int levels = gen->map->twoLevel ? 2 : 1;
  174. for (int i=0; i<width; i++)
  175. {
  176. for(int j=0; j<height; j++)
  177. {
  178. for (int k = 0; k < levels; k++)
  179. {
  180. distances.clear();
  181. int3 pos(i, j, k);
  182. for (auto zone : zones)
  183. {
  184. if (zone.second->getPos().z == k)
  185. distances.push_back (std::make_pair(zone.second, metric(pos, zone.second->getPos())));
  186. else
  187. distances.push_back (std::make_pair(zone.second, std::numeric_limits<float>::max()));
  188. }
  189. boost::sort (distances, compareByDistance);
  190. distances.front().first->addTile(pos); //closest tile belongs to zone
  191. }
  192. }
  193. }
  194. //set position to center of mass
  195. for (auto zone : zones)
  196. {
  197. int3 total(0,0,0);
  198. auto tiles = zone.second->getTileInfo();
  199. for (auto tile : tiles)
  200. {
  201. total += tile;
  202. }
  203. int size = tiles.size();
  204. assert (size);
  205. zone.second->setPos (int3(total.x/size, total.y/size, total.z/size));
  206. //TODO: similiar for islands
  207. if (zone.second->getPos().z)
  208. {
  209. zone.second->discardDistantTiles(gen, zone.second->getSize());
  210. //make sure that terrain inside zone is not a rock
  211. //FIXME: reorder actions?
  212. zone.second->paintZoneTerrain (gen, ETerrainType::SUBTERRANEAN);
  213. }
  214. }
  215. logGlobal->infoStream() << "Finished zone colouring";
  216. }