summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-12-02 19:09:18 +0100
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2012-01-10 01:12:51 +0100
commit4c9db4a20aad649f80c9db8379f372de7361096b (patch)
tree317b817b830140a07970549995ed683b90856d22
parent1c40074d28676ec996ec91f1719cff43077f15f6 (diff)
downloadmanaserv-4c9db4a20aad649f80c9db8379f372de7361096b.tar.gz
manaserv-4c9db4a20aad649f80c9db8379f372de7361096b.tar.bz2
manaserv-4c9db4a20aad649f80c9db8379f372de7361096b.tar.xz
manaserv-4c9db4a20aad649f80c9db8379f372de7361096b.zip
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.
-rw-r--r--src/game-server/inventory.cpp9
-rw-r--r--src/game-server/inventory.h8
-rw-r--r--src/scripting/lua.cpp71
3 files changed, 87 insertions, 1 deletions
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
@@ -481,6 +481,75 @@ static int chr_inv_count(lua_State *s)
}
/**
+ * 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 },