Lua AIs: eliminate usages of __cfg

These are cases for which the respective variable was not accessible otherwise when the code was originally written.
This commit is contained in:
mattsc 2019-12-18 07:21:33 -08:00
parent bcffc9c7b9
commit f2df220579
4 changed files with 13 additions and 14 deletions

View file

@ -1262,12 +1262,12 @@ end
function ai_helper.has_ability(unit, ability)
-- Returns true/false depending on whether unit has the given ability
local has_ability = false
local abilities = wml.get_child(unit.__cfg, "abilities")
if abilities then
if wml.get_child(abilities, ability) then has_ability = true end
for _,ability_id in ipairs(unit.abilities) do
if (ability == ability_id) then
return true
end
end
return has_ability
return false
end
function ai_helper.has_weapon_special(unit, special)

View file

@ -14,10 +14,10 @@ local battle_calcs = {}
function battle_calcs.unit_attack_info(unit, cache)
-- Return a table containing information about attack-related properties of @unit
-- The result can be cached if variable @cache is given
-- This is done in order to avoid duplication of slow processes, such as access to unit.__cfg
-- This is done in order to avoid duplication of slow processes
-- Return table has fields:
-- - attacks: the attack tables from unit.__cfg
-- - attacks: the attack tables
-- - resist_mod: resistance modifiers (multiplicative factors) index by attack type
-- - alignment: just that
@ -32,11 +32,10 @@ function battle_calcs.unit_attack_info(unit, cache)
end
-- Otherwise collect the information
local unit_cfg = unit.__cfg
local unit_info = {
attacks = {},
resist_mod = {},
alignment = unit_cfg.alignment
alignment = unit.alignment
}
local attacks = unit.attacks
for i_a = 1,#attacks do

View file

@ -758,7 +758,7 @@ return {
if eta_turn <= wesnoth.game_config.last_turn then
lawful_bonus = wesnoth.get_time_of_day(wesnoth.current.turn + eta).lawful_bonus / eta^2
end
local damage_bonus = AH.get_unit_time_of_day_bonus(recruit_unit.__cfg.alignment, lawful_bonus)
local damage_bonus = AH.get_unit_time_of_day_bonus(recruit_unit.alignment, lawful_bonus)
-- Estimate effectiveness on offense and defense
local offense_score =
(recruit_effectiveness[recruit_id].damage*damage_bonus+recruit_effectiveness[recruit_id].poison_damage)
@ -847,7 +847,7 @@ return {
for attack_range, count in pairs(unit_attack_range_count[recruit_id]) do
bonus = bonus + 0.02 * most_common_range_count / (attack_range_count[attack_range]+1)
end
local race = wesnoth.races[wesnoth.unit_types[recruit_id].__cfg.race]
local race = wesnoth.races[wesnoth.unit_types[recruit_id].race]
local num_traits = race and race.num_traits or 0
bonus = bonus + 0.03 * num_traits^2
if target_hex[1] then

View file

@ -141,7 +141,7 @@ local function bottleneck_get_rating(unit, x, y, has_leadership, is_healer, on_m
if (unit.hitpoints < unit.max_hitpoints) then
for xa,ya in H.adjacent_tiles(x, y) do
local adjacent_unit = wesnoth.units.get(xa, ya)
if adjacent_unit and (adjacent_unit.__cfg.usage == "healer") then
if adjacent_unit and (adjacent_unit.usage == "healer") then
leadership_rating = leadership_rating + 100
break
end
@ -306,7 +306,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
for _,unit in ipairs(all_units) do
-- Is this a healer or leadership unit?
local is_healer = (unit.__cfg.usage == "healer")
local is_healer = (unit.usage == "healer")
local has_leadership = AH.has_ability(unit, "leadership")
local on_my_territory = BD_is_my_territory:get(unit.x, unit.y)
@ -355,7 +355,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
local max_rating, best_unit, best_hex = 0
for _,unit in ipairs(units) do
local is_healer = (unit.__cfg.usage == "healer")
local is_healer = (unit.usage == "healer")
local has_leadership = AH.has_ability(unit, "leadership")
local on_my_territory = BD_is_my_territory:get(unit.x, unit.y)