TownPlacer.cpp 7.7 KB

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