Skip to content

Commit

Permalink
Improved make hole for hardest area
Browse files Browse the repository at this point in the history
  • Loading branch information
sefirosweb committed Dec 30, 2024
1 parent 976e95e commit f0754ed
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 119 deletions.
4 changes: 2 additions & 2 deletions core/src/BehaviorModules/BehaviorMoveTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export class BehaviorMoveTo implements StateBehavior {
this.startMoving()
}

isFinished() {
return this.isEndFinished
isFinished(distance: number = 2) {
return this.bot.getControlState('jump') === false && (this.isEndFinished || this.distanceToTarget() < distance)
}

isSuccess() {
Expand Down
119 changes: 76 additions & 43 deletions core/src/BehaviorModules/minerJob/BehaviorMinerCheckLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LegionStateMachineTargets, MineCordsConfig } from "base-types"
import mineflayerPathfinder from 'mineflayer-pathfinder'
import { botWebsocket } from '@/modules'
import { Vec3 } from 'vec3'
import { StateBehavior } from "mineflayer-statemachine";
import { StateBehavior } from "minecraftlegion-statemachine";
import { Block } from "prismarine-block";
import { Bot } from "mineflayer";

Expand Down Expand Up @@ -117,27 +117,58 @@ export class BehaviorMinerCheckLayer implements StateBehavior {
continue
}

let block = this.getBlockType()
if (!block) {
botWebsocket.log(`Block: ${this.xCurrent} ${this.yCurrent} ${this.zCurrent} It is very far! I can't see the block, approaching the block to check it`)
botWebsocket.log('Area less than 200 blocks radius distance is recommended')
await this.moveToSeeBlock(this.xCurrent, this.yCurrent, this.zCurrent)
this.bot.removeAllListeners('customEventPhysicTick')
block = this.getBlockType()
if (this.xCurrent === undefined || this.yCurrent === undefined || this.zCurrent === undefined) {
throw new Error('No setted: this.xCurrent === undefined || this.yCurrent === undefined || this.zCurrent === undefined')
}

const block = await this.getBlock(this.xCurrent, this.yCurrent, this.zCurrent)

if (block === null) {
throw new Error('Block is null')
}

if (this.checkValidBlock(block)) {
const isValidBlock = await this.checkValidBlock(block)
if (isValidBlock) {
resolve()
return
}
} while (true)
})
}

getBlockType(x: number, y: number, z: number) {
const position = new Vec3(x, y, z)
return this.bot.blockAt(position)
}

async getBlock(x: number, y: number, z: number): Promise<Block> {
const block = await this.moveToSeeBlock(x, y, z)
this.bot.removeAllListeners('customEventPhysicTick')
return block
}

async moveToSeeBlock(x: number, y: number, z: number): Promise<Block> {
return new Promise((resolve) => {
const block = this.getBlockType(x, y, z)

if (block) {
resolve(block)
}

const goal = new mineflayerPathfinder.goals.GoalBlock(x, y, z)
this.bot.pathfinder.setMovements(this.targets.movements)
this.bot.pathfinder.setGoal(goal)

this.bot.on('customEventPhysicTick', () => {
const block = this.getBlockType(x, y, z)
if (block) {
this.bot.pathfinder.setGoal(null)
resolve(block)
}
})
})
}

next() {
if (this.minerCords === undefined) {
throw new Error('No setted: this.minerCords === undefined')
Expand Down Expand Up @@ -207,15 +238,6 @@ export class BehaviorMinerCheckLayer implements StateBehavior {
}
}

getBlockType() {
if (this.xCurrent === undefined || this.yCurrent === undefined || this.zCurrent === undefined) {
throw new Error('No setted: this.xCurrent === undefined || this.yCurrent === undefined || this.zCurrent === undefined')
}

const position = new Vec3(this.xCurrent, this.yCurrent, this.zCurrent)
return this.bot.blockAt(position)
}

setMinerCords(minerCords: MineCordsConfig) {
this.isLayerFinished = false
this.minerCords = minerCords
Expand All @@ -230,8 +252,8 @@ export class BehaviorMinerCheckLayer implements StateBehavior {
this.yStart = this.minerCords.yStart > this.minerCords.yEnd ? this.minerCords.yStart : this.minerCords.yEnd
this.yEnd = this.minerCords.yStart > this.minerCords.yEnd ? this.minerCords.yEnd : this.minerCords.yStart

this.yStart++
if (this.minerCords.tunnel === 'horizontally') {
this.yStart++
this.yEnd--
}

Expand Down Expand Up @@ -304,18 +326,31 @@ export class BehaviorMinerCheckLayer implements StateBehavior {
this.zCurrent = this.zStart
}

checkValidBlock(block: Block) {
if (this.minerCords === undefined) {
throw new Error('No setted: this.minerCords === undefined')
}
checkHorizontally(minerCords: MineCordsConfig, block: Block): boolean {
if (this.blocksToFind.includes(block.name)) return true;

if (!this.floorBlocksToFind.includes(block.name)) return false;
if (minerCords.yStart - 1 !== this.yCurrent) return false;

this.targets.position = block.position
this.foundLavaOrWater = true

return true
}

if (this.blocksToFind.includes(block.name) ||
(
this.floorBlocksToFind.includes(block.name) &&
this.minerCords.tunnel === 'horizontally' &&
(this.minerCords.yStart - 1) === this.yCurrent
)
) {
async checkVertically(block: Block): Promise<boolean> {
if (this.blocksToFind.includes(block.name)) {
this.targets.position = block.position
this.foundLavaOrWater = true
return true
};

if (!this.floorBlocksToFind.includes(block.name)) return false;

// If bottom block is air and bottom also is air/water/lava
const bottomBlock = await this.getBlock(block.position.x, block.position.y - 1, block.position.z)

if (this.blocksToFind.includes(bottomBlock.name) || this.floorBlocksToFind.includes(bottomBlock.name)) {
this.targets.position = block.position
this.foundLavaOrWater = true
return true
Expand All @@ -324,19 +359,17 @@ export class BehaviorMinerCheckLayer implements StateBehavior {
return false
}

moveToSeeBlock(x: number, y: number, z: number): Promise<void> {
return new Promise((resolve) => {
const goal = new mineflayerPathfinder.goals.GoalBlock(x, y, z)
this.bot.pathfinder.setMovements(this.targets.movements)
this.bot.pathfinder.setGoal(goal)
async checkValidBlock(block: Block) {
if (this.minerCords === undefined) {
throw new Error('No setted: this.minerCords === undefined')
}

this.bot.on('customEventPhysicTick', () => {
const block = this.getBlockType()
if (block) {
this.bot.pathfinder.setGoal(null)
resolve()
}
})
})
if (this.minerCords.tunnel === 'horizontally') {
return this.checkHorizontally(this.minerCords, block)
}

return this.checkVertically(block)
}


}
3 changes: 2 additions & 1 deletion core/src/NestedStateModules/minerJob/fillFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ function fillFunction(bot: Bot, targets: LegionStateMachineTargets) {
},
shouldTransition: () => {
const block = targets.position ? bot.blockAt(targets.position.offset(0, 1, 0)) : null
return (moveToBlock.isFinished() || moveToBlock.distanceToTarget() < 2.5) &&
return moveToBlock.isFinished() &&
block !== null && blocksCanBeReplaced.includes(block.name) &&
!bot.pathfinder.isMining()

}
}),

Expand Down
4 changes: 1 addition & 3 deletions core/src/NestedStateModules/minerJob/miningFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ function miningFunction(bot: Bot, targets: LegionStateMachineTargets) {
onTransition: () => {
targets.position = targets.minerJob.mineBlock
},
shouldTransition: () =>
(moveToBlock.isFinished() || moveToBlock.distanceToTarget() < 2.5) &&
!bot.pathfinder.isMining()
shouldTransition: () => moveToBlock.isFinished(2.5) && !bot.pathfinder.isMining()
}),

new StateTransition({
Expand Down
72 changes: 2 additions & 70 deletions core/test/legionTests/14_makeHoleHard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ describe('01 Basic Mining', function () {
}
}

bot.chat(`/fill -11 -50 -10 -11 -50 -10 minecraft:air`)

bot.chat(`/teleport ${xStart} ${yStart + 2} ${zStart}`)
bot.creative.stopFlying()
bot.test.becomeSurvival()
Expand Down Expand Up @@ -168,74 +170,4 @@ describe('01 Basic Mining', function () {
bot.once('finishedJob', () => resolve())
})
})

it('Making a Hole Z+ Reverse', (): Promise<void> => {
yStart--;

const newMinerCords: MineCordsConfig = {
...minerCords,
yStart: yStart,
yEnd: yStart,
orientation: "z+",
reverse: true,
}

const newConfig = {
...config,
minerCords: newMinerCords
}

Object.assign(bot.config, newConfig)
bot.emit('reloadBotConfig')
return new Promise((resolve) => {
bot.once('finishedJob', () => resolve())
})
})

it('Making a Hole Z- Normal', (): Promise<void> => {
yStart--;

const newMinerCords: MineCordsConfig = {
...minerCords,
yStart: yStart,
yEnd: yStart,
orientation: "z-",
reverse: false,
}

const newConfig = {
...config,
minerCords: newMinerCords
}

Object.assign(bot.config, newConfig)
bot.emit('reloadBotConfig')
return new Promise((resolve) => {
bot.once('finishedJob', () => resolve())
})
})

it('Making a Hole Z- Reverse', (): Promise<void> => {
yStart--;

const newMinerCords: MineCordsConfig = {
...minerCords,
yStart: yStart,
yEnd: yStart,
orientation: "z-",
reverse: true,
}

const newConfig = {
...config,
minerCords: newMinerCords
}

Object.assign(bot.config, newConfig)
bot.emit('reloadBotConfig')
return new Promise((resolve) => {
bot.once('finishedJob', () => resolve())
})
})

})

0 comments on commit f0754ed

Please sign in to comment.