Skip to content

Commit

Permalink
Coward Micro AI: add optional attack_if_trapped key
Browse files Browse the repository at this point in the history
If set, the unit attacks the weakest enemy next to which it ends up at
the end of its move.
  • Loading branch information
mattsc committed Jun 15, 2014
1 parent 38dd3f6 commit 9645fc4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions data/ai/micro_ais/cas/ca_coward.lua
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/micro_ai_wml_tag.lua
Expand Up @@ -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 }
Expand Down

0 comments on commit 9645fc4

Please sign in to comment.