CAltarCreatures.cpp 7.2 KB

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