From 4c57a567eec8b3df0b0009b5520d647cccb83338 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 23 Jun 2013 12:21:55 +0300 Subject: Add support for protecting items. Protected item cant be selled, traded, dropped, used. Protect/unprotect item can be from context menu. --- src/client.cpp | 1 + src/dropshortcut.cpp | 17 +++-- src/gui/inventorywindow.cpp | 13 ++++ src/gui/popupmenu.cpp | 155 +++++++++++++++++++++++++++------------- src/gui/popupmenu.h | 2 + src/gui/selldialog.cpp | 6 +- src/gui/tradewindow.cpp | 5 +- src/gui/widgets/shoplistbox.cpp | 5 +- src/playerinfo.cpp | 43 +++++++++++ src/playerinfo.h | 8 +++ 10 files changed, 198 insertions(+), 57 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 029c9c417..54c7d06e9 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1082,6 +1082,7 @@ int Client::gameExec() mServerName = mCurrentServer.hostname; initServerConfig(mCurrentServer.hostname); initFeatures(); + PlayerInfo::loadData(); loginData.registerUrl = mCurrentServer.registerUrl; if (!mCurrentServer.onlineListUrl.empty()) mOnlineListUrl = mCurrentServer.onlineListUrl; diff --git a/src/dropshortcut.cpp b/src/dropshortcut.cpp index d0e49ab5f..87e992b78 100644 --- a/src/dropshortcut.cpp +++ b/src/dropshortcut.cpp @@ -103,6 +103,8 @@ void DropShortcut::dropFirst() const const int itemId = getItem(0); const unsigned char itemColor = getItemColor(0); + if (PlayerInfo::isItemProtected(itemId)) + return; if (itemId > 0) { @@ -161,10 +163,14 @@ bool DropShortcut::dropItem(const int cnt) unsigned char itemColor = 1; while (mLastDropIndex < DROP_SHORTCUT_ITEMS && itemId < 1) { - itemId = getItem(mLastDropIndex); - itemColor = getItemColor(mLastDropIndex); + if (!PlayerInfo::isItemProtected(itemId)) + { + itemId = getItem(mLastDropIndex); + itemColor = getItemColor(mLastDropIndex); + } mLastDropIndex ++; } + if (itemId > 0) { const Item *const item = inv->findItem(itemId, itemColor); @@ -181,8 +187,11 @@ bool DropShortcut::dropItem(const int cnt) { while (mLastDropIndex < DROP_SHORTCUT_ITEMS && itemId < 1) { - itemId = getItem(mLastDropIndex); - itemColor = getItemColor(mLastDropIndex); + if (!PlayerInfo::isItemProtected(itemId)) + { + itemId = getItem(mLastDropIndex); + itemColor = getItemColor(mLastDropIndex); + } mLastDropIndex++; } if (itemId > 0) diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index f659b8693..553313289 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -360,6 +360,8 @@ void InventoryWindow::action(const gcn::ActionEvent &event) } else { + if (PlayerInfo::isItemProtected(item->getId())) + return; Net::getInventoryHandler()->useItem(item); } } @@ -374,11 +376,16 @@ void InventoryWindow::action(const gcn::ActionEvent &event) } else { + if (PlayerInfo::isItemProtected(item->getId())) + return; Net::getInventoryHandler()->useItem(item); } } else if (eventId == "drop") { + if (PlayerInfo::isItemProtected(item->getId())) + return; + if (isStorageActive()) { Net::getInventoryHandler()->moveItem2(Inventory::INVENTORY, @@ -503,6 +510,8 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event) } else if (mod2 && mInventory->isMainInventory()) { + if (PlayerInfo::isItemProtected(item->getId())) + return; if (event.getButton() == gcn::MouseEvent::RIGHT) { ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, @@ -525,6 +534,8 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event) } else if (tradeWindow && tradeWindow->isWindowVisible()) { + if (PlayerInfo::isItemProtected(item->getId())) + return; ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, tradeWindow, item); } @@ -539,6 +550,8 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event) } else { + if (PlayerInfo::isItemProtected(item->getId())) + return; Net::getInventoryHandler()->useItem(item); } } diff --git a/src/gui/popupmenu.cpp b/src/gui/popupmenu.cpp index b6272a4e4..ef71160d7 100644 --- a/src/gui/popupmenu.cpp +++ b/src/gui/popupmenu.cpp @@ -448,6 +448,7 @@ void PopupMenu::showPopup(const int x, const int y, // TRANSLATORS: popup menu item mBrowserBox->addRow("pickup", _("Pick up")); } + addProtection(); // TRANSLATORS: popup menu item mBrowserBox->addRow("chat", _("Add to chat")); mBrowserBox->addRow("##3---"); @@ -962,7 +963,8 @@ void PopupMenu::handleLink(const std::string &link, } else { - Net::getInventoryHandler()->useItem(item); + if (!PlayerInfo::isItemProtected(item->getId())) + Net::getInventoryHandler()->useItem(item); } } } @@ -1066,12 +1068,16 @@ void PopupMenu::handleLink(const std::string &link, } else if (link == "drop" && mItem) { - ItemAmountWindow::showWindow(ItemAmountWindow::ItemDrop, - inventoryWindow, mItem); + if (!PlayerInfo::isItemProtected(mItem->getId())) + { + ItemAmountWindow::showWindow(ItemAmountWindow::ItemDrop, + inventoryWindow, mItem); + } } else if (link == "drop all" && mItem) { - Net::getInventoryHandler()->dropItem(mItem, mItem->getQuantity()); + if (!PlayerInfo::isItemProtected(mItem->getId())) + Net::getInventoryHandler()->dropItem(mItem, mItem->getQuantity()); } else if (link == "store" && mItem) { @@ -1107,12 +1113,15 @@ void PopupMenu::handleLink(const std::string &link, } else if (link == "addtrade" && mItem) { - ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, - tradeWindow, mItem); + if (!PlayerInfo::isItemProtected(mItem->getId())) + { + ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, + tradeWindow, mItem); + } } else if (link == "addtrade 10" && mItem) { - if (tradeWindow) + if (tradeWindow && !PlayerInfo::isItemProtected(mItem->getId())) { int cnt = 10; if (cnt > mItem->getQuantity()) @@ -1122,17 +1131,17 @@ void PopupMenu::handleLink(const std::string &link, } else if (link == "addtrade half" && mItem) { - if (tradeWindow) + if (tradeWindow && !PlayerInfo::isItemProtected(mItem->getId())) tradeWindow->tradeItem(mItem, mItem->getQuantity() / 2, true); } else if (link == "addtrade all-1" && mItem) { - if (tradeWindow) + if (tradeWindow && !PlayerInfo::isItemProtected(mItem->getId())) tradeWindow->tradeItem(mItem, mItem->getQuantity() - 1, true); } else if (link == "addtrade all" && mItem) { - if (tradeWindow) + if (tradeWindow && !PlayerInfo::isItemProtected(mItem->getId())) tradeWindow->tradeItem(mItem, mItem->getQuantity(), true); } else if (link == "retrieve" && mItem) @@ -1167,6 +1176,14 @@ void PopupMenu::handleLink(const std::string &link, mItem->getInvIndex(), mItem->getQuantity(), Inventory::INVENTORY); } + else if (link == "protect item" && mItemId) + { + PlayerInfo::protectItem(mItemId); + } + else if (link == "unprotect item" && mItemId) + { + PlayerInfo::unprotectItem(mItemId); + } else if (link == "party" && being && being->getType() == ActorSprite::PLAYER) { @@ -1769,10 +1786,11 @@ void PopupMenu::showPopup(Window *const parent, const int x, const int y, mBrowserBox->clearRows(); const int cnt = item->getQuantity(); + const bool isProtected = PlayerInfo::isItemProtected(mItemId); if (isInventory) { - if (tradeWindow && tradeWindow->isWindowVisible()) + if (tradeWindow && tradeWindow->isWindowVisible() && !isProtected) { // TRANSLATORS: popup menu item mBrowserBox->addRow("addtrade", _("Add to trade")); @@ -1828,21 +1846,27 @@ void PopupMenu::showPopup(Window *const parent, const int x, const int y, } else { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("use", _("Use")); + if (!isProtected) + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("use", _("Use")); + } } - if (cnt > 1) + if (!isProtected) { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop", _("Drop...")); - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop all", _("Drop all")); - } - else - { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop", _("Drop")); + if (cnt > 1) + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop", _("Drop...")); + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop all", _("Drop all")); + } + else + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop", _("Drop")); + } } if (Net::getInventoryHandler()->canSplit(item)) @@ -1871,6 +1895,7 @@ void PopupMenu::showPopup(Window *const parent, const int x, const int y, mBrowserBox->addRow("retrieve all", _("Retrieve all")); } } + addProtection(); if (config.getBoolValue("enablePickupFilter")) { mNick = item->getName(); @@ -1907,8 +1932,12 @@ void PopupMenu::showItemPopup(const int x, const int y, const int itemId, mY = y; mBrowserBox->clearRows(); - // TRANSLATORS: popup menu item - mBrowserBox->addRow("use", _("Use")); + if (!PlayerInfo::isItemProtected(mItemId)) + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("use", _("Use")); + } + addProtection(); mBrowserBox->addRow("##3---"); // TRANSLATORS: popup menu item mBrowserBox->addRow("cancel", _("Cancel")); @@ -1937,6 +1966,7 @@ void PopupMenu::showItemPopup(const int x, const int y, Item *const item) if (item) { + const bool isProtected = PlayerInfo::isItemProtected(mItemId); if (item->isEquipment()) { if (item->isEquipped()) @@ -1952,21 +1982,27 @@ void PopupMenu::showItemPopup(const int x, const int y, Item *const item) } else { + if (!isProtected) + { // TRANSLATORS: popup menu item - mBrowserBox->addRow("use", _("Use")); + mBrowserBox->addRow("use", _("Use")); + } } - if (item->getQuantity() > 1) + if (!isProtected) { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop", _("Drop...")); - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop all", _("Drop all")); - } - else - { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop", _("Drop")); + if (item->getQuantity() > 1) + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop", _("Drop...")); + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop all", _("Drop all")); + } + else + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop", _("Drop")); + } } if (Net::getInventoryHandler()->canSplit(item)) @@ -1991,6 +2027,7 @@ void PopupMenu::showItemPopup(const int x, const int y, Item *const item) addPickupFilter(mNick); } } + addProtection(); mBrowserBox->addRow("##3---"); // TRANSLATORS: popup menu item mBrowserBox->addRow("cancel", _("Cancel")); @@ -2010,6 +2047,7 @@ void PopupMenu::showDropPopup(const int x, const int y, Item *const item) { mItemId = item->getId(); mItemColor = item->getColor(); + const bool isProtected = PlayerInfo::isItemProtected(mItemId); if (item->isEquipment()) { @@ -2026,21 +2064,27 @@ void PopupMenu::showDropPopup(const int x, const int y, Item *const item) } else { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("use", _("Use")); + if (!isProtected) + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("use", _("Use")); + } } - if (item->getQuantity() > 1) - { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop", _("Drop...")); - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop all", _("Drop all")); - } - else + if (!isProtected) { - // TRANSLATORS: popup menu item - mBrowserBox->addRow("drop", _("Drop")); + if (item->getQuantity() > 1) + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop", _("Drop...")); + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop all", _("Drop all")); + } + else + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("drop", _("Drop")); + } } if (Net::getInventoryHandler()->canSplit(item)) @@ -2054,6 +2098,7 @@ void PopupMenu::showDropPopup(const int x, const int y, Item *const item) // TRANSLATORS: popup menu item mBrowserBox->addRow("store", _("Store")); } + addProtection(); // TRANSLATORS: popup menu item mBrowserBox->addRow("chat", _("Add to chat")); if (config.getBoolValue("enablePickupFilter")) @@ -2635,6 +2680,20 @@ void PopupMenu::clear() mTextField = nullptr; } +void PopupMenu::addProtection() +{ + if (PlayerInfo::isItemProtected(mItemId)) + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("unprotect item", _("Unprotect item")); + } + else + { + // TRANSLATORS: popup menu item + mBrowserBox->addRow("protect item", _("Protect item")); + } +} + RenameListener::RenameListener() : gcn::ActionListener(), mMapItem(nullptr), diff --git a/src/gui/popupmenu.h b/src/gui/popupmenu.h index 3eac9355a..3b615afec 100644 --- a/src/gui/popupmenu.h +++ b/src/gui/popupmenu.h @@ -203,6 +203,8 @@ class PopupMenu final : public Popup, public LinkHandler void addPickupFilter(const std::string &name); + void addProtection(); + BrowserBox *mBrowserBox; ScrollArea *mScrollArea; diff --git a/src/gui/selldialog.cpp b/src/gui/selldialog.cpp index 8006355bc..c61dec541 100644 --- a/src/gui/selldialog.cpp +++ b/src/gui/selldialog.cpp @@ -22,6 +22,7 @@ #include "gui/selldialog.h" +#include "playerinfo.h" #include "shopitem.h" #include "units.h" @@ -228,9 +229,11 @@ void SellDialog::action(const gcn::ActionEvent &event) { if (mNpcId != -1) { + ShopItem *const item = mShopItems->at(selectedItem); + if (PlayerInfo::isItemProtected(item->getId())) + return; if (eventId == "presell") { - const ShopItem *const item = mShopItems->at(selectedItem); const ItemInfo &info = ItemDB::get(item->getId()); if (info.isProtected()) { @@ -245,7 +248,6 @@ void SellDialog::action(const gcn::ActionEvent &event) } } // Attempt sell - ShopItem *const item = mShopItems->at(selectedItem); mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice(); mMaxItems -= mAmountItems; diff --git a/src/gui/tradewindow.cpp b/src/gui/tradewindow.cpp index 3c1ce60bb..a71b26f60 100644 --- a/src/gui/tradewindow.cpp +++ b/src/gui/tradewindow.cpp @@ -458,8 +458,11 @@ void TradeWindow::initTrade(const std::string &nick) bool TradeWindow::checkItem(const Item *const item) const { + const int itemId = item->getId(); + if (PlayerInfo::isItemProtected(itemId)) + return false; const Item *const tItem = mMyInventory->findItem( - item->getId(), item->getColor()); + itemId, item->getColor()); if (tItem && (tItem->getQuantity() > 1 || item->getQuantity() > 1)) diff --git a/src/gui/widgets/shoplistbox.cpp b/src/gui/widgets/shoplistbox.cpp index 615b93010..827bb6755 100644 --- a/src/gui/widgets/shoplistbox.cpp +++ b/src/gui/widgets/shoplistbox.cpp @@ -24,6 +24,7 @@ #include "client.h" #include "configuration.h" +#include "playerinfo.h" #include "shopitem.h" #include "gui/itempopup.h" @@ -107,8 +108,8 @@ void ShopListBox::draw(gcn::Graphics *gcnGraphics) gcn::Color* backgroundColor = &mBackgroundColor; ShopItem *const item = mShopItems->at(i); - if (mShopItems && item && - mPlayerMoney < item->getPrice() && mPriceCheck) + if ((mShopItems && item && mPlayerMoney < item->getPrice() + && mPriceCheck) || PlayerInfo::isItemProtected(item->getId())) { if (i != mSelected) { diff --git a/src/playerinfo.cpp b/src/playerinfo.cpp index 673e6f8cf..f2e933a24 100644 --- a/src/playerinfo.cpp +++ b/src/playerinfo.cpp @@ -22,6 +22,7 @@ #include "playerinfo.h" #include "client.h" +#include "configuration.h" #include "depricatedevent.h" #include "inventory.h" #include "depricatedlistener.h" @@ -53,6 +54,7 @@ signed char mSpecialRechargeUpdateNeeded = 0; bool mTrading = false; int mLevelProgress = 0; +std::set mProtectedItems; // --- Triggers --------------------------------------------------------------- @@ -313,6 +315,13 @@ void deinit() clearInventory(); } +void loadData() +{ + mProtectedItems.clear(); + splitToIntSet(mProtectedItems, + serverConfig.getStringValue("protectedItems"), ','); +} + void clear() { mData.mSkills.clear(); @@ -344,4 +353,38 @@ void stateChange(const int state) } } +static void saveProtectedItems() +{ + std::string str; + std::set::const_iterator it = mProtectedItems.begin(); + std::set::const_iterator it_end = mProtectedItems.end(); + if (it != it_end) + str.append(toString(*it)); + ++ it; + while (it != it_end) + { + str.append(",").append(toString(*it)); + ++ it; + } + serverConfig.setValue("protectedItems", str); + serverConfig.write(); +} + +void protectItem(const int id) +{ + mProtectedItems.insert(id); + saveProtectedItems(); +} + +void unprotectItem(const int id) +{ + mProtectedItems.erase(id); + saveProtectedItems(); +} + +bool isItemProtected(const int id) +{ + return mProtectedItems.find(id) != mProtectedItems.end(); +} + } // namespace PlayerInfo diff --git a/src/playerinfo.h b/src/playerinfo.h index 7eac4ef98..6a1a25f81 100644 --- a/src/playerinfo.h +++ b/src/playerinfo.h @@ -235,6 +235,8 @@ namespace PlayerInfo void deinit(); + void loadData(); + bool isTalking(); void gameDestroyed(); @@ -252,6 +254,12 @@ namespace PlayerInfo void setEquipmentBackend(Equipment::Backend *const backend); + void protectItem(const int id); + + void unprotectItem(const int id); + + bool isItemProtected(const int id); + } // namespace PlayerInfo #endif // PLAYERINFO_H -- cgit v1.2.3-60-g2f50