diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2011-11-06 21:02:23 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2011-11-07 18:54:37 +0100 |
commit | 391916f685afe93d9afb021b81f8d5b5789822bc (patch) | |
tree | 74c209d27da8db63bac99669b2dccb55f9e55650 /src/scripting/script.cpp | |
parent | 80f0899c16931b41b51b062a3d020781c033bc87 (diff) | |
download | manaserv-391916f685afe93d9afb021b81f8d5b5789822bc.tar.gz manaserv-391916f685afe93d9afb021b81f8d5b5789822bc.tar.bz2 manaserv-391916f685afe93d9afb021b81f8d5b5789822bc.tar.xz manaserv-391916f685afe93d9afb021b81f8d5b5789822bc.zip |
Merged three global script states into one
These scripts could trivially share one script state, since the methods
called on them from the server are not overlapping. This does leave them
open to access each other's global variables, but that's the problem
with global variables.
The one remaining global script file name is now configurable, so that
it may also be set to a script in a different scripting language. The
two related script options are:
script_mainFile (default: scripts/main.lua)
script_defaultEngine (default: lua) - renamed from defaultScriptEngine
Reviewed-by: jurkan
Reviewed-by: Yohann Ferreira
Diffstat (limited to 'src/scripting/script.cpp')
-rw-r--r-- | src/scripting/script.cpp | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index b6121bb8..722979fd 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -34,8 +34,6 @@ 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), @@ -110,11 +108,21 @@ void Script::loadNPC(const std::string &name, int id, int x, int y, execute(); } +bool Script::loadGlobalEventScript(const std::string &file) +{ + std::string engineName = determineEngineByFilename(file); + if (Script *script = Script::create(engineName)) + { + globalEventScript = script; + return globalEventScript->loadFile(file); + } + return false; +} + bool Script::executeGlobalEventFunction(const std::string &function, Being* obj) { bool isScriptHandled = false; - Script *script = Script::globalEventScript; - if (script) + if (Script *script = globalEventScript) { script->setMap(obj->getMap()); script->prepare(function); @@ -127,7 +135,7 @@ bool Script::executeGlobalEventFunction(const std::string &function, Being* obj) } -void Script::addDataToSpecial(int id, Special* special) +void Script::addDataToSpecial(int id, Special *special) { /* currently only gets the recharge cost. TODO: get any other info in a similar way, but @@ -136,8 +144,7 @@ void Script::addDataToSpecial(int id, Special* special) */ if (special) { - Script *script = Script::specialActionsScript; - if (script) + if (Script *script = globalEventScript) { script->prepare("get_special_recharge_cost"); script->push(id); @@ -148,10 +155,9 @@ void Script::addDataToSpecial(int id, Special* special) } -bool Script::performSpecialAction(int specialId, Being* caster) +bool Script::performSpecialAction(int specialId, Being *caster) { - Script *script = Script::specialActionsScript; - if (script) + if (Script *script = globalEventScript) { script->prepare("use_special"); script->push(caster); @@ -161,10 +167,10 @@ bool Script::performSpecialAction(int specialId, Being* caster) return true; } -bool Script::performCraft(Being* crafter, std::list<InventoryItem> recipe) +bool Script::performCraft(Being *crafter, + const std::list<InventoryItem> &recipe) { - Script *script = Script::craftScript; - if (script) + if (Script *script = globalEventScript) { script->prepare("on_craft"); script->push(crafter); @@ -187,6 +193,6 @@ std::string Script::determineEngineByFilename(const std::string &filename) // Set to default engine and print warning LOG_WARN("Unknown file extension for script \"" + filename + "\", falling back to default script engine"); - return Configuration::getValue("defaultScriptEngine", "lua"); + return Configuration::getValue("script_defaultEngine", "lua"); } } |