From 4c9db4a20aad649f80c9db8379f372de7361096b Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Fri, 2 Dec 2011 19:09:18 +0100 Subject: Added equip lua script functions. One per inventory slot, one per item id or name. + Fixes from 2 Ablu's reviews. Reviewed-by: Ablu. 1st part of Mana-Mantis #339, 350. --- src/game-server/inventory.cpp | 9 ++++++ src/game-server/inventory.h | 8 ++++- src/scripting/lua.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index 7bbaf483..1cc92145 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -228,6 +228,15 @@ unsigned int Inventory::count(unsigned int itemId) const return nb; } +int Inventory::getFirstSlot(unsigned int itemId) +{ + for (InventoryData::iterator it = mPoss->inventory.begin(), + it_end = mPoss->inventory.end(); it != it_end; ++it) + if (it->second.itemId == itemId) + return (int)it->first; + return -1; +} + unsigned int Inventory::remove(unsigned int itemId, unsigned int amount) { if (!itemId || !amount) diff --git a/src/game-server/inventory.h b/src/game-server/inventory.h index 547abdf0..84981ea3 100644 --- a/src/game-server/inventory.h +++ b/src/game-server/inventory.h @@ -96,7 +96,7 @@ class Inventory unsigned int removeFromSlot(unsigned int slot, unsigned int amount); /** - * Counts number of items with given ID. + * Counts number of items with given Id. */ unsigned int count(unsigned int itemId) const; @@ -105,6 +105,12 @@ class Inventory */ unsigned int getItem(unsigned int slot) const; + /** + * Returns the first inventory slot with the given item Id. + * Returns -1 otherwise. + */ + int getFirstSlot(unsigned int itemId); + private: /** * Tell whether the equipment slot has enough room in an equipment slot. diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 9f905bcd..58492319 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -480,6 +480,75 @@ static int chr_inv_count(lua_State *s) return nb_items; } +/** + * mana.chr_equip_slot(Character*, int inventorySlot): bool success + * Makes the character equip the item in the given inventory slot. + */ +static int chr_equip_slot(lua_State *s) +{ + int inventorySlot = luaL_checkint(s, 2); + + Character *ch = getCharacter(s, 1); + if (!ch) + { + raiseScriptError(s, "chr_equip_slot " + "called for nonexistent character."); + return 0; + } + Inventory inv(ch); + + lua_pushboolean(s, inv.equip(inventorySlot)); + return 1; +} + +/** + * mana.chr_equip_item(Character*, int itemId || string itemName): bool success + * Makes the character equip the item id when it's existing + * in the player's inventory. + */ +static int chr_equip_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_equip_item called with invalid " + "item id or name."); + return 0; + } + unsigned int id = it->getDatabaseID(); + if (!id) + { + LOG_WARN("chr_equip_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_equip_item " + "called for nonexistent character."); + return 0; + } + Inventory inv(ch); + + int inventorySlot = inv.getFirstSlot(id); + bool success = false; + + if (inventorySlot > -1) + success = inv.equip(inventorySlot); + + lua_pushboolean(s, success); + return 1; +} + /** * mana.chr_get_level(Character*): int level * Tells the character current level. @@ -2324,6 +2393,8 @@ LuaScript::LuaScript(): { "chr_warp", &chr_warp }, { "chr_inv_change", &chr_inv_change }, { "chr_inv_count", &chr_inv_count }, + { "chr_equip_slot", &chr_equip_slot }, + { "chr_equip_item", &chr_equip_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