CZonePlacer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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)
  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(const CMapGenOptions * mapGenOptions, CRandomGenerator * rand)
  30. {
  31. //gravity-based algorithm
  32. float gravityConstant = 1e-2;
  33. float zoneScale = 0.5f; //zones starts small and then inflate
  34. const float inflateModifier = 1.02;
  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. /*
  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. std::vector<std::pair<TRmgTemplateZoneId, CRmgTemplateZone*>> zonesVector (zones.begin(), zones.end());
  46. assert (zonesVector.size());
  47. RandomGeneratorUtil::randomShuffle(zonesVector, *rand);
  48. TRmgTemplateZoneId firstZone = zones.begin()->first; //we want lowest ID here
  49. bool undergroundFlag = false;
  50. float totalSize = 0;
  51. for (auto zone : zonesVector)
  52. {
  53. //even distribution for surface / underground zones. Surface zones always have priority.
  54. int level = 0;
  55. if (underground) //only then consider underground zones
  56. {
  57. if (zone.first == firstZone)
  58. {
  59. level = 0;
  60. }
  61. else
  62. {
  63. level = undergroundFlag;
  64. undergroundFlag = !undergroundFlag; //toggle underground on/off
  65. }
  66. }
  67. totalSize += (zone.second->getSize() * zone.second->getSize());
  68. zone.second->setCenter (float3(rand->nextDouble(0.2, 0.8), rand->nextDouble(0.2, 0.8), level)); //start away from borders
  69. }
  70. //prescale zones
  71. if (underground) //map is twice as big, so zones occupy only half of normal space
  72. totalSize /= 2;
  73. float prescaler = sqrt ((width * height) / (totalSize * 3.14f));
  74. float mapSize = sqrt (width * height);
  75. for (auto zone : zones)
  76. {
  77. zone.second->setSize (zone.second->getSize() * prescaler);
  78. }
  79. //gravity-based algorithm. connected zones attract, intersceting zones and map boundaries push back
  80. auto getDistance = [](float distance) -> float
  81. {
  82. return (distance ? distance * distance : 1e-6);
  83. };
  84. std::map <CRmgTemplateZone *, float3> forces;
  85. while (zoneScale < 1) //until zones reach their desired size and fill the map tightly
  86. {
  87. for (auto zone : zones)
  88. {
  89. float3 forceVector(0,0,0);
  90. float3 pos = zone.second->getCenter();
  91. //attract connected zones
  92. for (auto con : zone.second->getConnections())
  93. {
  94. auto otherZone = zones[con];
  95. float3 otherZoneCenter = otherZone->getCenter();
  96. float distance = pos.dist2d (otherZoneCenter);
  97. float minDistance = (zone.second->getSize() + otherZone->getSize())/mapSize * zoneScale; //scale down to (0,1) coordinates
  98. if (distance > minDistance)
  99. {
  100. //WARNING: compiler used to 'optimize' that line so it never actually worked
  101. forceVector += (((otherZoneCenter - pos) / getDistance(distance))); //positive value
  102. }
  103. }
  104. //separate overlaping zones
  105. for (auto otherZone : zones)
  106. {
  107. float3 otherZoneCenter = otherZone.second->getCenter();
  108. //zones on different levels don't push away
  109. if (zone == otherZone || pos.z != otherZoneCenter.z)
  110. continue;
  111. float distance = pos.dist2d (otherZoneCenter);
  112. float minDistance = (zone.second->getSize() + otherZone.second->getSize())/mapSize * zoneScale;
  113. if (distance < minDistance)
  114. {
  115. forceVector -= (otherZoneCenter - pos) / getDistance(distance); //negative value
  116. }
  117. }
  118. //move zones away from boundaries
  119. //do not scale boundary distance - zones tend to get squashed
  120. float size = zone.second->getSize() / mapSize;
  121. auto pushAwayFromBoundary = [&forceVector, pos, &getDistance](float x, float y)
  122. {
  123. float3 boundary = float3 (x, y, pos.z);
  124. float distance = pos.dist2d(boundary);
  125. forceVector -= (boundary - pos) / getDistance(distance); //negative value
  126. };
  127. if (pos.x < size)
  128. {
  129. pushAwayFromBoundary(0, pos.y);
  130. }
  131. if (pos.x > 1-size)
  132. {
  133. pushAwayFromBoundary(1, pos.y);
  134. }
  135. if (pos.y < size)
  136. {
  137. pushAwayFromBoundary(pos.x, 0);
  138. }
  139. if (pos.y > 1-size)
  140. {
  141. pushAwayFromBoundary(pos.x, 1);
  142. }
  143. forceVector.z = 0; //operator - doesn't preserve z coordinate :/
  144. forces[zone.second] = forceVector * gravityConstant;
  145. }
  146. //update positions
  147. for (auto zone : forces)
  148. {
  149. zone.first->setCenter (zone.first->getCenter() + zone.second);
  150. }
  151. zoneScale *= inflateModifier; //increase size
  152. }
  153. for (auto zone : zones) //finalize zone positions
  154. {
  155. zone.second->setPos(cords(zone.second->getCenter()));
  156. logGlobal->infoStream() << boost::format ("Placed zone %d at relative position %s and coordinates %s") % zone.first % zone.second->getCenter() % zone.second->getPos();
  157. }
  158. }
  159. float CZonePlacer::metric (const int3 &A, const int3 &B) const
  160. {
  161. /*
  162. Matlab code
  163. dx = abs(A(1) - B(1)); %distance must be symmetric
  164. dy = abs(A(2) - B(2));
  165. d = 0.01 * dx^3 - 0.1618 * dx^2 + 1 * dx + ...
  166. 0.01618 * dy^3 + 0.1 * dy^2 + 0.168 * dy;
  167. */
  168. float dx = abs(A.x - B.x) * scaleX;
  169. float dy = abs(A.y - B.y) * scaleY;
  170. //Horner scheme
  171. return dx * (1 + dx * (0.1 + dx * 0.01)) + dy * (1.618 + dy * (-0.1618 + dy * 0.01618));
  172. }
  173. void CZonePlacer::assignZones(const CMapGenOptions * mapGenOptions)
  174. {
  175. logGlobal->infoStream() << "Starting zone colouring";
  176. auto width = mapGenOptions->getWidth();
  177. auto height = mapGenOptions->getHeight();
  178. //scale to Medium map to ensure smooth results
  179. scaleX = 72.f / width;
  180. scaleY = 72.f / height;
  181. auto zones = gen->getZones();
  182. typedef std::pair<CRmgTemplateZone *, float> Dpair;
  183. std::vector <Dpair> distances;
  184. distances.reserve(zones.size());
  185. auto compareByDistance = [](const Dpair & lhs, const Dpair & rhs) -> bool
  186. {
  187. return lhs.second < rhs.second;
  188. };
  189. int levels = gen->map->twoLevel ? 2 : 1;
  190. for (int i=0; i<width; i++)
  191. {
  192. for(int j=0; j<height; j++)
  193. {
  194. for (int k = 0; k < levels; k++)
  195. {
  196. distances.clear();
  197. int3 pos(i, j, k);
  198. for (auto zone : zones)
  199. {
  200. if (zone.second->getPos().z == k)
  201. distances.push_back (std::make_pair(zone.second, metric(pos, zone.second->getPos())));
  202. else
  203. distances.push_back (std::make_pair(zone.second, std::numeric_limits<float>::max()));
  204. }
  205. boost::sort (distances, compareByDistance);
  206. distances.front().first->addTile(pos); //closest tile belongs to zone
  207. }
  208. }
  209. }
  210. //set position to center of mass
  211. for (auto zone : zones)
  212. {
  213. int3 total(0,0,0);
  214. auto tiles = zone.second->getTileInfo();
  215. for (auto tile : tiles)
  216. {
  217. total += tile;
  218. }
  219. int size = tiles.size();
  220. assert (size);
  221. zone.second->setPos (int3(total.x/size, total.y/size, total.z/size));
  222. //TODO: similiar for islands
  223. if (zone.second->getPos().z)
  224. {
  225. zone.second->discardDistantTiles(gen, zone.second->getSize() + 1);
  226. //make sure that terrain inside zone is not a rock
  227. //FIXME: reorder actions?
  228. zone.second->paintZoneTerrain (gen, ETerrainType::SUBTERRANEAN);
  229. }
  230. }
  231. logGlobal->infoStream() << "Finished zone colouring";
  232. }