TownPlacer.cpp 7.2 KB

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