diff options
Diffstat (limited to 'src/scripting')
-rw-r--r-- | src/scripting/lua.cpp | 97 | ||||
-rw-r--r-- | src/scripting/script.cpp | 18 | ||||
-rw-r--r-- | src/scripting/script.h | 9 | ||||
-rw-r--r-- | src/scripting/scriptmanager.cpp | 32 | ||||
-rw-r--r-- | src/scripting/scriptmanager.h | 4 |
5 files changed, 154 insertions, 6 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 33bd9eb9..3d59ab98 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -50,6 +50,7 @@ extern "C" { #include "net/messageout.h" #include "scripting/luautil.h" #include "scripting/luascript.h" +#include "scripting/scriptmanager.h" #include "utils/logger.h" #include "utils/speedconv.h" @@ -125,6 +126,90 @@ static int on_being_remove(lua_State *s) return 0; } +static int on_update(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + Script::setUpdateCallback(getScript(s)); + return 0; +} + +static int on_npc_start(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + NPC::setStartCallback(getScript(s)); + return 0; +} + +static int on_npc_next(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + NPC::setNextCallback(getScript(s)); + return 0; +} + +static int on_npc_choose(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + NPC::setChooseCallback(getScript(s)); + return 0; +} + +static int on_npc_integer(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + NPC::setIntegerCallback(getScript(s)); + return 0; +} + +static int on_npc_string(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + NPC::setStringCallback(getScript(s)); + return 0; +} + +static int on_npc_update(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + NPC::setUpdateCallback(getScript(s)); + return 0; +} + +static int on_create_npc_delayed(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + Script::setCreateNpcDelayedCallback(getScript(s)); + return 0; +} + +static int on_map_initialize(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + MapComposite::setInitializeCallback(getScript(s)); + return 0; +} + +static int on_craft(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + ScriptManager::setCraftCallback(getScript(s)); + return 0; +} + +static int on_use_special(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + ScriptManager::setSpecialCallback(getScript(s)); + return 0; +} + +static int on_get_special_recharge_cost(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + ScriptManager::setGetSpecialRechargeCostCallback(getScript(s)); + return 0; +} + /** * mana.npc_message(NPC*, Character*, string): void * Callback for sending a NPC_MESSAGE. @@ -2622,6 +2707,18 @@ LuaScript::LuaScript(): { "on_npc_post_reply", &on_npc_post_reply }, { "on_being_death", &on_being_death }, { "on_being_remove", &on_being_remove }, + { "on_update", &on_update }, + { "on_npc_start", &on_npc_start }, + { "on_npc_next", &on_npc_next }, + { "on_npc_choose", &on_npc_choose }, + { "on_npc_integer", &on_npc_integer }, + { "on_npc_string", &on_npc_string }, + { "on_npc_update", &on_npc_update }, + { "on_create_npc_delayed", &on_create_npc_delayed }, + { "on_map_initialize", &on_map_initialize }, + { "on_craft", &on_craft }, + { "on_use_special", &on_use_special }, + { "on_get_special_recharge_cost", &on_get_special_recharge_cost }, { "npc_create", &npc_create }, { "npc_message", &npc_message }, { "npc_choice", &npc_choice }, diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index db44bd57..9ef069d5 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -34,6 +34,9 @@ typedef std::map< std::string, Script::Factory > Engines; static Engines *engines = NULL; +Script::Ref Script::mCreateNpcDelayedCallback; +Script::Ref Script::mUpdateCallback; + Script::Script(): mMap(NULL), mEventListener(&scriptEventDispatch) @@ -68,7 +71,12 @@ Script *Script::create(const std::string &engine) void Script::update() { - prepare("update"); + if (!mUpdateCallback.isValid()) + { + LOG_ERROR("Could not find callback for update function!"); + return; + } + prepare(mUpdateCallback); execute(); } @@ -98,8 +106,14 @@ bool Script::loadFile(const std::string &name) void Script::loadNPC(const std::string &name, int id, int x, int y, const char *prog) { + if (!mCreateNpcDelayedCallback.isValid()) + { + LOG_ERROR("No callback for creating npcs delayed assigned. " + "Could not enabled NPC"); + return; + } load(prog, name.c_str()); - prepare("create_npc_delayed"); + prepare(mCreateNpcDelayedCallback); push(name); push(id); push(x); diff --git a/src/scripting/script.h b/src/scripting/script.h index 9cc50a1c..bbfc7691 100644 --- a/src/scripting/script.h +++ b/src/scripting/script.h @@ -163,6 +163,12 @@ class Script virtual void processRemoveEvent(Thing *thing) = 0; + static void setCreateNpcDelayedCallback(Script *script) + { script->assignCallback(mCreateNpcDelayedCallback); } + + static void setUpdateCallback(Script *script) + { script->assignCallback(mUpdateCallback); } + protected: std::string mScriptFile; @@ -170,6 +176,9 @@ class Script MapComposite *mMap; EventListener mEventListener; /**< Tracking of being deaths. */ + static Ref mCreateNpcDelayedCallback; + static Ref mUpdateCallback; + friend struct ScriptEventDispatch; }; diff --git a/src/scripting/scriptmanager.cpp b/src/scripting/scriptmanager.cpp index d58379f6..c133a5e2 100644 --- a/src/scripting/scriptmanager.cpp +++ b/src/scripting/scriptmanager.cpp @@ -25,6 +25,10 @@ static Script *_currentState; +static Script::Ref _craftCallback; +static Script::Ref _specialCallback; +static Script::Ref _getSpecialRechargeCostCallback; + void ScriptManager::initialize() { const std::string engine = Configuration::getValue("script_engine", "lua"); @@ -56,9 +60,9 @@ void ScriptManager::addDataToSpecial(int id, Special *special) first we have to agree on what other info we actually want to provide. */ - if (special) + if (special && _getSpecialRechargeCostCallback.isValid()) { - _currentState->prepare("get_special_recharge_cost"); + _currentState->prepare(_getSpecialRechargeCostCallback); _currentState->push(id); int scriptReturn = _currentState->execute(); special->neededMana = scriptReturn; @@ -67,7 +71,13 @@ void ScriptManager::addDataToSpecial(int id, Special *special) bool ScriptManager::performSpecialAction(int specialId, Being *caster) { - _currentState->prepare("use_special"); + if (!_specialCallback.isValid()) + { + LOG_WARN("No callback for specials set! Specials disabled."); + return false; + } + + _currentState->prepare(_specialCallback); _currentState->push(caster); _currentState->push(specialId); _currentState->execute(); @@ -77,9 +87,23 @@ bool ScriptManager::performSpecialAction(int specialId, Being *caster) bool ScriptManager::performCraft(Being *crafter, const std::list<InventoryItem> &recipe) { - _currentState->prepare("on_craft"); + if (!_craftCallback.isValid()) + { + LOG_WARN("No crafting callback set! Crafting disabled."); + return false; + } + _currentState->prepare(_craftCallback); _currentState->push(crafter); _currentState->push(recipe); _currentState->execute(); return true; } + +void ScriptManager::setCraftCallback(Script *script) +{ script->assignCallback(_craftCallback); } + +void ScriptManager::setSpecialCallback(Script *script) +{ script->assignCallback(_specialCallback); } + +void ScriptManager::setGetSpecialRechargeCostCallback(Script *script) +{ script->assignCallback(_getSpecialRechargeCostCallback); } diff --git a/src/scripting/scriptmanager.h b/src/scripting/scriptmanager.h index ade76c19..d0d03707 100644 --- a/src/scripting/scriptmanager.h +++ b/src/scripting/scriptmanager.h @@ -60,6 +60,10 @@ void addDataToSpecial(int specialId, Special *special); bool performSpecialAction(int specialId, Being *caster); bool performCraft(Being *crafter, const std::list<InventoryItem> &recipe); +void setCraftCallback(Script *script); +void setSpecialCallback(Script *script); +void setGetSpecialRechargeCostCallback(Script *script); + } // namespace ScriptManager #endif // SCRIPTMANAGER_H |