Skip to content

Commit

Permalink
Updated digging code to account for raycasted tall grass checks (Pris…
Browse files Browse the repository at this point in the history
…marineJS#3285)

* Updated digging code to account for raycasted tall grass checks

* Fixing lint

* Removing unnecessary typing comment

* Fix lint
  • Loading branch information
GenerelSchwerz authored Jan 25, 2024
1 parent 298d442 commit bd0fb5c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/plugins/digging.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function inject (bot) {
z: Math.sign(Math.abs(dz) > 0.5 ? dz : 0)
}
const validFaces = []
const closerBlocks = []
for (const i in visibleFaces) {
if (!visibleFaces[i]) continue // skip as this face is not visible
// target position on the target block face. -> 0.5 + (current face) * 0.5
Expand All @@ -65,13 +66,19 @@ function inject (bot) {
const startPos = bot.entity.position.offset(0, bot.entity.height, 0)
const rayBlock = bot.world.raycast(startPos, targetPos.clone().subtract(startPos).normalize(), 5)
if (rayBlock) {
if (startPos.distanceTo(rayBlock.intersect) < startPos.distanceTo(targetPos)) {
// Block is closer then the raycasted block
closerBlocks.push(rayBlock)
// continue since if distance is ever less, then we did not intersect the block we wanted,
// meaning that the position of the intersected block is not what we want.
continue
}
const rayPos = rayBlock.position
if (
rayPos.x === block.position.x &&
rayPos.y === block.position.y &&
rayPos.z === block.position.z
) {
// console.info(rayBlock)
validFaces.push({
face: rayBlock.face,
targetPos: rayBlock.intersect
Expand All @@ -95,6 +102,11 @@ function inject (bot) {
}
await bot.lookAt(closest.targetPos, forceLook)
diggingFace = closest.face
} else if (closerBlocks.length === 0 && block.shapes.length === 0) {
// no other blocks were detected and the block has no shapes.
// The block in question is replaceable (like tall grass) so we can just dig it
// TODO: do AABB + ray intercept check to this position for diggingFace.
await bot.lookAt(block.position.offset(0.5, 0.5, 0.5), forceLook)
} else {
// Block is obstructed return error?
throw new Error('Block not in view')
Expand Down

0 comments on commit bd0fb5c

Please sign in to comment.