ArtifactsUIController.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * ArtifactsUIController.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 "ArtifactsUIController.h"
  12. #include "CGameInfo.h"
  13. #include "CPlayerInterface.h"
  14. #include "../CCallback.h"
  15. #include "../lib/ArtifactUtils.h"
  16. #include "../lib/CGeneralTextHandler.h"
  17. #include "../lib/mapObjects/CGHeroInstance.h"
  18. #include "gui/CGuiHandler.h"
  19. #include "gui/WindowHandler.h"
  20. #include "widgets/CComponent.h"
  21. #include "windows/CWindowWithArtifacts.h"
  22. bool ArtifactsUIController::askToAssemble(const ArtifactLocation & al, const bool onlyEquipped, std::set<ArtifactID> * ignoredArtifacts)
  23. {
  24. if(auto hero = LOCPLINT->cb->getHero(al.artHolder))
  25. {
  26. if(hero->getArt(al.slot) == nullptr)
  27. {
  28. logGlobal->error("artifact location %d points to nothing", al.slot.num);
  29. return false;
  30. }
  31. return askToAssemble(hero, al.slot, onlyEquipped, ignoredArtifacts);
  32. }
  33. return false;
  34. }
  35. bool ArtifactsUIController::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot,
  36. const bool onlyEquipped, std::set<ArtifactID> * ignoredArtifacts)
  37. {
  38. assert(hero);
  39. const auto art = hero->getArt(slot);
  40. assert(art);
  41. if(hero->tempOwner != LOCPLINT->playerID)
  42. return false;
  43. if(numOfArtsAskAssembleSession != 0)
  44. numOfArtsAskAssembleSession--;
  45. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId(), onlyEquipped);
  46. if(!assemblyPossibilities.empty())
  47. {
  48. auto askThread = new boost::thread([this, hero, art, slot, assemblyPossibilities, ignoredArtifacts]() -> void
  49. {
  50. boost::mutex::scoped_lock askLock(askAssembleArtifactMutex);
  51. for(const auto combinedArt : assemblyPossibilities)
  52. {
  53. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  54. if(ignoredArtifacts)
  55. {
  56. if(vstd::contains(*ignoredArtifacts, combinedArt->getId()))
  57. continue;
  58. ignoredArtifacts->emplace(combinedArt->getId());
  59. }
  60. bool assembleConfirmed = false;
  61. MetaString message = MetaString::createFromTextID(art->artType->getDescriptionTextID());
  62. message.appendEOL();
  63. message.appendEOL();
  64. message.appendRawString(CGI->generaltexth->allTexts[732]); // You possess all of the components needed to assemble the
  65. message.replaceName(ArtifactID(combinedArt->getId()));
  66. LOCPLINT->showYesNoDialog(message.toString(), [&assembleConfirmed, hero, slot, combinedArt]()
  67. {
  68. assembleConfirmed = true;
  69. LOCPLINT->cb.get()->assembleArtifacts(hero->id, slot, true, combinedArt->getId());
  70. }, nullptr, {std::make_shared<CComponent>(ComponentType::ARTIFACT, combinedArt->getId())});
  71. LOCPLINT->waitWhileDialog();
  72. if(assembleConfirmed)
  73. break;
  74. }
  75. });
  76. askThread->detach();
  77. return true;
  78. }
  79. return false;
  80. }
  81. bool ArtifactsUIController::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  82. {
  83. assert(hero);
  84. const auto art = hero->getArt(slot);
  85. assert(art);
  86. if(hero->tempOwner != LOCPLINT->playerID)
  87. return false;
  88. if(art->isCombined())
  89. {
  90. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
  91. return false;
  92. MetaString message = MetaString::createFromTextID(art->artType->getDescriptionTextID());
  93. message.appendEOL();
  94. message.appendEOL();
  95. message.appendRawString(CGI->generaltexth->allTexts[733]); // Do you wish to disassemble this artifact?
  96. LOCPLINT->showYesNoDialog(message.toString(), [hero, slot]()
  97. {
  98. LOCPLINT->cb->assembleArtifacts(hero->id, slot, false, ArtifactID());
  99. }, nullptr);
  100. return true;
  101. }
  102. return false;
  103. }
  104. void ArtifactsUIController::artifactRemoved()
  105. {
  106. for(const auto & artWin : GH.windows().findWindows<CWindowWithArtifacts>())
  107. artWin->update();
  108. LOCPLINT->waitWhileDialog();
  109. }
  110. void ArtifactsUIController::artifactMoved()
  111. {
  112. // If a bulk transfer has arrived, then redrawing only the last art movement.
  113. if(numOfMovedArts != 0)
  114. numOfMovedArts--;
  115. if(numOfMovedArts == 0)
  116. for(const auto & artWin : GH.windows().findWindows<CWindowWithArtifacts>())
  117. {
  118. artWin->update();
  119. }
  120. LOCPLINT->waitWhileDialog();
  121. }
  122. void ArtifactsUIController::bulkArtMovementStart(size_t totalNumOfArts, size_t possibleAssemblyNumOfArts)
  123. {
  124. assert(totalNumOfArts >= possibleAssemblyNumOfArts);
  125. numOfMovedArts = totalNumOfArts;
  126. if(numOfArtsAskAssembleSession == 0)
  127. {
  128. // Do not start the next session until the previous one is finished
  129. numOfArtsAskAssembleSession = possibleAssemblyNumOfArts;
  130. ignoredArtifacts.clear();
  131. }
  132. }
  133. void ArtifactsUIController::artifactAssembled()
  134. {
  135. for(const auto & artWin : GH.windows().findWindows<CWindowWithArtifacts>())
  136. artWin->update();
  137. }
  138. void ArtifactsUIController::artifactDisassembled()
  139. {
  140. for(const auto & artWin : GH.windows().findWindows<CWindowWithArtifacts>())
  141. artWin->update();
  142. }