RiverPlacer.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * RiverPlacer.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 "RiverPlacer.h"
  12. #include "Functions.h"
  13. #include "CMapGenerator.h"
  14. #include "RmgMap.h"
  15. #include "../mapping/CMap.h"
  16. #include "../mapping/CMapEditManager.h"
  17. #include "../mapObjects/CObjectClassesHandler.h"
  18. #include "RmgPath.h"
  19. #include "ObjectManager.h"
  20. #include "ObstaclePlacer.h"
  21. #include "WaterProxy.h"
  22. #include "RoadPlacer.h"
  23. const int RIVER_DELTA_ID = 143;
  24. const int RIVER_DELTA_SUBTYPE = 0;
  25. const std::map<std::string, std::string> RIVER_DELTA_TEMPLATE_NAME
  26. {
  27. {RIVER_NAMES[1], "clrdelt"},
  28. {RIVER_NAMES[2], "icedelt"},
  29. {RIVER_NAMES[3], "muddelt"},
  30. {RIVER_NAMES[4], "lavdelt"}
  31. };
  32. const std::array<std::array<int, 25>, 4> deltaTemplates
  33. {
  34. //0 - must be on ground
  35. //1 - delta entry
  36. //2 - must be on water
  37. //3 - anything
  38. //4 - prohibit river placement
  39. //5 - must be on ground + position
  40. //6 - must be on water + position
  41. std::array<int, 25>{
  42. 3, 4, 3, 4, 3,
  43. 3, 4, 1, 4, 3,
  44. 3, 0, 0, 0, 3,
  45. 3, 0, 0, 0, 3,
  46. 3, 2, 2, 6, 3
  47. },
  48. std::array<int, 25>{
  49. 3, 2, 2, 2, 3,
  50. 3, 0, 0, 0, 3,
  51. 3, 0, 0, 5, 3,
  52. 3, 4, 1, 4, 3,
  53. 3, 4, 3, 4, 3
  54. },
  55. std::array<int, 25>{
  56. 3, 3, 3, 3, 3,
  57. 4, 4, 0, 0, 2,
  58. 3, 1, 0, 0, 2,
  59. 4, 4, 0, 0, 6,
  60. 3, 3, 3, 3, 3
  61. },
  62. std::array<int, 25> {
  63. 3, 3, 3, 3, 3,
  64. 2, 0, 0, 4, 4,
  65. 2, 0, 0, 1, 3,
  66. 2, 0, 5, 4, 4,
  67. 3, 3, 3, 3, 3
  68. }
  69. };
  70. void RiverPlacer::process()
  71. {
  72. preprocess();
  73. for(auto & t : riverNodes)
  74. connectRiver(t);
  75. if(!rivers.empty())
  76. drawRivers();
  77. }
  78. void RiverPlacer::init()
  79. {
  80. DEPENDENCY_ALL(WaterProxy);
  81. DEPENDENCY(ObjectManager);
  82. DEPENDENCY(ObstaclePlacer);
  83. }
  84. void RiverPlacer::drawRivers()
  85. {
  86. map.getEditManager()->getTerrainSelection().setSelection(rivers.getTilesVector());
  87. map.getEditManager()->drawRiver(Terrain::Manager::getInfo(zone.getTerrainType()).river, &generator.rand);
  88. }
  89. char RiverPlacer::dump(const int3 & t)
  90. {
  91. if(riverNodes.count(t))
  92. return '@';
  93. if(rivers.contains(t))
  94. return '~';
  95. if(sink.contains(t))
  96. return '2';
  97. if(source.contains(t))
  98. return '1';
  99. if(zone.area().contains(t))
  100. return ' ';
  101. return '?';
  102. }
  103. void RiverPlacer::addRiverNode(const int3 & node)
  104. {
  105. assert(zone.area().contains(node));
  106. riverNodes.insert(node);
  107. }
  108. rmg::Area & RiverPlacer::riverSource()
  109. {
  110. return source;
  111. }
  112. rmg::Area & RiverPlacer::riverSink()
  113. {
  114. return sink;
  115. }
  116. rmg::Area & RiverPlacer::riverProhibit()
  117. {
  118. return prohibit;
  119. }
  120. void RiverPlacer::prepareHeightmap()
  121. {
  122. rmg::Area roads;
  123. if(auto * m = zone.getModificator<RoadPlacer>())
  124. {
  125. roads.unite(m->getRoads());
  126. }
  127. for(auto & t : zone.area().getTilesVector())
  128. {
  129. heightMap[t] = generator.rand.nextInt(5);
  130. if(roads.contains(t))
  131. heightMap[t] += 30.f;
  132. if(zone.areaUsed().contains(t))
  133. heightMap[t] += 1000.f;
  134. }
  135. //make grid
  136. for(int j = 0; j < map.map().height; j += 2)
  137. {
  138. for(int i = 0; i < map.map().width; i += 2)
  139. {
  140. int3 t{i, j, zone.getPos().z};
  141. if(zone.area().contains(t))
  142. heightMap[t] += 10.f;
  143. }
  144. }
  145. }
  146. void RiverPlacer::preprocess()
  147. {
  148. rmg::Area outOfMapTiles;
  149. std::map<TRmgTemplateZoneId, rmg::Area> neighbourZonesTiles;
  150. rmg::Area borderArea(zone.getArea().getBorder());
  151. TRmgTemplateZoneId connectedToWaterZoneId = -1;
  152. for(auto & t : zone.getArea().getBorderOutside())
  153. {
  154. if(!map.isOnMap(t))
  155. {
  156. outOfMapTiles.add(t);
  157. }
  158. else if(map.getZoneID(t) != zone.getId())
  159. {
  160. if(map.getZones()[map.getZoneID(t)]->getType() == ETemplateZoneType::WATER)
  161. connectedToWaterZoneId = map.getZoneID(t);
  162. neighbourZonesTiles[map.getZoneID(t)].add(t);
  163. }
  164. }
  165. rmg::Area outOfMapInternal(outOfMapTiles.getBorderOutside());
  166. outOfMapInternal.intersect(borderArea);
  167. //looking outside map
  168. if(!outOfMapInternal.empty())
  169. {
  170. auto elem = *RandomGeneratorUtil::nextItem(outOfMapInternal.getTilesVector(), generator.rand);
  171. source.add(elem);
  172. outOfMapInternal.erase(elem);
  173. }
  174. if(!outOfMapInternal.empty())
  175. {
  176. auto elem = *RandomGeneratorUtil::nextItem(outOfMapInternal.getTilesVector(), generator.rand);
  177. sink.add(elem);
  178. outOfMapInternal.erase(elem);
  179. }
  180. //calculate delta positions
  181. if(connectedToWaterZoneId > -1)
  182. {
  183. auto river = Terrain::Manager::getInfo(zone.getTerrainType()).river;
  184. auto & a = neighbourZonesTiles[connectedToWaterZoneId];
  185. auto availableArea = zone.areaPossible() + zone.freePaths();
  186. for(auto & tileToProcess : availableArea.getTilesVector())
  187. {
  188. int templateId = -1;
  189. for(int tId = 0; tId < 4; ++tId)
  190. {
  191. templateId = tId;
  192. for(int i = 0; i < 25; ++i)
  193. {
  194. if((deltaTemplates[tId][i] == 2 || deltaTemplates[tId][i] == 6) && !a.contains(tileToProcess + int3(i % 5 - 2, i / 5 - 2, 0)))
  195. {
  196. templateId = -1;
  197. break;
  198. }
  199. if((deltaTemplates[tId][i] < 2 || deltaTemplates[tId][i] == 5) && !availableArea.contains(tileToProcess + int3(i % 5 - 2, i / 5 - 2, 0)))
  200. {
  201. templateId = -1;
  202. break;
  203. }
  204. }
  205. if(templateId > -1)
  206. break;
  207. }
  208. if(templateId > -1)
  209. {
  210. for(int i = 0; i < 25; ++i)
  211. {
  212. auto p = tileToProcess + int3(i % 5 - 2, i / 5 - 2, 0);
  213. if(deltaTemplates[templateId][i] == 1)
  214. {
  215. sink.add(p);
  216. deltaSink.add(p);
  217. deltaOrientations[p] = templateId + 1;
  218. //specific case: deltas for ice rivers amd mud rivers are messed :(
  219. if(river == RIVER_NAMES[2])
  220. {
  221. switch(deltaOrientations[p])
  222. {
  223. case 1:
  224. deltaOrientations[p] = 2;
  225. break;
  226. case 2:
  227. deltaOrientations[p] = 3;
  228. break;
  229. case 3:
  230. deltaOrientations[p] = 4;
  231. break;
  232. case 4:
  233. deltaOrientations[p] = 1;
  234. break;
  235. }
  236. }
  237. if(river == RIVER_NAMES[3])
  238. {
  239. switch(deltaOrientations[p])
  240. {
  241. case 1:
  242. deltaOrientations[p] = 4;
  243. break;
  244. case 2:
  245. deltaOrientations[p] = 3;
  246. break;
  247. case 3:
  248. deltaOrientations[p] = 1;
  249. break;
  250. case 4:
  251. deltaOrientations[p] = 2;
  252. break;
  253. }
  254. }
  255. for(auto j = 0; j < 25; ++j)
  256. {
  257. if(deltaTemplates[templateId][j] >= 5)
  258. {
  259. deltaPositions[p] = tileToProcess + int3(j % 5 - 2, j / 5 - 2, 0);
  260. }
  261. }
  262. }
  263. if(deltaTemplates[templateId][i] == 0 || deltaTemplates[templateId][i] == 4 || deltaTemplates[templateId][i] == 5)
  264. {
  265. prohibit.add(p);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. prepareHeightmap();
  272. //decorative river
  273. if(!sink.empty() && !source.empty() && riverNodes.empty() && !zone.areaPossible().empty())
  274. {
  275. addRiverNode(*RandomGeneratorUtil::nextItem(source.getTilesVector(), generator.rand));
  276. }
  277. if(source.empty())
  278. {
  279. logGlobal->info("River source is empty!");
  280. //looking outside map
  281. for(auto & i : heightMap)
  282. {
  283. if(i.second > 0)
  284. source.add(i.first);
  285. }
  286. }
  287. if(sink.empty())
  288. {
  289. logGlobal->error("River sink is empty!");
  290. for(auto & i : heightMap)
  291. {
  292. if(i.second <= 0)
  293. sink.add(i.first);
  294. }
  295. }
  296. }
  297. void RiverPlacer::connectRiver(const int3 & tile)
  298. {
  299. auto river = Terrain::Manager::getInfo(zone.getTerrainType()).river;
  300. if(river.empty() || river == RIVER_NAMES[0])
  301. return;
  302. rmg::Area roads;
  303. if(auto * m = zone.getModificator<RoadPlacer>())
  304. {
  305. roads.unite(m->getRoads());
  306. }
  307. auto movementCost = [this, &roads](const int3 & s, const int3 & d)
  308. {
  309. float cost = heightMap[d];
  310. if(roads.contains(s))
  311. cost += 1000.f; //allow road intersection, but avoid long overlaps
  312. return cost;
  313. };
  314. auto availableArea = zone.area() - prohibit;
  315. rmg::Path pathToSource(availableArea);
  316. pathToSource.connect(source);
  317. pathToSource.connect(rivers);
  318. pathToSource = pathToSource.search(tile, true, movementCost);
  319. availableArea.subtract(pathToSource.getPathArea());
  320. rmg::Path pathToSink(availableArea);
  321. pathToSink.connect(sink);
  322. pathToSource.connect(rivers);
  323. pathToSink = pathToSink.search(tile, true, movementCost);
  324. if(pathToSource.getPathArea().empty() || pathToSink.getPathArea().empty())
  325. {
  326. logGlobal->error("Cannot build river");
  327. return;
  328. }
  329. //delta placement
  330. auto deltaPos = pathToSink.getPathArea() * deltaSink;
  331. if(!deltaPos.empty())
  332. {
  333. assert(deltaPos.getTilesVector().size() == 1);
  334. auto pos = deltaPos.getTilesVector().front();
  335. auto handler = VLC->objtypeh->getHandlerFor(RIVER_DELTA_ID, RIVER_DELTA_SUBTYPE);
  336. assert(handler->isStaticObject());
  337. std::vector<std::shared_ptr<const ObjectTemplate>> tmplates;
  338. for(auto & temp : handler->getTemplates())
  339. {
  340. if(temp->canBePlacedAt(zone.getTerrainType()))
  341. tmplates.push_back(temp);
  342. }
  343. if(tmplates.size() > 3)
  344. {
  345. if(tmplates.size() % 4 != 0)
  346. throw rmgException(boost::to_string(boost::format("River templates for (%d,%d) at terrain %s, river %s are incorrect") % RIVER_DELTA_ID % RIVER_DELTA_SUBTYPE % zone.getTerrainType() % river));
  347. std::string targetTemplateName = RIVER_DELTA_TEMPLATE_NAME.at(river) + std::to_string(deltaOrientations[pos]) + ".def";
  348. for(auto & templ : tmplates)
  349. {
  350. if(templ->animationFile == targetTemplateName)
  351. {
  352. auto obj = handler->create(templ);
  353. rmg::Object deltaObj(*obj, deltaPositions[pos]);
  354. deltaObj.finalize(map);
  355. }
  356. }
  357. }
  358. }
  359. rivers.unite(pathToSource.getPathArea());
  360. rivers.unite(pathToSink.getPathArea());
  361. }