ObjectDistributor.cpp 4.7 KB

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