ObjectDistributor.cpp 4.6 KB

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