Lua AIs: avoid using __cfg for accessing weapon specials

(cherry-picked from commit aa59ac5455)
This commit is contained in:
mattsc 2018-08-28 19:07:42 -07:00
parent 2d0996982e
commit 3906d78abd
2 changed files with 29 additions and 29 deletions

View file

@ -38,38 +38,35 @@ function battle_calcs.unit_attack_info(unit, cache)
resist_mod = {},
alignment = unit_cfg.alignment
}
for attack in wml.child_range(unit_cfg, 'attack') do
local attacks = unit.attacks
for i_a = 1,#attacks do
local attack = attacks[i_a]
-- Extract information for specials; we do this first because some
-- custom special might have the same name as one of the default scalar fields
local a = {}
for special in wml.child_range(attack, 'specials') do
for _,sp in ipairs(special) do
if (sp[1] == 'damage') then -- this is 'backstab'
if (sp[2].id == 'backstab') then
a.backstab = true
else
if (sp[2].id == 'charge') then a.charge = true end
end
for _,sp in ipairs(attack.specials) do
if (sp[1] == 'damage') then -- this is 'backstab'
if (sp[2].id == 'backstab') then
a.backstab = true
else
-- magical, marksman
if (sp[1] == 'chance_to_hit') then
a[sp[2].id] = true
else
a[sp[1]] = true
end
if (sp[2].id == 'charge') then a.charge = true end
end
else
-- magical, marksman
if (sp[1] == 'chance_to_hit') then
a[sp[2].id] = true
else
a[sp[1]] = true
end
end
end
-- Now extract the scalar (string and number) values from attack
for k,v in pairs(attack) do
if (type(v) == 'number') or (type(v) == 'string') then
a[k] = v
end
end
-- [attack]number= defaults to zero; must be defined for battle_calcs.best_weapons()
a.number = a.number or 0
a.damage = attack.damage
a.type = attack.type
a.range = attack.range
-- number must be defined for battle_calcs.best_weapons()
a.number = attack.number or 0
table.insert(unit_info.attacks, a)
end

View file

@ -193,10 +193,12 @@ return {
-- Handle drain for defender
local drain_recovery = 0
for defender_attack in wml.child_range(defender.__cfg, 'attack') do
local defender_attacks = defender.attacks
for i_d = 1,#defender_attacks do
local defender_attack = defender_attacks[i_d]
if (defender_attack.range == attack.range) then
for special in wml.child_range(defender_attack, 'specials') do
if wml.get_child(special, 'drains') and drainable(attacker) then
for _,sp in ipairs(defender_attack.specials) do
if (sp[1] == 'drains') and drainable(attacker) then
-- TODO: calculate chance to hit
-- currently assumes 50% chance to hit using supplied constant
local attacker_resistance = attacker:resistance(defender_attack.type)
@ -291,9 +293,10 @@ return {
end
function can_slow(unit)
for defender_attack in wml.child_range(unit.__cfg, 'attack') do
for special in wml.child_range(defender_attack, 'specials') do
if wml.get_child(special, 'slow') then
local attacks = unit.attacks
for i_a = 1,#attacks do
for _,sp in ipairs(attacks[i_a].specials) do
if (sp[1] == 'slow') then
return true
end
end