summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-17 08:59:40 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-17 08:59:40 +0000
commit0d187f7e18301901502fe5586ee3f7a30a2bac68 (patch)
treef5f3fb7ce408b819f550268bd0aa64b693bab55c
parent5560e259363a41e39f18dcbd8f3109e253d1fd0d (diff)
downloadmanaserv-0d187f7e18301901502fe5586ee3f7a30a2bac68.tar.gz
manaserv-0d187f7e18301901502fe5586ee3f7a30a2bac68.tar.bz2
manaserv-0d187f7e18301901502fe5586ee3f7a30a2bac68.tar.xz
manaserv-0d187f7e18301901502fe5586ee3f7a30a2bac68.zip
Added Lua function for trading between players and NPCs.
-rw-r--r--ChangeLog5
-rw-r--r--data/test.lua4
-rw-r--r--src/scripting/lua.cpp61
3 files changed, 44 insertions, 26 deletions
diff --git a/ChangeLog b/ChangeLog
index 49b701db..de07c8ff 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2007-08-17 Guillaume Melquiond <guillaume.melquiond@gmail.com>
+
+ * src/scripting/lua.cpp, data/test.lua: Added Lua function for trading
+ between players and NPCs.
+
2007-08-16 Guillaume Melquiond <guillaume.melquiond@gmail.com>
* data/scripts/libtmw.lua: Improved NPC state machine, so that the
diff --git a/data/test.lua b/data/test.lua
index 28dbf0e8..c6416756 100644
--- a/data/test.lua
+++ b/data/test.lua
@@ -26,9 +26,9 @@ function my_npc1(npc, ch)
end
end
elseif v == 3 then
- tmw.test_npc_buy(npc, ch)
+ tmw.npc_trade(npc, ch, false, { {533, 10, 20}, {535, 10, 30}, {537, 10, 50} })
elseif v == 4 then
- tmw.test_npc_sell(npc, ch)
+ tmw.npc_trade(npc, ch, true, { {511, 10, 200}, {524, 10, 300}, {508, 10, 500}, {537, 10, 25} })
end
end
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 34415a27..79061154 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -301,31 +301,45 @@ static int LuaChr_InvCount(lua_State *s)
return nb_items;
}
-// For testing purpose.
-static int test_NpcBuy(lua_State *s)
-{
- NPC *p = getNPC(s, 1);
- Character *q = getCharacter(s, 2);
- if (!p || !q) return 0;
- BuySell *t = new BuySell(q, false);
- t->registerItem(533, 10, 20);
- t->registerItem(535, 10, 30);
- t->registerItem(537, 10, 50);
- t->start(p);
- return 0;
-}
-
-// For testing purpose.
-static int test_NpcSell(lua_State *s)
+/**
+ * Callback for trading between a a player and an NPC.
+ * tmw.npc_trade(npc, character, bool sell, table items)
+ */
+static int LuaNpc_Trade(lua_State *s)
{
NPC *p = getNPC(s, 1);
Character *q = getCharacter(s, 2);
- if (!p || !q) return 0;
- BuySell *t = new BuySell(q, true);
- t->registerItem(511, 10, 200);
- t->registerItem(524, 10, 300);
- t->registerItem(508, 10, 500);
- t->registerItem(537, 10, 25);
+ if (!p || !q || !lua_isboolean(s, 3) || !lua_istable(s, 4))
+ {
+ LOG_WARN("LuaNpc_Trade called with incorrect parameters.");
+ return 0;
+ }
+ BuySell *t = new BuySell(q, lua_toboolean(s, 3));
+ lua_pushnil(s);
+ while (lua_next(s, 4))
+ {
+ if (!lua_istable(s, -1))
+ {
+ LOG_WARN("LuaNpc_Trade called with incorrect parameters.");
+ t->cancel();
+ return 0;
+ }
+ int v[3];
+ for (int i = 0; i < 3; ++i)
+ {
+ lua_rawgeti(s, -1, i + 1);
+ if (!lua_isnumber(s, -1))
+ {
+ LOG_WARN("LuaNpc_Trade called with incorrect parameters.");
+ t->cancel();
+ return 0;
+ }
+ v[i] = lua_tointeger(s, -1);
+ lua_pop(s, 1);
+ }
+ t->registerItem(v[0], v[1], v[2]);
+ lua_pop(s, 1);
+ }
t->start(p);
return 0;
}
@@ -344,8 +358,7 @@ LuaScript::LuaScript():
{ "chr_warp", &LuaChr_Warp },
{ "chr_inv_change", &LuaChr_InvChange },
{ "chr_inv_count", &LuaChr_InvCount },
- { "test_npc_buy", &test_NpcBuy },
- { "test_npc_sell", &test_NpcSell },
+ { "npc_trade", &LuaNpc_Trade },
{ NULL, NULL }
};
luaL_register(mState, "tmw", callbacks);