File size: 6,652 Bytes
3f30612 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | // 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
}
|