WaterProxy.cpp 10 KB

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