From 097ef85eed1e7ea957f3204f4601a2ae63ba20f7 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Mon, 22 Aug 2005 21:57:34 +0000 Subject: Converted the buy & sell diaogs asserts into ifs to avoid game crashing, and made additional checks on buy sell events to have buttons enabled only when they're useful. --- src/game.cpp | 2 ++ src/gui/buy.cpp | 104 ++++++++++++++++++++++++++++++++++++------------------- src/gui/sell.cpp | 5 +++ 3 files changed, 76 insertions(+), 35 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index a0f2a518..05c5f13b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1574,7 +1574,9 @@ void do_parse() // Buy/Sell dialog case 0x00c4: buyDialog->setVisible(false); + buyDialog->reset(); sellDialog->setVisible(false); + sellDialog->reset(); buySellDialog->setVisible(true); current_npc = RFIFOL(2); break; diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp index 9d7cd7b2..fe8a525b 100644 --- a/src/gui/buy.cpp +++ b/src/gui/buy.cpp @@ -145,6 +145,11 @@ void BuyDialog::reset() { shopInventory.clear(); m_money = 0; + + // Reset Previous Selected Items to prevent failing asserts + itemList->setSelected(-1); + increaseButton->setEnabled(false); + decreaseButton->setEnabled(false); } void BuyDialog::addItem(short id, int price) @@ -181,7 +186,13 @@ void BuyDialog::action(const std::string& eventId) moneyLabel->setCaption(oss.str()); moneyLabel->adjustSize(); decreaseButton->setEnabled(false); + m_maxItems = m_money / shopInventory[selectedItem].price; + if (m_maxItems == 0) // The player cannot afford such an item + { + increaseButton->setEnabled(false); + buyButton->setEnabled(false); + } } else { @@ -229,8 +240,15 @@ void BuyDialog::action(const std::string& eventId) } else if (eventId == "+" && selectedItem > -1) { - assert(m_amountItems < m_maxItems); - m_amountItems++; + if (m_amountItems < m_maxItems) + { + m_amountItems++; + } + else + { + m_amountItems = m_maxItems; + } + slider->setValue(double(m_amountItems)/double(m_maxItems)); decreaseButton->setEnabled(true); @@ -239,6 +257,11 @@ void BuyDialog::action(const std::string& eventId) { increaseButton->setEnabled(false); } + if (m_maxItems == 0) // The player cannot afford such an item + { + decreaseButton->setEnabled(false); + buyButton->setEnabled(false); + } oss << m_amountItems; quantityLabel->setCaption(oss.str()); @@ -251,8 +274,14 @@ void BuyDialog::action(const std::string& eventId) } else if (eventId == "-" && selectedItem > -1) { - assert(m_amountItems > 0); - m_amountItems--; + if (m_amountItems > 0) + { + m_amountItems--; + } + else + { + m_amountItems = 0; + } slider->setValue(double(m_amountItems)/double(m_maxItems)); @@ -274,40 +303,45 @@ void BuyDialog::action(const std::string& eventId) } else if (eventId == "buy" && selectedItem > -1) { - // Attempt purchase - assert(m_amountItems > 0 && m_amountItems <= m_maxItems); - assert(selectedItem >= 0 && selectedItem < int(shopInventory.size())); - - WFIFOW(0) = net_w_value(0x00c8); - WFIFOW(2) = net_w_value(8); - WFIFOW(4) = net_w_value(m_amountItems); - WFIFOW(6) = net_w_value(shopInventory[selectedItem].id); - WFIFOSET(8); - - // update money ! - m_money -= m_amountItems * shopInventory[selectedItem].price; - - if (m_amountItems == m_maxItems) + // if purchase acceptable + if (m_amountItems > 0 && // Number of Items asked > 0 + m_amountItems <= m_maxItems && // Max of Items not overset + selectedItem >= 0 && // There is a selection > 0 + selectedItem < int(shopInventory.size()) ) // There a selection not too far in the list. { - m_maxItems = 0; - slider->setEnabled(false); - increaseButton->setEnabled(false); - } - else - { - m_maxItems = m_money / shopInventory[selectedItem].price; - } - - decreaseButton->setEnabled(false); - buyButton->setEnabled(false); - m_amountItems = 0; - slider->setValue(0); - quantityLabel->setCaption("O"); - quantityLabel->adjustSize(); + WFIFOW(0) = net_w_value(0x00c8); + WFIFOW(2) = net_w_value(8); + WFIFOW(4) = net_w_value(m_amountItems); + WFIFOW(6) = net_w_value(shopInventory[selectedItem].id); + WFIFOSET(8); + + // update money ! + m_money -= m_amountItems * shopInventory[selectedItem].price; + + if (m_amountItems == m_maxItems) + { + m_maxItems = 0; + slider->setEnabled(false); + increaseButton->setEnabled(false); + } + else + { + m_maxItems = m_money / shopInventory[selectedItem].price; + } + + decreaseButton->setEnabled(false); + buyButton->setEnabled(false); + + m_amountItems = 0; + slider->setValue(0); + quantityLabel->setCaption("O"); + quantityLabel->adjustSize(); + + moneyLabel->setCaption("price : 0 G"); + moneyLabel->adjustSize(); - moneyLabel->setCaption("price : 0 G"); - moneyLabel->adjustSize(); + } // End if purchase acceptable } else if (eventId == "quit") { diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index 6bac0bb1..2a2a591e 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -125,6 +125,11 @@ void SellDialog::reset() m_amountItems = 0; quantityLabel->setCaption("0"); quantityLabel->adjustSize(); + + // Reset Previous Selected Items to prevent failing asserts + itemList->setSelected(-1); + increaseButton->setEnabled(false); + decreaseButton->setEnabled(false); } void SellDialog::addItem(short index, int price) -- cgit v1.2.3-70-g09d2