QuestArtifactPlacer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * QuestArtifact.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 "QuestArtifactPlacer.h"
  12. #include "CMapGenerator.h"
  13. #include "RmgMap.h"
  14. #include "TreasurePlacer.h"
  15. #include "CZonePlacer.h"
  16. #include "../VCMI_Lib.h"
  17. #include "../mapObjects/CObjectHandler.h"
  18. #include "../mapObjects/CommonConstructors.h"
  19. #include "../mapObjects/MapObjects.h"
  20. void QuestArtifactPlacer::process()
  21. {
  22. findZonesForQuestArts();
  23. placeQuestArtifacts(&generator.rand);
  24. }
  25. void QuestArtifactPlacer::init()
  26. {
  27. DEPENDENCY_ALL(TreasurePlacer);
  28. }
  29. void QuestArtifactPlacer::addQuestArtZone(std::shared_ptr<Zone> otherZone)
  30. {
  31. questArtZones.push_back(otherZone);
  32. }
  33. void QuestArtifactPlacer::addQuestArtifact(const ArtifactID& id)
  34. {
  35. logGlobal->info("Need to place quest artifact artifact %s", VLC->artifacts()->getById(id)->getNameTranslated());
  36. questArtifactsToPlace.emplace_back(id);
  37. }
  38. void QuestArtifactPlacer::rememberPotentialArtifactToReplace(CGObjectInstance* obj)
  39. {
  40. artifactsToReplace.push_back(obj);
  41. }
  42. std::vector<CGObjectInstance*> QuestArtifactPlacer::getPossibleArtifactsToReplace() const
  43. {
  44. return artifactsToReplace;
  45. }
  46. void QuestArtifactPlacer::findZonesForQuestArts()
  47. {
  48. const auto& distances = generator.getZonePlacer()->getDistanceMap().at(zone.getId());
  49. for (auto const& connectedZone : distances)
  50. {
  51. // Choose zones that are 1 or 2 connections away
  52. if (vstd::iswithin(connectedZone.second, 1, 2))
  53. {
  54. addQuestArtZone(map.getZones().at(connectedZone.first));
  55. }
  56. }
  57. logGlobal->info("Number of nearby zones suitable for quest artifacts: %d", questArtZones.size());
  58. }
  59. void QuestArtifactPlacer::placeQuestArtifacts(CRandomGenerator * rand)
  60. {
  61. for (const auto & artifactToPlace : questArtifactsToPlace)
  62. {
  63. RandomGeneratorUtil::randomShuffle(questArtZones, *rand);
  64. for (auto zone : questArtZones)
  65. {
  66. auto* qap = zone->getModificator<QuestArtifactPlacer>();
  67. std::vector<CGObjectInstance *> artifactsToReplace = qap->getPossibleArtifactsToReplace();
  68. if (artifactsToReplace.empty())
  69. continue;
  70. auto artifactToReplace = *RandomGeneratorUtil::nextItem(artifactsToReplace, *rand);
  71. logGlobal->info("Replacing %s at %s with the quest artifact %s",
  72. artifactToReplace->getObjectName(),
  73. artifactToReplace->getPosition().toString(),
  74. VLC->artifacts()->getById(artifactToPlace)->getNameTranslated());
  75. artifactToReplace->ID = Obj::ARTIFACT;
  76. artifactToReplace->subID = artifactToPlace;
  77. //Update appearance. Terrain is irrelevant.
  78. auto handler = VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, artifactToPlace);
  79. auto templates = handler->getTemplates();
  80. artifactToReplace->appearance = templates.front();
  81. //FIXME: Instance name is still "randomArtifact"
  82. for (auto z : map.getZones())
  83. {
  84. //Every qap has its OWN collection of artifacts
  85. auto * localQap = zone->getModificator<QuestArtifactPlacer>();
  86. if (localQap)
  87. {
  88. localQap->dropReplacedArtifact(artifactToReplace);
  89. }
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. void QuestArtifactPlacer::dropReplacedArtifact(CGObjectInstance* obj)
  96. {
  97. boost::remove(artifactsToReplace, obj);
  98. }
  99. size_t QuestArtifactPlacer::getMaxQuestArtifactCount() const
  100. {
  101. return questArtifacts.size();
  102. }
  103. ArtifactID QuestArtifactPlacer::drawRandomArtifact()
  104. {
  105. if (!questArtifacts.empty())
  106. {
  107. ArtifactID ret = questArtifacts.back();
  108. questArtifacts.pop_back();
  109. RandomGeneratorUtil::randomShuffle(questArtifacts, generator.rand);
  110. return ret;
  111. }
  112. else
  113. {
  114. throw rmgException("No quest artifacts left for this zone!");
  115. }
  116. }
  117. void QuestArtifactPlacer::addRandomArtifact(ArtifactID artid)
  118. {
  119. questArtifacts.push_back(artid);
  120. }