Skip to content

Commit

Permalink
Lua API: Some tweaks of the experimental wml module
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 17, 2018
1 parent 23aac4f commit 08ec901
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
4 changes: 2 additions & 2 deletions data/campaigns/Eastern_Invasion/lua/bandits.lua
Expand Up @@ -35,7 +35,7 @@ end

local function bandits_found(x,y)
local bandit_types = vars.bandit_types
local bandit_villages = wml.variable.get_array("bandit_villages")
local bandit_villages = wml.array_access.get("bandit_villages")
local boss_found = vars.boss_found
local visited = vars.villages_visited
local rand1 = helper.rand("3,4")
Expand Down Expand Up @@ -69,7 +69,7 @@ local function bandits_found(x,y)
end

function wml_actions.bandit_village_capture(cfg)
local bandit_villages = wml.variable.get_proxy_array("bandit_villages")
local bandit_villages = wml.array_access.get_proxy("bandit_villages")
local x = cfg.x or helper.wml_error("[bandit_village_capture] missing required x= attribute.")
local y = cfg.y or helper.wml_error("[bandit_village_capture] missing required y= attribute.")

Expand Down
51 changes: 39 additions & 12 deletions data/lua/core.lua
Expand Up @@ -213,11 +213,25 @@ end
if wesnoth.kernel_type() == "Game Lua Kernel" then
--[========[Basic variable access]========]

wml.variable = {}
wml.variable.get = wesnoth.get_variable
wml.variable.set = wesnoth.set_variable
wml.variable.get_all = wesnoth.get_all_vars
-- Get all variables via wml.all_variables (read-only)
setmetatable(wml, {
__metatable = "WML module",
__index = function(self, key)
if key == 'all_variables' then
return wesnoth.get_all_variables()
end
return rawget(self, key)
end,
__newindex = function(self, key, value)
if key == 'all_variables' then
error("all_variables is read-only")
-- TODO Implement writing?
end
rawset(self, key, value)
end
})

-- Get and set variables via wml.variables[variable_path]
wml.variables = setmetatable({}, {
__metatable = "WML variables",
__index = function(_, key)
Expand Down Expand Up @@ -283,7 +297,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
end
}

wml.variable.proxy = setmetatable({}, root_variable_mt)
wml.variables_proxy = setmetatable({}, root_variable_mt)

--[========[Variable Array Access]========]

Expand Down Expand Up @@ -314,9 +328,11 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
error(string.format("Invalid context for %s: expected nil, side, or unit", err_hint), 3)
end

wml.array_access = {}

--! Fetches all the WML container variables with name @a var.
--! @returns a table containing all the variables (starting at index 1).
function wml.variable.get_array(var, context)
function wml.array_access.get(var, context)
context = resolve_variable_context(context, "get_variable_array")
local result = {}
for i = 1, context.get(var .. ".length") do
Expand All @@ -326,7 +342,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
end

--! Puts all the elements of table @a t inside a WML container with name @a var.
function wml.variable.set_array(var, t, context)
function wml.array_access.set(var, t, context)
context = resolve_variable_context(context, "set_variable_array")
context.set(var)
for i, v in ipairs(t) do
Expand All @@ -335,25 +351,36 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
end

--! Creates proxies for all the WML container variables with name @a var.
--! This is similar to wml.variable.get_array, except that the elements
--! This is similar to wml.array_access.get, except that the elements
--! can be used for writing too.
--! @returns a table containing all the variable proxies (starting at index 1).
function wml.variable.get_proxy_array(var)
function wml.array_access.get_proxy(var)
local result = {}
for i = 1, wesnoth.get_variable(var .. ".length") do
result[i] = get_variable_proxy(string.format("%s[%d]", var, i - 1))
end
return result
end

-- More convenient when accessing global variables
wml.array_variables = setmetatable({}, {
__metatable = "WML variables",
__index = function(_, key)
return wml.array_access.get(key)
end,
__newindex = function(_, key, value)
wml.array_access.set(key, value)
end
})
end

-- Some C++ functions are deprecated; apply the messages here.
-- Note: It must happen AFTER the C++ functions are reassigned above to their new location.
-- These deprecated functions will probably never be removed.
if wesnoth.kernel_type() == "Game Lua Kernel" then
wesnoth.get_variable = wesnoth.deprecate_api('wesnoth.get_variable', 'wml.variable.get', 1, nil, wesnoth.get_variable)
wesnoth.set_variable = wesnoth.deprecate_api('wesnoth.set_variable', 'wml.variable.set', 1, nil, wesnoth.set_variable)
wesnoth.get_all_vars = wesnoth.deprecate_api('wesnoth.get_all_vars', 'wml.variable.get_all', 1, nil, wesnoth.get_all_vars)
wesnoth.get_variable = wesnoth.deprecate_api('wesnoth.get_variable', 'wml.variables', 1, nil, wesnoth.get_variable)
wesnoth.set_variable = wesnoth.deprecate_api('wesnoth.set_variable', 'wml.variables', 1, nil, wesnoth.set_variable)
wesnoth.get_all_vars = wesnoth.deprecate_api('wesnoth.get_all_vars', 'wml.all_variables', 1, nil, wesnoth.get_all_vars)
end
wesnoth.tovconfig = wesnoth.deprecate_api('wesnoth.tovconfig', 'wml.tovconfig', 1, nil, wesnoth.tovconfig)
wesnoth.debug = wesnoth.deprecate_api('wesnoth.debug', 'wml.tostring', 1, nil, wesnoth.debug)
12 changes: 6 additions & 6 deletions data/lua/helper.lua
Expand Up @@ -99,11 +99,11 @@ function helper.move_unit_fake(filter, to_x, to_y)
wesnoth.set_variable("LUA_move_unit")
end

-- Metatable that redirects access to wml.variable.proxy
-- Metatable that redirects access to wml.variables_proxy
local proxy_var_mt = {
__metatable = "WML variables",
__index = function(t, k) return wml.variable.proxy[k] end,
__newindex = function(t, k, v) wml.variable.proxy[k] = v end,
__index = function(t, k) return wml.variables_proxy[k] end,
__newindex = function(t, k, v) wml.variables_proxy[k] = v end,
}

function helper.set_wml_var_metatable(t)
Expand Down Expand Up @@ -323,9 +323,9 @@ helper.child_count = wml.child_count
helper.child_range = wml.child_range
helper.child_array = wml.child_array
if wesnoth.kernel_type() == "Game Lua Kernel" then
helper.get_variable_array = wml.variable.get_array
helper.set_variable_array = wml.variable.set_array
helper.get_variable_proxy_array = wml.variable.get_proxy_array
helper.get_variable_array = wml.array_access.get
helper.set_variable_array = wml.array_access.set
helper.get_variable_proxy_array = wml.array_access.get_proxy
end
helper.literal = wml.literal
helper.parsed = wml.parsed
Expand Down
4 changes: 2 additions & 2 deletions data/lua/wml-flow.lua
Expand Up @@ -178,7 +178,7 @@ function wml_actions.foreach(cfg)
end

local array_name = cfg.array or helper.wml_error "[foreach] missing required array= attribute"
local array = wml.variable.get_array(array_name)
local array = wml.array_access.get(array_name)
if #array == 0 then return end -- empty and scalars unwanted
local item_name = cfg.variable or "this_item"
local this_item = utils.start_var_scope(item_name) -- if this_item is already set
Expand Down Expand Up @@ -232,7 +232,7 @@ function wml_actions.foreach(cfg)
Note that altering the array via indexing (with the index_var)
is not supported; any such changes will be reverted by this line.
]]
wml.variable.set_array(array_name, array)
wml.array_access.set(array_name, array)
end

function wml_actions.switch(cfg)
Expand Down
4 changes: 2 additions & 2 deletions data/lua/wml-utils.lua
Expand Up @@ -192,7 +192,7 @@ end
--note: when using these, make sure that nothing can throw over the call to end_var_scope
function utils.start_var_scope(name)
local var = wml.variable.get_array(name) --containers and arrays
local var = wml.array_access.get(name) --containers and arrays
if #var == 0 then var = wesnoth.get_variable(name) end --scalars (and nil/empty)
wesnoth.set_variable(name)
return var
Expand All @@ -201,7 +201,7 @@ end
function utils.end_var_scope(name, var)
wesnoth.set_variable(name)
if type(var) == "table" then
wml.variable.set_array(name, var)
wml.array_access.set(name, var)
else
wesnoth.set_variable(name, var)
end
Expand Down
2 changes: 1 addition & 1 deletion data/lua/wml/set_variable.lua
Expand Up @@ -120,7 +120,7 @@ function wesnoth.wml_actions.set_variable(cfg)

local string_to_join = ''

for i, element in ipairs(wml.variable.get_array(array_name)) do
for i, element in ipairs(wml.array_access.get(array_name)) do
if element[key_name] ~= nil or (not remove_empty) then
if #string_to_join > 0 then
string_to_join = string_to_join .. separator
Expand Down

0 comments on commit 08ec901

Please sign in to comment.