From 623efc120c0147739ec97a151184b63b8d3369f5 Mon Sep 17 00:00:00 2001 From: Ic3Tank <61137113+IceTank@users.noreply.github.com> Date: Fri, 16 Feb 2024 23:36:53 +0100 Subject: [PATCH] asdfasdfadfsfasdsdfa --- examples/partialPathTest.js | 26 +++++++++--- src/index.ts | 2 +- .../pathProducers/partialPathProducer.ts | 41 +++++++++++++------ 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/examples/partialPathTest.js b/examples/partialPathTest.js index 49ecba4..7c31b40 100644 --- a/examples/partialPathTest.js +++ b/examples/partialPathTest.js @@ -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) }); /** diff --git a/src/index.ts b/src/index.ts index 1c824a7..5aa2084 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) } } diff --git a/src/mineflayer-specific/pathProducers/partialPathProducer.ts b/src/mineflayer-specific/pathProducers/partialPathProducer.ts index af0f2e8..e17ec92 100644 --- a/src/mineflayer-specific/pathProducers/partialPathProducer.ts +++ b/src/mineflayer-specific/pathProducers/partialPathProducer.ts @@ -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() 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