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

A Road Pathfinder (and extras) More...

Classes

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 LoadPath ()
 Load an existing 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 _IsSlopedRoad (start, middle, end)
 
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...
 
 _cost_no_existing_road = null
 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...
 
 _cost_only_existing_roads = null
 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...
 
 _road_type = null
 
 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

A Road Pathfinder (and extras)

Note
This file is licensed under the original license – LGPL v2.1 – and is based on the NoAI Team's Road Pathfinder v3.
Version
v.9 (2012-12-28)
Author
NoAI Team
W. Minchin (MinchinWeb)
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
See also
_MinchinWeb_DLS_
_MinchinWeb_ShipPathfinder_
Todo:

upgrade slow bridges along path

convert existing level crossings (road/rail) to road bridge

do something about one-way roads - build a pair? route around?

Add example usage code.

if(AIRoad.AreRoadTilesConnected(new_tile, prev_tile) &&
!AIRoad.AreRoadTilesConnected(prev_tile, new_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.

function _MinchinWeb_RoadPathfinder_::_IsSlopedRoad ( start  ,
middle  ,
end   
)
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.

function _MinchinWeb_RoadPathfinder_::BuildPath ( )

Build the found path.

See also
BuildPath()

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

_MinchinWeb_RoadPathfinder_::constructor ( )
inlineprivate

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

function _MinchinWeb_RoadPathfinder_::FindPath ( iterations  )

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

Parameters
iterationsAfter 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.
See also
AyStar::FindPath()

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

function _MinchinWeb_RoadPathfinder_::GetBuildCost ( )

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.

function _MinchinWeb_RoadPathfinder_::GetPath ( )

Export the found path.

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

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

function _MinchinWeb_RoadPathfinder_::GetPathLength ( )

Get the length of the found path.

Runs over the path to determine its length.

Returns
The length of the path in tiles.
See also
LoadPath()

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
sourcesThe source tiles.
goalsThe target tiles.
See also
AyStar::InitializePath()
InitializePathOnTowns()

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

function _MinchinWeb_RoadPathfinder_::InitializePathOnTowns ( )

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.
See also
InitializePath()

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

function _MinchinWeb_RoadPathfinder_::LoadPath ( )

Load an existing path.

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

See also
GetBuildCost()
FindPath()

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

function _MinchinWeb_RoadPathfinder_::PathToTilePairs ( )

Get the found path as tile pairs.

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

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

function _MinchinWeb_RoadPathfinder_::PathToTiles ( )

Get a list of all the tiles in the path.

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

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

function _MinchinWeb_RoadPathfinder_::PresetCheckExisting ( )

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.

function _MinchinWeb_RoadPathfinder_::PresetOriginal ( )

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

function _MinchinWeb_RoadPathfinder_::PresetPerfectPath ( )

Good preset for reusing existing roads.

My slightly updated version of PresetOriginal().

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

function _MinchinWeb_RoadPathfinder_::PresetQuickAndDirty ( )

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.

function _MinchinWeb_RoadPathfinder_::PresetStreetcar ( )

Reserved.

Reserved preset for future use for intraurban tram lines.

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

function _MinchinWeb_RoadPathfinder_::TilePairsToBuild ( )

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.
See also
BuildPath()

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

Member Data Documentation

_MinchinWeb_RoadPathfinder_::_aystar_class = import("graph.aystar", "", 6)
private

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

_MinchinWeb_RoadPathfinder_::_cost_bridge_per_tile = null
private

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

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

_MinchinWeb_RoadPathfinder_::_cost_coast = null
private

The extra cost for a coast tile.

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

_MinchinWeb_RoadPathfinder_::_cost_drivethru_station = null
private

The extra cost for drive-thru road stations.

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

_MinchinWeb_RoadPathfinder_::_cost_level_crossing = null
private

the extra cost for rail/road level crossings.

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

_MinchinWeb_RoadPathfinder_::_cost_no_existing_road = null
private

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

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

_MinchinWeb_RoadPathfinder_::_cost_only_existing_roads = null
private

Choose whether to only search through existing connected roads.

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

_MinchinWeb_RoadPathfinder_::_cost_slope = null
private

The extra cost if a road tile is sloped.

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

_MinchinWeb_RoadPathfinder_::_cost_tile = null
private

The cost for a single tile.

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

_MinchinWeb_RoadPathfinder_::_cost_tunnel_per_tile = null
private

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

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

_MinchinWeb_RoadPathfinder_::_cost_turn = null
private

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

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

_MinchinWeb_RoadPathfinder_::_distance_penalty = null
private

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

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

_MinchinWeb_RoadPathfinder_::_max_bridge_length = null
private

The maximum length of a bridge that will be build.

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

_MinchinWeb_RoadPathfinder_::_max_cost = null
private

The maximum cost for a route.

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

_MinchinWeb_RoadPathfinder_::_max_tunnel_length = null
private

The maximum length of a tunnel that will be build.

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

_MinchinWeb_RoadPathfinder_::_mypath = null
private

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

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

_MinchinWeb_RoadPathfinder_::_pathfinder = null
private

A reference to the used AyStar object.

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

_MinchinWeb_RoadPathfinder_::_road_type = null
private

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

_MinchinWeb_RoadPathfinder_::_running = null
private

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

_MinchinWeb_RoadPathfinder_::cost = null
private

Used to change the costs.

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

_MinchinWeb_RoadPathfinder_::info = null
private

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


The documentation for this class was generated from the following file: