summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-15 06:35:03 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-15 06:35:03 +0000
commit99548fb47fee447b5f22d5338501ac574086c4fd (patch)
treefcef60aed6d7d0f04437249815af36c25461c5dd /src
parent2fa455ff7870dc09d75bb89a897c7c1d26eb9020 (diff)
downloadmanaserv-99548fb47fee447b5f22d5338501ac574086c4fd.tar.gz
manaserv-99548fb47fee447b5f22d5338501ac574086c4fd.tar.bz2
manaserv-99548fb47fee447b5f22d5338501ac574086c4fd.tar.xz
manaserv-99548fb47fee447b5f22d5338501ac574086c4fd.zip
Added helper functions for loading files and NPCs as scripts. Put Lua helpers into a separate library automatically loaded into new contexts.
Diffstat (limited to 'src')
-rw-r--r--src/game-server/testing.cpp39
-rw-r--r--src/scripting/lua.cpp1
-rw-r--r--src/scripting/script.cpp25
-rw-r--r--src/scripting/script.hpp16
4 files changed, 62 insertions, 19 deletions
diff --git a/src/game-server/testing.cpp b/src/game-server/testing.cpp
index f32ca68c..4f57631a 100644
--- a/src/game-server/testing.cpp
+++ b/src/game-server/testing.cpp
@@ -4,15 +4,11 @@
#include <cassert>
-#include "defines.h"
-#include "resourcemanager.h"
#include "game-server/gamehandler.hpp"
#include "game-server/item.hpp"
#include "game-server/itemmanager.hpp"
#include "game-server/mapcomposite.hpp"
-#include "game-server/mapmanager.hpp"
#include "game-server/state.hpp"
-#include "net/messageout.hpp"
#include "scripting/script.hpp"
static void dropItem(MapComposite *map, int x, int y, int type)
@@ -26,29 +22,40 @@ static void dropItem(MapComposite *map, int x, int y, int type)
GameState::insert(i);
}
+static char const *npc1 =
+ "npc2_times = 1\n"
+ "function npc_handler(npc, ch)\n"
+ " do_message(npc, ch, \"You know what?\")\n"
+ " do_message(npc, ch, string.format(\"I have already asked this question %d times today.\", npc2_times))\n"
+ " npc2_times = npc2_times + 1\n"
+ "end\n";
+
+static char const *npc2 =
+ "function npc_handler(npc, ch)\n"
+ " do_message(npc, ch, \"Don't you think the guy behind me is my evil twin?\")\n"
+ "end\n";
+
void testingMap(MapComposite *map)
{
switch (map->getID())
{
case 1:
{
- // Drop some items
+ // Drop some items.
dropItem(map, 58 * 32 + 16, 20 * 32 + 16, 508);
dropItem(map, 58 * 32 + 16, 21 * 32 + 16, 524);
- // Associate a script
- Script *s = Script::create("lua");
- if (s)
+ // Create a Lua context.
+ if (Script *s = Script::create("lua"))
{
- ResourceManager *resman = ResourceManager::getInstance();
- int fileSize;
- char *buffer = (char *)resman->loadFile("test.lua", fileSize);
- if (buffer)
- {
- s->load(buffer);
- free(buffer);
- }
+ // Load a script.
+ s->loadFile("test.lua");
+
+ // Create two NPCs.
+ s->loadNPC(107, 53 * 32 + 16, 21 * 32 + 16, npc1);
+ s->loadNPC(107, 53 * 32 + 16, 23 * 32 + 16, npc2);
+ // Associate the script context to the map.
map->setScript(s);
s->setMap(map);
s->prepare("initialize");
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 0fc3283d..34415a27 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -356,6 +356,7 @@ LuaScript::LuaScript():
lua_settable(mState, LUA_REGISTRYINDEX);
lua_settop(mState, 0);
+ loadFile("scripts/libtmw.lua");
}
LuaScript::~LuaScript()
diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp
index 12aa0d18..3e19305d 100644
--- a/src/scripting/script.cpp
+++ b/src/scripting/script.cpp
@@ -23,9 +23,11 @@
#include <map>
-#include "utils/logger.h"
#include "scripting/script.hpp"
+#include "resourcemanager.h"
+#include "utils/logger.h"
+
typedef std::map< std::string, Script::Factory > Engines;
static Engines *engines = NULL;
@@ -60,3 +62,24 @@ void Script::update()
execute();
}
+void Script::loadFile(std::string const &name)
+{
+ ResourceManager *resman = ResourceManager::getInstance();
+ int size;
+ char *buffer = (char *)resman->loadFile(name, size);
+ if (buffer)
+ {
+ load(buffer);
+ free(buffer);
+ }
+}
+
+void Script::loadNPC(int id, int x, int y, char const *prog)
+{
+ load(prog);
+ prepare("create_npc_delayed");
+ push(id);
+ push(x);
+ push(y);
+ execute();
+}
diff --git a/src/scripting/script.hpp b/src/scripting/script.hpp
index 136ae9ff..65fc0044 100644
--- a/src/scripting/script.hpp
+++ b/src/scripting/script.hpp
@@ -53,12 +53,24 @@ class Script
virtual ~Script() {}
/**
- * Loads a chunk of text into the script context and executes
- * its global statements.
+ * Loads a chunk of text into script context and executes its global
+ * statements.
*/
virtual void load(char const *) = 0;
/**
+ * Loads a text file into script context and executes its global
+ * statements.
+ */
+ virtual void loadFile(std::string const &);
+
+ /**
+ * Loads a chunk of text and considers it as an NPC handler. This
+ * handler will later be used to create the given NPC.
+ */
+ virtual void loadNPC(int id, int x, int y, char const *);
+
+ /**
* Called every tick for the script to manage its data.
* Calls the "update" function of the script by default.
*/