From c1a6e9947231cc511016378bd326a5d64b0aa18c Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Wed, 27 Apr 2011 15:24:36 +0200 Subject: Added a simple crafting system A client can craft something using the @craft command. The command needs a list of item names and amounts. The gameserver checks if the character has these items in the inventory and then passes the list together with the character handle to the lua script function on_craft in the script file scripts/crafting.lua. This function can then be used to evaluate if the list is a valid crafting combination and when this is the case take or give items. Implemented two example crafting scripts there, one which enforces exact item order and amount and one which doesn't. Both are disabled per default and one needs to be enabled by uncommenting a line. Also gave the player group permission to use the @craft command in permissions.xml and added two new items (wood and iron) required for the example crafting combination. Resolves: #333 Reviewed-by: bcs86, Bertram --- src/scripting/luascript.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/scripting/luascript.h | 3 +++ src/scripting/script.cpp | 14 ++++++++++++++ src/scripting/script.h | 8 ++++++++ 4 files changed, 63 insertions(+) (limited to 'src/scripting') diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp index a9c43b7..dc6230c 100644 --- a/src/scripting/luascript.cpp +++ b/src/scripting/luascript.cpp @@ -21,6 +21,9 @@ #include "luascript.h" + +#include "scripting/luautil.h" + #include "game-server/being.h" #include "utils/logger.h" @@ -61,6 +64,30 @@ void LuaScript::push(Thing *v) ++nbArgs; } +void LuaScript::push(const std::list &itemList) +{ + assert(nbArgs >= 0); + int position = 0; + + lua_createtable(mState, itemList.size(), 0); + int itemTable = lua_gettop(mState); + + for (std::list::const_iterator i = itemList.begin(); + i != itemList.end(); + i++) + { + // create the item structure + std::map item; + item["id"] = i->itemId; + item["amount"] = i->amount; + // add the item structure to the item table under the next index + lua_pushinteger(mState, ++position); + pushSTLContainer(mState, item); + lua_settable(mState, itemTable); + } + ++nbArgs; +} + int LuaScript::execute() { assert(nbArgs >= 0); @@ -182,3 +209,14 @@ bool LuaScript::loadSpecialActionsScript(const std::string &file) } return true; } + +bool LuaScript::loadCraftScript(const std::string &file) +{ + Script::craftScript = new LuaScript(); + if (!Script::craftScript->loadFile(file)) + { + Script::craftScript = NULL; + return false; + } + return true; +} diff --git a/src/scripting/luascript.h b/src/scripting/luascript.h index af13aa2..b9bde2d 100644 --- a/src/scripting/luascript.h +++ b/src/scripting/luascript.h @@ -52,6 +52,8 @@ class LuaScript: public Script void push(Thing *); + void push(const std::list &itemList); + int execute(); static void getQuestCallback(Character *, const std::string &, @@ -69,6 +71,7 @@ class LuaScript: public Script */ static bool loadGlobalEventScript(const std::string &file); static bool loadSpecialActionsScript(const std::string &file); + static bool loadCraftScript(const std::string &file); private: lua_State *mState; diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index b222b0f..490abf0 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -34,6 +34,7 @@ typedef std::map< std::string, Script::Factory > Engines; static Engines *engines = NULL; Script *Script::globalEventScript = NULL; Script *Script::specialActionsScript = NULL; +Script *Script::craftScript = NULL; Script::Script(): mMap(NULL), @@ -158,3 +159,16 @@ bool Script::performSpecialAction(int specialId, Being* caster) } return true; } + +bool Script::performCraft(Being* crafter, std::list recipe) +{ + Script *script = Script::craftScript; + if (script) + { + script->prepare("on_craft"); + script->push(crafter); + script->push(recipe); + script->execute(); + } + return true; +} diff --git a/src/scripting/script.h b/src/scripting/script.h index 43eebe1..44a8b7a 100644 --- a/src/scripting/script.h +++ b/src/scripting/script.h @@ -104,6 +104,12 @@ class Script */ virtual void push(Thing *) = 0; + /** + * Pushes a list of items with amounts to the + * script engine. + */ + virtual void push(const std::list &itemList) = 0; + /** * Executes the function being prepared. * @return the value returned by the script. @@ -135,11 +141,13 @@ class Script static bool executeGlobalEventFunction(const std::string &function, Being *obj); static void addDataToSpecial(int specialId, Special *special); static bool performSpecialAction(int specialId, Being *caster); + static bool performCraft(Being* crafter, std::list recipe); protected: static Script *globalEventScript; static Script *specialActionsScript; + static Script *craftScript; std::string mScriptFile; private: -- cgit