WaterProxy.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. int zoneTowns = 0;
  127. if(auto * m = dst.getModificator<TownPlacer>())
  128. zoneTowns = m->getTotalTowns();
  129. if(dst.getType() == ETemplateZoneType::PLAYER_START || dst.getType() == ETemplateZoneType::CPU_START || zoneTowns)
  130. {
  131. if(placeShipyard(dst, lake, generator.getConfig().shipyardGuard, result))
  132. {
  133. logGlobal->info("Shipyard successfully placed at zone %d", dst.getId());
  134. }
  135. else
  136. {
  137. logGlobal->warn("Shipyard placement failed, trying boat at zone %d", dst.getId());
  138. if(placeBoat(dst, lake, result))
  139. {
  140. logGlobal->warn("Boat successfully placed at zone %d", dst.getId());
  141. }
  142. else
  143. {
  144. logGlobal->error("Boat placement failed at zone %d", dst.getId());
  145. }
  146. }
  147. }
  148. else
  149. {
  150. if(placeBoat(dst, lake, result))
  151. {
  152. logGlobal->info("Boat successfully placed at zone %d", dst.getId());
  153. }
  154. else
  155. {
  156. logGlobal->error("Boat placement failed at zone %d", dst.getId());
  157. }
  158. }
  159. }
  160. }
  161. return result;
  162. }
  163. bool WaterProxy::waterKeepConnection(TRmgTemplateZoneId zoneA, TRmgTemplateZoneId zoneB)
  164. {
  165. for(auto & lake : lakes)
  166. {
  167. if(lake.neighbourZones.count(zoneA) && lake.neighbourZones.count(zoneB))
  168. {
  169. lake.keepConnections.insert(zoneA);
  170. lake.keepConnections.insert(zoneB);
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. bool WaterProxy::placeBoat(Zone & land, const Lake & lake, RouteInfo & info)
  177. {
  178. auto * manager = zone.getModificator<ObjectManager>();
  179. if(!manager)
  180. return false;
  181. auto subObjects = VLC->objtypeh->knownSubObjects(Obj::BOAT);
  182. auto * boat = dynamic_cast<CGBoat *>(VLC->objtypeh->getHandlerFor(Obj::BOAT, *RandomGeneratorUtil::nextItem(subObjects, generator.rand))->create());
  183. rmg::Object rmgObject(*boat);
  184. rmgObject.setTemplate(zone.getTerrainType());
  185. auto waterAvailable = zone.areaPossible() + zone.freePaths();
  186. rmg::Area coast = lake.neighbourZones.at(land.getId()); //having land tiles
  187. coast.intersect(land.areaPossible() + land.freePaths()); //having only available land tiles
  188. auto boardingPositions = coast.getSubarea([&waterAvailable](const int3 & tile) //tiles where boarding is possible
  189. {
  190. rmg::Area a({tile});
  191. a = a.getBorderOutside();
  192. a.intersect(waterAvailable);
  193. return !a.empty();
  194. });
  195. while(!boardingPositions.empty())
  196. {
  197. auto boardingPosition = *boardingPositions.getTiles().begin();
  198. rmg::Area shipPositions({boardingPosition});
  199. auto boutside = shipPositions.getBorderOutside();
  200. shipPositions.assign(boutside);
  201. shipPositions.intersect(waterAvailable);
  202. if(shipPositions.empty())
  203. {
  204. boardingPositions.erase(boardingPosition);
  205. continue;
  206. }
  207. //try to place boat at water, create paths on water and land
  208. auto path = manager->placeAndConnectObject(shipPositions, rmgObject, 2, false, true, ObjectManager::OptimizeType::NONE);
  209. auto landPath = land.searchPath(boardingPosition, false);
  210. if(!path.valid() || !landPath.valid())
  211. {
  212. boardingPositions.erase(boardingPosition);
  213. continue;
  214. }
  215. info.blocked = rmgObject.getArea();
  216. info.visitable = rmgObject.getVisitablePosition();
  217. info.boarding = boardingPosition;
  218. info.water = shipPositions;
  219. zone.connectPath(path);
  220. land.connectPath(landPath);
  221. manager->placeObject(rmgObject, false, true);
  222. break;
  223. }
  224. return !boardingPositions.empty();
  225. }
  226. bool WaterProxy::placeShipyard(Zone & land, const Lake & lake, si32 guard, RouteInfo & info)
  227. {
  228. auto * manager = land.getModificator<ObjectManager>();
  229. if(!manager)
  230. return false;
  231. int subtype = chooseRandomAppearance(generator.rand, Obj::SHIPYARD, land.getTerrainType());
  232. auto * shipyard = dynamic_cast<CGShipyard *>(VLC->objtypeh->getHandlerFor(Obj::SHIPYARD, subtype)->create());
  233. shipyard->tempOwner = PlayerColor::NEUTRAL;
  234. rmg::Object rmgObject(*shipyard);
  235. rmgObject.setTemplate(land.getTerrainType());
  236. bool guarded = manager->addGuard(rmgObject, guard);
  237. auto waterAvailable = zone.areaPossible() + zone.freePaths();
  238. waterAvailable.intersect(lake.area);
  239. rmg::Area coast = lake.neighbourZones.at(land.getId()); //having land tiles
  240. coast.intersect(land.areaPossible() + land.freePaths()); //having only available land tiles
  241. auto boardingPositions = coast.getSubarea([&waterAvailable](const int3 & tile) //tiles where boarding is possible
  242. {
  243. rmg::Area a({tile});
  244. a = a.getBorderOutside();
  245. a.intersect(waterAvailable);
  246. return !a.empty();
  247. });
  248. while(!boardingPositions.empty())
  249. {
  250. auto boardingPosition = *boardingPositions.getTiles().begin();
  251. rmg::Area shipPositions({boardingPosition});
  252. auto boutside = shipPositions.getBorderOutside();
  253. shipPositions.assign(boutside);
  254. shipPositions.intersect(waterAvailable);
  255. if(shipPositions.empty())
  256. {
  257. boardingPositions.erase(boardingPosition);
  258. continue;
  259. }
  260. //try to place shipyard close to boarding position and appropriate water access
  261. auto path = manager->placeAndConnectObject(land.areaPossible(), rmgObject, [&rmgObject, &shipPositions, &boardingPosition](const int3 & tile)
  262. {
  263. //Must only check the border of shipyard and not the added guard
  264. rmg::Area shipyardOut = rmgObject.instances().front()->getBlockedArea().getBorderOutside();
  265. if(!shipyardOut.contains(boardingPosition) || (shipyardOut * shipPositions).empty())
  266. return -1.f;
  267. return 1.0f;
  268. }, guarded, true, ObjectManager::OptimizeType::NONE);
  269. //search path to boarding position
  270. auto searchArea = land.areaPossible() - rmgObject.getArea();
  271. rmg::Path pathToBoarding(searchArea);
  272. pathToBoarding.connect(land.freePaths());
  273. pathToBoarding.connect(path);
  274. pathToBoarding = pathToBoarding.search(boardingPosition, false);
  275. //make sure shipyard places ship at position we defined
  276. rmg::Area shipyardOutToBlock(rmgObject.getArea().getBorderOutside());
  277. shipyardOutToBlock.intersect(waterAvailable);
  278. shipyardOutToBlock.subtract(shipPositions);
  279. shipPositions.subtract(shipyardOutToBlock);
  280. auto pathToBoat = zone.searchPath(shipPositions, true);
  281. if(!path.valid() || !pathToBoarding.valid() || !pathToBoat.valid())
  282. {
  283. boardingPositions.erase(boardingPosition);
  284. continue;
  285. }
  286. land.connectPath(path);
  287. land.connectPath(pathToBoarding);
  288. zone.connectPath(pathToBoat);
  289. info.blocked = rmgObject.getArea();
  290. info.visitable = rmgObject.getVisitablePosition();
  291. info.boarding = boardingPosition;
  292. info.water = shipPositions;
  293. manager->placeObject(rmgObject, guarded, true);
  294. zone.areaPossible().subtract(shipyardOutToBlock);
  295. for(const auto & i : shipyardOutToBlock.getTilesVector())
  296. if(map.isOnMap(i) && map.isPossible(i))
  297. map.setOccupied(i, ETileType::BLOCKED);
  298. break;
  299. }
  300. return !boardingPositions.empty();
  301. }
  302. char WaterProxy::dump(const int3 & t)
  303. {
  304. auto lakeIter = lakeMap.find(t);
  305. if(lakeIter == lakeMap.end())
  306. return '?';
  307. Lake & lake = lakes[lakeMap.at(t)];
  308. for(const auto & i : lake.neighbourZones)
  309. {
  310. if(i.second.contains(t))
  311. return lake.keepConnections.count(i.first) ? std::to_string(i.first)[0] : '=';
  312. }
  313. return '~';
  314. }
  315. VCMI_LIB_NAMESPACE_END