Working on state for location

This commit is contained in:
Vladislav Khorev committed 2026-06-18 21:48:27 +03:00
1 parent e0968b5522
commit b01e970554
13 files changed
+494 -101

No files matched your search

+64 -3
View File
@@ -281,12 +281,12 @@ namespace ZL {
api.set_function("is_night",
[loc]() {
return loc->isNight;
return loc->state.isNight;
});
api.set_function("is_dawn",
[loc]() {
return loc->isDawn;
return loc->state.isDawn;
});
// advance_darklands_hud()
@@ -311,7 +311,7 @@ namespace ZL {
// is_darklands() → bool
api.set_function("is_darklands",
[loc]() { return loc->isDarklands; });
[loc]() { return loc->state.isDarklands; });
api.set_function("setFloatValue",
[this_impl = impl.get()](const std::string& key, float value) {
@@ -974,4 +974,65 @@ namespace ZL {
}
}
void ScriptEngine::saveScriptGlobals(nlohmann::json& out) const
{
if (!impl) return;
lua_State* L = impl->lua.lua_state();
// Iterate the global table (_G). lua_next pops the key and pushes key+value.
lua_pushglobaltable(L); // stack: [_G]
lua_pushnil(L); // stack: [_G, nil] (first key)
while (lua_next(L, -2) != 0) { // stack: [_G, key, value]
if (lua_type(L, -2) == LUA_TSTRING) {
const char* key = lua_tostring(L, -2);
// Skip Lua built-in globals and the game API table.
const std::string k(key);
if (k == "_G" || k == "_VERSION" || k == "game_api") {
lua_pop(L, 1); // pop value, keep key for next iteration
continue;
}
const int t = lua_type(L, -1);
if (t == LUA_TNUMBER) {
if (lua_isinteger(L, -1)) {
out[k] = static_cast<int64_t>(lua_tointeger(L, -1));
} else {
out[k] = lua_tonumber(L, -1);
}
} else if (t == LUA_TSTRING) {
out[k] = lua_tostring(L, -1);
} else if (t == LUA_TBOOLEAN) {
out[k] = (lua_toboolean(L, -1) != 0);
}
// Skip functions, tables, userdata — recreated by re-running the script.
}
lua_pop(L, 1); // pop value, keep key for next iteration
}
lua_pop(L, 1); // pop _G
}
void ScriptEngine::loadScriptGlobals(const nlohmann::json& in)
{
if (!impl) return;
lua_State* L = impl->lua.lua_state();
for (auto it = in.begin(); it != in.end(); ++it) {
const std::string& key = it.key();
const auto& val = it.value();
if (val.is_number_integer()) {
lua_pushinteger(L, static_cast<lua_Integer>(val.get<int64_t>()));
} else if (val.is_number_float()) {
lua_pushnumber(L, static_cast<lua_Number>(val.get<double>()));
} else if (val.is_string()) {
const std::string s = val.get<std::string>();
lua_pushstring(L, s.c_str());
} else if (val.is_boolean()) {
lua_pushboolean(L, val.get<bool>() ? 1 : 0);
} else {
continue;
}
lua_setglobal(L, key.c_str());
}
}
} // namespace ZL