Skip to content

Commit

Permalink
Lua code: replace deprecated helper.child_range() calls
Browse files Browse the repository at this point in the history
(cherry-picked from commit 7c137e1)
  • Loading branch information
mattsc committed Oct 7, 2018
1 parent 58529e9 commit 4586e04
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions data/ai/lua/battle_calcs.lua
Expand Up @@ -38,11 +38,11 @@ function battle_calcs.unit_attack_info(unit, cache)
resist_mod = {},
alignment = unit_cfg.alignment
}
for attack in H.child_range(unit_cfg, 'attack') do
for attack in wml.child_range(unit_cfg, 'attack') do
-- 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 H.child_range(attack, 'specials') do
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
Expand Down
14 changes: 7 additions & 7 deletions data/ai/lua/generic_recruit_engine.lua
Expand Up @@ -63,7 +63,7 @@ return {
local abilities = wml.get_child(unit.__cfg, "abilities")
local regen_amount = 0
if abilities then
for regen in H.child_range(abilities, "regenerate") do
for regen in wml.child_range(abilities, "regenerate") do
if regen.value > regen_amount then
regen_amount = regen.value
end
Expand Down Expand Up @@ -121,14 +121,14 @@ return {
-- This may be rectifiable by looking at retaliation damage as well.
local steadfast = false

for attack in H.child_range(wesnoth.unit_types[attacker.type].__cfg, "attack") do
for attack in wml.child_range(wesnoth.unit_types[attacker.type].__cfg, "attack") do
local defense = defender_defense
local poison = false
local damage_multiplier = 1
local damage_bonus = 0
local weapon_damage = attack.damage

for special in H.child_range(attack, 'specials') do
for special in wml.child_range(attack, 'specials') do
local mod
if wml.get_child(special, 'poison') and can_poison then
poison = true
Expand Down Expand Up @@ -195,9 +195,9 @@ return {

-- Handle drain for defender
local drain_recovery = 0
for defender_attack in H.child_range(defender.__cfg, 'attack') do
for defender_attack in wml.child_range(defender.__cfg, 'attack') do
if (defender_attack.range == attack.range) then
for special in H.child_range(defender_attack, 'specials') do
for special in wml.child_range(defender_attack, 'specials') do
if wml.get_child(special, 'drains') and drainable(attacker) then
-- TODO: calculate chance to hit
-- currently assumes 50% chance to hit using supplied constant
Expand Down Expand Up @@ -293,8 +293,8 @@ return {
end

function can_slow(unit)
for defender_attack in H.child_range(unit.__cfg, 'attack') do
for special in H.child_range(defender_attack, 'specials') do
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
return true
end
Expand Down
2 changes: 1 addition & 1 deletion data/ai/lua/retreat.lua
Expand Up @@ -85,7 +85,7 @@ function retreat_functions.get_healing_locations()
local heal_amount = 0
local cure = 0
local abilities = wml.get_child(u.__cfg, "abilities") or {}
for ability in H.child_range(abilities, "heals") do
for ability in wml.child_range(abilities, "heals") do
heal_amount = ability.value
if ability.poison == "slowed" then
cure = 1
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_fast_attack_utils.lua
Expand Up @@ -128,11 +128,11 @@ function ca_fast_attack_utils.single_unit_info(unit_proxy)
-- Information about the attacks indexed by weapon number,
-- including specials (e.g. 'poison = true')
single_unit_info.attacks = {}
for attack in H.child_range(unit_cfg, 'attack') do
for attack in wml.child_range(unit_cfg, 'attack') do
-- 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 H.child_range(attack, 'specials') do
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
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_hang_out.lua
Expand Up @@ -66,7 +66,7 @@ function ca_hang_out:execution(cfg)
avoid_map = LS.of_pairs(wesnoth.get_locations(avoid_tag))
else
local ai_tag = wml.get_child(wesnoth.sides[wesnoth.current.side].__cfg, 'ai')
for aspect in H.child_range(ai_tag, 'aspect') do
for aspect in wml.child_range(ai_tag, 'aspect') do
if (aspect.id == 'avoid') then
local facet = wml.get_child(aspect, 'facet')
if facet or aspect.name ~= "composite_aspect" then
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_protect_unit_attack.lua
Expand Up @@ -10,7 +10,7 @@ function ca_protect_unit_attack:evaluation(cfg)
-- or the counter attack on the enemy turn, it does not attack, even if that's really unlikely

local units = {}
for u in H.child_range(cfg, "unit") do
for u in wml.child_range(cfg, "unit") do
table.insert(units, AH.get_units_with_attacks { id = u.id }[1])
end
if (not units[1]) then return 0 end
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_protect_unit_finish.lua
Expand Up @@ -5,7 +5,7 @@ local ca_protect_unit_finish, PU_unit, PU_goal = {}

function ca_protect_unit_finish:evaluation(cfg)
-- If a unit can make it to the goal, this is the first thing that happens
for u in H.child_range(cfg, "unit") do
for u in wml.child_range(cfg, "unit") do
local unit = AH.get_units_with_moves { id = u.id }[1]
if unit then
local path, cost = AH.find_path_with_shroud(unit, u.goal_x, u.goal_y)
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_protect_unit_move.lua
Expand Up @@ -5,7 +5,7 @@ local BC = wesnoth.require "ai/lua/battle_calcs.lua"

local function get_protected_units(cfg)
local units = {}
for u in H.child_range(cfg, "unit") do
for u in wml.child_range(cfg, "unit") do
table.insert(units, AH.get_units_with_moves { id = u.id }[1])
end
return units
Expand Down Expand Up @@ -38,7 +38,7 @@ function ca_protect_unit_move:execution(cfg, data)
-- We move the weakest (fewest HP unit) first
local unit = AH.choose(protected_units, function(u) return - u.hitpoints end)
local goal = {}
for u in H.child_range(cfg, "unit") do
for u in wml.child_range(cfg, "unit") do
if (unit.id == u.id) then goal = { u.goal_x, u.goal_y } end
end

Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_recruit_random.lua
Expand Up @@ -59,7 +59,7 @@ function ca_recruit_random:evaluation(cfg)
local probabilities, probability_sum = {}, 0

-- Go through all the types listed in [probability] tags (which can be comma-separated lists)
for prob in H.child_range(cfg, "probability") do
for prob in wml.child_range(cfg, "probability") do
types = AH.split(prob.type, ",")
for _,typ in ipairs(types) do -- 'type' is a reserved keyword in Lua
-- If this type is in the recruit list, add it
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/mai-defs/protect.lua
Expand Up @@ -11,7 +11,7 @@ function wesnoth.micro_ais.protect_unit(cfg)
}

local unit_ids = {}
for u in H.child_range(cfg, "unit") do
for u in wml.child_range(cfg, "unit") do
if not u.id then
H.wml_error("Protect Unit Micro AI missing id key in [unit] tag")
end
Expand Down
18 changes: 9 additions & 9 deletions data/ai/micro_ais/micro_ai_helper.lua
Expand Up @@ -39,9 +39,9 @@ function micro_ai_helper.add_CAs(side, ca_id_core, CA_parms, CA_cfg)
while id_found do -- This is really just a precaution
id_found = false

for ai_tag in H.child_range(wesnoth.sides[side].__cfg, 'ai') do
for stage in H.child_range(ai_tag, 'stage') do
for ca in H.child_range(stage, 'candidate_action') do
for ai_tag in wml.child_range(wesnoth.sides[side].__cfg, 'ai') do
for stage in wml.child_range(ai_tag, 'stage') do
for ca in wml.child_range(stage, 'candidate_action') do
if string.find(ca.name, ai_id .. '_') then
id_found = true
break
Expand All @@ -54,10 +54,10 @@ function micro_ai_helper.add_CAs(side, ca_id_core, CA_parms, CA_cfg)
-- AI's data variable. However, the MAI can be changed while it is not
-- the AI's turn, when this is not possible. So instead, we check for the
-- existence of such tags and make sure we are using a different ai_id.
for ai_tag in H.child_range(wesnoth.sides[side].__cfg, 'ai') do
for engine in H.child_range(ai_tag, 'engine') do
for data in H.child_range(engine, 'data') do
for mai in H.child_range(data, 'micro_ai') do
for ai_tag in wml.child_range(wesnoth.sides[side].__cfg, 'ai') do
for engine in wml.child_range(ai_tag, 'engine') do
for data in wml.child_range(engine, 'data') do
for mai in wml.child_range(data, 'micro_ai') do
if (mai.ai_id == ai_id) then
id_found = true
break
Expand Down Expand Up @@ -172,7 +172,7 @@ function micro_ai_helper.micro_ai_setup(cfg, CA_parms, required_keys, optional_k
if not wml.get_child(cfg, v) then
H.wml_error("[micro_ai] tag (" .. cfg.ai_type .. ") is missing required parameter: [" .. v .. "]")
end
for child in H.child_range(cfg, v) do
for child in wml.child_range(cfg, v) do
table.insert(CA_cfg, T[v](child))
end
else
Expand All @@ -187,7 +187,7 @@ function micro_ai_helper.micro_ai_setup(cfg, CA_parms, required_keys, optional_k
for _,v in pairs(optional_keys) do
if v:match('%[[a-zA-Z0-9_]+%]') then
v = v:sub(2,-2)
for child in H.child_range(cfg, v) do
for child in wml.child_range(cfg, v) do
table.insert(CA_cfg, T[v](child))
end
else
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/micro_ai_self_data.lua
Expand Up @@ -79,7 +79,7 @@ function micro_ai_self_data.get_mai_self_data(self_data, ai_id, key)
-- table of key=value pairs (including the ai_id key)
-- - If no such tag is found: nil (if @key is set), otherwise empty table

for mai in H.child_range(self_data, "micro_ai") do
for mai in wml.child_range(self_data, "micro_ai") do
if (mai.ai_id == ai_id) then
if key then
return mai[key]
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/micro_ai_unit_variables.lua
Expand Up @@ -70,7 +70,7 @@ function micro_ai_unit_variables.get_mai_unit_variables(unit, ai_id, key)
-- table of key=value pairs (including the ai_id key)
-- - If no such tag is found: nil (if @key is set), otherwise empty table

for mai in H.child_range(unit.variables.__cfg, "micro_ai") do
for mai in wml.child_range(unit.variables.__cfg, "micro_ai") do
if (mai.ai_id == ai_id) then
if key then
return mai[key]
Expand Down
2 changes: 1 addition & 1 deletion data/multiplayer/scenarios/2p_Dark_Forecast.lua
Expand Up @@ -251,7 +251,7 @@ local function final_spawn()
local spawn = wml.variables[string.format("fixed_spawn[%d]", spawn_index)]
wml.variables[string.format("fixed_spawn[%d]", spawn_index)] = nil
local types = {}
for tag in helper.child_range(spawn, "type") do
for tag in wml.child_range(spawn, "type") do
table.insert(types, tag.type)
end
place_units(types, spawn.x, spawn.y)
Expand Down

0 comments on commit 4586e04

Please sign in to comment.