RiverPlacer.cpp 9.5 KB

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