TownPlacer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "../../mapping/CMap.h"
  17. #include "../../mapping/CMapEditManager.h"
  18. #include "../../spells/CSpellHandler.h" //for choosing random spells
  19. #include "../RmgPath.h"
  20. #include "../RmgObject.h"
  21. #include "ObjectManager.h"
  22. #include "../Functions.h"
  23. #include "RoadPlacer.h"
  24. #include "MinePlacer.h"
  25. #include "WaterAdopter.h"
  26. #include "../TileInfo.h"
  27. VCMI_LIB_NAMESPACE_BEGIN
  28. void TownPlacer::process()
  29. {
  30. auto * manager = zone.getModificator<ObjectManager>();
  31. if(!manager)
  32. {
  33. logGlobal->error("ObjectManager doesn't exist for zone %d, skip modificator %s", zone.getId(), getName());
  34. return;
  35. }
  36. placeTowns(*manager);
  37. }
  38. void TownPlacer::init()
  39. {
  40. POSTFUNCTION(MinePlacer);
  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.getPlayer(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->getFaction());
  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 (zone.getRand().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(), zone.getRand()));
  114. else if(!zone.getMonsterTypes().empty())
  115. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getMonsterTypes(), zone.getRand())); //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. int3 position(-1, -1, -1);
  127. {
  128. Zone::Lock lock(zone.areaMutex);
  129. position = manager.findPlaceForObject(zone.areaPossible(), rmgObject, [this](const int3& t)
  130. {
  131. float distance = zone.getPos().dist2dSQ(t);
  132. return 100000.f - distance; //some big number
  133. }, ObjectManager::OptimizeType::WEIGHT);
  134. }
  135. rmgObject.setPosition(position + int3(2, 2, 0)); //place visitable tile in the exact center of a zone
  136. manager.placeObject(rmgObject, false, true, true);
  137. cleanupBoundaries(rmgObject);
  138. zone.setPos(rmgObject.getVisitablePosition()); //roads lead to main town
  139. return position;
  140. }
  141. void TownPlacer::cleanupBoundaries(const rmg::Object & rmgObject)
  142. {
  143. Zone::Lock lock(zone.areaMutex);
  144. for(const auto & t : rmgObject.getArea().getBorderOutside())
  145. {
  146. if (t.y > rmgObject.getVisitablePosition().y) //Line below the town
  147. {
  148. if (map.isOnMap(t))
  149. {
  150. map.setOccupied(t, ETileType::FREE);
  151. zone.areaPossible().erase(t);
  152. zone.freePaths().add(t);
  153. }
  154. }
  155. }
  156. }
  157. void TownPlacer::addNewTowns(int count, bool hasFort, const PlayerColor & player, ObjectManager & manager)
  158. {
  159. for(int i = 0; i < count; i++)
  160. {
  161. si32 subType = zone.getTownType();
  162. if(totalTowns>0)
  163. {
  164. if(!zone.areTownsSameType())
  165. {
  166. if(!zone.getTownTypes().empty())
  167. subType = *RandomGeneratorUtil::nextItem(zone.getTownTypes(), zone.getRand());
  168. else
  169. subType = *RandomGeneratorUtil::nextItem(zone.getDefaultTownTypes(), zone.getRand()); //it is possible to have zone with no towns allowed
  170. }
  171. }
  172. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, subType);
  173. auto * town = dynamic_cast<CGTownInstance *>(townFactory->create());
  174. town->ID = Obj::TOWN;
  175. town->tempOwner = player;
  176. if (hasFort)
  177. town->builtBuildings.insert(BuildingID::FORT);
  178. town->builtBuildings.insert(BuildingID::DEFAULT);
  179. for(auto spell : VLC->spellh->objects) //add all regular spells to town
  180. {
  181. if(!spell->isSpecial() && !spell->isCreatureAbility())
  182. town->possibleSpells.push_back(spell->id);
  183. }
  184. if(totalTowns <= 0)
  185. {
  186. //FIXME: discovered bug with small zones - getPos is close to map boarder and we have outOfMap exception
  187. //register MAIN town of zone
  188. map.registerZone(town->getFaction());
  189. //first town in zone goes in the middle
  190. placeMainTown(manager, *town);
  191. }
  192. else
  193. {
  194. manager.addRequiredObject(RequiredObjectInfo(town, 0, true));
  195. }
  196. totalTowns++;
  197. }
  198. }
  199. si32 TownPlacer::getRandomTownType(bool matchUndergroundType)
  200. {
  201. auto townTypesAllowed = (!zone.getTownTypes().empty() ? zone.getTownTypes() : zone.getDefaultTownTypes());
  202. if(matchUndergroundType)
  203. {
  204. std::set<FactionID> townTypesVerify;
  205. for(auto factionIdx : townTypesAllowed)
  206. {
  207. bool preferUnderground = (*VLC->townh)[factionIdx]->preferUndergroundPlacement;
  208. if(zone.isUnderground() ? preferUnderground : !preferUnderground)
  209. {
  210. townTypesVerify.insert(factionIdx);
  211. }
  212. }
  213. if(!townTypesVerify.empty())
  214. townTypesAllowed = townTypesVerify;
  215. }
  216. return *RandomGeneratorUtil::nextItem(townTypesAllowed, zone.getRand());
  217. }
  218. int TownPlacer::getTotalTowns() const
  219. {
  220. return totalTowns;
  221. }
  222. VCMI_LIB_NAMESPACE_END