1return {
2    init = function()
3        local priority_target = {}
4
5        local H = wesnoth.require "helper"
6        local W = H.set_wml_action_metatable {}
7        local AH = wesnoth.require "ai/lua/ai_helper.lua"
8
9        function priority_target:change_attacks_aspect(target_id)
10            -- Set 'attacks' aspect so that only unit with id=@target_id
11            -- is attacked if it can be reached, delete aspect otherwise
12
13            local attackers = AH.get_units_with_attacks {
14                side = wesnoth.current.side,
15                canrecruit = "no"
16            }
17
18            local attack_locs = {}
19            for _,attacker in ipairs(attackers) do
20                -- Need to find reachable hexes that are
21                -- 1. next to target
22                -- 2. not occupied by an allied unit (except for unit itself)
23                W.store_reachable_locations {
24                    { "filter", { id = attacker.id } },
25                    { "filter_location", {
26                        { "filter_adjacent_location", {
27                            { "filter", { id = target_id } }
28                        } },
29                        { "not", {
30                            { "filter", { { "not", { id = attacker.id } } } }
31                        } }
32                    } },
33                    moves = "current",
34                    variable = "tmp_locs"
35                }
36                attack_locs = wml.array_access.get("tmp_locs")
37                W.clear_variable { name = "tmp_locs" }
38                if (#attack_locs > 0) then break end
39            end
40
41            -- Always delete the attacks aspect first, so that we do not end up with 100 copies of the facet
42            wesnoth.delete_ai_component(wesnoth.current.side, "aspect[attacks].facet[limited_attack]")
43
44            -- Also delete aggression, caution - for the same reason
45            wesnoth.delete_ai_component(wesnoth.current.side, "aspect[aggression].facet[*]")
46            wesnoth.delete_ai_component(wesnoth.current.side, "aspect[caution].facet[*]")
47
48            -- If the target can be attacked, set the attacks aspect accordingly
49            if attack_locs[1] then
50                wesnoth.add_ai_component(wesnoth.current.side, "aspect[attacks].facet",
51                    {
52                       name = "ai_default_rca::aspect_attacks",
53                       id = "limited_attack",
54                       invalidate_on_gamestate_change = "yes",
55                       { "filter_enemy", { id = target_id } }
56                    }
57                )
58
59                -- We also want to set aggression=1 and caution=0,
60                -- otherwise there could be turns on which nothing happens
61                wesnoth.append_ai(wesnoth.current.side, { aggression = 1, caution = 0 })
62            end
63
64            return 0
65        end
66
67        return priority_target
68    end
69}
70