diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2011-08-04 23:31:17 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2011-08-10 21:09:27 +0200 |
commit | 38eb4fa16f9ac0775bb4cab968c13ef594d355e1 (patch) | |
tree | 2f70b2a5b2a3b6989317912c8bdc6d91218eb853 | |
parent | f57ff09c26cf1cb441e20e5798c78ff4c43c04a2 (diff) | |
download | manaserv-38eb4fa16f9ac0775bb4cab968c13ef594d355e1.tar.gz manaserv-38eb4fa16f9ac0775bb4cab968c13ef594d355e1.tar.bz2 manaserv-38eb4fa16f9ac0775bb4cab968c13ef594d355e1.tar.xz manaserv-38eb4fa16f9ac0775bb4cab968c13ef594d355e1.zip |
Made chr_inv_count and npc_trade capable of taking a name or an id.
Resolves: Mana-Mantis #318.
Reviewed-by: Bertram.
-rw-r--r-- | example/serverdata/scripts/maps/desert.lua | 7 | ||||
-rw-r--r-- | src/scripting/lua.cpp | 39 |
2 files changed, 40 insertions, 6 deletions
diff --git a/example/serverdata/scripts/maps/desert.lua b/example/serverdata/scripts/maps/desert.lua index b2eb9a46..6d88b374 100644 --- a/example/serverdata/scripts/maps/desert.lua +++ b/example/serverdata/scripts/maps/desert.lua @@ -28,12 +28,12 @@ atinit(function() create_npc("Banker", 8, 35 * TILESIZE + TILESIZE / 2, 24 * TILESIZE + TILESIZE / 2, Banker, nil) -- A simple merchant. - merchant_buy_table = { {1, 10, 20}, {2, 10, 30}, {3, 10, 50} } - merchant_sell_table = { {1, 10, 19}, {5, 10, 30}, {6, 10, 200}, {7, 10, 300} } + merchant_buy_table = { {"Candy", 10, 20}, {"Regenerative trinket", 10, 30}, {"Minor health potion", 10, 50} } + merchant_sell_table = { {"Candy", 10, 19}, {"Sword", 10, 30}, {"Bow", 10, 200}, {"Leather shirt", 10, 300} } create_npc("Merchant", 3, 4 * TILESIZE + TILESIZE / 2, 16 * TILESIZE + TILESIZE / 2, npclib.talk(Merchant, merchant_buy_table, merchant_sell_table), nil) -- Another Merchant, selling some equipment, and buying everything... - smith_buy_table = { {5, 10, 50}, {7, 10, 70} } + smith_buy_table = { {"Sword", 10, 50}, {7, 10, 70} } create_npc("Smith", 5, 15 * TILESIZE + TILESIZE / 2, 16 * TILESIZE + TILESIZE / 2, npclib.talk(Merchant, smith_buy_table), nil) -- The most simple NPC - Welcoming new ones around. @@ -85,6 +85,7 @@ function Harmony_update(npc) end function Tamer(npc, ch, list) + mana.being_say(npc, string.format("You have %s Swords.", mana.chr_inv_count(ch, "Sword"))) mana.being_say(npc, string.format("You are %s pixel away.", mana.get_distance(npc, ch))) mana.being_say(npc, "I will now spawn a monster for your training session.") diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 047fe934..47bf3358 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -447,7 +447,19 @@ static int chr_inv_count(lua_State *s) int id, nb = 0; for (int i = 2; i <= nb_items + 1; ++i) { - id = luaL_checkint(s, i); + ItemClass *it; + if (lua_isnumber(s, i)) + it = itemManager->getItem(lua_tointeger(s, i)); + else + it = itemManager->getItemByName(lua_tostring(s, i)); + + if (!it) + { + raiseScriptError(s, "chr_inv_count called with invalid " + "item id or name."); + return 0; + } + id = it->getDatabaseID(); if (id == 0) { LOG_WARN("chr_inv_count called with id 0! " @@ -536,7 +548,25 @@ static int npc_trade(lua_State *s) for (int i = 0; i < 3; ++i) { lua_rawgeti(s, -1, i + 1); - if (!lua_isnumber(s, -1)) + if (i == 0) // item id or name + { + ItemClass *it; + if (lua_isnumber(s, -1)) + it = itemManager->getItem(lua_tointeger(s, -1)); + else + it = itemManager->getItemByName(lua_tostring(s, -1)); + + if (!it) + { + raiseWarning(s, "npc_trade called with incorrect " + "item id or name."); + t->cancel(); + lua_pushinteger(s, 2); + return 1; + } + v[0] = it->getDatabaseID(); + } + else if (!lua_isnumber(s, -1)) { raiseWarning(s, "npc_trade called with incorrect parameters " "in item table."); @@ -544,7 +574,10 @@ static int npc_trade(lua_State *s) lua_pushinteger(s, 2); return 1; } - v[i] = lua_tointeger(s, -1); + else + { + v[i] = lua_tointeger(s, -1); + } lua_pop(s, 1); } if (t->registerItem(v[0], v[1], v[2])) |