From 2a092bff6a889415d68b63db6e11942aa2d2bb33 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Sat, 3 Dec 2011 00:41:08 +0100 Subject: Added unequip lua script functions. You can unequip using the slot or an item id. + Fixes from Ablu's review. Resolves: Mana-Mantis #350. Reviewed-by: Ablu. --- src/game-server/inventory.cpp | 31 +++++++++++++++++++++ src/game-server/inventory.h | 15 ++++++++++ src/scripting/lua.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) (limited to 'src') diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index 1cc92145..4d447d3a 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -711,6 +711,37 @@ bool Inventory::equip(int inventorySlot) return true; } +unsigned int Inventory::getSlotItemInstance(unsigned int slot) +{ + EquipData::iterator it = mPoss->equipSlots.find(slot); + if (it != mPoss->equipSlots.end()) + return it->second.itemInstance; + return 0; +} + +bool Inventory::unequipItem(unsigned int itemId) +{ + std::set itemInstances; + for (EquipData::iterator it = mPoss->equipSlots.begin(), + it_end = mPoss->equipSlots.end(); it != it_end; ++it) + { + if (it->second.itemId == itemId) + itemInstances.insert(it->second.itemInstance); + } + + // Nothing to do but it's a success + if (itemInstances.empty()) + return true; + + for (std::set::const_iterator it = itemInstances.begin(), + it_end = itemInstances.end(); it != it_end; ++it) + { + if (!unequip(*it)); + return false; + } + return true; +} + bool Inventory::unequip(unsigned int itemInstance) { if (!itemInstance) diff --git a/src/game-server/inventory.h b/src/game-server/inventory.h index 84981ea3..812c142d 100644 --- a/src/game-server/inventory.h +++ b/src/game-server/inventory.h @@ -63,6 +63,15 @@ class Inventory */ bool equip(int inventorySlot); + /** + * Unequips all the items with the given item if + * from given equipment slot. + * @param itemId The item Id to unequip. + * @returns whether all item id could be unequipped. + * @note returns true when no item with given ids were equipped. + */ + bool unequipItem(unsigned int itemId); + /** * Unequips item from given equipment slot. * @param itemInstance The item instance id used to know what to unequip @@ -70,6 +79,12 @@ class Inventory */ bool unequip(unsigned int itemInstance); + /** + * Gets the item instance from the given equipment slot. + * Return 0 if none. + */ + unsigned int getSlotItemInstance(unsigned int slot); + /** * Inserts some items into the inventory. * @return number of items not inserted (to be dropped on floor?). diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 58492319..6eba4353 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -549,6 +549,69 @@ static int chr_equip_item(lua_State *s) return 1; } +/** + * mana.chr_unequip_slot(Character*, int inventorySlot): bool success + * Makes the character unequip the item in the given equipment slot. + */ +static int chr_unequip_slot(lua_State *s) +{ + int equipmentSlot = luaL_checkint(s, 2); + + Character *ch = getCharacter(s, 1); + if (!ch) + { + raiseScriptError(s, "chr_unequip_slot " + "called for nonexistent character."); + return 0; + } + Inventory inv(ch); + + lua_pushboolean(s, inv.unequip(inv.getSlotItemInstance(equipmentSlot))); + return 1; +} + +/** + * mana.chr_unequip_item(Character*, int itemId || string itemName): bool success + * Makes the character unequip the item(s) corresponding to the id + * when it's existing in the player's equipment. + * Returns true when every item were unequipped from equipment. + */ +static int chr_unequip_item(lua_State *s) +{ + // Get the itemId + ItemClass *it; + if (lua_isnumber(s, 2)) + it = itemManager->getItem(lua_tointeger(s, 2)); + else + it = itemManager->getItemByName(lua_tostring(s, 2)); + + if (!it) + { + raiseScriptError(s, "chr_unequip_item called with invalid " + "item id or name."); + return 0; + } + unsigned int id = it->getDatabaseID(); + if (!id) + { + LOG_WARN("chr_unequip_item called with id 0! " + "Currency is now handled through attributes!"); + lua_pushboolean(s, false); + return 1; + } + + Character *ch = getCharacter(s, 1); + if (!ch) + { + raiseScriptError(s, "chr_unequip_item " + "called for nonexistent character."); + return 0; + } + Inventory inv(ch); + lua_pushboolean(s, inv.unequipItem(id)); + return 1; +} + /** * mana.chr_get_level(Character*): int level * Tells the character current level. @@ -2395,6 +2458,8 @@ LuaScript::LuaScript(): { "chr_inv_count", &chr_inv_count }, { "chr_equip_slot", &chr_equip_slot }, { "chr_equip_item", &chr_equip_item }, + { "chr_unequip_slot", &chr_unequip_slot }, + { "chr_unequip_item", &chr_unequip_item }, { "chr_get_level", &chr_get_level }, { "chr_get_quest", &chr_get_quest }, { "chr_set_quest", &chr_set_quest }, -- cgit v1.2.3-60-g2f50