RiverPlacer.cpp 9.3 KB

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