Lua AIs: use new syntax for iterating over attacks
This is both simpler and faster than the old syntax.
This commit is contained in:
parent
3683c75968
commit
acecd77bb4
5 changed files with 7 additions and 20 deletions
|
@ -1146,11 +1146,9 @@ end
|
|||
function ai_helper.has_weapon_special(unit, special)
|
||||
-- Returns true/false depending on whether @unit has a weapon with special @special
|
||||
-- Also returns the number of the first weapon with this special
|
||||
local weapon_number = 0
|
||||
for att in H.child_range(unit.__cfg, 'attack') do
|
||||
weapon_number = weapon_number + 1
|
||||
for sp in H.child_range(att, 'specials') do
|
||||
if H.get_child(sp, special) then
|
||||
for weapon_number,att in ipairs(unit.attacks) do
|
||||
for _,sp in ipairs(att.specials) do
|
||||
if (sp[1] == special) then
|
||||
return true, weapon_number
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,10 +17,7 @@ function ca_bottleneck_attack:evaluation(cfg, data)
|
|||
local targets = AH.get_attackable_enemies { { "filter_adjacent", { id = attacker.id } } }
|
||||
|
||||
for _,target in ipairs(targets) do
|
||||
local n_weapon = 0
|
||||
for weapon in H.child_range(attacker.__cfg, "attack") do
|
||||
n_weapon = n_weapon + 1
|
||||
|
||||
for n_weapon,weapon in ipairs(attacker.attacks) do
|
||||
local att_stats, def_stats = wesnoth.simulate_combat(attacker, n_weapon, target)
|
||||
|
||||
local rating
|
||||
|
|
|
@ -379,9 +379,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
|
|||
if (attack.x == loc[1]) and (attack.y == loc[2]) and
|
||||
(unit.max_experience - unit.experience <= 8 * attack.defender_level)
|
||||
then
|
||||
local n_weapon = 0
|
||||
for weapon in H.child_range(unit.__cfg, "attack") do
|
||||
n_weapon = n_weapon + 1
|
||||
for n_weapon,weapon in ipairs(unit.attacks) do
|
||||
local att_stats, def_stats = BC.simulate_combat_loc(unit, { attack.x, attack.y }, attack.defender, n_weapon)
|
||||
|
||||
-- Execute level-up attack when:
|
||||
|
|
|
@ -93,10 +93,7 @@ function ca_messenger_move:execution(cfg)
|
|||
|
||||
local max_rating, best_target, best_weapon = -9e99
|
||||
for _,target in ipairs(targets) do
|
||||
local n_weapon = 0
|
||||
for weapon in H.child_range(messenger.__cfg, "attack") do
|
||||
n_weapon = n_weapon + 1
|
||||
|
||||
for n_weapon,weapon in ipairs(messenger.attacks) do
|
||||
local att_stats, def_stats = wesnoth.simulate_combat(messenger, n_weapon, target)
|
||||
|
||||
local rating = -9e99
|
||||
|
|
|
@ -49,11 +49,8 @@ function ca_protect_unit_attack:evaluation(cfg)
|
|||
max_counter_damage = max_counter_damage + counter_damage_table[str]
|
||||
else -- if not, calculate it and save value
|
||||
-- Go thru all weapons, as "best weapon" might be different later on
|
||||
local n_weapon = 0
|
||||
local min_hp = unit.hitpoints
|
||||
for weapon in H.child_range(enemy_attack.enemy.__cfg, "attack") do
|
||||
n_weapon = n_weapon + 1
|
||||
|
||||
for n_weapon,weapon in ipairs(enemy_attack.enemy.attacks) do
|
||||
-- Terrain does not matter for this, we're only interested in the maximum damage
|
||||
local att_stats, def_stats = wesnoth.simulate_combat(enemy_attack.enemy, n_weapon, unit)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue