Lua: Replace uses of deprecated (moved) unit functions

Also backtracked the removal of wesnoth.get_recall_units, which is now available as wesnoth.units.find_on_recall - it's just more convenient than passing x=recall to wesnoth.units.find
This commit is contained in:
Celtic Minstrel 2019-11-15 00:20:51 -05:00
parent 4dc986f028
commit 977f05d77f
83 changed files with 253 additions and 254 deletions

View file

@ -311,7 +311,7 @@ function ai_helper.robust_move_and_attack(ai, src, dst, target_loc, cfg)
local src_x, src_y = src.x or src[1], src.y or src[2] -- this works with units or locations local src_x, src_y = src.x or src[1], src.y or src[2] -- this works with units or locations
local dst_x, dst_y = dst.x or dst[1], dst.y or dst[2] local dst_x, dst_y = dst.x or dst[1], dst.y or dst[2]
local unit = wesnoth.get_unit(src_x, src_y) local unit = wesnoth.units.get(src_x, src_y)
if (not unit) then if (not unit) then
return ai_helper.dummy_check_action(false, false, 'robust_move_and_attack::NO_UNIT') return ai_helper.dummy_check_action(false, false, 'robust_move_and_attack::NO_UNIT')
end end
@ -320,7 +320,7 @@ function ai_helper.robust_move_and_attack(ai, src, dst, target_loc, cfg)
local target, target_x, target_y local target, target_x, target_y
if target_loc then if target_loc then
target_x, target_y = target_loc.x or target_loc[1], target_loc.y or target_loc[2] target_x, target_y = target_loc.x or target_loc[1], target_loc.y or target_loc[2]
target = wesnoth.get_unit(target_x, target_y) target = wesnoth.units.get(target_x, target_y)
if (not target) then if (not target) then
return ai_helper.dummy_check_action(false, false, 'robust_move_and_attack::NO_TARGET') return ai_helper.dummy_check_action(false, false, 'robust_move_and_attack::NO_TARGET')
@ -344,7 +344,7 @@ function ai_helper.robust_move_and_attack(ai, src, dst, target_loc, cfg)
else else
local unit_old_moves = unit.moves local unit_old_moves = unit.moves
local unit_in_way = wesnoth.get_unit(dst_x, dst_y) local unit_in_way = wesnoth.units.get(dst_x, dst_y)
if unit_in_way and (unit_in_way.side == wesnoth.current.side) and (unit_in_way.moves > 0) then if unit_in_way and (unit_in_way.side == wesnoth.current.side) and (unit_in_way.moves > 0) then
local uiw_old_moves = unit_in_way.moves local uiw_old_moves = unit_in_way.moves
ai_helper.move_unit_out_of_way(ai, unit_in_way, cfg) ai_helper.move_unit_out_of_way(ai, unit_in_way, cfg)
@ -363,7 +363,7 @@ function ai_helper.robust_move_and_attack(ai, src, dst, target_loc, cfg)
end end
-- Check whether dst hex is free now (an event could have done something funny) -- Check whether dst hex is free now (an event could have done something funny)
local unit_in_way = wesnoth.get_unit(dst_x, dst_y) local unit_in_way = wesnoth.units.get(dst_x, dst_y)
if unit_in_way then if unit_in_way then
return ai_helper.dummy_check_action(true, false, 'robust_move_and_attack::ANOTHER_UNIT_IN_WAY') return ai_helper.dummy_check_action(true, false, 'robust_move_and_attack::ANOTHER_UNIT_IN_WAY')
end end
@ -1058,7 +1058,7 @@ end
function ai_helper.get_live_units(filter) function ai_helper.get_live_units(filter)
-- Note: the order of the filters and the [and] tags are important for speed reasons -- Note: the order of the filters and the [and] tags are important for speed reasons
return wesnoth.get_units { { "not", { status = "petrified" } }, { "and", filter } } return wesnoth.units.find { { "not", { status = "petrified" } }, { "and", filter } }
end end
function ai_helper.get_units_with_moves(filter, exclude_guardians) function ai_helper.get_units_with_moves(filter, exclude_guardians)
@ -1068,7 +1068,7 @@ function ai_helper.get_units_with_moves(filter, exclude_guardians)
if exclude_guardians then if exclude_guardians then
exclude_status = exclude_status .. ',guardian' exclude_status = exclude_status .. ',guardian'
end end
return wesnoth.get_units { return wesnoth.units.find {
{ "and", { formula = "moves > 0" } }, { "and", { formula = "moves > 0" } },
{ "not", { status = exclude_status } }, { "not", { status = exclude_status } },
{ "and", filter } { "and", filter }
@ -1077,7 +1077,7 @@ end
function ai_helper.get_units_with_attacks(filter) function ai_helper.get_units_with_attacks(filter)
-- Note: the order of the filters and the [and] tags are important for speed reasons -- Note: the order of the filters and the [and] tags are important for speed reasons
return wesnoth.get_units { return wesnoth.units.find {
{ "and", { formula = "attacks_left > 0 and size(attacks) > 0" } }, { "and", { formula = "attacks_left > 0 and size(attacks) > 0" } },
{ "not", { status = "petrified" } }, { "not", { status = "petrified" } },
{ "and", filter } { "and", filter }
@ -1109,7 +1109,7 @@ function ai_helper.get_visible_units(viewing_side, filter)
end end
local units = {} local units = {}
local all_units = wesnoth.get_units() local all_units = wesnoth.units.find()
for _,unit in ipairs(all_units) do for _,unit in ipairs(all_units) do
if unit:matches(filter_plus_vision) then if unit:matches(filter_plus_vision) then
table.insert(units, unit) table.insert(units, unit)
@ -1169,7 +1169,7 @@ function ai_helper.get_attackable_enemies(filter, side, cfg)
end end
local enemies = {} local enemies = {}
local all_units = wesnoth.get_units() local all_units = wesnoth.units.find()
for _,unit in ipairs(all_units) do for _,unit in ipairs(all_units) do
if wesnoth.is_enemy(side, unit.side) if wesnoth.is_enemy(side, unit.side)
and (not unit.status.petrified) and (not unit.status.petrified)
@ -1221,7 +1221,7 @@ function ai_helper.get_closest_enemy(loc, side, cfg)
local x, y local x, y
if not loc then if not loc then
local leader = wesnoth.get_units { side = side, canrecruit = 'yes' }[1] local leader = wesnoth.units.find { side = side, canrecruit = 'yes' }[1]
x, y = leader.x, leader.y x, y = leader.x, leader.y
else else
x, y = loc[1], loc[2] x, y = loc[1], loc[2]
@ -1316,7 +1316,7 @@ function ai_helper.get_dst_src(units, cfg)
-- all parameters for wesnoth.find_reach -- all parameters for wesnoth.find_reach
if (not units) then if (not units) then
units = wesnoth.get_units { side = wesnoth.current.side } units = wesnoth.units.find { side = wesnoth.current.side }
end end
return ai_helper.get_dst_src_units(units, cfg) return ai_helper.get_dst_src_units(units, cfg)
@ -1328,7 +1328,7 @@ function ai_helper.get_enemy_dst_src(enemies, cfg)
-- all parameters for wesnoth.find_reach -- all parameters for wesnoth.find_reach
if (not enemies) then if (not enemies) then
enemies = wesnoth.get_units { enemies = wesnoth.units.find {
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side} } } } { "filter_side", { { "enemy_of", { side = wesnoth.current.side} } } }
} }
end end
@ -1405,7 +1405,7 @@ function ai_helper.next_hop(unit, x, y, cfg)
-- Check for unit in way only if cfg.ignore_units is not set -- Check for unit in way only if cfg.ignore_units is not set
local unit_in_way local unit_in_way
if (not cfg) or (not cfg.ignore_units) then if (not cfg) or (not cfg.ignore_units) then
unit_in_way = wesnoth.get_unit(path[i][1], path[i][2]) unit_in_way = wesnoth.units.get(path[i][1], path[i][2])
-- If ignore_own_units is set, ignore own side units that can move out of the way -- If ignore_own_units is set, ignore own side units that can move out of the way
if cfg and cfg.ignore_own_units then if cfg and cfg.ignore_own_units then
@ -1442,7 +1442,7 @@ function ai_helper.can_reach(unit, x, y, cfg)
local viewing_side = cfg.viewing_side or unit.side local viewing_side = cfg.viewing_side or unit.side
-- Is there a unit at the goal hex? -- Is there a unit at the goal hex?
local unit_in_way = wesnoth.get_unit(x, y) local unit_in_way = wesnoth.units.get(x, y)
if (cfg.exclude_occupied) if (cfg.exclude_occupied)
and unit_in_way and ai_helper.is_visible_unit(viewing_side, unit_in_way) and unit_in_way and ai_helper.is_visible_unit(viewing_side, unit_in_way)
then then
@ -1509,7 +1509,7 @@ function ai_helper.get_reachmap(unit, cfg)
if cfg and cfg.avoid_map and cfg.avoid_map:get(loc[1], loc[2]) then if cfg and cfg.avoid_map and cfg.avoid_map:get(loc[1], loc[2]) then
is_available = false is_available = false
else else
local unit_in_way = wesnoth.get_unit(loc[1], loc[2]) local unit_in_way = wesnoth.units.get(loc[1], loc[2])
if unit_in_way and (unit_in_way.id ~= unit.id) and ai_helper.is_visible_unit(viewing_side, unit_in_way) then if unit_in_way and (unit_in_way.id ~= unit.id) and ai_helper.is_visible_unit(viewing_side, unit_in_way) then
if cfg and cfg.exclude_occupied then if cfg and cfg.exclude_occupied then
is_available = false is_available = false
@ -1558,7 +1558,7 @@ function ai_helper.find_path_with_shroud(unit, x, y, cfg)
if wesnoth.sides[viewing_side] and wesnoth.sides[viewing_side].shroud then if wesnoth.sides[viewing_side] and wesnoth.sides[viewing_side].shroud then
local extracted_units = {} local extracted_units = {}
if (not cfg) or (not cfg.ignore_units) then if (not cfg) or (not cfg.ignore_units) then
local all_units = wesnoth.get_units() local all_units = wesnoth.units.find()
for _,u in ipairs(all_units) do for _,u in ipairs(all_units) do
if (u.side ~= viewing_side) if (u.side ~= viewing_side)
and (not ai_helper.is_visible_unit(viewing_side, u)) and (not ai_helper.is_visible_unit(viewing_side, u))
@ -1651,7 +1651,7 @@ function ai_helper.move_unit_out_of_way(ai, unit, cfg)
local max_rating, best_hex = - math.huge local max_rating, best_hex = - math.huge
for _,loc in ipairs(reach) do for _,loc in ipairs(reach) do
local unit_in_way = wesnoth.get_unit(loc[1], loc[2]) local unit_in_way = wesnoth.units.get(loc[1], loc[2])
if (not unit_in_way) -- also excludes current hex if (not unit_in_way) -- also excludes current hex
or (not ai_helper.is_visible_unit(viewing_side, unit_in_way)) or (not ai_helper.is_visible_unit(viewing_side, unit_in_way))
then then
@ -1716,7 +1716,7 @@ function ai_helper.movefull_outofway_stopunit(ai, unit, x, y, cfg)
-- Only move unit out of way if the main unit can get there -- Only move unit out of way if the main unit can get there
local path, cost = ai_helper.find_path_with_shroud(unit, x, y, cfg) local path, cost = ai_helper.find_path_with_shroud(unit, x, y, cfg)
if (cost <= unit.moves) then if (cost <= unit.moves) then
local unit_in_way = wesnoth.get_unit(x, y) local unit_in_way = wesnoth.units.get(x, y)
if unit_in_way and (unit_in_way ~= unit) if unit_in_way and (unit_in_way ~= unit)
and ai_helper.is_visible_unit(viewing_side, unit_in_way) and ai_helper.is_visible_unit(viewing_side, unit_in_way)
then then
@ -1777,7 +1777,7 @@ function ai_helper.get_attacks(units, cfg)
-- Note: the remainder is optimized for speed, so we only get_units once, -- Note: the remainder is optimized for speed, so we only get_units once,
-- do not use WML filters, etc. -- do not use WML filters, etc.
local all_units = wesnoth.get_units() local all_units = wesnoth.units.find()
local enemy_map, my_unit_map, other_unit_map = LS.create(), LS.create(), LS.create() local enemy_map, my_unit_map, other_unit_map = LS.create(), LS.create(), LS.create()
for i,unit in ipairs(all_units) do for i,unit in ipairs(all_units) do
@ -1848,7 +1848,7 @@ function ai_helper.get_attacks(units, cfg)
-- unit that is moving out of the way of the initial unit (etc.). -- unit that is moving out of the way of the initial unit (etc.).
for _,uiw_loc in ipairs(uiw_reach) do for _,uiw_loc in ipairs(uiw_reach) do
-- Unit in the way of the unit in the way -- Unit in the way of the unit in the way
local uiw_uiw = wesnoth.get_unit(uiw_loc[1], uiw_loc[2]) local uiw_uiw = wesnoth.units.get(uiw_loc[1], uiw_loc[2])
if (not uiw_uiw) or (not ai_helper.is_visible_unit(viewing_side, uiw_uiw)) then if (not uiw_uiw) or (not ai_helper.is_visible_unit(viewing_side, uiw_uiw)) then
add_target = true add_target = true
break break
@ -1990,7 +1990,7 @@ function ai_helper.get_attack_combos(units, enemy, cfg)
-- TODO: generalize it so that it works not only for units with moves=0, but blocked units etc. -- TODO: generalize it so that it works not only for units with moves=0, but blocked units etc.
local blocked_hexes = LS.create() local blocked_hexes = LS.create()
if units[1] and (units[1].side == wesnoth.current.side) then if units[1] and (units[1].side == wesnoth.current.side) then
local all_units = wesnoth.get_units { side = wesnoth.current.side } local all_units = wesnoth.units.find { side = wesnoth.current.side }
for _,unit in ipairs(all_units) do for _,unit in ipairs(all_units) do
if (unit.moves == 0) then if (unit.moves == 0) then
blocked_hexes:insert(unit.x, unit.y) blocked_hexes:insert(unit.x, unit.y)

View file

@ -791,7 +791,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- We also need the leader (well, the location at least) -- We also need the leader (well, the location at least)
-- because if there's no other difference, prefer location _between_ the leader and the defender -- because if there's no other difference, prefer location _between_ the leader and the defender
local leader = wesnoth.get_units { side = attacker.side, canrecruit = 'yes' }[1] local leader = wesnoth.units.find { side = attacker.side, canrecruit = 'yes' }[1]
------ All the attacker contributions: ------ ------ All the attacker contributions: ------
-- Add up rating for the attacking unit -- Add up rating for the attacking unit
@ -815,7 +815,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- Equivalently, if attack is adjacent to an unoccupied healing location, that's bad -- Equivalently, if attack is adjacent to an unoccupied healing location, that's bad
for xa,ya in H.adjacent_tiles(dst[1], dst[2]) do for xa,ya in H.adjacent_tiles(dst[1], dst[2]) do
local healing = wesnoth.get_terrain_info(wesnoth.get_terrain(xa, ya)).healing local healing = wesnoth.get_terrain_info(wesnoth.get_terrain(xa, ya)).healing
if (healing > 0) and (not wesnoth.get_unit(xa, ya)) then if (healing > 0) and (not wesnoth.units.get(xa, ya)) then
damage = damage + 1.25 * healing damage = damage + 1.25 * healing
end end
end end
@ -939,7 +939,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- Add a very small penalty for attack hexes occupied by other units -- Add a very small penalty for attack hexes occupied by other units
-- Note: it must be checked previously that the unit on the hex can move away -- Note: it must be checked previously that the unit on the hex can move away
if (dst[1] ~= attacker.x) or (dst[2] ~= attacker.y) then if (dst[1] ~= attacker.x) or (dst[2] ~= attacker.y) then
if wesnoth.get_unit(dst[1], dst[2]) then if wesnoth.units.get(dst[1], dst[2]) then
defender_value = defender_value + occupied_hex_penalty defender_value = defender_value + occupied_hex_penalty
end end
end end
@ -1159,7 +1159,7 @@ function battle_calcs.get_attack_map_unit(unit, cfg)
-- MP left off the map (for enemy pathfinding) -- MP left off the map (for enemy pathfinding)
local units_MP = {} local units_MP = {}
if (unit.side ~= wesnoth.current.side) then if (unit.side ~= wesnoth.current.side) then
local all_units = wesnoth.get_units { side = wesnoth.current.side } local all_units = wesnoth.units.find { side = wesnoth.current.side }
for _,unit in ipairs(all_units) do for _,unit in ipairs(all_units) do
if (unit.moves > 0) then if (unit.moves > 0) then
table.insert(units_MP, unit) table.insert(units_MP, unit)
@ -1466,7 +1466,7 @@ function battle_calcs.get_attack_combos_subset(units, enemy, cfg)
local blocked_hexes = LS.create() local blocked_hexes = LS.create()
if units[1] and (units[1].side == wesnoth.current.side) then if units[1] and (units[1].side == wesnoth.current.side) then
for xa,ya in H.adjacent_tiles(enemy.x, enemy.y) do for xa,ya in H.adjacent_tiles(enemy.x, enemy.y) do
local unit_in_way = wesnoth.get_unit(xa, ya) local unit_in_way = wesnoth.units.get(xa, ya)
if unit_in_way then if unit_in_way then
-- Units on the same side are blockers if they cannot move away -- Units on the same side are blockers if they cannot move away
if (unit_in_way.side == wesnoth.current.side) then if (unit_in_way.side == wesnoth.current.side) then

View file

@ -35,7 +35,7 @@ function ca_castle_switch:evaluation(cfg, data)
return 0 return 0
end end
local leader = wesnoth.get_units { local leader = wesnoth.units.find {
side = wesnoth.current.side, side = wesnoth.current.side,
canrecruit = 'yes', canrecruit = 'yes',
formula = '(movement_left = total_movement) and (hitpoints = max_hitpoints)' formula = '(movement_left = total_movement) and (hitpoints = max_hitpoints)'
@ -168,7 +168,7 @@ function ca_castle_switch:evaluation(cfg, data)
} }
local should_wait = false local should_wait = false
for i,loc in ipairs(castle) do for i,loc in ipairs(castle) do
local unit = wesnoth.get_unit(loc[1], loc[2]) local unit = wesnoth.units.get(loc[1], loc[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit)) then if (not AH.is_visible_unit(wesnoth.current.side, unit)) then
should_wait = false should_wait = false
break break
@ -190,7 +190,7 @@ function ca_castle_switch:evaluation(cfg, data)
end end
function ca_castle_switch:execution(cfg, data) function ca_castle_switch:execution(cfg, data)
local leader = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1] local leader = wesnoth.units.find { side = wesnoth.current.side, canrecruit = 'yes' }[1]
if AH.print_exec() then AH.print_ts(' Executing castle_switch CA') end if AH.print_exec() then AH.print_ts(' Executing castle_switch CA') end
if AH.show_messages() then wesnoth.wml_actions.message { speaker = leader.id, message = 'Switching castles' } end if AH.show_messages() then wesnoth.wml_actions.message { speaker = leader.id, message = 'Switching castles' } end

View file

@ -49,7 +49,7 @@ function ca_grab_villages:evaluation(cfg, data)
-- First collect all information that only depends on the village -- First collect all information that only depends on the village
local village_rating = 0 -- This is the unit independent rating local village_rating = 0 -- This is the unit independent rating
local unit_in_way = wesnoth.get_unit(v[1], v[2]) local unit_in_way = wesnoth.units.get(v[1], v[2])
-- If an enemy can get within one move of the village, we want to hold it -- If an enemy can get within one move of the village, we want to hold it
if enemy_attack_map:get(v[1], v[2]) then if enemy_attack_map:get(v[1], v[2]) then

View file

@ -97,7 +97,7 @@ function ca_attack_highxp:evaluation(cfg, data)
local attack_hexes = LS.create() local attack_hexes = LS.create()
for xa,ya in H.adjacent_tiles(target.x, target.y) do for xa,ya in H.adjacent_tiles(target.x, target.y) do
if (not avoid_map:get(xa, ya)) then if (not avoid_map:get(xa, ya)) then
local unit_in_way = wesnoth.get_unit(xa, ya) local unit_in_way = wesnoth.units.get(xa, ya)
if AH.is_visible_unit(wesnoth.current.side, unit_in_way) then if AH.is_visible_unit(wesnoth.current.side, unit_in_way) then
if (unit_in_way.side == wesnoth.current.side) then if (unit_in_way.side == wesnoth.current.side) then
@ -115,7 +115,7 @@ function ca_attack_highxp:evaluation(cfg, data)
local can_move = false local can_move = false
for _,uiw_loc in ipairs(uiw_reach) do for _,uiw_loc in ipairs(uiw_reach) do
-- Unit in the way of the unit in the way -- Unit in the way of the unit in the way
local uiw_uiw = wesnoth.get_unit(uiw_loc[1], uiw_loc[2]) local uiw_uiw = wesnoth.units.get(uiw_loc[1], uiw_loc[2])
if (not AH.is_visible_unit(wesnoth.current.side, uiw_uiw)) then if (not AH.is_visible_unit(wesnoth.current.side, uiw_uiw)) then
can_move = true can_move = true
break break

View file

@ -40,8 +40,8 @@ function ca_spread_poison:evaluation(cfg, data)
local max_rating, best_attack = - math.huge local max_rating, best_attack = - math.huge
for i,a in ipairs(attacks) do for i,a in ipairs(attacks) do
if (not avoid_map:get(a.dst.x, a.dst.y)) then if (not avoid_map:get(a.dst.x, a.dst.y)) then
local attacker = wesnoth.get_unit(a.src.x, a.src.y) local attacker = wesnoth.units.get(a.src.x, a.src.y)
local defender = wesnoth.get_unit(a.target.x, a.target.y) local defender = wesnoth.units.get(a.target.x, a.target.y)
-- Don't try to poison a unit that cannot be poisoned -- Don't try to poison a unit that cannot be poisoned
local cant_poison = defender.status.poisoned or defender.status.unpoisonable local cant_poison = defender.status.poisoned or defender.status.unpoisonable
@ -93,7 +93,7 @@ function ca_spread_poison:evaluation(cfg, data)
end end
function ca_spread_poison:execution(cfg, data) function ca_spread_poison:execution(cfg, data)
local attacker = wesnoth.get_unit(SP_attack.src.x, SP_attack.src.y) local attacker = wesnoth.units.get(SP_attack.src.x, SP_attack.src.y)
-- If several attacks have poison, this will always find the last one -- If several attacks have poison, this will always find the last one
local is_poisoner, poison_weapon = AH.has_weapon_special(attacker, "poison") local is_poisoner, poison_weapon = AH.has_weapon_special(attacker, "poison")

View file

@ -45,7 +45,7 @@ return {
-- gained from regeneration -- gained from regeneration
local effective_hp = wesnoth.unit_types[recruit_id].max_hitpoints local effective_hp = wesnoth.unit_types[recruit_id].max_hitpoints
local unit = wesnoth.create_unit { local unit = wesnoth.units.create {
type = recruit_id, type = recruit_id,
random_traits = false, random_traits = false,
name = "X", name = "X",
@ -250,7 +250,7 @@ return {
return analysis[ally_type] return analysis[ally_type]
end end
local unit = wesnoth.create_unit { local unit = wesnoth.units.create {
type = enemy_type, type = enemy_type,
random_traits = false, random_traits = false,
name = "X", name = "X",
@ -260,7 +260,7 @@ return {
local flat_defense = unit:defense("Gt") local flat_defense = unit:defense("Gt")
local best_defense = get_best_defense(unit) local best_defense = get_best_defense(unit)
local recruit = wesnoth.create_unit { local recruit = wesnoth.units.create {
type = ally_type, type = ally_type,
random_traits = false, random_traits = false,
name = "X", name = "X",
@ -336,7 +336,7 @@ return {
function do_recruit_eval(data) function do_recruit_eval(data)
-- Check if leader is on keep -- Check if leader is on keep
local leader = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1] local leader = wesnoth.units.find { side = wesnoth.current.side, canrecruit = 'yes' }[1]
if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then
return 0 return 0
@ -352,7 +352,7 @@ return {
get_current_castle(leader, data) get_current_castle(leader, data)
local no_space = true local no_space = true
for i,c in ipairs(data.castle.locs) do for i,c in ipairs(data.castle.locs) do
local unit = wesnoth.get_unit(c[1], c[2]) local unit = wesnoth.units.get(c[1], c[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit)) then if (not AH.is_visible_unit(wesnoth.current.side, unit)) then
no_space = false no_space = false
break break
@ -566,7 +566,7 @@ return {
end end
local recruit_type local recruit_type
local leader = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1] local leader = wesnoth.units.find { side = wesnoth.current.side, canrecruit = 'yes' }[1]
repeat repeat
recruit_data.recruit.best_hex, recruit_data.recruit.target_hex = ai_cas:find_best_recruit_hex(leader, recruit_data) recruit_data.recruit.best_hex, recruit_data.recruit.target_hex = ai_cas:find_best_recruit_hex(leader, recruit_data)
recruit_type = ai_cas:find_best_recruit(attack_type_count, unit_attack_type_count, recruit_effectiveness, recruit_vulnerability, attack_range_count, unit_attack_range_count, most_common_range_count) recruit_type = ai_cas:find_best_recruit(attack_type_count, unit_attack_type_count, recruit_effectiveness, recruit_vulnerability, attack_range_count, unit_attack_range_count, most_common_range_count)
@ -577,7 +577,7 @@ return {
-- If the recruited unit cannot reach the target hex, return it to the pool of targets -- If the recruited unit cannot reach the target hex, return it to the pool of targets
if recruit_data.recruit.target_hex and recruit_data.recruit.target_hex[1] then if recruit_data.recruit.target_hex and recruit_data.recruit.target_hex[1] then
local unit = wesnoth.get_unit(recruit_data.recruit.best_hex[1], recruit_data.recruit.best_hex[2]) local unit = wesnoth.units.get(recruit_data.recruit.best_hex[1], recruit_data.recruit.best_hex[2])
local path, cost = wesnoth.find_path(unit, recruit_data.recruit.target_hex[1], recruit_data.recruit.target_hex[2], {viewing_side=0, max_cost=unit.max_moves+1}) local path, cost = wesnoth.find_path(unit, recruit_data.recruit.target_hex[1], recruit_data.recruit.target_hex[2], {viewing_side=0, max_cost=unit.max_moves+1})
if cost > unit.max_moves then if cost > unit.max_moves then
-- The last village added to the list should be the one we tried to aim for, check anyway -- The last village added to the list should be the one we tried to aim for, check anyway
@ -634,7 +634,7 @@ return {
for i,c in ipairs(data.castle.locs) do for i,c in ipairs(data.castle.locs) do
local rating = 0 local rating = 0
local unit = wesnoth.get_unit(c[1], c[2]) local unit = wesnoth.units.get(c[1], c[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit)) then if (not AH.is_visible_unit(wesnoth.current.side, unit)) then
for j,e in ipairs(enemy_leaders) do for j,e in ipairs(enemy_leaders) do
rating = rating + 1 / M.distance_between(c[1], c[2], e.x, e.y) ^ 2. rating = rating + 1 / M.distance_between(c[1], c[2], e.x, e.y) ^ 2.
@ -713,7 +713,7 @@ return {
-- Use time to enemy to encourage recruiting fast units when the opponent is far away (game is beginning or we're winning) -- Use time to enemy to encourage recruiting fast units when the opponent is far away (game is beginning or we're winning)
-- Base distance on -- Base distance on
local recruit_unit = wesnoth.create_unit { local recruit_unit = wesnoth.units.create {
type = recruit_id, type = recruit_id,
x = best_hex[1], x = best_hex[1],
y = best_hex[2], y = best_hex[2],
@ -953,7 +953,7 @@ return {
for _,loc in ipairs(data.castle.locs) do for _,loc in ipairs(data.castle.locs) do
local dist = M.distance_between(v[1], v[2], loc[1], loc[2]) local dist = M.distance_between(v[1], v[2], loc[1], loc[2])
if (dist <= fastest_unit_speed) then if (dist <= fastest_unit_speed) then
if (not wesnoth.get_unit(loc[1], loc[2])) then if (not wesnoth.units.get(loc[1], loc[2])) then
table.insert(close_castle_hexes, loc) table.insert(close_castle_hexes, loc)
end end
end end
@ -1031,7 +1031,7 @@ return {
movetypes[movetype] = wesnoth.unit_types[id].max_moves movetypes[movetype] = wesnoth.unit_types[id].max_moves
end end
num_recruits = num_recruits + 1 num_recruits = num_recruits + 1
test_units[num_recruits] = wesnoth.create_unit({ test_units[num_recruits] = wesnoth.units.create({
type = id, type = id,
side = wesnoth.current.side, side = wesnoth.current.side,
random_traits = false, random_traits = false,

View file

@ -5,7 +5,7 @@ function patrol_gen(n, wp)
-- n is the name of the unit, like Kiressh -- n is the name of the unit, like Kiressh
-- wp - a table of waypoint tables of form {x,y} -- wp - a table of waypoint tables of form {x,y}
local unit = wesnoth.get_units({name=n})[1] local unit = wesnoth.units.find({name=n})[1]
local x, y = unit.x, unit.y local x, y = unit.x, unit.y
local wpn = 1 --WayPoint Number - we have to remember which waypoint we are heading to local wpn = 1 --WayPoint Number - we have to remember which waypoint we are heading to

View file

@ -167,7 +167,7 @@ function retreat_functions.get_retreat_injured_units(healees, regen_amounts)
base_rating = base_rating * 1000 base_rating = base_rating * 1000
for j,loc in ipairs(possible_locations) do for j,loc in ipairs(possible_locations) do
local unit_in_way = wesnoth.get_unit(loc[1], loc[2]) local unit_in_way = wesnoth.units.get(loc[1], loc[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or ((unit_in_way.moves > 0) and (unit_in_way.side == wesnoth.current.side)) or ((unit_in_way.moves > 0) and (unit_in_way.side == wesnoth.current.side))
then then

View file

@ -8,7 +8,7 @@ local function get_units_target(cfg)
{ "and", wml.get_child(cfg, "filter") } { "and", wml.get_child(cfg, "filter") }
} }
local target = wesnoth.get_units { local target = wesnoth.units.find {
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } }, { "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },
{ "and", wml.get_child(cfg, "filter_second") } { "and", wml.get_child(cfg, "filter_second") }
}[1] }[1]
@ -151,7 +151,7 @@ function ca_assassin_move:execution(cfg)
for i = 2,#path do for i = 2,#path do
local sub_path, sub_cost = AH.find_path_with_shroud(unit, path[i][1], path[i][2]) local sub_path, sub_cost = AH.find_path_with_shroud(unit, path[i][1], path[i][2])
if sub_cost <= unit.moves then if sub_cost <= unit.moves then
local unit_in_way = wesnoth.get_unit(path[i][1], path[i][2]) local unit_in_way = wesnoth.units.get(path[i][1], path[i][2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then
farthest_hex = path[i] farthest_hex = path[i]
end end

View file

@ -65,7 +65,7 @@ function ca_big_animals:execution(cfg)
-- Proximity to an enemy unit is a plus -- Proximity to an enemy unit is a plus
local enemy_hp = 500 local enemy_hp = 500
for xa,ya in H.adjacent_tiles(x, y) do for xa,ya in H.adjacent_tiles(x, y) do
local enemy = wesnoth.get_unit(xa, ya) local enemy = wesnoth.units.get(xa, ya)
if AH.is_attackable_enemy(enemy) then if AH.is_attackable_enemy(enemy) then
if (enemy.hitpoints < enemy_hp) then enemy_hp = enemy.hitpoints end if (enemy.hitpoints < enemy_hp) then enemy_hp = enemy.hitpoints end
end end
@ -97,7 +97,7 @@ function ca_big_animals:execution(cfg)
-- Finally, if the unit ended up next to enemies, attack the weakest of those -- Finally, if the unit ended up next to enemies, attack the weakest of those
local min_hp, target = math.huge local min_hp, target = math.huge
for xa,ya in H.adjacent_tiles(unit.x, unit.y) do for xa,ya in H.adjacent_tiles(unit.x, unit.y) do
local enemy = wesnoth.get_unit(xa, ya) local enemy = wesnoth.units.get(xa, ya)
if AH.is_attackable_enemy(enemy) then if AH.is_attackable_enemy(enemy) then
if (enemy.hitpoints < min_hp) then if (enemy.hitpoints < min_hp) then
min_hp, target = enemy.hitpoints, enemy min_hp, target = enemy.hitpoints, enemy

View file

@ -16,8 +16,8 @@ local function bottleneck_is_my_territory(map, enemy_map)
-- Get copy of leader to do pathfinding from each hex to the -- Get copy of leader to do pathfinding from each hex to the
-- front-line hexes, both own (stored in @map) and enemy (@enemy_map) front-line hexes -- front-line hexes, both own (stored in @map) and enemy (@enemy_map) front-line hexes
-- If there is no leader, use first unit found -- If there is no leader, use first unit found
local unit = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1] local unit = wesnoth.units.find { side = wesnoth.current.side, canrecruit = 'yes' }[1]
if (not unit) then unit = wesnoth.get_units { side = wesnoth.current.side }[1] end if (not unit) then unit = wesnoth.units.find { side = wesnoth.current.side }[1] end
local dummy_unit = unit:clone() local dummy_unit = unit:clone()
local territory_map = LS.create() local territory_map = LS.create()
@ -140,7 +140,7 @@ local function bottleneck_get_rating(unit, x, y, has_leadership, is_healer, data
-- If leadership unit is injured -> prefer hexes next to healers -- If leadership unit is injured -> prefer hexes next to healers
if (unit.hitpoints < unit.max_hitpoints) then if (unit.hitpoints < unit.max_hitpoints) then
for xa,ya in H.adjacent_tiles(x, y) do for xa,ya in H.adjacent_tiles(x, y) do
local adjacent_unit = wesnoth.get_unit(xa, ya) local adjacent_unit = wesnoth.units.get(xa, ya)
if adjacent_unit and (adjacent_unit.__cfg.usage == "healer") then if adjacent_unit and (adjacent_unit.__cfg.usage == "healer") then
leadership_rating = leadership_rating + 100 leadership_rating = leadership_rating + 100
break break
@ -269,7 +269,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
-- Healing map: positions next to healers -- Healing map: positions next to healers
-- Healers get moved with higher priority, so don't need to check their MP -- Healers get moved with higher priority, so don't need to check their MP
local healers = wesnoth.get_units { side = wesnoth.current.side, ability = "healing" } local healers = wesnoth.units.find { side = wesnoth.current.side, ability = "healing" }
BD_healing_map = LS.create() BD_healing_map = LS.create()
for _,healer in ipairs(healers) do for _,healer in ipairs(healers) do
for xa,ya in H.adjacent_tiles(healer.x, healer.y) do for xa,ya in H.adjacent_tiles(healer.x, healer.y) do
@ -294,7 +294,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
-- 1. the rating of the unit on the target hex (if there is one) -- 1. the rating of the unit on the target hex (if there is one)
-- 2. the rating of the currently considered unit on its current hex -- 2. the rating of the currently considered unit on its current hex
local all_units = wesnoth.get_units { side = wesnoth.current.side } local all_units = wesnoth.units.find { side = wesnoth.current.side }
local current_rating_map = LS.create() local current_rating_map = LS.create()
for _,unit in ipairs(all_units) do for _,unit in ipairs(all_units) do
@ -317,7 +317,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
for _,enemy in ipairs(enemies) do for _,enemy in ipairs(enemies) do
for xa,ya in H.adjacent_tiles(enemy.x, enemy.y) do for xa,ya in H.adjacent_tiles(enemy.x, enemy.y) do
if BD_is_my_territory:get(xa, ya) then if BD_is_my_territory:get(xa, ya) then
local unit_in_way = wesnoth.get_unit(xa, ya) local unit_in_way = wesnoth.units.get(xa, ya)
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then
unit_in_way = nil unit_in_way = nil
end end
@ -433,7 +433,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
BD_bottleneck_moves_done = true BD_bottleneck_moves_done = true
else else
-- If there's another unit in the best location, moving it out of the way becomes the best move -- If there's another unit in the best location, moving it out of the way becomes the best move
local unit_in_way = wesnoth.get_units { x = best_hex[1], y = best_hex[2], local unit_in_way = wesnoth.units.find { x = best_hex[1], y = best_hex[2],
{ "not", { id = best_unit.id } } { "not", { id = best_unit.id } }
}[1] }[1]
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then

View file

@ -40,7 +40,7 @@ function ca_coward:execution(cfg)
for i,hex in ipairs(reach) do for i,hex in ipairs(reach) do
-- Only consider unoccupied hexes -- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(hex[1], hex[2]) local unit_in_way = wesnoth.units.get(hex[1], hex[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == coward) or (unit_in_way == coward)
then then
@ -98,7 +98,7 @@ function ca_coward:execution(cfg)
if cfg.attack_if_trapped then if cfg.attack_if_trapped then
local max_rating, best_target = - math.huge local max_rating, best_target = - math.huge
for xa,ya in H.adjacent_tiles(coward.x, coward.y) do for xa,ya in H.adjacent_tiles(coward.x, coward.y) do
local target = wesnoth.get_unit(xa, ya) local target = wesnoth.units.get(xa, ya)
if target and wesnoth.is_enemy(coward.side, target.side) then if target and wesnoth.is_enemy(coward.side, target.side) then
local rating = - target.hitpoints local rating = - target.hitpoints

View file

@ -134,7 +134,7 @@ function ca_fast_attack_utils.get_unit_copy(id, gamedata)
-- already, otherwise store in there -- already, otherwise store in there
if (not gamedata.unit_copies[id]) then if (not gamedata.unit_copies[id]) then
local unit_proxy = wesnoth.get_units { id = id }[1] local unit_proxy = wesnoth.units.find { id = id }[1]
gamedata.unit_copies[id] = unit_proxy:clone() gamedata.unit_copies[id] = unit_proxy:clone()
end end

View file

@ -92,7 +92,7 @@ function ca_fast_combat:evaluation(cfg, data)
if enemy_map:get(attack.target.x, attack.target.y) if enemy_map:get(attack.target.x, attack.target.y)
and (not avoid_map:get(attack.dst.x, attack.dst.y)) and (not avoid_map:get(attack.dst.x, attack.dst.y))
then then
local target = wesnoth.get_unit(attack.target.x, attack.target.y) local target = wesnoth.units.get(attack.target.x, attack.target.y)
local target_info = FAU.get_unit_info(target, gamedata) local target_info = FAU.get_unit_info(target, gamedata)
local att_stat, def_stat = FAU.battle_outcome( local att_stat, def_stat = FAU.battle_outcome(

View file

@ -151,7 +151,7 @@ function ca_fast_combat_leader:evaluation(cfg, data)
end end
if acceptable_attack then if acceptable_attack then
local target = wesnoth.get_unit(attack.target.x, attack.target.y) local target = wesnoth.units.get(attack.target.x, attack.target.y)
local target_info = FAU.get_unit_info(target, gamedata) local target_info = FAU.get_unit_info(target, gamedata)
local att_stat, def_stat = FAU.battle_outcome( local att_stat, def_stat = FAU.battle_outcome(

View file

@ -23,7 +23,7 @@ function ca_fast_move:execution(cfg)
if (not unit.status.guardian) then table.insert(units, unit) end if (not unit.status.guardian) then table.insert(units, unit) end
end end
local leader = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1] local leader = wesnoth.units.find { side = wesnoth.current.side, canrecruit = 'yes' }[1]
local goals = {} local goals = {}
@ -206,7 +206,7 @@ function ca_fast_move:execution(cfg)
local unit_in_way local unit_in_way
if (rating > max_rating) then if (rating > max_rating) then
unit_in_way = wesnoth.get_unit(loc[1], loc[2]) unit_in_way = wesnoth.units.get(loc[1], loc[2])
if (unit_in_way == unit) or (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then if (unit_in_way == unit) or (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then
unit_in_way = nil unit_in_way = nil
end end

View file

@ -12,7 +12,7 @@ local function get_forest_animals(cfg)
} }
local tusker_type = cfg.tusker_type or "no_unit_of_this_type" local tusker_type = cfg.tusker_type or "no_unit_of_this_type"
local all_tuskers = wesnoth.get_units { side = wesnoth.current.side, type = tusker_type } local all_tuskers = wesnoth.units.find { side = wesnoth.current.side, type = tusker_type }
for _,tusker in ipairs(all_tuskers) do for _,tusker in ipairs(all_tuskers) do
if (tusker.moves > 0) then table.insert(forest_animals, tusker) end if (tusker.moves > 0) then table.insert(forest_animals, tusker) end
end end
@ -20,7 +20,7 @@ local function get_forest_animals(cfg)
-- Tusklets get moved by this CA if there are no tuskers left -- Tusklets get moved by this CA if there are no tuskers left
if not all_tuskers[1] then if not all_tuskers[1] then
local tusklet_type = cfg.tusklet_type or "no_unit_of_this_type" local tusklet_type = cfg.tusklet_type or "no_unit_of_this_type"
local tusklets = wesnoth.get_units { side = wesnoth.current.side, type = tusklet_type } local tusklets = wesnoth.units.find { side = wesnoth.current.side, type = tusklet_type }
for _,tusklet in ipairs(tusklets) do for _,tusklet in ipairs(tusklets) do
if (tusklet.moves > 0) then table.insert(forest_animals, tusklet) end if (tusklet.moves > 0) then table.insert(forest_animals, tusklet) end
end end

View file

@ -42,12 +42,12 @@ function ca_forest_animals_new_rabbit:execution(cfg)
end end
table.sort(holes, function(a, b) return a.random > b.random end) table.sort(holes, function(a, b) return a.random > b.random end)
local rabbits = wesnoth.get_units { side = wesnoth.current.side, type = cfg.rabbit_type } local rabbits = wesnoth.units.find { side = wesnoth.current.side, type = cfg.rabbit_type }
number = number - #rabbits number = number - #rabbits
number = math.min(number, #holes) number = math.min(number, #holes)
-- Now we simply take the first 'number' (randomized) holes -- Now we simply take the first 'number' (randomized) holes
local tmp_unit = wesnoth.get_units { side = wesnoth.current.side }[1] local tmp_unit = wesnoth.units.find { side = wesnoth.current.side }[1]
for i = 1,number do for i = 1,number do
local x, y = -1, -1 local x, y = -1, -1
if tmp_unit then if tmp_unit then

View file

@ -44,7 +44,7 @@ function ca_forest_animals_tusker_attack:execution(cfg)
-- The tusker moves as close to enemy as possible -- The tusker moves as close to enemy as possible
-- Closeness to tusklets is secondary criterion -- Closeness to tusklets is secondary criterion
local adj_tusklets = wesnoth.get_units { local adj_tusklets = wesnoth.units.find {
side = wesnoth.current.side, side = wesnoth.current.side,
type = cfg.tusklet_type, type = cfg.tusklet_type,
{ "filter_adjacent", { id = target.id } } { "filter_adjacent", { id = target.id } }

View file

@ -10,7 +10,7 @@ local function get_tusklets(cfg)
end end
local function get_tuskers(cfg) local function get_tuskers(cfg)
local tuskers = wesnoth.get_units { local tuskers = wesnoth.units.find {
side = wesnoth.current.side, side = wesnoth.current.side,
type = cfg.tusker_type type = cfg.tusker_type
} }

View file

@ -158,7 +158,7 @@ function ca_goto:execution(cfg, data)
else else
local enemy_at_goal local enemy_at_goal
if cfg.ignore_enemy_at_goal then if cfg.ignore_enemy_at_goal then
enemy_at_goal = wesnoth.get_unit(loc[1], loc[2]) enemy_at_goal = wesnoth.units.get(loc[1], loc[2])
if enemy_at_goal and wesnoth.is_enemy(wesnoth.current.side, enemy_at_goal.side) then if enemy_at_goal and wesnoth.is_enemy(wesnoth.current.side, enemy_at_goal.side) then
enemy_at_goal:extract() enemy_at_goal:extract()
else else
@ -179,7 +179,7 @@ function ca_goto:execution(cfg, data)
local rating = - cost local rating = - cost
-- Add a small penalty for hexes occupied by an allied unit -- Add a small penalty for hexes occupied by an allied unit
local unit_in_way = wesnoth.get_unit(loc[1], loc[2]) local unit_in_way = wesnoth.units.get(loc[1], loc[2])
if unit_in_way and (unit_in_way ~= unit) then if unit_in_way and (unit_in_way ~= unit) then
rating = rating - 0.01 rating = rating - 0.01
end end
@ -214,7 +214,7 @@ function ca_goto:execution(cfg, data)
for i = 2,#best_path do for i = 2,#best_path do
local sub_path, sub_cost = AH.find_path_with_shroud(best_unit, best_path[i][1], best_path[i][2], cfg) local sub_path, sub_cost = AH.find_path_with_shroud(best_unit, best_path[i][1], best_path[i][2], cfg)
if sub_cost <= best_unit.moves then if sub_cost <= best_unit.moves then
local unit_in_way = wesnoth.get_unit(best_path[i][1], best_path[i][2]) local unit_in_way = wesnoth.units.get(best_path[i][1], best_path[i][2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then
closest_hex = best_path[i] closest_hex = best_path[i]
end end

View file

@ -27,7 +27,7 @@ function ca_hang_out:evaluation(cfg, data)
MAISD.insert_mai_self_data(data, cfg.ai_id, { mobilize_units = true }) MAISD.insert_mai_self_data(data, cfg.ai_id, { mobilize_units = true })
-- Need to unmark all units also (all units, with and without moves) -- Need to unmark all units also (all units, with and without moves)
local units = wesnoth.get_units { side = wesnoth.current.side, { "and", wml.get_child(cfg, "filter") } } local units = wesnoth.units.find { side = wesnoth.current.side, { "and", wml.get_child(cfg, "filter") } }
for _,unit in ipairs(units) do for _,unit in ipairs(units) do
MAIUV.delete_mai_unit_variables(unit, cfg.ai_id) MAIUV.delete_mai_unit_variables(unit, cfg.ai_id)
end end

View file

@ -11,7 +11,7 @@ function ca_healer_move:evaluation(cfg, data)
-- find an appropriate hex to back up other units -- find an appropriate hex to back up other units
local score = data.HS_healer_move_score or 105000 local score = data.HS_healer_move_score or 105000
local all_healers = wesnoth.get_units { local all_healers = wesnoth.units.find {
side = wesnoth.current.side, side = wesnoth.current.side,
ability = "healing", ability = "healing",
{ "and", wml.get_child(cfg, "filter") } { "and", wml.get_child(cfg, "filter") }
@ -27,7 +27,7 @@ function ca_healer_move:evaluation(cfg, data)
end end
if (not healers[1]) then return 0 end if (not healers[1]) then return 0 end
local all_healees = wesnoth.get_units { local all_healees = wesnoth.units.find {
side = wesnoth.current.side, side = wesnoth.current.side,
{ "and", wml.get_child(cfg, "filter_second") } { "and", wml.get_child(cfg, "filter_second") }
} }

View file

@ -2,7 +2,7 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map local M = wesnoth.map
local function get_sheep(cfg) local function get_sheep(cfg)
local sheep = wesnoth.get_units { local sheep = wesnoth.units.find {
side = wesnoth.current.side, side = wesnoth.current.side,
{ "and", wml.get_child(cfg, "filter_second") } { "and", wml.get_child(cfg, "filter_second") }
} }

View file

@ -12,7 +12,7 @@ local function get_dogs(cfg)
end end
local function get_sheep_to_herd(cfg) local function get_sheep_to_herd(cfg)
local all_sheep = wesnoth.get_units { local all_sheep = wesnoth.units.find {
side = wesnoth.current.side, side = wesnoth.current.side,
{ "and", wml.get_child(cfg, "filter_second") }, { "and", wml.get_child(cfg, "filter_second") },
{ "not", { { "filter_adjacent", { side = wesnoth.current.side, { "and", wml.get_child(cfg, "filter") } } } } } { "not", { { "filter_adjacent", { side = wesnoth.current.side, { "and", wml.get_child(cfg, "filter") } } } } }

View file

@ -28,7 +28,7 @@ function ca_herding_sheep_move:execution(cfg)
-- Exclude those that are next to a dog -- Exclude those that are next to a dog
reach_map:iter( function(x, y, v) reach_map:iter( function(x, y, v)
for xa, ya in H.adjacent_tiles(x, y) do for xa, ya in H.adjacent_tiles(x, y) do
local dog = wesnoth.get_unit(xa, ya) local dog = wesnoth.units.get(xa, ya)
if dog and dog:matches(dogs_filter) then if dog and dog:matches(dogs_filter) then
reach_map:remove(x, y) reach_map:remove(x, y)
end end

View file

@ -23,7 +23,7 @@ function ca_herding_sheep_runs_dog:execution(cfg)
local sheep = get_next_sheep(cfg) local sheep = get_next_sheep(cfg)
-- Get the first dog that the sheep is adjacent to -- Get the first dog that the sheep is adjacent to
local dog = wesnoth.get_units { side = wesnoth.current.side, { "and", wml.get_child(cfg, "filter") }, local dog = wesnoth.units.find { side = wesnoth.current.side, { "and", wml.get_child(cfg, "filter") },
{ "filter_adjacent", { x = sheep.x, y = sheep.y } } { "filter_adjacent", { x = sheep.x, y = sheep.y } }
}[1] }[1]

View file

@ -14,7 +14,7 @@ local function hunter_attack_weakest_adj_enemy(ai, hunter)
local min_hp, target = math.huge local min_hp, target = math.huge
for xa,ya in H.adjacent_tiles(hunter.x, hunter.y) do for xa,ya in H.adjacent_tiles(hunter.x, hunter.y) do
local enemy = wesnoth.get_unit(xa, ya) local enemy = wesnoth.units.get(xa, ya)
if AH.is_attackable_enemy(enemy) then if AH.is_attackable_enemy(enemy) then
if (enemy.hitpoints < min_hp) then if (enemy.hitpoints < min_hp) then
min_hp, target = enemy.hitpoints, enemy min_hp, target = enemy.hitpoints, enemy
@ -82,7 +82,7 @@ function ca_hunter:execution(cfg)
-- Huge rating bonus if this is next to an enemy -- Huge rating bonus if this is next to an enemy
local enemy_hp = 500 local enemy_hp = 500
for xa,ya in H.adjacent_tiles(x, y) do for xa,ya in H.adjacent_tiles(x, y) do
local enemy = wesnoth.get_unit(xa, ya) local enemy = wesnoth.units.get(xa, ya)
if AH.is_attackable_enemy(enemy) then if AH.is_attackable_enemy(enemy) then
if (enemy.hitpoints < enemy_hp) then enemy_hp = enemy.hitpoints end if (enemy.hitpoints < enemy_hp) then enemy_hp = enemy.hitpoints end
end end
@ -139,7 +139,7 @@ function ca_hunter:execution(cfg)
-- If there's an enemy on the 'home' hex and we got right next to it, attack that enemy -- If there's an enemy on the 'home' hex and we got right next to it, attack that enemy
if (M.distance_between(home_loc[1], home_loc[2], hunter.x, hunter.y) == 1) then if (M.distance_between(home_loc[1], home_loc[2], hunter.x, hunter.y) == 1) then
local enemy = wesnoth.get_unit(home_loc[1], home_loc[2]) local enemy = wesnoth.units.get(home_loc[1], home_loc[2])
if AH.is_attackable_enemy(enemy) then if AH.is_attackable_enemy(enemy) then
if cfg.show_messages then if cfg.show_messages then
wesnoth.wml_actions.message { speaker = hunter.id, message = 'Get out of my home!' } wesnoth.wml_actions.message { speaker = hunter.id, message = 'Get out of my home!' }

View file

@ -18,7 +18,7 @@ local function messenger_find_enemies_in_way(messenger, goal_x, goal_y)
-- Is there an enemy unit on the second path hex? -- Is there an enemy unit on the second path hex?
-- This would be caught by the adjacent hex check later, but not in the right order -- This would be caught by the adjacent hex check later, but not in the right order
local enemy = wesnoth.get_unit(path[2][1], path[2][2]) local enemy = wesnoth.units.get(path[2][1], path[2][2])
if AH.is_attackable_enemy(enemy) then return enemy end if AH.is_attackable_enemy(enemy) then return enemy end
-- After that, go through adjacent hexes of all the other path hexes -- After that, go through adjacent hexes of all the other path hexes
@ -26,7 +26,7 @@ local function messenger_find_enemies_in_way(messenger, goal_x, goal_y)
local sub_path, sub_cost = AH.find_path_with_shroud(messenger, path[i][1], path[i][2], { ignore_units = true }) local sub_path, sub_cost = AH.find_path_with_shroud(messenger, path[i][1], path[i][2], { ignore_units = true })
if (sub_cost <= messenger.moves) then if (sub_cost <= messenger.moves) then
for xa,ya in H.adjacent_tiles(path[i][1], path[i][2]) do for xa,ya in H.adjacent_tiles(path[i][1], path[i][2]) do
local enemy = wesnoth.get_unit(xa, ya) local enemy = wesnoth.units.get(xa, ya)
if AH.is_attackable_enemy(enemy) then return enemy end if AH.is_attackable_enemy(enemy) then return enemy end
end end
else -- If we've reached the end of the path for this turn else -- If we've reached the end of the path for this turn
@ -77,7 +77,7 @@ local function messenger_find_clearing_attack(messenger, goal_x, goal_y, cfg)
local rating = attack.att_stats.average_hp - 2 * attack.def_stats.average_hp local rating = attack.att_stats.average_hp - 2 * attack.def_stats.average_hp
-- Give a huge bonus for closeness to enemy_in_way -- Give a huge bonus for closeness to enemy_in_way
local tmp_defender = wesnoth.get_unit(attack.target.x, attack.target.y) local tmp_defender = wesnoth.units.get(attack.target.x, attack.target.y)
local dist = wesnoth.map.distance_between(enemy_in_way.x, enemy_in_way.y, tmp_defender.x, tmp_defender.y) local dist = wesnoth.map.distance_between(enemy_in_way.x, enemy_in_way.y, tmp_defender.x, tmp_defender.y)
rating = rating + 100. / dist rating = rating + 100. / dist

View file

@ -10,7 +10,7 @@ return function(cfg)
-- Returns nil for all arguments if there are no messengers on the map -- Returns nil for all arguments if there are no messengers on the map
local filter = wml.get_child(cfg, "filter") or { id = cfg.id } local filter = wml.get_child(cfg, "filter") or { id = cfg.id }
local messengers = wesnoth.get_units { side = wesnoth.current.side, { "and", filter } } local messengers = wesnoth.units.find { side = wesnoth.current.side, { "and", filter } }
if (not messengers[1]) then return end if (not messengers[1]) then return end
local waypoints = AH.get_multi_named_locs_xy('waypoint', cfg) local waypoints = AH.get_multi_named_locs_xy('waypoint', cfg)

View file

@ -36,7 +36,7 @@ function ca_messenger_move:execution(cfg)
if sub_cost > messenger.moves then if sub_cost > messenger.moves then
break break
else else
local unit_in_way = wesnoth.get_unit(step[1], step[2]) local unit_in_way = wesnoth.units.get(step[1], step[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) then
unit_in_way = nil unit_in_way = nil
end end
@ -55,14 +55,14 @@ function ca_messenger_move:execution(cfg)
-- Now compare how long it would take from the end of both of these options -- Now compare how long it would take from the end of both of these options
local x_current, y_current = messenger.x, messenger.y local x_current, y_current = messenger.x, messenger.y
local unit_in_way = wesnoth.get_unit(next_hop[1], next_hop[2]) local unit_in_way = wesnoth.units.get(next_hop[1], next_hop[2])
if (unit_in_way == messenger) then unit_in_way = nil end if (unit_in_way == messenger) then unit_in_way = nil end
if unit_in_way then unit_in_way:extract() end if unit_in_way then unit_in_way:extract() end
messenger.loc = { next_hop[1], next_hop[2] } messenger.loc = { next_hop[1], next_hop[2] }
local _, cost1 = AH.find_path_with_shroud(messenger, x, y, { ignore_units = 'yes' }) local _, cost1 = AH.find_path_with_shroud(messenger, x, y, { ignore_units = 'yes' })
local unit_in_way2 = wesnoth.get_unit(optimum_hop[1], optimum_hop[2]) local unit_in_way2 = wesnoth.units.get(optimum_hop[1], optimum_hop[2])
if (unit_in_way2 == messenger) then unit_in_way2 = nil end if (unit_in_way2 == messenger) then unit_in_way2 = nil end
if unit_in_way2 then unit_in_way2:extract() end if unit_in_way2 then unit_in_way2:extract() end

View file

@ -37,7 +37,7 @@ function ca_protect_unit_attack:evaluation(cfg)
and (attack.att_stats.slowed == 0) and (attack.att_stats.slowed == 0)
then then
-- Get maximum possible counter attack damage by enemies on their turn -- Get maximum possible counter attack damage by enemies on their turn
local unit = wesnoth.get_unit(attack.src.x, attack.src.y) local unit = wesnoth.units.get(attack.src.x, attack.src.y)
local max_counter_damage = 0 local max_counter_damage = 0
for _,enemy_attack in ipairs(enemy_attacks) do for _,enemy_attack in ipairs(enemy_attacks) do

View file

@ -26,7 +26,7 @@ function ca_protect_unit_move:execution(cfg, data)
-- as long as they can still move -- as long as they can still move
for _,unit in ipairs(protected_units) do unit:extract() end for _,unit in ipairs(protected_units) do unit:extract() end
local units = wesnoth.get_units { side = wesnoth.current.side } local units = wesnoth.units.find { side = wesnoth.current.side }
local enemy_units = AH.get_attackable_enemies() local enemy_units = AH.get_attackable_enemies()
local attack_map = BC.get_attack_map(units).units -- enemy attack map local attack_map = BC.get_attack_map(units).units -- enemy attack map

View file

@ -10,7 +10,7 @@ function ca_recruit_random:evaluation(cfg)
-- Random recruiting from all the units the side has -- Random recruiting from all the units the side has
-- Check if leader is on keep -- Check if leader is on keep
local leader = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1] local leader = wesnoth.units.find { side = wesnoth.current.side, canrecruit = 'yes' }[1]
if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then
return 0 return 0
end end
@ -48,7 +48,7 @@ function ca_recruit_random:evaluation(cfg)
-- Check if there is space left for recruiting -- Check if there is space left for recruiting
local no_space = true local no_space = true
castle_map:iter(function(x, y) castle_map:iter(function(x, y)
local unit = wesnoth.get_unit(x, y) local unit = wesnoth.units.get(x, y)
if (not unit) then if (not unit) then
no_space = false no_space = false
end end

View file

@ -35,8 +35,8 @@ function ca_simple_attack:evaluation(cfg)
end end
if valid_target then if valid_target then
local attacker = wesnoth.get_unit(att.src.x, att.src.y) local attacker = wesnoth.units.get(att.src.x, att.src.y)
local enemy = wesnoth.get_unit(att.target.x, att.target.y) local enemy = wesnoth.units.get(att.target.x, att.target.y)
local dst = { att.dst.x, att.dst.y } local dst = { att.dst.x, att.dst.y }
local rating = BC.attack_rating(attacker, enemy, dst) local rating = BC.attack_rating(attacker, enemy, dst)

View file

@ -57,7 +57,7 @@ function ca_stationed_guardian:execution(cfg)
local best_defense, attack_loc = - math.huge local best_defense, attack_loc = - math.huge
for xa,ya in H.adjacent_tiles(target.x, target.y) do for xa,ya in H.adjacent_tiles(target.x, target.y) do
-- Only consider unoccupied hexes -- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(xa, ya) local unit_in_way = wesnoth.units.get(xa, ya)
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian) or (unit_in_way == guardian)
then then
@ -82,7 +82,7 @@ function ca_stationed_guardian:execution(cfg)
local min_dist, nh = math.huge local min_dist, nh = math.huge
for _,hex in ipairs(reach) do for _,hex in ipairs(reach) do
-- Only consider unoccupied hexes -- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(hex[1], hex[2]) local unit_in_way = wesnoth.units.get(hex[1], hex[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian) or (unit_in_way == guardian)
then then

View file

@ -4,7 +4,7 @@ local M = wesnoth.map
local ca_swarm_move = {} local ca_swarm_move = {}
function ca_swarm_move:evaluation(cfg) function ca_swarm_move:evaluation(cfg)
local units = wesnoth.get_units { side = wesnoth.current.side } local units = wesnoth.units.find { side = wesnoth.current.side }
for _,unit in ipairs(units) do for _,unit in ipairs(units) do
if (unit.moves > 0) then return cfg.ca_score end if (unit.moves > 0) then return cfg.ca_score end
end end
@ -17,7 +17,7 @@ function ca_swarm_move:execution(cfg)
local vision_distance = cfg.vision_distance or 12 local vision_distance = cfg.vision_distance or 12
-- If no close enemies, swarm will move semi-randomly, staying close together, but away from enemies -- If no close enemies, swarm will move semi-randomly, staying close together, but away from enemies
local all_units = wesnoth.get_units { side = wesnoth.current.side } local all_units = wesnoth.units.find { side = wesnoth.current.side }
local units, units_no_moves = {}, {} local units, units_no_moves = {}, {}
for _,unit in ipairs(all_units) do for _,unit in ipairs(all_units) do
if (unit.moves > 0) then if (unit.moves > 0) then

View file

@ -36,7 +36,7 @@ function ca_wolves_multipacks_attack:execution(cfg)
local wolves, attacks = {}, {} local wolves, attacks = {}, {}
for _,pack_wolf in ipairs(pack) do for _,pack_wolf in ipairs(pack) do
-- Wolf might have moved in previous attack -> use id to identify it -- Wolf might have moved in previous attack -> use id to identify it
local wolf = wesnoth.get_units { id = pack_wolf.id }[1] local wolf = wesnoth.units.find { id = pack_wolf.id }[1]
if wolf and (wolf.attacks_left > 0) then table.insert(wolves, wolf) end if wolf and (wolf.attacks_left > 0) then table.insert(wolves, wolf) end
end end
@ -88,14 +88,14 @@ function ca_wolves_multipacks_attack:execution(cfg)
for _,h in pairs(attack_map_hexes[attack_ind]) do number_hexes = number_hexes + 1 end for _,h in pairs(attack_map_hexes[attack_ind]) do number_hexes = number_hexes + 1 end
local rating = math.min(number_wolves, number_hexes) local rating = math.min(number_wolves, number_hexes)
local target = wesnoth.get_unit(attack_ind % 1000, math.floor(attack_ind / 1000)) local target = wesnoth.units.get(attack_ind % 1000, math.floor(attack_ind / 1000))
rating = rating - target.hitpoints / 100. rating = rating - target.hitpoints / 100.
-- Also, any target sitting next to a wolf of the same pack that has -- Also, any target sitting next to a wolf of the same pack that has
-- no attacks left is priority targeted (in order to stick with -- no attacks left is priority targeted (in order to stick with
-- the same target for all wolves of the pack) -- the same target for all wolves of the pack)
for xa,ya in H.adjacent_tiles(target.x, target.y) do for xa,ya in H.adjacent_tiles(target.x, target.y) do
local adj_unit = wesnoth.get_unit(xa, ya) local adj_unit = wesnoth.units.get(xa, ya)
if adj_unit then if adj_unit then
local unit_pack_number = MAIUV.get_mai_unit_variables(adj_unit, cfg.ai_id, "pack_number") local unit_pack_number = MAIUV.get_mai_unit_variables(adj_unit, cfg.ai_id, "pack_number")
if (unit_pack_number == pack_number) if (unit_pack_number == pack_number)
@ -129,8 +129,8 @@ function ca_wolves_multipacks_attack:execution(cfg)
WMPF.clear_label(best_attack.src.x, best_attack.src.y) WMPF.clear_label(best_attack.src.x, best_attack.src.y)
end end
local attacker = wesnoth.get_unit(best_attack.src.x, best_attack.src.y) local attacker = wesnoth.units.get(best_attack.src.x, best_attack.src.y)
local defender = wesnoth.get_unit(best_attack.target.x, best_attack.target.y) local defender = wesnoth.units.get(best_attack.target.x, best_attack.target.y)
AH.robust_move_and_attack(ai, attacker, best_attack.dst, defender) AH.robust_move_and_attack(ai, attacker, best_attack.dst, defender)
@ -154,7 +154,7 @@ function ca_wolves_multipacks_attack:execution(cfg)
local wolves_moves, wolves_no_moves = {}, {} local wolves_moves, wolves_no_moves = {}, {}
for _,pack_wolf in ipairs(pack) do for _,pack_wolf in ipairs(pack) do
-- Wolf might have moved in previous attack -> use id to identify it -- Wolf might have moved in previous attack -> use id to identify it
local wolf = wesnoth.get_units { id = pack_wolf.id }[1] local wolf = wesnoth.units.find { id = pack_wolf.id }[1]
if wolf then if wolf then
if (wolf.moves > 0) then if (wolf.moves > 0) then
table.insert(wolves_moves, wolf) table.insert(wolves_moves, wolf)

View file

@ -21,7 +21,7 @@ function wolves_multipacks_functions.assign_packs(cfg)
local pack_size = cfg.pack_size or 3 local pack_size = cfg.pack_size or 3
local wolves = wesnoth.get_units { side = wesnoth.current.side, type = cfg.type or "Wolf" } local wolves = wesnoth.units.find { side = wesnoth.current.side, type = cfg.type or "Wolf" }
local packs = {} local packs = {}
-- Find wolves that already have a pack number assigned -- Find wolves that already have a pack number assigned
@ -37,7 +37,7 @@ function wolves_multipacks_functions.assign_packs(cfg)
-- Do not change numbers of existing packs -> pack numbers might not be consecutive afterward -- Do not change numbers of existing packs -> pack numbers might not be consecutive afterward
for pack_number,pack in pairs(packs) do for pack_number,pack in pairs(packs) do
if (#pack == 1) then if (#pack == 1) then
local wolf = wesnoth.get_unit(pack[1].x, pack[1].y) local wolf = wesnoth.units.get(pack[1].x, pack[1].y)
MAIUV.delete_mai_unit_variables(wolf, cfg.ai_id) MAIUV.delete_mai_unit_variables(wolf, cfg.ai_id)
packs[pack_number] = nil packs[pack_number] = nil
end end

View file

@ -27,7 +27,7 @@ function ca_wolves_multipacks_wander:execution(cfg)
-- If any of the wolves has a goal set, this is used for the entire pack -- If any of the wolves has a goal set, this is used for the entire pack
local wolves, goal = {}, {} local wolves, goal = {}, {}
for _,loc in ipairs(pack) do for _,loc in ipairs(pack) do
local wolf = wesnoth.get_unit(loc.x, loc.y) local wolf = wesnoth.units.get(loc.x, loc.y)
table.insert(wolves, wolf) table.insert(wolves, wolf)
-- If any of the wolves in the pack has a goal set, we use that one -- If any of the wolves in the pack has a goal set, we use that one

View file

@ -42,7 +42,7 @@ function ca_zone_guardian:execution(cfg)
local best_defense, attack_loc = - math.huge local best_defense, attack_loc = - math.huge
for xa,ya in H.adjacent_tiles(target.x, target.y) do for xa,ya in H.adjacent_tiles(target.x, target.y) do
-- Only consider unoccupied hexes -- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(xa, ya) local unit_in_way = wesnoth.units.get(xa, ya)
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian) or (unit_in_way == guardian)
then then
@ -67,7 +67,7 @@ function ca_zone_guardian:execution(cfg)
local min_dist, nh = math.huge local min_dist, nh = math.huge
for _,hex in ipairs(reach) do for _,hex in ipairs(reach) do
-- Only consider unoccupied hexes -- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(hex[1], hex[2]) local unit_in_way = wesnoth.units.get(hex[1], hex[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian) or (unit_in_way == guardian)
then then

View file

@ -6,7 +6,7 @@ return {
-- This is taken almost literally from 'Ka'lian under Attack' in 'Legend of Wesmere' -- This is taken almost literally from 'Ka'lian under Attack' in 'Legend of Wesmere'
function urudin:retreat() function urudin:retreat()
local urudin = wesnoth.get_units({ side = 3, id = "Urudin" })[1] local urudin = wesnoth.units.find({ side = 3, id = "Urudin" })[1]
if urudin and urudin.valid then if urudin and urudin.valid then
local max_hp, hp = urudin.max_hitpoints, urudin.hitpoints local max_hp, hp = urudin.max_hitpoints, urudin.hitpoints
local turn = wesnoth.current.turn local turn = wesnoth.current.turn

View file

@ -92,12 +92,12 @@ local save_rabbit_spawn, save_rabbit_despawn
local function register_rabbit_commands() local function register_rabbit_commands()
function wesnoth.custom_synced_commands.rabbit_despawn(cfg) function wesnoth.custom_synced_commands.rabbit_despawn(cfg)
--TODO: maybe we only want to allow erasing of unit of certain types/sides/locations? --TODO: maybe we only want to allow erasing of unit of certain types/sides/locations?
wesnoth.erase_unit(cfg.x, cfg.y) wesnoth.units.erase(cfg.x, cfg.y)
end end
function wesnoth.custom_synced_commands.rabbit_spawn(cfg) function wesnoth.custom_synced_commands.rabbit_spawn(cfg)
--TODO: maybe we only want to allow creation of unit of certain types/sides/locations? --TODO: maybe we only want to allow creation of unit of certain types/sides/locations?
wesnoth.put_unit({ side = wesnoth.current.side, type = cfg.rabbit_type}, cfg.x, cfg.y) wesnoth.units.to_map({ side = wesnoth.current.side, type = cfg.rabbit_type}, cfg.x, cfg.y)
end end
end end

View file

@ -111,7 +111,7 @@ function micro_ai_helper.delete_CAs(side, ca_id_core, CA_parms)
wesnoth.delete_ai_component(side, "stage[main_loop].candidate_action[" .. ca_id .. "]") wesnoth.delete_ai_component(side, "stage[main_loop].candidate_action[" .. ca_id .. "]")
-- Also need to delete variable stored in all units of the side, so that later MAIs can use these units -- Also need to delete variable stored in all units of the side, so that later MAIs can use these units
local units = wesnoth.get_units { side = side } local units = wesnoth.units.find { side = side }
for _,unit in ipairs(units) do for _,unit in ipairs(units) do
MAIUV.delete_mai_unit_variables(unit, CA_parms.ai_id) MAIUV.delete_mai_unit_variables(unit, CA_parms.ai_id)
end end

View file

@ -535,7 +535,7 @@
[lua] [lua]
code=<< code=<<
local helper = wesnoth.require "helper" local helper = wesnoth.require "helper"
local units = wesnoth.get_units( { side = 1 } ) local units = wesnoth.units.find( { side = 1 } )
for i = 1, #units do for i = 1, #units do
-- pick an advancement randomly, and remove others so advance() doesn't show the dialog -- pick an advancement randomly, and remove others so advance() doesn't show the dialog
if units[i].advances_to[1] ~= nil then if units[i].advances_to[1] ~= nil then

View file

@ -420,12 +420,12 @@
saved_orc_leader.facing = "se" saved_orc_leader.facing = "se"
local loc = wesnoth.get_starting_location(2) local loc = wesnoth.get_starting_location(2)
wesnoth.put_unit(saved_orc_leader, loc[1], loc[2]) wesnoth.units.to_map(saved_orc_leader, loc[1], loc[2])
wml.variables.orc_leader_store = nil wml.variables.orc_leader_store = nil
else else
local loc = wesnoth.get_starting_location(2) local loc = wesnoth.get_starting_location(2)
wesnoth.put_unit(fallback_orc_leader, loc[1], loc[2]) wesnoth.units.to_map(fallback_orc_leader, loc[1], loc[2])
end end
>> >>
[/lua] [/lua]

View file

@ -14,21 +14,21 @@ end
function ca_ogres_flee:execution() function ca_ogres_flee:execution()
local units = AH.get_units_with_moves { side = wesnoth.current.side } local units = AH.get_units_with_moves { side = wesnoth.current.side }
local units_noMP = wesnoth.get_units { side = wesnoth.current.side, local units_noMP = wesnoth.units.find { side = wesnoth.current.side,
formula = 'movement_left = 0' formula = 'movement_left = 0'
} }
local width, height = wesnoth.get_map_size() local width, height = wesnoth.get_map_size()
-- Need the enemy map and enemy attack map if avoid_enemies is set -- Need the enemy map and enemy attack map if avoid_enemies is set
local enemies = wesnoth.get_units { { "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } } } local enemies = wesnoth.units.find { { "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } } }
local enemy_attack_map = BC.get_attack_map(enemies) local enemy_attack_map = BC.get_attack_map(enemies)
local max_rating, best_hex, best_unit = - math.huge local max_rating, best_hex, best_unit = - math.huge
for i,u in ipairs(units) do for i,u in ipairs(units) do
local reach = wesnoth.find_reach(u) local reach = wesnoth.find_reach(u)
for j,r in ipairs(reach) do for j,r in ipairs(reach) do
local unit_in_way = wesnoth.get_unit(r[1], r[2]) local unit_in_way = wesnoth.units.get(r[1], r[2])
if (not unit_in_way) or (unit_in_way == u) then if (not unit_in_way) or (unit_in_way == u) then

View file

@ -53,7 +53,7 @@ local function bandits_found(x,y)
local loc_i = helper.rand("1.."..#locs) local loc_i = helper.rand("1.."..#locs)
wml_actions.move_unit_fake({x = string.format("%d,%d", x, locs[loc_i][1]), y = string.format("%d,%d", y, locs[loc_i][2]), type = bandit, side = "4"}) wml_actions.move_unit_fake({x = string.format("%d,%d", x, locs[loc_i][1]), y = string.format("%d,%d", y, locs[loc_i][2]), type = bandit, side = "4"})
wesnoth.put_unit(locs[loc_i][1], locs[loc_i][2], { type = bandit, side = "4", random_traits = "yes", generate_name = "yes", upkeep = "loyal" }) wesnoth.units.to_map({ type = bandit, side = "4", random_traits = "yes", generate_name = "yes", upkeep = "loyal" }, locs[loc_i][1], locs[loc_i][2])
end end
if not boss_found and visited > 2 then if not boss_found and visited > 2 then

View file

@ -715,7 +715,7 @@
# wmllint: markcheck off # wmllint: markcheck off
code= << code= <<
local ai_helper = wesnoth.require "ai/lua/ai_helper.lua" local ai_helper = wesnoth.require "ai/lua/ai_helper.lua"
local delf = wesnoth.get_units { id = 'Delfador' }[1] local delf = wesnoth.units.find { id = 'Delfador' }[1]
local sceptre_loc= wesnoth.special_locations.sceptre local sceptre_loc= wesnoth.special_locations.sceptre
local path = wesnoth.find_path(delf, sceptre_loc[1], sceptre_loc[2], {ignore_units = true, viewing_side = 0}) -- # wmllint: noconvert local path = wesnoth.find_path(delf, sceptre_loc[1], sceptre_loc[2], {ignore_units = true, viewing_side = 0}) -- # wmllint: noconvert
_ = wesnoth.textdomain 'wesnoth-httt' _ = wesnoth.textdomain 'wesnoth-httt'

View file

@ -141,7 +141,7 @@ function wesnoth.wml_actions.persistent_carryover_unstore(cfg)
end end
for i = 1, vars["side_store.unit.length"] do for i = 1, vars["side_store.unit.length"] do
vars[string.format("side_store.unit[%d].side", i - 1)] = num vars[string.format("side_store.unit[%d].side", i - 1)] = num
local u = wesnoth.get_unit(vars[string.format("side_store.unit[%d].id", i - 1)]) local u = wesnoth.units.get(vars[string.format("side_store.unit[%d].id", i - 1)])
if u then if u then
vars[string.format("side_store.unit[%d].x", i - 1)] = u.x vars[string.format("side_store.unit[%d].x", i - 1)] = u.x

View file

@ -193,7 +193,7 @@
local my_ai = { } local my_ai = { }
function my_ai:retreat() function my_ai:retreat()
local urudin = wesnoth.get_units({id="Urudin"})[1] local urudin = wesnoth.units.find({id="Urudin"})[1]
if urudin and urudin.valid then if urudin and urudin.valid then
local mhp, hp = urudin.max_hitpoints, urudin.hitpoints local mhp, hp = urudin.max_hitpoints, urudin.hitpoints
local turn = wesnoth.current.turn local turn = wesnoth.current.turn

View file

@ -94,7 +94,7 @@
[lua] [lua]
code = << code = <<
local u = wesnoth.get_units({ id = 'Tallin' })[1] local u = wesnoth.units.find({ id = 'Tallin' })[1]
if u.level <= 1 then if u.level <= 1 then
u:transform('Dark Adept') u:transform('Dark Adept')
elseif u.level == 2 then elseif u.level == 2 then

View file

@ -61,7 +61,7 @@
[lua] [lua]
code = << code = <<
local u = wesnoth.get_units({ id = 'Tallin' })[1] local u = wesnoth.units.find({ id = 'Tallin' })[1]
if u.type == 'Dark Adept' then if u.type == 'Dark Adept' then
u:transform('Sergeant') u:transform('Sergeant')
elseif u.type == 'Dark Sorcerer' then elseif u.type == 'Dark Sorcerer' then

View file

@ -1036,7 +1036,7 @@
name=prestart name=prestart
[lua] [lua]
code=<< code=<<
for i, u in ipairs(wesnoth.get_recall_units { type = 'SotA Vampire Bat, SotA Blood Bat, SotA Dread Bat' }) do for i, u in ipairs(wesnoth.units.find { type = 'SotA Vampire Bat, SotA Blood Bat, SotA Dread Bat', x = 'recall' }) do
if u.type == 'SotA Vampire Bat' then if u.type == 'SotA Vampire Bat' then
u:transform('Vampire Bat') u:transform('Vampire Bat')
elseif u.type == 'SotA Blood Bat' then elseif u.type == 'SotA Blood Bat' then

View file

@ -13,7 +13,7 @@ local ca_transport = {}
-- Also unload units onto best hexes adjacent to landing site -- Also unload units onto best hexes adjacent to landing site
function ca_transport:evaluation() function ca_transport:evaluation()
local units = wesnoth.get_units { side = wesnoth.current.side, formula = 'movement_left > 0' } local units = wesnoth.units.find { side = wesnoth.current.side, formula = 'movement_left > 0' }
for i,u in ipairs(units) do for i,u in ipairs(units) do
if u.variables.destination_x and u.variables.destination_y then if u.variables.destination_x and u.variables.destination_y then
@ -25,7 +25,7 @@ function ca_transport:evaluation()
end end
function ca_transport:execution() function ca_transport:execution()
local units = wesnoth.get_units {} local units = wesnoth.units.find {}
-- Need all transport units plus maps of all units, all transport units and -- Need all transport units plus maps of all units, all transport units and
-- all other units (as those block hexes accessible to transport units) -- all other units (as those block hexes accessible to transport units)

View file

@ -204,7 +204,7 @@ local helper = wesnoth.require "helper"
function wesnoth.custom_synced_commands.ship_unload(cfg) function wesnoth.custom_synced_commands.ship_unload(cfg)
local unit = wesnoth.get_unit(cfg.x, cfg.y) local unit = wesnoth.units.get(cfg.x, cfg.y)
unit.variables.landed = 'yes' unit.variables.landed = 'yes'
unit.variables.destination_x = 1 unit.variables.destination_x = 1
unit.variables.destination_y = 30 unit.variables.destination_y = 30
@ -212,12 +212,12 @@ function wesnoth.custom_synced_commands.ship_unload(cfg)
local locs = wml.child_array(cfg, "dst") local locs = wml.child_array(cfg, "dst")
local l2_type = helper.rand('Swordsman,Javelineer,Pikeman') local l2_type = helper.rand('Swordsman,Javelineer,Pikeman')
wesnoth.put_unit({ side = wesnoth.current.side, type = l2_type, moves = 2 }, locs[1].x, locs[1].y) wesnoth.units.to_map({ side = wesnoth.current.side, type = l2_type, moves = 2 }, locs[1].x, locs[1].y)
wesnoth.add_known_unit(l2_type) wesnoth.add_known_unit(l2_type)
for i = 2, #locs do for i = 2, #locs do
local l1_type = helper.rand('Fencer,Mage,Cavalryman,Bowman,Spearman') local l1_type = helper.rand('Fencer,Mage,Cavalryman,Bowman,Spearman')
wesnoth.put_unit({ side = wesnoth.current.side, type = l1_type, moves = 2 }, locs[i].x, locs[i].y) wesnoth.units.to_map({ side = wesnoth.current.side, type = l1_type, moves = 2 }, locs[i].x, locs[i].y)
wesnoth.add_known_unit(l1_type) wesnoth.add_known_unit(l1_type)
end end
end end

View file

@ -24,7 +24,7 @@ function wml_actions.spawn_units(cfg)
local loc_i = helper.rand("1.."..#locs) local loc_i = helper.rand("1.."..#locs)
wml_actions.move_unit_fake({x = string.format("%d,%d", x, locs[loc_i][1]) , y = string.format("%d,%d", y, locs[loc_i][2]) , type = unit_type , side = side}) wml_actions.move_unit_fake({x = string.format("%d,%d", x, locs[loc_i][1]) , y = string.format("%d,%d", y, locs[loc_i][2]) , type = unit_type , side = side})
wesnoth.put_unit({ type = unit_type , side = side, random_traits = "yes", generate_name = "yes" , upkeep = "loyal" }, locs[loc_i][1], locs[loc_i][2]) wesnoth.units.to_map({ type = unit_type , side = side, random_traits = "yes", generate_name = "yes" , upkeep = "loyal" }, locs[loc_i][1], locs[loc_i][2])
end end
if done > 0 then if done > 0 then

View file

@ -405,7 +405,7 @@
local loc_i = helper.rand("1.."..#locs) local loc_i = helper.rand("1.."..#locs)
wml_actions.move_unit_fake({x = string.format("%d,%d", x, locs[loc_i][1]), y = string.format("%d,%d", y, locs[loc_i][2]), type = orc, side = "4"}) wml_actions.move_unit_fake({x = string.format("%d,%d", x, locs[loc_i][1]), y = string.format("%d,%d", y, locs[loc_i][2]), type = orc, side = "4"})
wesnoth.put_unit(locs[loc_i][1], locs[loc_i][2], { type = orc, side = "4", random_traits = "yes", generate_name = "yes", upkeep = "loyal" }) wesnoth.units.to_map({ type = orc, side = "4", random_traits = "yes", generate_name = "yes", upkeep = "loyal" }, locs[loc_i][1], locs[loc_i][2])
end end
vars.norcs = nil vars.norcs = nil
>> >>

View file

@ -16,8 +16,8 @@ function ca_aggressive_attack_no_suicide:evaluation(cfg, data)
-- Now find the best of the possible attacks -- Now find the best of the possible attacks
local max_rating, best_attack = - math.huge local max_rating, best_attack = - math.huge
for i, att in ipairs(attacks) do for i, att in ipairs(attacks) do
local attacker = wesnoth.get_unit(att.src.x, att.src.y) local attacker = wesnoth.units.get(att.src.x, att.src.y)
local defender = wesnoth.get_unit(att.target.x, att.target.y) local defender = wesnoth.units.get(att.target.x, att.target.y)
local attacker_dst = attacker:clone() local attacker_dst = attacker:clone()
attacker_dst.x, attacker_dst.y = att.dst.x, att.dst.y attacker_dst.x, attacker_dst.y = att.dst.x, att.dst.y

View file

@ -5,7 +5,7 @@ local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local muff_toras_move = {} local muff_toras_move = {}
function muff_toras_move:evaluation() function muff_toras_move:evaluation()
local muff_toras = wesnoth.get_units { id = 'Muff Toras' }[1] local muff_toras = wesnoth.units.find { id = 'Muff Toras' }[1]
if muff_toras and (muff_toras.moves > 0) then if muff_toras and (muff_toras.moves > 0) then
return 15000 return 15000
@ -15,8 +15,8 @@ function muff_toras_move:evaluation()
end end
function muff_toras_move:execution() function muff_toras_move:execution()
local muff_toras = wesnoth.get_units { id = 'Muff Toras' }[1] local muff_toras = wesnoth.units.find { id = 'Muff Toras' }[1]
local units = wesnoth.get_units { side = 3, { 'not', { id = 'Muff Toras' } } } local units = wesnoth.units.find { side = 3, { 'not', { id = 'Muff Toras' } } }
local enemies = AH.get_attackable_enemies() local enemies = AH.get_attackable_enemies()
local enemy_attack_map = BC.get_attack_map(enemies) local enemy_attack_map = BC.get_attack_map(enemies)

View file

@ -12,10 +12,10 @@
code=<< code=<<
local _ = wesnoth.textdomain "wesnoth-utbs" local _ = wesnoth.textdomain "wesnoth-utbs"
local teachers = wesnoth.get_units { ability = "teaching", side = wesnoth.current.side } local teachers = wesnoth.units.find { ability = "teaching", side = wesnoth.current.side }
for i, teacher in ipairs(teachers) do for i, teacher in ipairs(teachers) do
local students = wesnoth.get_units { side = wesnoth.current.side, { "filter_adjacent", { id = teacher.id } } } local students = wesnoth.units.find { side = wesnoth.current.side, { "filter_adjacent", { id = teacher.id } } }
if #students > 0 then -- don't divide by zero if #students > 0 then -- don't divide by zero
wesnoth.scroll_to_tile(teacher.x, teacher.y) wesnoth.scroll_to_tile(teacher.x, teacher.y)
@ -57,7 +57,7 @@
wesnoth.float_label(student.x, student.y, "<span color='cyan'>" .. tostring(_ "+%d XP"):format(xp_to_add) .. "</span>") wesnoth.float_label(student.x, student.y, "<span color='cyan'>" .. tostring(_ "+%d XP"):format(xp_to_add) .. "</span>")
if student.experience >= student.max_experience then if student.experience >= student.max_experience then
-- advance unit, animate and fire events -- advance unit, animate and fire events
wesnoth.advance_unit(student, true, true) student:advance(true, true)
end end
end end
end end

View file

@ -25,14 +25,14 @@ res.turns_over_advantage = function()
local r, g, b = 255, 255, 255 local r, g, b = 255, 255, 255
if team.__cfg.color == 1 then r, g, b = 255, 0, 0 if team.__cfg.color == 1 then r, g, b = 255, 0, 0
elseif team.__cfg.color == 2 then r, g, b = 0, 0, 255 end elseif team.__cfg.color == 2 then r, g, b = 0, 0, 255 end
if # wesnoth.get_units( { side = side } ) == 0 then if # wesnoth.units.find( { side = side } ) == 0 then
side_comparison = side_comparison .. string.format( tostring( _ "<span strikethrough='true' foreground='#%02x%02x%02x'>Side %d</span>") .. "\n", side_comparison = side_comparison .. string.format( tostring( _ "<span strikethrough='true' foreground='#%02x%02x%02x'>Side %d</span>") .. "\n",
r, g, b, side) r, g, b, side)
else else
local income = team.total_income * income_factor local income = team.total_income * income_factor
local units = 0 local units = 0
-- Calc the total unit-score here -- Calc the total unit-score here
for i, unit in ipairs( wesnoth.get_units { side = side } ) do for i, unit in ipairs( wesnoth.units.find { side = side } ) do
if not unit.__cfg.canrecruit then if not unit.__cfg.canrecruit then
wesnoth.fire("unit_worth", { id = unit.id }) wesnoth.fire("unit_worth", { id = unit.id })
units = units + wml.variables["unit_worth"] units = units + wml.variables["unit_worth"]

View file

@ -88,7 +88,7 @@ function wml_actions.select_character()
local unit = wml.variables.student_store local unit = wml.variables.student_store
if character == 2 then if character == 2 then
wesnoth.put_unit({ wesnoth.units.to_map({
type = "Fighteress", type = "Fighteress",
id = unit.id, id = unit.id,
name = _"Lisar", name = _"Lisar",
@ -101,7 +101,7 @@ function wml_actions.select_character()
-- enable the help to display this unit's page -- enable the help to display this unit's page
wesnoth.add_known_unit("Fighteress") wesnoth.add_known_unit("Fighteress")
else else
wesnoth.put_unit(unit) wesnoth.units.to_map(unit)
end end
wesnoth.redraw {} wesnoth.redraw {}

View file

@ -198,7 +198,7 @@
[lua] [lua]
code= << code= <<
local unit = wesnoth.get_units({id = "student"})[1] local unit = wesnoth.units.find({id = "student"})[1]
local hp_diff = unit.max_hitpoints - unit.hitpoints local hp_diff = unit.max_hitpoints - unit.hitpoints
if hp_diff > 7 then hp_diff = 8 end if hp_diff > 7 then hp_diff = 8 end

View file

@ -480,7 +480,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
--! Fakes the move of a unit satisfying the given @a filter to position @a x, @a y. --! Fakes the move of a unit satisfying the given @a filter to position @a x, @a y.
--! @note Usable only during WML actions. --! @note Usable only during WML actions.
function wesnoth.interface.move_unit_fake(filter, to_x, to_y) function wesnoth.interface.move_unit_fake(filter, to_x, to_y)
local moving_unit = wesnoth.get_units(filter)[1] local moving_unit = wesnoth.units.find(filter)[1]
local from_x, from_y = moving_unit.x, moving_unit.y local from_x, from_y = moving_unit.x, moving_unit.y
wesnoth.interface.scroll_to_hex(from_x, from_y) wesnoth.interface.scroll_to_hex(from_x, from_y)
@ -512,7 +512,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
--! @param vars key/value pairs that need changing. --! @param vars key/value pairs that need changing.
--! @note Usable only during WML actions. --! @note Usable only during WML actions.
function wesnoth.units.modify(filter, vars) function wesnoth.units.modify(filter, vars)
local units = wesnoth.get_units(filter) local units = wesnoth.units.find(filter)
for u in pairs(units) do for u in pairs(units) do
for k, v in pairs(vars) do for k, v in pairs(vars) do
-- Minor TODO: What if you want to change values of subtags? -- Minor TODO: What if you want to change values of subtags?
@ -523,8 +523,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
end end
end end
--! This function is deprecated, do not call directly. function wesnoth.units.find_on_recall(filter)
function wesnoth.get_recall_units(filter)
filter = filter or {} filter = filter or {}
if getmetatable(filter) == 'wml object' then if getmetatable(filter) == 'wml object' then
filter = filter.__literal filter = filter.__literal
@ -627,7 +626,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
wesnoth.create_unit = wesnoth.deprecate_api('wesnoth.create_unit', 'wesnoth.units.create', 1, nil, wesnoth.units.create) wesnoth.create_unit = wesnoth.deprecate_api('wesnoth.create_unit', 'wesnoth.units.create', 1, nil, wesnoth.units.create)
wesnoth.get_unit = wesnoth.deprecate_api('wesnoth.get_unit', 'wesnoth.units.get', 1, nil, wesnoth.units.get) wesnoth.get_unit = wesnoth.deprecate_api('wesnoth.get_unit', 'wesnoth.units.get', 1, nil, wesnoth.units.get)
wesnoth.get_units = wesnoth.deprecate_api('wesnoth.get_units', 'wesnoth.units.find', 1, nil, wesnoth.units.find) wesnoth.get_units = wesnoth.deprecate_api('wesnoth.get_units', 'wesnoth.units.find', 1, nil, wesnoth.units.find)
wesnoth.get_recall_units = wesnoth.deprecate_api('wesnoth.get_units', 'wesnoth.units.find with x="recall", y="recall"', 1, nil, wesnoth.get_recall_units) wesnoth.get_recall_units = wesnoth.deprecate_api('wesnoth.get_units', 'wesnoth.units.find_on_recall', 1, nil, wesnoth.units.find_on_recall)
end end
wesnoth.tovconfig = wesnoth.deprecate_api('wesnoth.tovconfig', 'wml.tovconfig', 1, nil, wml.tovconfig) wesnoth.tovconfig = wesnoth.deprecate_api('wesnoth.tovconfig', 'wml.tovconfig', 1, nil, wml.tovconfig)
wesnoth.debug = wesnoth.deprecate_api('wesnoth.debug', 'wml.tostring', 1, nil, wml.tostring) wesnoth.debug = wesnoth.deprecate_api('wesnoth.debug', 'wml.tostring', 1, nil, wml.tostring)

View file

@ -11,8 +11,8 @@ on_event("die", function()
return return
end end
local u_killer = wesnoth.get_unit(ec.x2, ec.y2) local u_killer = wesnoth.units.get(ec.x2, ec.y2)
local u_victim = wesnoth.get_unit(ec.x1, ec.y1) local u_victim = wesnoth.units.get(ec.x1, ec.y1)
if not u_killer or not u_killer:matches { ability = "feeding" } then if not u_killer or not u_killer:matches { ability = "feeding" } then
return return
@ -27,7 +27,7 @@ on_event("die", function()
effect.increase_total = effect.increase_total + 1 effect.increase_total = effect.increase_total + 1
u_killer_cfg.max_hitpoints = u_killer_cfg.max_hitpoints + 1 u_killer_cfg.max_hitpoints = u_killer_cfg.max_hitpoints + 1
u_killer_cfg.hitpoints = u_killer_cfg.hitpoints + 1 u_killer_cfg.hitpoints = u_killer_cfg.hitpoints + 1
wesnoth.create_unit(u_killer_cfg):to_map() wesnoth.units.to_map(u_killer_cfg)
wesnoth.float_label(ec.x2, ec.y2, _ "+1 max HP", "0,255,0") wesnoth.float_label(ec.x2, ec.y2, _ "+1 max HP", "0,255,0")
return return
end end

View file

@ -137,12 +137,12 @@ end
function wml_actions.fire_event(cfg) function wml_actions.fire_event(cfg)
local u1 = wml.get_child(cfg, "primary_unit") local u1 = wml.get_child(cfg, "primary_unit")
u1 = u1 and wesnoth.get_units(u1)[1] u1 = u1 and wesnoth.units.find(u1)[1]
local x1, y1 = 0, 0 local x1, y1 = 0, 0
if u1 then x1, y1 = u1.x, u1.y end if u1 then x1, y1 = u1.x, u1.y end
local u2 = wml.get_child(cfg, "secondary_unit") local u2 = wml.get_child(cfg, "secondary_unit")
u2 = u2 and wesnoth.get_units(u2)[1] u2 = u2 and wesnoth.units.find(u2)[1]
local x2, y2 = 0, 0 local x2, y2 = 0, 0
if u2 then x2, y2 = u2.x, u2.y end if u2 then x2, y2 = u2.x, u2.y end
@ -169,7 +169,7 @@ end
function wml_actions.allow_extra_recruit(cfg) function wml_actions.allow_extra_recruit(cfg)
local recruits = cfg.extra_recruit or helper.wml_error("[allow_extra_recruit] missing required extra_recruit= attribute") local recruits = cfg.extra_recruit or helper.wml_error("[allow_extra_recruit] missing required extra_recruit= attribute")
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.units.find(cfg)) do
local v = unit.extra_recruit local v = unit.extra_recruit
for recruit in utils.split(recruits) do for recruit in utils.split(recruits) do
table.insert(v, recruit) table.insert(v, recruit)
@ -201,7 +201,7 @@ end
function wml_actions.disallow_extra_recruit(cfg) function wml_actions.disallow_extra_recruit(cfg)
local recruits = cfg.extra_recruit or helper.wml_error("[disallow_extra_recruit] missing required extra_recruit= attribute") local recruits = cfg.extra_recruit or helper.wml_error("[disallow_extra_recruit] missing required extra_recruit= attribute")
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.units.find(cfg)) do
local v = unit.extra_recruit local v = unit.extra_recruit
for w in utils.split(recruits) do for w in utils.split(recruits) do
for i, r in ipairs(v) do for i, r in ipairs(v) do
@ -234,7 +234,7 @@ function wml_actions.set_extra_recruit(cfg)
table.insert(v, w) table.insert(v, w)
end end
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.units.find(cfg)) do
unit.extra_recruit = v unit.extra_recruit = v
end end
end end
@ -248,7 +248,7 @@ function wml_actions.store_map_dimensions(cfg)
end end
function wml_actions.unit_worth(cfg) function wml_actions.unit_worth(cfg)
local u = wesnoth.get_units(cfg)[1] or local u = wesnoth.units.find(cfg)[1] or
helper.wml_error "[unit_worth]'s filter didn't match any unit" helper.wml_error "[unit_worth]'s filter didn't match any unit"
local ut = wesnoth.unit_types[u.type] local ut = wesnoth.unit_types[u.type]
local hp = u.hitpoints / u.max_hitpoints local hp = u.hitpoints / u.max_hitpoints
@ -324,7 +324,7 @@ function wml_actions.scroll_to(cfg)
end end
function wml_actions.scroll_to_unit(cfg) function wml_actions.scroll_to_unit(cfg)
local u = wesnoth.get_units(cfg)[1] local u = wesnoth.units.find(cfg)[1]
if not u then return end if not u then return end
if not utils.optional_side_filter(cfg, "for_side", "for_side") then return end if not utils.optional_side_filter(cfg, "for_side", "for_side") then return end
wesnoth.scroll_to_tile(u.x, u.y, cfg.check_fogged, cfg.immediate) wesnoth.scroll_to_tile(u.x, u.y, cfg.check_fogged, cfg.immediate)
@ -343,9 +343,9 @@ function wml_actions.unlock_view(cfg)
end end
function wml_actions.select_unit(cfg) function wml_actions.select_unit(cfg)
local u = wesnoth.get_units(cfg)[1] local u = wesnoth.units.find(cfg)[1]
if not u then return end if not u then return end
wesnoth.select_unit(u.x, u.y, cfg.highlight, cfg.fire_event) wesnoth.interface.select_unit(u.x, u.y, cfg.highlight, cfg.fire_event)
end end
local function get_overlay_object_id(overlay) local function get_overlay_object_id(overlay)
@ -354,7 +354,7 @@ end
function wml_actions.unit_overlay(cfg) function wml_actions.unit_overlay(cfg)
local img = cfg.image or helper.wml_error( "[unit_overlay] missing required image= attribute" ) local img = cfg.image or helper.wml_error( "[unit_overlay] missing required image= attribute" )
for i,u in ipairs(wesnoth.get_units(cfg)) do for i,u in ipairs(wesnoth.units.find(cfg)) do
local has_already = false local has_already = false
for i, w in ipairs(u.overlays) do for i, w in ipairs(u.overlays) do
if w == img then has_already = true end if w == img then has_already = true end
@ -374,7 +374,7 @@ end
function wml_actions.remove_unit_overlay(cfg) function wml_actions.remove_unit_overlay(cfg)
local img = cfg.image or helper.wml_error( "[remove_unit_overlay] missing required image= attribute" ) local img = cfg.image or helper.wml_error( "[remove_unit_overlay] missing required image= attribute" )
for i,u in ipairs(wesnoth.get_units(cfg)) do for i,u in ipairs(wesnoth.units.find(cfg)) do
local has_already = false local has_already = false
for i, w in ipairs(u.overlays) do for i, w in ipairs(u.overlays) do
if w == img then has_already = true end if w == img then has_already = true end
@ -402,8 +402,8 @@ function wml_actions.store_unit(cfg)
local kill_units = cfg.kill local kill_units = cfg.kill
--cache the needed units here, since the filter might reference the to-be-cleared variable(s) --cache the needed units here, since the filter might reference the to-be-cleared variable(s)
local units = wesnoth.get_units(filter) local units = wesnoth.units.find(filter)
local recall_units = wesnoth.get_recall_units(filter) local recall_units = wesnoth.units.find{x = 'recall', wml.tag["and"](filter)}
local writer = utils.vwriter.init(cfg, "unit") local writer = utils.vwriter.init(cfg, "unit")
@ -459,7 +459,7 @@ function wml_actions.store_reachable_locations(cfg)
local reach = location_set.create() local reach = location_set.create()
for i,unit in ipairs(wesnoth.get_units(unit_filter)) do for i,unit in ipairs(wesnoth.units.find(unit_filter)) do
local unit_reach local unit_reach
if moves == "max" then if moves == "max" then
local saved_moves = unit.moves local saved_moves = unit.moves
@ -491,14 +491,14 @@ function wml_actions.store_reachable_locations(cfg)
end end
function wml_actions.hide_unit(cfg) function wml_actions.hide_unit(cfg)
for i,u in ipairs(wesnoth.get_units(cfg)) do for i,u in ipairs(wesnoth.units.find(cfg)) do
u.hidden = true u.hidden = true
end end
wml_actions.redraw {} wml_actions.redraw {}
end end
function wml_actions.unhide_unit(cfg) function wml_actions.unhide_unit(cfg)
for i,u in ipairs(wesnoth.get_units(cfg)) do for i,u in ipairs(wesnoth.units.find(cfg)) do
u.hidden = false u.hidden = false
end end
wml_actions.redraw {} wml_actions.redraw {}
@ -547,27 +547,27 @@ function wml_actions.floating_text(cfg)
end end
function wml_actions.petrify(cfg) function wml_actions.petrify(cfg)
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.units.find(cfg)) do
unit.status.petrified = true unit.status.petrified = true
-- Extract unit and put it back to update animation (not needed for recall units) -- Extract unit and put it back to update animation (not needed for recall units)
unit:extract() unit:extract()
unit:to_map() unit:to_map()
end end
for index, unit in ipairs(wesnoth.get_recall_units(cfg)) do for index, unit in ipairs(wesnoth.units.find_on_recall{wml.tag["and"](cfg)}) do
unit.status.petrified = true unit.status.petrified = true
end end
end end
function wml_actions.unpetrify(cfg) function wml_actions.unpetrify(cfg)
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.units.find(cfg)) do
unit.status.petrified = false unit.status.petrified = false
-- Extract unit and put it back to update animation (not needed for recall units) -- Extract unit and put it back to update animation (not needed for recall units)
unit:extract() unit:extract()
unit:to_map() unit:to_map()
end end
for index, unit in ipairs(wesnoth.get_recall_units(cfg)) do for index, unit in ipairs(wesnoth.units.find_on_recall(cfg)) do
unit.status.petrified = false unit.status.petrified = false
end end
end end
@ -575,10 +575,10 @@ end
function wml_actions.transform_unit(cfg) function wml_actions.transform_unit(cfg)
local transform_to = cfg.transform_to local transform_to = cfg.transform_to
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.units.find(cfg)) do
if transform_to then if transform_to then
wesnoth.transform_unit( unit, transform_to ) unit:transform(transform_to)
else else
local hitpoints = unit.hitpoints local hitpoints = unit.hitpoints
local experience = unit.experience local experience = unit.experience
@ -586,7 +586,7 @@ function wml_actions.transform_unit(cfg)
local status = wml.get_child( unit.__cfg, "status" ) local status = wml.get_child( unit.__cfg, "status" )
unit.experience = unit.max_experience unit.experience = unit.max_experience
wesnoth.advance_unit(unit, false, false) unit:advance(false, false)
unit.hitpoints = hitpoints unit.hitpoints = hitpoints
unit.experience = experience unit.experience = experience
@ -618,7 +618,7 @@ function wml_actions.store_side(cfg)
end end
function wml_actions.add_ai_behavior(cfg) function wml_actions.add_ai_behavior(cfg)
local unit = wesnoth.get_units(wml.get_child(cfg, "filter"))[1] or local unit = wesnoth.units.find(wml.get_child(cfg, "filter"))[1] or
helper.wml_error("[add_ai_behavior]: no unit specified") helper.wml_error("[add_ai_behavior]: no unit specified")
local side = cfg.side or local side = cfg.side or
@ -687,7 +687,7 @@ function wml_actions.store_villages( cfg )
end end
function wml_actions.put_to_recall_list(cfg) function wml_actions.put_to_recall_list(cfg)
local units = wesnoth.get_units(cfg) local units = wesnoth.units.find(cfg)
for i, unit in ipairs(units) do for i, unit in ipairs(units) do
if cfg.heal then if cfg.heal then
@ -697,7 +697,7 @@ function wml_actions.put_to_recall_list(cfg)
unit.status.poisoned = false unit.status.poisoned = false
unit.status.slowed = false unit.status.slowed = false
end end
wesnoth.put_recall_unit(unit, unit.side) unit:to_recall(unit.side)
end end
end end
@ -845,7 +845,7 @@ wml_actions.unstore_unit = function(cfg)
if type(unit_cfg) ~= "table" or unit_cfg.type == nil then if type(unit_cfg) ~= "table" or unit_cfg.type == nil then
helper.wml_error("[unstore_unit]: variable '" .. variable .. "' doesn't contain unit data") helper.wml_error("[unstore_unit]: variable '" .. variable .. "' doesn't contain unit data")
end end
local unit = wesnoth.create_unit(unit_cfg) local unit = wesnoth.units.create(unit_cfg)
local advance = cfg.advance ~= false local advance = cfg.advance ~= false
local animate = cfg.animate ~= false local animate = cfg.animate ~= false
local check_passability = cfg.check_passability ~= false or nil local check_passability = cfg.check_passability ~= false or nil
@ -874,7 +874,7 @@ wml_actions.unstore_unit = function(cfg)
wesnoth.float_label(x, y, text, color) wesnoth.float_label(x, y, text, color)
end end
if advance then if advance then
wesnoth.advance_unit(unit, animate, cfg.fire_event) unit:advance(animate, cfg.fire_event)
end end
else else
unit:to_recall() unit:to_recall()
@ -884,7 +884,7 @@ end
wml_actions.teleport = function(cfg) wml_actions.teleport = function(cfg)
local context = wesnoth.current.event_context local context = wesnoth.current.event_context
local filter = wml.get_child(cfg, "filter") or { x = context.x1, y = context.y1 } local filter = wml.get_child(cfg, "filter") or { x = context.x1, y = context.y1 }
local unit = wesnoth.get_units(filter)[1] local unit = wesnoth.units.find(filter)[1]
if not unit then if not unit then
-- No error if no unit matches. -- No error if no unit matches.
return return
@ -966,16 +966,16 @@ function wesnoth.wml_actions.cancel_action(cfg)
end end
function wesnoth.wml_actions.store_unit_defense(cfg) function wesnoth.wml_actions.store_unit_defense(cfg)
local unit = wesnoth.get_units(cfg)[1] or helper.wml_error "[store_unit_defense]'s filter didn't match any unit" local unit = wesnoth.units.find(cfg)[1] or helper.wml_error "[store_unit_defense]'s filter didn't match any unit"
local terrain = cfg.terrain local terrain = cfg.terrain
local defense local defense
if terrain then if terrain then
defense = wesnoth.unit_defense(unit, terrain) defense = units:defense(terrain)
elseif cfg.loc_x and cfg.loc_y then elseif cfg.loc_x and cfg.loc_y then
defense = wesnoth.unit_defense(unit, wesnoth.get_terrain(cfg.loc_x, cfg.loc_y)) defense = units:defense(wesnoth.get_terrain(cfg.loc_x, cfg.loc_y))
else else
defense = wesnoth.unit_defense(unit, wesnoth.get_terrain(unit.x, unit.y)) defense = units:defense(wesnoth.get_terrain(unit.x, unit.y))
end end
wml.variables[cfg.variable or "terrain_defense"] = defense wml.variables[cfg.variable or "terrain_defense"] = defense
end end
@ -1023,7 +1023,7 @@ end
function wml_actions.remove_trait(cfg) function wml_actions.remove_trait(cfg)
local obj_id = cfg.trait_id local obj_id = cfg.trait_id
for _,unit in ipairs(wesnoth.get_units(cfg)) do for _,unit in ipairs(wesnoth.units.find(cfg)) do
wesnoth.remove_modifications(unit, {id = obj_id}, "trait") unit:remove_modifications({id = obj_id}, "trait")
end end
end end

View file

@ -7,12 +7,12 @@ local function add_animation(anim, cfg)
local filter = wml.get_child(cfg, "filter") local filter = wml.get_child(cfg, "filter")
local unit local unit
if filter then if filter then
unit = wesnoth.get_units{ unit = wesnoth.units.find{
limit = 1, limit = 1,
T["and"](filter) T["and"](filter)
}[1] }[1]
else else
unit = wesnoth.get_unit( unit = wesnoth.units.get(
wesnoth.current.event_context.x1, wesnoth.current.event_context.x1,
wesnoth.current.event_context.y1 wesnoth.current.event_context.y1
) )

View file

@ -4,7 +4,7 @@ local utils = wesnoth.require "wml-utils"
function wesnoth.wml_actions.find_path(cfg) function wesnoth.wml_actions.find_path(cfg)
local filter_unit = wml.get_child(cfg, "traveler") or helper.wml_error("[find_path] missing required [traveler] tag") local filter_unit = wml.get_child(cfg, "traveler") or helper.wml_error("[find_path] missing required [traveler] tag")
-- only the first unit matching -- only the first unit matching
local unit = wesnoth.get_units(filter_unit)[1] or helper.wml_error("[find_path]'s filter didn't match any unit") local unit = wesnoth.units.find(filter_unit)[1] or helper.wml_error("[find_path]'s filter didn't match any unit")
local filter_location = wml.get_child(cfg, "destination") or helper.wml_error( "[find_path] missing required [destination] tag" ) local filter_location = wml.get_child(cfg, "destination") or helper.wml_error( "[find_path] missing required [destination] tag" )
-- support for $this_unit -- support for $this_unit
local this_unit = utils.start_var_scope("this_unit") local this_unit = utils.start_var_scope("this_unit")

View file

@ -21,7 +21,7 @@ function wml_actions.harm_unit(cfg)
local this_unit = utils.start_var_scope("this_unit") local this_unit = utils.start_var_scope("this_unit")
for index, unit_to_harm in ipairs(wesnoth.get_units(filter)) do for index, unit_to_harm in ipairs(wesnoth.units.find(filter)) do
if unit_to_harm.valid then if unit_to_harm.valid then
-- block to support $this_unit -- block to support $this_unit
wml.variables["this_unit"] = nil -- clearing this_unit wml.variables["this_unit"] = nil -- clearing this_unit
@ -36,7 +36,7 @@ function wml_actions.harm_unit(cfg)
local harmer_filter = wml.get_child(cfg, "filter_second") local harmer_filter = wml.get_child(cfg, "filter_second")
local experience = cfg.experience local experience = cfg.experience
local resistance_multiplier = tonumber(cfg.resistance_multiplier) or 1 local resistance_multiplier = tonumber(cfg.resistance_multiplier) or 1
if harmer_filter then harmer = wesnoth.get_units(harmer_filter)[1] end if harmer_filter then harmer = wesnoth.units.find(harmer_filter)[1] end
-- end of block to support $this_unit -- end of block to support $this_unit
if animate then if animate then
@ -91,7 +91,7 @@ function wml_actions.harm_unit(cfg)
amount, amount,
cfg.alignment or "neutral", cfg.alignment or "neutral",
wesnoth.get_time_of_day( { unit_to_harm.x, unit_to_harm.y, true } ).lawful_bonus, wesnoth.get_time_of_day( { unit_to_harm.x, unit_to_harm.y, true } ).lawful_bonus,
wesnoth.unit_resistance( unit_to_harm, cfg.damage_type or "dummy" ), unit_to_harm:resistance( cfg.damage_type or "dummy" ),
resistance_multiplier resistance_multiplier
) )
@ -130,8 +130,8 @@ function wml_actions.harm_unit(cfg)
set_status("unhealable", _"unhealable", _"female^unhealable") set_status("unhealable", _"unhealable", _"female^unhealable")
-- Extract unit and put it back to update animation if status was changed -- Extract unit and put it back to update animation if status was changed
wesnoth.extract_unit(unit_to_harm) unit_to_harm:extract()
wesnoth.put_unit(unit_to_harm) unit_to_harm:to_map()
if add_tab then if add_tab then
text = string.format("%s%s", "\t", text) text = string.format("%s%s", "\t", text)

View file

@ -3,7 +3,7 @@ local T = wml.tag
function wesnoth.wml_actions.heal_unit(cfg) function wesnoth.wml_actions.heal_unit(cfg)
local healers = wml.get_child(cfg, "filter_second") local healers = wml.get_child(cfg, "filter_second")
if healers then if healers then
healers = wesnoth.get_units{ healers = wesnoth.units.find{
ability_type = "heals", ability_type = "heals",
T["and"](healers) T["and"](healers)
} }
@ -13,9 +13,9 @@ function wesnoth.wml_actions.heal_unit(cfg)
local who = wml.get_child(cfg, "filter") local who = wml.get_child(cfg, "filter")
if who then if who then
who = wesnoth.get_units(who) who = wesnoth.units.find(who)
else else
who = wesnoth.get_units{ who = wesnoth.units.find{
x = wesnoth.current.event_context.x1, x = wesnoth.current.event_context.x1,
y = wesnoth.current.event_context.y1 y = wesnoth.current.event_context.y1
} }

View file

@ -8,7 +8,7 @@ function wesnoth.wml_actions.kill(cfg)
local secondary_unit = wml.get_child(cfg, "secondary_unit") local secondary_unit = wml.get_child(cfg, "secondary_unit")
local killer_loc = {0, 0} local killer_loc = {0, 0}
if secondary_unit then if secondary_unit then
secondary_unit = wesnoth.get_units(secondary_unit)[1] secondary_unit = wesnoth.units.find(secondary_unit)[1]
if cfg.fire_event then if cfg.fire_event then
if secondary_unit then if secondary_unit then
killer_loc = { x = tonumber(secondary_unit.x) or 0, y = tonumber(secondary_unit.y) or 0 } killer_loc = { x = tonumber(secondary_unit.x) or 0, y = tonumber(secondary_unit.y) or 0 }
@ -17,7 +17,7 @@ function wesnoth.wml_actions.kill(cfg)
end end
end end
end end
local dead_men_walking = wesnoth.get_units(cfg) local dead_men_walking = wesnoth.units.find(cfg)
for i,unit in ipairs(dead_men_walking) do for i,unit in ipairs(dead_men_walking) do
local death_loc = {x = tonumber(unit.x) or 0, y = tonumber(unit.y) or 0} local death_loc = {x = tonumber(unit.x) or 0, y = tonumber(unit.y) or 0}
if not secondary_unit then killer_loc = death_loc end if not secondary_unit then killer_loc = death_loc end
@ -93,7 +93,7 @@ function wesnoth.wml_actions.kill(cfg)
end end
if (cfg.x == "recall" or cfg.x == nil) and (cfg.y == "recall" or cfg.y == nil) then if (cfg.x == "recall" or cfg.x == nil) and (cfg.y == "recall" or cfg.y == nil) then
local dead_men_sleeping = wesnoth.get_recall_units(cfg) local dead_men_sleeping = wesnoth.units.find_on_recall(cfg)
for i,unit in ipairs(dead_men_sleeping) do for i,unit in ipairs(dead_men_sleeping) do
unit:erase() unit:erase()
end end

View file

@ -219,13 +219,13 @@ local function get_speaker(cfg)
if cfg.speaker == "narrator" then if cfg.speaker == "narrator" then
speaker = "narrator" speaker = "narrator"
elseif cfg.speaker == "unit" then elseif cfg.speaker == "unit" then
speaker = wesnoth.get_unit(context.x1 or 0, context.y1 or 0) speaker = wesnoth.units.get(context.x1 or 0, context.y1 or 0)
elseif cfg.speaker == "second_unit" then elseif cfg.speaker == "second_unit" then
speaker = wesnoth.get_unit(context.x2 or 0, context.y2 or 0) speaker = wesnoth.units.get(context.x2 or 0, context.y2 or 0)
elseif cfg.speaker ~= nil then elseif cfg.speaker ~= nil then
speaker = wesnoth.get_unit(cfg.speaker) speaker = wesnoth.units.get(cfg.speaker)
else else
speaker = wesnoth.get_units(cfg)[1] speaker = wesnoth.units.find(cfg)[1]
end end
return speaker return speaker

View file

@ -85,10 +85,10 @@ end
-- gets map and recalllist units. -- gets map and recalllist units.
local function get_all_units(filter) local function get_all_units(filter)
local res = wesnoth.get_units(filter) local res = wesnoth.units.find(filter)
if (not filter.x or filter.x == "recall") and (not filter.y or filter.y == "recall") then if (not filter.x or filter.x == "recall") and (not filter.y or filter.y == "recall") then
--append recall units to result. --append recall units to result.
for i, u in ipairs(wesnoth.get_recall_units(filter)) do for i, u in ipairs(wesnoth.units.find_on_recall(filter)) do
res[#res + 1] = u res[#res + 1] = u
end end
end end
@ -247,8 +247,8 @@ function wml_actions.modify_unit(cfg)
mod = wml.parsed(mod) mod = wml.parsed(mod)
end end
local unit = wml.variables[unit_path] local unit = wml.variables[unit_path]
unit = wesnoth.create_unit(unit) unit = wesnoth.units.create(unit)
wesnoth.add_modification(unit, current_tag, mod) unit:add_modification(current_tag, mod)
unit = unit.__cfg; unit = unit.__cfg;
wml.variables[unit_path] = unit wml.variables[unit_path] = unit
elseif current_tag == "effect" then elseif current_tag == "effect" then
@ -256,7 +256,7 @@ function wml_actions.modify_unit(cfg)
local apply_to = mod.apply_to local apply_to = mod.apply_to
if wesnoth.effects[apply_to] then if wesnoth.effects[apply_to] then
local unit = wml.variables[unit_path] local unit = wml.variables[unit_path]
unit = wesnoth.create_unit(unit) unit = wesnoth.units.create(unit)
wesnoth.effects[apply_to](unit, mod) wesnoth.effects[apply_to](unit, mod)
unit = unit.__cfg; unit = unit.__cfg;
wml.variables[unit_path] = unit wml.variables[unit_path] = unit
@ -264,11 +264,11 @@ function wml_actions.modify_unit(cfg)
helper.wml_error("[modify_unit] had invalid [effect]apply_to value") helper.wml_error("[modify_unit] had invalid [effect]apply_to value")
end end
elseif current_tag == "set_variable" then elseif current_tag == "set_variable" then
local unit = wesnoth.create_unit(wml.variables[unit_path]) local unit = wesnoth.units.create(wml.variables[unit_path])
wesnoth.wml_actions.set_variable(current_table[2], unit.variables) wesnoth.wml_actions.set_variable(current_table[2], unit.variables)
wml.variables[unit_path] = unit.__cfg wml.variables[unit_path] = unit.__cfg
elseif current_tag == "clear_variable" then elseif current_tag == "clear_variable" then
local unit = wesnoth.create_unit(wml.variables[unit_path]) local unit = wesnoth.units.create(wml.variables[unit_path])
wesnoth.wml_actions.clear_variable(current_table[2], unit.variables) wesnoth.wml_actions.clear_variable(current_table[2], unit.variables)
wml.variables[unit_path] = unit.__cfg wml.variables[unit_path] = unit.__cfg
else else

View file

@ -59,7 +59,7 @@ function wesnoth.wml_actions.move_unit(cfg)
if check_passability == nil then check_passability = true end if check_passability == nil then check_passability = true end
cfg = wml.literal(cfg) cfg = wml.literal(cfg)
cfg.to_location, cfg.to_x, cfg.to_y, cfg.fire_event, cfg.clear_shroud = nil cfg.to_location, cfg.to_x, cfg.to_y, cfg.fire_event, cfg.clear_shroud = nil
local units = wesnoth.get_units(cfg) local units = wesnoth.units.find(cfg)
for current_unit_index, current_unit in ipairs(units) do for current_unit_index, current_unit in ipairs(units) do
if not fire_event or current_unit.valid then if not fire_event or current_unit.valid then
@ -88,7 +88,7 @@ function wesnoth.wml_actions.move_unit(cfg)
elseif current_unit.x > x then current_unit.facing = "sw" elseif current_unit.x > x then current_unit.facing = "sw"
end end
wesnoth.extract_unit(current_unit) current_unit:extract()
local current_unit_cfg = current_unit.__cfg local current_unit_cfg = current_unit.__cfg
wesnoth.wml_actions.move_unit_fake { wesnoth.wml_actions.move_unit_fake {
type = current_unit_cfg.type, type = current_unit_cfg.type,
@ -102,7 +102,7 @@ function wesnoth.wml_actions.move_unit(cfg)
} }
local x2, y2 = current_unit.x, current_unit.y local x2, y2 = current_unit.x, current_unit.y
current_unit.x, current_unit.y = x, y current_unit.x, current_unit.y = x, y
wesnoth.put_unit(current_unit) current_unit:to_map()
if unshroud then if unshroud then
wesnoth.wml_actions.redraw {clear_shroud=true} wesnoth.wml_actions.redraw {clear_shroud=true}

View file

@ -18,9 +18,9 @@ function wml_actions.object(cfg)
local filter = wml.get_child(cfg, "filter") local filter = wml.get_child(cfg, "filter")
if filter then if filter then
unit = wesnoth.get_units(filter)[1] unit = wesnoth.units.find(filter)[1]
else else
unit = wesnoth.get_unit(context.x1, context.y1) unit = wesnoth.units.get(context.x1, context.y1)
end end
-- If a unit matches the filter, proceed -- If a unit matches the filter, proceed
@ -45,13 +45,13 @@ function wml_actions.object(cfg)
local dvs = cfg.delayed_variable_substitution local dvs = cfg.delayed_variable_substitution
local add = cfg.no_write ~= true local add = cfg.no_write ~= true
if dvs then if dvs then
wesnoth.add_modification(unit, "object", wml.literal(cfg), add) unit:add_modification("object", wml.literal(cfg), add)
else else
wesnoth.add_modification(unit, "object", wml.parsed(cfg), add) unit:add_modification("object", wml.parsed(cfg), add)
end end
if not silent then if not silent then
wesnoth.select_unit(unit, false) unit:select(false)
wesnoth.highlight_hex(unit.x, unit.y) wesnoth.highlight_hex(unit.x, unit.y)
end end
@ -81,8 +81,8 @@ end
function wml_actions.remove_object(cfg) function wml_actions.remove_object(cfg)
local obj_id = cfg.object_id local obj_id = cfg.object_id
for _,unit in ipairs(wesnoth.get_units(cfg)) do for _,unit in ipairs(wesnoth.units.find(cfg)) do
wesnoth.remove_modifications(unit, {id = obj_id}) unit:remove_modifications({id = obj_id})
end end
end end

View file

@ -53,13 +53,13 @@ function wesnoth.wml_actions.role(cfg)
if not reassign then if not reassign then
if search_map then if search_map then
local unit = wesnoth.get_units{role=role}[1] local unit = wesnoth.units.find{role=role}[1]
if unit then if unit then
return return
end end
end end
if recall and search_recall then if recall and search_recall then
local unit = wesnoth.get_recall_units{role=role}[1] local unit = wesnoth.units.find_on_recall{role=role}[1]
if unit then if unit then
recall.id = unit.id recall.id = unit.id
wesnoth.wml_actions.recall(recall) wesnoth.wml_actions.recall(recall)
@ -76,7 +76,7 @@ function wesnoth.wml_actions.role(cfg)
if #types > 0 then if #types > 0 then
filter.type = types[i] filter.type = types[i]
end end
local unit = wesnoth.get_units(filter)[1] local unit = wesnoth.units.find(filter)[1]
if unit then if unit then
unit.role = role unit.role = role
return return
@ -92,7 +92,7 @@ function wesnoth.wml_actions.role(cfg)
if #types > 0 then if #types > 0 then
filter.type = types[i] filter.type = types[i]
end end
local unit = wesnoth.get_recall_units(filter)[1] local unit = wesnoth.units.find_on_recall(filter)[1]
if unit then if unit then
unit.role = role unit.role = role
if recall then if recall then

View file

@ -12,9 +12,9 @@ res.quick_4mp_leaders = function(args)
end end
local trait_quick = args[1][2] local trait_quick = args[1][2]
for i, unit in ipairs(wesnoth.get_units { canrecruit = true, T.filter_wml { max_moves = 4 } }) do for i, unit in ipairs(wesnoth.units.find { canrecruit = true, T.filter_wml { max_moves = 4 } }) do
if not unit.variables.dont_make_me_quick then if not unit.variables.dont_make_me_quick then
wesnoth.add_modification(unit, "trait", trait_quick ) unit:add_modification("trait", trait_quick )
unit.moves = unit.max_moves unit.moves = unit.max_moves
unit.hitpoints = unit.max_hitpoints unit.hitpoints = unit.max_hitpoints
end end
@ -58,14 +58,14 @@ res.turns_over_advantage = function()
elseif team.__cfg.color == 7 then r, g, b = 255, 165, 0 elseif team.__cfg.color == 7 then r, g, b = 255, 165, 0
elseif team.__cfg.color == 8 then r, g, b = 255, 255, 255 elseif team.__cfg.color == 8 then r, g, b = 255, 255, 255
elseif team.__cfg.color == 9 then r, g, b = 0, 128, 128 end elseif team.__cfg.color == 9 then r, g, b = 0, 128, 128 end
if # wesnoth.get_units( { side = side } ) == 0 then if # wesnoth.units.find( { side = side } ) == 0 then
side_comparison = side_comparison .. string.format( tostring( _ "<span strikethrough='true' foreground='#%02x%02x%02x'>Side %d</span>") .. "\n", side_comparison = side_comparison .. string.format( tostring( _ "<span strikethrough='true' foreground='#%02x%02x%02x'>Side %d</span>") .. "\n",
r, g, b, side) r, g, b, side)
else else
local income = team.total_income * income_factor local income = team.total_income * income_factor
local units = 0 local units = 0
-- Calc the total unit-score here -- Calc the total unit-score here
for i, unit in ipairs( wesnoth.get_units { side = side } ) do for i, unit in ipairs( wesnoth.units.find { side = side } ) do
if not unit.__cfg.canrecruit then if not unit.__cfg.canrecruit then
wesnoth.fire("unit_worth", { id = unit.id }) wesnoth.fire("unit_worth", { id = unit.id })
units = units + wml.variables["unit_worth"] units = units + wml.variables["unit_worth"]

View file

@ -213,7 +213,7 @@ end
-- @a x,y: the location where to spawn the units on the map. -- @a x,y: the location where to spawn the units on the map.
local function place_units(unittypes, x, y) local function place_units(unittypes, x, y)
for i,v in ipairs(unittypes) do for i,v in ipairs(unittypes) do
local u = wesnoth.create_unit { type = v, generate_name = true, side = 2 } local u = wesnoth.units.create { type = v, generate_name = true, side = 2 }
u:add_modification("object", { u:add_modification("object", {
T.effect { T.effect {
apply_to = "movement_costs", apply_to = "movement_costs",
@ -262,13 +262,13 @@ end
-- convert all 'veteran' units from side 2 to the more aggressive side 1 -- convert all 'veteran' units from side 2 to the more aggressive side 1
-- this must happen before the new units are created from spawns. -- this must happen before the new units are created from spawns.
on_event("new turn", function() on_event("new turn", function()
for i, unit in ipairs(wesnoth.get_units { side = 2 }) do for i, unit in ipairs(wesnoth.units.find { side = 2 }) do
unit.side = 1 unit.side = 1
end end
end) end)
on_event("prestart", function() on_event("prestart", function()
local leaders = wesnoth.get_units { side = "3,4", canrecruit= true} local leaders = wesnoth.units.find { side = "3,4", canrecruit= true}
if #leaders < 2 then if #leaders < 2 then
create_timed_spaws(5, 11, 50, 5, 4, 21) create_timed_spaws(5, 11, 50, 5, 4, 21)
else else

View file

@ -1194,7 +1194,7 @@ end)
if result.button == -1 then if result.button == -1 then
local ev = wesnoth.current.event_context local ev = wesnoth.current.event_context
wesnoth.transform_unit(wesnoth.get_units{x=ev.x1, y=ev.y1}[1], types[result.list_item]) wesnoth.units.find{x=ev.x1, y=ev.y1}[1]:transform(types[result.list_item])
wesnoth.fire("redraw", {}) wesnoth.fire("redraw", {})
end end
>> >>
@ -1265,7 +1265,7 @@ end
function wml_actions.my_lua_action(t) function wml_actions.my_lua_action(t)
wesnoth.fire("label", { text = wesnoth.fire("label", { text =
string.format("Lua says:\n%d units and %d gold on side %d", string.format("Lua says:\n%d units and %d gold on side %d",
#wesnoth.get_units { side = t.on_side }, #wesnoth.units.find { side = t.on_side },
wesnoth.sides[t.on_side].gold, wesnoth.sides[t.on_side].gold,
t.on_side), t.on_side),
x = t.x, y = t.y }) x = t.x, y = t.y })
@ -1283,7 +1283,7 @@ end
end end
end end
function has_teleport(u) function has_teleport(u)
return wesnoth.unit_ability(u, "teleport") return u:ability("teleport")
end end
local old_unit_status = wesnoth.theme_items.unit_status local old_unit_status = wesnoth.theme_items.unit_status
function wesnoth.theme_items.unit_status() function wesnoth.theme_items.unit_status()
@ -1307,7 +1307,7 @@ end
first_time_only=no first_time_only=no
[lua] [lua]
code=<< code=<<
for i,u in ipairs(wesnoth.get_units()) do for i,u in ipairs(wesnoth.units.find()) do
if u.status.entangled then if u.status.entangled then
u.moves = 0 u.moves = 0
end end
@ -1573,7 +1573,7 @@ My best advancement costs $next_cost gold and Im $experience|% there."
[lua] [lua]
code = << code = <<
wesnoth.set_village_owner(20, 1, 1) wesnoth.set_village_owner(20, 1, 1)
local u = wesnoth.get_units({ lua_function = "has_teleport" })[1] local u = wesnoth.units.find({ lua_function = "has_teleport" })[1]
local t, c = wesnoth.find_path(u, 23, 7) local t, c = wesnoth.find_path(u, 23, 7)
for i,l in ipairs(t) do for i,l in ipairs(t) do
wesnoth.fire("item", wesnoth.fire("item",
@ -3158,13 +3158,13 @@ For game purposes, the races group into factions; for example, orcs often cooper
[lua] [lua]
code=<< code=<<
wesnoth.put_recall_unit { type = "Elvish Lady" } wesnoth.units.to_recall { type = "Elvish Lady" }
local u = wesnoth.get_recall_units()[2] local u = wesnoth.units.find_on_recall()[2]
local l = wesnoth.get_units { side = 1, canrecruit = true }[1] local l = wesnoth.units.find { side = 1, canrecruit = true }[1]
local x, y = wesnoth.find_vacant_tile(l.x, l.y, u) local x, y = wesnoth.find_vacant_tile(l.x, l.y, u)
u.side = 3 u.side = 3
wesnoth.put_unit(u, x, y) u:to_map(x, y)
local ul = wesnoth.get_recall_units() local ul = wesnoth.units.find_on_recall()
local t = {} local t = {}
for i,u in ipairs(ul) do table.insert(t, u.type) end for i,u in ipairs(ul) do table.insert(t, u.type) end
wesnoth.message(string.format("Recall list: %s", table.concat(t, ','))) wesnoth.message(string.format("Recall list: %s", table.concat(t, ',')))
@ -3726,7 +3726,7 @@ unplagueable: $wml_unit.status.unplagueable"
[lua] [lua]
code=<< code=<<
local args = ... local args = ...
local unit = wesnoth.get_unit(args.x1, args.y1) local unit = wesnoth.units.get(args.x1, args.y1)
local raw_msg = "Unit statuses in Lua:\n\nnot_living: %s\nunpoisonable: %s\nundrainable: %s\nunplagueable: %s" local raw_msg = "Unit statuses in Lua:\n\nnot_living: %s\nunpoisonable: %s\nundrainable: %s\nunplagueable: %s"
local msg = string.format(raw_msg, unit.status.not_living, unit.status.unpoisonable, unit.status.undrainable, unit.status.unplagueable) local msg = string.format(raw_msg, unit.status.not_living, unit.status.unpoisonable, unit.status.undrainable, unit.status.unplagueable)
wesnoth.wml_actions.message({speaker = "narrator", message = msg}) wesnoth.wml_actions.message({speaker = "narrator", message = msg})
@ -3806,7 +3806,7 @@ unplagueable: $wml_unit.status.unplagueable"
[lua] [lua]
code = << code = <<
for i, v in ipairs(wml.array_access.get_proxy("temp_villages_area")) do for i, v in ipairs(wml.array_access.get_proxy("temp_villages_area")) do
wesnoth.put_unit({ type = "Goblin Spearman", side = 2 }, v.x, v.y) wesnoth.units.to_map({ type = "Goblin Spearman", side = 2 }, v.x, v.y)
end end
wml.variables["temp_villages_area"] = nil wml.variables["temp_villages_area"] = nil
>> >>

View file

@ -618,7 +618,7 @@ int game_lua_kernel::intf_match_unit(lua_State *L)
if(unit* u_adj = luaW_tounit(L, 3)) { if(unit* u_adj = luaW_tounit(L, 3)) {
if(int side = u.on_recall_list()) { if(int side = u.on_recall_list()) {
WRN_LUA << "wesnoth.match_unit called with a secondary unit (3rd argument), "; WRN_LUA << "wesnoth.units.matches called with a secondary unit (3rd argument), ";
WRN_LUA << "but unit to match was on recall list. "; WRN_LUA << "but unit to match was on recall list. ";
WRN_LUA << "Thus the 3rd argument is ignored.\n"; WRN_LUA << "Thus the 3rd argument is ignored.\n";
team &t = board().get_team(side); team &t = board().get_team(side);
@ -2734,7 +2734,7 @@ int game_lua_kernel::intf_scroll_to_tile(lua_State *L)
int game_lua_kernel::intf_select_hex(lua_State *L) int game_lua_kernel::intf_select_hex(lua_State *L)
{ {
events::command_disabler command_disabler; events::command_disabler command_disabler;
deprecated_message("wesnoth.select_hex", DEP_LEVEL::PREEMPTIVE, {1, 15, 0}, "Use wesnoth.select_unit and/or wesnoth.highlight_hex instead."); deprecated_message("wesnoth.select_hex", DEP_LEVEL::PREEMPTIVE, {1, 15, 0}, "Use wesnoth.units.select and/or wesnoth.interface.highlight_hex instead.");
// Need this because check_location may change the stack // Need this because check_location may change the stack
// By doing this now, we ensure that it won't do so when // By doing this now, we ensure that it won't do so when

View file

@ -189,7 +189,7 @@ public:
we are currently operating on a unit& and removing it might cause memory corruptions we are currently operating on a unit& and removing it might cause memory corruptions
note that we don't check for the dtor of lua owned units because we assume that note that we don't check for the dtor of lua owned units because we assume that
we operate on such a unit that the lua function that invoked the operation on that unit we operate on such a unit that the lua function that invoked the operation on that unit
(like wesnoth.add_modification, wesnoth.match_unit ..) have a local copy of that (like wesnoth.units.add_modification, wesnoth.units.matches ..) have a local copy of that
lua_unit* userdata in its stack that prevents it from being collected. lua_unit* userdata in its stack that prevents it from being collected.
*/ */
int map_locked_; int map_locked_;