CAltarCreatures.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. offerSlider = std::make_shared<CSlider>(Point(231, 481), 137, std::bind(&CAltarCreatures::onOfferSliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  33. maxAmount = 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->moveTo(pos.topLeft() + Point(45, 110));
  41. leftTradePanel->selectedImage->moveTo(pos.topLeft() + Point(149, 422));
  42. leftTradePanel->selectedSubtitle->moveTo(pos.topLeft() + Point(180, 503));
  43. for(const auto & slot : leftTradePanel->slots)
  44. slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot) {CAltarCreatures::onSlotClickPressed(heroSlot, hLeft);};
  45. // Altar creatures panel
  46. rightTradePanel = std::make_shared<CreaturesPanel>([this](const std::shared_ptr<CTradeableItem> & altarSlot)
  47. {
  48. CAltarCreatures::onSlotClickPressed(altarSlot, hRight);
  49. }, leftTradePanel->slots);
  50. rightTradePanel->moveTo(pos.topLeft() + Point(334, 110));
  51. rightTradePanel->selectedImage->moveTo(pos.topLeft() + Point(395, 422));
  52. rightTradePanel->selectedSubtitle->moveTo(pos.topLeft() + Point(426, 503));
  53. leftTradePanel->deleteSlotsCheck = rightTradePanel->deleteSlotsCheck = std::bind(&CCreaturesSelling::slotDeletingCheck, this, _1);
  54. readExpValues();
  55. CAltarCreatures::deselect();
  56. };
  57. void CAltarCreatures::readExpValues()
  58. {
  59. int bidQty = 0;
  60. for(auto heroSlot : leftTradePanel->slots)
  61. {
  62. if(heroSlot->id >= 0)
  63. market->getOffer(heroSlot->id, 0, bidQty, 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. maxAmount->block(offerSlider->getAmount() == 0);
  96. }
  97. void CAltarCreatures::updateSelected()
  98. {
  99. std::optional<size_t> lImageIndex = std::nullopt;
  100. std::optional<size_t> rImageIndex = std::nullopt;
  101. if(hLeft)
  102. {
  103. leftTradePanel->selectedSubtitle->setText(std::to_string(offerSlider->getValue()));
  104. lImageIndex = CGI->creatures()->getByIndex(hLeft->id)->getIconIndex();
  105. }
  106. else
  107. {
  108. leftTradePanel->selectedSubtitle->setText("");
  109. }
  110. if(hRight)
  111. {
  112. rightTradePanel->selectedSubtitle->setText(hRight->subtitle);
  113. if(offerSlider->getValue() != 0)
  114. rImageIndex = CGI->creatures()->getByIndex(hRight->id)->getIconIndex();
  115. }
  116. else
  117. {
  118. rightTradePanel->selectedSubtitle->setText("");
  119. }
  120. leftTradePanel->setSelectedFrameIndex(lImageIndex);
  121. rightTradePanel->setSelectedFrameIndex(rImageIndex);
  122. }
  123. void CAltarCreatures::updateSlots()
  124. {
  125. rightTradePanel->deleteSlots();
  126. CCreaturesSelling::updateSlots();
  127. assert(leftTradePanel->slots.size() == rightTradePanel->slots.size());
  128. readExpValues();
  129. }
  130. void CAltarCreatures::deselect()
  131. {
  132. CTradeBase::deselect();
  133. updateSelected();
  134. expForHero->setText(std::to_string(0));
  135. }
  136. TExpType CAltarCreatures::calcExpAltarForHero()
  137. {
  138. TExpType expOnAltar(0);
  139. auto oneUnitExp = expPerUnit.begin();
  140. for(const auto units : unitsOnAltar)
  141. expOnAltar += *oneUnitExp++ * units;
  142. auto resultExp = hero->calculateXp(expOnAltar);
  143. expForHero->setText(std::to_string(resultExp));
  144. return resultExp;
  145. }
  146. void CAltarCreatures::makeDeal()
  147. {
  148. deselect();
  149. std::vector<TradeItemSell> ids;
  150. std::vector<ui32> toSacrifice;
  151. for(int i = 0; i < unitsOnAltar.size(); i++)
  152. {
  153. if(unitsOnAltar[i])
  154. {
  155. ids.push_back(SlotID(i));
  156. toSacrifice.push_back(unitsOnAltar[i]);
  157. }
  158. }
  159. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_EXP, ids, {}, toSacrifice, hero);
  160. for(int & units : unitsOnAltar)
  161. units = 0;
  162. for(auto heroSlot : rightTradePanel->slots)
  163. {
  164. heroSlot->setType(EType::CREATURE_PLACEHOLDER);
  165. heroSlot->subtitle.clear();
  166. }
  167. }
  168. void CAltarCreatures::sacrificeAll()
  169. {
  170. std::optional<SlotID> lastSlot;
  171. for(auto heroSlot : leftTradePanel->slots)
  172. {
  173. auto stackCount = hero->getStackCount(SlotID(heroSlot->serial));
  174. if(stackCount > unitsOnAltar[heroSlot->serial])
  175. {
  176. if(!lastSlot.has_value())
  177. lastSlot = SlotID(heroSlot->serial);
  178. unitsOnAltar[heroSlot->serial] = stackCount;
  179. }
  180. }
  181. if(hero->needsLastStack())
  182. {
  183. assert(lastSlot.has_value());
  184. unitsOnAltar[lastSlot.value().num]--;
  185. }
  186. if(hRight)
  187. offerSlider->scrollTo(unitsOnAltar[hRight->serial]);
  188. for(auto altarSlot : rightTradePanel->slots)
  189. updateAltarSlot(altarSlot);
  190. updateSelected();
  191. deal->block(calcExpAltarForHero() == 0);
  192. }
  193. void CAltarCreatures::updateAltarSlot(std::shared_ptr<CTradeableItem> slot)
  194. {
  195. auto units = unitsOnAltar[slot->serial];
  196. slot->setType(units > 0 ? EType::CREATURE : EType::CREATURE_PLACEHOLDER);
  197. slot->subtitle = units > 0 ?
  198. boost::str(boost::format(CGI->generaltexth->allTexts[122]) % std::to_string(hero->calculateXp(units * expPerUnit[slot->serial]))) : "";
  199. }
  200. void CAltarCreatures::onOfferSliderMoved(int newVal)
  201. {
  202. if(hLeft)
  203. unitsOnAltar[hLeft->serial] = newVal;
  204. if(hRight)
  205. updateAltarSlot(hRight);
  206. deal->block(calcExpAltarForHero() == 0);
  207. updateControls();
  208. updateSelected();
  209. redraw();
  210. }
  211. void CAltarCreatures::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
  212. {
  213. if(hCurSlot == newSlot)
  214. return;
  215. auto * oppositeSlot = &hLeft;
  216. auto oppositePanel = leftTradePanel;
  217. CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
  218. if(hCurSlot == hLeft)
  219. {
  220. oppositeSlot = &hRight;
  221. oppositePanel = rightTradePanel;
  222. }
  223. std::shared_ptr<CTradeableItem> oppositeNewSlot;
  224. for(const auto & slot : oppositePanel->slots)
  225. if(slot->serial == newSlot->serial)
  226. {
  227. oppositeNewSlot = slot;
  228. break;
  229. }
  230. assert(oppositeNewSlot);
  231. CTradeBase::onSlotClickPressed(oppositeNewSlot, *oppositeSlot);
  232. updateControls();
  233. updateSelected();
  234. redraw();
  235. }