ObjectDistributor.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ObjectDistributor.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 "ObjectDistributor.h"
  12. #include "../../GameLibrary.h"
  13. #include "../RmgMap.h"
  14. #include "../CMapGenerator.h"
  15. #include "TreasurePlacer.h"
  16. #include "PrisonHeroPlacer.h"
  17. #include "QuestArtifactPlacer.h"
  18. #include "TownPlacer.h"
  19. #include "TerrainPainter.h"
  20. #include "../../mapObjectConstructors/AObjectTypeHandler.h"
  21. #include "../../mapObjectConstructors/CObjectClassesHandler.h"
  22. #include "../../mapObjects/MapObjects.h"
  23. #include "../Functions.h"
  24. #include "../RmgObject.h"
  25. #include <vstd/RNG.h>
  26. VCMI_LIB_NAMESPACE_BEGIN
  27. void ObjectDistributor::process()
  28. {
  29. distributeLimitedObjects();
  30. distributePrisons();
  31. distributeSeerHuts();
  32. }
  33. void ObjectDistributor::init()
  34. {
  35. //All of the terrain types need to be determined
  36. DEPENDENCY_ALL(TerrainPainter);
  37. POSTFUNCTION(TreasurePlacer);
  38. }
  39. void ObjectDistributor::distributeLimitedObjects()
  40. {
  41. auto zones = map.getZones();
  42. for (auto primaryID : LIBRARY->objtypeh->knownObjects())
  43. {
  44. for (auto secondaryID : LIBRARY->objtypeh->knownSubObjects(primaryID))
  45. {
  46. auto handler = LIBRARY->objtypeh->getHandlerFor(primaryID, secondaryID);
  47. if (!handler->isStaticObject() && handler->getRMGInfo().value)
  48. {
  49. auto rmgInfo = handler->getRMGInfo();
  50. //Skip objects which don't have global per-map limit here
  51. if (rmgInfo.mapLimit)
  52. {
  53. //Count all zones where this object can be placed
  54. std::vector<std::shared_ptr<Zone>> matchingZones;
  55. for (const auto& it : zones)
  56. {
  57. if (!handler->getTemplates(it.second->getTerrainType()).empty() &&
  58. rmgInfo.value <= it.second->getMaxTreasureValue())
  59. {
  60. matchingZones.push_back(it.second);
  61. }
  62. }
  63. size_t numZones = matchingZones.size();
  64. if (!numZones)
  65. continue;
  66. auto rmgInfo = handler->getRMGInfo();
  67. RandomGeneratorUtil::randomShuffle(matchingZones, zone.getRand());
  68. for (auto& zone : matchingZones)
  69. {
  70. ObjectInfo oi(primaryID, secondaryID);
  71. oi.generateObject = [cb=map.mapInstance->cb, primaryID, secondaryID]()
  72. {
  73. return LIBRARY->objtypeh->getHandlerFor(primaryID, secondaryID)->create(cb, nullptr);
  74. };
  75. oi.value = rmgInfo.value;
  76. oi.probability = rmgInfo.rarity;
  77. oi.setTemplates(primaryID, secondaryID, zone->getTerrainType());
  78. //Rounding up will make sure all possible objects are exhausted
  79. uint32_t mapLimit = rmgInfo.mapLimit.value();
  80. uint32_t maxPerZone = std::ceil(float(mapLimit) / numZones);
  81. //But not more than zone limit
  82. oi.maxPerZone = std::min(maxPerZone, rmgInfo.zoneLimit);
  83. numZones--;
  84. rmgInfo.setMapLimit(mapLimit - oi.maxPerZone);
  85. //Don't add objects with 0 count remaining
  86. if(oi.maxPerZone && !oi.templates.empty())
  87. {
  88. zone->getModificator<TreasurePlacer>()->addObjectToRandomPool(oi);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. void ObjectDistributor::distributeSeerHuts()
  97. {
  98. //TODO: Move typedef outside the class?
  99. //Copy by value to random shuffle
  100. const auto & zoneMap = map.getZones();
  101. RmgMap::ZoneVector zones(zoneMap.begin(), zoneMap.end());
  102. RandomGeneratorUtil::randomShuffle(zones, zone.getRand());
  103. const auto & possibleQuestArts = generator.getAllPossibleQuestArtifacts();
  104. size_t availableArts = possibleQuestArts.size();
  105. auto artIt = possibleQuestArts.begin();
  106. for (int i = zones.size() - 1; i >= 0 ; i--)
  107. {
  108. size_t localArts = std::ceil((float)availableArts / (i + 1));
  109. availableArts -= localArts;
  110. auto * qap = zones[i].second->getModificator<QuestArtifactPlacer>();
  111. if (qap)
  112. {
  113. for (;localArts > 0 && artIt != possibleQuestArts.end(); artIt++, localArts--)
  114. {
  115. qap->addRandomArtifact(*artIt);
  116. }
  117. }
  118. }
  119. }
  120. void ObjectDistributor::distributePrisons()
  121. {
  122. //Copy by value to random shuffle
  123. const auto & zoneMap = map.getZones();
  124. RmgMap::ZoneVector zones(zoneMap.begin(), zoneMap.end());
  125. RandomGeneratorUtil::randomShuffle(zones, zone.getRand());
  126. // TODO: Some shorthand for unique Modificator
  127. PrisonHeroPlacer * prisonHeroPlacer = nullptr;
  128. for(auto & z : map.getZones())
  129. {
  130. prisonHeroPlacer = z.second->getModificator<PrisonHeroPlacer>();
  131. if (prisonHeroPlacer)
  132. {
  133. break;
  134. }
  135. }
  136. size_t allowedPrisons = prisonHeroPlacer->getPrisonsRemaining();
  137. for (int i = zones.size() - 1; i >= 0; i--)
  138. {
  139. auto zone = zones[i].second;
  140. auto * tp = zone->getModificator<TreasurePlacer>();
  141. if (tp)
  142. {
  143. tp->setMaxPrisons(std::ceil(float(allowedPrisons) / (i + 1)));
  144. allowedPrisons -= tp->getMaxPrisons();
  145. }
  146. }
  147. }
  148. VCMI_LIB_NAMESPACE_END