From 99548fb47fee447b5f22d5338501ac574086c4fd Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Wed, 15 Aug 2007 06:35:03 +0000 Subject: Added helper functions for loading files and NPCs as scripts. Put Lua helpers into a separate library automatically loaded into new contexts. --- src/scripting/lua.cpp | 1 + src/scripting/script.cpp | 25 ++++++++++++++++++++++++- src/scripting/script.hpp | 16 ++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) (limited to 'src/scripting') diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 0fc3283..34415a2 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 12aa0d1..3e19305 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -23,9 +23,11 @@ #include -#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 136ae9f..65fc004 100644 --- a/src/scripting/script.hpp +++ b/src/scripting/script.hpp @@ -53,11 +53,23 @@ 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. -- cgit