diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_fr> | 2010-05-28 02:59:45 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_fr> | 2010-05-28 03:42:16 +0200 |
commit | 39b00578c249a3396bb03faa92bc9cccdcb1c68a (patch) | |
tree | fb9376dd3229f37c4b8d988c569493178ee130bd /src/scripting/lua.cpp | |
parent | e6ce3d52648aec7139f1c3383a0ef0921411f0d4 (diff) | |
download | manaserv-39b00578c249a3396bb03faa92bc9cccdcb1c68a.tar.gz manaserv-39b00578c249a3396bb03faa92bc9cccdcb1c68a.tar.bz2 manaserv-39b00578c249a3396bb03faa92bc9cccdcb1c68a.tar.xz manaserv-39b00578c249a3396bb03faa92bc9cccdcb1c68a.zip |
Modified the npc_trade() lua function to permit selling the whole player inventory.
It permits to open a sell box with every items in the player inventory
as requested by Striker.
Also added different return value support to both the buy selling functions,
and made fixes where relevant.
The test.lua script will be upgraded to show examples in a next commit.
What's left to be done is to fix the inventory handling for both selling
functions. (Sigh...)
Concerns: Manasource mantis: #78, #101.
Reviewed-by: Jaxad0127
Diffstat (limited to 'src/scripting/lua.cpp')
-rw-r--r-- | src/scripting/lua.cpp | 87 |
1 files changed, 74 insertions, 13 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 790d6d91..da47d931 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -59,7 +59,6 @@ extern "C" { * http://doc.manasource.org/scripting */ - /** * Callback for sending a NPC_MESSAGE. * mana.npc_message(npc, character, string) @@ -79,7 +78,7 @@ static int npc_message(lua_State *s) msg.writeShort(p->getPublicID()); msg.writeString(std::string(m), l); gameHandler->sendTo(q, msg); - return 0; + return 1; } /** @@ -442,44 +441,106 @@ static int chr_inv_count(lua_State *s) /** * Callback for trading between a player and an NPC. * mana.npc_trade(npc, character, bool sell, table items) + * Let the player buy or sell only a subset of predeterminded items. + * @param table items: a subset of buyable/sellable items. + * When selling, if the 4 parameter is omitted or invalid, + * everything in the player inventory can be sold. + * @return 0 if something to buy/sell, 1 if no items, 2 in case of errors. */ static int npc_trade(lua_State *s) { NPC *p = getNPC(s, 1); Character *q = getCharacter(s, 2); - if (!p || !q || !lua_isboolean(s, 3) || !lua_istable(s, 4)) + if (!p || !q || !lua_isboolean(s, 3)) { - raiseScriptError(s, "npc_trade called with incorrect parameters."); - return 0; + raiseWarning(s, "npc_trade called with incorrect parameters."); + lua_pushinteger(s, 2); // return value + return 1; // Returns 1 parameter } - BuySell *t = new BuySell(q, lua_toboolean(s, 3)); + + bool sellMode = lua_toboolean(s, 3); + BuySell *t = new BuySell(q, sellMode); + if (!lua_istable(s, 4)) + { + if (sellMode) + { + // Can sell everything + if (!t->registerPlayerItems()) + { + // No items to sell in player inventory + t->cancel(); + lua_pushinteger(s, 1); + return 1; + } + + if (t->start(p)) + { + lua_pushinteger(s, 0); + return 1; + } + else + { + lua_pushinteger(s, 1); + return 1; + } + } + else + { + raiseWarning(s, "npc_trade[Buy] called with invalid or empty items table parameter."); + t->cancel(); + lua_pushinteger(s, 2); + return 1; + } + } + + int nbItems = 0; + lua_pushnil(s); while (lua_next(s, 4)) { if (!lua_istable(s, -1)) { - raiseScriptError(s, "npc_trade called with incorrect parameters."); + raiseWarning(s, "npc_trade called with invalid or empty items table parameter."); t->cancel(); - return 0; + lua_pushinteger(s, 2); + return 1; } + int v[3]; for (int i = 0; i < 3; ++i) { lua_rawgeti(s, -1, i + 1); if (!lua_isnumber(s, -1)) { - raiseScriptError(s, "rpc_trade called with incorrect parameters."); + raiseWarning(s, "npc_trade called with incorrect parameters in item table."); t->cancel(); - return 0; + lua_pushinteger(s, 2); + return 1; } v[i] = lua_tointeger(s, -1); lua_pop(s, 1); } - t->registerItem(v[0], v[1], v[2]); + if (t->registerItem(v[0], v[1], v[2])) + nbItems++; lua_pop(s, 1); } - t->start(p); - return 0; + + if (nbItems == 0) + { + t->cancel(); + lua_pushinteger(s, 1); + return 1; + } + if (t->start(p)) + { + lua_pushinteger(s, 0); + return 1; + } + else + { + lua_pushinteger(s, 1); + return 1; + } } /** |