aboutsummaryrefslogtreecommitdiff
path: root/engines/hdb
diff options
context:
space:
mode:
Diffstat (limited to 'engines/hdb')
-rw-r--r--engines/hdb/lua-script.cpp58
-rw-r--r--engines/hdb/lua-script.h18
2 files changed, 76 insertions, 0 deletions
diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp
index da1f91ea85..337496b75b 100644
--- a/engines/hdb/lua-script.cpp
+++ b/engines/hdb/lua-script.cpp
@@ -86,6 +86,64 @@ bool LuaScript::loadLua(const char *name) {
return true;
}
+void LuaScript::saveGlobalNumber(const char *global, double value) {
+ // see if global already exists; if so, overwrite it.
+ for (uint i = 0; i < _globals.size(); i++) {
+ if (!scumm_stricmp(global, _globals[i]->global)) {
+ _globals[i]->valueOrString = 0;
+ _globals[i]->value = value;
+ return;
+ }
+ }
+
+ Global *g = new Global;
+ strcpy(g->global, global);
+ g->valueOrString = 0;
+ g->value = value;
+
+ _globals.push_back(g);
+}
+
+void LuaScript::saveGlobalString(const char *global, const char *string) {
+ if (!string)
+ return;
+
+ // see if global already exists; if so, overwrite it.
+ for (uint i = 0; i < _globals.size(); i++) {
+ if (!scumm_stricmp(global, _globals[i]->global)) {
+ _globals[i]->valueOrString = 1;
+ strcpy(_globals[i]->string, string);
+ return;
+ }
+ }
+
+ Global *g = new Global;
+ strcpy(g->global, global);
+ g->valueOrString = 1;
+ strcpy(g->string, string);
+
+ _globals.push_back(g);
+}
+
+void LuaScript::loadGlobal(const char *global) {
+ for (uint i = 0; i < _globals.size(); i++) {
+ if (!scumm_stricmp(global, _globals[i]->global)) {
+ if (_globals[i]->valueOrString) {
+ lua_pushstring(_state, _globals[i]->string);
+ lua_setglobal(_state, _globals[i]->global);
+ } else {
+ lua_pushnumber(_state, _globals[i]->value);
+ lua_setglobal(_state, _globals[i]->global);
+ }
+ return;
+ }
+ }
+}
+
+void LuaScript::purgeGlobals() {
+ _globals.clear();
+}
+
void LuaScript::setLuaGlobalValue(const char *name, int value) {
if (!_state)
return;
diff --git a/engines/hdb/lua-script.h b/engines/hdb/lua-script.h
index 4f2ffd2d09..b84a19da7d 100644
--- a/engines/hdb/lua-script.h
+++ b/engines/hdb/lua-script.h
@@ -37,12 +37,28 @@ enum {
kCameraYOff = (32 * 2 + 16) // 2.50 Tiles Extra
};
+struct Global {
+ char global[32]; // name of global variable
+ int valueOrString; // value = 0, string = 1
+ double value; // value
+ char string[32]; // string
+
+ Global() : valueOrString(0), value(0) {
+ global[0] = 0;
+ string[0] = 0;
+ }
+};
+
class LuaScript {
public:
LuaScript();
~LuaScript();
bool loadLua(const char *name);
+ void saveGlobalNumber(const char *global, double value);
+ void saveGlobalString(const char *global, const char *string);
+ void loadGlobal(const char *global);
+ void purgeGlobals();
bool init();
bool initScript(Common::SeekableReadStream *stream, const char *scriptName, int32 length);
@@ -74,6 +90,8 @@ private:
bool registerExtensions();
void stripComments(char *chunk);
void addPatches(Common::String &chunk, const char *scriptName);
+
+ Common::Array<Global *> _globals;
};
void lua_printstack(lua_State *L);