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