Skip to content

Commit

Permalink
AI: add [filter_own] to all Experimental AI candidate actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsc committed Nov 22, 2019
1 parent 997e08c commit 1816ee5
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 21 deletions.
13 changes: 9 additions & 4 deletions data/ai/lua/ca_castle_switch.lua
Expand Up @@ -26,7 +26,7 @@ end

local ca_castle_switch = {}

function ca_castle_switch:evaluation(cfg, data)
function ca_castle_switch:evaluation(cfg, data, filter_own)
local start_time, ca_name = wesnoth.get_time_stamp() / 1000., 'castle_switch'
if AH.print_eval() then AH.print_ts(' - Evaluating castle_switch CA:') end

Expand All @@ -38,7 +38,8 @@ function ca_castle_switch:evaluation(cfg, data)
local leader = wesnoth.units.find_on_map {
side = wesnoth.current.side,
canrecruit = 'yes',
formula = '(movement_left = total_movement) and (hitpoints = max_hitpoints)'
formula = '(movement_left = total_movement) and (hitpoints = max_hitpoints)',
{ "and", filter_own }
}[1]
if not leader then
-- CA is irrelevant if no leader or the leader may have moved from another CA
Expand Down Expand Up @@ -189,8 +190,12 @@ function ca_castle_switch:evaluation(cfg, data)
return 0
end

function ca_castle_switch:execution(cfg, data)
local leader = wesnoth.units.find_on_map { side = wesnoth.current.side, canrecruit = 'yes' }[1]
function ca_castle_switch:execution(cfg, data, filter_own)
local leader = wesnoth.units.find_on_map {
side = wesnoth.current.side,
canrecruit = 'yes',
{ "and", filter_own }
}[1]

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
Expand Down
8 changes: 6 additions & 2 deletions data/ai/lua/ca_grab_villages.lua
Expand Up @@ -8,12 +8,16 @@ local GV_unit, GV_village

local ca_grab_villages = {}

function ca_grab_villages:evaluation(cfg, data)
function ca_grab_villages:evaluation(cfg, data, filter_own)
local start_time, ca_name = wesnoth.get_time_stamp() / 1000., 'grab_villages'
if AH.print_eval() then AH.print_ts(' - Evaluating grab_villages CA:') end

-- Check if there are units with moves left
local units = AH.get_units_with_moves({ side = wesnoth.current.side, canrecruit = 'no' }, true)
local units = AH.get_units_with_moves({
side = wesnoth.current.side,
canrecruit = 'no',
{ "and", filter_own }
}, true)
if (not units[1]) then
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
return 0
Expand Down
5 changes: 3 additions & 2 deletions data/ai/lua/ca_move_to_any_enemy.lua
Expand Up @@ -9,13 +9,14 @@ local MTAE_unit, MTAE_destination

local ca_move_to_any_enemy = {}

function ca_move_to_any_enemy:evaluation(cfg, data)
function ca_move_to_any_enemy:evaluation(cfg, data, filter_own)
local start_time, ca_name = wesnoth.get_time_stamp() / 1000., 'move_to_any_enemy'
if AH.print_eval() then AH.print_ts(' - Evaluating move_to_any_enemy CA:') end

local units = AH.get_units_with_moves {
side = wesnoth.current.side,
canrecruit = 'no'
canrecruit = 'no',
{ "and", filter_own }
}

if (not units[1]) then
Expand Down
4 changes: 2 additions & 2 deletions data/ai/lua/ca_place_healers.lua
Expand Up @@ -5,11 +5,11 @@ local HS = wesnoth.require "ai/micro_ais/cas/ca_healer_move.lua"

local ca_place_healers = {}

function ca_place_healers:evaluation(cfg, data)
function ca_place_healers:evaluation(cfg, data, filter_own)
local start_time, ca_name = wesnoth.get_time_stamp() / 1000., 'place_healers'
if AH.print_eval() then AH.print_ts(' - Evaluating place_healers CA:') end

if HS:evaluation(cfg, data) > 0 then
if HS:evaluation({ { 'filter', filter_own } }, data) > 0 then
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
return 96000
end
Expand Down
3 changes: 2 additions & 1 deletion data/ai/lua/ca_recruit_rushers.lua
Expand Up @@ -34,9 +34,10 @@ wesnoth.require("ai/lua/generic_recruit_engine.lua").init(dummy_engine, params)

local ca_recruit_rushers = {}

function ca_recruit_rushers:evaluation(cfg, data)
function ca_recruit_rushers:evaluation(cfg, data, filter_own)
params.high_level_fraction = cfg.high_level_fraction
params.randomness = cfg.randomness
params.filter_own = filter_own
return dummy_engine:recruit_rushers_eval()
end

Expand Down
7 changes: 5 additions & 2 deletions data/ai/lua/ca_retreat_injured.lua
Expand Up @@ -7,11 +7,14 @@ local retreat_unit, retreat_loc

local ca_retreat_injured = {}

function ca_retreat_injured:evaluation(cfg, data)
function ca_retreat_injured:evaluation(cfg, data, filter_own)
local start_time, ca_name = wesnoth.get_time_stamp() / 1000., 'retreat_injured'
if AH.print_eval() then AH.print_ts(' - Evaluating retreat_injured CA:') end

local units = AH.get_units_with_moves({ side = wesnoth.current.side }, true)
local units = AH.get_units_with_moves({
side = wesnoth.current.side,
{ "and", filter_own }
}, true)
local unit, loc = R.retreat_injured_units(units)
if unit then
retreat_unit = unit
Expand Down
5 changes: 3 additions & 2 deletions data/ai/lua/ca_spread_poison.lua
Expand Up @@ -7,7 +7,7 @@ local SP_attack

local ca_spread_poison = {}

function ca_spread_poison:evaluation(cfg, data)
function ca_spread_poison:evaluation(cfg, data, filter_own)
local start_time, ca_name = wesnoth.get_time_stamp() / 1000., 'spread_poison'
if AH.print_eval() then AH.print_ts(' - Evaluating spread_poison CA:') end

Expand All @@ -21,7 +21,8 @@ function ca_spread_poison:evaluation(cfg, data)
} }
} }
} },
canrecruit = 'no'
canrecruit = 'no',
{ "and", filter_own }
}
if (not poisoners[1]) then
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
Expand Down
16 changes: 12 additions & 4 deletions data/ai/lua/ca_village_hunt.lua
Expand Up @@ -6,7 +6,7 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"

local ca_village_hunt = {}

function ca_village_hunt:evaluation(cfg, data)
function ca_village_hunt:evaluation(cfg, data, filter_own)
local start_time, ca_name = wesnoth.get_time_stamp() / 1000., 'village_hunt'
if AH.print_eval() then AH.print_ts(' - Evaluating village_hunt CA:') end

Expand All @@ -30,7 +30,11 @@ function ca_village_hunt:evaluation(cfg, data)
return 0
end

local units = AH.get_units_with_moves({ side = wesnoth.current.side, canrecruit = 'no' }, true)
local units = AH.get_units_with_moves({
side = wesnoth.current.side,
canrecruit = 'no',
{ "and", filter_own }
}, true)

if not units[1] then
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
Expand All @@ -41,8 +45,12 @@ function ca_village_hunt:evaluation(cfg, data)
return 30000
end

function ca_village_hunt:execution(cfg, data)
local unit = AH.get_units_with_moves({ side = wesnoth.current.side, canrecruit = 'no' }, true)[1]
function ca_village_hunt:execution(cfg, data, filter_own)
local unit = AH.get_units_with_moves({
side = wesnoth.current.side,
canrecruit = 'no',
{ "and", filter_own }
}, true)[1]

if AH.print_exec() then AH.print_ts(' Executing village_hunt CA') end

Expand Down
12 changes: 10 additions & 2 deletions data/ai/lua/generic_recruit_engine.lua
Expand Up @@ -336,7 +336,11 @@ return {

function do_recruit_eval(data)
-- Check if leader is on keep
local leader = wesnoth.units.find_on_map { side = wesnoth.current.side, canrecruit = 'yes' }[1]
local leader = wesnoth.units.find_on_map {
side = wesnoth.current.side,
canrecruit = 'yes',
{ "and", params.filter_own }
}[1]

if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then
return 0
Expand Down Expand Up @@ -566,7 +570,11 @@ return {
end

local recruit_type
local leader = wesnoth.units.find_on_map { side = wesnoth.current.side, canrecruit = 'yes' }[1]
local leader = wesnoth.units.find_on_map {
side = wesnoth.current.side,
canrecruit = 'yes',
{ "and", params.filter_own }
}[1]
repeat
recruit_data.recruit.best_hex, recruit_data.recruit.target_hex = ai_cas:find_best_recruit_hex(leader, recruit_data)
recruit_type = ai_cas:find_best_recruit(attack_type_count, unit_attack_type_count, recruit_effectiveness, recruit_vulnerability, attack_range_count, unit_attack_range_count, most_common_range_count)
Expand Down

0 comments on commit 1816ee5

Please sign in to comment.