AI: enable multiple leaders in castle_switch CA

Previously the CA would already move multiple leaders if all leaders were to be moved, but it would abandon moving any leader after finding one that should not move.
This commit is contained in:
mattsc 2019-12-05 07:00:51 -08:00
parent 64e969af11
commit ab2c3bfcc2
2 changed files with 134 additions and 125 deletions

View file

@ -4,7 +4,7 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map local M = wesnoth.map
local CS_leader_score local CS_leader_score
-- Note that leader_target is also needed by the recruiting CA, so it must be stored in 'data' -- Note that CS_leader and CS_leader_target are also needed by the recruiting CA, so they must be stored in 'data'
local function get_reachable_enemy_leaders(unit, avoid_map) local function get_reachable_enemy_leaders(unit, avoid_map)
-- We're cheating a little here and also find hidden enemy leaders. That's -- We're cheating a little here and also find hidden enemy leaders. That's
@ -38,15 +38,16 @@ function ca_castle_switch:evaluation(cfg, data, filter_own)
return 0 return 0
end end
local leader = AH.get_units_with_moves({ local leaders = AH.get_units_with_moves({
side = wesnoth.current.side, side = wesnoth.current.side,
canrecruit = 'yes', canrecruit = 'yes',
formula = '(movement_left = total_movement) and (hitpoints = max_hitpoints)', formula = '(movement_left = total_movement) and (hitpoints = max_hitpoints)',
{ "and", filter_own } { "and", filter_own }
}, true)[1] }, true)
if not leader then
if (not leaders[1]) then
-- CA is irrelevant if no leader or the leader may have moved from another CA -- CA is irrelevant if no leader or the leader may have moved from another CA
data.leader_target = nil data.CS_leader, data.CS_leader_target = nil, nil
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
return 0 return 0
end end
@ -55,18 +56,23 @@ function ca_castle_switch:evaluation(cfg, data, filter_own)
local avoid_map = AH.get_avoid_map(ai, nil, true) local avoid_map = AH.get_avoid_map(ai, nil, true)
if data.leader_target and wesnoth.sides[wesnoth.current.side].gold >= cheapest_unit_cost then if data.CS_leader and wesnoth.sides[wesnoth.current.side].gold >= cheapest_unit_cost then
-- make sure move is still valid -- make sure move is still valid
local path, cost = AH.find_path_with_avoid(leader, data.leader_target[1], data.leader_target[2], avoid_map) local path, cost = AH.find_path_with_avoid(data.CS_leader, data.CS_leader_target[1], data.CS_leader_target[2], avoid_map)
local next_hop = AH.next_hop(leader, nil, nil, { path = path, avoid_map = avoid_map }) local next_hop = AH.next_hop(data.CS_leader, nil, nil, { path = path, avoid_map = avoid_map })
if next_hop and next_hop[1] == data.leader_target[1] if next_hop and next_hop[1] == data.CS_leader_target[1]
and next_hop[2] == data.leader_target[2] then and next_hop[2] == data.CS_leader_target[2]
then
return CS_leader_score return CS_leader_score
else else
data.leader_target = nil data.CS_leader, data.CS_leader_target = nil, nil
end end
end end
-- Look for the best keep
local overall_best_score = 0
for _,leader in ipairs(leaders) do
local best_score, best_loc, best_turns, best_path = 0, {}, 3
local keeps = AH.get_locations_no_borders { local keeps = AH.get_locations_no_borders {
terrain = 'K*,K*^*,*^K*', -- Keeps terrain = 'K*,K*^*,*^K*', -- Keeps
{ "not", { {"filter", {}} }}, -- That have no unit { "not", { {"filter", {}} }}, -- That have no unit
@ -91,8 +97,6 @@ function ca_castle_switch:evaluation(cfg, data, filter_own)
local enemy_leaders = get_reachable_enemy_leaders(leader, avoid_map) local enemy_leaders = get_reachable_enemy_leaders(leader, avoid_map)
-- Look for the best keep
local best_score, best_loc, best_turns, best_path = 0, {}, 3
for i,loc in ipairs(keeps) do for i,loc in ipairs(keeps) do
-- Only consider keeps within 2 turns movement -- Only consider keeps within 2 turns movement
local path, cost = AH.find_path_with_avoid(leader, loc[1], loc[2], avoid_map) local path, cost = AH.find_path_with_avoid(leader, loc[1], loc[2], avoid_map)
@ -165,10 +169,9 @@ function ca_castle_switch:evaluation(cfg, data, filter_own)
end end
end end
data.leader_target = next_hop
-- if we're on a keep, wait until there are no movable units on the castle before moving off -- if we're on a keep, wait until there are no movable units on the castle before moving off
CS_leader_score = 195000 local leader_score = 195000
if wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep then if wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep then
local castle = AH.get_locations_no_borders { local castle = AH.get_locations_no_borders {
{ "and", { { "and", {
@ -187,10 +190,22 @@ function ca_castle_switch:evaluation(cfg, data, filter_own)
end end
end end
if should_wait then if should_wait then
CS_leader_score = 15000 leader_score = 15000
end end
end end
best_score = best_score + leader_score
if (best_score > overall_best_score) then
overall_best_score = best_score
CS_leader_score = leader_score
data.CS_leader = leader
data.CS_leader_target = next_hop
end
end
end
if (overall_best_score > 0) then
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
return CS_leader_score return CS_leader_score
end end
@ -200,17 +215,11 @@ function ca_castle_switch:evaluation(cfg, data, filter_own)
end end
function ca_castle_switch:execution(cfg, data, filter_own) function ca_castle_switch:execution(cfg, data, filter_own)
local leader = AH.get_units_with_moves({
side = wesnoth.current.side,
canrecruit = 'yes',
{ "and", filter_own }
}, true)[1]
if AH.print_exec() then AH.print_ts(' Executing castle_switch CA') end if AH.print_exec() then AH.print_ts(' Executing castle_switch CA') end
if AH.show_messages() then wesnoth.wml_actions.message { speaker = leader.id, message = 'Switching castles' } end if AH.show_messages() then wesnoth.wml_actions.message { speaker = leader.id, message = 'Switching castles' } end
AH.checked_move(ai, leader, data.leader_target[1], data.leader_target[2]) AH.checked_move(ai, data.CS_leader, data.CS_leader_target[1], data.CS_leader_target[2])
data.leader_target = nil data.CS_leader, data.CS_leader_target = nil
end end
return ca_castle_switch return ca_castle_switch

View file

@ -20,8 +20,8 @@ if ca_castle_switch then
params.leader_takes_village = (function() params.leader_takes_village = (function()
if ca_castle_switch:evaluation({}, dummy_engine.data) > 0 then if ca_castle_switch:evaluation({}, dummy_engine.data) > 0 then
local take_village = #(wesnoth.get_villages { local take_village = #(wesnoth.get_villages {
x = dummy_engine.data.leader_target[1], x = dummy_engine.data.CS_leader_target[1],
y = dummy_engine.data.leader_target[2] y = dummy_engine.data.CS_leader_target[2]
}) > 0 }) > 0
return take_village return take_village
end end