Skip to content

Commit

Permalink
Lua AIs: avoid using __cfg for accessing weapon specials
Browse files Browse the repository at this point in the history
(cherry-picked from commit aa59ac5)
  • Loading branch information
mattsc committed Oct 7, 2018
1 parent 2d09969 commit 3906d78
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
43 changes: 20 additions & 23 deletions data/ai/lua/battle_calcs.lua
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions data/ai/lua/generic_recruit_engine.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3906d78

Please sign in to comment.