WaterProxy.cpp 11 KB

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