WaterProxy.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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, this](const int3 & tile) //tiles where boarding is possible
  195. {
  196. //We don't want place boat right to any land object, especiallly the zone guard
  197. if (map.getTile(tile).getNearestObjectDistance() <= 3)
  198. return false;
  199. rmg::Area a({tile});
  200. a = a.getBorderOutside();
  201. a.intersect(waterAvailable);
  202. return !a.empty();
  203. });
  204. while(!boardingPositions.empty())
  205. {
  206. auto boardingPosition = *boardingPositions.getTiles().begin();
  207. rmg::Area shipPositions({boardingPosition});
  208. auto boutside = shipPositions.getBorderOutside();
  209. shipPositions.assign(boutside);
  210. shipPositions.intersect(waterAvailable);
  211. if(shipPositions.empty())
  212. {
  213. boardingPositions.erase(boardingPosition);
  214. continue;
  215. }
  216. //try to place boat at water, create paths on water and land
  217. auto path = manager->placeAndConnectObject(shipPositions, rmgObject, 4, false, true, ObjectManager::OptimizeType::NONE);
  218. auto landPath = land.searchPath(boardingPosition, false);
  219. if(!path.valid() || !landPath.valid())
  220. {
  221. boardingPositions.erase(boardingPosition);
  222. continue;
  223. }
  224. info.blocked = rmgObject.getArea();
  225. info.visitable = rmgObject.getVisitablePosition();
  226. info.boarding = boardingPosition;
  227. info.water = shipPositions;
  228. zone.connectPath(path);
  229. land.connectPath(landPath);
  230. manager->placeObject(rmgObject, false, true);
  231. land.getModificator<ObjectManager>()->updateDistances(rmgObject); //Keep land objects away from the boat
  232. break;
  233. }
  234. return !boardingPositions.empty();
  235. }
  236. bool WaterProxy::placeShipyard(Zone & land, const Lake & lake, si32 guard, RouteInfo & info)
  237. {
  238. auto * manager = land.getModificator<ObjectManager>();
  239. if(!manager)
  240. return false;
  241. int subtype = chooseRandomAppearance(generator.rand, Obj::SHIPYARD, land.getTerrainType());
  242. auto * shipyard = dynamic_cast<CGShipyard *>(VLC->objtypeh->getHandlerFor(Obj::SHIPYARD, subtype)->create());
  243. shipyard->tempOwner = PlayerColor::NEUTRAL;
  244. rmg::Object rmgObject(*shipyard);
  245. rmgObject.setTemplate(land.getTerrainType());
  246. bool guarded = manager->addGuard(rmgObject, guard);
  247. auto waterAvailable = zone.areaPossible() + zone.freePaths();
  248. waterAvailable.intersect(lake.area);
  249. rmg::Area coast = lake.neighbourZones.at(land.getId()); //having land tiles
  250. coast.intersect(land.areaPossible() + land.freePaths()); //having only available land tiles
  251. auto boardingPositions = coast.getSubarea([&waterAvailable](const int3 & tile) //tiles where boarding is possible
  252. {
  253. rmg::Area a({tile});
  254. a = a.getBorderOutside();
  255. a.intersect(waterAvailable);
  256. return !a.empty();
  257. });
  258. while(!boardingPositions.empty())
  259. {
  260. auto boardingPosition = *boardingPositions.getTiles().begin();
  261. rmg::Area shipPositions({boardingPosition});
  262. auto boutside = shipPositions.getBorderOutside();
  263. shipPositions.assign(boutside);
  264. shipPositions.intersect(waterAvailable);
  265. if(shipPositions.empty())
  266. {
  267. boardingPositions.erase(boardingPosition);
  268. continue;
  269. }
  270. //try to place shipyard close to boarding position and appropriate water access
  271. auto path = manager->placeAndConnectObject(land.areaPossible(), rmgObject, [&rmgObject, &shipPositions, &boardingPosition](const int3 & tile)
  272. {
  273. //Must only check the border of shipyard and not the added guard
  274. rmg::Area shipyardOut = rmgObject.instances().front()->getBlockedArea().getBorderOutside();
  275. if(!shipyardOut.contains(boardingPosition) || (shipyardOut * shipPositions).empty())
  276. return -1.f;
  277. return 1.0f;
  278. }, guarded, true, ObjectManager::OptimizeType::NONE);
  279. //search path to boarding position
  280. auto searchArea = land.areaPossible() - rmgObject.getArea();
  281. rmg::Path pathToBoarding(searchArea);
  282. pathToBoarding.connect(land.freePaths());
  283. pathToBoarding.connect(path);
  284. pathToBoarding = pathToBoarding.search(boardingPosition, false);
  285. //make sure shipyard places ship at position we defined
  286. rmg::Area shipyardOutToBlock(rmgObject.getArea().getBorderOutside());
  287. shipyardOutToBlock.intersect(waterAvailable);
  288. shipyardOutToBlock.subtract(shipPositions);
  289. shipPositions.subtract(shipyardOutToBlock);
  290. auto pathToBoat = zone.searchPath(shipPositions, true);
  291. if(!path.valid() || !pathToBoarding.valid() || !pathToBoat.valid())
  292. {
  293. boardingPositions.erase(boardingPosition);
  294. continue;
  295. }
  296. land.connectPath(path);
  297. land.connectPath(pathToBoarding);
  298. zone.connectPath(pathToBoat);
  299. info.blocked = rmgObject.getArea();
  300. info.visitable = rmgObject.getVisitablePosition();
  301. info.boarding = boardingPosition;
  302. info.water = shipPositions;
  303. manager->placeObject(rmgObject, guarded, true);
  304. zone.areaPossible().subtract(shipyardOutToBlock);
  305. for(const auto & i : shipyardOutToBlock.getTilesVector())
  306. if(map.isOnMap(i) && map.isPossible(i))
  307. map.setOccupied(i, ETileType::BLOCKED);
  308. break;
  309. }
  310. return !boardingPositions.empty();
  311. }
  312. char WaterProxy::dump(const int3 & t)
  313. {
  314. auto lakeIter = lakeMap.find(t);
  315. if(lakeIter == lakeMap.end())
  316. return '?';
  317. Lake & lake = lakes[lakeMap.at(t)];
  318. for(const auto & i : lake.neighbourZones)
  319. {
  320. if(i.second.contains(t))
  321. return lake.keepConnections.count(i.first) ? std::to_string(i.first)[0] : '=';
  322. }
  323. return '~';
  324. }
  325. VCMI_LIB_NAMESPACE_END