Lua code: replace deprecated wesnoth.get_variable() calls

(cherry-picked from commit f1764d182f)
This commit is contained in:
mattsc 2018-05-10 06:45:42 -07:00
parent 448a20779f
commit 05d5afa6a9
15 changed files with 64 additions and 64 deletions

View file

@ -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]
@ -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]

View file

@ -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
@ -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
@ -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

View file

@ -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)
@ -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 })

View file

@ -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

View file

@ -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
@ -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
@ -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)
@ -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)
@ -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::
@ -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.

View file

@ -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
@ -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

View file

@ -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
@ -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.
@ -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)
@ -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

View file

@ -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)
@ -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;
@ -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;
@ -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

View file

@ -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
@ -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)
@ -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))
@ -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")

View file

@ -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

View file

@ -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
@ -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
@ -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

View file

@ -160,7 +160,7 @@ end
-- @a base_gold_amount, gold_increment: used to cauculate the amount of gold available for each timed spawn
-- @a units_amount, gold_per_unit_amount: used to cauculate the number of units spawned in each timed spawn
local function create_timed_spaws(interval, num_spawns, base_gold_amount, gold_increment, units_amount, gold_per_unit_amount)
local configure_gold_factor = ((wesnoth.get_variable("enemey_gold_factor") or 0) + 100)/100
local configure_gold_factor = ((wml.variables["enemey_gold_factor"] or 0) + 100)/100
local random_spawn_numbers = {}
for i = 1, #random_spawns do
table.insert(random_spawn_numbers, i)
@ -243,12 +243,12 @@ local function place_units(unittypes, x, y)
end
local function final_spawn()
local spawns_left = wesnoth.get_variable("fixed_spawn.length")
local spawns_left = wml.variables["fixed_spawn.length"]
if spawns_left == 0 then
return
end
local spawn_index = wesnoth.random(spawns_left) - 1
local spawn = wesnoth.get_variable(string.format("fixed_spawn[%d]", spawn_index))
local spawn = wml.variables[string.format("fixed_spawn[%d]", spawn_index)]
wesnoth.set_variable(string.format("fixed_spawn[%d]", spawn_index))
local types = {}
for tag in helper.child_range(spawn, "type") do
@ -278,7 +278,7 @@ end)
-- when they appear is defined in the 'timed_spawn' wml array. which is created at prestart
-- which unit types get spawned is defined in the 'main_spawn' wml array which is also spawned at prestart
on_event("new turn", function()
local next_spawn = wesnoth.get_variable("timed_spawn[0]")
local next_spawn = wml.variables["timed_spawn[0]"]
if wesnoth.current.turn ~= next_spawn.turn then
return
end
@ -293,7 +293,7 @@ end)
-- on turn 'final_turn' the first 'final spawn' appears
on_event("new turn", function()
if wesnoth.current.turn ~= wesnoth.get_variable("final_turn") then
if wesnoth.current.turn ~= wml.variables["final_turn"] then
return
end
wesnoth.wml_actions.music {
@ -313,7 +313,7 @@ end)
-- after the first final spawn, spawn a new final spawn every 1 or 2 turns.
on_event("new turn", function()
if wesnoth.current.turn ~= wesnoth.get_variable("next_final_spawn") then
if wesnoth.current.turn ~= wml.variables["next_final_spawn"] then
return
end
final_spawn()
@ -322,7 +322,7 @@ end)
-- The victory condition: win when there are no enemy unit after the first final spawn appeared.
on_event("die", function()
if wesnoth.current.turn < wesnoth.get_variable("final_turn") then
if wesnoth.current.turn < wml.variables["final_turn"] then
return
end
if wesnoth.wml_conditionals.have_unit { side = "1,2"} then
@ -445,7 +445,7 @@ end
-- change weather at side 3 turns, TODO: consider the case that side 3 is empty.
on_event("side 3 turn", function()
-- get next weather event
local weather_event = wesnoth.get_variable("weather_event[0]")
local weather_event = wml.variables["weather_event[0]"]
if weather_event == nil then
return
end

View file

@ -3615,7 +3615,7 @@ unplagueable: $wml_unit.status.unplagueable"
{ "command", {
{ "set_variable", { name = "choice", value = "done" } } } } } }
})
local choice = wesnoth.get_variable("choice")
local choice = wml.variables["choice"]
-- debug :unit will reapply musthave traits, breaking these modifications for undead
-- if you want something more permanent, give the unit an object
if choice == "not_living" then

View file

@ -14,7 +14,7 @@
<<
local H = wesnoth.require('helper')
local expected = H.get_variable_array('expected')
local aspect = wesnoth.get_variable('test_attribute')
local aspect = wml.variables['test_attribute']
if ai.aspects[aspect] ~= expected[wesnoth.current.turn].value then
wesnoth.set_variable('is_valid', false)
local msg = 'Failed on turn ' .. tostring(wesnoth.current.turn)

View file

@ -3,7 +3,7 @@
name=recruit
[lua]
code =<<
local temp = wesnoth.get_variable("unit")
local temp = wml.variables["unit"]
local result = wesnoth.synchronize_choice(
function()
return { value = temp.facing }