PrismarineJS / mineflayer-navigate

mineflayer plugin which gives bots a high level 3d navigating API using A*
59 stars 22 forks source link

No callbacks? #37

Closed ghost closed 5 years ago

ghost commented 5 years ago

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

ghost commented 5 years ago

Just noticed there was one in .walk but It would be nice if there was one on .navigate so I didnt have to create my own function.

rom1504 commented 5 years ago

There are events. You're authorised to read the example

On Fri, Feb 8, 2019, 06:43 KenWa51 notifications@github.com wrote:

Just noticed there was one in .walk but It would be nice if there was one on .navigate so I didnt have to create my own function.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/PrismarineJS/mineflayer-navigate/issues/37#issuecomment-461697483, or mute the thread https://github.com/notifications/unsubscribe-auth/ACPN_mwImrR0vdQkMhuTWcGs8Nb2-Zaeks5vLQ6YgaJpZM4atOjD .

ghost commented 5 years ago

Is there a way I could add an option where it'd be like flee(position to run from) or something like that?

rom1504 commented 5 years ago

I don't think it really belong in that package. You can code it in your bot. Just pick a random position from that position using bot.blockAt and try go there

ghost commented 5 years ago

thats not very reliable though, also I think it belongs in this package because what if you need to flee from a creep/skeleton/angry mob?

rom1504 commented 5 years ago

this package is about going from A to B. It does that well.

Fleeing from an entity is by definition not reliable, in some cases it might even be impossible.

You could make another mineflayer plugins for this, it could use mineflayer-navigate for movement.

The way I suggested is a simple way to implement it but of course you're free to do something better.

ghost commented 5 years ago

Could you help me come up with an idea? I want to take the player's position and the mobs position and go backwards away from it what do you think I could do?

rom1504 commented 5 years ago

something like that :

function norm(v) {
  return Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z)
}
function fleePosition(botPosition, entityPosition, distanceToFlee = 20) {
  const fromBotToEntity = entityPosition.minus(botPosition)
  const directionVector = fromBotToEntity.scaled(-distanceToFlee/norm(fromBotToEntity))
  return botPosition.plus(directionVector)
}

const fleePosition = fleePosition(bot.entity.position, yourEntity.position)
bot.navigate.to(fleePosition)
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");
});
ghost commented 5 years ago

You should add that as an example if it works!

rom1504 commented 5 years ago

It should work but you need to replace the "yourEntity" by the entity you want to flee from (you can use bot.entities to get it) I didn't test it at all though, you can test it ;)

ghost commented 5 years ago

It worked! 😄 However its important to note that the timeout needs to be pretty low.

ghost commented 5 years ago

Also It'd be nice if the bot would be able to move on a chunk by chunk basis so if a coordinate is out of the bot's range of view it will slowly make its way there using its available chunks

imharvol commented 5 years ago

I found a callback in the navigateTo function: bot.navigate.to(position, {onArrived () { // Your callback }})