Skip to content

jorbascrumps/trekker

Repository files navigation

Trekker Coverage Status Build Status

Note: Trekker is currently in a prerelease state and is therefore subject to frequent (sometimes undocumented) breaking changes.

Installation

npm i trekker -S

Usage

Trekker can be used on both the client- and server-side:

import Trekker from 'trekker';

const grid = [
    [ 1, 1, 1 ],
    [ 1, 0, 1 ],
    [ 1, 1, 1 ],
];
const path = new Trekker(grid);
    .search(0, 0, 0, 2);

API

The goal of the public API is to be chainable in any configuration. A notable exception to this, however, is Trekker.search which will terminate the chain as it returns a solution path instead of the Trekker object.

Trekker.search(x1, y1, x2, y2)

Arguments

  • x1 (Number) — x position of the starting position
  • y1 (Number) — y position of the starting position
  • x2 (Number) — x position of the end position
  • y2 (Number) — y position of the end position

Returns an Array of Nodes representing the solution path. If no path is found, an empty Array is returned.

Trekker.setDiagonalMode(mode)

Arguments

  • mode (Number) — A value representing the diagonal mode to use. The following options are available from Trekker.DIAGONAL_MODE:
    • NEVER — Disable diagonal movement (default)
    • ALWAYS — Allow diagonal movement regardless of obstacles
    • ONE_OBSTACLE — Allow diagonal movement with at most one obstacle
    • NO_OBSTACLES — Allow diagonal movement only when there are no obstacles

Returns a Trekker object for chaining.

Trekker.processNeighbour(fn)

Arguments

  • fn(current, neighbour): Boolean (Function) — A custom function to use for additional consideration when processing a neighbour tile

Returns a Trekker object for chaining.

Trekker.toString()

Returns a String representing a solution path. An example output from the Usage section would be:

'[1,0],[2,0]'

Note: This is primarly a utility for testing.

TODO

  • Customize acceptable tiles per instance
  • Performance optimizations

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published