2
0

CAltar.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * CAltarWindow.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 "CAltar.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/CursorHandler.h"
  14. #include "../widgets/Buttons.h"
  15. #include "../widgets/Slider.h"
  16. #include "../widgets/TextControls.h"
  17. #include "../CGameInfo.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../../CCallback.h"
  20. #include "../../lib/networkPacks/ArtifactLocation.h"
  21. #include "../../lib/CGeneralTextHandler.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/mapObjects/CGMarket.h"
  24. CAltarArtifacts::CAltarArtifacts(const IMarket * market, const CGHeroInstance * hero)
  25. : CTradeBase(market, hero)
  26. {
  27. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  28. labels.emplace_back(std::make_shared<CLabel>(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
  29. labels.emplace_back(std::make_shared<CLabel>(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
  30. selectedCost = std::make_shared<CLabel>(302, 500, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  31. selectedArt = std::make_shared<CArtPlace>(Point(280, 442));
  32. sacrificeAllButton = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTFILL.DEF"),
  33. CGI->generaltexth->zelp[571], std::bind(&CExpAltar::sacrificeAll, this));
  34. sacrificeAllButton->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  35. sacrificeBackpackButton = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("ALTEMBK.DEF"),
  36. CGI->generaltexth->zelp[570], std::bind(&CAltarArtifacts::sacrificeBackpack, this));
  37. sacrificeBackpackButton->block(hero->artifactsInBackpack.empty());
  38. arts = std::make_shared<CArtifactsOfHeroAltar>(Point(-365, -11));
  39. arts->setHero(hero);
  40. int slotNum = 0;
  41. for(auto & altarSlotPos : posSlotsAltar)
  42. {
  43. auto altarSlot = std::make_shared<CTradeableItem>(altarSlotPos, EType::ARTIFACT_PLACEHOLDER, -1, false, slotNum++);
  44. altarSlot->clickPressedCallback = std::bind(&CAltarArtifacts::onSlotClickPressed, this, _1);
  45. altarSlot->subtitle = "";
  46. items.front().emplace_back(altarSlot);
  47. }
  48. calcExpAltarForHero();
  49. deselect();
  50. };
  51. TExpType CAltarArtifacts::calcExpAltarForHero()
  52. {
  53. auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  54. TExpType expOnAltar(0);
  55. for(const auto art : artifactsOfHero->artifactsOnAltar)
  56. {
  57. int dmp, expOfArt;
  58. market->getOffer(art->artType->getId(), 0, dmp, expOfArt, EMarketMode::ARTIFACT_EXP);
  59. expOnAltar += expOfArt;
  60. }
  61. auto resultExp = hero->calculateXp(expOnAltar);
  62. expForHero->setText(std::to_string(resultExp));
  63. return resultExp;
  64. }
  65. void CAltarArtifacts::makeDeal()
  66. {
  67. std::vector<TradeItemSell> positions;
  68. for(const auto art : arts->artifactsOnAltar)
  69. {
  70. positions.push_back(hero->getSlotByInstance(art));
  71. }
  72. std::sort(positions.begin(), positions.end());
  73. std::reverse(positions.begin(), positions.end());
  74. LOCPLINT->cb->trade(market, EMarketMode::ARTIFACT_EXP, positions, std::vector<TradeItemBuy>(), std::vector<ui32>(), hero);
  75. arts->artifactsOnAltar.clear();
  76. for(auto item : items[0])
  77. {
  78. item->setID(-1);
  79. item->subtitle = "";
  80. }
  81. deal->block(true);
  82. calcExpAltarForHero();
  83. }
  84. void CAltarArtifacts::sacrificeAll()
  85. {
  86. std::vector<ConstTransitivePtr<CArtifactInstance>> artsForMove;
  87. for(const auto & slotInfo : arts->getHero()->artifactsWorn)
  88. {
  89. if(!slotInfo.second.locked && slotInfo.second.artifact->artType->isTradable())
  90. artsForMove.push_back(slotInfo.second.artifact);
  91. }
  92. for(auto artInst : artsForMove)
  93. moveArtToAltar(nullptr, artInst);
  94. arts->updateWornSlots();
  95. sacrificeBackpack();
  96. }
  97. void CAltarArtifacts::sacrificeBackpack()
  98. {
  99. while(!arts->visibleArtSet.artifactsInBackpack.empty())
  100. {
  101. if(!putArtOnAltar(nullptr, arts->visibleArtSet.artifactsInBackpack[0].artifact))
  102. break;
  103. };
  104. calcExpAltarForHero();
  105. }
  106. void CAltarArtifacts::setSelectedArtifact(const CArtifactInstance * art)
  107. {
  108. if(art)
  109. {
  110. selectedArt->setArtifact(art);
  111. int dmp, exp;
  112. market->getOffer(art->getTypeId(), 0, dmp, exp, EMarketMode::ARTIFACT_EXP);
  113. selectedCost->setText(std::to_string(hero->calculateXp(exp)));
  114. }
  115. else
  116. {
  117. selectedArt->setArtifact(nullptr);
  118. selectedCost->setText("");
  119. }
  120. }
  121. void CAltarArtifacts::moveArtToAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance * art)
  122. {
  123. if(putArtOnAltar(altarSlot, art))
  124. {
  125. CCS->curh->dragAndDropCursor(nullptr);
  126. arts->unmarkSlots();
  127. }
  128. }
  129. std::shared_ptr<CArtifactsOfHeroAltar> CAltarArtifacts::getAOHset() const
  130. {
  131. return arts;
  132. }
  133. bool CAltarArtifacts::putArtOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance * art)
  134. {
  135. if(!art->artType->isTradable())
  136. {
  137. logGlobal->warn("Cannot put special artifact on altar!");
  138. return false;
  139. }
  140. if(!altarSlot || altarSlot->id != -1)
  141. {
  142. int slotIndex = -1;
  143. while(items[0][++slotIndex]->id >= 0 && slotIndex + 1 < items[0].size());
  144. slotIndex = items[0][slotIndex]->id == -1 ? slotIndex : -1;
  145. if(slotIndex < 0)
  146. {
  147. logGlobal->warn("No free slots on altar!");
  148. return false;
  149. }
  150. altarSlot = items[0][slotIndex];
  151. }
  152. int dmp, exp;
  153. market->getOffer(art->artType->getId(), 0, dmp, exp, EMarketMode::ARTIFACT_EXP);
  154. exp = static_cast<int>(hero->calculateXp(exp));
  155. arts->artifactsOnAltar.insert(art);
  156. altarSlot->setArtInstance(art);
  157. altarSlot->subtitle = std::to_string(exp);
  158. deal->block(false);
  159. return true;
  160. };
  161. void CAltarArtifacts::onSlotClickPressed(std::shared_ptr<CTradeableItem> altarSlot)
  162. {
  163. const auto pickedArtInst = arts->getPickedArtifact();
  164. if(pickedArtInst)
  165. {
  166. arts->pickedArtMoveToAltar(ArtifactPosition::TRANSITION_POS);
  167. moveArtToAltar(altarSlot, pickedArtInst);
  168. }
  169. else if(const CArtifactInstance * art = altarSlot->getArtInstance())
  170. {
  171. const auto hero = arts->getHero();
  172. const auto slot = hero->getSlotByInstance(art);
  173. assert(slot != ArtifactPosition::PRE_FIRST);
  174. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero->id, slot),
  175. ArtifactLocation(hero->id, ArtifactPosition::TRANSITION_POS));
  176. arts->pickedArtFromSlot = slot;
  177. arts->artifactsOnAltar.erase(art);
  178. altarSlot->setID(-1);
  179. altarSlot->subtitle.clear();
  180. deal->block(!arts->artifactsOnAltar.size());
  181. }
  182. calcExpAltarForHero();
  183. }
  184. CAltarCreatures::CAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
  185. : CTradeBase(market, hero)
  186. {
  187. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  188. labels.emplace_back(std::make_shared<CLabel>(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW,
  189. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  190. labels.emplace_back(std::make_shared<CLabel>(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]));
  191. texts.emplace_back(std::make_unique<CTextBox>(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  192. lSubtitle = std::make_shared<CLabel>(180, 503, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  193. rSubtitle = std::make_shared<CLabel>(426, 503, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  194. offerSlider = std::make_shared<CSlider>(Point(231, 481), 137, std::bind(&CAltarCreatures::onOfferSliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  195. maxUnits = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[578], std::bind(&CSlider::scrollToMax, offerSlider));
  196. unitsOnAltar.resize(GameConstants::ARMY_SIZE, 0);
  197. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  198. sacrificeAllButton = std::make_shared<CButton>(
  199. Point(393, 520), AnimationPath::builtin("ALTARMY.DEF"), CGI->generaltexth->zelp[579], std::bind(&CExpAltar::sacrificeAll, this));
  200. // Hero creatures panel
  201. assert(leftTradePanel);
  202. leftTradePanel->moveBy(Point(45, 110));
  203. leftTradePanel->updateSlotsCallback = std::bind(&CCreaturesSelling::updateSubtitle, this);
  204. // Altar creatures panel
  205. rightTradePanel = std::make_shared<SCreaturesPanel>([this](std::shared_ptr<CTradeableItem> altarSlot) -> void
  206. {
  207. onSlotClickPressed(altarSlot, hRight);
  208. }, leftTradePanel->slots);
  209. rightTradePanel->moveBy(Point(334, 110));
  210. leftTradePanel->deleteSlotsCheck = rightTradePanel->deleteSlotsCheck = std::bind(&CCreaturesSelling::slotDeletingCheck, this, _1);
  211. readExpValues();
  212. calcExpAltarForHero();
  213. deselect();
  214. };
  215. void CAltarCreatures::readExpValues()
  216. {
  217. int dump;
  218. for(auto heroSlot : leftTradePanel->slots)
  219. {
  220. if(heroSlot->id >= 0)
  221. market->getOffer(heroSlot->id, 0, dump, expPerUnit[heroSlot->serial], EMarketMode::CREATURE_EXP);
  222. }
  223. }
  224. void CAltarCreatures::updateControls()
  225. {
  226. int sliderAmount = 0;
  227. if(hLeft)
  228. {
  229. std::optional<SlotID> lastSlot;
  230. for(auto slot = SlotID(0); slot.num < GameConstants::ARMY_SIZE; slot++)
  231. {
  232. if(hero->getStackCount(slot) > unitsOnAltar[slot.num])
  233. {
  234. if(lastSlot.has_value())
  235. {
  236. lastSlot = std::nullopt;
  237. break;
  238. }
  239. else
  240. {
  241. lastSlot = slot;
  242. }
  243. }
  244. }
  245. sliderAmount = hero->getStackCount(SlotID(hLeft->serial));
  246. if(lastSlot.has_value() && lastSlot.value() == SlotID(hLeft->serial))
  247. sliderAmount--;
  248. }
  249. offerSlider->setAmount(sliderAmount);
  250. offerSlider->block(!offerSlider->getAmount());
  251. if(hLeft)
  252. offerSlider->scrollTo(unitsOnAltar[hLeft->serial]);
  253. maxUnits->block(offerSlider->getAmount() == 0);
  254. }
  255. void CAltarCreatures::updateSubtitlesForSelected()
  256. {
  257. if(hLeft)
  258. lSubtitle->setText(std::to_string(offerSlider->getValue()));
  259. else
  260. lSubtitle->setText("");
  261. if(hRight)
  262. rSubtitle->setText(hRight->subtitle);
  263. else
  264. rSubtitle->setText("");
  265. }
  266. void CAltarCreatures::updateSlots()
  267. {
  268. rightTradePanel->deleteSlots();
  269. leftTradePanel->deleteSlots();
  270. assert(leftTradePanel->slots.size() == rightTradePanel->slots.size());
  271. readExpValues();
  272. leftTradePanel->updateSlots();
  273. }
  274. void CAltarCreatures::deselect()
  275. {
  276. CTradeBase::deselect();
  277. offerSlider->block(true);
  278. maxUnits->block(true);
  279. updateSubtitlesForSelected();
  280. }
  281. TExpType CAltarCreatures::calcExpAltarForHero()
  282. {
  283. TExpType expOnAltar(0);
  284. auto oneUnitExp = expPerUnit.begin();
  285. for(const auto units : unitsOnAltar)
  286. expOnAltar += *oneUnitExp++ * units;
  287. auto resultExp = hero->calculateXp(expOnAltar);
  288. expForHero->setText(std::to_string(resultExp));
  289. return resultExp;
  290. }
  291. void CAltarCreatures::makeDeal()
  292. {
  293. deselect();
  294. offerSlider->scrollTo(0);
  295. expForHero->setText(std::to_string(0));
  296. std::vector<TradeItemSell> ids;
  297. std::vector<ui32> toSacrifice;
  298. for(int i = 0; i < unitsOnAltar.size(); i++)
  299. {
  300. if(unitsOnAltar[i])
  301. {
  302. ids.push_back(SlotID(i));
  303. toSacrifice.push_back(unitsOnAltar[i]);
  304. }
  305. }
  306. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_EXP, ids, {}, toSacrifice, hero);
  307. for(int & units : unitsOnAltar)
  308. units = 0;
  309. for(auto heroSlot : rightTradePanel->slots)
  310. {
  311. heroSlot->setType(CREATURE_PLACEHOLDER);
  312. heroSlot->subtitle.clear();
  313. }
  314. }
  315. void CAltarCreatures::sacrificeAll()
  316. {
  317. std::optional<SlotID> lastSlot;
  318. for(auto heroSlot : leftTradePanel->slots)
  319. {
  320. auto stackCount = hero->getStackCount(SlotID(heroSlot->serial));
  321. if(stackCount > unitsOnAltar[heroSlot->serial])
  322. {
  323. if(!lastSlot.has_value())
  324. lastSlot = SlotID(heroSlot->serial);
  325. unitsOnAltar[heroSlot->serial] = stackCount;
  326. }
  327. }
  328. assert(lastSlot.has_value());
  329. unitsOnAltar[lastSlot.value().num]--;
  330. if(hRight)
  331. offerSlider->scrollTo(unitsOnAltar[hRight->serial]);
  332. for(auto altarSlot : rightTradePanel->slots)
  333. updateAltarSlot(altarSlot);
  334. updateSubtitlesForSelected();
  335. deal->block(calcExpAltarForHero() == 0);
  336. }
  337. void CAltarCreatures::updateAltarSlot(std::shared_ptr<CTradeableItem> slot)
  338. {
  339. auto units = unitsOnAltar[slot->serial];
  340. slot->setType(units > 0 ? CREATURE : CREATURE_PLACEHOLDER);
  341. slot->subtitle = units > 0 ?
  342. boost::str(boost::format(CGI->generaltexth->allTexts[122]) % std::to_string(hero->calculateXp(units * expPerUnit[slot->serial]))) : "";
  343. }
  344. void CAltarCreatures::onOfferSliderMoved(int newVal)
  345. {
  346. if(hLeft)
  347. unitsOnAltar[hLeft->serial] = newVal;
  348. if(hRight)
  349. updateAltarSlot(hRight);
  350. deal->block(calcExpAltarForHero() == 0);
  351. updateControls();
  352. updateSubtitlesForSelected();
  353. }
  354. void CAltarCreatures::onSlotClickPressed(std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSide)
  355. {
  356. if(hCurSide == newSlot)
  357. return;
  358. auto * oppositeSlot = &hLeft;
  359. auto oppositePanel = leftTradePanel;
  360. CTradeBase::onSlotClickPressed(newSlot, hCurSide);
  361. if(hCurSide == hLeft)
  362. {
  363. oppositeSlot = &hRight;
  364. oppositePanel = rightTradePanel;
  365. }
  366. std::shared_ptr<CTradeableItem> oppositeNewSlot;
  367. for(const auto & slot : oppositePanel->slots)
  368. if(slot->serial == newSlot->serial)
  369. {
  370. oppositeNewSlot = slot;
  371. break;
  372. }
  373. assert(oppositeNewSlot);
  374. CTradeBase::onSlotClickPressed(oppositeNewSlot, *oppositeSlot);
  375. updateControls();
  376. updateSubtitlesForSelected();
  377. redraw();
  378. }