feat(mover): add an action to move / run away from an entity or goal

based on `pathfinder` goal `GoalInvert`; behaves slightly strange
This commit is contained in:
jay 2021-04-07 06:26:16 +05:00
parent b453b7d6bd
commit c42a3a2304

View File

@ -82,6 +82,66 @@ function follow(entity, dynamic = true, distance = 3) {
bot.pathfinder.setGoal(new GoalFollow(entity, distance), dynamic) bot.pathfinder.setGoal(new GoalFollow(entity, distance), dynamic)
} }
function away(entity = bot.nearestEntity(), invertInvert = true, dynamic = true, distance = 10) {
const currentGoal = bot.pathfinder.goal
console.assert(currentGoal || entity)
const { GoalInvert } = require('mineflayer-pathfinder').goals
bot.pathfinder.setMovements(movements.defaultMove)
if (!currentGoal) {
const { GoalFollow } = require('mineflayer-pathfinder').goals
if (entity.entity) {
console.log("go away entity:", entity, entity.entity)
entity = entity.entity
}
cfg.quiet || bot.chat(
`going away from ${entity?.type
}: ${entity?.username || entity?.displayName
}${dynamic ? "" : " once"}`
)
// alternative implementation
// follow(entity, dynamic, distance)
// bot.pathfinder.setGoal(new GoalInvert(bot.pathfinder.goal), dynamic)
return bot.pathfinder.setGoal(new GoalInvert(
new GoalFollow(entity, distance)
), dynamic)
}
if (currentGoal instanceof GoalInvert) {
const currEntity = currentGoal.goal.entity
console.log("go away inverse goal:", currentGoal.goal)
if (invertInvert) {
cfg.quiet || bot.chat(
`switching towards ${currentGoal.goal?.constructor.name
}: ${currEntity?.type
}: ${currEntity?.username || currEntity?.displayName
}${dynamic ? "" : " once"}`
)
bot.pathfinder.setGoal(currentGoal.goal, dynamic)
} else {
cfg.quiet || bot.chat(
`already going away from ${currentGoal.goal?.constructor.name
}; not switching`
)
}
} else {
const currEntity = currentGoal.entity
console.log("go away goal:", currentGoal)
cfg.quiet || bot.chat(
`going away from ${currentGoal?.constructor.name
}: ${currEntity?.type
}: ${currEntity?.username || currEntity?.displayName
}${dynamic ? "" : " once"}`
)
bot.pathfinder.setGoal(new GoalInvert(currentGoal), dynamic)
}
}
function ride(entity) { function ride(entity) {
entity = entity?.entity || entity entity = entity?.entity || entity
const ridableMobs = ["Horse", "Donkey", "Pig", "Strider"] const ridableMobs = ["Horse", "Donkey", "Pig", "Strider"]
@ -230,6 +290,11 @@ function command(message_parts, player) {
case "mount": case "mount":
ride(message_parts2[0]) ride(message_parts2[0])
break break
case "away":
case "run":
case "runaway":
away()
break
case "w": case "w":
case "f": case "f":
moveOrRide(0, -1, "forward", message_parts2) moveOrRide(0, -1, "forward", message_parts2)