TownPlacer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * TownPlacer.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 "TownPlacer.h"
  12. #include "../CMapGenerator.h"
  13. #include "../RmgMap.h"
  14. #include "../../entities/faction/CTownHandler.h"
  15. #include "../../mapObjectConstructors/AObjectTypeHandler.h"
  16. #include "../../mapObjectConstructors/CObjectClassesHandler.h"
  17. #include "../../mapObjects/CGTownInstance.h"
  18. #include "../../mapping/CMap.h"
  19. #include "../../mapping/CMapEditManager.h"
  20. #include "../../spells/CSpellHandler.h" //for choosing random spells
  21. #include "../RmgPath.h"
  22. #include "../RmgObject.h"
  23. #include "ObjectManager.h"
  24. #include "../Functions.h"
  25. #include "RoadPlacer.h"
  26. #include "MinePlacer.h"
  27. #include "WaterAdopter.h"
  28. #include "../TileInfo.h"
  29. #include <vstd/RNG.h>
  30. VCMI_LIB_NAMESPACE_BEGIN
  31. void TownPlacer::process()
  32. {
  33. auto * manager = zone.getModificator<ObjectManager>();
  34. if(!manager)
  35. {
  36. logGlobal->error("ObjectManager doesn't exist for zone %d, skip modificator %s", zone.getId(), getName());
  37. return;
  38. }
  39. placeTowns(*manager);
  40. }
  41. void TownPlacer::init()
  42. {
  43. for(auto & townHint : zone.getTownHints())
  44. {
  45. logGlobal->info("Town hint of zone %d: %d", zone.getId(), townHint.likeZone);
  46. if(townHint.likeZone != rmg::ZoneOptions::NO_ZONE)
  47. {
  48. logGlobal->info("Dependency on town type of zone %d", townHint.likeZone);
  49. dependency(map.getZones().at(townHint.likeZone)->getModificator<TownPlacer>());
  50. }
  51. else if(!townHint.notLikeZone.empty())
  52. {
  53. for(auto zoneId : townHint.notLikeZone)
  54. {
  55. logGlobal->info("Dependency on town unlike type of zone %d", zoneId);
  56. dependency(map.getZones().at(zoneId)->getModificator<TownPlacer>());
  57. }
  58. }
  59. else if(townHint.relatedToZoneTerrain != rmg::ZoneOptions::NO_ZONE)
  60. {
  61. logGlobal->info("Dependency on town related to zone terrain of zone %d", townHint.relatedToZoneTerrain);
  62. dependency(map.getZones().at(townHint.relatedToZoneTerrain)->getModificator<TownPlacer>());
  63. }
  64. }
  65. POSTFUNCTION(MinePlacer);
  66. POSTFUNCTION(RoadPlacer);
  67. }
  68. void TownPlacer::placeTowns(ObjectManager & manager)
  69. {
  70. // TODO: Configurew each subseqquent town based on townHints
  71. if(zone.getOwner() && ((zone.getType() == ETemplateZoneType::CPU_START) || (zone.getType() == ETemplateZoneType::PLAYER_START)))
  72. {
  73. //set zone types to player faction, generate main town
  74. logGlobal->info("Preparing playing zone");
  75. int player_id = *zone.getOwner() - 1;
  76. const auto & playerSettings = map.getMapGenOptions().getPlayersSettings();
  77. PlayerColor player;
  78. if (playerSettings.size() > player_id)
  79. {
  80. const auto & currentPlayerSettings = std::next(playerSettings.begin(), player_id);
  81. player = currentPlayerSettings->first;
  82. zone.setTownType(currentPlayerSettings->second.getStartingTown());
  83. if(zone.getTownType() == FactionID::RANDOM)
  84. zone.setTownType(getRandomTownType(true));
  85. }
  86. else //no player - randomize town
  87. {
  88. player = PlayerColor::NEUTRAL;
  89. zone.setTownType(getTownTypeFromHint(0));
  90. }
  91. auto townFactory = LIBRARY->objtypeh->getHandlerFor(Obj::TOWN, zone.getTownType());
  92. auto town = std::dynamic_pointer_cast<CGTownInstance>(townFactory->create(map.mapInstance->cb, nullptr));
  93. town->tempOwner = player;
  94. town->addBuilding(BuildingID::FORT);
  95. town->addBuilding(BuildingID::DEFAULT);
  96. for(auto spellID : LIBRARY->spellh->getDefaultAllowed()) //add all regular spells to town
  97. town->possibleSpells.push_back(spellID);
  98. auto position = placeMainTown(manager, town);
  99. totalTowns++;
  100. //register MAIN town of zone only
  101. map.registerZone(town->getFactionID());
  102. if(player.isValidPlayer()) //configure info for owning player
  103. {
  104. logGlobal->trace("Fill player info %d", player_id);
  105. // Update player info
  106. auto & playerInfo = map.getPlayer(player.getNum());
  107. playerInfo.allowedFactions.clear();
  108. playerInfo.allowedFactions.insert(zone.getTownType());
  109. playerInfo.hasMainTown = true;
  110. playerInfo.posOfMainTown = position;
  111. playerInfo.generateHeroAtMainTown = true;
  112. //now create actual towns
  113. addNewTowns(zone.getPlayerTowns().getCastleCount() - 1, true, player, manager);
  114. addNewTowns(zone.getPlayerTowns().getTownCount(), false, player, manager);
  115. }
  116. else
  117. {
  118. addNewTowns(zone.getPlayerTowns().getCastleCount() - 1, true, PlayerColor::NEUTRAL, manager);
  119. addNewTowns(zone.getPlayerTowns().getTownCount(), false, PlayerColor::NEUTRAL, manager);
  120. }
  121. }
  122. else //randomize town types for non-player zones
  123. {
  124. zone.setTownType(getTownTypeFromHint(0));
  125. }
  126. addNewTowns(zone.getNeutralTowns().getCastleCount(), true, PlayerColor::NEUTRAL, manager);
  127. addNewTowns(zone.getNeutralTowns().getTownCount(), false, PlayerColor::NEUTRAL, manager);
  128. if(!totalTowns) //if there's no town present, get random faction for dwellings and pandoras
  129. {
  130. // TODO: Use townHints also when there are no towns in zone
  131. //25% chance for neutral
  132. if (zone.getRand().nextInt(1, 100) <= 25)
  133. {
  134. zone.setTownType(ETownType::NEUTRAL);
  135. }
  136. else
  137. {
  138. if(!zone.getTownTypes().empty())
  139. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getTownTypes(), zone.getRand()));
  140. else if(!zone.getMonsterTypes().empty())
  141. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getMonsterTypes(), zone.getRand())); //this happens in Clash of Dragons in treasure zones, where all towns are banned
  142. else //just in any case
  143. zone.setTownType(getRandomTownType());
  144. }
  145. }
  146. }
  147. int3 TownPlacer::placeMainTown(ObjectManager & manager, std::shared_ptr<CGTownInstance> town)
  148. {
  149. //towns are big objects and should be centered around visitable position
  150. rmg::Object rmgObject(town);
  151. rmgObject.setTemplate(zone.getTerrainType(), zone.getRand());
  152. int3 position(-1, -1, -1);
  153. {
  154. Zone::Lock lock(zone.areaMutex);
  155. position = manager.findPlaceForObject(zone.areaPossible().get(), rmgObject, [this](const int3& t)
  156. {
  157. float distance = zone.getPos().dist2dSQ(t);
  158. return 100000.f - distance; //some big number
  159. }, ObjectManager::OptimizeType::WEIGHT);
  160. }
  161. rmgObject.setPosition(position + int3(2, 2, 0)); //place visitable tile in the exact center of a zone
  162. manager.placeObject(rmgObject, false, true, true);
  163. cleanupBoundaries(rmgObject);
  164. zone.setPos(rmgObject.getVisitablePosition()); //roads lead to main town
  165. return position;
  166. }
  167. void TownPlacer::cleanupBoundaries(const rmg::Object & rmgObject)
  168. {
  169. Zone::Lock lock(zone.areaMutex);
  170. for(const auto & t : rmgObject.getArea().getBorderOutside())
  171. {
  172. if (t.y > rmgObject.getVisitablePosition().y) //Line below the town
  173. {
  174. if (map.isOnMap(t))
  175. {
  176. map.setOccupied(t, ETileType::FREE);
  177. zone.areaPossible()->erase(t);
  178. zone.freePaths()->add(t);
  179. }
  180. }
  181. }
  182. }
  183. FactionID TownPlacer::getTownTypeFromHint(size_t hintIndex)
  184. {
  185. const auto & hints = zone.getTownHints();
  186. if(hints.size() <= hintIndex)
  187. {
  188. return *RandomGeneratorUtil::nextItem(zone.getTownTypes(), zone.getRand());
  189. }
  190. const auto & townHints = hints[hintIndex];
  191. FactionID subType = zone.getTownType();
  192. if(townHints.likeZone != rmg::ZoneOptions::NO_ZONE)
  193. {
  194. // Copy directly from other zone
  195. subType = map.getZones().at(townHints.likeZone)->getTownType();
  196. }
  197. else if(!townHints.notLikeZone.empty())
  198. {
  199. // Exclude type rolled for other zone
  200. auto townTypes = zone.getTownTypes();
  201. for(auto zoneId : townHints.notLikeZone)
  202. {
  203. townTypes.erase(map.getZones().at(zoneId)->getTownType());
  204. }
  205. zone.setTownTypes(townTypes);
  206. if(!townTypes.empty())
  207. subType = *RandomGeneratorUtil::nextItem(townTypes, zone.getRand());
  208. }
  209. else if(townHints.relatedToZoneTerrain != rmg::ZoneOptions::NO_ZONE)
  210. {
  211. auto townTerrain = map.getZones().at(townHints.relatedToZoneTerrain)->getTerrainType();
  212. auto townTypesAllowed = zone.getTownTypes();
  213. vstd::erase_if(townTypesAllowed, [townTerrain](FactionID type)
  214. {
  215. return (*LIBRARY->townh)[type]->getNativeTerrain() != townTerrain;
  216. });
  217. zone.setTownTypes(townTypesAllowed);
  218. if(!townTypesAllowed.empty())
  219. subType = *RandomGeneratorUtil::nextItem(townTypesAllowed, zone.getRand());
  220. }
  221. return subType;
  222. }
  223. void TownPlacer::addNewTowns(int count, bool hasFort, const PlayerColor & player, ObjectManager & manager)
  224. {
  225. for(int i = 0; i < count; i++)
  226. {
  227. FactionID subType = zone.getTownType();
  228. if(totalTowns > 0)
  229. {
  230. if(!zone.areTownsSameType())
  231. {
  232. subType = getTownTypeFromHint(totalTowns);
  233. }
  234. }
  235. auto townFactory = LIBRARY->objtypeh->getHandlerFor(Obj::TOWN, subType);
  236. auto town = std::dynamic_pointer_cast<CGTownInstance>(townFactory->create(map.mapInstance->cb, nullptr));
  237. town->ID = Obj::TOWN;
  238. town->tempOwner = player;
  239. if (hasFort)
  240. town->addBuilding(BuildingID::FORT);
  241. town->addBuilding(BuildingID::DEFAULT);
  242. for(auto spellID : LIBRARY->spellh->getDefaultAllowed()) //add all regular spells to town
  243. town->possibleSpells.push_back(spellID);
  244. if(totalTowns <= 0)
  245. {
  246. //FIXME: discovered bug with small zones - getPos is close to map boarder and we have outOfMap exception
  247. //register MAIN town of zone
  248. map.registerZone(town->getFactionID());
  249. //first town in zone goes in the middle
  250. placeMainTown(manager, town);
  251. }
  252. else
  253. {
  254. manager.addRequiredObject(RequiredObjectInfo(town, 0, true));
  255. }
  256. totalTowns++;
  257. }
  258. }
  259. FactionID TownPlacer::getRandomTownType(bool matchUndergroundType)
  260. {
  261. auto townTypesAllowed = (!zone.getTownTypes().empty() ? zone.getTownTypes() : zone.getDefaultTownTypes());
  262. if(matchUndergroundType)
  263. {
  264. std::set<FactionID> townTypesVerify;
  265. for(auto factionIdx : townTypesAllowed)
  266. {
  267. bool preferUnderground = (*LIBRARY->townh)[factionIdx]->preferUndergroundPlacement;
  268. if(zone.isUnderground() ? preferUnderground : !preferUnderground)
  269. {
  270. townTypesVerify.insert(factionIdx);
  271. }
  272. }
  273. if(!townTypesVerify.empty())
  274. townTypesAllowed = townTypesVerify;
  275. }
  276. return *RandomGeneratorUtil::nextItem(townTypesAllowed, zone.getRand());
  277. }
  278. int TownPlacer::getTotalTowns() const
  279. {
  280. return totalTowns;
  281. }
  282. VCMI_LIB_NAMESPACE_END