Skip to content

Commit

Permalink
Lua AIs: use new syntax for iterating over attacks
Browse files Browse the repository at this point in the history
This is both simpler and faster than the old syntax.
  • Loading branch information
mattsc committed Oct 22, 2016
1 parent 3683c75 commit acecd77
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 20 deletions.
8 changes: 3 additions & 5 deletions data/ai/lua/ai_helper.lua
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions data/ai/micro_ais/cas/ca_bottleneck_attack.lua
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions data/ai/micro_ais/cas/ca_bottleneck_move.lua
Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions data/ai/micro_ais/cas/ca_messenger_move.lua
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions data/ai/micro_ais/cas/ca_protect_unit_attack.lua
Expand Up @@ -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)

Expand Down

0 comments on commit acecd77

Please sign in to comment.