WaterProxy.cpp 10 KB

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