RiverPlacer.cpp 9.1 KB

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