TownPlacer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.map().players[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 (generator.rand.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(), generator.rand));
  113. else if(!zone.getMonsterTypes().empty())
  114. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getMonsterTypes(), generator.rand)); //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. auto position = manager.findPlaceForObject(zone.areaPossible(), rmgObject, [this](const int3 & t)
  126. {
  127. float distance = zone.getPos().dist2dSQ(t);
  128. return 100000.f - distance; //some big number
  129. }, ObjectManager::OptimizeType::WEIGHT);
  130. rmgObject.setPosition(position + int3(2, 2, 0)); //place visitable tile in the exact center of a zone
  131. manager.placeObject(rmgObject, false, true);
  132. cleanupBoundaries(rmgObject);
  133. zone.setPos(rmgObject.getVisitablePosition()); //roads lead to main town
  134. return position;
  135. }
  136. void TownPlacer::cleanupBoundaries(const rmg::Object & rmgObject)
  137. {
  138. for(const auto & t : rmgObject.getArea().getBorderOutside())
  139. {
  140. if(map.isOnMap(t))
  141. {
  142. map.setOccupied(t, ETileType::FREE);
  143. zone.areaPossible().erase(t);
  144. zone.freePaths().add(t);
  145. }
  146. }
  147. }
  148. void TownPlacer::addNewTowns(int count, bool hasFort, const PlayerColor & player, ObjectManager & manager)
  149. {
  150. for(int i = 0; i < count; i++)
  151. {
  152. si32 subType = zone.getTownType();
  153. if(totalTowns>0)
  154. {
  155. if(!zone.areTownsSameType())
  156. {
  157. if(!zone.getTownTypes().empty())
  158. subType = *RandomGeneratorUtil::nextItem(zone.getTownTypes(), generator.rand);
  159. else
  160. subType = *RandomGeneratorUtil::nextItem(zone.getDefaultTownTypes(), generator.rand); //it is possible to have zone with no towns allowed
  161. }
  162. }
  163. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, subType);
  164. auto * town = dynamic_cast<CGTownInstance *>(townFactory->create());
  165. town->ID = Obj::TOWN;
  166. town->tempOwner = player;
  167. if (hasFort)
  168. town->builtBuildings.insert(BuildingID::FORT);
  169. town->builtBuildings.insert(BuildingID::DEFAULT);
  170. for(auto spell : VLC->spellh->objects) //add all regular spells to town
  171. {
  172. if(!spell->isSpecial() && !spell->isCreatureAbility())
  173. town->possibleSpells.push_back(spell->id);
  174. }
  175. if(totalTowns <= 0)
  176. {
  177. //FIXME: discovered bug with small zones - getPos is close to map boarder and we have outOfMap exception
  178. //register MAIN town of zone
  179. map.registerZone(town->getFaction());
  180. //first town in zone goes in the middle
  181. placeMainTown(manager, *town);
  182. }
  183. else
  184. manager.addRequiredObject(town);
  185. totalTowns++;
  186. }
  187. }
  188. si32 TownPlacer::getRandomTownType(bool matchUndergroundType)
  189. {
  190. auto townTypesAllowed = (!zone.getTownTypes().empty() ? zone.getTownTypes() : zone.getDefaultTownTypes());
  191. if(matchUndergroundType)
  192. {
  193. std::set<FactionID> townTypesVerify;
  194. for(auto factionIdx : townTypesAllowed)
  195. {
  196. bool preferUnderground = (*VLC->townh)[factionIdx]->preferUndergroundPlacement;
  197. if(zone.isUnderground() ? preferUnderground : !preferUnderground)
  198. {
  199. townTypesVerify.insert(factionIdx);
  200. }
  201. }
  202. if(!townTypesVerify.empty())
  203. townTypesAllowed = townTypesVerify;
  204. }
  205. return *RandomGeneratorUtil::nextItem(townTypesAllowed, generator.rand);
  206. }
  207. int TownPlacer::getTotalTowns() const
  208. {
  209. return totalTowns;
  210. }
  211. VCMI_LIB_NAMESPACE_END