Skip to content

Commit

Permalink
ai_helper.get_attacks: improve check for units in the way
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattsc committed Jun 15, 2014
1 parent 0e79ef9 commit 130389a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions data/ai/lua/ai_helper.lua
Expand Up @@ -1243,10 +1243,9 @@ function ai_helper.get_attacks(units, cfg)
-- 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
attack_hex_occupied = true
add_target = false

if (not cfg.include_occupied) then
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 uiw_reach
if reaches:get(unit_in_way.x, unit_in_way.y) then
Expand All @@ -1256,8 +1255,17 @@ function ai_helper.get_attacks(units, cfg)
reaches:insert(unit_in_way.x, unit_in_way.y, uiw_reach)
end

-- Units that cannot move away have only one hex in uiw_reach
if (#uiw_reach <= 1) then add_target = false end
-- Check whether the unit to move out of the way has an unoccupied hex to move to.
-- 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

Expand Down

0 comments on commit 130389a

Please sign in to comment.