TownPlacer.cpp 7.7 KB

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