Various Lua AI code: use simpler and faster new syntax

No change in behavior.
This commit is contained in:
mattsc 2016-09-04 18:54:21 -07:00
parent 0b15dfd0fc
commit c03bd5411f
8 changed files with 9 additions and 10 deletions

View file

@ -736,12 +736,11 @@ end
function ai_helper.get_units_with_attacks(filter)
-- Using formula = '$this_unit.attacks_left > 0' is slow, this method is much faster
-- Also need to check that units actually have attacks (as attacks_left > 0 with no attacks is possible)
-- The latter has to go through unit.__cfg which is slow, but there is no way around that, as far as I know
local all_units = wesnoth.get_units(filter)
local units = {}
for _,unit in ipairs(all_units) do
if (unit.attacks_left > 0) and (H.get_child(unit.__cfg, 'attack')) then
if (unit.attacks_left > 0) and (#unit.attacks > 0) then
table.insert(units, unit)
end
end

View file

@ -40,7 +40,7 @@ function ca_attack_highxp:evaluation(cfg, data)
if (unit.side == wesnoth.current.side) and (unit.attacks_left > 0) and (#unit.attacks > 0) then
table.insert(units, unit)
local level = wesnoth.unit_types[unit.type].level
local level = unit.level
if (level > max_unit_level) then
max_unit_level = level
end
@ -60,7 +60,7 @@ function ca_attack_highxp:evaluation(cfg, data)
local ind_attackers, ind_other_units = {}, {}
for i_u,unit in ipairs(units) do
if (H.distance_between(enemy.x, enemy.y, unit.x, unit.y) <= unit.moves + 1) then
if (wesnoth.unit_types[unit.type].level >= XP_to_levelup) then
if (unit.level >= XP_to_levelup) then
potential_target = true
table.insert(ind_attackers, i_u)
else

View file

@ -13,7 +13,7 @@ function retreat_functions.min_hp(unit)
-- The minimum hp to retreat is a function of level and terrain defense
-- We want to stay longer on good terrain and leave early on very bad terrain
local hp_per_level = wesnoth.unit_defense(unit, wesnoth.get_terrain(unit.x, unit.y))/15
local level = wesnoth.unit_types[unit.type].level
local level = unit.level
-- Leaders are considered to be higher level because of their value
if unit.canrecruit then level = level+2 end

View file

@ -111,7 +111,7 @@ function ca_assassin_move:execution(cfg, data)
local zoc_active = (not is_skirmisher)
if zoc_active then
local level = wesnoth.unit_types[enemy.type].level
local level = enemy.level
if (level == 0) then zoc_active = false end
end

View file

@ -319,7 +319,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
local unit_in_way = wesnoth.get_unit(xa, ya)
local data = { x = xa, y = ya,
defender = enemy,
defender_level = wesnoth.unit_types[enemy.type].level,
defender_level = enemy.level,
unit_in_way = unit_in_way
}
table.insert(attacks, data)

View file

@ -77,7 +77,7 @@ function ca_fast_combat:evaluation(cfg, data)
local unit_info = FAU.get_unit_info(unit, data.gamedata)
local unit_copy = FAU.get_unit_copy(unit.id, data.gamedata)
if (unit.attacks_left > 0) and (H.get_child(unit.__cfg, 'attack')) then
if (unit.attacks_left > 0) and (#unit.attacks > 0) then
local attacks = AH.get_attacks({ unit }, { include_occupied = cfg.include_occupied_attack_hexes })
if (#attacks > 0) then

View file

@ -37,7 +37,7 @@ function ca_fast_combat_leader:evaluation(cfg, data)
end
end
if (leader.attacks_left == 0) or (not H.get_child(leader.__cfg, 'attack')) then return 0 end
if (leader.attacks_left == 0) or (#leader.attacks == 0) then return 0 end
local excluded_enemies_map = LS.create()

View file

@ -88,7 +88,7 @@ function ca_messenger_move:execution(cfg)
-- 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
if (#messenger.attacks == 0) then return end
local targets = wesnoth.get_units {
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },