ai_helper.get_closest_enemy: change return values

This is done for consistency with similar functions in mainline, and so that the function actual returns what it name says. This breaks backward compatibility, but note that the only practical difference for that is the order in which the arguments are returned, as both 'closest_enemy' and 'location' contain the enemy location in .x/.y format.
This commit is contained in:
mattsc 2018-10-27 16:24:13 -07:00
parent 5c73537412
commit a831edda6b
4 changed files with 12 additions and 11 deletions

View file

@ -1045,7 +1045,7 @@ function ai_helper.is_attackable_enemy(unit, side, cfg)
end
function ai_helper.get_closest_enemy(loc, side, cfg)
-- Return distance to and location of the enemy closest to @loc, or to the
-- Return the enemy closest to @loc and its distance from @loc, or to the
-- leader of side with number @side if @loc is not specified
--
-- Optional parameters:
@ -1066,16 +1066,16 @@ function ai_helper.get_closest_enemy(loc, side, cfg)
x, y = loc[1], loc[2]
end
local closest_distance, location = math.huge
local closest_distance, closest_enemy = math.huge
for _,enemy in ipairs(enemies) do
enemy_distance = M.distance_between(x, y, enemy.x, enemy.y)
if (enemy_distance < closest_distance) then
closest_enemy = enemy
closest_distance = enemy_distance
location = { x = enemy.x, y = enemy.y}
end
end
return closest_distance, location
return closest_enemy, closest_distance
end
function ai_helper.has_ability(unit, ability)

View file

@ -64,7 +64,7 @@ function ca_grab_villages:evaluation(cfg, data)
if wesnoth.is_enemy(owner, wesnoth.current.side) then village_rating = village_rating + 20000 end
end
local enemy_distance_from_village = AH.get_closest_enemy(v)
local _, enemy_distance_from_village = AH.get_closest_enemy(v)
-- Now we go on to the unit-dependent rating
local best_unit_rating = - math.huge
@ -96,7 +96,7 @@ function ca_grab_villages:evaluation(cfg, data)
end
-- Prefer not backtracking and moving more distant units to capture villages
local enemy_distance_from_unit = AH.get_closest_enemy({u.x, u.y})
local _, enemy_distance_from_unit = AH.get_closest_enemy({u.x, u.y})
rating = rating - (enemy_distance_from_village + enemy_distance_from_unit)/5
if (rating > best_unit_rating) then

View file

@ -27,7 +27,7 @@ function ca_move_to_any_enemy:evaluation(cfg, data)
local unit, destination
-- Find a unit that has a path to an space close to an enemy
for i,u in ipairs(units) do
local distance, target = AH.get_closest_enemy({u.x, u.y})
local target = AH.get_closest_enemy({u.x, u.y})
if target then
unit = u

View file

@ -620,7 +620,7 @@ return {
local max_rating = -1
local enemy_leaders = AH.get_attackable_enemies { canrecruit = 'yes' }
local closest_enemy_distance, closest_enemy_location = AH.get_closest_enemy()
local closest_enemy = AH.get_closest_enemy()
for i,c in ipairs(data.castle.locs) do
local rating = 0
@ -629,8 +629,8 @@ return {
for j,e in ipairs(enemy_leaders) do
rating = rating + 1 / M.distance_between(c[1], c[2], e.x, e.y) ^ 2.
end
if closest_enemy_location then
rating = rating + 1 / M.distance_between(c[1], c[2], closest_enemy_location.x, closest_enemy_location.y) ^ 2.
if closest_enemy then
rating = rating + 1 / M.distance_between(c[1], c[2], closest_enemy.x, closest_enemy.y) ^ 2.
end
if (rating > max_rating) then
max_rating, best_hex = rating, { c[1], c[2] }
@ -657,7 +657,8 @@ return {
local target_hex = recruit_data.recruit.target_hex
local reference_hex = target_hex[1] and target_hex or best_hex
local distance_to_enemy, enemy_location = AH.get_closest_enemy(reference_hex, wesnoth.current.side, { viewing_side = 0 })
local enemy_location, distance_to_enemy = AH.get_closest_enemy(reference_hex, wesnoth.current.side, { viewing_side = 0 })
-- If no enemy is on the map, then we first use closest enemy start hex,
-- and if that does not exist either, a location mirrored w.r.t the center of the map
if not enemy_location then