RiverPlacer.cpp 9.4 KB

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