CAltar.cpp 14 KB

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