TownPlacer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.getOwner() && ((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();
  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. rmgObject.setTemplate(zone.getTerrainType());
  124. auto position = manager.findPlaceForObject(zone.areaPossible(), rmgObject, [this](const int3 & t)
  125. {
  126. float distance = zone.getPos().dist2dSQ(t);
  127. return 100000.f - distance; //some big number
  128. }, ObjectManager::OptimizeType::WEIGHT);
  129. rmgObject.setPosition(position);
  130. manager.placeObject(rmgObject, false, true);
  131. cleanupBoundaries(rmgObject);
  132. zone.setPos(rmgObject.getVisitablePosition()); //roads lead to main town
  133. return position;
  134. }
  135. bool TownPlacer::placeMines(ObjectManager & manager)
  136. {
  137. using namespace Res;
  138. std::vector<CGMine*> createdMines;
  139. for(const auto & mineInfo : zone.getMinesInfo())
  140. {
  141. ERes res = (ERes)mineInfo.first;
  142. for(int i = 0; i < mineInfo.second; ++i)
  143. {
  144. auto mineHandler = VLC->objtypeh->getHandlerFor(Obj::MINE, res);
  145. auto & rmginfo = mineHandler->getRMGInfo();
  146. auto mine = (CGMine*)mineHandler->create();
  147. mine->producedResource = res;
  148. mine->tempOwner = PlayerColor::NEUTRAL;
  149. mine->producedQuantity = mine->defaultResProduction();
  150. createdMines.push_back(mine);
  151. if(!i && (res == ERes::WOOD || res == ERes::ORE))
  152. manager.addCloseObject(mine, rmginfo.value); //only first wood&ore mines are close
  153. else
  154. manager.addRequiredObject(mine, rmginfo.value);
  155. }
  156. }
  157. //create extra resources
  158. if(int extraRes = generator.getConfig().mineExtraResources)
  159. {
  160. for(auto * mine : createdMines)
  161. {
  162. for(int rc = generator.rand.nextInt(1, extraRes); rc > 0; --rc)
  163. {
  164. auto resourse = (CGResource*) VLC->objtypeh->getHandlerFor(Obj::RESOURCE, mine->producedResource)->create();
  165. resourse->amount = CGResource::RANDOM_AMOUNT;
  166. manager.addNearbyObject(resourse, mine);
  167. }
  168. }
  169. }
  170. return true;
  171. }
  172. void TownPlacer::cleanupBoundaries(const rmg::Object & rmgObject)
  173. {
  174. for(auto & t : rmgObject.getArea().getBorderOutside())
  175. {
  176. if(map.isOnMap(t))
  177. {
  178. map.setOccupied(t, ETileType::FREE);
  179. zone.areaPossible().erase(t);
  180. zone.freePaths().add(t);
  181. }
  182. }
  183. }
  184. void TownPlacer::addNewTowns(int count, bool hasFort, PlayerColor player, ObjectManager & manager)
  185. {
  186. for(int i = 0; i < count; i++)
  187. {
  188. si32 subType = zone.getTownType();
  189. if(totalTowns>0)
  190. {
  191. if(!zone.areTownsSameType())
  192. {
  193. if (zone.getTownTypes().size())
  194. subType = *RandomGeneratorUtil::nextItem(zone.getTownTypes(), generator.rand);
  195. else
  196. subType = *RandomGeneratorUtil::nextItem(zone.getDefaultTownTypes(), generator.rand); //it is possible to have zone with no towns allowed
  197. }
  198. }
  199. auto townFactory = VLC->objtypeh->getHandlerFor(Obj::TOWN, subType);
  200. auto town = (CGTownInstance *) townFactory->create();
  201. town->ID = Obj::TOWN;
  202. town->tempOwner = player;
  203. if (hasFort)
  204. town->builtBuildings.insert(BuildingID::FORT);
  205. town->builtBuildings.insert(BuildingID::DEFAULT);
  206. for(auto spell : VLC->spellh->objects) //add all regular spells to town
  207. {
  208. if(!spell->isSpecial() && !spell->isCreatureAbility())
  209. town->possibleSpells.push_back(spell->id);
  210. }
  211. if(totalTowns <= 0)
  212. {
  213. //FIXME: discovered bug with small zones - getPos is close to map boarder and we have outOfMap exception
  214. //register MAIN town of zone
  215. map.registerZone(town->subID);
  216. //first town in zone goes in the middle
  217. placeMainTown(manager, *town);
  218. }
  219. else
  220. manager.addRequiredObject(town);
  221. totalTowns++;
  222. }
  223. }
  224. si32 TownPlacer::getRandomTownType(bool matchUndergroundType)
  225. {
  226. auto townTypesAllowed = (zone.getTownTypes().size() ? zone.getTownTypes() : zone.getDefaultTownTypes());
  227. if(matchUndergroundType)
  228. {
  229. std::set<TFaction> townTypesVerify;
  230. for(TFaction factionIdx : townTypesAllowed)
  231. {
  232. bool preferUnderground = (*VLC->townh)[factionIdx]->preferUndergroundPlacement;
  233. if(zone.isUnderground() ? preferUnderground : !preferUnderground)
  234. {
  235. townTypesVerify.insert(factionIdx);
  236. }
  237. }
  238. if(!townTypesVerify.empty())
  239. townTypesAllowed = townTypesVerify;
  240. }
  241. return *RandomGeneratorUtil::nextItem(townTypesAllowed, generator.rand);
  242. }
  243. int TownPlacer::getTotalTowns() const
  244. {
  245. return totalTowns;
  246. }