Skip to content

Commit

Permalink
Merge pull request #3079 from mattsc/lua_deprecation_fixes
Browse files Browse the repository at this point in the history
Replace deprecated Lua code
  • Loading branch information
mattsc committed May 13, 2018
2 parents bf81191 + c9ef14a commit 2f611a9
Show file tree
Hide file tree
Showing 88 changed files with 335 additions and 385 deletions.
4 changes: 2 additions & 2 deletions data/ai/lua/ai_helper.lua
Expand Up @@ -1092,9 +1092,9 @@ end
function ai_helper.has_ability(unit, ability)
-- Returns true/false depending on whether unit has the given ability
local has_ability = false
local abilities = H.get_child(unit.__cfg, "abilities")
local abilities = wml.get_child(unit.__cfg, "abilities")
if abilities then
if H.get_child(abilities, ability) then has_ability = true end
if wml.get_child(abilities, ability) then has_ability = true end
end
return has_ability
end
Expand Down
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
32 changes: 15 additions & 17 deletions data/ai/lua/generic_recruit_engine.lua
Expand Up @@ -23,8 +23,6 @@ return {
end
math.randomseed(os.time())

local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
Expand Down Expand Up @@ -60,10 +58,10 @@ return {
random_gender = false
}
-- Find the best regeneration ability and use it to estimate hp regained by regeneration
local abilities = H.get_child(unit.__cfg, "abilities")
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,21 +119,21 @@ 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 H.get_child(special, 'poison') and can_poison then
if wml.get_child(special, 'poison') and can_poison then
poison = true
end

-- Handle marksman and magical
mod = H.get_child(special, 'chance_to_hit')
mod = wml.get_child(special, 'chance_to_hit')
if mod then
if mod.value then
if mod.cumulative then
Expand All @@ -157,7 +155,7 @@ return {
end

-- Handle most damage specials (assumes all are cumulative)
mod = H.get_child(special, 'damage')
mod = wml.get_child(special, 'damage')
if mod and mod.active_on ~= "defense" then
local special_multiplier = 1
local special_bonus = 0
Expand Down Expand Up @@ -195,10 +193,10 @@ 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
if H.get_child(special, 'drains') and drainable(attacker) then
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
local attacker_resistance = wesnoth.unit_resistance(attacker, defender_attack.type)
Expand Down Expand Up @@ -293,9 +291,9 @@ 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
if H.get_child(special, 'slow') then
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
end
Expand Down Expand Up @@ -443,7 +441,7 @@ return {
end

function ai_cas:recruit_rushers_exec()
if AH.show_messages() then W.message { speaker = 'narrator', message = 'Recruiting' } end
if AH.show_messages() then wesnoth.wml_actions.message { speaker = 'narrator', message = 'Recruiting' } end

local enemy_counts = recruit_data.recruit.enemy_counts
local enemy_types = recruit_data.recruit.enemy_types
Expand Down Expand Up @@ -936,7 +934,7 @@ return {
local test_units, num_recruits = {}, 0
local movetypes = {}
for x,id in ipairs(wesnoth.sides[wesnoth.current.side].recruit) do
local custom_movement = H.get_child(wesnoth.unit_types[id].__cfg, "movement_costs")
local custom_movement = wml.get_child(wesnoth.unit_types[id].__cfg, "movement_costs")
local movetype = wesnoth.unit_types[id].__cfg.movement_type
if custom_movement
or (not movetypes[movetype])
Expand Down
8 changes: 3 additions & 5 deletions data/ai/lua/generic_rush_engine.lua
Expand Up @@ -6,8 +6,6 @@ return {

-- More generic grunt rush (and can, in fact, be used with other unit types as well)

local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local LS = wesnoth.require "location_set"
Expand Down Expand Up @@ -256,7 +254,7 @@ return {
local leader = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1]

if AH.print_exec() then print_time(' Executing castle_switch CA') end
if AH.show_messages() then W.message { speaker = leader.id, message = 'Switching castles' } end
if AH.show_messages() then wesnoth.wml_actions.message { speaker = leader.id, message = 'Switching castles' } end

AH.checked_move(ai, leader, self.data.leader_target[1], self.data.leader_target[2])
self.data.leader_target = nil
Expand Down Expand Up @@ -395,7 +393,7 @@ return {

function generic_rush:grab_villages_exec()
if AH.print_exec() then print_time(' Executing grab_villages CA') end
if AH.show_messages() then W.message { speaker = self.data.unit.id, message = 'Grab villages' } end
if AH.show_messages() then wesnoth.wml_actions.message { speaker = self.data.unit.id, message = 'Grab villages' } end

AH.movefull_stopunit(ai, self.data.unit, self.data.village)
self.data.unit, self.data.village = nil, nil
Expand Down Expand Up @@ -492,7 +490,7 @@ return {
local is_poisoner, poison_weapon = AH.has_weapon_special(attacker, "poison")

if AH.print_exec() then print_time(' Executing spread_poison CA') end
if AH.show_messages() then W.message { speaker = attacker.id, message = 'Poison attack' } end
if AH.show_messages() then wesnoth.wml_actions.message { speaker = attacker.id, message = 'Poison attack' } end

AH.robust_move_and_attack(ai, attacker, self.data.attack.dst, self.data.attack.target, { weapon = poison_weapon })

Expand Down
4 changes: 2 additions & 2 deletions data/ai/lua/retreat.lua
Expand Up @@ -84,8 +84,8 @@ function retreat_functions.get_healing_locations()
if u.moves == 0 or u.side ~= wesnoth.current.side then
local heal_amount = 0
local cure = 0
local abilities = H.get_child(u.__cfg, "abilities") or {}
for ability in H.child_range(abilities, "heals") do
local abilities = wml.get_child(u.__cfg, "abilities") or {}
for ability in wml.child_range(abilities, "heals") do
heal_amount = ability.value
if ability.poison == "slowed" then
cure = 1
Expand Down
8 changes: 4 additions & 4 deletions data/ai/micro_ais/cas/ca_assassin_move.lua
Expand Up @@ -5,12 +5,12 @@ local LS = wesnoth.require "location_set"
local function get_units_target(cfg)
local units = AH.get_units_with_moves {
side = wesnoth.current.side,
{ "and", H.get_child(cfg, "filter") }
{ "and", wml.get_child(cfg, "filter") }
}

local target = wesnoth.get_units {
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },
{ "and", H.get_child(cfg, "filter_second") }
{ "and", wml.get_child(cfg, "filter_second") }
}[1]

return units, target
Expand Down Expand Up @@ -55,7 +55,7 @@ function ca_assassin_move:execution(cfg)

local enemies = AH.get_visible_units(wesnoth.current.side, {
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },
{ "not", H.get_child(cfg, "filter_second") }
{ "not", wml.get_child(cfg, "filter_second") }
})

-- Maximum damage the enemies can theoretically do for all hexes they can attack
Expand Down Expand Up @@ -128,7 +128,7 @@ function ca_assassin_move:execution(cfg)
-- Preferred hexes (do this here once for all hexes, so that it does not need
-- to get done for every step of the a* search.
-- We only need to know whether a hex is preferred or not, there's no additional rating.
local prefer_slf = H.get_child(cfg, "prefer")
local prefer_slf = wml.get_child(cfg, "prefer")
local prefer_map -- want this to be nil, not empty LS if [prefer] tag not given
if prefer_slf then
local preferred_hexes = wesnoth.get_locations(prefer_slf)
Expand Down
8 changes: 4 additions & 4 deletions data/ai/micro_ais/cas/ca_big_animals.lua
Expand Up @@ -6,7 +6,7 @@ local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local function get_big_animals(cfg)
local big_animals = AH.get_units_with_moves {
side = wesnoth.current.side,
{ "and" , H.get_child(cfg, "filter") }
{ "and" , wml.get_child(cfg, "filter") }
}
return big_animals
end
Expand All @@ -24,7 +24,7 @@ function ca_big_animals:execution(cfg)

local unit = get_big_animals(cfg)[1]

local avoid_tag = H.get_child(cfg, "avoid_unit")
local avoid_tag = wml.get_child(cfg, "avoid_unit")
local avoid_map = LS.create()
if avoid_tag then
local enemies_to_be_avoided = AH.get_attackable_enemies(avoid_tag)
Expand All @@ -41,15 +41,15 @@ function ca_big_animals:execution(cfg)
-- Unit gets a new goal if none is set or on any move with a 10% random chance
local r = math.random(10)
if (not goal.goal_x) or (r == 1) then
local locs = AH.get_passable_locations(H.get_child(cfg, "filter_location") or {})
local locs = AH.get_passable_locations(wml.get_child(cfg, "filter_location") or {})
local rand = math.random(#locs)

goal.goal_x, goal.goal_y = locs[rand][1], locs[rand][2]
MAIUV.set_mai_unit_variables(unit, cfg.ai_id, goal)
end

local reach_map = AH.get_reachable_unocc(unit)
local wander_terrain = H.get_child(cfg, "filter_location_wander") or {}
local wander_terrain = wml.get_child(cfg, "filter_location_wander") or {}
reach_map:iter( function(x, y, v)
-- Remove tiles that do not comform to the wander terrain filter
if (not wesnoth.match_location(x, y, wander_terrain)) then
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_coward.lua
Expand Up @@ -2,7 +2,7 @@ local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"

local function get_coward(cfg)
local filter = H.get_child(cfg, "filter") or { id = cfg.id }
local filter = wml.get_child(cfg, "filter") or { id = cfg.id }
local coward = AH.get_units_with_moves {
side = wesnoth.current.side,
{ "and", filter }
Expand All @@ -23,7 +23,7 @@ function ca_coward:execution(cfg)
local reach = wesnoth.find_reach(coward)

local filter_second =
H.get_child(cfg, "filter_second")
wml.get_child(cfg, "filter_second")
or { { "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } } }
local enemies = AH.get_live_units {
{ "and", filter_second },
Expand Down
9 changes: 4 additions & 5 deletions data/ai/micro_ais/cas/ca_fast_attack_utils.lua
@@ -1,4 +1,3 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local T = wml.tag
Expand All @@ -22,7 +21,7 @@ function ca_fast_attack_utils.get_avoid_map(cfg)
-- Use [micro_ai][avoid] tag with priority over [ai][avoid].
-- If neither is given, return an empty location set.

local avoid_tag = H.get_child(cfg, "avoid")
local avoid_tag = wml.get_child(cfg, "avoid")

if not avoid_tag then
return LS.of_pairs(ai.aspects.avoid)
Expand Down Expand Up @@ -118,7 +117,7 @@ function ca_fast_attack_utils.single_unit_info(unit_proxy)
}

-- Include the ability type, such as: hides, heals, regenerate, skirmisher (set up as 'hides = true')
local abilities = H.get_child(unit_proxy.__cfg, "abilities")
local abilities = wml.get_child(unit_proxy.__cfg, "abilities")
if abilities then
for _,ability in ipairs(abilities) do
single_unit_info[ability[1]] = true
Expand All @@ -128,11 +127,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
5 changes: 2 additions & 3 deletions data/ai/micro_ais/cas/ca_fast_combat.lua
@@ -1,4 +1,3 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local FAU = wesnoth.require "ai/micro_ais/cas/ca_fast_attack_utils.lua"
local LS = wesnoth.require "location_set"
Expand All @@ -9,8 +8,8 @@ function ca_fast_combat:evaluation(cfg, data)
data.move_cache = { turn = wesnoth.current.turn }
data.gamedata = FAU.gamedata_setup()

local filter_own = H.get_child(cfg, "filter")
local filter_enemy = H.get_child(cfg, "filter_second")
local filter_own = wml.get_child(cfg, "filter")
local filter_enemy = wml.get_child(cfg, "filter_second")

local enemies
local units_sorted = true
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_fast_combat_leader.lua
Expand Up @@ -18,8 +18,8 @@ function ca_fast_combat_leader:evaluation(cfg, data)
data.move_cache = { turn = wesnoth.current.turn }
data.gamedata = FAU.gamedata_setup()

local filter_own = H.get_child(cfg, "filter")
local filter_enemy = H.get_child(cfg, "filter_second")
local filter_own = wml.get_child(cfg, "filter")
local filter_enemy = wml.get_child(cfg, "filter_second")

local enemies, leader
if (not filter_own) and (not filter_enemy) then
Expand Down
10 changes: 4 additions & 6 deletions data/ai/micro_ais/cas/ca_forest_animals_move.lua
@@ -1,5 +1,3 @@
local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
Expand Down Expand Up @@ -45,9 +43,9 @@ function ca_forest_animals_move:execution(cfg)
local enemies = AH.get_attackable_enemies()

-- Get the locations of all the rabbit holes
W.store_items { variable = 'holes_wml' }
local all_items = H.get_variable_array('holes_wml')
W.clear_variable { name = 'holes_wml' }
wesnoth.wml_actions.store_items { variable = 'holes_wml' }
local all_items = wml.array_access.get('holes_wml')
wesnoth.wml_actions.clear_variable { name = 'holes_wml' }

-- If cfg.rabbit_hole_img is set, only items with that image or halo count as holes
local holes
Expand All @@ -73,7 +71,7 @@ function ca_forest_animals_move:execution(cfg)
end

-- If no close enemies, do a random move
local wander_terrain = H.get_child(cfg, "filter_location") or {}
local wander_terrain = wml.get_child(cfg, "filter_location") or {}
if (not close_enemies[1]) then
local reach = AH.get_reachable_unocc(unit)
local width, height = wesnoth.get_map_size()
Expand Down
8 changes: 3 additions & 5 deletions data/ai/micro_ais/cas/ca_forest_animals_new_rabbit.lua
@@ -1,5 +1,3 @@
local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local T = wml.tag

Expand All @@ -18,9 +16,9 @@ function ca_forest_animals_new_rabbit:execution(cfg)
local rabbit_enemy_distance = cfg.rabbit_enemy_distance or 3

-- Get the locations of all items on that map (which could be rabbit holes)
W.store_items { variable = 'holes_wml' }
local all_items = H.get_variable_array('holes_wml')
W.clear_variable { name = 'holes_wml' }
wesnoth.wml_actions.store_items { variable = 'holes_wml' }
local all_items = wml.array_access.get('holes_wml')
wesnoth.wml_actions.clear_variable { name = 'holes_wml' }

-- Eliminate all holes that have an enemy within 'rabbit_enemy_distance' hexes
-- We also add a random number to the ones we keep, for selection of the holes later
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_goto.lua
Expand Up @@ -31,7 +31,7 @@ function ca_goto:evaluation(cfg, data)
local all_locs = wesnoth.get_locations {
x = '1-' .. width,
y = '1-' .. height,
{ "and", H.get_child(cfg, "filter_location") }
{ "and", wml.get_child(cfg, "filter_location") }
}
if (#all_locs == 0) then return 0 end

Expand Down Expand Up @@ -62,7 +62,7 @@ function ca_goto:evaluation(cfg, data)

local all_units = AH.get_units_with_moves {
side = wesnoth.current.side,
{ "and", H.get_child(cfg, "filter") }
{ "and", wml.get_child(cfg, "filter") }
}

local units = {}
Expand Down

0 comments on commit 2f611a9

Please sign in to comment.