summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-12-03 00:41:08 +0100
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2012-01-10 01:19:09 +0100
commit2a092bff6a889415d68b63db6e11942aa2d2bb33 (patch)
treed64f5f2b8675ef42ce19f1ca641945e7d308189c /src
parent4c9db4a20aad649f80c9db8379f372de7361096b (diff)
downloadmanaserv-2a092bff6a889415d68b63db6e11942aa2d2bb33.tar.gz
manaserv-2a092bff6a889415d68b63db6e11942aa2d2bb33.tar.bz2
manaserv-2a092bff6a889415d68b63db6e11942aa2d2bb33.tar.xz
manaserv-2a092bff6a889415d68b63db6e11942aa2d2bb33.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/game-server/inventory.cpp31
-rw-r--r--src/game-server/inventory.h15
-rw-r--r--src/scripting/lua.cpp65
3 files changed, 111 insertions, 0 deletions
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<unsigned> 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<unsigned>::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
@@ -64,6 +64,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
* @returns Whether it was unequipped.
@@ -71,6 +80,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
@@ -550,6 +550,69 @@ static int chr_equip_item(lua_State *s)
}
/**
+ * 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 },