mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-07 16:00:04 +00:00

Previously it calculated max movement, and then added the adjacent hexes. This version should correctly handle: * units with vp different to max mp * units with vision costs different to movement costs * jamming by enemy units Draft changelog entry for this: * `[store_reachable_locations]range=vision` now calculates vision, instead of using movement costs and max movement points (issue #4179)
57 lines
1.7 KiB
Lua
57 lines
1.7 KiB
Lua
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
|
|
|
|
local reach = location_set.create()
|
|
|
|
if range == "vision" then
|
|
for i,unit in ipairs(wesnoth.units.find_on_map(unit_filter)) do
|
|
local unit_reach = location_set.of_pairs(wesnoth.find_vision_range(unit))
|
|
reach:union(unit_reach)
|
|
end
|
|
else
|
|
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 == "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
|
|
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
|