Skip to content

Fast, promise based, 3D pathfinding library for Mineflayer using A* and D*Lite algorithms.

License

Notifications You must be signed in to change notification settings

Suficio/mineflayer-pathfinder

Repository files navigation

mineflayer-pathfinder

Fast, promise based, 3D pathfinding library using A* and D*Lite algorithms, for Mineflayer found under: https://github.com/superjoe30/mineflayer/

Table of Contents

Features

  • Provides high level API for determining paths between two points
  • Multiple algorithms for pathfinding, A* and D*Lite
  • Often nearly 3 times as fast as mineflayer-navigate
  • Exposed internal functions to allow easier user replacement
  • Based solely on a promise based API

Basic Usage

Firstly, install:

    npm install --save cheezbarger/mineflayer-pathfinder

To get started just paste this code into your bot:

const mineflayer = require('mineflayer');
const pathfinder = require('mineflayer-pathfinder');

// Install pathfinder
pathfinder(bot);

bot.on('chat', function(username, message)
{
    // Find path to whoever talked
    if (message === 'come')
    {
        bot.pathfinder
            .to(
                bot.entity.position,
                bot.players[username].entity.position
            )
            .then(function(ReturnState)
            {
                const path = ReturnState.path;
                // Move bot along path and youre done!
            });
    }
});

Advanced Usage

The following code illustrates how a rudimentary D* Lite implementation of pathfinding could work. Familiarize yourself with how the pathfinder algorithms work before using them.

const mineflayer = require('mineflayer');
const pathfinder = require('mineflayer-pathfinder');
const move = require('mineflayer-move');

// Install move and pathfinder
pathfinder(bot);
move(bot);

bot.on('chat', function(username, message)
{
    if (message === 'come')
    {
        const endPoint = bot.players[username].entity.position.floored();
        const startPoint = bot.entity.position.floored();

        const endPoint = bot.players[username].entity.position.floored();
        let lastPoint = undefined;

        function move(returnState)
        {
            bot.move.along(returnState.path)
                .then(function(moveReturn)
                {
                    const position = bot.entity.position.floored();

                    if (moveReturn === bot.move.ENUMStatus.Arrived)
                    {
                        if (endPoint.equals(position))
                            resolve(position);

                        else if (lastPoint && lastPoint.equals(position))
                            resolve(position);

                        else
                        {
                            lastPoint = position;
                            returnState.path.replan().then(move);
                        }
                    }
                    else
                    {
                        lastPoint = position;
                        returnState.path.replan().then(move);
                    }
                });
        }

        bot.pathfind
            .to(bot.entity.position, endPoint)
            .then(function(returnState)
            {
                if (returnState.ENUMState === bot.pathfinder.ENUMStatus.Incomplete)
                    endPoint = returnState.closestPoint;

                bot.pathfind
                    .to(bot.entity.position, endPoint, bot.pathfinder.ENUMPathfinder.DLITE)
                    .then(move);
            });
    }
});

Documentation

bot.pathfinder / bot.pathfind

Main pathfinder class.

bot.pathfinder.to( startPoint, endPoint [, ENUMPathfinder])

Attempts a path search from the start point to the end point using the provided pathfinder.

  • startPoint - the point from which you want to find the path
  • endPoint - the end point of the path
  • ENUMPathfinder - specifies which pathfinding algorithim to use, see bot.pathfinder.ENUMPathfinder
    • Defaults to bot.pathfinder.ENUMPathfinder.ASTAR

Returns a promise which evaluates to the corresponding ENUMPathfinder object, see Algorithm Documentation.

bot.pathfinder.getSuccessors( position)

Determines positions to which the bot can move from the given position. There is some discussion about how the default function for getSuccessors works at Default Conditions

position - coordinates of the position you want to move from

bot.pathfinder.getPredecessors( position)

Determines positions from which the bot could have moved to the given position.

position - coordinates of the position you want to move to

bot.pathfinder.getBlock( position)

Slightly faster version of bot.blockAt

position - coordinates of the block from which you want to get data

bot.pathfinder.MAX_EXPANSIONS

Integer values which determines the maximum ammount of positions an algorithim will inspect, defaults to 80000.

bot.pathfinder.HEURISTIC( startPoint, endPoint)

Determines the heuristic value from the startPoint to the endPoint. Defaults to euclidean distance.

bot.pathfinder.COST( startPoint, endPoint)

Determines the cost value from the startPoint to the endPoint. Defaults to bot.pathfinder.HEURISTIC.

bot.pathfinder.ENUMPathfinder

Object with the following properties:

  • ASTAR - Refers to the standard A* algorithm, see A* Pathfinding
  • DLITE - Refers to the optimized D* lite algorithm, see D* Lite Pathfinding

bot.pathfinder.ENUMStatus

Object with the following properties:

  • Complete - Occurs when the path could be successfully computed
  • Incomplete - Occurs when the pathfinder encountered an error or could not compute the complete path

Algorithm Documentation

Detailed overview of the algorithms used avaliable at Algorithm Documentation

About

Fast, promise based, 3D pathfinding library for Mineflayer using A* and D*Lite algorithms.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published