CAltar.cpp 14 KB

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