ArtifactsUIController.cpp 5.1 KB

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