Micro AIs other engines: code cleanup

This commit is contained in:
mattsc 2014-04-20 14:29:52 -07:00
parent ac89d4e1e9
commit f02857b0f1
2 changed files with 20 additions and 30 deletions

View file

@ -1,52 +1,44 @@
return {
init = function(ai)
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
-- Set 'attacks' aspect so that only unit with id=@target_id
-- is attacked if it can be reached, delete aspect otherwise
-- The following can be simplified significantly once the 'attacks' variable is available
-- All units that have attacks left (but are not leaders)
local attackers = wesnoth.get_units{side = wesnoth.current.side, canrecruit = "no", formula = "$this_unit.attacks_left > 0"}
--print("\nAttackers:",#attackers)
-- This gets set to >0 if unit that can attack target is found
local target_in_reach
-- See if any of those units can reach our target(s)
for i,u in ipairs(attackers) do
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 = u.id } },
{ "filter", { id = attacker.id } },
{ "filter_location", {
{ "filter_adjacent_location", {
{ "filter", { id = target_id } }
} },
{ "not", {
{ "filter", { { "not", { id = u.id } } } }
{ "filter", { { "not", { id = attacker.id } } } }
} }
} },
moves = "current",
variable = "tmp_locs"
}
local tir = H.get_variable_array("tmp_locs")
attack_locs = H.get_variable_array("tmp_locs")
W.clear_variable { name = "tmp_locs" }
--print("reachable locs:",u.id,#tir)
-- If unit can reach a target -> set variable to 1
if (#tir > 0) then target_in_reach = true end
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
--print("Deleting attacks aspect")
W.modify_ai {
side = wesnoth.current.side,
action = "try_delete",
@ -65,9 +57,8 @@ return {
path = "aspect[caution].facet[*]"
}
-- If the target is in reach, set the 'attacks' aspect accordingly ...
if target_in_reach then
--print("Setting attacks aspect")
-- If the target can be attacked, set the attacks aspect accordingly
if attack_locs[1] then
W.modify_ai {
side = wesnoth.current.side,
action = "add",
@ -80,8 +71,8 @@ return {
} }
}
-- We also want to set
-- 'aggression=1' and 'caution=0', otherwise there could be turns on which nothing happens
-- We also want to set aggression=1 and caution=0,
-- otherwise there could be turns on which nothing happens
W.modify_side {
side = wesnoth.current.side,
{ "ai", { aggression = 1, caution = 0 } }

View file

@ -1,17 +1,16 @@
return {
init = function(ai)
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local urudin = { }
local urudin = {}
-- This is taken almost literally from 'Ka'lian under Attack' in 'Legend of Wesmere'
function urudin:retreat()
local urudin = wesnoth.get_units({side = 3, id="Urudin"})[1]
local urudin = wesnoth.get_units({ side = 3, id = "Urudin" })[1]
if urudin and urudin.valid then
local mhp, hp = urudin.max_hitpoints, urudin.hitpoints
local max_hp, hp = urudin.max_hitpoints, urudin.hitpoints
local turn = wesnoth.current.turn
if (turn >= 5) or (hp < mhp / 2) then
if (turn >= 5) or (hp < max_hp / 2) then
AH.movefull_stopunit(ai, urudin, 33, 8)
end
end