TownPlacer.cpp 7.6 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. 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. auto& playerInfo = map.getPlayer(player_id);
  52. PlayerColor player(player_id);
  53. if(playerInfo.canAnyonePlay())
  54. {
  55. player = PlayerColor(player_id);
  56. zone.setTownType(map.getMapGenOptions().getPlayersSettings().find(player)->second.getStartingTown());
  57. if(zone.getTownType() == FactionID::RANDOM)
  58. zone.setTownType(getRandomTownType(true));
  59. }
  60. else //no player - randomize town
  61. {
  62. player = PlayerColor::NEUTRAL;
  63. zone.setTownType(getRandomTownType());
  64. }
  65. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, zone.getTownType());
  66. CGTownInstance * town = dynamic_cast<CGTownInstance *>(townFactory->create(map.mapInstance->cb, nullptr));
  67. town->tempOwner = player;
  68. town->builtBuildings.insert(BuildingID::FORT);
  69. town->builtBuildings.insert(BuildingID::DEFAULT);
  70. for(auto spell : VLC->spellh->objects) //add all regular spells to town
  71. {
  72. if(!spell->isSpecial() && !spell->isCreatureAbility())
  73. town->possibleSpells.push_back(spell->id);
  74. }
  75. auto position = placeMainTown(manager, *town);
  76. totalTowns++;
  77. //register MAIN town of zone only
  78. map.registerZone(town->getFaction());
  79. if(playerInfo.canAnyonePlay()) //configure info for owning player
  80. {
  81. logGlobal->trace("Fill player info %d", player_id);
  82. // Update player info
  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(), 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 spell : VLC->spellh->objects) //add all regular spells to town
  181. {
  182. if(!spell->isSpecial() && !spell->isCreatureAbility())
  183. town->possibleSpells.push_back(spell->id);
  184. }
  185. if(totalTowns <= 0)
  186. {
  187. //FIXME: discovered bug with small zones - getPos is close to map boarder and we have outOfMap exception
  188. //register MAIN town of zone
  189. map.registerZone(town->getFaction());
  190. //first town in zone goes in the middle
  191. placeMainTown(manager, *town);
  192. }
  193. else
  194. {
  195. manager.addRequiredObject(RequiredObjectInfo(town, 0, true));
  196. }
  197. totalTowns++;
  198. }
  199. }
  200. FactionID TownPlacer::getRandomTownType(bool matchUndergroundType)
  201. {
  202. auto townTypesAllowed = (!zone.getTownTypes().empty() ? zone.getTownTypes() : zone.getDefaultTownTypes());
  203. if(matchUndergroundType)
  204. {
  205. std::set<FactionID> townTypesVerify;
  206. for(auto factionIdx : townTypesAllowed)
  207. {
  208. bool preferUnderground = (*VLC->townh)[factionIdx]->preferUndergroundPlacement;
  209. if(zone.isUnderground() ? preferUnderground : !preferUnderground)
  210. {
  211. townTypesVerify.insert(factionIdx);
  212. }
  213. }
  214. if(!townTypesVerify.empty())
  215. townTypesAllowed = townTypesVerify;
  216. }
  217. return *RandomGeneratorUtil::nextItem(townTypesAllowed, zone.getRand());
  218. }
  219. int TownPlacer::getTotalTowns() const
  220. {
  221. return totalTowns;
  222. }
  223. VCMI_LIB_NAMESPACE_END