The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Game::DijkstraMap - a numeric grid of weights plus some related functions

SYNOPSIS

  use Game::DijkstraMap;
  my $map = Game::DijkstraMap->new;
  $map->map( [[ ... ]] );
  my $dimap = $map->dimap;

  $map->update( [ 0, 1, -1 ] );

DESCRIPTION

This module implements what the author of the "The Incredible Power of Dijkstra Maps" article (see below) calls Dijkstra Maps. Such maps have various uses in roguelikes or other games. This implementation may not be particularly fast but should allow quick prototyping of map-building and path-finding exercises.

CONSTRUCTOR

The new method accepts any of the "ATTRIBUTES".

ATTRIBUTES

max_cost

Cost for non-goal non-wall points. A large number by default. These points should be reduced by normalize_costs if all goes well.

min_cost

Cost for points that are goals. Zero by default.

bad_cost

Cost for cells through which motion is illegal (walls, typically). -1 by default, and ignored when updating the map.

costfn

A code reference called with the object and the contents of the map passed to map. This function must convert those items into suitable cost numbers for the internal Dijkstra Map. Defaults to a function that assigns bad_cost to # and min_cost to x, otherwise max_cost.

dimap

Accessor for the Dijkstra Map, presently an array reference of array references. Do not change this reference unless you know what you are doing.

iters

This is set after map or update calls and indicates how many iterations it took the normalize_costs method to stabilize the map.

METHODS

These return the object so can be chained with other calls. These methods will throw exceptions if something goes awry (especially when given known bad input).

map map

Accepts a level map (an array reference of array references, or a 2D grid) and uses the costfn to convert the objects in that map to the internal Dijkstra Map that is held in the dimap attribute.

normalize_costs dimap

Mostly an internal routine called by map or update that reduces max_cost cells as appropriate relative to the connected min_cost cells.

update [row, col, value] ..

Updates the given row and column with the given value for each array reference passed then updates the Dijkstra Map.

BUGS

Reporting Bugs

Please report any bugs or feature requests to bug-game-dijkstramap at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Game-DijkstraMap.

Patches might best be applied towards:

https://github.com/thrig/Game-DijkstraMap

Known Issues

New code. In particular the update method will need a different interface or an attribute to handle two different possibilities, see the commentary in the test code. Also need to add path finding (routes) and next cell (steps along routes) methods.

SEE ALSO

The code in this module is based on the following article (as of August 2018):

http://www.roguebasin.com/index.php?title=The_Incredible_Power_of_Dijkstra_Maps

There are various other graph and pathfinding modules on CPAN that may be more suitable to the task at hand.

AUTHOR

thrig - Jeremy Mates (cpan:JMATES) <jmates at cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2018 by Jeremy Mates

This program is distributed under the (Revised) BSD License: http://www.opensource.org/licenses/BSD-3-Clause