TownPlacer.cpp 7.8 KB

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