diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2012-03-03 13:36:28 +0100 |
---|---|---|
committer | Erik Schilling <ablu.erikschilling@googlemail.com> | 2012-03-03 16:41:39 +0100 |
commit | f872528771f0b71741fb36ddf70f2ae23f54c1e3 (patch) | |
tree | 9b3751cab7d9f58ae0b15acf061e428f18bc07db /src | |
parent | c0c208d4c29ff49f940e8a6c54adb26cc4e5eba3 (diff) | |
download | manaserv-f872528771f0b71741fb36ddf70f2ae23f54c1e3.tar.gz manaserv-f872528771f0b71741fb36ddf70f2ae23f54c1e3.tar.bz2 manaserv-f872528771f0b71741fb36ddf70f2ae23f54c1e3.tar.xz manaserv-f872528771f0b71741fb36ddf70f2ae23f54c1e3.zip |
Added further missing callbacks
Reviewed-by: bjorn.
Diffstat (limited to 'src')
-rw-r--r-- | src/game-server/mapcomposite.cpp | 17 | ||||
-rw-r--r-- | src/game-server/mapcomposite.h | 7 | ||||
-rw-r--r-- | src/game-server/npc.cpp | 33 | ||||
-rw-r--r-- | src/game-server/npc.h | 25 | ||||
-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 |
9 files changed, 222 insertions, 20 deletions
diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp index c9090008..ec455962 100644 --- a/src/game-server/mapcomposite.cpp +++ b/src/game-server/mapcomposite.cpp @@ -456,6 +456,8 @@ MapZone& MapContent::getZone(const Point &pos) const * MapComposite *****************************************************************************/ +Script::Ref MapComposite::mInitializeCallback; + MapComposite::MapComposite(int id, const std::string &name): mMap(NULL), mContent(NULL), @@ -493,10 +495,17 @@ bool MapComposite::activate() else mPvPRules = PVP_NONE; - Script *s = ScriptManager::currentState(); - s->setMap(this); - s->prepare("initialize"); - s->execute(); + if (!mInitializeCallback.isValid()) + { + LOG_WARN("No callback for map initialization found"); + } + else + { + Script *s = ScriptManager::currentState(); + s->setMap(this); + s->prepare(mInitializeCallback); + s->execute(); + } return true; } diff --git a/src/game-server/mapcomposite.h b/src/game-server/mapcomposite.h index 49296911..355f0f46 100644 --- a/src/game-server/mapcomposite.h +++ b/src/game-server/mapcomposite.h @@ -26,6 +26,8 @@ #include <vector> #include <map> +#include "scripting/script.h" + class Actor; class Being; class Character; @@ -336,6 +338,9 @@ class MapComposite const std::string &value) { mScriptVariables[key] = value; } + static void setInitializeCallback(Script *script) + { script->assignCallback(mInitializeCallback); } + private: MapComposite(const MapComposite &); @@ -348,6 +353,8 @@ class MapComposite /** Cached persistent variables */ std::map<std::string, std::string> mScriptVariables; PvPRules mPvPRules; + + static Script::Ref mInitializeCallback; }; #endif diff --git a/src/game-server/npc.cpp b/src/game-server/npc.cpp index c1b91d66..052b296e 100644 --- a/src/game-server/npc.cpp +++ b/src/game-server/npc.cpp @@ -22,6 +22,13 @@ #include "game-server/npc.h" #include "scripting/script.h" +Script::Ref NPC::mStartCallback; +Script::Ref NPC::mNextCallback; +Script::Ref NPC::mChooseCallback; +Script::Ref NPC::mIntegerCallback; +Script::Ref NPC::mStringCallback; +Script::Ref NPC::mUpdateCallback; + NPC::NPC(const std::string &name, int id, Script *s): Being(OBJECT_NPC), mScript(s), @@ -38,16 +45,19 @@ void NPC::enable(bool enabled) void NPC::update() { - if (!mScript || !mEnabled) return; - mScript->prepare("npc_update"); + if (!mScript || !mEnabled || !mUpdateCallback.isValid()) + return; + mScript->prepare(mUpdateCallback); mScript->push(this); mScript->execute(); } void NPC::prompt(Character *ch, bool restart) { - if (!mScript || !mEnabled) return; - mScript->prepare(restart ? "npc_start" : "npc_next"); + if (!mScript || !mEnabled || !mStartCallback.isValid() + || !mNextCallback.isValid()) + return; + mScript->prepare(restart ? mStartCallback : mNextCallback); mScript->push(this); mScript->push(ch); mScript->execute(); @@ -55,8 +65,9 @@ void NPC::prompt(Character *ch, bool restart) void NPC::select(Character *ch, int v) { - if (!mScript || !mEnabled) return; - mScript->prepare("npc_choose"); + if (!mScript || !mEnabled || !mChooseCallback.isValid()) + return; + mScript->prepare(mChooseCallback); mScript->push(this); mScript->push(ch); mScript->push(v); @@ -65,8 +76,9 @@ void NPC::select(Character *ch, int v) void NPC::integerReceived(Character *ch, int v) { - if (!mScript || !mEnabled) return; - mScript->prepare("npc_integer"); + if (!mScript || !mEnabled || !mIntegerCallback.isValid()) + return; + mScript->prepare(mIntegerCallback); mScript->push(this); mScript->push(ch); mScript->push(v); @@ -75,8 +87,9 @@ void NPC::integerReceived(Character *ch, int v) void NPC::stringReceived(Character *ch, const std::string &v) { - if (!mScript || !mEnabled) return; - mScript->prepare("npc_string"); + if (!mScript || !mEnabled || !mStringCallback.isValid()) + return; + mScript->prepare(mStringCallback); mScript->push(this); mScript->push(ch); mScript->push(v); diff --git a/src/game-server/npc.h b/src/game-server/npc.h index 566b481c..1ca6b1bb 100644 --- a/src/game-server/npc.h +++ b/src/game-server/npc.h @@ -73,6 +73,24 @@ class NPC : public Being virtual unsigned char getWalkMask() const { return 0x83; } // blocked like a monster by walls, monsters and characters ( bin 1000 0011) + static void setStartCallback(Script *script) + { script->assignCallback(mStartCallback); } + + static void setNextCallback(Script *script) + { script->assignCallback(mNextCallback); } + + static void setChooseCallback(Script *script) + { script->assignCallback(mChooseCallback); } + + static void setIntegerCallback(Script *script) + { script->assignCallback(mIntegerCallback); } + + static void setStringCallback(Script *script) + { script->assignCallback(mStringCallback); } + + static void setUpdateCallback(Script *script) + { script->assignCallback(mUpdateCallback); } + protected: /** * Gets the way a monster blocks pathfinding for other objects @@ -84,6 +102,13 @@ class NPC : public Being Script *mScript; /**< Script describing NPC behavior. */ unsigned short mID; /**< ID of the NPC. */ bool mEnabled; /**< Whether NPC is enabled */ + + static Script::Ref mStartCallback; + static Script::Ref mNextCallback; + static Script::Ref mChooseCallback; + static Script::Ref mIntegerCallback; + static Script::Ref mStringCallback; + static Script::Ref mUpdateCallback; }; #endif 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 |