summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2011-11-06 21:02:23 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2011-11-07 18:54:37 +0100
commit391916f685afe93d9afb021b81f8d5b5789822bc (patch)
tree74c209d27da8db63bac99669b2dccb55f9e55650
parent80f0899c16931b41b51b062a3d020781c033bc87 (diff)
downloadmanaserv-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
-rw-r--r--docs/manaserv.xml.example7
-rw-r--r--example/serverdata/scripts/main.lua17
-rw-r--r--src/game-server/character.cpp3
-rw-r--r--src/game-server/main-game.cpp11
-rw-r--r--src/game-server/mapcomposite.cpp4
-rw-r--r--src/scripting/luascript.cpp33
-rw-r--r--src/scripting/luascript.h9
-rw-r--r--src/scripting/script.cpp34
-rw-r--r--src/scripting/script.h12
9 files changed, 62 insertions, 68 deletions
diff --git a/docs/manaserv.xml.example b/docs/manaserv.xml.example
index 75b77437..3ce0b419 100644
--- a/docs/manaserv.xml.example
+++ b/docs/manaserv.xml.example
@@ -289,4 +289,11 @@
<!-- end of mail configuration ******************************************** -->
+<!-- Scripting configuration ********************************************** -->
+
+ <option name="script_defaultEngine" value="lua"/>
+ <option name="script_mainFile" value="scripts/main.lua"/>
+
+<!-- End of scripting configuration *************************************** -->
+
</configuration>
diff --git a/example/serverdata/scripts/main.lua b/example/serverdata/scripts/main.lua
new file mode 100644
index 00000000..d5d8bd03
--- /dev/null
+++ b/example/serverdata/scripts/main.lua
@@ -0,0 +1,17 @@
+----------------------------------------------------------------------------------
+-- Copyright 2011 Manasource Development Team --
+-- --
+-- This file is part of Manasource. --
+-- --
+-- Manasource is free software; you can redistribute it and/or modify it --
+-- under the terms of the GNU General Public License as published by the Free --
+-- Software Foundation; either version 2 of the License, or any later version. --
+----------------------------------------------------------------------------------
+
+-- This is the main script file loaded by the server, as configured in
+-- manaserv.xml. It defines how certain global events should be handled.
+
+-- At the moment the event handlers are split up over the following files:
+require "scripts/global_events"
+require "scripts/special_actions"
+require "scripts/crafting"
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp
index 41d05455..f1792261 100644
--- a/src/game-server/character.cpp
+++ b/src/game-server/character.cpp
@@ -714,7 +714,8 @@ void Character::takeSpecial(int id)
void Character::clearSpecials()
{
- for(std::map<int, Special*>::iterator i = mSpecials.begin(); i != mSpecials.end(); i++)
+ for (std::map<int, Special*>::iterator i = mSpecials.begin();
+ i != mSpecials.end(); i++)
{
delete i->second;
}
diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp
index 62da4f9c..daf15611 100644
--- a/src/game-server/main-game.cpp
+++ b/src/game-server/main-game.cpp
@@ -71,9 +71,7 @@ using utils::Logger;
#define DEFAULT_MONSTERSDB_FILE "monsters.xml"
#define DEFAULT_STATUSDB_FILE "status-effects.xml"
#define DEFAULT_PERMISSION_FILE "permissions.xml"
-#define DEFAULT_GLOBAL_EVENT_SCRIPT_FILE "scripts/global_events.lua"
-#define DEFAULT_SPECIAL_ACTIONS_SCRIPT_FILE "scripts/special_actions.lua"
-#define DEFAULT_CRAFT_SCRIPT_FILE "scripts/crafting.lua"
+#define DEFAULT_GLOBAL_EVENT_SCRIPT_FILE "scripts/main.lua"
static int const WORLD_TICK_SKIP = 2; /** tolerance for lagging behind in world calculation) **/
@@ -141,9 +139,10 @@ static void initializeServer()
StatusManager::initialize(DEFAULT_STATUSDB_FILE);
PermissionManager::initialize(DEFAULT_PERMISSION_FILE);
- LuaScript::loadGlobalEventScript(DEFAULT_GLOBAL_EVENT_SCRIPT_FILE);
- LuaScript::loadSpecialActionsScript(DEFAULT_SPECIAL_ACTIONS_SCRIPT_FILE);
- LuaScript::loadCraftScript(DEFAULT_CRAFT_SCRIPT_FILE);
+ const std::string mainScriptFile =
+ Configuration::getValue("script_mainFile",
+ DEFAULT_GLOBAL_EVENT_SCRIPT_FILE);
+ Script::loadGlobalEventScript(mainScriptFile);
// --- Initialize the global handlers
// FIXME: Make the global handlers global vars or part of a bigger
diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp
index 4d0581ad..99590566 100644
--- a/src/game-server/mapcomposite.cpp
+++ b/src/game-server/mapcomposite.cpp
@@ -737,7 +737,7 @@ void MapComposite::initializeContent()
if (scriptEngineName.empty())
{
// Set engine to default value and print warning
- scriptEngineName = Configuration::getValue("defaultScriptEngine", "lua");
+ scriptEngineName = Configuration::getValue("script_defaultEngine", "lua");
LOG_WARN("No script engine specified for map script \""
+ mName + "\", falling back to default");
}
@@ -772,7 +772,7 @@ void MapComposite::initializeContent()
else if (scriptEngineName.empty())
{
// Set engine to default value and print warning
- scriptEngineName = Configuration::getValue("defaultScriptEngine", "lua");
+ scriptEngineName = Configuration::getValue("script_defaultEngine", "lua");
LOG_WARN("No script engine specified for map script \""
+ mName + "\", falling back to default");
}
diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp
index dc6230ca..af9da17b 100644
--- a/src/scripting/luascript.cpp
+++ b/src/scripting/luascript.cpp
@@ -187,36 +187,3 @@ void LuaScript::getPostCallback(Character *q, const std::string &sender,
s->nbArgs = 3;
s->execute();
}
-
-bool LuaScript::loadGlobalEventScript(const std::string &file)
-{
- Script::globalEventScript = new LuaScript();
- if (!Script::globalEventScript->loadFile(file))
- {
- Script::globalEventScript = NULL;
- return false;
- }
- return true;
-}
-
-bool LuaScript::loadSpecialActionsScript(const std::string &file)
-{
- Script::specialActionsScript = new LuaScript();
- if (!Script::specialActionsScript->loadFile(file))
- {
- Script::specialActionsScript = NULL;
- return false;
- }
- 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 b9bde2d8..0d59703c 100644
--- a/src/scripting/luascript.h
+++ b/src/scripting/luascript.h
@@ -31,7 +31,7 @@ extern "C" {
/**
* Implementation of the Script class for Lua.
*/
-class LuaScript: public Script
+class LuaScript : public Script
{
public:
/**
@@ -66,13 +66,6 @@ class LuaScript: public Script
void processRemoveEvent(Thing *thing);
- /**
- * Loads the global event script file
- */
- 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;
int nbArgs;
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");
}
}
diff --git a/src/scripting/script.h b/src/scripting/script.h
index e2ab3afa..a7737512 100644
--- a/src/scripting/script.h
+++ b/src/scripting/script.h
@@ -136,25 +136,29 @@ class Script
virtual void processRemoveEvent(Thing *thing) = 0;
/**
+ * Loads the global event script file
+ */
+ static bool loadGlobalEventScript(const std::string &file);
+
+ /**
* Runs a function from the global event script file
*/
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<InventoryItem> recipe);
+ static bool performCraft(Being *crafter, const std::list<InventoryItem> &recipe);
static std::string determineEngineByFilename(const std::string &filename);
protected:
- static Script *globalEventScript;
- static Script *specialActionsScript;
- static Script *craftScript;
std::string mScriptFile;
private:
MapComposite *mMap;
EventListener mEventListener; /**< Tracking of being deaths. */
+ static Script *globalEventScript;
+
friend struct ScriptEventDispatch;
};