Skip to content

Commit d84e23c

Browse files
fix aimmodulo360
1 parent a934763 commit d84e23c

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

lib/plugins/physics.js

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function cloneInput(input) {
3838
}
3939

4040
function inject(bot, {physicsEnabled, maxCatchupTicks}) {
41-
const FLYING_EVERY_N_TICKS = (bot.version === "1.21.5" ? 19 : 20)
41+
const POSITION_EVERY_N_TICKS = (bot.version === "1.21.5" ? 19 : 20)
4242
const PHYSICS_CATCHUP_TICKS = maxCatchupTicks ?? 4
4343
const world = {
4444
getBlock: (pos) => {
@@ -47,8 +47,6 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
4747
}
4848
const physics = Physics(bot.registry, world)
4949

50-
const positionUpdateSentEveryTick = bot.supportFeature('positionUpdateSentEveryTick')
51-
5250
bot.jumpQueued = false
5351
bot.jumpTicks = 0 // autojump cooldown
5452

@@ -62,8 +60,6 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
6260
sneak: false
6361
}
6462

65-
let lastSentYaw = null
66-
let lastSentPitch = null
6763
let doPhysicsTimer = null
6864
let lastPhysicsFrameTime = null
6965
let shouldUsePhysics = false
@@ -74,8 +70,12 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
7470
x: 0,
7571
y: 0,
7672
z: 0,
73+
// notchian
7774
yaw: 0,
7875
pitch: 0,
76+
// non-notchian
77+
entityYaw: 0,
78+
entityPitch: 0,
7979
onGround: false,
8080
ticker: 20,
8181
flags: {onGround: false, hasHorizontalCollision: false},
@@ -126,11 +126,6 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
126126
bot.emit('physicsTickEnd')
127127
}
128128

129-
// remove this when 'physicTick' is removed
130-
bot.on('newListener', (name) => {
131-
if (name === 'physicTick') console.warn('Mineflayer detected that you are using a deprecated event (physicTick)! Please use this event (physicsTick) instead.')
132-
})
133-
134129
function cleanup() {
135130
clearInterval(doPhysicsTimer)
136131
doPhysicsTimer = null
@@ -226,51 +221,52 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
226221
})
227222
}
228223

229-
// Increment the yaw in baby steps so that notchian clients (not the server) can keep up.
230-
const dYaw = deltaYaw(bot.entity.yaw, lastSentYaw)
231-
const dPitch = bot.entity.pitch - (lastSentPitch || 0)
224+
bot.entity.pitch = math.clamp(-PI / 2, bot.entity.pitch, PI / 2)
232225

233-
const maxDeltaYaw = PHYSICS_TIMESTEP * physics.yawSpeed
234-
const maxDeltaPitch = PHYSICS_TIMESTEP * physics.pitchSpeed
226+
const lastSentYaw = lastSent.entityYaw
227+
const lastSentPitch = lastSent.entityPitch
235228

236-
lastSentYaw += math.clamp(-maxDeltaYaw, dYaw, maxDeltaYaw)
237-
lastSentPitch += math.clamp(-maxDeltaPitch, dPitch, maxDeltaPitch)
229+
const sensitivity = conv.fromNotchianPitch(rawSensitivity) // this is equal to 100% sensitivity in vanilla
230+
const dYaw = Math.round(deltaYaw(bot.entity.yaw, lastSentYaw) / sensitivity) * sensitivity
231+
const dPitch = Math.round((bot.entity.pitch - (lastSentPitch || 0)) / sensitivity) * sensitivity
238232

239-
lastSentPitch = math.clamp(-PI / 2, lastSentPitch, PI / 2)
233+
const yaw = Math.fround(conv.toNotchianYaw(lastSentYaw + dYaw))
234+
const pitch = Math.fround(conv.toNotchianPitch(lastSentPitch + dPitch))
240235

241-
const yaw = Math.fround(conv.toNotchianYaw(lastSentYaw))
242-
const pitch = Math.fround(conv.toNotchianPitch(lastSentPitch))
243236
const position = bot.entity.position
244237
const onGround = bot.entity.onGround
245238

246239
// Only send a position update if necessary, select the appropriate packet
247240
const positionUpdated = lastSent.x !== position.x || lastSent.y !== position.y || lastSent.z !== position.z ||
248-
// Send a position update every second, even if no other update was made
249-
// This function rounds to the nearest 50ms (or PHYSICS_INTERVAL_MS) and checks if a second has passed.
250-
// should be
251-
lastSent.ticker === 0
241+
// Send a position update every second, even if no other update was made
242+
// This function rounds to the nearest 50ms (or PHYSICS_INTERVAL_MS) and checks if a second has passed.
243+
// should be
244+
lastSent.ticker === 0
252245
const lookUpdated = lastSent.yaw !== yaw || lastSent.pitch !== pitch
253246
if (positionUpdated && lookUpdated) {
254247
sendPacketPositionAndLook(position, yaw, pitch, onGround)
255-
lastSent.ticker = FLYING_EVERY_N_TICKS // only reset if positionUpdated is true
248+
lastSent.ticker = POSITION_EVERY_N_TICKS // only reset if positionUpdated is true
256249
} else if (positionUpdated) {
257250
sendPacketPosition(position, onGround)
258-
lastSent.ticker = FLYING_EVERY_N_TICKS // only reset if positionUpdated is true
251+
lastSent.ticker = POSITION_EVERY_N_TICKS // only reset if positionUpdated is true
259252
} else if (lookUpdated) {
260253
sendPacketLook(yaw, pitch, onGround)
261254
} else if (onGround !== lastSent.onGround) {
262255
// For versions < 1.12, one player packet should be sent every tick
263256
// for the server to update health correctly
264257
// For versions >= 1.12, onGround !== lastSent.onGround should be used, but it doesn't ever trigger outside of login
265-
lastSent.ticker = FLYING_EVERY_N_TICKS
266258
bot._client.write('flying', {
267259
onGround: bot.entity.onGround,
268260
flags: {onGround: bot.entity.onGround, hasHorizontalCollision: undefined} // 1.21.3+
269261
})
270-
} else {
262+
}
263+
264+
if (!positionUpdated) {
271265
lastSent.ticker -= 1
272266
}
273267

268+
lastSent.entityYaw = lastSentYaw + dYaw
269+
lastSent.entityPitch = lastSentPitch + dPitch
274270
lastSent.onGround = bot.entity.onGround // onGround is always set
275271
}
276272

@@ -354,10 +350,22 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
354350
}
355351

356352
let lookingTask = createDoneTask()
353+
let targetYaw = null
354+
let targetPitch = null
357355

358-
bot.on('move', () => {
359-
if (!lookingTask.done && Math.abs(deltaYaw(bot.entity.yaw, lastSentYaw)) < 0.001) {
360-
lookingTask.finish()
356+
bot.on('physicsTick', () => {
357+
if (!lookingTask.done) {
358+
if (Math.abs(deltaYaw(bot.entity.yaw, targetYaw)) < 0.001 && Math.abs(bot.entity.pitch - targetPitch) < 0.001) {
359+
lookingTask.finish()
360+
} else {
361+
// look toward it
362+
const dYaw = deltaYaw(targetYaw, bot.entity.yaw)
363+
const dPitch = targetPitch - bot.entity.pitch
364+
const yawChange = math.clamp(-physics.yawSpeed, dYaw, physics.yawSpeed)
365+
const pitchChange = math.clamp(-physics.pitchSpeed, dPitch, physics.pitchSpeed)
366+
bot.entity.yaw += yawChange
367+
bot.entity.pitch += pitchChange
368+
}
361369
}
362370
})
363371

@@ -381,8 +389,9 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
381389
bot.look = async (yaw, pitch, force) => {
382390
if (!lookingTask.done) {
383391
lookingTask.finish() // finish the previous one
392+
targetYaw = null
393+
targetPitch = null
384394
}
385-
lookingTask = createTask()
386395

387396
// this is done to bypass certain anticheat checks that detect the player's sensitivity
388397
// by calculating the gcd of how much they move the mouse each tick
@@ -394,16 +403,15 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
394403
return
395404
}
396405

397-
bot.entity.yaw += yawChange
398-
bot.entity.pitch += pitchChange
399-
400406
if (force) {
401-
lastSentYaw = yaw
402-
lastSentPitch = pitch
407+
bot.entity.yaw += yawChange
408+
bot.entity.pitch += pitchChange
403409
return
404410
}
405-
406-
await lookingTask.promise
411+
lookingTask = createTask()
412+
targetYaw = yaw
413+
targetPitch = pitch
414+
return await lookingTask.promise
407415
}
408416

409417
bot.lookAt = async (point, force) => {
@@ -435,31 +443,31 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
435443
// Modern path with bitflags object
436444
// Velocity is only set to 0 if the flag is not set, otherwise keep current velocity
437445
vel.set(
438-
packet.flags.x ? vel.x : 0,
439-
packet.flags.y ? vel.y : 0,
440-
packet.flags.z ? vel.z : 0
446+
packet.flags.x ? vel.x : 0,
447+
packet.flags.y ? vel.y : 0,
448+
packet.flags.z ? vel.z : 0
441449
)
442450
// If flag is set, then the corresponding value is relative, else it is absolute
443451
pos.set(
444-
packet.flags.x ? (pos.x + packet.x) : packet.x,
445-
packet.flags.y ? (pos.y + packet.y) : packet.y,
446-
packet.flags.z ? (pos.z + packet.z) : packet.z
452+
packet.flags.x ? (pos.x + packet.x) : packet.x,
453+
packet.flags.y ? (pos.y + packet.y) : packet.y,
454+
packet.flags.z ? (pos.z + packet.z) : packet.z
447455
)
448456
newYaw = (packet.flags.yaw ? conv.toNotchianYaw(bot.entity.yaw) : 0) + packet.yaw
449457
newPitch = (packet.flags.pitch ? conv.toNotchianPitch(bot.entity.pitch) : 0) + packet.pitch
450458
} else {
451459
// Legacy path with bitmask number
452460
// Velocity is only set to 0 if the flag is not set, otherwise keep current velocity
453461
vel.set(
454-
packet.flags & 1 ? vel.x : 0,
455-
packet.flags & 2 ? vel.y : 0,
456-
packet.flags & 4 ? vel.z : 0
462+
packet.flags & 1 ? vel.x : 0,
463+
packet.flags & 2 ? vel.y : 0,
464+
packet.flags & 4 ? vel.z : 0
457465
)
458466
// If flag is set, then the corresponding value is relative, else it is absolute
459467
pos.set(
460-
packet.flags & 1 ? (pos.x + packet.x) : packet.x,
461-
packet.flags & 2 ? (pos.y + packet.y) : packet.y,
462-
packet.flags & 4 ? (pos.z + packet.z) : packet.z
468+
packet.flags & 1 ? (pos.x + packet.x) : packet.x,
469+
packet.flags & 2 ? (pos.y + packet.y) : packet.y,
470+
packet.flags & 4 ? (pos.z + packet.z) : packet.z
463471
)
464472
newYaw = (packet.flags & 8 ? conv.toNotchianYaw(bot.entity.yaw) : 0) + packet.yaw
465473
newPitch = (packet.flags & 16 ? conv.toNotchianPitch(bot.entity.pitch) : 0) + packet.pitch
@@ -476,8 +484,6 @@ function inject(bot, {physicsEnabled, maxCatchupTicks}) {
476484

477485
shouldUsePhysics = true
478486
bot.jumpTicks = 0
479-
lastSentYaw = bot.entity.yaw
480-
lastSentPitch = bot.entity.pitch
481487

482488
bot.emit('forcedMove')
483489
})

0 commit comments

Comments
 (0)