Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
GenerelSchwerz committed Mar 12, 2024
1 parent 04a438c commit d2bee9d
Show file tree
Hide file tree
Showing 16 changed files with 441 additions and 133 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ node_modules/
dist/
yarn-*
package-lock.json
*.log
vscode-profile-*
flamegraph.htm
4 changes: 2 additions & 2 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const bot = createBot({
username: "testing1",
auth: "offline",

// host: "node2.meowbot.de",
// port: 5000,
host: "node2.meowbot.de",
port: 5000,
version: "1.19.4",
});
const pathfinder = createPlugin();
Expand Down
6 changes: 3 additions & 3 deletions src/PathfinderHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
StraightUpExecutor,
IdleMovementExecutor
} from './mineflayer-specific/movements/movementExecutors'
import { DropDownOpt, ForwardJumpUpOpt, StraightAheadOpt } from './mineflayer-specific/post/optimizers'
import { DropDownOpt, ForwardJumpUpOpt, LandStraightAheadOpt } from './mineflayer-specific/post/optimizers'
import { BuildableOptimizer, OptimizationSetup, MovementOptimizer, OptimizationMap, Optimizer } from './mineflayer-specific/post'
import { ContinuousPathProducer, PartialPathProducer } from './mineflayer-specific/pathProducers'
import { Block, ResetReason } from './types'
Expand Down Expand Up @@ -91,8 +91,8 @@ DEFAULT_PROVIDER_EXECUTORS.reverse()
* They can reveal patterns at calculation time otherwise not noticeable at execution time.
*/
const DEFAULT_OPTIMIZERS = [
[Forward, StraightAheadOpt],
[Diagonal, StraightAheadOpt],
[Forward, LandStraightAheadOpt],
[Diagonal, LandStraightAheadOpt],
[ForwardDropDown, DropDownOpt],
[ForwardJump, ForwardJumpUpOpt]
] as Array<[BuildableMoveProvider, BuildableOptimizer]>
Expand Down
13 changes: 8 additions & 5 deletions src/ThePathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
StraightUpExecutor,
IdleMovementExecutor
} from './mineflayer-specific/movements/movementExecutors'
import { DropDownOpt, ForwardJumpUpOpt, StraightAheadOpt } from './mineflayer-specific/post/optimizers'
import { DropDownOpt, ForwardJumpUpOpt, LandStraightAheadOpt } from './mineflayer-specific/post/optimizers'
import { BuildableOptimizer, MovementOptimizer, OptimizationMap, Optimizer } from './mineflayer-specific/post'
import { ContinuousPathProducer, PartialPathProducer } from './mineflayer-specific/pathProducers'
import { Block, HandlerOpts, ResetReason } from './types'
Expand Down Expand Up @@ -89,8 +89,8 @@ DEFAULT_PROVIDER_EXECUTORS.reverse()
* They can reveal patterns at calculation time otherwise not noticeable at execution time.
*/
const DEFAULT_OPTIMIZERS = [
[Forward, StraightAheadOpt],
[Diagonal, StraightAheadOpt],
[Forward, LandStraightAheadOpt],
[Diagonal, LandStraightAheadOpt],
[ForwardDropDown, DropDownOpt],
[ForwardJump, ForwardJumpUpOpt]
] as Array<[BuildableMoveProvider, BuildableOptimizer]>
Expand Down Expand Up @@ -639,7 +639,6 @@ export class ThePathfinder {

if (this.resetReason == null) {
console.log('finished!', this.bot.entity.position, this.bot.listeners('entityMoved'), this.bot.listeners('entityGone'))
if (this.currentExecutor != null) await goal.onFinish(this.currentExecutor)
await this.cleanupBot()
manualCleanup()
setupWait()
Expand Down Expand Up @@ -864,12 +863,16 @@ export class ThePathfinder {
delete this.currentExecutor
}

async cleanupAll (goal: goals.Goal): Promise<void> {
async cleanupAll (goal: goals.Goal, executor = this.currentExecutor): Promise<void> {
if (goal instanceof goals.GoalDynamic && goal.dynamic) {
goal.cleanup?.()
}

await this.cleanupBot()
if (executor != null) {
await goal.onFinish(executor)
await this.cleanupBot()
}
// this.bot.chat(this.world.getCacheSize())
this.world.cleanup?.()

Expand Down
9 changes: 5 additions & 4 deletions src/mineflayer-specific/algs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export class AStarNeighbor extends AStar {
return
}

let bestLocal = node
const test = node
let bestLocal = test
// if (node.f < this.bestNode.f - 50) return;

// allow specific implementations to access visited and closed data.
Expand Down Expand Up @@ -150,7 +151,7 @@ export class AStarNeighbor extends AStar {
const neighbor = new CPathNode(gFromThisNode, heuristic, neighborData, node)
this.assignBestNodes(neighbor)
// if (neighbor.h < this.bestNode.h) this.bestNode = neighbor
if (neighbor.h < node.h) {
if (neighbor.h < test.h) {
bestLocal = neighbor
// bestLocal = neighbor;
// oldBestCost = pastNeighborNode.f;
Expand All @@ -164,14 +165,14 @@ export class AStarNeighbor extends AStar {
this.assignBestNodes(pastNeighbor)
this.openHeap.update(pastNeighbor)
// if (pastNeighbor.h < this.bestNode.h) this.bestNode = pastNeighbor
if (pastNeighbor.h < node.h) {
if (pastNeighbor.h < test.h) {
bestLocal = pastNeighbor
// this.test(pastNeighbor, maxDepth, depth + 1, seen)
// bestLocal = pastNeighborNode;
}
}
}
if (bestLocal !== node) {
if (bestLocal !== test) {
this.test(bestLocal, maxDepth, depth + 1, seen)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/mineflayer-specific/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class GoalNearXZ extends Goal {
* A goal to look at a specific coordinate within a certain distance.
*/
export class GoalLookAt extends Goal {
private readonly bb: AABB
protected readonly bb: AABB

public x: number
public y: number
Expand Down Expand Up @@ -447,7 +447,9 @@ export class GoalPlaceBlock extends GoalLookAt {
* Prevent overlap.
*/
isEnd (node: Move): boolean {
return super.isEnd(node) && node.x !== this.x && node.y !== this.y && node.z !== this.z
if (!super.isEnd(node)) return false
const bb = AABB.fromBlock(node).extend(0, 1, 0)
return !bb.collides(this.bb)
}

override async onFinish (node: MovementExecutor): Promise<void> {
Expand Down
157 changes: 157 additions & 0 deletions src/mineflayer-specific/movements/baritone/baritoneProviders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { Vec3 } from "vec3";
import { Move } from "../../move";
import { MovementProvider } from "../movementProvider";
import { Goal } from "../../goals";
import { canUseFrostWalker, canWalkOn, canWalkThrough, findPlaceOpts, getMiningDurationTicks, isBottomSlab } from "./movementHelper";
import { BreakHandler, PlaceHandler } from "../interactionUtils";
import { CENTER_AFTER_FALL_COST, COST_INF, FALL_N_BLOCKS_COST, JUMP_ONE_BLOCK_COST, WALK_OFF_BLOCK_COST, WALK_ONE_BLOCK_COST, WALK_ONE_OVER_SOUL_SAND_COST } from "../costs";
import { BlockInfo } from "../../world/cacheWorld";

export class IdleMovement extends MovementProvider {
movementDirs: Vec3[] = [];
provideMovements(start: Move, storage: Move[]): void {}
async performInit(thisMove: Move, currentIndex: number, path: Move[]): Promise<void> {}
async performPerTick(thisMove: Move, tickCount: number, currentIndex: number, path: Move[]): Promise<boolean> {
return true;
}
}

// base all code in this file off of:
// https://github.com/cabaletta/baritone/blob/1.19.4/src/main/java/baritone/pathing/movement/movements/MovementAscend.java

export class MovementAscend extends MovementProvider {
movementDirs = [new Vec3(0, 1, 0)];
provideMovements(start: Move, storage: Move[], goal: Goal, closed: Set<string>): void {
for (const dir of this.movementDirs) {
const off = start.cachedVec.plus(dir);
if (closed.has(off.toString())) return;
this.provideAscend(start, dir, storage, closed);
}
}

provideAscend(node: Move, dir: Vec3, storage: Move[], closed: Set<string>): void {
const blPlace = this.getBlockInfo(node, dir.x, 0, dir.y);

if (blPlace.isInvalid) return;

// potentially get rid of these, as we don't actually need them for the time being.
const toPlace: PlaceHandler[] = [];
const toBreak: BreakHandler[] = [];

let cost = 0;
if (!blPlace.physical) {
if (!blPlace.replaceable) {
if ((cost += this.safeOrBreak(blPlace, toBreak)) >= COST_INF) return;
}
if ((cost += this.safeOrPlace(blPlace, toPlace)) >= COST_INF) return;
if (findPlaceOpts(this, node, blPlace.position) == null) return;
}

const srcUp1 = this.getBlockInfo(node, 0, 1, 0);
const srcUp2 = this.getBlockInfo(node, 0, 2, 0);
const srcUp3 = this.getBlockInfo(node, 0, 3, 0);
// translate below to typescript
// if (context.get(x, y + 3, z).getBlock() instanceof FallingBlock && (MovementHelper.canWalkThrough(context, x, y + 1, z) || !(srcUp2.getBlock() instanceof FallingBlock))) {//it would fall on us and possibly suffocate us

if (srcUp3.canFall && (canWalkThrough(srcUp1) || !srcUp2.canFall)) {
return;
}

const srcDown1 = this.getBlockInfo(node, 0, -1, 0);
if (srcDown1.climbable) return;

const jumpFromBottomSlab = isBottomSlab(srcDown1);
const jumpToBottomSlab = isBottomSlab(blPlace);

if (jumpFromBottomSlab && !jumpToBottomSlab) {
return;
}

let walk = 0;
if (jumpToBottomSlab) {
if (jumpFromBottomSlab) {
walk = Math.max(JUMP_ONE_BLOCK_COST, WALK_ONE_BLOCK_COST); // we hit space immediately on entering this action
walk += this.settings.jumpCost;
} else {
walk = WALK_ONE_BLOCK_COST;
}
} else {
if (blPlace.block!.type === BlockInfo.soulsandId) {
walk = WALK_ONE_OVER_SOUL_SAND_COST;
} else {
walk = Math.max(JUMP_ONE_BLOCK_COST, WALK_ONE_BLOCK_COST);
}
walk += this.settings.jumpCost;
}

if ((cost += walk) >= COST_INF) return;
if ((cost += getMiningDurationTicks(this, srcUp2)) >= COST_INF) return;

const target1 = this.getBlockInfo(node, dir.x, 1, dir.z);
if ((cost += getMiningDurationTicks(this, target1)) >= COST_INF) return;

const target2 = this.getBlockInfo(node, dir.x, 2, dir.z);
if ((cost += getMiningDurationTicks(this, target2)) >= COST_INF) return;
}
}


export class MovementDescend extends MovementProvider {
movementDirs = [new Vec3(0, -1, 0)];
provideMovements(start: Move, storage: Move[], goal: Goal, closed: Set<string>): void {
for (const dir of this.movementDirs) {
this.provideDescend(start, dir, storage, closed);
}
}

provideDescend(node: Move, dir: Vec3, storage: Move[], closed: Set<string>): void {

const srcN1 = this.getBlockInfo(node, dir.x, -1, dir.z);
if (srcN1.climbable) return;


const srcN2 = this.getBlockInfo(node, dir.x, -2, dir.z);
if (!canWalkOn(srcN2)) {
return // for now, do not calculate this movement as it will be handled by movementFall.
// this.dynamicFallCosts(srcN2, cost);
}

if (canUseFrostWalker(this, srcN2)) {
return;
}

let cost = 0;

const destN1 = this.getBlockInfo(node, dir.x, -1, dir.z);
if ((cost+=getMiningDurationTicks(this, destN1)) >= COST_INF) return;

const dest = this.getBlockInfo(node, dir.x, 0, dir.z);
if ((cost+=getMiningDurationTicks(this, dest)) >= COST_INF) return;

const dest1 = this.getBlockInfo(node, dir.x, 1, dir.z);
if ((cost+=getMiningDurationTicks(this, dest1, true)) >= COST_INF) return;


let walk = WALK_OFF_BLOCK_COST
if (srcN1.block!.type === BlockInfo.soulsandId) {
walk = WALK_ONE_OVER_SOUL_SAND_COST / WALK_ONE_BLOCK_COST
}

cost += walk;
cost + Math.max(FALL_N_BLOCKS_COST[1], CENTER_AFTER_FALL_COST);
}


// TODO: implement mutables
dynamicFallCosts(info: BlockInfo, cost: number): void {

}
}

export class MovementDiagonal extends MovementProvider {
movementDirs = MovementProvider.diagonalDirs;
provideMovements(start: Move, storage: Move[], goal: Goal, closed: Set<string>): void {

}

}
58 changes: 58 additions & 0 deletions src/mineflayer-specific/movements/baritone/movementHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Vec3 } from "vec3";
import { BlockInfo } from "../../world/cacheWorld";
import { World } from "../../world/worldInterface";
import { Movement } from "../movement";
import { Vec3Properties } from "../../../types";

export function canWalkOn(info: BlockInfo): boolean {
if (info.block == null) return false
return info.block.boundingBox === "block" || info.safe
}

export function canWalkThrough(info: BlockInfo): boolean {
if (info.block == null) return false
return info.block.boundingBox === "empty" || info.safe
}

const ALL_DIRS_BUT_UP = [
new Vec3(1, 0, 0),
new Vec3(-1, 0, 0),
new Vec3(0, 0, 1),
new Vec3(0, 0, -1),
new Vec3(0, -1, 0),
]
export function findPlaceOpts(move: Movement, orgPos: Vec3Properties, pos: Vec3Properties): BlockInfo | null {
for (const dir of ALL_DIRS_BUT_UP) {
const nX = pos.x + dir.x
const nZ = pos.z + dir.z
if (nX === orgPos.x && nZ === orgPos.z) continue
const info = move.getBlockInfo(pos, dir.x, dir.y, dir.z)
if (canPlaceAgainst(info)) return info
}

return null;

}

export function canPlaceAgainst(info: BlockInfo): boolean {
return info.physical
}

export function isBottomSlab(info: BlockInfo): boolean {
return info.dY === 0.5
}

export function getMiningDurationTicks(move: Movement, info: BlockInfo, includeFalling=false): number {
if (!includeFalling) return move.breakCost(info);

const above = move.getBlockInfo(info.position, 0, 1, 0);
return move.breakCost(info) + getMiningDurationTicks(move, above, true); // recurse upwards. potentially slow.
}

export function getMiningDurationTicksCoords(move: Movement, pos: Vec3, includeFalling=false): number {
return getMiningDurationTicks(move, move.getBlockInfoRaw(pos), includeFalling);
}

export function canUseFrostWalker(move: Movement, info: BlockInfo): boolean {
return info.liquid && false; // TODO: frostwalker.
}
Loading

0 comments on commit d2bee9d

Please sign in to comment.