From 0c4d8ebbe6a551d5803bf5d998bdd87f9f4b7191 Mon Sep 17 00:00:00 2001 From: mattsc Date: Sun, 28 Feb 2021 14:57:57 -0800 Subject: [PATCH] ai_helper.lua: add 'avoid_map' functionality to move_unit_out_of_way() Also add comment about passing it through from robust_move_and_attack(). --- data/ai/lua/ai_helper.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/data/ai/lua/ai_helper.lua b/data/ai/lua/ai_helper.lua index 02f615c47e8..06e99a34989 100644 --- a/data/ai/lua/ai_helper.lua +++ b/data/ai/lua/ai_helper.lua @@ -295,6 +295,9 @@ function ai_helper.robust_move_and_attack(ai, src, dst, target_loc, cfg) -- parameter is true, a partial move is done instead. -- weapon: The number (starting at 1) of the attack weapon to be used. -- If omitted, the best weapon is automatically selected. + -- avoid_map (location set): if given, the hexes in avoid_map are excluded + -- This is only used for passing through to move_unit_out_of_way. It is assumed + -- that @dst has been checked to lie outside areas to avoid. -- all optional parameters for ai_helper.move_unit_out_of_way() -- Notes: @@ -1967,6 +1970,7 @@ function ai_helper.move_unit_out_of_way(ai, unit, cfg) -- Main rating is the moves the unit still has left after that -- Other, configurable, parameters are given to function in @cfg: -- dx, dy: the direction in which moving out of the way is preferred + -- avoid_map (location set): if given, the hexes in avoid_map are excluded -- labels: if set, display labels of the rating for each hex the unit can reach -- viewing_side: see comments at beginning of this file. Defaults to side of @unit. -- ignore_visibility: see comments at beginning of this file. Defaults to nil. @@ -1992,17 +1996,20 @@ function ai_helper.move_unit_out_of_way(ai, unit, cfg) if (not unit_in_way) -- also excludes current hex or ((not ignore_visibility) and (not ai_helper.is_visible_unit(viewing_side, unit_in_way))) then - local rating = loc[3] -- also disfavors hexes next to visible enemy units for which loc[3] = 0 + local avoid_this_hex = cfg and cfg.avoid_map and cfg.avoid_map:get(loc[1], loc[2]) + if (not avoid_this_hex) then + local rating = loc[3] -- also disfavors hexes next to visible enemy units for which loc[3] = 0 - if dx then - rating = rating + (loc[1] - unit.x) * dx * 0.01 - rating = rating + (loc[2] - unit.y) * dy * 0.01 - end + if dx then + rating = rating + (loc[1] - unit.x) * dx * 0.01 + rating = rating + (loc[2] - unit.y) * dy * 0.01 + end - if cfg.labels then reach_map:insert(loc[1], loc[2], rating) end + if cfg.labels then reach_map:insert(loc[1], loc[2], rating) end - if (rating > max_rating) then - max_rating, best_hex = rating, { loc[1], loc[2] } + if (rating > max_rating) then + max_rating, best_hex = rating, { loc[1], loc[2] } + end end end end