From 34ac0d64e23f2b2d3981dbb0ea72157f334805dd Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 26 Feb 2012 22:06:10 +0100 Subject: Merged all the different Lua states into one No more Lua state for each status effect, monster, item effect or map. All scripts are loaded into the same state. This should be more efficient overall and make it easier to implement dynamic reloading of the scripts in the future. Now, this introduces the problem of name collisions between different Lua scripts. For now this is solved by using more specific function names, like 'tick_plague' and 'tick_jump' rather than just 'tick'. The plan is however to get rid of these globals, and register these callbacks from the script, so that they can be local functions without the danger of colliding with other scripts. Reviewed-by: Erik Schilling Reviewed-by: Yohann Ferreira --- src/scripting/script.cpp | 90 ------------------------------------------------ 1 file changed, 90 deletions(-) (limited to 'src/scripting/script.cpp') diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index 722979fd..db44bd57 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -33,7 +33,6 @@ typedef std::map< std::string, Script::Factory > Engines; static Engines *engines = NULL; -Script *Script::globalEventScript = NULL; Script::Script(): mMap(NULL), @@ -107,92 +106,3 @@ void Script::loadNPC(const std::string &name, int id, int x, int y, push(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; - if (Script *script = globalEventScript) - { - script->setMap(obj->getMap()); - script->prepare(function); - script->push(obj); - script->execute(); - script->setMap(NULL); - isScriptHandled = true; // TODO: don't set to true when execution failed - } - return isScriptHandled; -} - - -void Script::addDataToSpecial(int id, Special *special) -{ - /* currently only gets the recharge cost. - TODO: get any other info in a similar way, but - first we have to agree on what other - info we actually want to provide. - */ - if (special) - { - if (Script *script = globalEventScript) - { - script->prepare("get_special_recharge_cost"); - script->push(id); - int scriptReturn = script->execute(); - special->neededMana = scriptReturn; - } - } - -} - -bool Script::performSpecialAction(int specialId, Being *caster) -{ - if (Script *script = globalEventScript) - { - script->prepare("use_special"); - script->push(caster); - script->push(specialId); - script->execute(); - } - return true; -} - -bool Script::performCraft(Being *crafter, - const std::list &recipe) -{ - if (Script *script = globalEventScript) - { - script->prepare("on_craft"); - script->push(crafter); - script->push(recipe); - script->execute(); - } - return true; -} - -std::string Script::determineEngineByFilename(const std::string &filename) -{ - std::string ext = filename.substr(filename.find_last_of(".") + 1); - - if (ext == "lua") - { - return "lua"; - } - else - { - // Set to default engine and print warning - LOG_WARN("Unknown file extension for script \"" - + filename + "\", falling back to default script engine"); - return Configuration::getValue("script_defaultEngine", "lua"); - } -} -- cgit v1.2.3-70-g09d2