CAltar.cpp 14 KB

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