RiverPlacer.cpp 9.1 KB

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