diff --git a/data/ai/lua/ai_helper.lua b/data/ai/lua/ai_helper.lua index e8f3e6e1e75..31b2d1db9fc 100644 --- a/data/ai/lua/ai_helper.lua +++ b/data/ai/lua/ai_helper.lua @@ -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) diff --git a/data/ai/lua/ca_grab_villages.lua b/data/ai/lua/ca_grab_villages.lua index cce162527fc..f9ff3810f35 100644 --- a/data/ai/lua/ca_grab_villages.lua +++ b/data/ai/lua/ca_grab_villages.lua @@ -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 diff --git a/data/ai/lua/ca_move_to_any_enemy.lua b/data/ai/lua/ca_move_to_any_enemy.lua index a7f2b9e0d6e..7b799b4bb47 100644 --- a/data/ai/lua/ca_move_to_any_enemy.lua +++ b/data/ai/lua/ca_move_to_any_enemy.lua @@ -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 diff --git a/data/ai/lua/generic_recruit_engine.lua b/data/ai/lua/generic_recruit_engine.lua index ae00e253df0..50f35eb6f15 100644 --- a/data/ai/lua/generic_recruit_engine.lua +++ b/data/ai/lua/generic_recruit_engine.lua @@ -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