ai_helper.get_attacks: improve check for units in the way
Check whether the unit in the way has an unoccupied hex to move to. Previously, it was only checked whether its reach was >1, which sometimes can include only hexes occupied by units on its own side, making the actual move impossible.
This commit is contained in:
parent
0e79ef919f
commit
130389acdf
1 changed files with 13 additions and 5 deletions
|
@ -1243,10 +1243,9 @@ function ai_helper.get_attacks(units, cfg)
|
||||||
-- If another unit of same side is on this hex:
|
-- If another unit of same side is on this hex:
|
||||||
if my_unit_map:get(loc[1], loc[2]) and ((loc[1] ~= unit.x) or (loc[2] ~= unit.y)) then
|
if my_unit_map:get(loc[1], loc[2]) and ((loc[1] ~= unit.x) or (loc[2] ~= unit.y)) then
|
||||||
attack_hex_occupied = true
|
attack_hex_occupied = true
|
||||||
|
|
||||||
if (not cfg.include_occupied) then
|
|
||||||
add_target = false
|
add_target = false
|
||||||
else -- test whether it can move out of the way
|
|
||||||
|
if cfg.include_occupied then -- Test whether it can move out of the way
|
||||||
local unit_in_way = all_units[my_unit_map:get(loc[1], loc[2])]
|
local unit_in_way = all_units[my_unit_map:get(loc[1], loc[2])]
|
||||||
local uiw_reach
|
local uiw_reach
|
||||||
if reaches:get(unit_in_way.x, unit_in_way.y) then
|
if reaches:get(unit_in_way.x, unit_in_way.y) then
|
||||||
|
@ -1256,8 +1255,17 @@ function ai_helper.get_attacks(units, cfg)
|
||||||
reaches:insert(unit_in_way.x, unit_in_way.y, uiw_reach)
|
reaches:insert(unit_in_way.x, unit_in_way.y, uiw_reach)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Units that cannot move away have only one hex in uiw_reach
|
-- Check whether the unit to move out of the way has an unoccupied hex to move to.
|
||||||
if (#uiw_reach <= 1) then add_target = false end
|
-- We do not deal with cases where a unit can move out of the way for a
|
||||||
|
-- unit that is moving out of the way of the initial unit (etc.).
|
||||||
|
for _,uiw_loc in ipairs(uiw_reach) do
|
||||||
|
-- Unit in the way of the unit in the way
|
||||||
|
local uiw_uiw = wesnoth.get_unit(uiw_loc[1], uiw_loc[2])
|
||||||
|
if (not uiw_uiw) then
|
||||||
|
add_target = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue