Skip to content

Commit

Permalink
upgrades, people. Upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
GenerelSchwerz committed Feb 20, 2024
1 parent cd4582b commit 022339f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
23 changes: 14 additions & 9 deletions src/ThePathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ export class ThePathfinder {
}

async cancel (): Promise<void> {
await this._cancel(1000, true)
this.userCancelled = true
await this.interrupt(1000, true)
}

async _cancel (timeout = 1000, userCalled = false, reasonStr?: ResetReason): Promise<void> {
async interrupt (timeout = 1000, cancelCalculation = true, reasonStr?: ResetReason): Promise<void> {
if (this.currentProducer == null) return console.log('no producer')
this.cancelCalculation = true
this.userCancelled = userCalled
this.cancelCalculation = cancelCalculation

if (this.currentExecutor == null) return console.log('no executor')
// if (this.currentExecutor.aborted) return console.trace('already aborted')
Expand Down Expand Up @@ -201,7 +201,7 @@ export class ThePathfinder {

async reset (reason: ResetReason, cancelTimeout = 1000): Promise<void> {
this.bot.emit('resetPath', reason)
await this._cancel(cancelTimeout, false, reason)
await this.interrupt(cancelTimeout, false, reason)
}

setupListeners (): void {
Expand Down Expand Up @@ -453,7 +453,7 @@ export class ThePathfinder {
*/
async goto (goal: goals.Goal, performOpts: PerformOpts = {}): Promise<void> {
if (this.executing || goal == null) {
await this._cancel()
await this.interrupt()
}
this.executing = true

Expand All @@ -477,16 +477,17 @@ export class ThePathfinder {
throw new Error('Goto: Purposefully cancelled by user.')
}

if (this.resetReason == null && !this.cancelCalculation) {
if (this.resetReason == null) {
console.log('finished!', this.bot.entity.position, this.bot.listenerCount('physicsTick'))
break
}

await this.cleanupBot()
}
}
} while (this.resetReason != null && !this.cancelCalculation)

if (doForever) {
await this.cleanupBot()
const refGoal = goal

if (this.resetReason == null) {
Expand Down Expand Up @@ -660,15 +661,19 @@ export class ThePathfinder {
newGoal = goals.GoalBlock.fromVec(nextMove.vec)
}

const path1 = await this.getPathFromToRaw(this.bot.entity.position, EMPTY_VEC, newGoal)
let path1 = await this.getPathFromToRaw(this.bot.entity.position, EMPTY_VEC, newGoal)

if (path1 === null) {
// done
this.bot.emit('exitedRecovery', entry)
} else if (no) {
path1 = await this.postProcess(path1)

// execution of past recoveries failed or not easily saveable, so full recovery needed.
this.bot.emit('exitedRecovery', entry)
await this.perform(path1, goal, entry + 1)
} else {
path1 = await this.postProcess(path1)
// attempt recovery to nearby node.
await this.perform(path1, newGoal, entry + 1)
path.path.splice(0, ind + 1)
Expand Down
45 changes: 25 additions & 20 deletions src/mineflayer-specific/movements/movementProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ export abstract class MovementProvider extends Movement {
}

getBlockInfo (pos: Vec3Properties, dx: number, dy: number, dz: number): BlockInfo {
const yes = new Vec3(pos.x + dx, pos.y + dy, pos.z + dz)
// pos = {
// x: Math.floor(pos.x),
// y: Math.floor(pos.y),
// z: Math.floor(pos.z)
// }

const yes = new Vec3(Math.floor(pos.x) + dx, Math.floor(pos.y) + dy, Math.floor(pos.z) + dz)
let move: Move | undefined = this.currentMove

let i = 0
Expand All @@ -69,25 +75,22 @@ export abstract class MovementProvider extends Movement {
move = move.parent
}

pos = {
x: Math.floor(pos.x),
y: Math.floor(pos.y),
z: Math.floor(pos.z)
}

const wantedDx = pos.x - this.orgPos.x + dx + this.halfway[0]
// const wantedDx = pos.x - this.orgPos.x + dx + this.halfway[0]
const wantedDx = yes.x - this.orgPos.x + this.halfway[0]

// if (wantedDx < 0 || wantedDx >= this.boundaries[0]) {
// return super.getBlockInfo(pos, dx, dy, dz);
// }

const wantedDz = pos.z - this.orgPos.z + dz + this.halfway[1]
// const wantedDz = pos.z - this.orgPos.z + dz + this.halfway[1]
const wantedDz = yes.z - this.orgPos.z + this.halfway[1]

// if (wantedDz < 0 || wantedDz >= this.boundaries[2]) {
// return super.getBlockInfo(pos, dx, dy, dz);
// }

const wantedDy = pos.y - this.orgPos.y + dy + this.halfway[2]
// const wantedDy = pos.y - this.orgPos.y + dy + this.halfway[2]
const wantedDy = yes.y - this.orgPos.y + this.halfway[2]

// if (wantedDy < 0 || wantedDy >= this.boundaries[2]) {
// return super.getBlockInfo(pos, dx, dy, dz);
Expand All @@ -104,7 +107,8 @@ export abstract class MovementProvider extends Movement {
wantedDy >= this.boundaries[2]
) {
// console.log('hey', idx, this.localData[idx])
return super.getBlockInfo(pos, dx, dy, dz)
return this.world.getBlockInfo(yes)
// return super.getBlockInfo(pos, dx, dy, dz)
// console.log('out of bounds', pos, this.orgPos, wantedDx, wantedDy, wantedDz, this.boundaries)
}

Expand Down Expand Up @@ -141,7 +145,8 @@ export abstract class MovementProvider extends Movement {
return data
}

const ret = super.getBlockInfo(pos, dx, dy, dz)
const ret = this.world.getBlockInfo(yes)
// const ret = super.getBlockInfo(pos, dx, dy, dz)

this.localData[idx] = ret
return ret
Expand Down Expand Up @@ -256,15 +261,15 @@ export class MovementHandler implements AMovementProvider<Move> {
newMove.provideMovements(currentMove, moves, this.goal, closed)
}

for (const move of moves) {
const bl = move.moveType.getBlockInfo(move, 0, 0, 0)
if (bl.liquid && move.toPlace.length > 0) {
const blocksAtPoses = move.toPlace.map((p) => move.moveType.getBlockInfo(p, 0, 0, 0))
console.log(blocksAtPoses.map(i => [i, i.block?.getProperties(), (i.block as any)?._properties]))
// for (const move of moves) {
// const bl = move.moveType.getBlockInfo(move, 0, 0, 0)
// if (bl.liquid && move.toPlace.length > 0) {
// const blocksAtPoses = move.toPlace.map((p) => move.moveType.getBlockInfo(p, 0, 0, 0))
// console.log(blocksAtPoses.map(i => [i, i.block?.getProperties(), (i.block as any)?._properties]))

// throw new Error(`Liquid detected in toPlace: ${move.moveType.constructor.name} with placements ${move.toPlace.map((p) => p.vec).join(', ')} at pos ${move.vec.toString()} `)
}
}
// // throw new Error(`Liquid detected in toPlace: ${move.moveType.constructor.name} with placements ${move.toPlace.map((p) => p.vec).join(', ')} at pos ${move.vec.toString()} `)
// }
// }
// this.resetLocalData() // same speed, but less memory efficient.

// console.log(moves.length, moves.map(m=>m.moveType.constructor.name))
Expand Down
18 changes: 9 additions & 9 deletions src/mineflayer-specific/pathProducers/partialPathProducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export class PartialPathProducer implements PathProducer {
start = this.start
}

const lastClosedSet = this.lastAstarContext != null ? this.lastAstarContext.closedDataSet : new Set<string>()
// const lastClosedSet = this.lastAstarContext != null ? this.lastAstarContext.closedDataSet : new Set<string>()
const ret = new AStar(start, moveHandler, this.goal, -1, 45, -1, 0)

ret.closedDataSet = lastClosedSet
// ret.closedDataSet = lastClosedSet
return ret
}

Expand All @@ -75,11 +75,8 @@ export class PartialPathProducer implements PathProducer {

const result = this.lastAstarContext.compute()

const lastNode = result.path[result.path.length - 1]
if (lastNode != null) {
this.latestCost = this.latestCost + result.cost
console.info('Partial Path cost increased by', lastNode.cost, 'to', this.latestCost, 'total', this.latestMove?.vec, 'pos')
}
this.latestCost = this.latestCost + result.cost
console.info('Partial Path cost increased by', result.cost, 'to', this.latestCost, 'total', this.latestMove?.vec, 'pos')

if (result.status === 'noPath') {
this.latestMoves.pop()
Expand All @@ -99,9 +96,12 @@ export class PartialPathProducer implements PathProducer {
}

if (result.path.length > this.maxPathLength || result.status === 'success') {
this.latestMove = result.path[result.path.length - 1]
// const val = result.path.length - 1
const val = Math.ceil(result.path.length * 3 / 4) - 1
this.latestMove = result.path[val]
const toTake = result.path.slice(0, val + 1)
this.latestMoves.push(this.latestMove)
this.lastPath = [...this.lastPath, ...result.path]
this.lastPath = [...this.lastPath, ...toTake]
}

// console.log(result.path.length, 'found path length', this.lastPath.length, 'total length', this.lastPath.map(p => p.entryPos.toString()), this.lastPath[this.lastPath.length - 1].entryPos)
Expand Down

1 comment on commit 022339f

@AndyIsHereBoi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Please sign in to comment.