MinchinWeb's MetaLibrary  v.9 Library functions of OpenTTD AI writers.

A Road Pathfinder (and extras) More...

class  Cost

class  Info

## Public Member Functions

function InitializePath (sources, goals)
Initialize a path search between sources and goals. More...

function FindPath (iterations)
Try to find the path as indicated with InitializePath with the lowest cost. More...

function PresetOriginal ()
The settings in the original (v3) pathfinder by NoAI Team. More...

function PresetPerfectPath ()
Good preset for reusing existing roads. More...

function PresetQuickAndDirty ()
Quick but messy preset. More...

function PresetCheckExisting ()
Preset that only uses existing roads. More...

function PresetStreetcar ()
Reserved. More...

function GetBuildCost ()
Cost to build found path. More...

function BuildPath ()
Build the found path. More...

function GetPath ()
Export the found path. More...

function GetPathLength ()
Get the length of the found path. More...

function InitializePathOnTowns ()
Initializes the pathfinder using two towns. More...

function PathToTilePairs ()
Get the found path as tile pairs. More...

function PathToTiles ()
Get a list of all the tiles in the path. More...

function TilePairsToBuild ()
Tiles in the path that need to be built. More...

## Private Member Functions

constructor ()

function _GetBridgeNumSlopes (end_a, end_b)

function _Cost (self, path, new_tile, new_direction)

function _Estimate (self, cur_tile, cur_direction, goal_tiles)

function _Neighbours (self, path, cur_node)

function _CheckDirection (self, tile, existing_direction, new_direction)

function _GetDirection (from, to, is_bridge)

function _GetTunnelsBridges (last_node, cur_node, bridge_dir)

function _CheckTunnelBridge (current_tile, new_tile)

## Private Attributes

_aystar_class = import("graph.aystar", "", 6)

_max_cost = null
The maximum cost for a route. More...

_cost_tile = null
The cost for a single tile. More...

The cost that is added to _cost_tile if no road exists yet. More...

_cost_turn = null
The cost that is added to _cost_tile if the direction changes. More...

_cost_slope = null
The extra cost if a road tile is sloped. More...

_cost_bridge_per_tile = null
The cost per tile of a new bridge, this is added to _cost_tile. More...

_cost_tunnel_per_tile = null
The cost per tile of a new tunnel, this is added to _cost_tile. More...

_cost_coast = null
The extra cost for a coast tile. More...

_cost_level_crossing = null
the extra cost for rail/road level crossings. More...

_cost_drivethru_station = null
The extra cost for drive-thru road stations. More...

_pathfinder = null
A reference to the used AyStar object. More...

_max_bridge_length = null
The maximum length of a bridge that will be build. More...

_max_tunnel_length = null
The maximum length of a tunnel that will be build. More...

Choose whether to only search through existing connected roads. More...

_distance_penalty = null
Penalty to use to speed up pathfinder, 1 is no penalty. More...

cost = null
Used to change the costs. More...

_mypath = null
Used to store the path after it's been found for Building functions. More...

_running = null

info = null

## Detailed Description

Note
Version
v.9 (2012-12-28)
Since
MetaLibrary v.1

This road pathfinder tries to find a buildable / existing route for road vehicles. You can changes the costs below using for example roadpf.cost.turn = 30. Note that it's not allowed to change the cost between consecutive calls to FindPath. You can change the cost before the first call to FindPath and after FindPath has returned an actual route. To use only existing roads, set roadpf.cost.only_existing_road = True.

The pathfinder has been extended to provide 'presets' for configuration, store the found path, and build the found path.

Depends On:
Graph.AyStar v6
_MinchinWeb_DLS_
_MinchinWeb_ShipPathfinder_
Todo:

if(AIRoad.AreRoadTilesConnected(new_tile, prev_tile) &&

Todo:
allow pre-building of tunnels and bridges

Definition at line 83 of file Pathfinder.Road.nut.

## Member Function Documentation

 function _MinchinWeb_RoadPathfinder_::_CheckDirection ( self , tile , existing_direction , new_direction )
private

Definition at line 517 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::_CheckTunnelBridge ( current_tile , new_tile )
private

Definition at line 601 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::_Cost ( self , path , new_tile , new_direction )
private

Definition at line 360 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::_Estimate ( self , cur_tile , cur_direction , goal_tiles )
private

Definition at line 432 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::_GetBridgeNumSlopes ( end_a , end_b )
private

Definition at line 340 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::_GetDirection ( from , to , is_bridge )
private

Definition at line 521 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::_GetTunnelsBridges ( last_node , cur_node , bridge_dir )
private

Get a list of all bridges and tunnels that can be build from the current tile. Bridges will only be build starting on non-flat tiles for performance reasons. Tunnels will only be build if no terraforming is needed on both ends.

Definition at line 546 of file Pathfinder.Road.nut.

private

Definition at line 573 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::_Neighbours ( self , path , cur_node )
private

Definition at line 442 of file Pathfinder.Road.nut.

Build the found path.

BuildPath()

Definition at line 826 of file Pathfinder.Road.nut.

inlineprivate

Definition at line 107 of file Pathfinder.Road.nut.

Try to find the path as indicated with InitializePath with the lowest cost.

Parameters
 iterations After how many iterations it should abort for a moment. This value should either be -1 for infinite, or > 0. Any other value aborts immediately and will never find a path.
Returns
A route if one was found, or false if the amount of iterations was reached, or null if no path was found. You can call this function over and over as long as it returns false, which is an indication it is not yet done looking for a route.
AyStar::FindPath()

Definition at line 332 of file Pathfinder.Road.nut.

Cost to build found path.

Turns to 'test mode,' builds the route provided, and returns the cost.

Note
All money for AI's is in British Pounds.
Due to inflation, this value can get stale
Returns
Cost to build path
False if the test build fails somewhere

Definition at line 747 of file Pathfinder.Road.nut.

Export the found path.

Returns
The path stored by the pathfinder
GetPath()
BuildPath()

Definition at line 918 of file Pathfinder.Road.nut.

Get the length of the found path.

Runs over the path to determine its length.

Returns
The length of the path in tiles.

Definition at line 927 of file Pathfinder.Road.nut.

 function _MinchinWeb_RoadPathfinder_::InitializePath ( sources , goals )
inline

Initialize a path search between sources and goals.

Parameters
 sources The source tiles. goals The target tiles.
AyStar::InitializePath()
InitializePathOnTowns()

Definition at line 140 of file Pathfinder.Road.nut.

Initializes the pathfinder using two towns.

Note
Assumes that the town centres are road tiles. If this is not the case, the pathfinder will still run, but it will take a long time and eventually fail to return a path. This is generally not an issue because on map creation the centre of town will be a road tile.
InitializePath()

Definition at line 941 of file Pathfinder.Road.nut.

'Loads' a path to allow GetBuildCost(), BuildPath() and GetPathLength() to be used.

GetBuildCost()
FindPath()

Definition at line 908 of file Pathfinder.Road.nut.

Get the found path as tile pairs.

Returns
2D array that has each pair of tiles that path joins.
TilePairsToBuild()
PathToTiles()

Definition at line 949 of file Pathfinder.Road.nut.

Get a list of all the tiles in the path.

Returns
1D array that has each pair of tiles that path covers.
PathToTilePairs()

Definition at line 975 of file Pathfinder.Road.nut.

Preset that only uses existing roads.

Based on PerfectPath, but uses only existing roads. Useful for checking if there an existing route and how long it is.

Definition at line 721 of file Pathfinder.Road.nut.

The settings in the original (v3) pathfinder by NoAI Team.

Good preset for reusing existing roads.

My slightly updated version of PresetOriginal().

Definition at line 677 of file Pathfinder.Road.nut.

Quick but messy preset.

Runs in as little as 5% of the time of PresetPerfectPath(), but builds odd bridges and loops.

Definition at line 698 of file Pathfinder.Road.nut.

Reserved.

Reserved preset for future use for intraurban tram lines.

Definition at line 742 of file Pathfinder.Road.nut.

Tiles in the path that need to be built.

Similar to PathToTilePairs(), but only returns those pairs where there isn't a current road connection.

Returns
2D array that has each pair of tiles that path joins that are not currently joined by road.
BuildPath()

Definition at line 997 of file Pathfinder.Road.nut.

## Member Data Documentation

private

Definition at line 84 of file Pathfinder.Road.nut.

private

The cost per tile of a new bridge, this is added to _cost_tile.

Definition at line 90 of file Pathfinder.Road.nut.

private

The extra cost for a coast tile.

Definition at line 92 of file Pathfinder.Road.nut.

private

The extra cost for drive-thru road stations.

Definition at line 94 of file Pathfinder.Road.nut.

private

the extra cost for rail/road level crossings.

Definition at line 93 of file Pathfinder.Road.nut.

private

The cost that is added to _cost_tile if no road exists yet.

Definition at line 87 of file Pathfinder.Road.nut.

private

Choose whether to only search through existing connected roads.

Definition at line 98 of file Pathfinder.Road.nut.

private

The extra cost if a road tile is sloped.

Definition at line 89 of file Pathfinder.Road.nut.

private

The cost for a single tile.

Definition at line 86 of file Pathfinder.Road.nut.

private

The cost per tile of a new tunnel, this is added to _cost_tile.

Definition at line 91 of file Pathfinder.Road.nut.

private

The cost that is added to _cost_tile if the direction changes.

Definition at line 88 of file Pathfinder.Road.nut.

private

Penalty to use to speed up pathfinder, 1 is no penalty.

Definition at line 99 of file Pathfinder.Road.nut.

private

The maximum length of a bridge that will be build.

Definition at line 96 of file Pathfinder.Road.nut.

private

The maximum cost for a route.

Definition at line 85 of file Pathfinder.Road.nut.

private

The maximum length of a tunnel that will be build.

Definition at line 97 of file Pathfinder.Road.nut.

private

Used to store the path after it's been found for Building functions.

Definition at line 102 of file Pathfinder.Road.nut.

private

A reference to the used AyStar object.

Definition at line 95 of file Pathfinder.Road.nut.

private

Definition at line 100 of file Pathfinder.Road.nut.

private

Definition at line 103 of file Pathfinder.Road.nut.