diff --git a/data/lua/wml-tags.lua b/data/lua/wml-tags.lua index 6c234e13607..8cb066fab70 100644 --- a/data/lua/wml-tags.lua +++ b/data/lua/wml-tags.lua @@ -1,5 +1,4 @@ local helper = wesnoth.require "helper" -local location_set = wesnoth.require "location_set" local utils = wesnoth.require "wml-utils" local wml_actions = wesnoth.wml_actions local T = wml.tag @@ -430,57 +429,6 @@ function wml_actions.store_locations(cfg) end end -function wml_actions.store_reachable_locations(cfg) - local unit_filter = wml.get_child(cfg, "filter") or - wml.error "[store_reachable_locations] missing required [filter] tag" - local location_filter = wml.get_child(cfg, "filter_location") - local range = cfg.range or "movement" - local moves = cfg.moves or "current" - local variable = cfg.variable or wml.error "[store_reachable_locations] missing required variable= key" - local reach_param = { viewing_side = cfg.viewing_side } - if cfg.viewing_side == 0 then - wml.error "[store_reachable_locations] invalid viewing_side" - elseif cfg.viewing_side == nil then - reach_param.ignore_visibility = true - end - if range == "vision" then - moves = "max" - reach_param.ignore_units = true - end - - local reach = location_set.create() - - for i,unit in ipairs(wesnoth.units.find_on_map(unit_filter)) do - local unit_reach - if moves == "max" then - local saved_moves = unit.moves - unit.moves = unit.max_moves - unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param)) - unit.moves = saved_moves - else - unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param)) - end - - if range == "vision" or range == "attack" then - unit_reach:iter(function(x, y) - reach:insert(x, y) - for u,v in helper.adjacent_tiles(x, y) do - reach:insert(u, v) - end - end) - else - reach:union(unit_reach) - end - end - - if location_filter then - reach = reach:filter(function(x, y) - return wesnoth.map.matches(x, y, location_filter) - end) - end - reach:to_wml_var(variable) -end - function wml_actions.hide_unit(cfg) for i,u in ipairs(wesnoth.units.find_on_map(cfg)) do u.hidden = true diff --git a/data/lua/wml/store_reachable_locations.lua b/data/lua/wml/store_reachable_locations.lua new file mode 100644 index 00000000000..3a93af9a78d --- /dev/null +++ b/data/lua/wml/store_reachable_locations.lua @@ -0,0 +1,53 @@ +local helper = wesnoth.require "helper" +local location_set = wesnoth.require "location_set" + +function wesnoth.wml_actions.store_reachable_locations(cfg) + local unit_filter = wml.get_child(cfg, "filter") or + wml.error "[store_reachable_locations] missing required [filter] tag" + local location_filter = wml.get_child(cfg, "filter_location") + local range = cfg.range or "movement" + local moves = cfg.moves or "current" + local variable = cfg.variable or wml.error "[store_reachable_locations] missing required variable= key" + local reach_param = { viewing_side = cfg.viewing_side } + if cfg.viewing_side == 0 then + wml.error "[store_reachable_locations] invalid viewing_side" + elseif cfg.viewing_side == nil then + reach_param.ignore_visibility = true + end + if range == "vision" then + moves = "max" + reach_param.ignore_units = true + end + + local reach = location_set.create() + + for i,unit in ipairs(wesnoth.units.find_on_map(unit_filter)) do + local unit_reach + if moves == "max" then + local saved_moves = unit.moves + unit.moves = unit.max_moves + unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param)) + unit.moves = saved_moves + else + unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param)) + end + + if range == "vision" or range == "attack" then + unit_reach:iter(function(x, y) + reach:insert(x, y) + for u,v in helper.adjacent_tiles(x, y) do + reach:insert(u, v) + end + end) + else + reach:union(unit_reach) + end + end + + if location_filter then + reach = reach:filter(function(x, y) + return wesnoth.map.matches(x, y, location_filter) + end) + end + reach:to_wml_var(variable) +end