WaterProxy.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * WaterProxy.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 "WaterProxy.h"
  12. #include "CMapGenerator.h"
  13. #include "RmgMap.h"
  14. #include "../TerrainHandler.h"
  15. #include "../mapping/CMap.h"
  16. #include "../mapping/CMapEditManager.h"
  17. #include "../mapObjects/CObjectClassesHandler.h"
  18. #include "RmgPath.h"
  19. #include "RmgObject.h"
  20. #include "ObjectManager.h"
  21. #include "Functions.h"
  22. #include "RoadPlacer.h"
  23. #include "TreasurePlacer.h"
  24. #include "TownPlacer.h"
  25. #include "ConnectionsPlacer.h"
  26. #include "TileInfo.h"
  27. #include "WaterAdopter.h"
  28. #include "RmgArea.h"
  29. VCMI_LIB_NAMESPACE_BEGIN
  30. void WaterProxy::process()
  31. {
  32. for(const auto & t : zone.area().getTilesVector())
  33. {
  34. map.setZoneID(t, zone.getId());
  35. map.setOccupied(t, ETileType::POSSIBLE);
  36. }
  37. paintZoneTerrain(zone, generator.rand, map, zone.getTerrainType());
  38. //check terrain type
  39. for(const auto & t : zone.area().getTilesVector())
  40. {
  41. MAYBE_UNUSED(t);
  42. assert(map.isOnMap(t));
  43. assert(map.map().getTile(t).terType->getId() == zone.getTerrainType());
  44. }
  45. for(const auto & z : map.getZones())
  46. {
  47. if(z.second->getId() == zone.getId())
  48. continue;
  49. for(const auto & t : z.second->area().getTilesVector())
  50. {
  51. if(map.map().getTile(t).terType->getId() == zone.getTerrainType())
  52. {
  53. z.second->areaPossible().erase(t);
  54. z.second->area().erase(t);
  55. zone.area().add(t);
  56. zone.areaPossible().add(t);
  57. map.setZoneID(t, zone.getId());
  58. map.setOccupied(t, ETileType::POSSIBLE);
  59. }
  60. }
  61. }
  62. if(!zone.area().contains(zone.getPos()))
  63. {
  64. zone.setPos(zone.area().getTilesVector().front());
  65. }
  66. zone.initFreeTiles();
  67. collectLakes();
  68. }
  69. void WaterProxy::init()
  70. {
  71. for(auto & z : map.getZones())
  72. {
  73. dependency(z.second->getModificator<TownPlacer>());
  74. dependency(z.second->getModificator<WaterAdopter>());
  75. postfunction(z.second->getModificator<ConnectionsPlacer>());
  76. postfunction(z.second->getModificator<ObjectManager>());
  77. }
  78. POSTFUNCTION(TreasurePlacer);
  79. }
  80. const std::vector<WaterProxy::Lake> & WaterProxy::getLakes() const
  81. {
  82. return lakes;
  83. }
  84. void WaterProxy::collectLakes()
  85. {
  86. int lakeId = 0;
  87. for(const auto & lake : connectedAreas(zone.getArea(), true))
  88. {
  89. lakes.push_back(Lake{});
  90. lakes.back().area = lake;
  91. lakes.back().distanceMap = lake.computeDistanceMap(lakes.back().reverseDistanceMap);
  92. for(const auto & t : lake.getBorderOutside())
  93. if(map.isOnMap(t))
  94. lakes.back().neighbourZones[map.getZoneID(t)].add(t);
  95. for(const auto & t : lake.getTiles())
  96. lakeMap[t] = lakeId;
  97. //each lake must have at least one free tile
  98. if(!lake.overlap(zone.freePaths()))
  99. zone.freePaths().add(*lakes.back().reverseDistanceMap[lakes.back().reverseDistanceMap.size() - 1].begin());
  100. ++lakeId;
  101. }
  102. }
  103. RouteInfo WaterProxy::waterRoute(Zone & dst)
  104. {
  105. RouteInfo result;
  106. auto * adopter = dst.getModificator<WaterAdopter>();
  107. if(!adopter)
  108. return result;
  109. if(adopter->getCoastTiles().empty())
  110. return result;
  111. //block zones are not connected by template
  112. for(auto& lake : lakes)
  113. {
  114. if(lake.neighbourZones.count(dst.getId()))
  115. {
  116. if(!lake.keepConnections.count(dst.getId()))
  117. {
  118. for(const auto & ct : lake.neighbourZones[dst.getId()].getTiles())
  119. {
  120. if(map.isPossible(ct))
  121. map.setOccupied(ct, ETileType::BLOCKED);
  122. }
  123. dst.areaPossible().subtract(lake.neighbourZones[dst.getId()]);
  124. continue;
  125. }
  126. //Don't place shipyard or boats on the very small lake
  127. if (lake.area.getTiles().size() < 25)
  128. {
  129. logGlobal->info("Skipping very small lake at zone %d", dst.getId());
  130. continue;
  131. }
  132. int zoneTowns = 0;
  133. if(auto * m = dst.getModificator<TownPlacer>())
  134. zoneTowns = m->getTotalTowns();
  135. if(dst.getType() == ETemplateZoneType::PLAYER_START || dst.getType() == ETemplateZoneType::CPU_START || zoneTowns)
  136. {
  137. if(placeShipyard(dst, lake, generator.getConfig().shipyardGuard, result))
  138. {
  139. logGlobal->info("Shipyard successfully placed at zone %d", dst.getId());
  140. }
  141. else
  142. {
  143. logGlobal->warn("Shipyard placement failed, trying boat at zone %d", dst.getId());
  144. if(placeBoat(dst, lake, result))
  145. {
  146. logGlobal->warn("Boat successfully placed at zone %d", dst.getId());
  147. }
  148. else
  149. {
  150. logGlobal->error("Boat placement failed at zone %d", dst.getId());
  151. }
  152. }
  153. }
  154. else
  155. {
  156. if(placeBoat(dst, lake, result))
  157. {
  158. logGlobal->info("Boat successfully placed at zone %d", dst.getId());
  159. }
  160. else
  161. {
  162. logGlobal->error("Boat placement failed at zone %d", dst.getId());
  163. }
  164. }
  165. }
  166. }
  167. return result;
  168. }
  169. bool WaterProxy::waterKeepConnection(TRmgTemplateZoneId zoneA, TRmgTemplateZoneId zoneB)
  170. {
  171. for(auto & lake : lakes)
  172. {
  173. if(lake.neighbourZones.count(zoneA) && lake.neighbourZones.count(zoneB))
  174. {
  175. lake.keepConnections.insert(zoneA);
  176. lake.keepConnections.insert(zoneB);
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. bool WaterProxy::placeBoat(Zone & land, const Lake & lake, RouteInfo & info)
  183. {
  184. auto * manager = zone.getModificator<ObjectManager>();
  185. if(!manager)
  186. return false;
  187. auto subObjects = VLC->objtypeh->knownSubObjects(Obj::BOAT);
  188. auto * boat = dynamic_cast<CGBoat *>(VLC->objtypeh->getHandlerFor(Obj::BOAT, *RandomGeneratorUtil::nextItem(subObjects, generator.rand))->create());
  189. rmg::Object rmgObject(*boat);
  190. rmgObject.setTemplate(zone.getTerrainType());
  191. auto waterAvailable = zone.areaPossible() + zone.freePaths();
  192. rmg::Area coast = lake.neighbourZones.at(land.getId()); //having land tiles
  193. coast.intersect(land.areaPossible() + land.freePaths()); //having only available land tiles
  194. auto boardingPositions = coast.getSubarea([&waterAvailable](const int3 & tile) //tiles where boarding is possible
  195. {
  196. rmg::Area a({tile});
  197. a = a.getBorderOutside();
  198. a.intersect(waterAvailable);
  199. return !a.empty();
  200. });
  201. while(!boardingPositions.empty())
  202. {
  203. auto boardingPosition = *boardingPositions.getTiles().begin();
  204. rmg::Area shipPositions({boardingPosition});
  205. auto boutside = shipPositions.getBorderOutside();
  206. shipPositions.assign(boutside);
  207. shipPositions.intersect(waterAvailable);
  208. if(shipPositions.empty())
  209. {
  210. boardingPositions.erase(boardingPosition);
  211. continue;
  212. }
  213. //try to place boat at water, create paths on water and land
  214. auto path = manager->placeAndConnectObject(shipPositions, rmgObject, 2, false, true, ObjectManager::OptimizeType::NONE);
  215. auto landPath = land.searchPath(boardingPosition, false);
  216. if(!path.valid() || !landPath.valid())
  217. {
  218. boardingPositions.erase(boardingPosition);
  219. continue;
  220. }
  221. info.blocked = rmgObject.getArea();
  222. info.visitable = rmgObject.getVisitablePosition();
  223. info.boarding = boardingPosition;
  224. info.water = shipPositions;
  225. zone.connectPath(path);
  226. land.connectPath(landPath);
  227. manager->placeObject(rmgObject, false, true);
  228. break;
  229. }
  230. return !boardingPositions.empty();
  231. }
  232. bool WaterProxy::placeShipyard(Zone & land, const Lake & lake, si32 guard, RouteInfo & info)
  233. {
  234. auto * manager = land.getModificator<ObjectManager>();
  235. if(!manager)
  236. return false;
  237. int subtype = chooseRandomAppearance(generator.rand, Obj::SHIPYARD, land.getTerrainType());
  238. auto * shipyard = dynamic_cast<CGShipyard *>(VLC->objtypeh->getHandlerFor(Obj::SHIPYARD, subtype)->create());
  239. shipyard->tempOwner = PlayerColor::NEUTRAL;
  240. rmg::Object rmgObject(*shipyard);
  241. rmgObject.setTemplate(land.getTerrainType());
  242. bool guarded = manager->addGuard(rmgObject, guard);
  243. auto waterAvailable = zone.areaPossible() + zone.freePaths();
  244. waterAvailable.intersect(lake.area);
  245. rmg::Area coast = lake.neighbourZones.at(land.getId()); //having land tiles
  246. coast.intersect(land.areaPossible() + land.freePaths()); //having only available land tiles
  247. auto boardingPositions = coast.getSubarea([&waterAvailable](const int3 & tile) //tiles where boarding is possible
  248. {
  249. rmg::Area a({tile});
  250. a = a.getBorderOutside();
  251. a.intersect(waterAvailable);
  252. return !a.empty();
  253. });
  254. while(!boardingPositions.empty())
  255. {
  256. auto boardingPosition = *boardingPositions.getTiles().begin();
  257. rmg::Area shipPositions({boardingPosition});
  258. auto boutside = shipPositions.getBorderOutside();
  259. shipPositions.assign(boutside);
  260. shipPositions.intersect(waterAvailable);
  261. if(shipPositions.empty())
  262. {
  263. boardingPositions.erase(boardingPosition);
  264. continue;
  265. }
  266. //try to place shipyard close to boarding position and appropriate water access
  267. auto path = manager->placeAndConnectObject(land.areaPossible(), rmgObject, [&rmgObject, &shipPositions, &boardingPosition](const int3 & tile)
  268. {
  269. //Must only check the border of shipyard and not the added guard
  270. rmg::Area shipyardOut = rmgObject.instances().front()->getBlockedArea().getBorderOutside();
  271. if(!shipyardOut.contains(boardingPosition) || (shipyardOut * shipPositions).empty())
  272. return -1.f;
  273. return 1.0f;
  274. }, guarded, true, ObjectManager::OptimizeType::NONE);
  275. //search path to boarding position
  276. auto searchArea = land.areaPossible() - rmgObject.getArea();
  277. rmg::Path pathToBoarding(searchArea);
  278. pathToBoarding.connect(land.freePaths());
  279. pathToBoarding.connect(path);
  280. pathToBoarding = pathToBoarding.search(boardingPosition, false);
  281. //make sure shipyard places ship at position we defined
  282. rmg::Area shipyardOutToBlock(rmgObject.getArea().getBorderOutside());
  283. shipyardOutToBlock.intersect(waterAvailable);
  284. shipyardOutToBlock.subtract(shipPositions);
  285. shipPositions.subtract(shipyardOutToBlock);
  286. auto pathToBoat = zone.searchPath(shipPositions, true);
  287. if(!path.valid() || !pathToBoarding.valid() || !pathToBoat.valid())
  288. {
  289. boardingPositions.erase(boardingPosition);
  290. continue;
  291. }
  292. land.connectPath(path);
  293. land.connectPath(pathToBoarding);
  294. zone.connectPath(pathToBoat);
  295. info.blocked = rmgObject.getArea();
  296. info.visitable = rmgObject.getVisitablePosition();
  297. info.boarding = boardingPosition;
  298. info.water = shipPositions;
  299. manager->placeObject(rmgObject, guarded, true);
  300. zone.areaPossible().subtract(shipyardOutToBlock);
  301. for(const auto & i : shipyardOutToBlock.getTilesVector())
  302. if(map.isOnMap(i) && map.isPossible(i))
  303. map.setOccupied(i, ETileType::BLOCKED);
  304. break;
  305. }
  306. return !boardingPositions.empty();
  307. }
  308. char WaterProxy::dump(const int3 & t)
  309. {
  310. auto lakeIter = lakeMap.find(t);
  311. if(lakeIter == lakeMap.end())
  312. return '?';
  313. Lake & lake = lakes[lakeMap.at(t)];
  314. for(const auto & i : lake.neighbourZones)
  315. {
  316. if(i.second.contains(t))
  317. return lake.keepConnections.count(i.first) ? std::to_string(i.first)[0] : '=';
  318. }
  319. return '~';
  320. }
  321. VCMI_LIB_NAMESPACE_END