The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

TCOD::Path - An A-star pathfinder

SYNOPSIS

    use TCOD;

    my $map = TCOD::Map->new( 10, 10 );
    $map->clear( 1, 1 );

    my $path = TCOD::Path->new_using_map( $map, 1.41 );
    $path->compute( 3, 0, 9, 9 );

    printf "Step to [%s,%s]\n", $path->walk until $path->is_empty;
    # OUTPUT:
    # Step to [4,1]
    # Step to [4,2]
    # Step to [5,3]
    # Step to [6,4]
    # Step to [7,5]
    # Step to [8,6]
    # Step to [8,7]
    # Step to [9,8]
    # Step to [9,9]

DESCRIPTION

This class implements an A* pathfinder to be used in roguelike games.

METHODS

new_using_map

    $path = TCOD::Path->new_using_map( $map, $diagonal_cost );

Takes a TCOD::Map object and a diagonal cost value and returns a TCOD::Path object that can be used to find paths in that map.

The diagonal cost will determine the cost of diagonal movement compared to orthogonal movement. A good standard value to use is 1.41 (~sqrt(2)). Set to 0 to disable diagonal movement entirely, or 1 to make all directions have the same cost.

compute

    $path->compute( $x1, $y1, $x2, $y2 );

Takes the coordinates of a starting position and a target destination, and computes an A* map from one to the other.

The start and ends points will be stored internally, and can be retrieved with get_origin and get_destination (see below).

reverse

    $path->reverse;

Reverses the origin and destination points for this path.

is_empty

    $bool = $path->is_empty;

Returns true when the internal iterator has run out of steps in the path.

See walk for a way to move the internal iterator forward.

size

    $steps = $path->size;

Returns the number of steps in this path. This number will remain constant as long as the object's start and destination points haven't changed after computing a path.

get_origin

    ( $x, $y ) = $path->get_origin;

Returns a list with the coordinates of the point set as the path's origin. See compute for how this is done.

get_destination

    ( $x, $y ) = $path->get_destination;

Returns a list with the coordinates of the point set as the path's destination. See compute for how this is done.

get

    ( $x, $y ) = $path->get( $step );

Returns a list with the coordinates of the specified step. Together with size, this can be used to iterate through a path without using the internal iterator.

walk

    ( $x, $y ) = $path->walk;

Returns a list with the coordinates of the current step of the internal iterator, and moves the iterator forward. The values returned by this method will be meaningful as long as is_empty returns false.

SEE ALSO

TCOD
TCOD::Dijkstra
TCOD::Map

COPYRIGHT AND LICENSE

Copyright 2021 José Joaquín Atria

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.