Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/mineflayer-specific/movements/movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,13 @@ export abstract class Movement {
/**
* Ticks to travel `blocks` blocks of flat ground.
*
* The bot sprints when allowed (the fastest way to move), otherwise it walks.
* Pass `1` for one straight block, `Math.SQRT2` for one diagonal block, etc.
* On land the bot sprints when allowed (the fastest way to move), otherwise it
* walks. In water it can do neither, so it is priced at the (much slower) swim
* speed, which is why callers pass `inLiquid`. Pass `1` for one straight block,
* `Math.SQRT2` for one diagonal block, etc.
*/
travelCost (blocks: number): number {
travelCost (blocks: number, inLiquid = false): number {
if (inLiquid) return blocks * WALK_ONE_IN_WATER_COST
return blocks * (this.settings.allowSprinting ? SPRINT_ONE_BLOCK_COST : WALK_ONE_BLOCK_COST)
}

Expand Down
14 changes: 8 additions & 6 deletions src/mineflayer-specific/movements/movementProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export class Forward extends MovementProvider {
getMoveForward (start: Move, dir: Vec3, neighbors: Move[]): void {
const pos = start.cachedVec

let cost = this.travelCost(1) // sprint/walk one block forward

if (this.getBlockInfo(pos, 0, 0, 0).liquid) cost += this.settings.liquidCost
// In water the bot swims (no sprint/walk), so price the block at the water
// cost itself rather than land speed + a flat add (which undercharged it).
const inLiquid = this.getBlockInfo(pos, 0, 0, 0).liquid
let cost = this.travelCost(1, inLiquid)

const blockC = this.getBlockInfo(pos, dir.x, 0, dir.z)
if (blockC.isInvalid) return // out of range.
Expand Down Expand Up @@ -100,16 +101,17 @@ export class Diagonal extends MovementProvider {
}

getMoveDiagonal (node: Move, dir: Vec3, neighbors: Move[], goal: goals.Goal): void {
let cost = this.travelCost(Math.SQRT2) // one diagonal block is sqrt(2) blocks of travel
// In water the bot swims (no sprint/walk); price the diagonal at the water
// cost, which also scales with the sqrt(2) distance (a flat add did not).
const inLiquid = this.getBlockInfo(node, 0, 0, 0).liquid
let cost = this.travelCost(Math.SQRT2, inLiquid)

const block0 = this.getBlockInfo(node, dir.x, 0, dir.z)

if (block0.isInvalid) return // out of range.

if (!block0.walkthrough) return

if (this.getBlockInfo(node, 0, 0, 0).liquid) cost += this.settings.liquidCost

const toBreak: BreakHandler[] = []
const toPlace: PlaceHandler[] = []
const block00 = this.getBlockInfo(node, 0, 0, 0)
Expand Down
Loading