Lua: crash peacefully with [move_unit]

If there is an issue with to_x or to_y, such as a missmatching number of values, or none,
then the function is aborted by the 2nd/3rd wml.error statement.

At that place, current_unit:extract() was already called, so the function
would not only fail, but also remove the unit.

This commit unstores the unit prior to aborting.
The alternative of extracting the unit later would break the tests,
as it is then still using the hex, which is then not available to pathfinding.
This commit is contained in:
Severin Glöckner 2020-09-20 20:49:48 +02:00 committed by Pentarctagon
parent a748126b75
commit 640c08b7f5

View file

@ -83,8 +83,8 @@ function wesnoth.wml_actions.move_unit(cfg)
local x, y = locs(current_unit)
local prevX, prevY = tonumber(current_unit.x), tonumber(current_unit.y)
while true do
x = tonumber(x) or wml.error(coordinate_error)
y = tonumber(y) or wml.error(coordinate_error)
x = tonumber(x) or current_unit:to_map(false) or wml.error(coordinate_error)
y = tonumber(y) or current_unit:to_map(false) or wml.error(coordinate_error)
if not (x == prevX and y == prevY) then x, y = wesnoth.paths.find_vacant_hex(x, y, pass_check) end
if not x or not y then wml.error("Could not find a suitable hex near to one of the target hexes in [move_unit].") end
table.insert(x_list, x)