HEMPO / mc-bots /worker_pull.js
jasonfan's picture
Mirror main from GitHub PgUpDn/HEMPO
3f30612 verified
Raw History Blame Contribute Delete
6.65 kB
// worker_pull.js —— Piano Moving Boat Control Logic
const mineflayer = require('mineflayer');
const Vec3 = require('vec3');
// Boat state tracking
let boatEntity = null
let lastBoatPos = new Vec3(2, 71, 2) // FIXED: On solid ground
// Boat collaborative control function
function controlBoat(bot, targetPos, targetYaw) {
console.log(`${bot.username} starting collaborative boat movement to ${targetPos.x}, ${targetPos.y}, ${targetPos.z}, yaw:${targetYaw}`)
// Find nearest boat
const boat = bot.nearestEntity(entity => entity.name === 'boat')
if (!boat) {
console.log(`${bot.username} cargo boat not found`)
return
}
// Find nearest leash knot as handle
const leashKnot = bot.nearestEntity(entity => entity.name === 'leash_knot')
if (!leashKnot) {
console.log(`${bot.username} leash knot not found, pushing/pulling boat directly`)
}
let isControlling = true
// Control loop
const controlInterval = setInterval(() => {
if (!isControlling) {
clearInterval(controlInterval)
return
}
const boatPos = boat.position
const botPos = bot.entity.position
// Calculate direction vector from boat to target
const boatToTarget = targetPos.minus(boatPos).normalize()
// Calculate bot's ideal push/pull position relative to boat
const desiredOffset = boatToTarget.scaled(-2) // bot 2 blocks behind boat
const desiredBotPos = boatPos.plus(desiredOffset)
// Move bot to appropriate push/pull position
const botToDesired = desiredBotPos.minus(botPos)
if (botToDesired.norm() > 0.5) {
// If too far from ideal position, move there first
bot.lookAt(desiredBotPos)
bot.setControlState('forward', true)
bot.setControlState('sprint', true)
} else {
// Already in good position, start pushing boat
bot.setControlState('forward', false)
bot.setControlState('sprint', false)
// Face boat and push/pull
bot.lookAt(boatPos)
// Check alignment between boat velocity and bot force direction
const boatVelocity = boat.velocity || new Vec3(0, 0, 0)
const botDirection = boatPos.minus(botPos).normalize()
// Calculate angle (using dot product)
const alignment = boatVelocity.dot(botDirection)
if (alignment < 0 && boatVelocity.norm() > 0.01) {
// Boat moving direction opposes bot force direction by >90 degrees, stop applying force
console.log(`${bot.username} detected opposing force, stopping push/pull`)
bot.clearControlStates()
} else {
// Continue pushing boat
bot.setControlState('forward', true)
// If leash knot available, try using leash
if (leashKnot && botPos.distanceTo(leashKnot.position) < 1.5) {
// Simulate leash pulling: apply stronger force toward target direction
if (bot.heldItem && bot.heldItem.name === 'lead') {
// If holding leash, right-click to use
bot.activateItem()
}
}
}
}
// Check if target reached
const distanceToTarget = boatPos.distanceTo(targetPos)
const yawDifference = Math.abs(boat.yaw - targetYaw)
if (distanceToTarget < 0.8 && yawDifference < 0.2) {
console.log(`${bot.username} assisted movement complete, distance: ${distanceToTarget.toFixed(2)}, yaw diff: ${(yawDifference * 57.3).toFixed(1)}°`)
bot.clearControlStates()
bot.chat('status reached')
isControlling = false
}
}, 100) // update control every 100ms
// Safety timeout: automatically stop control after 30 seconds
setTimeout(() => {
if (isControlling) {
console.log(`${bot.username} control timeout, stopping boat operation`)
bot.clearControlStates()
isControlling = false
}
}, 30000)
// Listen for stop commands
const stopListener = (username, message) => {
if (username === 'leader' && message === 'STOP_CARGO') {
console.log(`${bot.username} received stop command`)
bot.clearControlStates()
isControlling = false
bot.removeListener('chat', stopListener)
}
}
bot.on('chat', stopListener)
}
// Advanced boat navigation (including obstacle avoidance and path planning)
function advancedBoatControl(bot, targetPos, targetYaw) {
console.log(`${bot.username} starting advanced boat navigation`)
const boat = bot.nearestEntity(entity => entity.name === 'boat')
if (!boat) return
// Use A* or other path planning algorithms
// Simplified to direct navigation here, could add obstacle avoidance in real applications
controlBoat(bot, targetPos, targetYaw)
}
// Multi-robot coordination logic
function coordinatedPush(bots, boat, targetPos, targetYaw) {
console.log('Starting multi-robot coordinated push/pull')
// Assign push/pull positions based on boat size and weight
const pushPositions = [
new Vec3(-1, 0, -1), // left rear
new Vec3(1, 0, -1), // right rear
new Vec3(0, 0, -2), // straight rear
new Vec3(0, 0, 2), // straight front (for precise positioning)
]
bots.forEach((bot, index) => {
if (index < pushPositions.length) {
const offset = pushPositions[index]
const assignedPos = targetPos.plus(offset)
controlBoat(bot, assignedPos, targetYaw)
}
})
}
// Collision detection and recovery
function handleCollision(bot, boat) {
console.log(`${bot.username} handling boat collision recovery`)
// Brief retreat
bot.setControlState('back', true)
setTimeout(() => {
bot.setControlState('back', false)
// Try pushing from the side
bot.setControlState('left', true)
setTimeout(() => {
bot.setControlState('left', false)
}, 1000)
}, 500)
}
function positionAroundCargo(bot, targetPos) {
// Position multiple bots around cargo for collaborative push/pull ON SOLID GROUND
const positions = [
targetPos.plus(new Vec3(-3, 0, 0)), // Left pusher (Y=71)
targetPos.plus(new Vec3(3, 0, 0)), // Right pusher (Y=71)
targetPos.plus(new Vec3(0, 0, -3)), // Front puller (Y=71)
targetPos.plus(new Vec3(0, 0, 3)) // Back pusher (Y=71)
]
// Choose position based on bot number
const botNum = parseInt(bot.username.replace('bot', '')) || 1
const chosenPos = positions[(botNum - 1) % positions.length]
console.log(`${bot.username} positioning at ${chosenPos} (on solid ground Y=71)`)
bot.pathfinder?.setGoal(new bot.pathfinder.goals.GoalBlock(chosenPos.x, chosenPos.y, chosenPos.z))
}
module.exports = {
controlBoat,
advancedBoatControl,
coordinatedPush,
handleCollision,
positionAroundCargo
}