TownPlacer.cpp 9.8 KB

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