summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--example/serverdata/scripts/maps/desert.lua7
-rw-r--r--src/scripting/lua.cpp39
2 files changed, 40 insertions, 6 deletions
diff --git a/example/serverdata/scripts/maps/desert.lua b/example/serverdata/scripts/maps/desert.lua
index b2eb9a4..6d88b37 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 047fe93..47bf335 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]))