Skip to content

Commit

Permalink
Lua code: replace deprecated wesnoth.set_variable() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsc committed May 14, 2018
1 parent 0f157bf commit ce7faae
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 132 deletions.
2 changes: 1 addition & 1 deletion data/lua/core.lua
Expand Up @@ -286,7 +286,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
if getmetatable(v) == "WML variable proxy" then
v = wml.variables[v.__varname]
end
wesnoth.set_variable(k, v)
wml.variables[k] = v
end

function variable_mt.__index(t, k)
Expand Down
14 changes: 7 additions & 7 deletions data/lua/helper.lua
Expand Up @@ -47,14 +47,14 @@ function helper.modify_unit(filter, vars)
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)
wml.variables[u .. '.' .. k] = v
end
wml_actions.unstore_unit({
variable = u,
find_vacant = false
})
end
wesnoth.set_variable("LUA_modify_unit")
wml.variables["LUA_modify_unit"] = nil
end

--! Fakes the move of a unit satisfying the given @a filter to position @a x, @a y.
Expand All @@ -71,12 +71,12 @@ function helper.move_unit_fake(filter, to_x, to_y)
wml_actions.scroll_to({ x=from_x, y=from_y })

if to_x < from_x then
wesnoth.set_variable("LUA_move_unit.facing", "sw")
wml.variables["LUA_move_unit.facing"] = "sw"
elseif to_x > from_x then
wesnoth.set_variable("LUA_move_unit.facing", "se")
wml.variables["LUA_move_unit.facing"] = "se"
end
wesnoth.set_variable("LUA_move_unit.x", to_x)
wesnoth.set_variable("LUA_move_unit.y", to_y)
wml.variables["LUA_move_unit.x"] = to_x
wml.variables["LUA_move_unit.y"] = to_y

wml_actions.kill({
x = from_x,
Expand All @@ -96,7 +96,7 @@ function helper.move_unit_fake(filter, to_x, to_y)

wml_actions.unstore_unit({ variable="LUA_move_unit", find_vacant=true })
wml_actions.redraw({})
wesnoth.set_variable("LUA_move_unit")
wml.variables["LUA_move_unit"] = nil
end

-- Metatable that redirects access to wml.variables_proxy
Expand Down
8 changes: 4 additions & 4 deletions data/lua/location_set.lua
Expand Up @@ -175,13 +175,13 @@ end

function methods:to_wml_var(name)
local i = 0
wesnoth.set_variable(name)
wml.variables[name] = nil
self:stable_iter(function(x, y, v)
if type(v) == 'table' then
wesnoth.set_variable(string.format("%s[%d]", name, i), v)
wml.variables[string.format("%s[%d]", name, i)] = v
end
wesnoth.set_variable(string.format("%s[%d].x", name, i), x)
wesnoth.set_variable(string.format("%s[%d].y", name, i), y)
wml.variables[string.format("%s[%d].x", name, i)] = x
wml.variables[string.format("%s[%d].y", name, i)] = y
i = i + 1
end)
end
Expand Down
8 changes: 4 additions & 4 deletions data/lua/wml-flow.lua
Expand Up @@ -113,7 +113,7 @@ wesnoth.wml_actions["for"] = function(cfg)
end
local i_var = cfg.variable or "i"
local save_i = utils.start_var_scope(i_var)
wesnoth.set_variable(i_var, first)
wml.variables[i_var] = first
local function loop_condition()
local sentinel = loop_lim.last
if loop_lim.step then
Expand Down Expand Up @@ -144,7 +144,7 @@ wesnoth.wml_actions["for"] = function(cfg)
goto exit
end
end
wesnoth.set_variable(i_var, wml.variables[i_var] + loop_lim.step)
wml.variables[i_var] = wml.variables[i_var] + loop_lim.step
end
::exit::
utils.end_var_scope(i_var, save_i)
Expand Down Expand Up @@ -192,9 +192,9 @@ function wml_actions.foreach(cfg)
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)
wml.variables[item_name] = value
-- set index variable
wesnoth.set_variable(i_name, index-1) -- here -1, because of WML array
wml.variables[i_name] = index-1 -- here -1, because of WML array
-- perform actions
for do_child in wml.child_range(cfg, "do") do
local action = utils.handle_event_commands(do_child, "loop")
Expand Down
34 changes: 17 additions & 17 deletions data/lua/wml-tags.lua
Expand Up @@ -46,13 +46,13 @@ function wml_actions.sync_variable(cfg)
local name = variable.name

if variable.type == "indexed" then
wesnoth.set_variable(name, variable[1][2])
wml.variables[name] = variable[1][2]
elseif variable.type == "array" then
for index, cfg_pair in ipairs(variable) do
wesnoth.set_variable( string.format("%s[%d]", name, index - 1), cfg_pair[2])
wml.variables[string.format("%s[%d]", name, index - 1)] = cfg_pair[2]
end
else
wesnoth.set_variable(name, variable.value)
wml.variables[name] = variable.value
end
end
end
Expand Down Expand Up @@ -102,14 +102,14 @@ end
--since the gold is stored in a scalar variable, not a container (there's no key).
function wml_actions.store_gold(cfg)
local team = wesnoth.get_sides(cfg)[1]
if team then wesnoth.set_variable(cfg.variable or "gold", team.gold) end
if team then wml.variables[cfg.variable or "gold"] = team.gold end
end

function wml_actions.clear_variable(cfg)
local names = cfg.name or
helper.wml_error "[clear_variable] missing required name= attribute."
for w in utils.split(names) do
wesnoth.set_variable(utils.trim(w))
wml.variables[utils.trim(w)] = nil
end
end

Expand All @@ -120,7 +120,7 @@ function wml_actions.store_unit_type_ids(cfg)
end
table.sort(types)
types = table.concat(types, ',')
wesnoth.set_variable(cfg.variable or "unit_type_ids", types)
wml.variables[cfg.variable or "unit_type_ids"] = types
end

function wml_actions.store_unit_type(cfg)
Expand Down Expand Up @@ -241,9 +241,9 @@ end
function wml_actions.store_map_dimensions(cfg)
local var = cfg.variable or "map_size"
local w, h, b = wesnoth.get_map_size()
wesnoth.set_variable(var .. ".width", w)
wesnoth.set_variable(var .. ".height", h)
wesnoth.set_variable(var .. ".border_size", b)
wml.variables[var .. ".width"] = w
wml.variables[var .. ".height"] = h
wml.variables[var .. ".border_size"] = b
end

function wml_actions.unit_worth(cfg)
Expand All @@ -257,12 +257,12 @@ function wml_actions.unit_worth(cfg)
local uta = wesnoth.unit_types[w]
if uta and uta.cost > best_adv then best_adv = uta.cost end
end
wesnoth.set_variable("cost", ut.cost)
wesnoth.set_variable("next_cost", best_adv)
wesnoth.set_variable("health", math.floor(hp * 100))
wesnoth.set_variable("experience", math.floor(xp * 100))
wesnoth.set_variable("recall_cost", ut.recall_cost)
wesnoth.set_variable("unit_worth", math.floor(math.max(ut.cost * hp, best_adv * xp)))
wml.variables["cost"] = ut.cost
wml.variables["next_cost"] = best_adv
wml.variables["health"] = math.floor(hp * 100)
wml.variables["experience"] = math.floor(xp * 100)
wml.variables["recall_cost"] = ut.recall_cost
wml.variables["unit_worth"] = math.floor(math.max(ut.cost * hp, best_adv * xp))
end

function wml_actions.lua(cfg)
Expand Down Expand Up @@ -373,7 +373,7 @@ end

function wml_actions.store_turns(cfg)
local var = cfg.variable or "turns"
wesnoth.set_variable(var, wesnoth.game_config.last_turn)
wml.variables[var] = wesnoth.game_config.last_turn
end

function wml_actions.store_unit(cfg)
Expand Down Expand Up @@ -948,5 +948,5 @@ function wesnoth.wml_actions.store_unit_defense(cfg)
else
defense = wesnoth.unit_defense(unit, wesnoth.get_terrain(unit.x, unit.y))
end
wesnoth.set_variable(cfg.variable or "terrain_defense", defense)
wml.variables[cfg.variable or "terrain_defense"] = defense
end
12 changes: 6 additions & 6 deletions data/lua/wml-utils.lua
Expand Up @@ -32,7 +32,7 @@ function utils.vwriter.init(cfg, default_variable)
elseif mode == "append" then
index = wml.variables[variable .. ".length"]
elseif mode ~= "replace" then
wesnoth.set_variable(variable)
wml.variables[variable] = nil
end
return {
variable = variable,
Expand All @@ -43,9 +43,9 @@ end

function utils.vwriter.write(self, container)
if self.is_explicit_index then
wesnoth.set_variable(self.variable, container)
wml.variables[self.variable] = container
else
wesnoth.set_variable(string.format("%s[%u]", self.variable, self.index), container)
wml.variables[string.format("%s[%u]", self.variable, self.index)] = container
end
self.index = self.index + 1
end
Expand Down Expand Up @@ -194,16 +194,16 @@ end
function utils.start_var_scope(name)
local var = wml.array_access.get(name) --containers and arrays
if #var == 0 then var = wml.variables[name] end --scalars (and nil/empty)
wesnoth.set_variable(name)
wml.variables[name] = nil
return var
end
function utils.end_var_scope(name, var)
wesnoth.set_variable(name)
wml.variables[name] = nil
if type(var) == "table" then
wml.array_access.set(name, var)
else
wesnoth.set_variable(name, var)
wml.variables[name] = var
end
end
Expand Down
24 changes: 11 additions & 13 deletions data/lua/wml/find_path.lua
Expand Up @@ -9,8 +9,8 @@ function wesnoth.wml_actions.find_path(cfg)
-- support for $this_unit
local this_unit = utils.start_var_scope("this_unit")

wesnoth.set_variable ( "this_unit" ) -- clearing this_unit
wesnoth.set_variable("this_unit", unit.__cfg) -- cfg field needed
wml.variables["this_unit"] = nil -- clearing this_unit
wml.variables["this_unit"] = unit.__cfg -- cfg field needed

local variable = cfg.variable or "path"
local ignore_units = false
Expand Down Expand Up @@ -78,28 +78,27 @@ function wesnoth.wml_actions.find_path(cfg)
end

if cost >= 42424242 then -- it's the high value returned for unwalkable or busy terrains
wesnoth.set_variable ( tostring(variable), { hexes = 0 } ) -- set only length, nil all other values
wml.variables[tostring(variable)] = { hexes = 0 } -- set only length, nil all other values
-- support for $this_unit
wesnoth.set_variable ( "this_unit" ) -- clearing this_unit
wml.variables["this_unit"] = nil -- clearing this_unit
utils.end_var_scope("this_unit", this_unit)
return end

if not allow_multiple_turns and turns > 1 then -- location cannot be reached in one turn
wesnoth.set_variable ( tostring(variable), { hexes = 0 } )
wml.variables[tostring(variable)] = { hexes = 0 }
-- support for $this_unit
wesnoth.set_variable ( "this_unit" ) -- clearing this_unit
wml.variables["this_unit"] = nil -- clearing this_unit
utils.end_var_scope("this_unit", this_unit)
return end -- skip the cycles below

wesnoth.set_variable (
tostring( variable ),
wml.variables[tostring( variable )] =
{
hexes = current_distance,
from_x = unit.x, from_y = unit.y,
to_x = current_location[1], to_y = current_location[2],
movement_cost = cost,
required_turns = turns
} )
}

for index, path_loc in ipairs(path) do
local sub_path, sub_cost = wesnoth.find_path(
Expand All @@ -120,18 +119,17 @@ function wesnoth.wml_actions.find_path(cfg)
sub_turns = math.ceil( ( ( sub_cost - unit.moves ) / unit.max_moves ) + 1 )
end

wesnoth.set_variable (
string.format( "%s.step[%d]", variable, index - 1 ),
wml.variables[string.format( "%s.step[%d]", variable, index - 1 )] =
{ -- this structure takes less space in the inspection window
x = path_loc[1], y = path_loc[2],
terrain = wesnoth.get_terrain( path_loc[1], path_loc[2] ),
movement_cost = sub_cost,
required_turns = sub_turns
} )
}
end
end

-- support for $this_unit
wesnoth.set_variable ( "this_unit" ) -- clearing this_unit
wml.variables["this_unit"] = nil -- clearing this_unit
utils.end_var_scope("this_unit", this_unit)
end
8 changes: 4 additions & 4 deletions data/lua/wml/harm_unit.lua
Expand Up @@ -24,8 +24,8 @@ function wml_actions.harm_unit(cfg)
for index, unit_to_harm in ipairs(wesnoth.get_units(filter)) do
if unit_to_harm.valid then
-- block to support $this_unit
wesnoth.set_variable ( "this_unit" ) -- clearing this_unit
wesnoth.set_variable("this_unit", unit_to_harm.__cfg) -- cfg field needed
wml.variables["this_unit"] = nil -- clearing this_unit
wml.variables["this_unit"] = unit_to_harm.__cfg -- cfg field needed
local amount = tonumber(cfg.amount)
local animate = cfg.animate -- attacker and defender are special values
local delay = cfg.delay or 500
Expand Down Expand Up @@ -187,7 +187,7 @@ function wml_actions.harm_unit(cfg)
end

if variable then
wesnoth.set_variable(string.format("%s[%d]", variable, index - 1), { harm_amount = damage })
wml.variables[string.format("%s[%d]", variable, index - 1)] = { harm_amount = damage }
end

-- both units may no longer be alive at this point, so double check
Expand All @@ -203,6 +203,6 @@ function wml_actions.harm_unit(cfg)
wml_actions.redraw {}
end

wesnoth.set_variable ( "this_unit" ) -- clearing this_unit
wml.variables["this_unit"] = nil -- clearing this_unit
utils.end_var_scope("this_unit", this_unit)
end
2 changes: 1 addition & 1 deletion data/lua/wml/heal_unit.lua
Expand Up @@ -67,7 +67,7 @@ function wesnoth.wml_actions.heal_unit(cfg)

if not heal_amount_set then
heal_amount_set = true
wesnoth.set_variable("heal_amount", heal_amount)
wml.variables["heal_amount"] = heal_amount
end
end
end
6 changes: 3 additions & 3 deletions data/lua/wml/items.lua
Expand Up @@ -75,7 +75,7 @@ function wml_actions.item(cfg)
local redraw = cfg.redraw
if redraw == nil then redraw = true end
if redraw then wml_actions.redraw {} end
if cfg.write_name then wesnoth.set_variable(cfg.write_name, cfg.name) end
if cfg.write_name then wml.variables[cfg.write_name] = cfg.name end
return cfg.name
end

Expand All @@ -89,13 +89,13 @@ end
function wml_actions.store_items(cfg)
local variable = cfg.variable or "items"
variable = tostring(variable or helper.wml_error("invalid variable= in [store_items]"))
wesnoth.set_variable(variable)
wml.variables[variable] = nil
local index = 0
for i, loc in ipairs(wesnoth.get_locations(cfg)) do
local items = scenario_items[loc[1] * 10000 + loc[2]]
if items then
for j, item in ipairs(items) do
wesnoth.set_variable(string.format("%s[%u]", variable, index), item)
wml.variables[string.format("%s[%u]", variable, index)] = item
index = index + 1
end
end
Expand Down
6 changes: 3 additions & 3 deletions data/lua/wml/message.lua
Expand Up @@ -441,7 +441,7 @@ function wesnoth.wml_actions.message(cfg)

if text_input ~= nil then
-- Implement the consequences of the choice
wesnoth.set_variable(text_input.variable or "input", choice.text)
wml.variables[text_input.variable or "input"] = choice.text
end
end

Expand All @@ -459,9 +459,9 @@ function wesnoth.wml_actions.message(cfg)

if cfg.variable ~= nil then
if options[option_chosen].value == nil then
wesnoth.set_variable(cfg.variable, option_chosen)
wml.variables[cfg.variable] = option_chosen
else
wesnoth.set_variable(cfg.variable, options[option_chosen].value)
wml.variables[cfg.variable] = options[option_chosen].value
end
end

Expand Down

0 comments on commit ce7faae

Please sign in to comment.