QuestArtifactPlacer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //FIXME: Store and access CZonePlacer from CMapGenerator
  49. const auto& distances = generator.getZonePlacer()->getDistanceMap().at(zone.getId());
  50. for (auto const& connectedZone : distances)
  51. {
  52. // Choose zones that are 1 or 2 connections away
  53. if (vstd::iswithin(connectedZone.second, 1, 2))
  54. {
  55. addQuestArtZone(map.getZones().at(connectedZone.first));
  56. }
  57. }
  58. logGlobal->info("Number of nearby zones suitable for quest artifacts: %d", questArtZones.size());
  59. logGlobal->info("Number of possible quest artifacts remaining: %d", generator.getQuestArtsRemaning().size());
  60. }
  61. void QuestArtifactPlacer::placeQuestArtifacts(CRandomGenerator * rand)
  62. {
  63. for (const auto & artifactToPlace : questArtifactsToPlace)
  64. {
  65. RandomGeneratorUtil::randomShuffle(questArtZones, *rand);
  66. for (auto zone : questArtZones)
  67. {
  68. auto* qap = zone->getModificator<QuestArtifactPlacer>();
  69. std::vector<CGObjectInstance *> artifactsToReplace = qap->getPossibleArtifactsToReplace();
  70. if (artifactsToReplace.empty())
  71. continue;
  72. auto artifactToReplace = *RandomGeneratorUtil::nextItem(artifactsToReplace, *rand);
  73. logGlobal->info("Replacing %s at %s with the quest artifact %s",
  74. artifactToReplace->getObjectName(),
  75. artifactToReplace->getPosition().toString(),
  76. VLC->artifacts()->getById(artifactToPlace)->getNameTranslated());
  77. artifactToReplace->ID = Obj::ARTIFACT;
  78. artifactToReplace->subID = artifactToPlace;
  79. //Update appearance. Terrain is irrelevant.
  80. auto handler = VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, artifactToPlace);
  81. auto templates = handler->getTemplates();
  82. artifactToReplace->appearance = templates.front();
  83. //FIXME: Instance name is still "randomArtifact"
  84. qap->dropReplacedArtifact(artifactToReplace);
  85. break;
  86. }
  87. }
  88. }
  89. void QuestArtifactPlacer::dropReplacedArtifact(CGObjectInstance* obj)
  90. {
  91. boost::remove(artifactsToReplace, obj);
  92. }