Replace helper.distance_between -> wesnoth.map.distance_between (fixes #1686)

This commit is contained in:
Celtic Minstrel 2017-05-11 04:14:03 -04:00
parent 7b13d6ca33
commit 6efc5ae090
40 changed files with 146 additions and 115 deletions

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local LS = wesnoth.require "location_set"
local F = wesnoth.require "functional"
local M = wesnoth.map
-- This is a collection of Lua functions used for custom AI development.
-- Note that this is still work in progress with significant changes occurring
@ -593,7 +594,7 @@ function ai_helper.find_opposite_hex_adjacent(hex, center_hex)
-- (or no opposite hex is found, e.g. for hexes on border)
-- If the two input hexes are not adjacent, return nil
if (H.distance_between(hex[1], hex[2], center_hex[1], center_hex[2]) ~= 1) then return nil end
if (M.distance_between(hex[1], hex[2], center_hex[1], center_hex[2]) ~= 1) then return nil end
-- Finding the opposite x position is easy
local opp_x = center_hex[1] + (center_hex[1] - hex[1])
@ -648,13 +649,13 @@ function ai_helper.get_closest_location(hex, location_filter, unit)
-- Find the maximum distance from 'hex' that's possible on the map
local max_distance = 0
local width, height = wesnoth.get_map_size()
local to_top_left = H.distance_between(hex[1], hex[2], 0, 0)
local to_top_left = M.distance_between(hex[1], hex[2], 0, 0)
if (to_top_left > max_distance) then max_distance = to_top_left end
local to_top_right = H.distance_between(hex[1], hex[2], width+1, 0)
local to_top_right = M.distance_between(hex[1], hex[2], width+1, 0)
if (to_top_right > max_distance) then max_distance = to_top_right end
local to_bottom_left = H.distance_between(hex[1], hex[2], 0, height+1)
local to_bottom_left = M.distance_between(hex[1], hex[2], 0, height+1)
if (to_bottom_left > max_distance) then max_distance = to_bottom_left end
local to_bottom_right = H.distance_between(hex[1], hex[2], width+1, height+1)
local to_bottom_right = M.distance_between(hex[1], hex[2], width+1, height+1)
if (to_bottom_right > max_distance) then max_distance = to_bottom_right end
local radius = 0
@ -728,7 +729,7 @@ function ai_helper.distance_map(units, map)
map:iter(function(x, y, data)
local dist = 0
for _,unit in ipairs(units) do
dist = dist + H.distance_between(unit.x, unit.y, x, y)
dist = dist + M.distance_between(unit.x, unit.y, x, y)
end
DM:insert(x, y, dist)
end)
@ -738,7 +739,7 @@ function ai_helper.distance_map(units, map)
for y = 1,height do
local dist = 0
for _,unit in ipairs(units) do
dist = dist + H.distance_between(unit.x, unit.y, x, y)
dist = dist + M.distance_between(unit.x, unit.y, x, y)
end
DM:insert(x, y, dist)
end
@ -758,7 +759,7 @@ function ai_helper.inverse_distance_map(units, map)
map:iter(function(x, y, data)
local dist = 0
for _,unit in ipairs(units) do
dist = dist + 1. / (H.distance_between(unit.x, unit.y, x, y) + 1)
dist = dist + 1. / (M.distance_between(unit.x, unit.y, x, y) + 1)
end
IDM:insert(x, y, dist)
end)
@ -768,7 +769,7 @@ function ai_helper.inverse_distance_map(units, map)
for y = 1,height do
local dist = 0
for _,unit in ipairs(units) do
dist = dist + 1. / (H.distance_between(unit.x, unit.y, x, y) + 1)
dist = dist + 1. / (M.distance_between(unit.x, unit.y, x, y) + 1)
end
IDM:insert(x, y, dist)
end
@ -790,7 +791,7 @@ function ai_helper.generalized_distance(x1, y1, x2, y2)
if (not y2) then return math.abs(x1 - x2) end
-- Otherwise, return standard distance
return H.distance_between(x1, y1, x2, y2)
return M.distance_between(x1, y1, x2, y2)
end
function ai_helper.xyoff(x, y, ori, hex)
@ -1078,7 +1079,7 @@ function ai_helper.get_closest_enemy(loc, side, cfg)
local closest_distance, location = 9e99
for _,enemy in ipairs(enemies) do
enemy_distance = H.distance_between(x, y, enemy.x, enemy.y)
enemy_distance = M.distance_between(x, y, enemy.x, enemy.y)
if (enemy_distance < closest_distance) then
closest_distance = enemy_distance
location = { x = enemy.x, y = enemy.y}
@ -1847,9 +1848,9 @@ function ai_helper.get_attack_combos(units, enemy, cfg)
for _,unit in ipairs(units) do
if ((unit.x == xa) and (unit.y == ya)) or (not blocked_hexes:get(xa, ya)) then
-- helper.distance_between() is much faster than wesnoth.find_path()
-- wesnoth.map.distance_between() is much faster than wesnoth.find_path()
--> pre-filter using the former
local cost = H.distance_between(unit.x, unit.y, xa, ya)
local cost = M.distance_between(unit.x, unit.y, xa, ya)
-- If the distance is <= the unit's MP, then see if it can actually get there
-- This also means that only short paths have to be evaluated (in most situations)

View file

@ -1,6 +1,7 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
-- This is a collection of Lua functions used for custom AI development.
-- Note that this is still work in progress with significant changes occurring
@ -938,8 +939,8 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- 'relative_distances' is larger for attack hexes closer to the side leader (possible values: -1 .. 1)
if leader then
local relative_distances =
H.distance_between(defender.x, defender.y, leader.x, leader.y)
- H.distance_between(dst[1], dst[2], leader.x, leader.y)
M.distance_between(defender.x, defender.y, leader.x, leader.y)
- M.distance_between(dst[1], dst[2], leader.x, leader.y)
defender_value = defender_value + relative_distances * distance_leader_weight
end
@ -1517,9 +1518,9 @@ function battle_calcs.get_attack_combos_subset(units, enemy, cfg)
if (not blocked_hexes:get(xa, ya) or ((xa == unit.x) and (ya == unit.y))) then
-- Check whether the unit can get to the hex
-- helper.distance_between() is much faster than wesnoth.find_path()
-- wesnoth.map.distance_between() is much faster than wesnoth.find_path()
--> pre-filter using the former
local cost = H.distance_between(unit.x, unit.y, xa, ya)
local cost = M.distance_between(unit.x, unit.y, xa, ya)
-- If the distance is <= the unit's MP, then see if it can actually get there
-- This also means that only short paths have to be evaluated (in most situations)

View file

@ -1,6 +1,7 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
-- Evaluation process:
--
@ -57,7 +58,7 @@ function ca_attack_highxp:evaluation(cfg, data)
local potential_target = false
local ind_attackers, ind_other_units = {}, {}
for i_u,unit in ipairs(units) do
if (H.distance_between(enemy.x, enemy.y, unit.x, unit.y) <= unit.moves + 1) then
if (M.distance_between(enemy.x, enemy.y, unit.x, unit.y) <= unit.moves + 1) then
if (unit.level >= XP_to_levelup) then
potential_target = true
table.insert(ind_attackers, i_u)

View file

@ -27,6 +27,7 @@ return {
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
local function print_time(...)
if turn_start_time then
@ -623,10 +624,10 @@ return {
local unit = wesnoth.get_unit(c[1], c[2])
if (not AH.is_visible_unit(wesnoth.current.side, unit)) then
for j,e in ipairs(enemy_leaders) do
rating = rating + 1 / H.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.
end
if closest_enemy_location then
rating = rating + 1 / H.distance_between(c[1], c[2], closest_enemy_location.x, closest_enemy_location.y) ^ 2.
rating = rating + 1 / M.distance_between(c[1], c[2], closest_enemy_location.x, closest_enemy_location.y) ^ 2.
end
if (rating > max_rating) then
max_rating, best_hex = rating, { c[1], c[2] }
@ -867,7 +868,7 @@ return {
c_index = c[1] + c[2]*1000
total_village_distance[c_index] = 0
for i,v in ipairs(villages) do
total_village_distance[c_index] = total_village_distance[c_index] + H.distance_between(c[1], c[2], v[1], v[2])
total_village_distance[c_index] = total_village_distance[c_index] + M.distance_between(c[1], c[2], v[1], v[2])
end
end

View file

@ -13,6 +13,7 @@ return {
local LS = wesnoth.require "location_set"
local HS = wesnoth.require "ai/micro_ais/cas/ca_healer_move.lua"
local R = wesnoth.require "ai/lua/retreat.lua"
local M = wesnoth.map
local function print_time(...)
if generic_rush.data.turn_start_time then
@ -154,7 +155,7 @@ return {
if turns <= 2 then
score = 1/turns
for j,e in ipairs(enemy_leaders) do
score = score + 1 / H.distance_between(loc[1], loc[2], e.x, e.y)
score = score + 1 / M.distance_between(loc[1], loc[2], e.x, e.y)
end
if score > best_score then
@ -180,7 +181,7 @@ return {
local score = 1/best_turns
for j,e in ipairs(enemy_leaders) do
-- count all distances as three less than they actually are
score = score + 1 / (H.distance_between(leader.x, leader.y, e.x, e.y) - 3)
score = score + 1 / (M.distance_between(leader.x, leader.y, e.x, e.y) - 3)
end
if score > best_score then
@ -340,7 +341,7 @@ return {
if (not village_occupied) then
-- Path finding is expensive, so we do a first cut simply by distance
-- There is no way a unit can get to the village if the distance is greater than its moves
local dist = H.distance_between(u.x, u.y, v[1], v[2])
local dist = M.distance_between(u.x, u.y, v[1], v[2])
if (dist <= u.moves) then
local path, cost = wesnoth.find_path(u, v[1], v[2])
if (cost <= u.moves) then

View file

@ -60,7 +60,7 @@ function ca_big_animals:execution(cfg)
-- Now find the one of these hexes that is closest to the goal
local max_rating, best_hex = -9e99
reach_map:iter( function(x, y, v)
local rating = - H.distance_between(x, y, goal.goal_x, goal.goal_y)
local rating = -wesnoth.map.distance_between(x, y, goal.goal_x, goal.goal_y)
-- Proximity to an enemy unit is a plus
local enemy_hp = 500

View file

@ -3,6 +3,7 @@ local LS = wesnoth.require "location_set"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local MAISD = wesnoth.require "ai/micro_ais/micro_ai_self_data.lua"
local M = wesnoth.map
local function bottleneck_is_my_territory(map, enemy_map)
-- Create map that contains 'true' for all hexes that are
@ -163,7 +164,7 @@ local function bottleneck_get_rating(unit, x, y, has_leadership, is_healer, data
if (rating <= 0) and data.BD_is_my_territory:get(x, y) then
local combined_dist = 0
data.BD_def_map:iter(function(x_def, y_def, v)
combined_dist = combined_dist + H.distance_between(x, y, x_def, y_def)
combined_dist = combined_dist + M.distance_between(x, y, x_def, y_def)
end)
combined_dist = combined_dist / data.BD_def_map:size()
rating = 1000 - combined_dist * 10.
@ -274,7 +275,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
if data.BD_is_my_territory:get(xa, ya) then
local min_dist = 9e99
data.BD_def_map:iter( function(xd, yd, vd)
local dist_line = H.distance_between(xa, ya, xd, yd)
local dist_line = M.distance_between(xa, ya, xd, yd)
if (dist_line < min_dist) then min_dist = dist_line end
end)
if (min_dist > 0) then

View file

@ -45,7 +45,7 @@ function ca_coward:execution(cfg)
then
local rating = 0
for _,enemy in ipairs(enemies) do
local dist = H.distance_between(hex[1], hex[2], enemy.x, enemy.y)
local dist = wesnoth.map.distance_between(hex[1], hex[2], enemy.x, enemy.y)
rating = rating - 1 / dist^2
end

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local T = H.set_wml_tag_metatable{}
local M = wesnoth.map
-- Functions to perform fast evaluation of attacks and attack combinations.
-- The emphasis with all of this is on speed, not elegance.
@ -411,8 +412,8 @@ function ca_fast_attack_utils.attack_rating(attacker_infos, defender_info, dsts,
local rel_dist_rating = 0.
for _,dst in ipairs(dsts) do
local relative_distance =
H.distance_between(defender_x, defender_y, leader_x, leader_y)
- H.distance_between(dst[1], dst[2], leader_x, leader_y)
M.distance_between(defender_x, defender_y, leader_x, leader_y)
- M.distance_between(dst[1], dst[2], leader_x, leader_y)
rel_dist_rating = rel_dist_rating + relative_distance
end
rel_dist_rating = rel_dist_rating / #dsts * distance_leader_weight

View file

@ -81,7 +81,7 @@ function ca_fast_combat_leader:evaluation(cfg, data)
for _,enemy in ipairs(enemies) do
-- Only need to consider enemies that are close enough
if (H.distance_between(leader.x, leader.y, enemy.x, enemy.y) <= (enemy.max_moves + leader.max_moves + 1)) then
if (wesnoth.map.distance_between(leader.x, leader.y, enemy.x, enemy.y) <= (enemy.max_moves + leader.max_moves + 1)) then
enemy_power = enemy.hitpoints
local old_moves = enemy.moves

View file

@ -1,6 +1,7 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local FAU = wesnoth.require "ai/micro_ais/cas/ca_fast_attack_utils.lua"
local M = wesnoth.map
local ca_fast_move = {}
@ -54,7 +55,7 @@ function ca_fast_move:execution(cfg)
for j = i+1,#villages do
local v2 = villages[j]
local dist = H.distance_between(v1[1], v1[2], v2[1], v2[2])
local dist = M.distance_between(v1[1], v1[2], v2[1], v2[2])
dist = math.ceil(dist / 5.) -- In discrete steps of 5 hexes
v1.rating = (v1.rating or 0) + 1. / dist
@ -67,7 +68,7 @@ function ca_fast_move:execution(cfg)
local dist = 1 -- Just in case there is no leader
dist = math.ceil(dist / 5.) -- In discrete steps of 5 hexes
if leader then
dist = H.distance_between(village[1], village[2], leader.x, leader.y)
dist = M.distance_between(village[1], village[2], leader.x, leader.y)
end
village.rating = (village.rating or 1.) * 1. / dist
@ -93,7 +94,7 @@ function ca_fast_move:execution(cfg)
for _,village in ipairs(villages) do
village.rating = village.rating / base_rating
* H.distance_between(x, y, village[1], village[2])
* M.distance_between(x, y, village[1], village[2])
end
end
end
@ -105,8 +106,8 @@ function ca_fast_move:execution(cfg)
-- Sort enemy leaders by distance to AI leader
if leader then
table.sort(enemy_leaders, function(a, b)
local dist_a = H.distance_between(leader.x, leader.y, a.x, a.y)
local dist_b = H.distance_between(leader.x, leader.y, b.x, b.y)
local dist_a = M.distance_between(leader.x, leader.y, a.x, a.y)
local dist_b = m.distance_between(leader.x, leader.y, b.x, b.y)
return (dist_a < dist_b)
end)
end
@ -123,7 +124,7 @@ function ca_fast_move:execution(cfg)
for _,goal in ipairs(goals) do
-- Insert information about the units
for i_u,unit in ipairs(units) do
local dist = H.distance_between(unit.x, unit.y, goal.x, goal.y)
local dist = M.distance_between(unit.x, unit.y, goal.x, goal.y)
goal[i_u] = {
dist = dist / unit.max_moves,
@ -201,8 +202,8 @@ function ca_fast_move:execution(cfg)
local max_rating, best_hex = -9e99
for _,loc in ipairs(reach) do
if (not avoid_map:get(loc[1], loc[2])) then
local rating = - H.distance_between(loc[1], loc[2], short_goal[1], short_goal[2])
local other_rating = - H.distance_between(loc[1], loc[2], goal.x, goal.y) / 10.
local rating = -M.distance_between(loc[1], loc[2], short_goal[1], short_goal[2])
local other_rating = -M.distance_between(loc[1], loc[2], goal.x, goal.y) / 10.
rating = rating + other_rating
local unit_in_way

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
local function get_forest_animals(cfg)
-- We want the deer/rabbits to move first, tuskers afterward
@ -65,7 +66,7 @@ function ca_forest_animals_move:execution(cfg)
-- Behavior is different depending on whether a predator is close or not
local close_enemies = {}
for _,enemy in ipairs(enemies) do
if (H.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= unit.max_moves+1) then
if (M.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= unit.max_moves+1) then
table.insert(close_enemies, enemy)
end
end
@ -99,7 +100,7 @@ function ca_forest_animals_move:execution(cfg)
else -- Or if no close reachable terrain was found, move toward the closest
local min_dist, best_hex = 9e99
for _,loc in ipairs(wander_locs) do
local dist = H.distance_between(loc[1], loc[2], unit.x, unit.y)
local dist = M.distance_between(loc[1], loc[2], unit.x, unit.y)
if dist < min_dist then
best_hex, min_dist = loc, dist
end
@ -123,7 +124,7 @@ function ca_forest_animals_move:execution(cfg)
enemies = AH.get_attackable_enemies()
close_enemies = {}
for _,enemy in ipairs(enemies) do
if (H.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= unit.max_moves+1) then
if (M.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= unit.max_moves+1) then
table.insert(close_enemies, enemy)
end
end
@ -137,7 +138,7 @@ function ca_forest_animals_move:execution(cfg)
local farthest_hex = AH.find_best_move(unit, function(x, y)
local rating = 0
for _,enemy in ipairs(close_enemies) do
local dist = H.distance_between(enemy.x, enemy.y, x, y)
local dist = M.distance_between(enemy.x, enemy.y, x, y)
rating = rating - 1 / dist^2
end

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local function get_tuskers(cfg)
local tuskers = AH.get_units_with_moves {
@ -35,7 +36,7 @@ function ca_forest_animals_tusker_attack:execution(cfg)
local min_dist, attacker, target = 9e99
for _,tusker in ipairs(tuskers) do
for _,enemy in ipairs(adjacent_enemies) do
local dist = H.distance_between(tusker.x, tusker.y, enemy.x, enemy.y)
local dist = M.distance_between(tusker.x, tusker.y, enemy.x, enemy.y)
if (dist < min_dist) then
min_dist, attacker, target = dist, tusker, enemy
end
@ -51,9 +52,9 @@ function ca_forest_animals_tusker_attack:execution(cfg)
}
local best_hex = AH.find_best_move(attacker, function(x, y)
local rating = - H.distance_between(x, y, target.x, target.y)
local rating = -M.distance_between(x, y, target.x, target.y)
for _,tusklet in ipairs(adj_tusklets) do
if (H.distance_between(x, y, tusklet.x, tusklet.y) == 1) then rating = rating + 0.1 end
if (M.distance_between(x, y, tusklet.x, tusklet.y) == 1) then rating = rating + 0.1 end
end
return rating

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local function get_tusklets(cfg)
local tusklets = AH.get_units_with_moves {
@ -35,14 +36,14 @@ function ca_forest_animals_tusklet_move:execution(cfg)
local goto_tusker, min_dist = {}, 9e99
for _,tusker in ipairs(tuskers) do
local dist = H.distance_between(tusker.x, tusker.y, tusklet.x, tusklet.y)
local dist = M.distance_between(tusker.x, tusker.y, tusklet.x, tusklet.y)
if (dist < min_dist) then
min_dist, goto_tusker = dist, tusker
end
end
local best_hex = AH.find_best_move(tusklet, function(x, y)
return - H.distance_between(x, y, goto_tusker.x, goto_tusker.y)
return - M.distance_between(x, y, goto_tusker.x, goto_tusker.y)
end)
AH.movefull_stopunit(ai, tusklet, best_hex)

View file

@ -4,6 +4,7 @@ local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local LS = wesnoth.require "location_set"
local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local MAISD = wesnoth.require "ai/micro_ais/micro_ai_self_data.lua"
local M = wesnoth.map
local function custom_cost(x, y, unit, enemy_map, enemy_attack_map, multiplier)
local terrain = wesnoth.get_terrain(x, y)
@ -123,10 +124,10 @@ function ca_goto:execution(cfg, data)
-- hex to the goal that the unit can get to
if cfg.use_straight_line then
local hex, _, rating = AH.find_best_move(unit, function(x, y)
local r = - H.distance_between(x, y, loc[1], loc[2])
local r = -M.distance_between(x, y, loc[1], loc[2])
-- Also add distance from unit as very small rating component
-- This is mostly here to keep unit in place when no better hexes are available
r = r - H.distance_between(x, y, unit.x, unit.y) / 1000.
r = r - M.distance_between(x, y, unit.x, unit.y) / 1000.
return r
end, { no_random = true })

View file

@ -3,6 +3,7 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local MAISD = wesnoth.require "ai/micro_ais/micro_ai_self_data.lua"
local M = wesnoth.map
local function get_hang_out_units(cfg)
local units = AH.get_units_with_moves {
@ -95,14 +96,14 @@ function ca_hang_out:execution(cfg)
if (not avoid_map:get(x, y)) then
for _,loc in ipairs(locs) do
-- Main rating is the distance from any of the goal hexes
local rating = -H.distance_between(x, y, loc[1], loc[2])
local rating = -M.distance_between(x, y, loc[1], loc[2])
-- Fastest unit moves first
rating = rating + unit.max_moves / 100.
-- Minor penalty for distance from current position of unit
-- so that there's not too much shuffling around
local rating = rating - H.distance_between(x, y, unit.x, unit.y) / 1000.
local rating = rating - M.distance_between(x, y, unit.x, unit.y) / 1000.
if (rating > max_rating_unit) then
max_rating_unit = rating

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local LS = wesnoth.require "location_set"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local M = wesnoth.map
local ca_healer_move, best_healer, best_hex = {}
@ -43,7 +44,7 @@ function ca_healer_move:evaluation(cfg, data)
if (not is_village) then
local is_healee = true
for _,healer in ipairs(healers_noMP) do
if (H.distance_between(healee.x, healee.y, healer.x, healer.y) == 1) then
if (M.distance_between(healee.x, healee.y, healer.x, healer.y) == 1) then
is_healee = false
break
end
@ -77,7 +78,7 @@ function ca_healer_move:evaluation(cfg, data)
local unit_in_way = wesnoth.get_unit(loc[1], loc[2])
if (not unit_in_way) or (unit_in_way == healer) then
for _,healee in ipairs(healees) do
if (H.distance_between(healee.x, healee.y, loc[1], loc[2]) == 1) then
if (M.distance_between(healee.x, healee.y, loc[1], loc[2]) == 1) then
-- Note: These ratings have to be positive or the method doesn't work
rating = rating + healee.max_hitpoints - healee.hitpoints

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local function get_sheep(cfg)
local sheep = wesnoth.get_units {
@ -54,13 +55,13 @@ function ca_herding_attack_close_enemy:execution(cfg)
local reach_map = AH.get_reachable_unocc(dog)
reach_map:iter( function(x, y, v)
-- Most important: distance from enemy
local rating = - H.distance_between(x, y, enemy.x, enemy.y) * 100.
local rating = -M.distance_between(x, y, enemy.x, enemy.y) * 100.
-- 2nd: distance from any sheep
for _,single_sheep in ipairs(sheep) do
rating = rating - H.distance_between(x, y, single_sheep.x, single_sheep.y)
rating = rating - M.distance_between(x, y, single_sheep.x, single_sheep.y)
end
-- 3rd: most distant dog goes first
rating = rating + H.distance_between(enemy.x, enemy.y, dog.x, dog.y) / 100.
rating = rating + M.distance_between(enemy.x, enemy.y, dog.x, dog.y) / 100.
reach_map:insert(x, y, rating)
if (rating > max_rating) then
@ -93,7 +94,7 @@ function ca_herding_attack_close_enemy:execution(cfg)
local min_dist, closest_sheep, closest_enemy = 9e99
for _,enemy in ipairs(enemies) do
for _,single_sheep in ipairs(sheep) do
local dist = H.distance_between(enemy.x, enemy.y, single_sheep.x, single_sheep.y)
local dist = M.distance_between(enemy.x, enemy.y, single_sheep.x, single_sheep.y)
if dist < min_dist then
min_dist = dist
closest_sheep, closest_enemy = single_sheep, enemy
@ -108,14 +109,14 @@ function ca_herding_attack_close_enemy:execution(cfg)
reach_map:iter( function(x, y, v)
-- We want equal distance between enemy and closest sheep
local rating = - math.abs(
H.distance_between(x, y, closest_sheep.x, closest_sheep.y)
- H.distance_between(x, y, closest_enemy.x, closest_enemy.y)
M.distance_between(x, y, closest_sheep.x, closest_sheep.y)
- M.distance_between(x, y, closest_enemy.x, closest_enemy.y)
) * 100
-- 2nd: closeness to sheep
rating = rating - H.distance_between(x, y, closest_sheep.x, closest_sheep.y)
rating = rating - M.distance_between(x, y, closest_sheep.x, closest_sheep.y)
reach_map:insert(x, y, rating)
-- 3rd: most distant dog goes first
rating = rating + H.distance_between(closest_enemy.x, closest_enemy.y, dog.x, dog.y) / 100.
rating = rating + M.distance_between(closest_enemy.x, closest_enemy.y, dog.x, dog.y) / 100.
reach_map:insert(x, y, rating)
if (rating > max_rating) then

View file

@ -1,6 +1,7 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
local function get_dog(cfg)
local dogs = AH.get_units_with_moves {
@ -27,7 +28,7 @@ function ca_herding_dog_move:execution(cfg)
-- Find average distance of herding_perimeter from center
local av_dist = 0
herding_perimeter:iter( function(x, y, v)
av_dist = av_dist + H.distance_between(x, y, cfg.herd_x, cfg.herd_y)
av_dist = av_dist + M.distance_between(x, y, cfg.herd_x, cfg.herd_y)
end)
av_dist = av_dist / herding_perimeter:size()
@ -39,7 +40,7 @@ function ca_herding_dog_move:execution(cfg)
rating = rating + 1000 + math.random(99) / 100.
else
rating = rating
- math.abs(H.distance_between(x, y, cfg.herd_x, cfg.herd_y) - av_dist)
- math.abs(M.distance_between(x, y, cfg.herd_x, cfg.herd_y) - av_dist)
+ math.random(99) / 100.
end

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local herding_area = wesnoth.require "ai/micro_ais/cas/ca_herding_f_herding_area.lua"
@ -46,21 +47,21 @@ function ca_herding_herd_sheep:execution(cfg)
local c_x, c_y = cfg.herd_x, cfg.herd_y
for _,single_sheep in ipairs(sheep_to_herd) do
-- Farthest sheep goes first
local sheep_rating = H.distance_between(c_x, c_y, single_sheep.x, single_sheep.y) / 10.
local sheep_rating = M.distance_between(c_x, c_y, single_sheep.x, single_sheep.y) / 10.
-- Sheep with no movement left gets big hit
if (single_sheep.moves == 0) then sheep_rating = sheep_rating - 100. end
for _,dog in ipairs(dogs) do
local reach_map = AH.get_reachable_unocc(dog)
reach_map:iter( function(x, y, v)
local dist = H.distance_between(x, y, single_sheep.x, single_sheep.y)
local dist = M.distance_between(x, y, single_sheep.x, single_sheep.y)
local rating = sheep_rating - dist
-- Needs to be on "far side" of sheep, wrt center for adjacent hexes
if (H.distance_between(x, y, c_x, c_y) <= H.distance_between(single_sheep.x, single_sheep.y, c_x, c_y))
if (M.distance_between(x, y, c_x, c_y) <= M.distance_between(single_sheep.x, single_sheep.y, c_x, c_y))
and (dist == 1)
then rating = rating - 1000 end
-- And the closer dog goes first (so that it might be able to chase another sheep afterward)
rating = rating - H.distance_between(x, y, dog.x, dog.y) / 100.
rating = rating - M.distance_between(x, y, dog.x, dog.y) / 100.
-- Finally, prefer to stay on path, if possible
if (wesnoth.match_location(x, y, H.get_child(cfg, "filter_location")) ) then rating = rating + 0.001 end

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local function get_next_sheep(cfg)
local sheep = AH.get_units_with_moves {
@ -30,11 +31,11 @@ function ca_herding_sheep_runs_dog:execution(cfg)
local c_x, c_y = cfg.herd_x, cfg.herd_y
-- If dog is farther from center, sheep moves in, otherwise it moves out
local sign = 1
if (H.distance_between(dog.x, dog.y, c_x, c_y) >= H.distance_between(sheep.x, sheep.y, c_x, c_y)) then
if (M.distance_between(dog.x, dog.y, c_x, c_y) >= M.distance_between(sheep.x, sheep.y, c_x, c_y)) then
sign = -1
end
local best_hex = AH.find_best_move(sheep, function(x, y)
return H.distance_between(x, y, c_x, c_y) * sign
return M.distance_between(x, y, c_x, c_y) * sign
end)
AH.movefull_stopunit(ai, sheep, best_hex)

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local function get_next_sheep_enemies(cfg)
local sheep = AH.get_units_with_moves {
@ -17,7 +18,7 @@ local function get_next_sheep_enemies(cfg)
for _,single_sheep in ipairs(sheep) do
local close_enemies = {}
for _,enemy in ipairs(enemies) do
if (H.distance_between(single_sheep.x, single_sheep.y, enemy.x, enemy.y) <= attention_distance) then
if (M.distance_between(single_sheep.x, single_sheep.y, enemy.x, enemy.y) <= attention_distance) then
table.insert(close_enemies, enemy)
end
end
@ -42,7 +43,7 @@ function ca_herding_sheep_runs_enemy:execution(cfg)
local best_hex = AH.find_best_move(sheep, function(x, y)
local rating = 0
for _,enemy in ipairs(close_enemies) do
rating = rating + H.distance_between(x, y, enemy.x, enemy.y)
rating = rating + M.distance_between(x, y, enemy.x, enemy.y)
end
return rating
end)

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local M = wesnoth.map
local function hunter_attack_weakest_adj_enemy(ai, hunter)
-- Attack the enemy with the fewest hitpoints adjacent to 'hunter', if there is one
@ -77,7 +78,7 @@ function ca_hunter:execution(cfg)
local max_rating, best_hex = -9e99
reach_map:iter( function(x, y, v)
-- Distance from goal is first rating
local rating = - H.distance_between(x, y, hunter_vars.goal_x, hunter_vars.goal_y)
local rating = -M.distance_between(x, y, hunter_vars.goal_x, hunter_vars.goal_y)
-- Huge rating bonus if this is next to an enemy
local enemy_hp = 500
@ -137,7 +138,7 @@ function ca_hunter:execution(cfg)
if (not hunter) or (not hunter.valid) then return end
-- If there's an enemy on the 'home' hex and we got right next to it, attack that enemy
if (H.distance_between(cfg.home_x, cfg.home_y, hunter.x, hunter.y) == 1) then
if (M.distance_between(cfg.home_x, cfg.home_y, hunter.x, hunter.y) == 1) then
local enemy = wesnoth.get_unit(cfg.home_x, cfg.home_y)
if AH.is_attackable_enemy(enemy) then
if cfg.show_messages then

View file

@ -78,7 +78,7 @@ local function messenger_find_clearing_attack(messenger, goal_x, goal_y, cfg)
-- Give a huge bonus for closeness to enemy_in_way
local tmp_defender = wesnoth.get_unit(attack.target.x, attack.target.y)
local dist = H.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

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local M = wesnoth.map
local messenger_next_waypoint = wesnoth.require "ai/micro_ais/cas/ca_messenger_f_next_waypoint.lua"
@ -53,7 +54,7 @@ function ca_messenger_escort_move:execution(cfg)
-- Give somewhat of a bonus for the messenger that has moved the farthest through the waypoints
local max_messenger_rating = -9e99
for _,m in ipairs(messengers) do
local messenger_rating = 1. / (H.distance_between(x, y, m.x, m.y) + 2.)
local messenger_rating = 1. / (M.distance_between(x, y, m.x, m.y) + 2.)
local wp_rating = MAIUV.get_mai_unit_variables(m, cfg.ai_id, "wp_rating")
messenger_rating = messenger_rating * 10. * (1. + wp_rating * 2.)
@ -67,7 +68,7 @@ function ca_messenger_escort_move:execution(cfg)
-- Distance from (sum of) enemies is important too
-- This favors placing escort units between the messenger and close enemies
for _,e in ipairs(enemies) do
base_rating = base_rating + 1. / (H.distance_between(x, y, e.x, e.y) + 2.)
base_rating = base_rating + 1. / (M.distance_between(x, y, e.x, e.y) + 2.)
end
base_rating_map:insert(x, y, base_rating)

View file

@ -32,7 +32,7 @@ return function(cfg)
-- If this messenger is within 3 hexes of the next waypoint, we go on to the one after that
-- except if it's the last one
local dist_wp = H.distance_between(messenger.x, messenger.y, wp_x, wp_y)
local dist_wp = wesnoth.map.distance_between(messenger.x, messenger.y, wp_x, wp_y)
if (dist_wp <= 3) and (wp_i < #waypoint_x) then wp_i = wp_i + 1 end
-- Also store the rating for each messenger

View file

@ -52,7 +52,7 @@ function ca_protect_unit_move:execution(cfg, data)
local goal_distance_map = LS.create()
reach_map:iter(function(x, y, data)
goal_distance_map:insert(x, y, H.distance_between(x, y, goal[1], goal[2]))
goal_distance_map:insert(x, y, wesnoth.map.distance_between(x, y, goal[1], goal[2]))
end)
-- Configuration parameters (no option to change these enabled at the moment)

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local function get_guardian(cfg)
local filter = H.get_child(cfg, "filter") or { id = cfg.id }
@ -38,8 +39,8 @@ function ca_stationed_guardian:execution(cfg)
-- simultaneously for guardian to attack
local min_dist, target = 9e99
for _,enemy in ipairs(enemies) do
local dist_s = H.distance_between(cfg.station_x, cfg.station_y, enemy.x, enemy.y)
local dist_g = H.distance_between(cfg.guard_x or cfg.station_x, cfg.guard_y or cfg.station_y, enemy.x, enemy.y)
local dist_s = M.distance_between(cfg.station_x, cfg.station_y, enemy.x, enemy.y)
local dist_g = M.distance_between(cfg.guard_x or cfg.station_x, cfg.guard_y or cfg.station_y, enemy.x, enemy.y)
-- If valid target found, save the one with the shortest distance from (g_x, g_y)
if (dist_s <= cfg.distance) and (dist_g <= cfg.distance) and (dist_g < min_dist) then
@ -83,7 +84,7 @@ function ca_stationed_guardian:execution(cfg)
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian)
then
local dist = H.distance_between(hex[1], hex[2], target.x, target.y)
local dist = M.distance_between(hex[1], hex[2], target.x, target.y)
if (dist < min_dist) then
min_dist, nh = dist, { hex[1], hex[2] }
end

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local ca_swarm_move = {}
@ -39,23 +40,23 @@ function ca_swarm_move:execution(cfg)
-- Only units within 'vision_distance' count for rejoining
local close_units_no_moves = {}
for _,unit_noMP in ipairs(units_no_moves) do
if (H.distance_between(unit.x, unit.y, unit_noMP.x, unit_noMP.y) <= vision_distance) then
if (M.distance_between(unit.x, unit.y, unit_noMP.x, unit_noMP.y) <= vision_distance) then
table.insert(close_units_no_moves, unit_noMP)
end
end
-- If all units on the side have moves left, simply go to a hex far away
if (not close_units_no_moves[1]) then
rating = rating + H.distance_between(x, y, unit.x, unit.y)
rating = rating + M.distance_between(x, y, unit.x, unit.y)
else -- Otherwise, minimize distance from units that have already moved
for _,close_unit in ipairs(close_units_no_moves) do
rating = rating - H.distance_between(x, y, close_unit.x, close_unit.y)
rating = rating - M.distance_between(x, y, close_unit.x, close_unit.y)
end
end
-- We also try to stay out of attack range of any enemy
for _,enemy in ipairs(enemies) do
local dist = H.distance_between(x, y, enemy.x, enemy.y)
local dist = M.distance_between(x, y, enemy.x, enemy.y)
if (dist < enemy_distance) then
rating = rating - (enemy_distance - dist) * 10.
end

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local M = wesnoth.map
local function get_enemies(cfg)
local scatter_distance = cfg.scatter_distance or 3
@ -33,7 +34,7 @@ function ca_swarm_scatter:execution(cfg)
for _,unit in ipairs(units) do
local unit_enemies = {}
for _,enemy in ipairs(enemies) do
if (H.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= vision_distance) then
if (M.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= vision_distance) then
table.insert(unit_enemies, enemy)
end
end
@ -42,7 +43,7 @@ function ca_swarm_scatter:execution(cfg)
local best_hex = AH.find_best_move(unit, function(x, y)
local rating = 0
for _,enemy in ipairs(unit_enemies) do
rating = rating + H.distance_between(x, y, enemy.x, enemy.y)
rating = rating + M.distance_between(x, y, enemy.x, enemy.y)
end
return rating
end)

View file

@ -1,6 +1,7 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local M = wesnoth.map
local function get_wolves(cfg)
local wolves = AH.get_units_with_moves {
@ -38,7 +39,7 @@ function ca_wolves_move:execution(cfg)
for _,prey_unit in ipairs(prey) do
local dist = 0
for _,wolf in ipairs(wolves) do
dist = dist + H.distance_between(wolf.x, wolf.y, prey_unit.x, prey_unit.y)
dist = dist + M.distance_between(wolf.x, wolf.y, prey_unit.x, prey_unit.y)
end
if (dist < min_dist) then
min_dist, target = dist, prey_unit
@ -47,13 +48,13 @@ function ca_wolves_move:execution(cfg)
-- Now sort wolf from farthest to closest
table.sort(wolves, function(a, b)
return H.distance_between(a.x, a.y, target.x, target.y) > H.distance_between(b.x, b.y, target.x, target.y)
return M.distance_between(a.x, a.y, target.x, target.y) > M.distance_between(b.x, b.y, target.x, target.y)
end)
-- First wolf moves toward target, but tries to stay away from map edges
local width, height = wesnoth.get_map_size()
local wolf1 = AH.find_best_move(wolves[1], function(x, y)
local dist_1t = H.distance_between(x, y, target.x, target.y)
local dist_1t = M.distance_between(x, y, target.x, target.y)
local rating = - dist_1t
if (x <= 5) then rating = rating - (6 - x) / 1.4 end
if (y <= 5) then rating = rating - (6 - y) / 1.4 end
@ -80,13 +81,13 @@ function ca_wolves_move:execution(cfg)
-- We ideally want wolves to be 2-3 hexes from each other
-- but this requirement gets weaker and weaker with increasing wolf number
for j = 1,i-1 do
local dst = H.distance_between(x, y, wolves[j].x, wolves[j].y)
local dst = M.distance_between(x, y, wolves[j].x, wolves[j].y)
rating = rating - (dst - 2.7 * j)^2 / j
end
-- Same distance from Wolf 1 and target for all the wolves
local dist_t = H.distance_between(x, y, target.x, target.y)
local dist_1t = H.distance_between(wolf1[1], wolf1[2], target.x, target.y)
local dist_t = M.distance_between(x, y, target.x, target.y)
local dist_1t = M.distance_between(wolf1[1], wolf1[2], target.x, target.y)
rating = rating - (dist_t - dist_1t)^2
-- Hexes that avoid_type units can reach get a massive penalty

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local WMPF = wesnoth.require "ai/micro_ais/cas/ca_wolves_multipacks_functions.lua"
local M = wesnoth.map
local ca_wolves_multipacks_attack = {}
@ -46,7 +47,7 @@ function ca_wolves_multipacks_attack:execution(cfg)
local attack_splits_pack = false
for _,wolf in ipairs(wolves) do
local nh = AH.next_hop(wolf, attack.dst.x, attack.dst.y)
local dist = H.distance_between(nh[1], nh[2], attack.dst.x, attack.dst.y)
local dist = M.distance_between(nh[1], nh[2], attack.dst.x, attack.dst.y)
if (dist > 3) then
attack_splits_pack = true
break
@ -165,7 +166,7 @@ function ca_wolves_multipacks_attack:execution(cfg)
local best_hex = AH.find_best_move(wolf_moves, function(x, y)
local rating = 0
for _,wolf_no_moves in ipairs(wolves_no_moves) do
rating = rating - H.distance_between(x, y, wolf_no_moves.x, wolf_no_moves.y)
rating = rating - M.distance_between(x, y, wolf_no_moves.x, wolf_no_moves.y)
end
return rating
end)

View file

@ -1,6 +1,7 @@
local H = wesnoth.require "helper"
local W = H.set_wml_action_metatable {}
local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local M = wesnoth.map
local wolves_multipacks_functions = {}
@ -60,8 +61,8 @@ function wolves_multipacks_functions.assign_packs(cfg)
local min_dist, best_wolf, best_ind = 9e99
for ind,wolf in ipairs(nopack_wolves) do
-- Criterion is distance from the first two wolves of the pack
local dist1 = H.distance_between(wolf.x, wolf.y, pack[1].x, pack[1].y)
local dist2 = H.distance_between(wolf.x, wolf.y, pack[2].x, pack[2].y)
local dist1 = M.distance_between(wolf.x, wolf.y, pack[1].x, pack[1].y)
local dist2 = M.distance_between(wolf.x, wolf.y, pack[2].x, pack[2].y)
if (dist1 + dist2 < min_dist) then
min_dist = dist1 + dist2
best_wolf, best_ind = wolf, ind
@ -100,7 +101,7 @@ function wolves_multipacks_functions.assign_packs(cfg)
for ind,nopack_wolf in ipairs(nopack_wolves) do
local dist = 0
for _,pack_wolf in ipairs(new_pack_wolves) do
dist = dist + H.distance_between(nopack_wolf.x, nopack_wolf.y, pack_wolf.x, pack_wolf.y)
dist = dist + M.distance_between(nopack_wolf.x, nopack_wolf.y, pack_wolf.x, pack_wolf.y)
end
if dist < min_dist then
min_dist, best_wolf, best_wolf_ind = dist, nopack_wolf, ind

View file

@ -3,6 +3,7 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local MAIUV = wesnoth.require "ai/micro_ais/micro_ai_unit_variables.lua"
local LS = wesnoth.require "location_set"
local WMPF = wesnoth.require "ai/micro_ais/cas/ca_wolves_multipacks_functions.lua"
local M = wesnoth.map
local ca_wolves_multipacks_wander = {}
@ -85,7 +86,7 @@ function ca_wolves_multipacks_wander:execution(cfg)
reach_map:iter( function(x, y, v)
local rating = reach_map:get(x, y)
if (rating == #pack * 100) then
rating = rating - H.distance_between(x, y, goal[1], goal[2])
rating = rating - M.distance_between(x, y, goal[1], goal[2])
reach_map:insert(x,y, rating)
if rating > max_rating then
max_rating, goto_hex = rating, { x, y }
@ -111,7 +112,7 @@ function ca_wolves_multipacks_wander:execution(cfg)
-- Find closest move for Wolf #1 to that position, which then becomes the goto hex
goto_hex = AH.find_best_move(wolves[1], function(x, y)
return -H.distance_between(x, y, cg[1], cg[2])
return -M.distance_between(x, y, cg[1], cg[2])
end)
-- We could move this wolf right here, but for convenience all the actual moves
-- are done together below. This should be a small extra calculation cost
@ -121,8 +122,8 @@ function ca_wolves_multipacks_wander:execution(cfg)
-- Distance to goal hex is taken into account as secondary criterion
for _,wolf in ipairs(wolves) do
local best_hex = AH.find_best_move(wolf, function(x, y)
local rating = - H.distance_between(x, y, goto_hex[1], goto_hex[2])
rating = rating - H.distance_between(x, y, goal[1], goal[2]) / 100.
local rating = -M.distance_between(x, y, goto_hex[1], goto_hex[2])
rating = rating -M.distance_between(x, y, goal[1], goal[2]) / 100.
return rating
end)

View file

@ -47,7 +47,7 @@ function ca_wolves_wander:execution(cfg)
for _,wolf in ipairs(wolves) do
-- For each wolf, we need to check that goal hex is reachable, and out of harm's way
local best_hex = AH.find_best_move(wolf, function(x, y)
local rating = - H.distance_between(x, y, goal_hex[1], goal_hex[2])
local rating = -wesnoth.map.distance_between(x, y, goal_hex[1], goal_hex[2])
if avoid_map:get(x, y) then rating = rating - 1000 end
return rating
end)

View file

@ -1,6 +1,7 @@
local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
local function get_guardian(cfg)
local filter = H.get_child(cfg, "filter") or { id = cfg.id }
@ -28,7 +29,7 @@ function ca_zone_guardian:execution(cfg)
if enemies[1] then
local min_dist, target = 9e99
for _,enemy in ipairs(enemies) do
local dist = H.distance_between(guardian.x, guardian.y, enemy.x, enemy.y)
local dist = M.distance_between(guardian.x, guardian.y, enemy.x, enemy.y)
if (dist < min_dist) then
target, min_dist = enemy, dist
end
@ -70,7 +71,7 @@ function ca_zone_guardian:execution(cfg)
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian)
then
local dist = H.distance_between(hex[1], hex[2], target.x, target.y)
local dist = M.distance_between(hex[1], hex[2], target.x, target.y)
if (dist < min_dist) then
min_dist, nh = dist, { hex[1], hex[2] }
end

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "helper"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
--local LS = wesnoth.require "location_set"
local M = wesnoth.map
local ca_ogres_flee = {}
@ -43,8 +44,8 @@ function ca_ogres_flee:execution()
-- First rating is distance from a map edge
local dist_left = r[1] - 1
local dist_right = width - r[1]
local dist_top_left = H.distance_between(r[1], r[2], 4, 1)
local dist_top_right = H.distance_between(r[1], r[2], 40, 1)
local dist_top_left = M.distance_between(r[1], r[2], 4, 1)
local dist_top_right = M.distance_between(r[1], r[2], 40, 1)
local dist_bottom = height - r[2]
local dist = math.min(dist_left, dist_right, dist_top_left, dist_top_right, dist_bottom)
@ -58,7 +59,7 @@ function ca_ogres_flee:execution()
local enemy_rating = 0
for k,e in ipairs(enemies) do
local dist = H.distance_between(r[1], r[2], e.x, e.y)
local dist = M.distance_between(r[1], r[2], e.x, e.y)
enemy_rating = enemy_rating + math.sqrt(dist)
end
@ -68,7 +69,7 @@ function ca_ogres_flee:execution()
local own_unit_weight = 0.5
local own_unit_rating = 0
for k,u_noMP in ipairs(units_noMP) do
local dist = H.distance_between(r[1], r[2], u_noMP.x, u_noMP.y)
local dist = M.distance_between(r[1], r[2], u_noMP.x, u_noMP.y)
own_unit_rating = own_unit_rating + math.sqrt(dist)
end

View file

@ -1,5 +1,6 @@
local H = wesnoth.require "helper"
local LS = wesnoth.require "location_set"
local M = wesnoth.map
local ca_transport = {}
@ -57,14 +58,14 @@ function ca_transport:execution()
for i,u in ipairs(transports) do
local dst = { u.variables.destination_x, u.variables.destination_y }
if (not u.variables.landed) and (H.distance_between(u.x, u.y, dst[1], dst[2]) <= u.moves) then
if (not u.variables.landed) and (M.distance_between(u.x, u.y, dst[1], dst[2]) <= u.moves) then
local reach = wesnoth.find_reach(u)
for i,r in ipairs(reach) do
if landing_site_map:get(r[1], r[2]) and (not unit_map:get(r[1], r[2]))
then
-- Distance from destination is minor rating
local rating = -H.distance_between(r[1], r[2], dst[1], dst[2]) / 100.
local rating = -M.distance_between(r[1], r[2], dst[1], dst[2]) / 100.
-- Main rating is number of unoccupied land hexes and
-- water hexes next to land hexes
@ -153,7 +154,7 @@ function ca_transport:execution()
local max_rating_unit, best_hex_unit = -9e99, {}
for i,r in ipairs(reach) do
if deep_water_map:get(r[1], r[2]) and (not blocked_hex_map:get(r[1], r[2])) then
local rating = -H.distance_between(r[1], r[2], dst[1], dst[2])
local rating = -M.distance_between(r[1], r[2], dst[1], dst[2])
-- If possible, also move in a straight line
rating = rating - math.abs(r[1] - dst[1]) / 100.
rating = rating - math.abs(r[2] - dst[2]) / 100.

View file

@ -40,7 +40,7 @@ function wesnoth.wml_actions.find_path(cfg)
-- we test if location passed to pathfinder is invalid (border); if is, do nothing, do not return and continue the cycle
if location[1] == 0 or location[1] == ( width + 1 ) or location[2] == 0 or location[2] == ( heigth + 1 ) then
else
local distance = helper.distance_between ( unit.x, unit.y, location[1], location[2] )
local distance = wesnoth.map.distance_between ( unit.x, unit.y, location[1], location[2] )
-- if we pass an unreachable locations an high value will be returned
local path, cost = wesnoth.find_path( unit, location[1], location[2], { max_cost = max_cost, ignore_units = ignore_units, ignore_teleport = ignore_teleport, viewing_side = viewing_side } )

View file

@ -47,7 +47,7 @@ wesnoth.wml_actions.random_placement = function(cfg)
local y1 = locs[j][2]
local x2 = point[1]
local y2 = point[2]
-- optimisation: same effect as "if helper.distance_between(x1,y1,x2,y2) <= distance then goto continue; end" but faster.
-- optimisation: same effect as "if wesnoth.map.distance_between(x1,y1,x2,y2) <= distance then goto continue; end" but faster.
local d_x = math_abs(x1-x2)
if d_x > distance then
goto continue