Coder Social home page Coder Social logo

mineflayer-navigate's Introduction

mineflayer-navigate

NPM version Build Status

Deprecated package : this library is a simple example of pathfinding and works well for the simple cases, you can study its code. However if you need something more robust, we advise to use https://github.com/Karang/mineflayer-pathfinder instead

A library to help your mineflayer bot navigate around the 3D world using the A* algorithm.

See https://github.com/superjoe30/mineflayer/

YouTube Demo

Usage

const mineflayer = require('mineflayer');
const navigatePlugin = require('mineflayer-navigate')(mineflayer);
const bot = mineflayer.createBot({ username: 'Player' });
// install the plugin
navigatePlugin(bot);
// optional configuration
bot.navigate.blocksToAvoid[132] = true; // avoid tripwire
bot.navigate.blocksToAvoid[59] = false; // ok to trample crops
bot.navigate.on('pathFound', function (path) {
  bot.chat("found path. I can get there in " + path.length + " moves.");
});
bot.navigate.on('cannotFind', function (closestPath) {
  bot.chat("unable to find path. getting as close as possible");
  bot.navigate.walk(closestPath);
});
bot.navigate.on('arrived', function () {
  bot.chat("I have arrived");
});
bot.navigate.on('interrupted', function() {
  bot.chat("stopping");
});
bot.on('chat', function(username, message) {
  // navigate to whoever talks
  if (username === bot.username) return;
  const target = bot.players[username].entity;
  if (message === 'come') {
    bot.navigate.to(target.position);
  } else if (message === 'stop') {
    bot.navigate.stop();
  }
});

Documentation

bot.navigate.to(point, options)

Finds a path to the specified location and goes there.

  • point - the block you want your feet to be standing on
  • options - See bot.navigate.findPathSync

event "pathPartFound" (path)

Emitted from bot.navigate when a partial path is found. path is an array of nodes.

event "pathFound" (path)

Emitted from bot.navigate when a complete path is found. path is an array of nodes.

event "cannotFind" (closestPoint)

Emitted when a path cannot be found.

  • closestPoint - a vec3 instance - the closest point that you could navigate to.

event "arrived"

Emitted when the destination is reached.

event "stop"

Emitted when navigation has been aborted.

bot.navigate.stop()

Aborts an in progress navigation job.

bot.navigate.findPathSync(end, [options])

Finds a path to end. Can be used to see if it is possible to navigate to a particular point.

Returns an object that looks like:

{
  status: 'success', // one of ['success', 'noPath', 'timeout', 'tooFar']
  path: [startPoint, point1, point2, ..., endPoint],
}

The value of status has several meanings:

  • success - path is an array of points that can be passed to walk().
  • noPath - there is no path to end. Try a larger endRadius. path is the path to the closest reachable point to end.
  • timeout - no path could be found in the allotted time. Try a larger endRadius or timeout. path is the path to the closest reachable point to end that could be found in the allotted time.
  • tooFar - end is too far away, so path contains the path to walk 100 meters in the general direction of end.

Parameters:

  • end - the block you want your feet to be standing on
  • options - optional parameters which come with sensible defaults
    • isEnd - function(node) - passed on to the A* library. node.point is a vec3 instance.
    • endRadius - used for default isEnd. Effectively defaults to 0.
    • timeout - passed on to the A* library. Default 10 seconds.
    • tooFarThreshold - if end is greater than tooFarThreshold, this function will search instead for a path to walk 100 meters in the general direction of end.

bot.navigate.walk(path, [callback])

Note: does not emit events

Walks the bot along the path and calls the callback function when it has arrived.

Call bot.navigate.stop() to interrupt walking.

  • path - array of points to be navigated.
  • callback(stopReason) - (optional) - called when the bot has arrived. stopReason can be: ['obstructed', 'arrived', 'interrupted']

History

0.0.10

  • depends on vec3 directly

0.0.9

  • don't emit arrived twice. (thanks Benjamin Grosse)

0.0.8

  • walk: detect being obstructed and call callback with 'obstructed' stopReason when it happens.

0.0.7

  • walk: callback is still called if bot.navigate.stop() is called elsewhere. Also it now has a stopReason argument.

0.0.6

  • fix default endRadius too low (thanks vogonistic)

0.0.5

  • recommended API is now callback based (thanks vogonistic)
  • add bot.navigate.findPathSync(end, [options])
  • add bot.navigate.walk(path, [callblack])

0.0.4

  • add 'interrupted' event

0.0.3

  • fix bot looking at its feet while walking
  • possible speed improvement by using native array methods
  • cannotFind event now has closestPoint parameter, the closest point it could get to
  • bot.navigate.blocksToAvoid is a map of block id to boolean value which tells whether to avoid the block. comes with sensible defaults like avoiding fire and crops.

0.0.2

  • fix pathfinding very far away

mineflayer-navigate's People

Contributors

andrewrk avatar dependabot-preview[bot] avatar dependabot[bot] avatar rom1504 avatar vogonistic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mineflayer-navigate's Issues

Index out of range error in buffer.js caused by A* callback

This is an error that randomly occured while my bot was walking around the world (in Nether):

  buffer.js:506
      throw new RangeError('index out of range');
      ^
  RangeError: index out of range
      at checkOffset (buffer.js:506:11)
      at Buffer.readUInt8 (buffer.js:544:5)
      at nib (C:\Users\Jakub\node_modules\mineflayer\lib\plugins\blocks.js:116:31)
      at Bot.blockAt (C:\Users\Jakub\node_modules\mineflayer\lib\plugins\blocks.js:102:22)
      at properties (C:\Users\Jakub\node_modules\mineflayer-navigate\index.js:326:25)
      at C:\Users\Jakub\node_modules\mineflayer-navigate\index.js:309:20
      at Array.forEach (native)
      at Object.getNeighbors [as neighbor] (C:\Users\Jakub\node_modules\mineflayer-navigate\index.js:187:30)
      at aStar (C:\Users\Jakub\node_modules\a-star\index.js:52:28)
      at findPathSync (C:\Users\Jakub\node_modules\mineflayer-navigate\index.js:72:19)

Probably it's checking non-existent blocks or something like that. I don't know how to fix it :(

General improvements to mineflayer-navigate / mineflayer-scaffold

mineflayer-navigate and mineflayer-scaffold are nice but not quite perfect :

  • integrate the time it takes to mine or jump/build into the length of an edge
  • options about what blocks it can use, about what block it can dig
  • area to avoid

This issue tracks general problems and ideas to improve it.

Bug with fences

When there's a fence in the path, it tries to jump above it as if it were a normal block, but it's not so it gets stuck

Don't run through fire

or other hazardous blocks. it's also rude to walk through crops. should we avoid tripping redstone triggers as well (pressure plates, trip wires, etc.)? perhaps there should be a parameter that is a list of ids to avoid.

Sprint Jumping

Does this let you sprint jump? You probably have it jump over gaps of 1 or 2 but you can sprint jump like 3-4 blocks

Error with onCannotFind

I get this error , I'm not sure why.
.../node_modules/mineflayer-navigate/index.js:94
params.onCannotFind(closestPoint);
^
TypeError: Object # has no method 'onCannotFind'
at navigate (.../node_modules/mineflayer-navigate/index.js:94:16)
at nextPartOnArrived (.../node_modules/mineflayer-navigate/index.js:148:9)
at Timer.monitorMovement (.../node_modules/mineflayer-navigate/index.js:113:13)
at Timer.exports.setInterval.timer.ontimeout (timers.js:234:14)

Bot times out after 15 seconds

When I run the default script (+ server information), the bot will join the server. When I say "come" in chat, the bot does not respond. It stands still for 15 seconds, then disconnects (reason in server console is "Timed out"). The script continues to work/doesn't close, and gives no errors.

I have tried this on 2 different servers, one through bungeecord, the other a standalone vanilla server (cracked).

No callbacks?

Without callbacks this is very limited in it's use.

`arrived` event is not fired when walking to nearest path

I am too lazy to scan for suitable place and instead, I make the bot walk right into the block he's digging. I've basically just copypasted the code from README.md:

bot.navigate.once('pathFound', function (path) {
  bot.chat("Found path. I can get there in " + path.length + " moves.");
});
bot.navigate.once('cannotFind', function (closestPath) {
  bot.chat("Unable to find path. getting as close as possible");
  bot.navigate.walk(closestPath);
});
bot.navigate.once('arrived', function () {
  bot.chat("I have arrived");
  _this.prepareTool(block, function() {_this.digBlock(block);});
});
bot.navigate.once('interrupted', function() {
  bot.chat("Walk interrupted at "+bot.entity.position);
  _this.prepareTool(block, function() {_this.digBlock(block);});
});
bot.navigate.once('stop', function() {
  bot.chat("Walk stopped at "+bot.entity.position);
  _this.prepareTool(block, function() {_this.digBlock(block);});
});
bot.navigate.to(block.position);
this.bot.chat("Walking to: "+block.position);

And this is the output. As you see, stop is fired, not arrived:

log

Bots can't walk through Snow

Bots seem incapable of walking through snow (block 78). It is treated as a solid block.

I am not sure why this is, as I see in mineflayer/lib/enums/blocks.json that the boundingBox is "empty".

Snow and DEFAULT_END_RADIUS lead to a timeout

On the snow (or other blocks from PrismarineJS/mineflayer#228), the bot goes 1 block higher, as a result, DEFAULT_END_RADIUS may be too small to reach the target. In this scenario, all findPathSync work until timeout.

p.s. In my case, I explicitly increase the radius, but in many algorithms this is not an obvious moment that affects the speed of navigation.

handle onPathFound and other on* function in params of navigateTo

It would be useful to be able to change those callbacks.
Events are not very adapted some time.
For exemple i'm doing
bot.navigate.once("pathFound",
and
bot.navigate.once("cannotFind",
in my function to get the nearest reachable mob and once any of these events has been caught the listener of the other one should be removed.
I'm not sure how to do that and it might be possible, but it would be simpler if i could simply give callbacks to the navigateTo function.

Add TypeScript typings.

Could not find a declaration file for module 'mineflayer-navigate'. '/node_modules/mineflayer-navigate/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/mineflayer-navigate` if it exists or add a new declaration (.d.ts) file containing `declare module 'mineflayer-navigate';`

make it run?

Is there a way to make it sprint to the target?

help

1 how to send a message when someone joins
2 how to make a commond like !follow that follows the player sent in if they were within render distance?

the bot is looking at its feet

Since a bug fix has been done in mineflayer, the bot is looking at its feet. I have seen this behaviour in my bot and i haven't tried it with mineflayer-navigate example but it should do the same with the example.

Sugar for queuing

Is it possible to get some sugar for chaining navigation requests? Maybe this belongs in the main repo but I want to be able to define a structure as a JSON document and have the bot build it step by step - this is possible right now but you end up in callback hell. Would be nice to have something like

var script = mineflayer.script()
  .walkTo(x,y)
  .dig(someBlock)
  .walkTo(x,y)
  .placeBlock(refBlock);

bot.runScript(script);

If it is out of the scope of the library I'd be happy to make it myself as a standlone - turning an async API into a queued chaining API is pretty simple to do these days. Just curious what your thoughts are about this - would be cool to eventually make some kind of a platform for kids to learn programming by writing minecraft scripts

Promise-based navigation

Good morning everybody,
i'am sorry for opening an issue on this; did not found an more appropriate place for it. While working on a project using mineflayer-navigate my code very fast became a mess because of all the events fired and holding status objects and so on.
After a while i started to wrap this module in a promise based manner in order to use it in a more sequential style (without loosing any of the concurrency features), and express temporary dependencies better. This weekend i decided to proper document my code and publish it.
Feel free to use it and it would be a pleasure if my code becomes part of this one (i avoided a pull request, because this topic was never discussed here, but it would be a pleasure to do so).

github: mineflayer-navigate-promise
npm: mineflayer-navigate-promise

Placing blocks messes up path

Hello,
Basically I have a bot which is just moving back and forth between 2 points, and while doing that it just places sand block off of the block its looking at. When I turn on the block placing, it seems to think it has arrived (after moving like 5 blocks) at the far point and tries to move to the first point again.

It almost looks like placing a block slows it down or something I can't seem to find why this happens. I changed the path to just be in 2 directions so the head doesn't move its just using control state left and right.

  • Mineflayer: v2.11.0
  • Mineflayer-navigate: v0.0.10

I have no clue what code to post since I have it all over the place, so Ill show a gif of what happens.
The yellow cords are the x of the block its looking at. The spam gray is showing which way its trying to set the control state/direction of movement to.

https://gyazo.com/6fbefb719a5797be7574b4662dc75948

Keep in mind it only freezes the path when I have it placing blocks.

I just want to know if theres a simple fix for this, because if not Its a bit more work but I can just make it go back and forth and detect when it hits the point with no path ai.

Vertical Drops > 3 Blocks

I have been writing a set of bots to connect to my arena server and jump down into the main area, all while being controlled through an electron form. Fall damage will be disabled, however they will not jump off of a block if if the drop is higher than 3 blocks. So far I have gotten everything to work but this bit. I looked in the index.js of mineflayer-navigate, but was not very sure on which to change. I did notice the isSafe functions, but again I was not sure. If anyone could point me in the right direction that would be great. I know that I could just create a staircase in game, but that is not the objective.

Unable to remove listener

Hi,

I'm using the bot.navigate.on('arrived', sleepInBed) to head to bed and then when arrived sleep. The thing is, it's causing a memory leak since the event listeners are stacking up. Now I have a bit to remove the event listener but it doesn't seem to work:

bot.navigate.removeListener('arrived', sleepInBed)

Am I missing something? Is there a solution to having the .on('arrived') for a single time use?

trying to navigate when blocks are null causes crash

timers.js:103
if (!process.listeners('uncaughtException').length) throw e;
^
TypeError: Cannot read property 'boundingBox' of null
at isSafe (/home/andy/dev/mc-bot-server/node_modules/rbot/node_modules/mineflayer-navigate/index.js:345:17)
at Object.getNeighbors as neighbor
at aStar (/home/andy/dev/mc-bot-server/node_modules/rbot/node_modules/mineflayer-navigate/node_modules/a-star/index.js:50:28)
at findPathSync (/home/andy/dev/mc-bot-server/node_modules/rbot/node_modules/mineflayer-navigate/index.js:74:19)
at EventEmitter.navigateTo as to
at moveTo (/home/andy/dev/mc-bot-server/node_modules/rbot/task.js:114:16)
at /home/andy/dev/mc-bot-server/node_modules/rbot/achieve.js:70:18
at listAux (/home/andy/dev/mc-bot-server/node_modules/rbot/achieve.js:119:7)
at achieveList (/home/andy/dev/mc-bot-server/node_modules/rbot/achieve.js:124:2)
at achieve (/home/andy/dev/mc-bot-server/node_modules/rbot/achieve.js:111:2)
at listAux (/home/andy/dev/mc-bot-server/node_modules/rbot/achieve.js:116:28)

does findPathSync work ?

I get this kind of thing :
goal : { x: 632.8125, y: 67, z: 171.84375 }
result : { status: 'timeout',
path:
[ { x: 630.5, y: 67, z: 168.5 },
{ x: 630.5, y: 67, z: 169.5 },
{ x: 630.5, y: 67, z: 170.5 },
{ x: 630.5, y: 68, z: 171.5 },
{ x: 631.5, y: 69, z: 171.5 },
{ x: 633.5, y: 67, z: 171.5 } ] }

And it does that no matter how much timeout i give him.

What is really strange is that navigateTo works.

I don't understand but did you try findPathSync ?

Bot can't walk over half slabs / stairs

Half slabs, stairs, or blocks that are less than one block high don't work. The bot does find a suitable path, but can't move on blocks that are less than one block high.

TypeError: Cannot read property 'boundingBox' of null

I want to create a little bot, which have to walk to a specific area, but I get a TypeError...

D:\mcbot\node_modules\minecraft-protocol\src\transforms\framing.js:66
          } else { throw e }
                   ^

TypeError: Cannot read property 'boundingBox' of null
    at isSafe (D:\mcbot\node_modules\mineflayer-navigate\index.js:349:18)
    at Object.getNeighbors [as neighbor] (D:\mcbot\node_modules\mineflayer-navigate\index.js:184:19)
    at aStar (D:\mcbot\node_modules\a-star\index.js:52:28)
    at findPathSync (D:\mcbot\node_modules\mineflayer-navigate\index.js:71:19)
    at EventEmitter.navigateTo [as to] (D:\mcbot\node_modules\mineflayer-navigate\index.js:148:19)
    at Bot.bot.on (D:\mcbot\walker.js:55:15)
    at emitNone (events.js:111:20)
    at Bot.emit (events.js:208:7)
    at Client.bot._client.on (D:\mcbot\node_modules\mineflayer\lib\plugins\health.js:22:11)
    at emitTwo (events.js:126:13)

Code:

bot.on('spawn', () => {
	var vector = new vec3(-5520.0, 66.0, 5564.0);
	console.log(vector);
	bot.navigate.to(vector);
});

I've tried several things with the Vector like parseFloat on each parameter, give a Float number (.0), I tried to instantiate with an Object {x, y, z}, but nothing seems to work.

I just want to build something like, First walk to xyz1, then walk to xyz2, etc. not a big deal???

Anyone can help me?

bot glich in farmland

Im testing to make a bot to farm wheat and use mineflayer-navigate to move the bot in the farm.
When the bot is on normal ground (dirt, stone...) all is fine, the bot move correctly. But when he goes on farmland, he glich and is unable to move... can you help me?

im on vanilla server 1.12.2

here a gif of the bot gliching: https://imgur.com/a/3Yo7Vwd

TypeError

When i try to run this command:
bot.navigate.to({ x: 539, y: 40, z: -22 })

i get this error:
TypeError: end.floored is not a function

What do i do to fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.