RiverPlacer.cpp 9.4 KB

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