Lua AIs: use ai_helper get_unit functions where applicable

The ai_helper functions are optimized for speed (as much as possible in a general setting) and do all the necessary tests.  For example, get_units_with_attacks() checks both whether the unit has attacks left, and whether it has any attacks in the first place.

(cherry-picked from commit 75843541bc)
This commit is contained in:
mattsc 2018-09-05 19:38:28 -07:00
parent 44ceaa1874
commit 46efcf15fd
8 changed files with 13 additions and 32 deletions

View file

@ -13,9 +13,7 @@ function ca_grab_villages:evaluation(cfg, data)
if AH.print_eval() then AH.print_ts(' - Evaluating grab_villages CA:') end
-- Check if there are units with moves left
local units = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'no',
formula = 'movement_left > 0'
}
local units = AH.get_units_with_moves { side = wesnoth.current.side, canrecruit = 'no' }
if (not units[1]) then
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
return 0

View file

@ -13,10 +13,9 @@ function ca_move_to_any_enemy:evaluation(cfg, data)
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 = wesnoth.get_units {
local units = AH.get_units_with_moves {
side = wesnoth.current.side,
canrecruit = 'no',
formula = 'movement_left > 0'
canrecruit = 'no'
}
if (not units[1]) then

View file

@ -11,10 +11,7 @@ function ca_retreat_injured:evaluation(cfg, data)
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 = wesnoth.get_units {
side = wesnoth.current.side,
formula = 'movement_left > 0'
}
local units = AH.get_units_with_moves { side = wesnoth.current.side }
local unit, loc = R.retreat_injured_units(units)
if unit then
retreat_unit = unit

View file

@ -12,8 +12,7 @@ function ca_spread_poison:evaluation(cfg, data)
-- If a unit with a poisoned weapon can make an attack, we'll do that preferentially
-- (with some exceptions)
local poisoners = AH.get_live_units { side = wesnoth.current.side,
formula = 'attacks_left > 0',
local poisoners = AH.get_units_with_attacks { side = wesnoth.current.side,
{ "filter_wml", {
{ "attack", {
{ "specials", {

View file

@ -30,10 +30,9 @@ function ca_village_hunt:evaluation(cfg, data)
return 0
end
local units = wesnoth.get_units {
local units = AH.get_units_with_moves {
side = wesnoth.current.side,
canrecruit = false,
formula = 'movement_left > 0'
canrecruit = false
}
if not units[1] then
@ -46,10 +45,9 @@ function ca_village_hunt:evaluation(cfg, data)
end
function ca_village_hunt:execution(cfg, data)
local unit = wesnoth.get_units({
local unit = AH.get_units_with_moves({
side = wesnoth.current.side,
canrecruit = false,
formula = 'movement_left > 0'
canrecruit = false
})[1]
if AH.print_exec() then AH.print_ts(' Executing village_hunt CA') end

View file

@ -6,18 +6,14 @@ local M = wesnoth.map
local ca_ogres_flee = {}
function ca_ogres_flee:evaluation()
local units = wesnoth.get_units { side = wesnoth.current.side,
formula = 'movement_left > 0'
}
local units = AH.get_units_with_moves { side = wesnoth.current.side }
if (not units[1]) then return 0 end
return 110000
end
function ca_ogres_flee:execution()
local units = wesnoth.get_units { side = wesnoth.current.side,
formula = 'movement_left > 0'
}
local units = AH.get_units_with_moves { side = wesnoth.current.side }
local units_noMP = wesnoth.get_units { side = wesnoth.current.side,
formula = 'movement_left = 0'

View file

@ -6,10 +6,7 @@ local ca_aggressive_attack_no_suicide = {}
function ca_aggressive_attack_no_suicide:evaluation(cfg, data)
local units = wesnoth.get_units {
side = wesnoth.current.side,
formula = 'attacks_left > 0'
}
local units = AH.get_units_with_attacks { side = wesnoth.current.side }
if (not units[1]) then return 0 end
-- Get all possible attacks

View file

@ -7,10 +7,7 @@ local retreat = {}
function retreat:evaluation(cfg, data)
local units = wesnoth.get_units {
side = wesnoth.current.side,
formula = 'movement_left > 0'
}
local units = AH.get_units_with_moves { side = wesnoth.current.side }
if (not units[1]) then return 0 end
local unit, dst, enemy_threat = R.retreat_injured_units(units)