From f1e880733088512392d2490e838e04d7b3be02d4 Mon Sep 17 00:00:00 2001 From: mattsc Date: Wed, 18 Dec 2019 07:40:16 -0800 Subject: [PATCH] ai_helper.has_ability: add optional parameter 'exact_match' The default behavior is unchanged. --- data/ai/lua/ai_helper.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/data/ai/lua/ai_helper.lua b/data/ai/lua/ai_helper.lua index 93820777f29..916bfd57241 100644 --- a/data/ai/lua/ai_helper.lua +++ b/data/ai/lua/ai_helper.lua @@ -1260,11 +1260,25 @@ function ai_helper.get_closest_enemy(loc, side, cfg) return closest_enemy, closest_distance end -function ai_helper.has_ability(unit, ability) +function ai_helper.has_ability(unit, ability, exact_match) -- Returns true/false depending on whether unit has the given ability + -- OPTIONAL INPUT: + -- - exact_match=true: (boolean) If set to true (the default), the ability id + -- has to match @ability exactly, otherwise it is sufficient if @ability appears + -- in the id. This is done so that, for example, regeneration abilities with + -- ids 'regenerates' and 'regenerates_4' can be matched simultaneously. + + if (exact_match == nil) then exact_match = true end + for _,ability_id in ipairs(unit.abilities) do - if (ability == ability_id) then - return true + if exact_match then + if (ability == ability_id) then + return true + end + else + if string.find(ability_id, ability) then + return true + end end end return false