Coward Micro AI: add optional attack_if_trapped key

If set, the unit attacks the weakest enemy next to which it ends up at
the end of its move.
This commit is contained in:
mattsc 2014-06-15 07:21:28 -07:00
parent 38dd3f6fed
commit 9645fc466b
2 changed files with 21 additions and 1 deletions

View file

@ -83,6 +83,26 @@ function ca_coward:execution(ai, cfg)
AH.movefull_stopunit(ai, coward, best_hex[1], best_hex[2])
if (not coward) or (not coward.valid) then return end
-- If 'attack_if_trapped' is set, the coward attacks the weakest unit it ends up next to
if cfg.attack_if_trapped then
local max_rating, best_target = -9e99
for xa,ya in H.adjacent_tiles(coward.x, coward.y) do
local target = wesnoth.get_unit(xa, ya)
if target and wesnoth.is_enemy(coward.side, target.side) then
local rating = - target.hitpoints
if (rating > max_rating) then
max_rating, best_target = rating, target
end
end
end
if best_target then
AH.checked_attack(ai, coward, best_target)
if (not coward) or (not coward.valid) then return end
end
end
AH.checked_stopunit_all(ai, coward)
end

View file

@ -234,7 +234,7 @@ function wesnoth.wml_actions.micro_ai(cfg)
H.wml_error("Coward [micro_ai] tag requires either id= key or [filter] tag")
end
required_keys = { "distance" }
optional_keys = { "id", "filter", "filter_second", "seek_x", "seek_y","avoid_x","avoid_y" }
optional_keys = { "attack_if_trapped", "id", "filter", "filter_second", "seek_x", "seek_y","avoid_x","avoid_y" }
CA_parms = {
ai_id = 'mai_coward',
{ ca_id = 'move', location = CA_path .. 'ca_coward.lua', score = cfg.ca_score or 300000 }