Difference between revisions of "LuaWML/Pathfinder"

From The Battle for Wesnoth Wiki
m (Vultraz moved page LuaWML:Pathfinder to LuaWML/Pathfinder)
(Added comments about hidden units)
Line 8: Line 8:
 
Returns the shortest path from one location to another. The source location is given either by coordinates as two arguments x and y; there must be a unit at the source location when using the standard path calculator. The source location can also be given by a unit as a single argument (as returned by the functions from [[LuaWML:Units]]). The second location is given by its coordinates. The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:
 
Returns the shortest path from one location to another. The source location is given either by coordinates as two arguments x and y; there must be a unit at the source location when using the standard path calculator. The source location can also be given by a unit as a single argument (as returned by the functions from [[LuaWML:Units]]). The second location is given by its coordinates. The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:
 
* '''max_cost''': if set, the pathfinder will ignore paths longer than its value
 
* '''max_cost''': if set, the pathfinder will ignore paths longer than its value
* '''ignore_units''': if set, the path will go through units and ignore zones of control
+
* '''ignore_units''': if set, the path will go through units and ignore zones of control. Note: hidden enemies are always ignored, whether this parameter is set or not, unless viewing_side (below) is set to an invalid side.
 
* '''ignore_teleport''': if set, the teleport ability of the unit is ignored
 
* '''ignore_teleport''': if set, the teleport ability of the unit is ignored
* '''viewing_side''': if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored; if left unset, the viewing side will be the unit side
+
* '''viewing_side''': if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored and hidden units will be taken into account as well; if left unset, the viewing side will be the unit side
  
 
The path is returned as a table of coordinate pairs. It contains both the source and destination tile if a path was found. The total cost of the path is also available as a second return value, if needed.
 
The path is returned as a table of coordinate pairs. It contains both the source and destination tile if a path was found. The total cost of the path is also available as a second return value, if needed.
Line 41: Line 41:
 
* '''wesnoth.find_vacant_tile(''x'', ''y'', [''unit''])'''
 
* '''wesnoth.find_vacant_tile(''x'', ''y'', [''unit''])'''
  
Returns the two coordinates of an empty tile the closest to the tile passed by coordinates. An optional unit (either a WML table or a proxy object) can be passed as a third argument; if so, the returned tile has terrain which is passable for the passed unit.
+
Returns the two coordinates of an empty tile the closest to the tile passed by coordinates. An optional unit (either a WML table or a proxy object) can be passed as a third argument; if so, the returned tile has terrain which is passable for the passed unit.  Note: hidden units are always seen by this function, no matter whether they are visible to the side of the passed unit.
  
 
  function teleport(src_x, src_y, dst_x, dst_y)
 
  function teleport(src_x, src_y, dst_x, dst_y)
Line 57: Line 57:
 
Returns all the locations reachable by a unit. The unit is given either by its two coordinates or by a proxy object.  The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:
 
Returns all the locations reachable by a unit. The unit is given either by its two coordinates or by a proxy object.  The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:
 
* '''additional_turns''': if set to an integer n, the pathfinder will consider tiles that can be reached in n+1 turns
 
* '''additional_turns''': if set to an integer n, the pathfinder will consider tiles that can be reached in n+1 turns
* '''ignore_units''': if set, the paths will go through units and ignore zones of control
+
* '''ignore_units''': if set, the paths will go through units and ignore zones of control. Note: hidden enemies are always ignored, whether this parameter is set or not, unless viewing_side (below) is set to an invalid side.
 
* '''ignore_teleport''': if set, the teleport ability of the unit is ignored
 
* '''ignore_teleport''': if set, the teleport ability of the unit is ignored
* '''viewing_side''': if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored; if left unset, the viewing side will be the unit side
+
* '''viewing_side''': if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored and hidden units will be taken into account as well; if left unset, the viewing side will be the unit side
 
The locations are stored as triples in an array. The first two elements of a triple are the coordinates of a reachable tile, the third one is the number of movement points left when reaching the tile.
 
The locations are stored as triples in an array. The first two elements of a triple are the coordinates of a reachable tile, the third one is the number of movement points left when reaching the tile.
  

Revision as of 14:38, 3 October 2016

This page describes the LuaWML functions and helpers for finding paths.

wesnoth.find_path

  • wesnoth.find_path(x1, y1, x2, y2, [path_options | cost_function])
  • wesnoth.find_path(x1, y1, x2, y2, cost_function, width, height), in map generation code. (see MapGeneratorWML#The_lua_generator)

Returns the shortest path from one location to another. The source location is given either by coordinates as two arguments x and y; there must be a unit at the source location when using the standard path calculator. The source location can also be given by a unit as a single argument (as returned by the functions from LuaWML:Units). The second location is given by its coordinates. The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:

  • max_cost: if set, the pathfinder will ignore paths longer than its value
  • ignore_units: if set, the path will go through units and ignore zones of control. Note: hidden enemies are always ignored, whether this parameter is set or not, unless viewing_side (below) is set to an invalid side.
  • ignore_teleport: if set, the teleport ability of the unit is ignored
  • viewing_side: if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored and hidden units will be taken into account as well; if left unset, the viewing side will be the unit side

The path is returned as a table of coordinate pairs. It contains both the source and destination tile if a path was found. The total cost of the path is also available as a second return value, if needed.

-- Display some items along the path from (x1,y1) to (x2,y2).
local u = wesnoth.get_units({ x = x1, y = y1 })[1]
local path, cost = wesnoth.find_path(u, x2, y2, { ignore_units = true, viewing_side = 0 })
if cost > u.moves then
    wesnoth.message("That's too far!")
else
    for i, loc in ipairs(path) do
        wesnoth.fire("item", { x = loc[1], y = loc[2], image = "items/buckler.png" })
    end
end

Instead of a parameter table, a cost function can be passed to the pathfinder. It will be called for all the tiles the computed path may possibly go through. It receives three arguments. The first two are the coordinates of the tile, the last one is the current cost for reaching that tile. The function should return a floating-point value that is the cost for entering the given tile. This cost should be greater or equal to one.

-- Count how many turns it would take, assuming the worst case (3 movement points per tile)
local max_moves = wesnoth.get_units({ x = x1, y = y1 })[1].max_moves
local path, cost = wesnoth.find_path(x1, y2, x2, y2,
    function(x, y, current_cost)
        local remaining_moves = max_moves - (current_cost % max_moves)
        if remaining_moves < 3 then current_cost = current_cost + remaining_moves end
        return current_cost + 3
    end)
wesnoth.message(string.format("It would take %d turns.", math.ceil(cost / 3)))

wesnoth.find_vacant_tile

  • wesnoth.find_vacant_tile(x, y, [unit])

Returns the two coordinates of an empty tile the closest to the tile passed by coordinates. An optional unit (either a WML table or a proxy object) can be passed as a third argument; if so, the returned tile has terrain which is passable for the passed unit. Note: hidden units are always seen by this function, no matter whether they are visible to the side of the passed unit.

function teleport(src_x, src_y, dst_x, dst_y)
  local u = wesnoth.get_units({x = src_x, y = src_y })[1]
  local ut = u.__cfg
  dst_x, dst_y = wesnoth.find_vacant_tile(dst_x, dst_y, u)
  wesnoth.put_unit(src_x, src_y)
  wesnoth.put_unit(dst_x, dst_y, ut)
end

wesnoth.find_reach

  • wesnoth.find_reach(unit, [path_options])

Returns all the locations reachable by a unit. The unit is given either by its two coordinates or by a proxy object. The last argument is an optional table that can be used to parametrize the pathfinder. Its options are:

  • additional_turns: if set to an integer n, the pathfinder will consider tiles that can be reached in n+1 turns
  • ignore_units: if set, the paths will go through units and ignore zones of control. Note: hidden enemies are always ignored, whether this parameter is set or not, unless viewing_side (below) is set to an invalid side.
  • ignore_teleport: if set, the teleport ability of the unit is ignored
  • viewing_side: if set to a valid side number, fog and shroud for this side will be taken into account; if set to an invalid number (e.g. 0), fog and shroud will be ignored and hidden units will be taken into account as well; if left unset, the viewing side will be the unit side

The locations are stored as triples in an array. The first two elements of a triple are the coordinates of a reachable tile, the third one is the number of movement points left when reaching the tile.

-- overlay the number of turns needed to reach each tile
local t = wesnoth.find_reach(u, { additional_turns = 8 })
local m = u.max_moves
for i,l in ipairs(t) do
  wesnoth.fire("label", { x = l[1], y = l[2], text = math.ceil(9 - l[3]/m) })
end

wesnoth.find_cost_map

Builds a cost map for one, multiple units or unit types. In a cost map each hex is mapped to two values: a) The summed cost to reach this hex for all input units b) A value which indicates how many units can reach this hex The caller can divide a) with b) to get a average cost to reach this hex for the input units. The costs will consider movement lost during turn changes. (So with simple calculus it is possible to get the turns to reach a hex)

Input arguments:

  • 1 + 2. A units location
  • OR 1. A unit
  • OR 1. A unit filter
  • 2. (optional) A array of triples (coordinates + unit type as string)
  • 3. (optional) A table with options: ignore_units, ignore_teleport, viewing_side, debug, use_max_moves
  • 4. (optional) A Standard Location Filter.

If the array of unit types is given the units will be added to the first parameter. Use a empty filter or a invalid location to only add unit types.

Return value: A array of quadruples (coordinates + a summed cost + reach count)

A location set can be build by calling location.set.of_pairs(retval).

helper.distance_between

  • helper.distance_between(x1, x2, y1, y2)

Returns the distance between two tiles given by their coordinates.

local d = distance_between(x1, y1, x2, y2)

helper.adjacent_tiles

  • helper.adjacent_tiles(x, y, [include_border])

Returns an iterator on the (at most six) tiles around a given location that are on the map. If the third argument is true, tiles on the map border are also visited.

-- remove all the units next to the (a,b) tile
for x, y in helper.adjacent_tiles(a, b) do
    wesnoth.put_unit(x, y)
end