Skip to content

Commit e645e24

Browse files
shit
1 parent e5bf68d commit e645e24

12 files changed

Lines changed: 229 additions & 249 deletions

File tree

examples/basic.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ bot.once("spawn", async () => {
5454
bot.loadPlugin(loader);
5555

5656
(bot.physics as any).autojumpCooldown = 0;
57+
bot.physics.yawSpeed = 50;
58+
(bot.physics as any).pitchSpeed = 50;
5759

5860
const rlline = rl.createInterface({
5961
input: process.stdin,
@@ -105,7 +107,7 @@ async function cmdHandler(username: string, msg: string) {
105107
return;
106108
}
107109

108-
const goal = GoalBlock.fromVec(player.entity.position)
110+
const goal = GoalBlock.fromVec(player.entity.position.offset(0,1,0))
109111
bot.whisper(username, `Coming to you ${username}`);
110112
await bot.pathfinder.goto(goal);
111113
bot.whisper(username, `Arrived, ${username}`);

src/ThePathfinder.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -775,19 +775,19 @@ export class ThePathfinder {
775775
continue
776776
}
777777

778-
// console.log('performing', move.moveType.constructor.name, 'at index', currentIndex + 1, 'of', path.path.length)
779-
// console.log(
780-
// 'toPlace',
781-
// move.toPlace.map((p) => p.vec),
782-
// 'toBreak',
783-
// move.toBreak.map((b) => b.vec),
784-
// 'entryPos',
785-
// move.entryPos,
786-
// 'asVec',
787-
// move.vec,
788-
// 'exitPos',
789-
// move.exitPos
790-
// )
778+
console.log('performing', move.moveType.constructor.name, 'at index', currentIndex + 1, 'of', pathEx.length, path.path.length)
779+
console.log(
780+
'toPlace',
781+
move.toPlace.map((p) => p.vec),
782+
'toBreak',
783+
move.toBreak.map((b) => b.vec),
784+
'entryPos',
785+
move.entryPos,
786+
'asVec',
787+
move.vec,
788+
'exitPos',
789+
move.exitPos
790+
)
791791

792792
// wrap this code in a try-catch as we intentionally throw errors.
793793
try {

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PathfinderOptions, ThePathfinder } from './ThePathfinder'
44
import { Vec3 } from 'vec3'
55

66
import utilPlugin from '@nxg-org/mineflayer-util-plugin'
7-
import physicsUtil, { initSetup } from '@nxg-org/mineflayer-physics-util'
7+
import physicsUtil, { EPhysicsCtx, PlayerState, initSetup } from '@nxg-org/mineflayer-physics-util'
88

99
import { Block, PlaceBlockOptions, ResetReason } from './types'
1010
import { PathingUtil } from './PathingUtil'
@@ -27,13 +27,19 @@ export function createPlugin (opts?: {
2727
initSetup(bot.registry)
2828
bot.pathfinder = new ThePathfinder(bot, opts)
2929
bot.pathingUtil = new PathingUtil(bot)
30+
bot.ectx = EPhysicsCtx.FROM_BOT(bot.physicsUtil.engine, bot)
31+
32+
bot.on('physicsTick', () => {
33+
bot.ectx.state.update(bot)
34+
})
3035
}
3136
}
3237

3338
declare module 'mineflayer' {
3439
interface Bot {
3540
pathfinder: ThePathfinder
3641
pathingUtil: PathingUtil
42+
ectx: EPhysicsCtx<PlayerState>
3743

3844
_placeBlockWithOptions: (referenceBlock: Block, faceVector: Vec3, options?: PlaceBlockOptions) => Promise<void>
3945
}

src/mineflayer-specific/movements/interactionUtils.ts

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,14 @@ import { AABB, AABBUtils, BlockFace } from '@nxg-org/mineflayer-util-plugin'
1010
import { CancelError } from '../exceptions'
1111
import { MovementOptions } from './movement'
1212
import { MovementExecutor } from './movementExecutor'
13-
import { Block } from '../../types'
13+
import { Block, InteractOpts, InteractionPerformInfo, RayType } from '../../types'
1414
import { Task } from '../../utils'
15-
import { PlayerState } from '@nxg-org/mineflayer-physics-util/dist/physics/states'
15+
import { ControlStateHandler, PlayerState } from '@nxg-org/mineflayer-physics-util/dist/physics/states'
1616

1717
export type InteractType = 'water' | 'solid' | 'replaceable'
18-
export type RayType = {
19-
intersect: Vec3
20-
face: BlockFace
21-
} & Block
22-
23-
interface InteractionPerformInfo {
24-
ticks: number
25-
tickAllowance: number
26-
shiftTick: number
27-
raycasts: RayType[]
28-
}
2918

30-
export interface InteractOpts {
31-
info?: InteractionPerformInfo
32-
returnToStart?: boolean
33-
returnToPos?: Vec3
34-
predictBlock?: boolean
35-
}
19+
20+
3621

3722
/**
3823
* TODO: Predict time of rotation for looking.
@@ -99,7 +84,7 @@ export abstract class InteractHandler {
9984

10085
abstract getItem (bot: Bot, block?: Block): Item | null
10186
abstract perform (bot: Bot, item: Item | null, opts?: InteractOpts): Promise<void>
102-
abstract performInfo (bot: Bot, ticks?: number): Promise<InteractionPerformInfo>
87+
abstract performInfo (bot: Bot, ticks?: number, controls?: ControlStateHandler): Promise<InteractionPerformInfo>
10388
abstract toBlockInfo (): BlockInfo
10489

10590
abstract abort (bot: Bot): Promise<void>
@@ -280,7 +265,7 @@ export class PlaceHandler extends InteractHandler {
280265
return true
281266
}
282267

283-
async performInfo (bot: Bot, ticks = 15, scale = 0.5): Promise<InteractionPerformInfo> {
268+
async performInfo (bot: Bot, ticks = 15, controls?: ControlStateHandler, scale = 0.1): Promise<InteractionPerformInfo> {
284269
// bot.chat(`/particle flame ${this.vec.x} ${this.vec.y} ${this.vec.z} 0 0 0 0 1 force`);
285270
// bot.chat(`pointed to: ${this.vec}`);
286271
// console.log(this.vec)
@@ -293,10 +278,13 @@ export class PlaceHandler extends InteractHandler {
293278
const works = []
294279

295280
let startTick = 0
296-
let shiftTick = Infinity
281+
let shiftTick = bot.controlState.sneak ? 0 : Infinity
297282
let i = 0
298283
for (; i <= ticks; i++) {
299284
const ectx = EPhysicsCtx.FROM_BOT(bot.physicsUtil.engine, bot)
285+
if (controls != null) {
286+
ectx.state.control = controls
287+
}
300288

301289
const state = ectx.state as PlayerState
302290

@@ -308,7 +296,7 @@ export class PlaceHandler extends InteractHandler {
308296

309297
const eyePos = state.pos.offset(0, state.eyeHeight, 0)
310298
// const bb0 = AABB.fromBlock(this.vec)
311-
const bb1 = AABBUtils.getEntityAABBRaw({ position: state.pos, width: 0.6, height: 1.8 })
299+
const stateBB = state.getBB()
312300

313301
const dx = state.pos.x - (this.vec.x + 0.5)
314302
const dy = state.pos.y + bot.entity.height - (this.vec.y + 0.5)
@@ -346,15 +334,18 @@ export class PlaceHandler extends InteractHandler {
346334
if (rayRes === null) continue
347335
const pos = rayRes.position.plus(this.faceToVec(rayRes.face))
348336
if (pos.equals(this.vec)) {
349-
if (bb1.containsVec(rayRes.intersect)) continue
350-
if (AABB.fromBlock(pos).intersects(bb1)) {
337+
if (stateBB.containsVec(rayRes.intersect)) continue
338+
// we fell off here.
339+
if (AABB.fromBlock(pos).intersects(stateBB)) {
351340
if (shiftTick === Infinity) {
352341
shiftTick = i
353342
i--
354343
}
355344

356345
continue
357346
}
347+
348+
// this matches what we want.
358349
good++
359350
if (startTick === 0) startTick = i
360351
works.push(rayRes as unknown as RayType)
@@ -364,7 +355,7 @@ export class PlaceHandler extends InteractHandler {
364355
if (good === 0) return { ticks: Math.floor((i + startTick) / 2), tickAllowance: i - startTick, shiftTick, raycasts: works }
365356
}
366357
}
367-
// console.log('RAN I', i)
358+
// console.trace('RAN I', i, works.length)
368359
return { ticks: Infinity, tickAllowance: Infinity, shiftTick: Infinity, raycasts: works }
369360
}
370361

@@ -405,7 +396,9 @@ export class PlaceHandler extends InteractHandler {
405396
}
406397

407398
case 'solid': {
399+
console.time('placeBlock')
408400
if (this.getCurrentItem(bot) !== item) await this.equipItem(bot, item)
401+
console.timeEnd('placeBlock')
409402

410403
const predictBlock = opts.predictBlock ?? true
411404

@@ -421,6 +414,7 @@ export class PlaceHandler extends InteractHandler {
421414
// const end = performance.now()
422415
// console.log("info took", end - start, "ms");
423416
}
417+
424418

425419
const stateEyePos = bot.entity.position.offset(0, state.eyeHeight, 0)
426420
const lookDir = bot.util.getViewDir()
@@ -437,6 +431,7 @@ export class PlaceHandler extends InteractHandler {
437431

438432
// const invalidPlacement1 = AABBUtils.getEntityAABB(bot.entity).intersects(AABB.fromBlock(pos))
439433

434+
const yLevel = bot.entity.position.y
440435
let i = 0
441436
for (; i < works.ticks; i++) {
442437
if (i === works.shiftTick) bot.setControlState('sneak', true)
@@ -447,37 +442,40 @@ export class PlaceHandler extends InteractHandler {
447442
const sPos = state.pos.offset(0, state.eyeHeight, 0)
448443
const testCheck = (await bot.world.raycast(
449444
sPos,
450-
rayRes.intersect.minus(sPos).normalize().scale(0.5),
451-
PlaceHandler.reach * 2
445+
rayRes.intersect.minus(sPos).normalize().scale(0.1),
446+
PlaceHandler.reach * 10
452447
)) as unknown as RayType
453448

454449
if (testCheck === null) break
455450

456451
const pos1 = testCheck.position.plus(this.faceToVec(testCheck.face))
457452
const pos1Bl = AABB.fromBlock(pos1)
458-
if (testCheck.position.equals(rayRes.position) && testCheck.face === rayRes.face && !state.getBB().intersects(pos1Bl)) {
459-
// console.log("skipping on tick", i, state.getAABB(), state.pos, pos1Bl);
460-
if (i < works.ticks - 1 && works.ticks !== 0) {
453+
if (testCheck.position.equals(rayRes.position) && testCheck.face === rayRes.face && !state.getBB().intersects(pos1Bl) && yLevel === state.pos.y) {
454+
console.log("skipping on tick", i, works.ticks, works.tickAllowance, state.pos, pos1, rayRes.intersect, testCheck.face, rayRes.face);
455+
console.log(works.raycasts.length)
456+
// if (i < works.ticks - 1 && works.ticks !== 0) {
461457
await bot.waitForTicks(1)
462-
}
458+
// }
463459
break
464460
}
461+
462+
if (!this.move.isLookingAt(rayRes.intersect)) {
463+
await this.move.lookAt(rayRes.intersect)
464+
// await bot.lookAt(rayRes.intersect, this.settings.forceLook)
465+
}
466+
467+
465468
await bot.waitForTicks(1)
466469
}
467470

468471
const botBB = AABBUtils.getEntityAABBRaw({ position: bot.entity.position, width: 0.6, height: 1.8 })
469472

470-
if (!this.move.isLookingAt(rayRes.intersect)) {
471-
await this.move.lookAt(rayRes.intersect)
472-
await bot.lookAt(rayRes.intersect, this.settings.forceLook)
473-
}
474473

475-
// console.log(i, works.ticks, works.tickAllowance, works.shiftTick, rayRes.intersect, this.faceToVec(rayRes.face));
476-
// console.log(bot.entity.position, bot.entity.velocity);
474+
console.log(i, works.ticks, works.tickAllowance, works.shiftTick, rayRes.intersect, this.faceToVec(rayRes.face));
477475

478476
const invalidPlacement = botBB.intersects(posBl)
479477
if (invalidPlacement) {
480-
// console.log('invalid placement', bot.entity.position, invalidPlacement, botBB, posBl)
478+
console.log('invalid placement', bot.entity.position, invalidPlacement, botBB, posBl)
481479
// console.log(botBB, posBl);
482480
await bot.lookAt(rayRes.intersect, this.settings.forceLook)
483481
throw new CancelError('Invalid placement')
@@ -486,31 +484,40 @@ export class PlaceHandler extends InteractHandler {
486484
let finished = false
487485
let sneaking = false
488486
const direction = this.faceToVec(rayRes.face)
489-
// console.log("looking at", rayRes.intersect);
487+
console.log("looking at", rayRes.intersect, bot.entity.position, bot.entity.velocity.y);
490488
// start = performance.now()
491489
this._placeTask = bot._placeBlockWithOptions(rayRes, direction, { forceLook: 'ignore', swingArm: 'right' })
492490
if (predictBlock) {
493-
// console.log('predicting block')
491+
console.log('predicting block')
494492
bot.world.setBlock(rayRes.position.plus(direction), BlockInfo.PBlock.fromStateId(BlockInfo.substituteBlockStateId, 0))
495493
// bot.world.setBlockStateId(rayRes.position.plus(direction), BlockInfo.substituteBlockStateId);
496494
}
497495

498496
this._internalLock = false
499497

500-
// auto crouch if block does not update (this is outdated code, see predictBlock)
501-
setTimeout(() => {
502-
if (finished) return
503-
sneaking = true
504-
bot.setControlState('sneak', true)
505-
}, Math.max(30 - bot._client.latency, 0))
498+
if (!bot.controlState.sneak) {
506499

507-
await this._placeTask
508-
finished = true
500+
// auto crouch if block does not update (this is outdated code, see predictBlock)
501+
setTimeout(() => {
502+
if (finished) return
503+
sneaking = true
504+
bot.setControlState('sneak', true)
505+
}, Math.max(30 - bot._client.latency, 0))
509506

510-
if (sneaking) bot.setControlState('sneak', false)
511-
if (works.shiftTick !== Infinity) bot.setControlState('sneak', false)
507+
await this._placeTask
508+
finished = true
509+
510+
if (sneaking) bot.setControlState('sneak', false)
511+
if (works.shiftTick !== Infinity) bot.setControlState('sneak', false)
512+
this.task?.finish()
513+
} else {
514+
515+
await this._placeTask
516+
finished = true
517+
this.task?.finish()
518+
}
512519

513-
this.task?.finish()
520+
514521
break
515522
}
516523
case 'replaceable':
@@ -605,7 +612,8 @@ export class BreakHandler extends InteractHandler {
605612
async performInfo (bot: Bot, ticks = 15): Promise<InteractionPerformInfo> {
606613
const bb = AABB.fromBlock(this.vec)
607614

608-
return bb.distanceToVec(bot.entity.position.offset(0, 1.62, 0)) < BreakHandler.reach + 5
615+
const eyeHeight = bot.ectx.state.eyeHeight
616+
return bb.distanceToVec(bot.entity.position.offset(0, eyeHeight, 0)) < BreakHandler.reach + 5
609617
? { ticks: 0, tickAllowance: 0, shiftTick: 0, raycasts: [] }
610618
: { ticks: Infinity, tickAllowance: Infinity, shiftTick: Infinity, raycasts: [] }
611619
}

src/mineflayer-specific/movements/movement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const DEFAULT_MOVEMENT_OPTS: MovementOptions = {
5959
digCost: 1,
6060
jumpCost: 0.5,
6161
velocityKillCost: 2, // implement at a later date.
62-
forceLook: true,
62+
forceLook: false,
6363
careAboutLookAlignment: true,
6464
allowDiagonalBridging: true,
6565
movementTimeoutMs: 1000,

0 commit comments

Comments
 (0)