diff options
author | Fedja Beader <fedja@protonmail.ch> | 2025-04-07 21:00:48 +0000 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2025-04-07 21:00:48 +0000 |
commit | a325968dab0c5897631d3430b5d3670cc1acd6c7 (patch) | |
tree | 299b39cadf6383b34b63769ae7e75f8192d34f54 | |
parent | 3d57093c605dbebc9a6a86b7666db802ad637ce1 (diff) | |
download | mv-a325968dab0c5897631d3430b5d3670cc1acd6c7.tar.gz mv-a325968dab0c5897631d3430b5d3670cc1acd6c7.tar.bz2 mv-a325968dab0c5897631d3430b5d3670cc1acd6c7.tar.xz mv-a325968dab0c5897631d3430b5d3670cc1acd6c7.zip |
Add total weight accounting for the "shopping list" of herc shops.
.. this will prevent the shopping list from going above your free
carry weight, and thus the buy action failing.
Note: I don't particularly like this implemention due to code repetition. But perfection is enemy of progress.
TODO:
1) add the same for total price (other MR?)
2) maybe do a small refactor + add comments where necessary (other MR?)
3) ~~free weight display~~ done
Squashed with:
* buydialog: Add free weight display to money label
+ fix free weight going below 0
****
mana/plus!152
-rw-r--r-- | src/gui/windows/buydialog.cpp | 30 | ||||
-rw-r--r-- | src/gui/windows/buydialog.h | 2 |
2 files changed, 23 insertions, 9 deletions
diff --git a/src/gui/windows/buydialog.cpp b/src/gui/windows/buydialog.cpp index f30fb92e4..038ddb3ac 100644 --- a/src/gui/windows/buydialog.cpp +++ b/src/gui/windows/buydialog.cpp @@ -226,6 +226,7 @@ BuyDialog::BuyDialog() : mMoney(0), mAmountItems(0), mMaxItems(0), + mTotalPurchaseWeight(0), mAdvanced(false) { init(); @@ -248,6 +249,7 @@ BuyDialog::BuyDialog(const BeingId npcId, mMoney(0), mAmountItems(0), mMaxItems(0), + mTotalPurchaseWeight(0), mAdvanced(Net::getNetworkType() != ServerType::TMWATHENA) { init(); @@ -272,6 +274,7 @@ BuyDialog::BuyDialog(const std::string &nick, mMoney(0), mAmountItems(0), mMaxItems(0), + mTotalPurchaseWeight(0), mAdvanced(false) { init(); @@ -296,6 +299,7 @@ BuyDialog::BuyDialog(const Being *const being, mMoney(0), mAmountItems(0), mMaxItems(0), + mTotalPurchaseWeight(0), mAdvanced(true) { init(); @@ -338,8 +342,8 @@ void BuyDialog::init() "%d / %d", mAmountItems, mMaxItems)); mQuantityLabel->setAlignment(Graphics::CENTER); mMoneyLabel = new Label(this, strprintf( - // TRANSLATORS: buy dialog label - _("Price: %s / Total: %s"), "", "")); + // TRANSLATORS: buy dialog label, price, remaining money & free weight + _("Price: %s, Remaining: %s & %s"), "", "", "")); mAmountField = new IntTextField(this, 1, 1, 123, Enable_true, 0); mAmountField->setActionEventId("amount"); @@ -473,6 +477,7 @@ void BuyDialog::reset() mShopItemList->setSelected(-1); mSlider->setValue(0); + mTotalPurchaseWeight = 0; setMoney(0); } @@ -637,6 +642,8 @@ void BuyDialog::action(const ActionEvent &event) else if (mNpcId == fromInt(Vending, BeingId)) { item->increaseUsedQuantity(mAmountItems); + const int itemWeight = item->getInfo().getWeight(); + mTotalPurchaseWeight += mAmountItems * itemWeight; item->update(); if (mConfirmButton != nullptr) mConfirmButton->setEnabled(true); @@ -650,6 +657,8 @@ void BuyDialog::action(const ActionEvent &event) if (mAdvanced) { item->increaseUsedQuantity(mAmountItems); + const int itemWeight = item->getInfo().getWeight(); + mTotalPurchaseWeight += mAmountItems * itemWeight; item->update(); if (mConfirmButton != nullptr) mConfirmButton->setEnabled(true); @@ -745,6 +754,10 @@ void BuyDialog::updateButtonsAndLabels() { const int selectedItem = mShopItemList->getSelected(); int price = 0; + int freeWeight + = PlayerInfo::getAttribute(Attributes::MAX_WEIGHT) + - PlayerInfo::getAttribute(Attributes::TOTAL_WEIGHT) + - mTotalPurchaseWeight; if (selectedItem > -1) { @@ -765,10 +778,7 @@ void BuyDialog::updateButtonsAndLabels() const int itemWeight = item->getInfo().getWeight(); if (itemWeight != 0) { - const int myFreeWeight - = PlayerInfo::getAttribute(Attributes::MAX_WEIGHT) - - PlayerInfo::getAttribute(Attributes::TOTAL_WEIGHT); - const int canCarry = myFreeWeight / itemWeight; + const int canCarry = freeWeight / itemWeight; mMaxItems = std::min(mMaxItems, canCarry); } @@ -786,6 +796,7 @@ void BuyDialog::updateButtonsAndLabels() if (mAmountItems > mMaxItems) mAmountItems = mMaxItems; + freeWeight -= mAmountItems * itemWeight; price = mAmountItems * itemPrice; } } @@ -802,10 +813,11 @@ void BuyDialog::updateButtonsAndLabels() mAmountField->setEnabled(mAmountItems > 0); mQuantityLabel->setCaption(strprintf("%d / %d", mAmountItems, mMaxItems)); - // TRANSLATORS: buy dialog label - mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"), + // TRANSLATORS: buy dialog label, price, remaining money & free weight + mMoneyLabel->setCaption(strprintf(_("Price: %s, Remaining: %s & %s"), UnitsDb::formatCurrency(mCurrency, price).c_str(), - UnitsDb::formatCurrency(mCurrency, mMoney - price).c_str())); + UnitsDb::formatCurrency(mCurrency, mMoney - price).c_str(), + UnitsDb::formatWeight(freeWeight).c_str())); } void BuyDialog::setVisible(Visible visible) diff --git a/src/gui/windows/buydialog.h b/src/gui/windows/buydialog.h index 4535e1201..969734aec 100644 --- a/src/gui/windows/buydialog.h +++ b/src/gui/windows/buydialog.h @@ -202,6 +202,8 @@ class BuyDialog final : public Window, int mMoney; int mAmountItems; int mMaxItems; + // combined weight of all items added to shopping list + int mTotalPurchaseWeight; bool mAdvanced; }; |