Move lua implementation of [store_reachable_locations] to its own file
This commit is contained in:
parent
508bc2519e
commit
147cd90527
2 changed files with 53 additions and 52 deletions
|
@ -1,5 +1,4 @@
|
||||||
local helper = wesnoth.require "helper"
|
local helper = wesnoth.require "helper"
|
||||||
local location_set = wesnoth.require "location_set"
|
|
||||||
local utils = wesnoth.require "wml-utils"
|
local utils = wesnoth.require "wml-utils"
|
||||||
local wml_actions = wesnoth.wml_actions
|
local wml_actions = wesnoth.wml_actions
|
||||||
local T = wml.tag
|
local T = wml.tag
|
||||||
|
@ -430,57 +429,6 @@ function wml_actions.store_locations(cfg)
|
||||||
end
|
end
|
||||||
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)
|
function wml_actions.hide_unit(cfg)
|
||||||
for i,u in ipairs(wesnoth.units.find_on_map(cfg)) do
|
for i,u in ipairs(wesnoth.units.find_on_map(cfg)) do
|
||||||
u.hidden = true
|
u.hidden = true
|
||||||
|
|
53
data/lua/wml/store_reachable_locations.lua
Normal file
53
data/lua/wml/store_reachable_locations.lua
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue