TownPlacer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "../../mapObjectConstructors/AObjectTypeHandler.h"
  15. #include "../../mapObjectConstructors/CObjectClassesHandler.h"
  16. #include "../../mapObjects/CGTownInstance.h"
  17. #include "../../mapping/CMap.h"
  18. #include "../../mapping/CMapEditManager.h"
  19. #include "../../spells/CSpellHandler.h" //for choosing random spells
  20. #include "../RmgPath.h"
  21. #include "../RmgObject.h"
  22. #include "ObjectManager.h"
  23. #include "../Functions.h"
  24. #include "RoadPlacer.h"
  25. #include "MinePlacer.h"
  26. #include "WaterAdopter.h"
  27. #include "../TileInfo.h"
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. void TownPlacer::process()
  30. {
  31. auto * manager = zone.getModificator<ObjectManager>();
  32. if(!manager)
  33. {
  34. logGlobal->error("ObjectManager doesn't exist for zone %d, skip modificator %s", zone.getId(), getName());
  35. return;
  36. }
  37. placeTowns(*manager);
  38. }
  39. void TownPlacer::init()
  40. {
  41. POSTFUNCTION(MinePlacer);
  42. POSTFUNCTION(RoadPlacer);
  43. }
  44. void TownPlacer::placeTowns(ObjectManager & manager)
  45. {
  46. if(zone.getOwner() && ((zone.getType() == ETemplateZoneType::CPU_START) || (zone.getType() == ETemplateZoneType::PLAYER_START)))
  47. {
  48. //set zone types to player faction, generate main town
  49. logGlobal->info("Preparing playing zone");
  50. int player_id = *zone.getOwner() - 1;
  51. const auto & playerSettings = map.getMapGenOptions().getPlayersSettings();
  52. PlayerColor player;
  53. if (playerSettings.size() > player_id)
  54. {
  55. const auto & currentPlayerSettings = std::next(playerSettings.begin(), player_id);
  56. player = currentPlayerSettings->first;
  57. zone.setTownType(currentPlayerSettings->second.getStartingTown());
  58. if(zone.getTownType() == FactionID::RANDOM)
  59. zone.setTownType(getRandomTownType(true));
  60. }
  61. else //no player - randomize town
  62. {
  63. player = PlayerColor::NEUTRAL;
  64. zone.setTownType(getRandomTownType());
  65. }
  66. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, zone.getTownType());
  67. CGTownInstance * town = dynamic_cast<CGTownInstance *>(townFactory->create(map.mapInstance->cb, nullptr));
  68. town->tempOwner = player;
  69. town->builtBuildings.insert(BuildingID::FORT);
  70. town->builtBuildings.insert(BuildingID::DEFAULT);
  71. for(auto spellID : VLC->spellh->getDefaultAllowed()) //add all regular spells to town
  72. town->possibleSpells.push_back(spellID);
  73. auto position = placeMainTown(manager, *town);
  74. totalTowns++;
  75. //register MAIN town of zone only
  76. map.registerZone(town->getFaction());
  77. if(player.isValidPlayer()) //configure info for owning player
  78. {
  79. logGlobal->trace("Fill player info %d", player_id);
  80. // Update player info
  81. auto & playerInfo = map.getPlayer(player.getNum());
  82. playerInfo.allowedFactions.clear();
  83. playerInfo.allowedFactions.insert(zone.getTownType());
  84. playerInfo.hasMainTown = true;
  85. playerInfo.posOfMainTown = position;
  86. playerInfo.generateHeroAtMainTown = true;
  87. //now create actual towns
  88. addNewTowns(zone.getPlayerTowns().getCastleCount() - 1, true, player, manager);
  89. addNewTowns(zone.getPlayerTowns().getTownCount(), false, player, manager);
  90. }
  91. else
  92. {
  93. addNewTowns(zone.getPlayerTowns().getCastleCount() - 1, true, PlayerColor::NEUTRAL, manager);
  94. addNewTowns(zone.getPlayerTowns().getTownCount(), false, PlayerColor::NEUTRAL, manager);
  95. }
  96. }
  97. else //randomize town types for any other zones as well
  98. {
  99. zone.setTownType(getRandomTownType());
  100. }
  101. addNewTowns(zone.getNeutralTowns().getCastleCount(), true, PlayerColor::NEUTRAL, manager);
  102. addNewTowns(zone.getNeutralTowns().getTownCount(), false, PlayerColor::NEUTRAL, manager);
  103. if(!totalTowns) //if there's no town present, get random faction for dwellings and pandoras
  104. {
  105. //25% chance for neutral
  106. if (zone.getRand().nextInt(1, 100) <= 25)
  107. {
  108. zone.setTownType(ETownType::NEUTRAL);
  109. }
  110. else
  111. {
  112. if(!zone.getTownTypes().empty())
  113. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getTownTypes(), zone.getRand()));
  114. else if(!zone.getMonsterTypes().empty())
  115. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getMonsterTypes(), zone.getRand())); //this happens in Clash of Dragons in treasure zones, where all towns are banned
  116. else //just in any case
  117. zone.setTownType(getRandomTownType());
  118. }
  119. }
  120. }
  121. int3 TownPlacer::placeMainTown(ObjectManager & manager, CGTownInstance & town)
  122. {
  123. //towns are big objects and should be centered around visitable position
  124. rmg::Object rmgObject(town);
  125. rmgObject.setTemplate(zone.getTerrainType(), zone.getRand());
  126. int3 position(-1, -1, -1);
  127. {
  128. Zone::Lock lock(zone.areaMutex);
  129. position = manager.findPlaceForObject(zone.areaPossible().get(), rmgObject, [this](const int3& t)
  130. {
  131. float distance = zone.getPos().dist2dSQ(t);
  132. return 100000.f - distance; //some big number
  133. }, ObjectManager::OptimizeType::WEIGHT);
  134. }
  135. rmgObject.setPosition(position + int3(2, 2, 0)); //place visitable tile in the exact center of a zone
  136. manager.placeObject(rmgObject, false, true, true);
  137. cleanupBoundaries(rmgObject);
  138. zone.setPos(rmgObject.getVisitablePosition()); //roads lead to main town
  139. return position;
  140. }
  141. void TownPlacer::cleanupBoundaries(const rmg::Object & rmgObject)
  142. {
  143. Zone::Lock lock(zone.areaMutex);
  144. for(const auto & t : rmgObject.getArea().getBorderOutside())
  145. {
  146. if (t.y > rmgObject.getVisitablePosition().y) //Line below the town
  147. {
  148. if (map.isOnMap(t))
  149. {
  150. map.setOccupied(t, ETileType::FREE);
  151. zone.areaPossible()->erase(t);
  152. zone.freePaths()->add(t);
  153. }
  154. }
  155. }
  156. }
  157. void TownPlacer::addNewTowns(int count, bool hasFort, const PlayerColor & player, ObjectManager & manager)
  158. {
  159. for(int i = 0; i < count; i++)
  160. {
  161. FactionID subType = zone.getTownType();
  162. if(totalTowns>0)
  163. {
  164. if(!zone.areTownsSameType())
  165. {
  166. if(!zone.getTownTypes().empty())
  167. subType = *RandomGeneratorUtil::nextItem(zone.getTownTypes(), zone.getRand());
  168. else
  169. subType = *RandomGeneratorUtil::nextItem(zone.getDefaultTownTypes(), zone.getRand()); //it is possible to have zone with no towns allowed
  170. }
  171. }
  172. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, subType);
  173. auto * town = dynamic_cast<CGTownInstance *>(townFactory->create(map.mapInstance->cb, nullptr));
  174. town->ID = Obj::TOWN;
  175. town->tempOwner = player;
  176. if (hasFort)
  177. town->builtBuildings.insert(BuildingID::FORT);
  178. town->builtBuildings.insert(BuildingID::DEFAULT);
  179. for(auto spellID : VLC->spellh->getDefaultAllowed()) //add all regular spells to town
  180. town->possibleSpells.push_back(spellID);
  181. if(totalTowns <= 0)
  182. {
  183. //FIXME: discovered bug with small zones - getPos is close to map boarder and we have outOfMap exception
  184. //register MAIN town of zone
  185. map.registerZone(town->getFaction());
  186. //first town in zone goes in the middle
  187. placeMainTown(manager, *town);
  188. }
  189. else
  190. {
  191. manager.addRequiredObject(RequiredObjectInfo(town, 0, true));
  192. }
  193. totalTowns++;
  194. }
  195. }
  196. FactionID TownPlacer::getRandomTownType(bool matchUndergroundType)
  197. {
  198. auto townTypesAllowed = (!zone.getTownTypes().empty() ? zone.getTownTypes() : zone.getDefaultTownTypes());
  199. if(matchUndergroundType)
  200. {
  201. std::set<FactionID> townTypesVerify;
  202. for(auto factionIdx : townTypesAllowed)
  203. {
  204. bool preferUnderground = (*VLC->townh)[factionIdx]->preferUndergroundPlacement;
  205. if(zone.isUnderground() ? preferUnderground : !preferUnderground)
  206. {
  207. townTypesVerify.insert(factionIdx);
  208. }
  209. }
  210. if(!townTypesVerify.empty())
  211. townTypesAllowed = townTypesVerify;
  212. }
  213. return *RandomGeneratorUtil::nextItem(townTypesAllowed, zone.getRand());
  214. }
  215. int TownPlacer::getTotalTowns() const
  216. {
  217. return totalTowns;
  218. }
  219. VCMI_LIB_NAMESPACE_END