TownPlacer.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "WaterAdopter.h"
  24. #include "TileInfo.h"
  25. void TownPlacer::process()
  26. {
  27. auto * manager = zone.getModificator<ObjectManager>();
  28. if(!manager)
  29. {
  30. logGlobal->error("ObjectManager doesn't exist for zone %d, skip modificator %s", zone.getId(), getName());
  31. return;
  32. }
  33. placeTowns(*manager);
  34. placeMines(*manager);
  35. }
  36. void TownPlacer::init()
  37. {
  38. POSTFUNCTION(ObjectManager);
  39. POSTFUNCTION(RoadPlacer);
  40. }
  41. void TownPlacer::placeTowns(ObjectManager & manager)
  42. {
  43. if((zone.getType() == ETemplateZoneType::CPU_START) || (zone.getType() == ETemplateZoneType::PLAYER_START))
  44. {
  45. //set zone types to player faction, generate main town
  46. logGlobal->info("Preparing playing zone");
  47. int player_id = *zone.getOwner() - 1;
  48. auto & playerInfo = map.map().players[player_id];
  49. PlayerColor player(player_id);
  50. if(playerInfo.canAnyonePlay())
  51. {
  52. player = PlayerColor(player_id);
  53. zone.setTownType(map.getMapGenOptions().getPlayersSettings().find(player)->second.getStartingTown());
  54. if(zone.getTownType() == CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
  55. zone.setTownType(getRandomTownType(true));
  56. }
  57. else //no player - randomize town
  58. {
  59. player = PlayerColor::NEUTRAL;
  60. zone.setTownType(getRandomTownType());
  61. }
  62. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, zone.getTownType());
  63. CGTownInstance * town = (CGTownInstance *) townFactory->create(ObjectTemplate());
  64. town->tempOwner = player;
  65. town->builtBuildings.insert(BuildingID::FORT);
  66. town->builtBuildings.insert(BuildingID::DEFAULT);
  67. for(auto spell : VLC->spellh->objects) //add all regular spells to town
  68. {
  69. if(!spell->isSpecial() && !spell->isCreatureAbility())
  70. town->possibleSpells.push_back(spell->id);
  71. }
  72. auto position = placeMainTown(manager, *town);
  73. totalTowns++;
  74. //register MAIN town of zone only
  75. map.registerZone(town->subID);
  76. if(playerInfo.canAnyonePlay()) //configure info for owning player
  77. {
  78. logGlobal->trace("Fill player info %d", player_id);
  79. // Update player info
  80. playerInfo.allowedFactions.clear();
  81. playerInfo.allowedFactions.insert(zone.getTownType());
  82. playerInfo.hasMainTown = true;
  83. playerInfo.posOfMainTown = position;
  84. playerInfo.generateHeroAtMainTown = true;
  85. //now create actual towns
  86. addNewTowns(zone.getPlayerTowns().getCastleCount() - 1, true, player, manager);
  87. addNewTowns(zone.getPlayerTowns().getTownCount(), false, player, manager);
  88. }
  89. else
  90. {
  91. addNewTowns(zone.getPlayerTowns().getCastleCount() - 1, true, PlayerColor::NEUTRAL, manager);
  92. addNewTowns(zone.getPlayerTowns().getTownCount(), false, PlayerColor::NEUTRAL, manager);
  93. }
  94. }
  95. else //randomize town types for any other zones as well
  96. {
  97. zone.setTownType(getRandomTownType());
  98. }
  99. addNewTowns(zone.getNeutralTowns().getCastleCount(), true, PlayerColor::NEUTRAL, manager);
  100. addNewTowns(zone.getNeutralTowns().getTownCount(), false, PlayerColor::NEUTRAL, manager);
  101. if(!totalTowns) //if there's no town present, get random faction for dwellings and pandoras
  102. {
  103. //25% chance for neutral
  104. if (generator.rand.nextInt(1, 100) <= 25)
  105. {
  106. zone.setTownType(ETownType::NEUTRAL);
  107. }
  108. else
  109. {
  110. if(zone.getTownTypes().size())
  111. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getTownTypes(), generator.rand));
  112. else if(zone.getMonsterTypes().size())
  113. zone.setTownType(*RandomGeneratorUtil::nextItem(zone.getMonsterTypes(), generator.rand)); //this happens in Clash of Dragons in treasure zones, where all towns are banned
  114. else //just in any case
  115. zone.setTownType(getRandomTownType());
  116. }
  117. }
  118. }
  119. int3 TownPlacer::placeMainTown(ObjectManager & manager, CGTownInstance & town)
  120. {
  121. //towns are big objects and should be centered around visitable position
  122. rmg::Object rmgObject(town);
  123. auto position = manager.findPlaceForObject(zone.areaPossible(), rmgObject, [this](const int3 & t)
  124. {
  125. float distance = zone.getPos().dist2dSQ(t);
  126. return 100000.f - distance; //some big number
  127. }, true);
  128. rmgObject.setPosition(position);
  129. manager.placeObject(rmgObject, false, true);
  130. cleanupBoundaries(rmgObject);
  131. zone.setPos(rmgObject.getVisitablePosition()); //roads lead to main town
  132. return position;
  133. }
  134. bool TownPlacer::placeMines(ObjectManager & manager)
  135. {
  136. using namespace Res;
  137. std::vector<CGMine*> createdMines;
  138. for(const auto & mineInfo : zone.getMinesInfo())
  139. {
  140. ERes res = (ERes)mineInfo.first;
  141. for(int i = 0; i < mineInfo.second; ++i)
  142. {
  143. auto mineHandler = VLC->objtypeh->getHandlerFor(Obj::MINE, res);
  144. auto & rmginfo = mineHandler->getRMGInfo();
  145. auto mine = (CGMine*)mineHandler->create(ObjectTemplate());
  146. mine->producedResource = res;
  147. mine->tempOwner = PlayerColor::NEUTRAL;
  148. mine->producedQuantity = mine->defaultResProduction();
  149. createdMines.push_back(mine);
  150. if(!i && (res == ERes::WOOD || res == ERes::ORE))
  151. manager.addCloseObject(mine, rmginfo.value); //only first wood&ore mines are close
  152. else
  153. manager.addRequiredObject(mine, rmginfo.value);
  154. }
  155. }
  156. //create extra resources
  157. if(int extraRes = generator.getConfig().mineExtraResources)
  158. {
  159. for(auto * mine : createdMines)
  160. {
  161. for(int rc = generator.rand.nextInt(1, extraRes); rc > 0; --rc)
  162. {
  163. auto resourse = (CGResource*) VLC->objtypeh->getHandlerFor(Obj::RESOURCE, mine->producedResource)->create(ObjectTemplate());
  164. resourse->amount = CGResource::RANDOM_AMOUNT;
  165. manager.addNearbyObject(resourse, mine);
  166. }
  167. }
  168. }
  169. return true;
  170. }
  171. void TownPlacer::cleanupBoundaries(const rmg::Object & rmgObject)
  172. {
  173. for(auto & t : rmgObject.getArea().getBorderOutside())
  174. {
  175. if(map.isOnMap(t))
  176. {
  177. map.setOccupied(t, ETileType::FREE);
  178. zone.areaPossible().erase(t);
  179. zone.freePaths().add(t);
  180. }
  181. }
  182. }
  183. void TownPlacer::addNewTowns(int count, bool hasFort, PlayerColor player, ObjectManager & manager)
  184. {
  185. for(int i = 0; i < count; i++)
  186. {
  187. si32 subType = zone.getTownType();
  188. if(totalTowns>0)
  189. {
  190. if(!zone.areTownsSameType())
  191. {
  192. if (zone.getTownTypes().size())
  193. subType = *RandomGeneratorUtil::nextItem(zone.getTownTypes(), generator.rand);
  194. else
  195. subType = *RandomGeneratorUtil::nextItem(zone.getDefaultTownTypes(), generator.rand); //it is possible to have zone with no towns allowed
  196. }
  197. }
  198. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, subType);
  199. auto town = (CGTownInstance *) townFactory->create(ObjectTemplate());
  200. town->ID = Obj::TOWN;
  201. town->tempOwner = player;
  202. if (hasFort)
  203. town->builtBuildings.insert(BuildingID::FORT);
  204. town->builtBuildings.insert(BuildingID::DEFAULT);
  205. for(auto spell : VLC->spellh->objects) //add all regular spells to town
  206. {
  207. if(!spell->isSpecial() && !spell->isCreatureAbility())
  208. town->possibleSpells.push_back(spell->id);
  209. }
  210. if(totalTowns <= 0)
  211. {
  212. //FIXME: discovered bug with small zones - getPos is close to map boarder and we have outOfMap exception
  213. //register MAIN town of zone
  214. map.registerZone(town->subID);
  215. //first town in zone goes in the middle
  216. placeMainTown(manager, *town);
  217. }
  218. else
  219. manager.addRequiredObject(town);
  220. totalTowns++;
  221. }
  222. }
  223. si32 TownPlacer::getRandomTownType(bool matchUndergroundType)
  224. {
  225. auto townTypesAllowed = (zone.getTownTypes().size() ? zone.getTownTypes() : zone.getDefaultTownTypes());
  226. if(matchUndergroundType)
  227. {
  228. std::set<TFaction> townTypesVerify;
  229. for(TFaction factionIdx : townTypesAllowed)
  230. {
  231. bool preferUnderground = (*VLC->townh)[factionIdx]->preferUndergroundPlacement;
  232. if(zone.isUnderground() ? preferUnderground : !preferUnderground)
  233. {
  234. townTypesVerify.insert(factionIdx);
  235. }
  236. }
  237. if(!townTypesVerify.empty())
  238. townTypesAllowed = townTypesVerify;
  239. }
  240. return *RandomGeneratorUtil::nextItem(townTypesAllowed, generator.rand);
  241. }
  242. int TownPlacer::getTotalTowns() const
  243. {
  244. return totalTowns;
  245. }