From 9645fc466b5d603c0bb2b59b7105a7df461c4608 Mon Sep 17 00:00:00 2001 From: mattsc Date: Sun, 15 Jun 2014 07:21:28 -0700 Subject: [PATCH] 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. --- data/ai/micro_ais/cas/ca_coward.lua | 20 ++++++++++++++++++++ data/ai/micro_ais/micro_ai_wml_tag.lua | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/data/ai/micro_ais/cas/ca_coward.lua b/data/ai/micro_ais/cas/ca_coward.lua index 3a98f8ff2bd..499d865988b 100644 --- a/data/ai/micro_ais/cas/ca_coward.lua +++ b/data/ai/micro_ais/cas/ca_coward.lua @@ -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 diff --git a/data/ai/micro_ais/micro_ai_wml_tag.lua b/data/ai/micro_ais/micro_ai_wml_tag.lua index 4b1203dfb39..98a1ad8af0d 100644 --- a/data/ai/micro_ais/micro_ai_wml_tag.lua +++ b/data/ai/micro_ais/micro_ai_wml_tag.lua @@ -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 }