CAltarCreatures.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * CAltarCreatures.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 "CAltarCreatures.h"
  12. #include "../../gui/CGuiHandler.h"
  13. #include "../../widgets/Buttons.h"
  14. #include "../../widgets/Slider.h"
  15. #include "../../widgets/TextControls.h"
  16. #include "../../CGameInfo.h"
  17. #include "../../CPlayerInterface.h"
  18. #include "../../../CCallback.h"
  19. #include "../../../lib/CGeneralTextHandler.h"
  20. #include "../../../lib/mapObjects/CGHeroInstance.h"
  21. #include "../../../lib/mapObjects/CGMarket.h"
  22. CAltarCreatures::CAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
  23. : CTradeBase(market, hero)
  24. {
  25. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  26. deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("ALTSACR.DEF"),
  27. CGI->generaltexth->zelp[584], [this]() {CAltarCreatures::makeDeal();});
  28. labels.emplace_back(std::make_shared<CLabel>(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW,
  29. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  30. labels.emplace_back(std::make_shared<CLabel>(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]));
  31. texts.emplace_back(std::make_unique<CTextBox>(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  32. lSubtitle = std::make_shared<CLabel>(180, 503, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  33. rSubtitle = std::make_shared<CLabel>(426, 503, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  34. offerSlider = std::make_shared<CSlider>(Point(231, 481), 137, std::bind(&CAltarCreatures::onOfferSliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  35. maxUnits = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[578], std::bind(&CSlider::scrollToMax, offerSlider));
  36. unitsOnAltar.resize(GameConstants::ARMY_SIZE, 0);
  37. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  38. sacrificeAllButton = std::make_shared<CButton>(
  39. Point(393, 520), AnimationPath::builtin("ALTARMY.DEF"), CGI->generaltexth->zelp[579], std::bind(&CExperienceAltar::sacrificeAll, this));
  40. // Hero creatures panel
  41. assert(leftTradePanel);
  42. leftTradePanel->moveBy(Point(45, 110));
  43. leftTradePanel->updateSlotsCallback = std::bind(&CCreaturesSelling::updateSubtitle, this);
  44. for(const auto & slot : leftTradePanel->slots)
  45. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot) {CAltarCreatures::onSlotClickPressed(heroSlot, hLeft);};
  46. // Altar creatures panel
  47. rightTradePanel = std::make_shared<CreaturesPanel>([this](const std::shared_ptr<CTradeableItem> & altarSlot)
  48. {
  49. CAltarCreatures::onSlotClickPressed(altarSlot, hRight);
  50. }, leftTradePanel->slots);
  51. rightTradePanel->moveBy(Point(334, 110));
  52. leftTradePanel->deleteSlotsCheck = rightTradePanel->deleteSlotsCheck = std::bind(&CCreaturesSelling::slotDeletingCheck, this, _1);
  53. readExpValues();
  54. expForHero->setText(std::to_string(0));
  55. CAltarCreatures::deselect();
  56. };
  57. void CAltarCreatures::readExpValues()
  58. {
  59. int dump;
  60. for(auto heroSlot : leftTradePanel->slots)
  61. {
  62. if(heroSlot->id >= 0)
  63. market->getOffer(heroSlot->id, 0, dump, expPerUnit[heroSlot->serial], EMarketMode::CREATURE_EXP);
  64. }
  65. }
  66. void CAltarCreatures::updateControls()
  67. {
  68. int sliderAmount = 0;
  69. if(hLeft)
  70. {
  71. std::optional<SlotID> lastSlot;
  72. for(auto slot = SlotID(0); slot.num < GameConstants::ARMY_SIZE; slot++)
  73. {
  74. if(hero->getStackCount(slot) > unitsOnAltar[slot.num])
  75. {
  76. if(lastSlot.has_value())
  77. {
  78. lastSlot = std::nullopt;
  79. break;
  80. }
  81. else
  82. {
  83. lastSlot = slot;
  84. }
  85. }
  86. }
  87. sliderAmount = hero->getStackCount(SlotID(hLeft->serial));
  88. if(lastSlot.has_value() && lastSlot.value() == SlotID(hLeft->serial))
  89. sliderAmount--;
  90. }
  91. offerSlider->setAmount(sliderAmount);
  92. offerSlider->block(!offerSlider->getAmount());
  93. if(hLeft)
  94. offerSlider->scrollTo(unitsOnAltar[hLeft->serial]);
  95. maxUnits->block(offerSlider->getAmount() == 0);
  96. }
  97. void CAltarCreatures::updateSubtitlesForSelected()
  98. {
  99. if(hLeft)
  100. lSubtitle->setText(std::to_string(offerSlider->getValue()));
  101. else
  102. lSubtitle->setText("");
  103. if(hRight)
  104. rSubtitle->setText(hRight->subtitle);
  105. else
  106. rSubtitle->setText("");
  107. }
  108. void CAltarCreatures::updateSlots()
  109. {
  110. rightTradePanel->deleteSlots();
  111. leftTradePanel->deleteSlots();
  112. assert(leftTradePanel->slots.size() == rightTradePanel->slots.size());
  113. readExpValues();
  114. leftTradePanel->updateSlots();
  115. }
  116. void CAltarCreatures::deselect()
  117. {
  118. CTradeBase::deselect();
  119. offerSlider->block(true);
  120. maxUnits->block(true);
  121. updateSubtitlesForSelected();
  122. }
  123. TExpType CAltarCreatures::calcExpAltarForHero()
  124. {
  125. TExpType expOnAltar(0);
  126. auto oneUnitExp = expPerUnit.begin();
  127. for(const auto units : unitsOnAltar)
  128. expOnAltar += *oneUnitExp++ * units;
  129. auto resultExp = hero->calculateXp(expOnAltar);
  130. expForHero->setText(std::to_string(resultExp));
  131. return resultExp;
  132. }
  133. void CAltarCreatures::makeDeal()
  134. {
  135. deselect();
  136. offerSlider->scrollTo(0);
  137. expForHero->setText(std::to_string(0));
  138. std::vector<TradeItemSell> ids;
  139. std::vector<ui32> toSacrifice;
  140. for(int i = 0; i < unitsOnAltar.size(); i++)
  141. {
  142. if(unitsOnAltar[i])
  143. {
  144. ids.push_back(SlotID(i));
  145. toSacrifice.push_back(unitsOnAltar[i]);
  146. }
  147. }
  148. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_EXP, ids, {}, toSacrifice, hero);
  149. for(int & units : unitsOnAltar)
  150. units = 0;
  151. for(auto heroSlot : rightTradePanel->slots)
  152. {
  153. heroSlot->setType(EType::CREATURE_PLACEHOLDER);
  154. heroSlot->subtitle.clear();
  155. }
  156. }
  157. void CAltarCreatures::sacrificeAll()
  158. {
  159. std::optional<SlotID> lastSlot;
  160. for(auto heroSlot : leftTradePanel->slots)
  161. {
  162. auto stackCount = hero->getStackCount(SlotID(heroSlot->serial));
  163. if(stackCount > unitsOnAltar[heroSlot->serial])
  164. {
  165. if(!lastSlot.has_value())
  166. lastSlot = SlotID(heroSlot->serial);
  167. unitsOnAltar[heroSlot->serial] = stackCount;
  168. }
  169. }
  170. assert(lastSlot.has_value());
  171. unitsOnAltar[lastSlot.value().num]--;
  172. if(hRight)
  173. offerSlider->scrollTo(unitsOnAltar[hRight->serial]);
  174. for(auto altarSlot : rightTradePanel->slots)
  175. updateAltarSlot(altarSlot);
  176. updateSubtitlesForSelected();
  177. deal->block(calcExpAltarForHero() == 0);
  178. }
  179. void CAltarCreatures::updateAltarSlot(std::shared_ptr<CTradeableItem> slot)
  180. {
  181. auto units = unitsOnAltar[slot->serial];
  182. slot->setType(units > 0 ? EType::CREATURE : EType::CREATURE_PLACEHOLDER);
  183. slot->subtitle = units > 0 ?
  184. boost::str(boost::format(CGI->generaltexth->allTexts[122]) % std::to_string(hero->calculateXp(units * expPerUnit[slot->serial]))) : "";
  185. }
  186. void CAltarCreatures::onOfferSliderMoved(int newVal)
  187. {
  188. if(hLeft)
  189. unitsOnAltar[hLeft->serial] = newVal;
  190. if(hRight)
  191. updateAltarSlot(hRight);
  192. deal->block(calcExpAltarForHero() == 0);
  193. updateControls();
  194. updateSubtitlesForSelected();
  195. }
  196. void CAltarCreatures::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSide)
  197. {
  198. if(hCurSide == newSlot)
  199. return;
  200. auto * oppositeSlot = &hLeft;
  201. auto oppositePanel = leftTradePanel;
  202. CTradeBase::onSlotClickPressed(newSlot, hCurSide);
  203. if(hCurSide == hLeft)
  204. {
  205. oppositeSlot = &hRight;
  206. oppositePanel = rightTradePanel;
  207. }
  208. std::shared_ptr<CTradeableItem> oppositeNewSlot;
  209. for(const auto & slot : oppositePanel->slots)
  210. if(slot->serial == newSlot->serial)
  211. {
  212. oppositeNewSlot = slot;
  213. break;
  214. }
  215. assert(oppositeNewSlot);
  216. CTradeBase::onSlotClickPressed(oppositeNewSlot, *oppositeSlot);
  217. updateControls();
  218. updateSubtitlesForSelected();
  219. redraw();
  220. }