Skip to content

Commit

Permalink
asdfasdfadfsfasdsdfa
Browse files Browse the repository at this point in the history
  • Loading branch information
IceTank committed Feb 16, 2024
1 parent 7a1bb04 commit 623efc1
Showing 3 changed files with 49 additions and 20 deletions.
26 changes: 20 additions & 6 deletions examples/partialPathTest.js
Original file line number Diff line number Diff line change
@@ -15,15 +15,18 @@ const bot = createBot({
host: 'Ic3TankD2HO.aternos.me',
version: '1.19.4'
});
const pathfinder = createPlugin({ partialPathProducer: true });
const pathfinder = createPlugin();

const targetGoal = new GoalBlock(-345, 109, 277)

function chatEverywhere(message) {
bot.chat(message);
console.log(message);
}

async function debugPath(goal) {
async function debugPath(goal, partialPathProducer) {
bot.pathfinder.world.setEnabled(true)
bot.pathfinder.setPathfinderOptions({ partialPathProducer })
console.info('Target:', new Vec3(goal.x, goal.y, goal.z).toString())
const generator = bot.pathfinder.getPathFromTo(bot.entity.position, new Vec3(0, 0, 0), goal)

@@ -51,9 +54,16 @@ async function debugPath(goal) {

const isDone = lastResult?.status === 'success'
const visitedNodes = lastResult?.visitedNodes ?? 0
const cost = lastResult?.cost ?? 0
const lastMove = lastResult?.path?.[lastResult.path.length - 1]
const lastPos = lastMove ? new Vec3(lastMove.x, lastMove.y, lastMove.z) : undefined
if (isDone) {
chatEverywhere(`Calc done: ✔️, Overlap: ${nodeOverlap} Visited: ${visitedNodes} (${(visitedNodes / ((end - start) / 1000)).toFixed(1)} n/s)`)
chatEverywhere(`Time: ${end - start}ms`)
chatEverywhere(`✔️ Calc ${partialPathProducer ? 'partial' : 'full'}, Overlap: ${nodeOverlap} Visited: ${visitedNodes} (${(visitedNodes / ((end - start) / 1000)).toFixed(1)} n/s)`)
chatEverywhere(` Time: ${end - start}ms`)
chatEverywhere(` Cost: ${cost}, Length: ${lastResult.path.length}`)
if (lastPos) {
console.info(' Last move distance to goal', lastPos.distanceTo(new Vec3(goal.x, goal.y, goal.z)))
}
} else {
chatEverywhere(`Calc done: ❌, Overlap: ${nodeOverlap} Visited: ${visitedNodes} (${(visitedNodes / ((end - start) / 1000)).toFixed(1)} n/s)`)
}
@@ -86,8 +96,10 @@ bot.once("spawn", async () => {
})

setTimeout(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000))
await bot.waitForChunksToLoad();
await debugPath(new GoalBlock(-311, 69, 304))
await debugPath(targetGoal, false)
await debugPath(targetGoal, true)
}, 1000)
});

@@ -117,7 +129,9 @@ bot._client.on("animation", (data) => {
const block = rayTraceEntitySight({ entity });
if (!block) return;
const goal = new goals.GoalBlock(block.position.x, block.position.y + 1, block.position.z)
debugPath(goal).catch(console.error);
debugPath(goal, true).then(() => {
debugPath(goal, false)
}).catch(console.error)
});

/**
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ export function createPlugin (settings?: any) {
return function (bot: Bot) {
void BlockInfo.init(bot.registry) // set up block info
if (!bot.hasPlugin(utilPlugin)) bot.loadPlugin(utilPlugin)
bot.pathfinder = new ThePathfinder(bot)
bot.pathfinder = new ThePathfinder(bot, undefined, undefined, settings)
bot.pathingUtil = new PathingUtil(bot)
}
}
41 changes: 28 additions & 13 deletions src/mineflayer-specific/pathProducers/partialPathProducer.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ export class PartialPathProducer implements PathProducer {
private readonly world: World
private readonly movements: ExecutorMap
private latestMove: Move | undefined
private latestMoves: Move[] = []
private latestCost: number = 0
private lastPath: Move[] = []

private readonly gcInterval: number = 10
@@ -43,32 +45,45 @@ export class PartialPathProducer implements PathProducer {
} else {
start = this.start
}
const lastClosedSet = this.lastAstarContext ? this.lastAstarContext.closedDataSet : new Set<string>()
this.lastAstarContext = new AStar(start, moveHandler, this.goal, -1, 45, -1, 0)
this.lastAstarContext.closedDataSet = lastClosedSet

const result = this.lastAstarContext.compute()

if ((global.gc != null) && ++this.lastGc % this.gcInterval === 0) {
// const starttime = performance.now()
const lastNode = result.path[result.path.length - 1]
if (lastNode != null) {
this.latestCost = this.latestCost + lastNode.cost
console.info('Partial Path cost increased by', lastNode.cost, 'to', this.latestCost, 'total')
}

// This probably does not work lol
// someone needs to think about this more
if (result.status === 'noPath') {
this.latestMove = this.latestMoves[this.latestMoves.length - 2]
this.latestMoves.pop()

if (this.lastGc % (this.gcInterval * 10) === 0) {
// global.gc();
} else {
(global as any).gc(true)
if (!this.latestMove) {
return {
result: {
...result,
cost: this.latestCost,
path: this.lastPath
},
astarContext: this.lastAstarContext
}
}

// console.log('Garbage collection took', performance.now() - starttime, 'ms')
} else {
// console.log('Garbage collection unavailable. Pass --expose-gc '
// + 'when launching node to enable forced garbage collection.');
this.latestMove = result.path[result.path.length - 1]
this.latestMoves.push(this.latestMove)
}

this.latestMove = result.path[result.path.length - 1]
this.lastPath = [...this.lastPath, ...result.path]

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)
// 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)
return {
result: {
...result,
cost: this.latestCost,
path: this.lastPath
},
astarContext: this.lastAstarContext

0 comments on commit 623efc1

Please sign in to comment.