Skip to content

Commit 5301562

Browse files
better typing
1 parent 198e844 commit 5301562

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

lib/plugins/physics.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const math = require('../math')
44
const conv = require('../conversions')
55
const { performance } = require('perf_hooks')
66
const { createDoneTask, createTask } = require('../promise_utils')
7-
const { EntityPhysics, EPhysicsCtx, PhysicsWorldSettings, initSetup } = require('@nxg-org/mineflayer-physics-util')
7+
const { EntityPhysics, EPhysicsCtx, PhysicsWorldSettings, initSetup, PlayerState } = require('@nxg-org/mineflayer-physics-util')
88

99
module.exports = inject
1010

@@ -53,7 +53,11 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
5353
settings.pitchSpeed = 0.3
5454
settings.overrides = {} // additional settings/attempts to modify the entity state can be used here. not sure how to make this elegant yet.
5555

56-
let ectx
56+
57+
/**
58+
* @type {EPhysicsCtx<PlayerState> | undefined}
59+
*/
60+
let ectx;
5761

5862
const lastSent = {
5963
x: 0,
@@ -95,16 +99,49 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
9599

96100
function tickPhysics (now) {
97101
if (bot.blockAt(bot.entity.position) == null) return // check if chunk is unloaded
102+
103+
// Initialize if null (e.g., right after spawn before first packet)
104+
if (lastSentYaw === null) lastSentYaw = bot.entity.yaw
105+
if (lastSentPitch === null) lastSentPitch = bot.entity.pitch
106+
107+
if (shouldUsePhysics) {
108+
// 1. Advance the interpolated yaw/pitch first
109+
const dYaw = deltaYaw(bot.entity.yaw, lastSentYaw)
110+
const dPitch = bot.entity.pitch - lastSentPitch
111+
112+
const maxDeltaYaw = PHYSICS_TIMESTEP * bot.physicsSettings.yawSpeed
113+
const maxDeltaPitch = PHYSICS_TIMESTEP * bot.physicsSettings.pitchSpeed
114+
115+
lastSentYaw += math.clamp(-maxDeltaYaw, dYaw, maxDeltaYaw)
116+
lastSentPitch += math.clamp(-maxDeltaPitch, dPitch, maxDeltaPitch)
117+
}
118+
98119
if (bot.physicsEnabled && shouldUsePhysics) {
99120
ectx ??= EPhysicsCtx.FROM_BOT(physics, bot, settings)
100121
ectx.state.update(bot)
122+
123+
// 2. Sync physics engine to the interpolated look to satisfy anticheats
124+
ectx.state.yaw = lastSentYaw
125+
ectx.state.pitch = lastSentPitch
126+
101127
Object.assign(ectx, settings.overrides)
102128

129+
// 3. Save target yaw/pitch before applying physics
130+
const targetYaw = bot.entity.yaw
131+
const targetPitch = bot.entity.pitch
132+
103133
physics.simulate(ectx, world).apply(bot)
104134

135+
// console.log(ectx.state.effects, bot.entity.effects)
136+
137+
// 4. Restore target yaw/pitch so the bot doesn't forget where it's trying to look
138+
bot.entity.yaw = targetYaw
139+
bot.entity.pitch = targetPitch
140+
105141
bot.emit('physicsTick')
106142
bot.emit('physicTick') // Deprecated, only exists to support old plugins. May be removed in the future
107143
}
144+
108145
if (shouldUsePhysics) {
109146
updatePosition(now)
110147
}
@@ -184,15 +221,7 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
184221
// Only send updates for 20 ticks after death
185222
if (isEntityRemoved()) return
186223

187-
// Increment the yaw in baby steps so that notchian clients (not the server) can keep up.
188-
const dYaw = deltaYaw(bot.entity.yaw, lastSentYaw)
189-
const dPitch = bot.entity.pitch - (lastSentPitch || 0)
190-
191-
// Vanilla doesn't clamp yaw, so we don't want to do it either
192-
const maxDeltaYaw = PHYSICS_TIMESTEP * bot.physicsSettings.yawSpeed
193-
const maxDeltaPitch = PHYSICS_TIMESTEP * bot.physicsSettings.pitchSpeed
194-
lastSentYaw += math.clamp(-maxDeltaYaw, dYaw, maxDeltaYaw)
195-
lastSentPitch += math.clamp(-maxDeltaPitch, dPitch, maxDeltaPitch)
224+
// Interpolation logic has been moved to tickPhysics()
196225

197226
const yaw = Math.fround(conv.toNotchianYaw(lastSentYaw))
198227
const pitch = Math.fround(conv.toNotchianPitch(lastSentPitch))
@@ -257,11 +286,6 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
257286
lastUpdated.pitch = pitch
258287
}
259288

260-
if (lookUpdated) {
261-
lastUpdated.yaw = yaw
262-
lastUpdated.pitch = pitch
263-
}
264-
265289
lastSent.onGround = bot.entity.onGround // onGround is always set
266290
bot.physicsEngineCtx = ectx
267291
}
@@ -604,4 +628,4 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
604628
}
605629
})
606630
bot.on('end', cleanup)
607-
}
631+
}

0 commit comments

Comments
 (0)