Messenger Escort Micro AI: code cleanup

This commit is contained in:
mattsc 2014-04-19 13:10:20 -07:00
parent eeb9ac86e5
commit 7b5b35ce7e
4 changed files with 77 additions and 120 deletions

View file

@ -3,15 +3,13 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local messenger_next_waypoint = wesnoth.require "ai/micro_ais/cas/ca_messenger_f_next_waypoint.lua"
local function messenger_find_enemies_in_way(unit, goal_x, goal_y)
local function messenger_find_enemies_in_way(messenger, goal_x, goal_y)
-- Returns the first unit on or next to the path of the messenger
-- unit: proxy table for the messenger unit
-- goal_x, goal_y: coordinates of the goal toward which the messenger moves
-- @messenger: proxy table for the messenger unit
-- @goal_x,@goal_y: coordinates of the goal toward which the messenger moves
-- Returns proxy table for the first unit found, or nil if none was found
local path, cost = wesnoth.find_path(unit, goal_x, goal_y, { ignore_units = true })
-- If unit cannot get there:
local path, cost = wesnoth.find_path(messenger, goal_x, goal_y, { ignore_units = true })
if cost >= 42424242 then return end
-- The second path hex is the first that is important for the following analysis
@ -20,119 +18,88 @@ local function messenger_find_enemies_in_way(unit, goal_x, goal_y)
-- Is there an enemy unit on the second path hex?
-- This would be caught by the adjacent hex check later, but not in the right order
local enemy = wesnoth.get_units { x = path[2][1], y = path[2][2],
{ "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } }
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } }
}[1]
if enemy then
--print(' enemy on second path hex:',enemy.id)
return enemy
end
if enemy then return enemy end
-- After that, go through adjacent hexes of all the other path hexes
for i = 2, #path do
local path_hex = path[i]
local sub_path, sub_cost = wesnoth.find_path( unit, path_hex[1], path_hex[2], { ignore_units = true })
if sub_cost <= unit.moves then
-- Check for enemy units on one of the adjacent hexes (which includes 2 hexes on path)
for x, y in H.adjacent_tiles(path_hex[1], path_hex[2]) do
local enemy = wesnoth.get_units { x = x, y = y,
{ "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } }
for i = 2,#path do
local sub_path, sub_cost = wesnoth.find_path(messenger, path[i][1], path[i][2], { ignore_units = true })
if (sub_cost <= messenger.moves) then
for xa,ya in H.adjacent_tiles(path[i][1], path[i][2]) do
local enemy = wesnoth.get_units { x = xa, y = ya,
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } }
}[1]
if enemy then
--print(' enemy next to path hex:',enemy.id)
return enemy
end
if enemy then return enemy end
end
else -- If we've reached the end of the path for this turn
return
end
end
-- If no unit was found, return nil
return
end
local function messenger_find_clearing_attack(unit, goal_x, goal_y)
local function messenger_find_clearing_attack(messenger, goal_x, goal_y)
-- Check if an enemy is in the way of the messenger
-- If so, find attack that would "clear" that enemy out of the way
-- unit: proxy table for the messenger unit
-- goal_x, goal_y: coordinates of the goal toward which the messenger moves
-- If so, find attack that would clear that enemy out of the way
-- @messenger: proxy table for the messenger unit
-- @goal_x,@goal_y: coordinates of the goal toward which the messenger moves
-- Returns proxy table containing the attack, or nil if none was found
local enemy_in_way = messenger_find_enemies_in_way(unit, goal_x, goal_y)
-- If none found, don't attack, just move
if not enemy_in_way then return end
local enemy_in_way = messenger_find_enemies_in_way(messenger, goal_x, goal_y)
if (not enemy_in_way) then return end
local max_rating, best_attack = -9e99, {}
--print('Finding attacks on',enemy_in_way.name,enemy_in_way.id)
-- Find all units that can attack this enemy
local units = AH.get_units_with_attacks {
side = wesnoth.current.side,
{ "not", { id = unit.id } }
{ "not", { id = messenger.id } }
}
if (not units[1]) then return end
local attacks = AH.get_attacks(units, { simulate_combat = true })
for i, att in ipairs(attacks) do
if (att.target.x == enemy_in_way.x) and (att.target.y == enemy_in_way.y) then
local max_rating, best_attack = -9e99
for _,attack in ipairs(attacks) do
if (attack.target.x == enemy_in_way.x) and (attack.target.y == enemy_in_way.y) then
-- Rating: expected HP of attacker and defender
local rating = att.att_stats.average_hp - 2 * att.def_stats.average_hp
--print(' rating:', rating)
local rating = attack.att_stats.average_hp - 2 * attack.def_stats.average_hp
if (rating > max_rating) then
max_rating = rating
best_attack = att
max_rating, best_attack = rating, attack
end
end
end
-- If attack on this enemy_in_way is possible, return it
if (max_rating > -9e99) then return best_attack end
if best_attack then return best_attack end
-- If we got here, that means there's an enemy in the way, but none of the units can reach it
--> try to fight our way to that enemy
--print('Find different attack to get to enemy in way')
for i, att in ipairs(attacks) do
for _,attack in ipairs(attacks) do
-- Rating: expected HP of attacker and defender
local rating = att.att_stats.average_hp - 2 * att.def_stats.average_hp
local rating = attack.att_stats.average_hp - 2 * attack.def_stats.average_hp
-- plus, give a huge bonus for closeness to enemy_in_way
local tmp_defender = wesnoth.get_unit(att.target.x, att.target.y)
-- Give a huge bonus for closeness to enemy_in_way
local tmp_defender = wesnoth.get_unit(attack.target.x, attack.target.y)
local dist = H.distance_between(enemy_in_way.x, enemy_in_way.y, tmp_defender.x, tmp_defender.y)
--print(' distance:',enemy_in_way.id, tmp_defender.id, dist)
rating = rating + 100. / dist
--print(' rating:', rating)
if (rating > max_rating) then
max_rating = rating
best_attack = att
max_rating, best_attack = rating, attack
end
end
if (max_rating > -9e99) then
return best_attack
else
return
end
if best_attack then return best_attack end
end
local ca_messenger_attack = {}
function ca_messenger_attack:evaluation(ai, cfg, self)
-- Attack units in the path of the messenger
-- id: id of the messenger unit
-- goal_x, goal_y: coordinates of the goal toward which the messenger moves
local messenger = wesnoth.get_units{ side = wesnoth.current.side, id = cfg.id }[1]
local messenger = wesnoth.get_units { side = wesnoth.current.side, id = cfg.id }[1]
if (not messenger) then return 0 end
local x, y = messenger_next_waypoint(messenger, cfg)
-- See if there's an enemy in the way that should be attacked
local attack = messenger_find_clearing_attack(messenger, x, y)
if attack then

View file

@ -10,16 +10,16 @@ local function get_escort(cfg)
end
local function get_messenger(cfg)
local messenger = wesnoth.get_units{ side = wesnoth.current.side, id = cfg.id }[1]
local messenger = wesnoth.get_units { side = wesnoth.current.side, id = cfg.id }[1]
return messenger
end
local ca_messenger_escort_move = {}
function ca_messenger_escort_move:evaluation(ai, cfg)
-- Move escort units close to messenger, and in between messenger and enemies
-- The messenger has moved at this time, so we don't need to exclude him
-- But we check that he exist (not for this scenario, but for others)
-- Move escort units close to messenger and in between messenger and enemies.
-- The messenger has moved at this time, so we don't need to exclude him,
-- but we check that he exist
if (not get_escort(cfg)) then return 0 end
if (not get_messenger(cfg)) then return 0 end
return cfg.ca_score
@ -29,36 +29,33 @@ function ca_messenger_escort_move:execution(ai, cfg)
local escort = get_escort(cfg)
local messenger = get_messenger(cfg)
-- Distance from messenger for each hex the unit can reach
local reach = LS.of_pairs(wesnoth.find_reach(escort))
local dist_messenger = AH.distance_map({messenger}, reach)
local dist_messenger = AH.distance_map({ messenger }, reach)
local enemies = wesnoth.get_units {
{ "filter_side", {{"enemy_of", {side = wesnoth.current.side} }} }
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } }
}
-- Rating (in the end, we pick the _minimum _rating):
-- Rating (in the end, we pick the _minimum_ rating):
-- 1. Minimize distance from enemies
local rating = AH.distance_map(enemies, reach)
-- 2. This one favors hexes in between messenger and enemies
rating:union_merge(dist_messenger, function(x, y, v1, v2)
return v1 + v2*#enemies
return v1 + v2 * #enemies
end)
-- 3. Strongly prefer hexes close to the messenger
rating:union_merge(dist_messenger, function(x, y, v1, v2)
return v1 + v2^2
end)
--AH.put_labels(rating)
-- Now find hex with minimum value that is unoccupied
min_rating, best_hex = 9e99, {}
min_rating, best_hex = 9e99
rating:iter(function(x, y, r)
local unit_in_way = wesnoth.get_units{ x = x, y = y, { "not", { id = escort.id } } }[1]
local unit_in_way = wesnoth.get_units { x = x, y = y, { "not", { id = escort.id } } }[1]
if (not unit_in_way) and (r < min_rating) then
min_rating, best_hex = r, { x, y }
end
end)
-- and move the unit there
AH.movefull_stopunit(ai, escort, best_hex)
end

View file

@ -8,13 +8,13 @@ return function(messenger, cfg)
local waypoint_x = AH.split(cfg.waypoint_x, ",")
local waypoint_y = AH.split(cfg.waypoint_y, ",")
for i,w in ipairs(waypoint_x) do
for i,_ in ipairs(waypoint_x) do
waypoint_x[i] = tonumber(waypoint_x[i])
waypoint_y[i] = tonumber(waypoint_y[i])
end
-- If we're within 3 hexes of the next waypoint, we go on to the one after that
-- except if that one's the last one already
-- If we're within 3 hexes of the next waypoint, we go on to the one
-- after that except if it's the last one
local dist_wp = H.distance_between(messenger.x, messenger.y, waypoint_x[wp], waypoint_y[wp])
if (dist_wp <= 3) and (wp < #waypoint_x) then wp = wp + 1 end

View file

@ -6,14 +6,11 @@ local messenger_next_waypoint = wesnoth.require "ai/micro_ais/cas/ca_messenger_f
local ca_messenger_move = {}
function ca_messenger_move:evaluation(ai, cfg)
-- Move the messenger (unit with passed id) toward goal, attack adjacent unit if possible
-- without retaliation or little expected damage with high chance of killing the enemy
-- Move the messenger toward goal, potentially attack adjacent unit
local messenger = AH.get_units_with_moves { id = cfg.id }[1]
if messenger then
return cfg.ca_score
end
if messenger then return cfg.ca_score end
return 0
end
@ -35,14 +32,14 @@ function ca_messenger_move:execution(ai, cfg)
if (not next_hop) then next_hop = { messenger.x, messenger.y } end
-- Compare this to the "ideal path"
local path, cost = wesnoth.find_path(messenger, x, y, { ignore_units = 'yes' })
local opt_hop, opt_cost = { messenger.x, messenger.y }, 0
for i, p in ipairs(path) do
local sub_path, sub_cost = wesnoth.find_path(messenger, p[1], p[2])
local path = wesnoth.find_path(messenger, x, y, { ignore_units = 'yes' })
local optimum_hop, optimum_cost = { messenger.x, messenger.y }, 0
for _,step in ipairs(path) do
local sub_path, sub_cost = wesnoth.find_path(messenger, step[1], step[2])
if sub_cost > messenger.moves then
break
else
local unit_in_way = wesnoth.get_unit(p[1], p[2])
local unit_in_way = wesnoth.get_unit(step[1], step[2])
if unit_in_way and (unit_in_way.side == messenger.side) then
local reach = AH.get_reachable_unocc(unit_in_way)
@ -50,38 +47,35 @@ function ca_messenger_move:execution(ai, cfg)
end
if not unit_in_way then
opt_hop, nh_cost = p, sub_cost
optimum_hop, nh_cost = step, sub_cost
end
end
end
--print(next_hop[1], next_hop[2], opt_hop[1], opt_hop[2])
-- Now compare how long it would take from the end of both of these options
local x1, y1 = messenger.x, messenger.y
local x_current, y_current = messenger.x, messenger.y
local unit_in_way = wesnoth.get_unit(next_hop[1], next_hop[2])
if (unit_in_way == messenger) then unit_in_way = nil end
if unit_in_way then wesnoth.extract_unit(unit_in_way) end
wesnoth.put_unit(next_hop[1], next_hop[2], messenger)
local tmp, cost1 = wesnoth.find_path(messenger, x, y, {ignore_units = 'yes'})
local _, cost1 = wesnoth.find_path(messenger, x, y, { ignore_units = 'yes' })
local unit_in_way2 = wesnoth.get_unit(opt_hop[1], opt_hop[2])
local unit_in_way2 = wesnoth.get_unit(optimum_hop[1], optimum_hop[2])
if (unit_in_way2 == messenger) then unit_in_way2 = nil end
if unit_in_way2 then wesnoth.extract_unit(unit_in_way2) end
wesnoth.put_unit(opt_hop[1], opt_hop[2], messenger)
local tmp, cost2 = wesnoth.find_path(messenger, x, y, {ignore_units = 'yes'})
wesnoth.put_unit(optimum_hop[1], optimum_hop[2], messenger)
local _, cost2 = wesnoth.find_path(messenger, x, y, { ignore_units = 'yes' })
wesnoth.put_unit(x1, y1, messenger)
wesnoth.put_unit(x_current, y_current, messenger)
if unit_in_way then wesnoth.put_unit(unit_in_way) end
if unit_in_way2 then wesnoth.put_unit(unit_in_way2) end
--print(cost1, cost2)
-- If cost2 is significantly less, that means that the other path might overall be faster
-- even though it is currently blocked
if (cost2 + messenger.max_moves/2 < cost1) then next_hop = opt_hop end
--print(next_hop[1], next_hop[2])
-- If cost2 is significantly less, that means that the optimum path might
-- overall be faster even though it is currently blocked
if (cost2 + messenger.max_moves/2 < cost1) then next_hop = optimum_hop end
if next_hop and ((next_hop[1] ~= messenger.x) or (next_hop[2] ~= messenger.y)) then
local unit_in_way = wesnoth.get_unit(next_hop[1], next_hop[2])
@ -94,22 +88,22 @@ function ca_messenger_move:execution(ai, cfg)
end
if (not messenger) or (not messenger.valid) then return end
-- We also test whether an attack without retaliation or with little damage is possible
-- Test whether an attack without retaliation or with little damage is possible
if (messenger.attacks_left <= 0) then return end
if (not H.get_child(messenger.__cfg, 'attack')) then return end
local targets = wesnoth.get_units {
{ "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } },
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },
{ "filter_adjacent", { id = cfg.id } }
}
local max_rating, best_tar, best_weapon = -9e99, {}, -1
for i,t in ipairs(targets) do
local max_rating, best_target, best_weapon = -9e99
for _,target in ipairs(targets) do
local n_weapon = 0
for weapon in H.child_range(messenger.__cfg, "attack") do
n_weapon = n_weapon + 1
local att_stats, def_stats = wesnoth.simulate_combat(messenger, n_weapon, t)
local att_stats, def_stats = wesnoth.simulate_combat(messenger, n_weapon, target)
local rating = -9e99
-- This is an acceptable attack if:
@ -122,25 +116,25 @@ function ca_messenger_move:execution(ai, cfg)
if (att_stats.hp_chance[messenger.hitpoints] == 1)
or (def_stats.hp_chance[0] >= tonumber(enemy_death_chance)) and (att_stats.hp_chance[0] <= tonumber(messenger_death_chance))
then
rating = t.max_hitpoints + def_stats.hp_chance[0]*100 + att_stats.average_hp - def_stats.average_hp
rating = target.max_hitpoints + def_stats.hp_chance[0]*100 + att_stats.average_hp - def_stats.average_hp
end
--print(messenger.id, t.id,weapon.name, rating)
if rating > max_rating then
max_rating, best_tar, best_weapon = rating, t, n_weapon
if (rating > max_rating) then
max_rating, best_target, best_weapon = rating, target, n_weapon
end
end
end
if max_rating > -9e99 then
AH.checked_attack(ai, messenger, best_tar, best_weapon)
if best_target then
AH.checked_attack(ai, messenger, best_target, best_weapon)
else
-- Otherwise, always attack enemy on last waypoint
-- Always attack enemy on last waypoint
local waypoint_x = AH.split(cfg.waypoint_x, ",")
local waypoint_y = AH.split(cfg.waypoint_y, ",")
local target = wesnoth.get_units {
x = tonumber(waypoint_x[#waypoint_x]),
y = tonumber(waypoint_y[#waypoint_y]),
{ "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } },
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },
{ "filter_adjacent", { id = cfg.id } }
}[1]
@ -150,7 +144,6 @@ function ca_messenger_move:execution(ai, cfg)
end
if (not messenger) or (not messenger.valid) then return end
-- Finally, make sure unit is really done after this
AH.checked_stopunit_attacks(ai, messenger)
end