TownPlacer.cpp 7.3 KB

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