From 100d8af2ef802edc369deba120fcda7c81e25d93 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 13 Dec 2014 14:26:19 +0300 Subject: Simplify actions based on inventory index. --- src/actions/actions.cpp | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/actions/actions.cpp b/src/actions/actions.cpp index 165a9add0..4b527d008 100644 --- a/src/actions/actions.cpp +++ b/src/actions/actions.cpp @@ -210,6 +210,15 @@ static Being *findBeing(const std::string &name) return being; } +static Item *getItemByInvIndex(const InputEvent &event) +{ + const int index = atoi(event.args.c_str()); + const Inventory *const inv = PlayerInfo::getInventory(); + if (inv) + return inv->getItem(index); + return nullptr; +} + impHandler(emote) { const int emotion = 1 + event.action - InputAction::EMOTE_1; @@ -382,12 +391,7 @@ impHandler(dropItemId) impHandler(dropItemInv) { - const Inventory *const inv = PlayerInfo::getInventory(); - if (!inv) - return false; - - Item *const item = inv->getItem(atoi(event.args.c_str())); - + Item *const item = getItemByInvIndex(event); if (item && !PlayerInfo::isItemProtected(item->getId())) { ItemAmountWindow::showWindow(ItemAmountWindow::ItemDrop, @@ -412,12 +416,7 @@ impHandler(dropItemIdAll) impHandler(dropItemInvAll) { - const Inventory *const inv = PlayerInfo::getInventory(); - if (!inv) - return false; - - Item *const item = inv->getItem(atoi(event.args.c_str())); - + Item *const item = getItemByInvIndex(event); if (item && !PlayerInfo::isItemProtected(item->getId())) PlayerInfo::dropItem(item, item->getQuantity(), true); return true; @@ -1380,26 +1379,16 @@ impHandler(useItem) impHandler(useItemInv) { - const int index = atoi(event.args.c_str()); - const Inventory *const inv = PlayerInfo::getInventory(); - if (inv) - { - Item *const item = inv->getItem(index); - PlayerInfo::useEquipItem(item, true); - } + Item *const item = getItemByInvIndex(event); + PlayerInfo::useEquipItem(item, true); return true; } impHandler(invToStorage) { - const int index = atoi(event.args.c_str()); - const Inventory *const inv = PlayerInfo::getInventory(); - if (inv) - { - Item *const item = inv->getItem(index); - ItemAmountWindow::showWindow(ItemAmountWindow::StoreAdd, - inventoryWindow, item); - } + Item *const item = getItemByInvIndex(event); + ItemAmountWindow::showWindow(ItemAmountWindow::StoreAdd, + inventoryWindow, item); return true; } -- cgit v1.2.3-70-g09d2