Skip to content

Commit

Permalink
Lua AIs: use ai_helper get_unit functions where applicable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattsc committed Sep 6, 2018
1 parent a7d8024 commit 7584354
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 32 deletions.
4 changes: 1 addition & 3 deletions data/ai/lua/ca_grab_villages.lua
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions data/ai/lua/ca_move_to_any_enemy.lua
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions data/ai/lua/ca_retreat_injured.lua
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions data/ai/lua/ca_spread_poison.lua
Expand Up @@ -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", {
Expand Down
10 changes: 4 additions & 6 deletions data/ai/lua/ca_village_hunt.lua
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions data/campaigns/Eastern_Invasion/ai/ca_ogres_flee.lua
Expand Up @@ -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'
Expand Down
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions data/campaigns/The_Rise_Of_Wesnoth/ai/ca_retreat.lua
Expand Up @@ -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)
Expand Down

0 comments on commit 7584354

Please sign in to comment.