Allow accessing variables through the new APIs in the mapgen kernel

This also reverses the deprecation of wml.tovconfig and friends in the mapgen kernel.
This commit is contained in:
Celtic Minstrel 2021-04-27 09:44:47 -04:00
parent 05b0b7a1df
commit 1410b5cef9
5 changed files with 34 additions and 10 deletions

View file

@ -37,9 +37,9 @@ function wc_ii_generate_scenario(nplayers, gen_args)
nplayers = settings.nplayers or nplayers
local id_suffix = gen_args.id_suffix or ""
local scenario_extra = wml.get_child(gen_args, "scenario")
local scenario_num = settings.scenario_num or wesnoth.get_variable("wc2_scenario") or 1
local scenario_num = settings.scenario_num or wml.variables.wc2_scenario or 1
--todo: does this work properly in the first scenario?
local enemy_stength = wesnoth.get_variable("wc2_difficulty.enemy_power") or 6
local enemy_stength = wml.variables["wc2_difficulty.enemy_power"] or 6
local scenario_data = wesnoth.dofile(string.format("./scenarios/WC_II_%dp_scenario%d.lua", nplayers, scenario_num))
local prestart_event = { name = "prestart" }

View file

@ -65,7 +65,7 @@ function random_placement(locs, num_items, min_distance, command)
end
function get_f_wct_bonus_location_filter(map)
local scenario_num = wesnoth.get_variable("wc2_scenario") or 1
local scenario_num = wml.variables.wc2_scenario or 1
return f.all(
f.terrain("G*,Hh,Uu,Uh,Dd,Ds,R*,Mm,Md,Ss,Hd,Hhd,Ww,Wwt,Wwg,Ds^Esd,Ur"),
--no adjacent to village, deep water, chasm or walls
@ -399,7 +399,7 @@ end
function world_conquest_tek_bonus_points(theme)
local res = {}
local scenario_num = wesnoth.get_variable("wc2_scenario") or 1
local scenario_num = wml.variables.wc2_scenario or 1
oceanic = get_oceanic()
f_wct_bonus_location_filter = wesnoth.map.filter(get_f_wct_bonus_location_filter(map), { oceanic = oceanic })
local possible_locs = map:find(f_wct_bonus_location_filter)

View file

@ -192,7 +192,9 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
function wml.fire(name, cfg)
wesnoth.wml_actions[name](wml.tovconfig(cfg or {}))
end
end
if wesnoth.kernel_type() ~= "Application Lua Kernel" then
--[========[Basic variable access]========]
-- Get all variables via wml.all_variables (read-only)
@ -215,7 +217,9 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
})
local get_variable_local = wml.get_variable
local set_variable_local = wml.set_variable
local set_variable_local = wml.set_variable or function()
error("Variables are read-only during map generation", 3)
end
-- Get and set variables via wml.variables[variable_path]
wml.variables = setmetatable({}, {
@ -438,10 +442,13 @@ wesnoth.tovconfig = wesnoth.deprecate_api('wesnoth.tovconfig', 'wml.tovconfig',
wesnoth.debug = wesnoth.deprecate_api('wesnoth.debug', 'wml.tostring', 1, nil, wml.tostring)
wesnoth.wml_matches_filter = wesnoth.deprecate_api('wesnoth.wml_matches_filter', 'wml.matches_filter', 1, nil, wml.matches_filter)
if wesnoth.kernel_type() == "Game Lua Kernel" then
if wesnoth.kernel_type() ~= "Application Lua Kernel" then
wesnoth.get_variable = wesnoth.deprecate_api('wesnoth.get_variable', 'wml.variables', 1, nil, wml.get_variable)
wesnoth.set_variable = wesnoth.deprecate_api('wesnoth.set_variable', 'wml.variables', 1, nil, wml.set_variable)
wesnoth.get_all_vars = wesnoth.deprecate_api('wesnoth.get_all_vars', 'wml.all_variables', 1, nil, wml.get_all_vars)
end
if wesnoth.kernel_type() == "Game Lua Kernel" then
wesnoth.set_variable = wesnoth.deprecate_api('wesnoth.set_variable', 'wml.variables', 1, nil, wml.set_variable)
wesnoth.fire = wesnoth.deprecate_api('wesnoth.fire', 'wml.fire', 1, nil, wml.fire)
wesnoth.eval_conditional = wesnoth.deprecate_api('wesnoth.eval_conditional', 'wml.eval_conditional', 1, nil, wml.eval_conditional)
end

View file

@ -222,7 +222,6 @@ mapgen_lua_kernel::mapgen_lua_kernel(const config* vars)
static luaL_Reg const callbacks[] {
{ "find_path", &intf_find_path },
{ "random", &intf_random },
{ "get_variable", &dispatch<&mapgen_lua_kernel::intf_get_variable> },
{ nullptr, nullptr }
};
@ -249,6 +248,18 @@ mapgen_lua_kernel::mapgen_lua_kernel(const config* vars)
luaL_setfuncs(L, map_callbacks, 0);
lua_pop(L, 1);
assert(lua_gettop(L) == 0);
// Add functions to the WML module
lua_getglobal(L, "wml");
static luaL_Reg const wml_callbacks[] {
{"tovconfig", &lua_common::intf_tovconfig},
// These aren't actually part of the API - they're used internally by the variable metatable.
{ "get_variable", &dispatch<&mapgen_lua_kernel::intf_get_variable>},
{ "get_all_vars", &dispatch<&mapgen_lua_kernel::intf_get_all_vars>},
{ nullptr, nullptr }
};
luaL_setfuncs(L, wml_callbacks, 0);
lua_pop(L, 1);
cmd_log_ << lua_terrainmap::register_metatables(L);
cmd_log_ << lua_terrainfilter::register_metatables(L);
@ -268,12 +279,17 @@ void mapgen_lua_kernel::user_config(const char * prog, const config & generator)
int mapgen_lua_kernel::intf_get_variable(lua_State *L)
{
static const config empty_cfg;
char const *m = luaL_checkstring(L, 1);
variable_access_const v(m, vars_ ? *vars_ : empty_cfg);
variable_access_const v(m, vars_ ? *vars_ : config());
return luaW_pushvariable(L, v) ? 1 : 0;
}
int mapgen_lua_kernel::intf_get_all_vars(lua_State *L) {
luaW_pushconfig(L, vars_ ? *vars_ : config());
return 1;
}
std::string mapgen_lua_kernel::create_map(const char * prog, const config & generator, std::optional<uint32_t> seed) // throws game::lua_error
{
random_seed_ = seed;

View file

@ -39,6 +39,7 @@ public:
private:
void run_generator(const char * prog, const config & generator);
int intf_get_variable(lua_State *L);
int intf_get_all_vars(lua_State *L);
std::optional<uint32_t> random_seed_;
std::optional<std::mt19937> default_rng_;
const config* vars_;