RiverPlacer.cpp 9.3 KB

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