Skip to content

Commit

Permalink
Lua code: replace deprecated wesnoth.get_variable() calls
Browse files Browse the repository at this point in the history
(cherry-picked from commit f1764d1)
  • Loading branch information
mattsc committed Oct 7, 2018
1 parent 448a207 commit 05d5afa
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 64 deletions.
4 changes: 2 additions & 2 deletions data/ai/scenarios/scenario-AI_Arena_small.cfg
Expand Up @@ -70,7 +70,7 @@
name="_replace_ai_2"
[/fire_event]
[lua]
code= << W.fire_event {name=wesnoth.get_variable("test_id") } >>
code= << W.fire_event {name=wml.variables["test_id"] } >>
[/lua]
[/command]
[/set_menu_item]
Expand Down Expand Up @@ -163,7 +163,7 @@
[/fire_event]
[/command]
[lua]
code= << W.fire_event {name=wesnoth.get_variable("test_id") } >>
code= << W.fire_event {name=wml.variables["test_id"] } >>
[/lua]
[/event]

Expand Down
6 changes: 3 additions & 3 deletions data/lua/core.lua
Expand Up @@ -271,7 +271,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
}

local function get_variable_proxy(k)
local v = wesnoth.get_variable(k)
local v = wml.variables[k]
if type(v) == "table" then
v = setmetatable({ __varname = k }, variable_mt)
end
Expand All @@ -280,7 +280,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then

local function set_variable_proxy(k, v)
if getmetatable(v) == "WML variable proxy" then
v = wesnoth.get_variable(v.__varname)
v = wml.variables[v.__varname]
end
wesnoth.set_variable(k, v)
end
Expand Down Expand Up @@ -378,7 +378,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
--! @returns a table containing all the variable proxies (starting at index 1).
function wml.array_access.get_proxy(var)
local result = {}
for i = 1, wesnoth.get_variable(var .. ".length") do
for i = 1, wml.variables[var .. ".length"] do
result[i] = get_variable_proxy(string.format("%s[%d]", var, i - 1))
end
return result
Expand Down
6 changes: 3 additions & 3 deletions data/lua/helper.lua
Expand Up @@ -44,7 +44,7 @@ function helper.modify_unit(filter, vars)
variable = "LUA_modify_unit",
kill = true
})
for i = 0, wesnoth.get_variable("LUA_modify_unit.length") - 1 do
for i = 0, wml.variables["LUA_modify_unit.length"] - 1 do
local u = string.format("LUA_modify_unit[%d]", i)
for k, v in pairs(vars) do
wesnoth.set_variable(u .. '.' .. k, v)
Expand All @@ -65,8 +65,8 @@ function helper.move_unit_fake(filter, to_x, to_y)
variable = "LUA_move_unit",
kill = false
})
local from_x = wesnoth.get_variable("LUA_move_unit.x")
local from_y = wesnoth.get_variable("LUA_move_unit.y")
local from_x = wml.variables["LUA_move_unit.x"]
local from_y = wml.variables["LUA_move_unit.y"]

wml_actions.scroll_to({ x=from_x, y=from_y })

Expand Down
4 changes: 2 additions & 2 deletions data/lua/location_set.lua
Expand Up @@ -145,8 +145,8 @@ end

function methods:of_wml_var(name)
local values = self.values
for i = 0, wesnoth.get_variable(name .. ".length") - 1 do
local t = wesnoth.get_variable(string.format("%s[%d]", name, i))
for i = 0, wml.variables[name .. ".length"] - 1 do
local t = wml.variables[string.format("%s[%d]", name, i)]
local x, y = t.x, t.y
t.x, t.y = nil, nil
values[index(x, y)] = next(t) and t or true
Expand Down
20 changes: 10 additions & 10 deletions data/lua/wml-flow.lua
Expand Up @@ -85,7 +85,7 @@ wesnoth.wml_actions["for"] = function(cfg)
local first
if cfg.array ~= nil then
if cfg.reverse then
first = wesnoth.get_variable(cfg.array .. ".length") - 1
first = wml.variables[cfg.array .. ".length"] - 1
loop_lim.last = 0
loop_lim.step = -1
else
Expand Down Expand Up @@ -119,16 +119,16 @@ wesnoth.wml_actions["for"] = function(cfg)
if loop_lim.step then
sentinel = sentinel + loop_lim.step
if loop_lim.step > 0 then
return wesnoth.get_variable(i_var) < sentinel
return wml.variables[i_var] < sentinel
else
return wesnoth.get_variable(i_var) > sentinel
return wml.variables[i_var] > sentinel
end
elseif loop_lim.last < first then
sentinel = sentinel - 1
return wesnoth.get_variable(i_var) > sentinel
return wml.variables[i_var] > sentinel
else
sentinel = sentinel + 1
return wesnoth.get_variable(i_var) < sentinel
return wml.variables[i_var] < sentinel
end
end
while loop_condition() do
Expand All @@ -144,7 +144,7 @@ wesnoth.wml_actions["for"] = function(cfg)
goto exit
end
end
wesnoth.set_variable(i_var, wesnoth.get_variable(i_var) + loop_lim.step)
wesnoth.set_variable(i_var, wml.variables[i_var] + loop_lim.step)
end
::exit::
utils.end_var_scope(i_var, save_i)
Expand Down Expand Up @@ -184,12 +184,12 @@ function wml_actions.foreach(cfg)
local this_item = utils.start_var_scope(item_name) -- if this_item is already set
local i_name = cfg.index_var or "i"
local i = utils.start_var_scope(i_name) -- if i is already set
local array_length = wesnoth.get_variable(array_name .. ".length")
local array_length = wml.variables[array_name .. ".length"]

for index, value in ipairs(array) do
-- Some protection against external modification
-- It's not perfect, though - it'd be nice if *any* change could be detected
if array_length ~= wesnoth.get_variable(array_name .. ".length") then
if array_length ~= wml.variables[array_name .. ".length"] then
helper.wml_error("WML array length changed during [foreach] iteration")
end
wesnoth.set_variable(item_name, value)
Expand All @@ -210,7 +210,7 @@ function wml_actions.foreach(cfg)
end
-- set back the content, in case the author made some modifications
if not cfg.readonly then
array[index] = wesnoth.get_variable(item_name)
array[index] = wml.variables[item_name]
end
end
::exit::
Expand All @@ -236,7 +236,7 @@ function wml_actions.foreach(cfg)
end

function wml_actions.switch(cfg)
local var_value = wesnoth.get_variable(cfg.variable)
local var_value = wml.variables[cfg.variable]
local found = false

-- Execute all the [case]s where the value matches.
Expand Down
12 changes: 6 additions & 6 deletions data/lua/wml-tags.lua
Expand Up @@ -26,17 +26,17 @@ function wml_actions.sync_variable(cfg)
local res = {}
for name_raw in utils.split(names) do
local name = utils.trim(name_raw)
local variable_type = string.sub(name, string.len(name)) == "]" and "indexed" or ( wesnoth.get_variable(name .. ".length") > 0 and "array" or "attribute")
local variable_type = string.sub(name, string.len(name)) == "]" and "indexed" or ( wml.variables[name .. ".length"] > 0 and "array" or "attribute")
local variable_info = { name = name, type = variable_type }
table.insert(res, { "variable", variable_info })
if variable_type == "indexed" then
table.insert(variable_info, { "value", wesnoth.get_variable(name) } )
table.insert(variable_info, { "value", wml.variables[name] } )
elseif variable_type == "array" then
for i = 1, wesnoth.get_variable(name .. ".length") do
table.insert(variable_info, { "value", wesnoth.get_variable(string.format("%s[%d]", name, i - 1)) } )
for i = 1, wml.variables[name .. ".length"] do
table.insert(variable_info, { "value", wml.variables[string.format("%s[%d]", name, i - 1)] } )
end
else
variable_info.value = wesnoth.get_variable(name)
variable_info.value = wml.variables[name]
end
end
return res
Expand Down Expand Up @@ -821,7 +821,7 @@ end

wml_actions.unstore_unit = function(cfg)
local variable = cfg.variable or helper.wml_error("[unstore_unit] missing required 'variable' attribute")
local unit_cfg = wesnoth.get_variable(variable) or helper.wml_error("[unstore_unit]: variable '" .. variable .. "' doesn't exist")
local unit_cfg = wml.variables[variable] or helper.wml_error("[unstore_unit]: variable '" .. variable .. "' doesn't exist")
if type(unit_cfg) ~= "table" or unit_cfg.type == nil then
helper.wml_error("[unstore_unit]: variable '" .. variable .. "' doesn't contain unit data")
end
Expand Down
10 changes: 5 additions & 5 deletions data/lua/wml-utils.lua
Expand Up @@ -30,7 +30,7 @@ function utils.vwriter.init(cfg, default_variable)
if is_explicit_index then
-- explicit indexes behave always like "replace"
elseif mode == "append" then
index = wesnoth.get_variable(variable .. ".length")
index = wml.variables[variable .. ".length"]
elseif mode ~= "replace" then
wesnoth.set_variable(variable)
end
Expand Down Expand Up @@ -119,7 +119,7 @@ function utils.handle_event_commands(cfg, scope_type)
local from = arg.variable or
helper.wml_error("[insert_tag] found with no variable= field")
arg = wesnoth.get_variable(from)
arg = wml.variables[from]
if type(arg) ~= "table" then
-- Corner case: A missing variable is replaced
-- by an empty container rather than being ignored.
Expand All @@ -138,8 +138,8 @@ function utils.handle_event_commands(cfg, scope_type)
cmd(arg)
if current_exit ~= "none" then break end
j = j + 1
if j >= wesnoth.get_variable(insert_from .. ".length") then break end
arg = wml.tovconfig(wesnoth.get_variable(string.format("%s[%d]", insert_from, j)))
if j >= wml.variables[insert_from .. ".length"] then break end
arg = wml.tovconfig(wml.variables[string.format("%s[%d]", insert_from, j)])
until false
else
cmd(arg)
Expand Down Expand Up @@ -193,7 +193,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.array_access.get(name) --containers and arrays
if #var == 0 then var = wesnoth.get_variable(name) end --scalars (and nil/empty)
if #var == 0 then var = wml.variables[name] end --scalars (and nil/empty)
wesnoth.set_variable(name)
return var
end
Expand Down
10 changes: 5 additions & 5 deletions data/lua/wml/modify_unit.lua
Expand Up @@ -33,7 +33,7 @@ function wml_actions.modify_unit(cfg)
local function handle_unit(unit_num)
local children_handled = {}
local unit_path = string.format("%s[%u]", unit_variable, unit_num)
local this_unit = wesnoth.get_variable(unit_path)
local this_unit = wml.variables[unit_path]
wesnoth.set_variable("this_unit", this_unit)
handle_attributes(cfg, unit_path, true)

Expand All @@ -48,7 +48,7 @@ function wml_actions.modify_unit(cfg)
else
mod = wml.parsed(mod)
end
local unit = wesnoth.get_variable(unit_path)
local unit = wml.variables[unit_path]
unit = wesnoth.create_unit(unit)
wesnoth.add_modification(unit, current_tag, mod)
unit = unit.__cfg;
Expand All @@ -57,7 +57,7 @@ function wml_actions.modify_unit(cfg)
local mod = current_table[2]
local apply_to = mod.apply_to
if wesnoth.effects[apply_to] then
local unit = wesnoth.get_variable(unit_path)
local unit = wml.variables[unit_path]
unit = wesnoth.create_unit(unit)
wesnoth.effects[apply_to](unit, mod)
unit = unit.__cfg;
Expand All @@ -78,14 +78,14 @@ function wml_actions.modify_unit(cfg)

if cfg.type then
if cfg.type ~= "" then wesnoth.set_variable(unit_path .. ".advances_to", cfg.type) end
wesnoth.set_variable(unit_path .. ".experience", wesnoth.get_variable(unit_path .. ".max_experience"))
wesnoth.set_variable(unit_path .. ".experience", wml.variables[unit_path .. ".max_experience"])
end
wml_actions.kill({ id = this_unit.id, animate = false })
wml_actions.unstore_unit { variable = unit_path }
end

wml_actions.store_unit { {"filter", filter}, variable = unit_variable }
local max_index = wesnoth.get_variable(unit_variable .. ".length") - 1
local max_index = wml.variables[unit_variable .. ".length"] - 1

local this_unit = utils.start_var_scope("this_unit")
for current_unit = 0, max_index do
Expand Down
26 changes: 13 additions & 13 deletions data/lua/wml/set_variable.lua
Expand Up @@ -12,43 +12,43 @@ function wesnoth.wml_actions.set_variable(cfg)
end

if cfg.to_variable then
wesnoth.set_variable(name, wesnoth.get_variable(cfg.to_variable))
wesnoth.set_variable(name, wml.variables[cfg.to_variable])
end

if cfg.suffix then
wesnoth.set_variable(name, (wesnoth.get_variable(name) or '') .. (cfg.suffix or ''))
wesnoth.set_variable(name, (wml.variables[name] or '') .. (cfg.suffix or ''))
end

if cfg.prefix then
wesnoth.set_variable(name, (cfg.prefix or '') .. (wesnoth.get_variable(name) or ''))
wesnoth.set_variable(name, (cfg.prefix or '') .. (wml.variables[name] or ''))
end

if cfg.add then
wesnoth.set_variable(name, (tonumber(wesnoth.get_variable(name)) or 0) + (tonumber(cfg.add) or 0))
wesnoth.set_variable(name, (tonumber(wml.variables[name]) or 0) + (tonumber(cfg.add) or 0))
end

if cfg.sub then
wesnoth.set_variable(name, (tonumber(wesnoth.get_variable(name)) or 0) - (tonumber(cfg.sub) or 0))
wesnoth.set_variable(name, (tonumber(wml.variables[name]) or 0) - (tonumber(cfg.sub) or 0))
end

if cfg.multiply then
wesnoth.set_variable(name, (tonumber(wesnoth.get_variable(name)) or 0) * (tonumber(cfg.multiply) or 0))
wesnoth.set_variable(name, (tonumber(wml.variables[name]) or 0) * (tonumber(cfg.multiply) or 0))
end

if cfg.divide then
local divide = tonumber(cfg.divide) or 0
if divide == 0 then helper.wml_error("division by zero on variable " .. name) end
wesnoth.set_variable(name, (tonumber(wesnoth.get_variable(name)) or 0) / divide)
wesnoth.set_variable(name, (tonumber(wml.variables[name]) or 0) / divide)
end

if cfg.modulo then
local modulo = tonumber(cfg.modulo) or 0
if modulo == 0 then helper.wml_error("division by zero on variable " .. name) end
wesnoth.set_variable(name, (tonumber(wesnoth.get_variable(name)) or 0) % modulo)
wesnoth.set_variable(name, (tonumber(wml.variables[name]) or 0) % modulo)
end

if cfg.abs then
wesnoth.set_variable(name, math.abs(tonumber(wesnoth.get_variable(name)) or 0))
wesnoth.set_variable(name, math.abs(tonumber(wml.variables[name]) or 0))
end

if cfg.root then
Expand All @@ -64,7 +64,7 @@ function wesnoth.wml_actions.set_variable(cfg)
root_fcn = function(n) return n ^ (1 / root) end
end

local radicand = tonumber(wesnoth.get_variable(name)) or 0
local radicand = tonumber(wml.variables[name]) or 0
if radicand < 0 and root % 2 == 0 then
if root == 2 then
helper.wml_error("square root of negative number on variable " .. name)
Expand All @@ -77,11 +77,11 @@ function wesnoth.wml_actions.set_variable(cfg)
end

if cfg.power then
wesnoth.set_variable(name, (tonumber(wesnoth.get_variable(name)) or 0) ^ (tonumber(cfg.power) or 0))
wesnoth.set_variable(name, (tonumber(wml.variables[name]) or 0) ^ (tonumber(cfg.power) or 0))
end

if cfg.round then
local var = tonumber(wesnoth.get_variable(name) or 0)
local var = tonumber(wml.variables[name] or 0)
local round_val = cfg.round
if round_val == "ceil" then
wesnoth.set_variable(name, math.ceil(var))
Expand Down Expand Up @@ -130,7 +130,7 @@ function wesnoth.wml_actions.set_variable(cfg)

if cfg.formula then
local fcn = wesnoth.compile_formula(cfg.formula)
wesnoth.set_variable(name, fcn(wesnoth.get_variable(name)))
wesnoth.set_variable(name, fcn(wml.variables[name]))
end

local join_child = wml.get_child(cfg, "join")
Expand Down
2 changes: 1 addition & 1 deletion data/lua/wml/test_condition.lua
Expand Up @@ -34,7 +34,7 @@ local function explain(current_cfg, expect, logger)
end
explanation = string.format("%s\n\t[/%s]", explanation, tag)
if tag == "variable" then
explanation = string.format("%s\n\tNote: The variable %s currently has the value %q.", explanation, this_cfg.name, tostring(wesnoth.get_variable(this_cfg.name)))
explanation = string.format("%s\n\tNote: The variable %s currently has the value %q.", explanation, this_cfg.name, tostring(wml.variables[this_cfg.name]))
end
wesnoth.log(logger, explanation, true)
return true
Expand Down
6 changes: 3 additions & 3 deletions data/multiplayer/eras.lua
Expand Up @@ -3,7 +3,7 @@ local T = wml.tag
local res = {}

res.quick_4mp_leaders = function(args)
local make_4mp_leaders_quick = wesnoth.get_variable("make_4mp_leaders_quick")
local make_4mp_leaders_quick = wml.variables["make_4mp_leaders_quick"]
if make_4mp_leaders_quick == nil then
make_4mp_leaders_quick = wesnoth.game_config.mp_settings and (wesnoth.game_config.mp_settings.mp_campaign == "")
end
Expand All @@ -22,7 +22,7 @@ res.quick_4mp_leaders = function(args)
end

res.turns_over_advantage = function()
local show_turns_over_advantage = wesnoth.get_variable("show_turns_over_advantage")
local show_turns_over_advantage = wml.variables["show_turns_over_advantage"]
if show_turns_over_advantage == nil then
show_turns_over_advantage = wesnoth.game_config.mp_settings and (wesnoth.game_config.mp_settings.mp_campaign == "")
end
Expand Down Expand Up @@ -67,7 +67,7 @@ res.turns_over_advantage = function()
for i, unit in ipairs( wesnoth.get_units { side = side } ) do
if not unit.__cfg.canrecruit then
wesnoth.fire("unit_worth", { id = unit.id })
units = units + wesnoth.get_variable("unit_worth")
units = units + wml.variables["unit_worth"]
end
end
-- Up to here
Expand Down

0 comments on commit 05d5afa

Please sign in to comment.