1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/widgets/markets/CAltarCreatures.cpp

252 lines
7.8 KiB
C++
Raw Normal View History

2023-12-04 23:21:49 +02:00
/*
* CAltarCreatures.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CAltarCreatures.h"
#include "../../gui/CGuiHandler.h"
#include "../../widgets/Buttons.h"
#include "../../widgets/Slider.h"
#include "../../widgets/TextControls.h"
#include "../../CGameInfo.h"
#include "../../CPlayerInterface.h"
#include "../../../CCallback.h"
#include "../../../lib/CGeneralTextHandler.h"
#include "../../../lib/mapObjects/CGHeroInstance.h"
#include "../../../lib/mapObjects/CGMarket.h"
CAltarCreatures::CAltarCreatures(const IMarket * market, const CGHeroInstance * hero)
2024-02-29 12:02:39 +02:00
: CTradeBase(market, hero, [this](){return CAltarCreatures::getSelectionParams();})
2023-12-04 23:21:49 +02:00
{
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
2024-01-10 18:01:34 +02:00
deal = std::make_shared<CButton>(dealButtonPos, AnimationPath::builtin("ALTSACR.DEF"),
CGI->generaltexth->zelp[584], [this]() {CAltarCreatures::makeDeal();});
2023-12-04 23:21:49 +02:00
labels.emplace_back(std::make_shared<CLabel>(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW,
boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
labels.emplace_back(std::make_shared<CLabel>(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]));
texts.emplace_back(std::make_unique<CTextBox>(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
offerSlider = std::make_shared<CSlider>(Point(231, 481), 137, std::bind(&CAltarCreatures::onOfferSliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
2024-02-19 23:40:43 +02:00
maxAmount = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[578], std::bind(&CSlider::scrollToMax, offerSlider));
2023-12-04 23:21:49 +02:00
unitsOnAltar.resize(GameConstants::ARMY_SIZE, 0);
expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
sacrificeAllButton = std::make_shared<CButton>(
Point(393, 520), AnimationPath::builtin("ALTARMY.DEF"), CGI->generaltexth->zelp[579], std::bind(&CExperienceAltar::sacrificeAll, this));
// Hero creatures panel
2024-02-25 22:58:53 +02:00
assert(bidTradePanel);
bidTradePanel->moveTo(pos.topLeft() + Point(45, 110));
2024-02-27 13:39:50 +02:00
bidTradePanel->selectedSlot->moveTo(pos.topLeft() + Point(149, 422));
bidTradePanel->selectedSlot->subtitle->moveBy(Point(0, 3));
2024-02-25 22:58:53 +02:00
for(const auto & slot : bidTradePanel->slots)
2024-01-11 13:52:12 +02:00
slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot) {CAltarCreatures::onSlotClickPressed(heroSlot, hLeft);};
2023-12-04 23:21:49 +02:00
// Altar creatures panel
2024-02-25 22:58:53 +02:00
offerTradePanel = std::make_shared<CreaturesPanel>([this](const std::shared_ptr<CTradeableItem> & altarSlot)
2023-12-04 23:21:49 +02:00
{
CAltarCreatures::onSlotClickPressed(altarSlot, hRight);
2024-02-25 22:58:53 +02:00
}, bidTradePanel->slots);
offerTradePanel->moveTo(pos.topLeft() + Point(334, 110));
2024-02-27 13:39:50 +02:00
offerTradePanel->selectedSlot->moveTo(pos.topLeft() + Point(395, 422));
offerTradePanel->selectedSlot->subtitle->moveBy(Point(0, 3));
2024-02-25 22:58:53 +02:00
offerTradePanel->updateSlotsCallback = [this]()
2024-02-22 22:49:30 +02:00
{
2024-02-25 22:58:53 +02:00
for(const auto & altarSlot : offerTradePanel->slots)
2024-02-22 22:49:30 +02:00
updateAltarSlot(altarSlot);
};
2024-02-25 22:58:53 +02:00
bidTradePanel->deleteSlotsCheck = offerTradePanel->deleteSlotsCheck = std::bind(&CCreaturesSelling::slotDeletingCheck, this, _1);
2024-02-19 23:40:43 +02:00
2023-12-04 23:21:49 +02:00
readExpValues();
CAltarCreatures::deselect();
};
void CAltarCreatures::readExpValues()
{
2024-02-19 23:40:43 +02:00
int bidQty = 0;
2024-02-25 22:58:53 +02:00
for(const auto & heroSlot : bidTradePanel->slots)
2023-12-04 23:21:49 +02:00
{
if(heroSlot->id >= 0)
2024-02-19 23:40:43 +02:00
market->getOffer(heroSlot->id, 0, bidQty, expPerUnit[heroSlot->serial], EMarketMode::CREATURE_EXP);
2023-12-04 23:21:49 +02:00
}
}
void CAltarCreatures::updateControls()
{
int sliderAmount = 0;
if(hLeft)
{
std::optional<SlotID> lastSlot;
for(auto slot = SlotID(0); slot.num < GameConstants::ARMY_SIZE; slot++)
{
if(hero->getStackCount(slot) > unitsOnAltar[slot.num])
{
if(lastSlot.has_value())
{
lastSlot = std::nullopt;
break;
}
else
{
lastSlot = slot;
}
}
}
sliderAmount = hero->getStackCount(SlotID(hLeft->serial));
if(lastSlot.has_value() && lastSlot.value() == SlotID(hLeft->serial))
sliderAmount--;
}
offerSlider->setAmount(sliderAmount);
offerSlider->block(!offerSlider->getAmount());
if(hLeft)
offerSlider->scrollTo(unitsOnAltar[hLeft->serial]);
2024-02-19 23:40:43 +02:00
maxAmount->block(offerSlider->getAmount() == 0);
2023-12-04 23:21:49 +02:00
}
void CAltarCreatures::updateSlots()
{
2024-02-29 22:33:12 +02:00
CExperienceAltar::updateSlots();
2024-02-25 22:58:53 +02:00
assert(bidTradePanel->slots.size() == offerTradePanel->slots.size());
2023-12-04 23:21:49 +02:00
}
void CAltarCreatures::deselect()
{
CTradeBase::deselect();
2024-02-03 22:59:05 +02:00
updateSelected();
2024-02-19 23:40:43 +02:00
expForHero->setText(std::to_string(0));
2023-12-04 23:21:49 +02:00
}
TExpType CAltarCreatures::calcExpAltarForHero()
{
TExpType expOnAltar(0);
auto oneUnitExp = expPerUnit.begin();
for(const auto units : unitsOnAltar)
expOnAltar += *oneUnitExp++ * units;
auto resultExp = hero->calculateXp(expOnAltar);
expForHero->setText(std::to_string(resultExp));
return resultExp;
}
void CAltarCreatures::makeDeal()
{
deselect();
std::vector<TradeItemSell> ids;
std::vector<ui32> toSacrifice;
for(int i = 0; i < unitsOnAltar.size(); i++)
{
if(unitsOnAltar[i])
{
ids.push_back(SlotID(i));
toSacrifice.push_back(unitsOnAltar[i]);
}
}
LOCPLINT->cb->trade(market, EMarketMode::CREATURE_EXP, ids, {}, toSacrifice, hero);
for(int & units : unitsOnAltar)
units = 0;
2024-02-25 22:58:53 +02:00
for(auto heroSlot : offerTradePanel->slots)
2023-12-04 23:21:49 +02:00
{
heroSlot->setType(EType::CREATURE_PLACEHOLDER);
2024-02-27 13:39:50 +02:00
heroSlot->subtitle->clear();
2023-12-04 23:21:49 +02:00
}
}
2024-02-29 12:02:39 +02:00
CTradeBase::SelectionParams CAltarCreatures::getSelectionParams() const
2024-02-27 13:39:50 +02:00
{
std::optional<SelectionParamOneSide> bidSelected = std::nullopt;
std::optional<SelectionParamOneSide> offerSelected = std::nullopt;
if(hLeft)
bidSelected = SelectionParamOneSide {std::to_string(offerSlider->getValue()), CGI->creatures()->getByIndex(hLeft->id)->getIconIndex()};
if(hRight && offerSlider->getValue() > 0)
offerSelected = SelectionParamOneSide {hRight->subtitle->getText(), CGI->creatures()->getByIndex(hRight->id)->getIconIndex()};
return std::make_tuple(bidSelected, offerSelected);
}
2023-12-04 23:21:49 +02:00
void CAltarCreatures::sacrificeAll()
{
std::optional<SlotID> lastSlot;
2024-02-25 22:58:53 +02:00
for(auto heroSlot : bidTradePanel->slots)
2023-12-04 23:21:49 +02:00
{
auto stackCount = hero->getStackCount(SlotID(heroSlot->serial));
if(stackCount > unitsOnAltar[heroSlot->serial])
{
if(!lastSlot.has_value())
lastSlot = SlotID(heroSlot->serial);
unitsOnAltar[heroSlot->serial] = stackCount;
}
}
2024-02-03 22:59:05 +02:00
if(hero->needsLastStack())
{
assert(lastSlot.has_value());
unitsOnAltar[lastSlot.value().num]--;
}
2023-12-04 23:21:49 +02:00
if(hRight)
offerSlider->scrollTo(unitsOnAltar[hRight->serial]);
2024-02-25 22:58:53 +02:00
offerTradePanel->updateSlots();
2024-02-03 22:59:05 +02:00
updateSelected();
2023-12-04 23:21:49 +02:00
deal->block(calcExpAltarForHero() == 0);
}
2024-02-22 22:49:30 +02:00
void CAltarCreatures::updateAltarSlot(const std::shared_ptr<CTradeableItem> & slot)
2023-12-04 23:21:49 +02:00
{
auto units = unitsOnAltar[slot->serial];
slot->setType(units > 0 ? EType::CREATURE : EType::CREATURE_PLACEHOLDER);
2024-02-27 13:39:50 +02:00
slot->subtitle->setText(units > 0 ?
boost::str(boost::format(CGI->generaltexth->allTexts[122]) % std::to_string(hero->calculateXp(units * expPerUnit[slot->serial]))) : "");
2023-12-04 23:21:49 +02:00
}
void CAltarCreatures::onOfferSliderMoved(int newVal)
{
if(hLeft)
unitsOnAltar[hLeft->serial] = newVal;
if(hRight)
updateAltarSlot(hRight);
deal->block(calcExpAltarForHero() == 0);
updateControls();
2024-02-03 22:59:05 +02:00
updateSelected();
2024-02-19 23:40:43 +02:00
redraw();
2023-12-04 23:21:49 +02:00
}
2024-02-03 22:59:05 +02:00
void CAltarCreatures::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
2023-12-04 23:21:49 +02:00
{
2024-02-03 22:59:05 +02:00
if(hCurSlot == newSlot)
2023-12-04 23:21:49 +02:00
return;
auto * oppositeSlot = &hLeft;
2024-02-25 22:58:53 +02:00
auto oppositePanel = bidTradePanel;
2024-02-03 22:59:05 +02:00
CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
if(hCurSlot == hLeft)
2023-12-04 23:21:49 +02:00
{
oppositeSlot = &hRight;
2024-02-25 22:58:53 +02:00
oppositePanel = offerTradePanel;
2023-12-04 23:21:49 +02:00
}
std::shared_ptr<CTradeableItem> oppositeNewSlot;
for(const auto & slot : oppositePanel->slots)
if(slot->serial == newSlot->serial)
{
oppositeNewSlot = slot;
break;
}
assert(oppositeNewSlot);
CTradeBase::onSlotClickPressed(oppositeNewSlot, *oppositeSlot);
updateControls();
2024-02-03 22:59:05 +02:00
updateSelected();
2023-12-04 23:21:49 +02:00
redraw();
}