Skip to content

Commit

Permalink
Micro AIs: fix some MAIs not working under shroud
Browse files Browse the repository at this point in the history
This is done by ignoring shroud for path finding (while still taking
hidden units into account correctly), consistent with default Wesnoth
AI behavior.
  • Loading branch information
mattsc committed Dec 10, 2016
1 parent 36b2b4e commit 3236a4b
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_assassin_move.lua
Expand Up @@ -149,7 +149,7 @@ function ca_assassin_move:execution(cfg)
-- We need to pick the farthest reachable hex along that path
local farthest_hex = path[1]
for i = 2,#path do
local sub_path, sub_cost = wesnoth.find_path(unit, path[i][1], path[i][2])
local sub_path, sub_cost = AH.find_path_with_shroud(unit, path[i][1], path[i][2])
if sub_cost <= unit.moves then
local unit_in_way = wesnoth.get_unit(path[i][1], path[i][2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_bottleneck_move.lua
Expand Up @@ -26,7 +26,7 @@ local function bottleneck_is_my_territory(map, enemy_map)
-- Find lowest movement cost to own front-line hexes
local min_cost, best_path = 9e99
map:iter(function(xm, ym, v)
local path, cost = wesnoth.find_path(dummy_unit, xm, ym, { ignore_units = true })
local path, cost = AH.find_path_with_shroud(dummy_unit, xm, ym, { ignore_units = true })
if (cost < min_cost) then
min_cost, best_path = cost, path
end
Expand All @@ -35,7 +35,7 @@ local function bottleneck_is_my_territory(map, enemy_map)
-- And the same to the enemy front line
local min_cost_enemy, best_path_enemy = 9e99
enemy_map:iter(function(xm, ym, v)
local path, cost = wesnoth.find_path(dummy_unit, xm, ym, { ignore_units = true })
local path, cost = AH.find_path_with_shroud(dummy_unit, xm, ym, { ignore_units = true })
if (cost < min_cost_enemy) then
min_cost_enemy, best_path_enemy = cost, path
end
Expand Down
10 changes: 5 additions & 5 deletions data/ai/micro_ais/cas/ca_fast_move.lua
Expand Up @@ -147,8 +147,8 @@ function ca_fast_move:execution(cfg)
for _,unit_info in ipairs(goal) do
if (not unit_info.cost) then
local _,cost =
wesnoth.find_path(
units[unit_info.i_unit].x, units[unit_info.i_unit].y,
AH.find_path_with_shroud(
units[unit_info.i_unit],
goal.x, goal.y,
{ ignore_units = true }
)
Expand All @@ -172,13 +172,13 @@ function ca_fast_move:execution(cfg)

-- We now want the hex that is 2 steps beyond the next hop for the unit
-- on its way toward the goal, ignoring any unit along the way
local path = wesnoth.find_path(unit, goal.x, goal.y, { ignore_units = true })
local path = AH.find_path_with_shroud(unit, goal.x, goal.y, { ignore_units = true })

-- Use current unit position as default
local short_goal, index = { unit.x, unit.y }, 1

for i = 2,#path do
local _, sub_cost = wesnoth.find_path(unit, path[i][1], path[i][2], { ignore_units = true })
local _, sub_cost = AH.find_path_with_shroud(unit, path[i][1], path[i][2], { ignore_units = true })

if (sub_cost <= unit.moves) then
short_goal, index = path[i], i
Expand Down Expand Up @@ -254,7 +254,7 @@ function ca_fast_move:execution(cfg)
if (pre_rating.rating <= max_rating) then break end

unit.x, unit.y = pre_rating.x, pre_rating.y
local _,cost = wesnoth.find_path(unit, short_goal[1], short_goal[2])
local _,cost = AH.find_path_with_shroud(unit, short_goal[1], short_goal[2])

local rating = - cost + pre_rating.other_rating

Expand Down
4 changes: 4 additions & 0 deletions data/ai/micro_ais/cas/ca_forest_animals_new_rabbit.lua
Expand Up @@ -65,6 +65,10 @@ function ca_forest_animals_new_rabbit:execution(cfg)
.. "' }, x1, y1)"
ai.synced_command(command, x, y)
end

if wesnoth.sides[wesnoth.current.side].shroud then
wesnoth.wml_actions.redraw { side = wesnoth.current.side }
end
end

return ca_forest_animals_new_rabbit
6 changes: 3 additions & 3 deletions data/ai/micro_ais/cas/ca_goto.lua
Expand Up @@ -152,7 +152,7 @@ function ca_goto:execution(cfg, data)
enemy_at_goal = nil
end
end
path, cost = wesnoth.find_path(unit, loc[1], loc[2], { ignore_units = cfg.ignore_units })
path, cost = AH.find_path_with_shroud(unit, loc[1], loc[2], { ignore_units = cfg.ignore_units })
if enemy_at_goal then
wesnoth.put_unit(enemy_at_goal)
--- Give massive penalty for this goal hex
Expand Down Expand Up @@ -193,13 +193,13 @@ function ca_goto:execution(cfg, data)
-- rather than using ai_helper.next_hop for standard pathfinding
-- Also, straight-line does not produce a path, so we do that first
if not best_path then
best_path = wesnoth.find_path(best_unit, closest_hex[1], closest_hex[2])
best_path = AH.find_path_with_shroud(best_unit, closest_hex[1], closest_hex[2])
end

-- Now go through the hexes along that path, use normal path finding
closest_hex = best_path[1]
for i = 2,#best_path do
local sub_path, sub_cost = wesnoth.find_path(best_unit, best_path[i][1], best_path[i][2], cfg)
local sub_path, sub_cost = AH.find_path_with_shroud(best_unit, best_path[i][1], best_path[i][2], cfg)
if sub_cost <= best_unit.moves then
local unit_in_way = wesnoth.get_unit(best_path[i][1], best_path[i][2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_messenger_attack.lua
Expand Up @@ -10,7 +10,7 @@ local function messenger_find_enemies_in_way(messenger, goal_x, goal_y)
-- @goal_x,@goal_y: coordinates of the goal toward which the messenger moves
-- Returns proxy table for the first unit found, or nil if none was found

local path, cost = wesnoth.find_path(messenger, goal_x, goal_y, { ignore_units = true })
local path, cost = AH.find_path_with_shroud(messenger, goal_x, goal_y, { ignore_units = true })
if cost >= 42424242 then return end

-- The second path hex is the first that is important for the following analysis
Expand All @@ -23,7 +23,7 @@ local function messenger_find_enemies_in_way(messenger, goal_x, goal_y)

-- After that, go through adjacent hexes of all the other path hexes
for i = 2,#path do
local sub_path, sub_cost = wesnoth.find_path(messenger, path[i][1], path[i][2], { ignore_units = true })
local sub_path, sub_cost = AH.find_path_with_shroud(messenger, path[i][1], path[i][2], { ignore_units = true })
if (sub_cost <= messenger.moves) then
for xa,ya in H.adjacent_tiles(path[i][1], path[i][2]) do
local enemy = wesnoth.get_unit(xa, ya)
Expand Down
8 changes: 4 additions & 4 deletions data/ai/micro_ais/cas/ca_messenger_move.lua
Expand Up @@ -30,10 +30,10 @@ function ca_messenger_move:execution(cfg)
if (not next_hop) then next_hop = { messenger.x, messenger.y } end

-- Compare this to the "ideal path"
local path = wesnoth.find_path(messenger, x, y, { ignore_units = 'yes' })
local path = AH.find_path_with_shroud(messenger, x, y, { ignore_units = 'yes' })
local optimum_hop, optimum_cost = { messenger.x, messenger.y }, 0
for _,step in ipairs(path) do
local sub_path, sub_cost = wesnoth.find_path(messenger, step[1], step[2])
local sub_path, sub_cost = AH.find_path_with_shroud(messenger, step[1], step[2])
if sub_cost > messenger.moves then
break
else
Expand Down Expand Up @@ -61,14 +61,14 @@ function ca_messenger_move:execution(cfg)
if unit_in_way then wesnoth.extract_unit(unit_in_way) end

wesnoth.put_unit(messenger, next_hop[1], next_hop[2])
local _, cost1 = wesnoth.find_path(messenger, x, y, { ignore_units = 'yes' })
local _, cost1 = AH.find_path_with_shroud(messenger, x, y, { ignore_units = 'yes' })

local unit_in_way2 = wesnoth.get_unit(optimum_hop[1], optimum_hop[2])
if (unit_in_way2 == messenger) then unit_in_way2 = nil end
if unit_in_way2 then wesnoth.extract_unit(unit_in_way2) end

wesnoth.put_unit(messenger, optimum_hop[1], optimum_hop[2])
local _, cost2 = wesnoth.find_path(messenger, x, y, { ignore_units = 'yes' })
local _, cost2 = AH.find_path_with_shroud(messenger, x, y, { ignore_units = 'yes' })

wesnoth.put_unit(messenger, x_current, y_current)
if unit_in_way then wesnoth.put_unit(unit_in_way) end
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_protect_unit_finish.lua
Expand Up @@ -8,7 +8,7 @@ function ca_protect_unit_finish:evaluation(cfg)
for u in H.child_range(cfg, "unit") do
local unit = AH.get_units_with_moves { id = u.id }[1]
if unit then
local path, cost = wesnoth.find_path(unit, u.goal_x, u.goal_y)
local path, cost = AH.find_path_with_shroud(unit, u.goal_x, u.goal_y)
if (cost <= unit.moves) and ((unit.x ~= u.goal_x) or (unit.y ~= u.goal_y)) then
PU_unit = unit
PU_goal = { u.goal_x, u.goal_y }
Expand Down

0 comments on commit 3236a4b

Please sign in to comment.