Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/physics.js
1467 views
1
const { Vec3 } = require('vec3')
2
const assert = require('assert')
3
const math = require('../math')
4
const conv = require('../conversions')
5
const { performance } = require('perf_hooks')
6
const { createDoneTask, createTask } = require('../promise_utils')
7
8
const { Physics, PlayerState } = require('prismarine-physics')
9
10
module.exports = inject
11
12
const PI = Math.PI
13
const PI_2 = Math.PI * 2
14
const PHYSICS_INTERVAL_MS = 50
15
const PHYSICS_TIMESTEP = PHYSICS_INTERVAL_MS / 1000 // 0.05
16
17
function inject (bot, { physicsEnabled, maxCatchupTicks }) {
18
const PHYSICS_CATCHUP_TICKS = maxCatchupTicks ?? 4
19
const world = { getBlock: (pos) => { return bot.blockAt(pos, false) } }
20
const physics = Physics(bot.registry, world)
21
22
const positionUpdateSentEveryTick = bot.supportFeature('positionUpdateSentEveryTick')
23
24
bot.jumpQueued = false
25
bot.jumpTicks = 0 // autojump cooldown
26
27
const controlState = {
28
forward: false,
29
back: false,
30
left: false,
31
right: false,
32
jump: false,
33
sprint: false,
34
sneak: false
35
}
36
let lastSentYaw = null
37
let lastSentPitch = null
38
let doPhysicsTimer = null
39
let lastPhysicsFrameTime = null
40
let shouldUsePhysics = false
41
bot.physicsEnabled = physicsEnabled ?? true
42
let deadTicks = 21
43
44
const lastSent = {
45
x: 0,
46
y: 0,
47
z: 0,
48
yaw: 0,
49
pitch: 0,
50
onGround: false,
51
time: 0,
52
flags: { onGround: false, hasHorizontalCollision: false }
53
}
54
55
// This function should be executed each tick (every 0.05 seconds)
56
// How it works: https://gafferongames.com/post/fix_your_timestep/
57
58
// WARNING: THIS IS NOT ACCURATE ON WINDOWS (15.6 Timer Resolution)
59
// use WSL or switch to Linux
60
// see: https://discord.com/channels/413438066984747026/519952494768685086/901948718255833158
61
let timeAccumulator = 0
62
let catchupTicks = 0
63
function doPhysics () {
64
const now = performance.now()
65
const deltaSeconds = (now - lastPhysicsFrameTime) / 1000
66
lastPhysicsFrameTime = now
67
68
timeAccumulator += deltaSeconds
69
catchupTicks = 0
70
while (timeAccumulator >= PHYSICS_TIMESTEP) {
71
tickPhysics(now)
72
timeAccumulator -= PHYSICS_TIMESTEP
73
catchupTicks++
74
if (catchupTicks >= PHYSICS_CATCHUP_TICKS) break
75
}
76
}
77
78
function tickPhysics (now) {
79
if (bot.blockAt(bot.entity.position) == null) return // check if chunk is unloaded
80
if (bot.physicsEnabled && shouldUsePhysics) {
81
physics.simulatePlayer(new PlayerState(bot, controlState), world).apply(bot)
82
bot.emit('physicsTick')
83
bot.emit('physicTick') // Deprecated, only exists to support old plugins. May be removed in the future
84
}
85
if (shouldUsePhysics) {
86
updatePosition(now)
87
}
88
}
89
90
// remove this when 'physicTick' is removed
91
bot.on('newListener', (name) => {
92
if (name === 'physicTick') console.warn('Mineflayer detected that you are using a deprecated event (physicTick)! Please use this event (physicsTick) instead.')
93
})
94
95
function cleanup () {
96
clearInterval(doPhysicsTimer)
97
doPhysicsTimer = null
98
}
99
100
function sendPacketPosition (position, onGround) {
101
// sends data, no logic
102
const oldPos = new Vec3(lastSent.x, lastSent.y, lastSent.z)
103
lastSent.x = position.x
104
lastSent.y = position.y
105
lastSent.z = position.z
106
lastSent.onGround = onGround
107
lastSent.flags = { onGround, hasHorizontalCollision: undefined } // 1.21.3+
108
bot._client.write('position', lastSent)
109
bot.emit('move', oldPos)
110
}
111
112
function sendPacketLook (yaw, pitch, onGround) {
113
// sends data, no logic
114
const oldPos = new Vec3(lastSent.x, lastSent.y, lastSent.z)
115
lastSent.yaw = yaw
116
lastSent.pitch = pitch
117
lastSent.onGround = onGround
118
lastSent.flags = { onGround, hasHorizontalCollision: undefined } // 1.21.3+
119
bot._client.write('look', lastSent)
120
bot.emit('move', oldPos)
121
}
122
123
function sendPacketPositionAndLook (position, yaw, pitch, onGround) {
124
// sends data, no logic
125
const oldPos = new Vec3(lastSent.x, lastSent.y, lastSent.z)
126
lastSent.x = position.x
127
lastSent.y = position.y
128
lastSent.z = position.z
129
lastSent.yaw = yaw
130
lastSent.pitch = pitch
131
lastSent.onGround = onGround
132
lastSent.flags = { onGround, hasHorizontalCollision: undefined } // 1.21.3+
133
bot._client.write('position_look', lastSent)
134
bot.emit('move', oldPos)
135
}
136
137
function deltaYaw (yaw1, yaw2) {
138
let dYaw = (yaw1 - yaw2) % PI_2
139
if (dYaw < -PI) dYaw += PI_2
140
else if (dYaw > PI) dYaw -= PI_2
141
142
return dYaw
143
}
144
145
// returns false if bot should send position packets
146
function isEntityRemoved () {
147
if (bot.isAlive === true) deadTicks = 0
148
if (bot.isAlive === false && deadTicks <= 20) deadTicks++
149
if (deadTicks >= 20) return true
150
return false
151
}
152
153
function updatePosition (now) {
154
// Only send updates for 20 ticks after death
155
if (isEntityRemoved()) return
156
157
// Increment the yaw in baby steps so that notchian clients (not the server) can keep up.
158
const dYaw = deltaYaw(bot.entity.yaw, lastSentYaw)
159
const dPitch = bot.entity.pitch - (lastSentPitch || 0)
160
161
// Vanilla doesn't clamp yaw, so we don't want to do it either
162
const maxDeltaYaw = PHYSICS_TIMESTEP * physics.yawSpeed
163
const maxDeltaPitch = PHYSICS_TIMESTEP * physics.pitchSpeed
164
lastSentYaw += math.clamp(-maxDeltaYaw, dYaw, maxDeltaYaw)
165
lastSentPitch += math.clamp(-maxDeltaPitch, dPitch, maxDeltaPitch)
166
167
const yaw = Math.fround(conv.toNotchianYaw(lastSentYaw))
168
const pitch = Math.fround(conv.toNotchianPitch(lastSentPitch))
169
const position = bot.entity.position
170
const onGround = bot.entity.onGround
171
172
// Only send a position update if necessary, select the appropriate packet
173
const positionUpdated = lastSent.x !== position.x || lastSent.y !== position.y || lastSent.z !== position.z ||
174
// Send a position update every second, even if no other update was made
175
// This function rounds to the nearest 50ms (or PHYSICS_INTERVAL_MS) and checks if a second has passed.
176
(Math.round((now - lastSent.time) / PHYSICS_INTERVAL_MS) * PHYSICS_INTERVAL_MS) >= 1000
177
const lookUpdated = lastSent.yaw !== yaw || lastSent.pitch !== pitch
178
179
if (positionUpdated && lookUpdated) {
180
sendPacketPositionAndLook(position, yaw, pitch, onGround)
181
lastSent.time = now // only reset if positionUpdated is true
182
} else if (positionUpdated) {
183
sendPacketPosition(position, onGround)
184
lastSent.time = now // only reset if positionUpdated is true
185
} else if (lookUpdated) {
186
sendPacketLook(yaw, pitch, onGround)
187
} else if (positionUpdateSentEveryTick || onGround !== lastSent.onGround) {
188
// For versions < 1.12, one player packet should be sent every tick
189
// for the server to update health correctly
190
// For versions >= 1.12, onGround !== lastSent.onGround should be used, but it doesn't ever trigger outside of login
191
bot._client.write('flying', {
192
onGround: bot.entity.onGround,
193
flags: { onGround: bot.entity.onGround, hasHorizontalCollision: undefined } // 1.21.3+
194
})
195
}
196
197
lastSent.onGround = bot.entity.onGround // onGround is always set
198
}
199
200
bot.physics = physics
201
202
function getEffectLevel (mcData, effectName, effects) {
203
const effectDescriptor = mcData.effectsByName[effectName]
204
if (!effectDescriptor) {
205
return 0
206
}
207
const effectInfo = effects[effectDescriptor.id]
208
if (!effectInfo) {
209
return 0
210
}
211
return effectInfo.amplifier + 1
212
}
213
214
bot.elytraFly = async () => {
215
if (bot.entity.elytraFlying) {
216
throw new Error('Already elytra flying')
217
} else if (bot.entity.onGround) {
218
throw new Error('Unable to fly from ground')
219
} else if (bot.entity.isInWater) {
220
throw new Error('Unable to elytra fly while in water')
221
}
222
223
const mcData = require('minecraft-data')(bot.version)
224
if (getEffectLevel(mcData, 'Levitation', bot.entity.effects) > 0) {
225
throw new Error('Unable to elytra fly with levitation effect')
226
}
227
228
const torsoSlot = bot.getEquipmentDestSlot('torso')
229
const item = bot.inventory.slots[torsoSlot]
230
if (item == null || item.name !== 'elytra') {
231
throw new Error('Elytra must be equip to start flying')
232
}
233
bot._client.write('entity_action', {
234
entityId: bot.entity.id,
235
actionId: 8,
236
jumpBoost: 0
237
})
238
}
239
240
bot.setControlState = (control, state) => {
241
assert.ok(control in controlState, `invalid control: ${control}`)
242
assert.ok(typeof state === 'boolean', `invalid state: ${state}`)
243
if (controlState[control] === state) return
244
controlState[control] = state
245
if (control === 'jump' && state) {
246
bot.jumpQueued = true
247
} else if (control === 'sprint') {
248
bot._client.write('entity_action', {
249
entityId: bot.entity.id,
250
actionId: state ? 3 : 4,
251
jumpBoost: 0
252
})
253
} else if (control === 'sneak') {
254
bot._client.write('entity_action', {
255
entityId: bot.entity.id,
256
actionId: state ? 0 : 1,
257
jumpBoost: 0
258
})
259
}
260
}
261
262
bot.getControlState = (control) => {
263
assert.ok(control in controlState, `invalid control: ${control}`)
264
return controlState[control]
265
}
266
267
bot.clearControlStates = () => {
268
for (const control in controlState) {
269
bot.setControlState(control, false)
270
}
271
}
272
273
bot.controlState = {}
274
275
for (const control of Object.keys(controlState)) {
276
Object.defineProperty(bot.controlState, control, {
277
get () {
278
return controlState[control]
279
},
280
set (state) {
281
bot.setControlState(control, state)
282
return state
283
}
284
})
285
}
286
287
let lookingTask = createDoneTask()
288
289
bot.on('move', () => {
290
if (!lookingTask.done && Math.abs(deltaYaw(bot.entity.yaw, lastSentYaw)) < 0.001) {
291
lookingTask.finish()
292
}
293
})
294
295
bot._client.on('explosion', explosion => {
296
// TODO: emit an explosion event with more info
297
if (bot.physicsEnabled && bot.game.gameMode !== 'creative') {
298
if (explosion.playerKnockback) { // 1.21.3+
299
bot.entity.velocity.add(explosion.playerMotionX, explosion.playerMotionY, explosion.playerMotionZ)
300
}
301
if ('playerMotionX' in explosion) {
302
bot.entity.velocity.x += explosion.playerMotionX
303
bot.entity.velocity.y += explosion.playerMotionY
304
bot.entity.velocity.z += explosion.playerMotionZ
305
}
306
}
307
})
308
309
bot.look = async (yaw, pitch, force) => {
310
if (!lookingTask.done) {
311
lookingTask.finish() // finish the previous one
312
}
313
lookingTask = createTask()
314
315
// this is done to bypass certain anticheat checks that detect the player's sensitivity
316
// by calculating the gcd of how much they move the mouse each tick
317
const sensitivity = conv.fromNotchianPitch(0.15) // this is equal to 100% sensitivity in vanilla
318
const yawChange = Math.round((yaw - bot.entity.yaw) / sensitivity) * sensitivity
319
const pitchChange = Math.round((pitch - bot.entity.pitch) / sensitivity) * sensitivity
320
321
if (yawChange === 0 && pitchChange === 0) {
322
return
323
}
324
325
bot.entity.yaw += yawChange
326
bot.entity.pitch += pitchChange
327
328
if (force) {
329
lastSentYaw = yaw
330
lastSentPitch = pitch
331
return
332
}
333
334
await lookingTask.promise
335
}
336
337
bot.lookAt = async (point, force) => {
338
const delta = point.minus(bot.entity.position.offset(0, bot.entity.eyeHeight, 0))
339
const yaw = Math.atan2(-delta.x, -delta.z)
340
const groundDistance = Math.sqrt(delta.x * delta.x + delta.z * delta.z)
341
const pitch = Math.atan2(delta.y, groundDistance)
342
await bot.look(yaw, pitch, force)
343
}
344
345
// 1.21.3+
346
bot._client.on('player_rotation', (packet) => {
347
bot.entity.yaw = conv.fromNotchianYaw(packet.yaw)
348
bot.entity.pitch = conv.fromNotchianPitch(packet.pitch)
349
})
350
351
// player position and look (clientbound)
352
bot._client.on('position', (packet) => {
353
// Is this necessary? Feels like it might wrongly overwrite hitbox size sometimes
354
// e.g. when crouching/crawling/swimming. Can someone confirm?
355
bot.entity.height = 1.8
356
357
const vel = bot.entity.velocity
358
const pos = bot.entity.position
359
let newYaw, newPitch
360
361
// Note: 1.20.5+ uses a bitflags object, older versions use a bitmask number
362
if (typeof packet.flags === 'object') {
363
// Modern path with bitflags object
364
// Velocity is only set to 0 if the flag is not set, otherwise keep current velocity
365
vel.set(
366
packet.flags.x ? vel.x : 0,
367
packet.flags.y ? vel.y : 0,
368
packet.flags.z ? vel.z : 0
369
)
370
// If flag is set, then the corresponding value is relative, else it is absolute
371
pos.set(
372
packet.flags.x ? (pos.x + packet.x) : packet.x,
373
packet.flags.y ? (pos.y + packet.y) : packet.y,
374
packet.flags.z ? (pos.z + packet.z) : packet.z
375
)
376
newYaw = (packet.flags.yaw ? conv.toNotchianYaw(bot.entity.yaw) : 0) + packet.yaw
377
newPitch = (packet.flags.pitch ? conv.toNotchianPitch(bot.entity.pitch) : 0) + packet.pitch
378
} else {
379
// Legacy path with bitmask number
380
// Velocity is only set to 0 if the flag is not set, otherwise keep current velocity
381
vel.set(
382
packet.flags & 1 ? vel.x : 0,
383
packet.flags & 2 ? vel.y : 0,
384
packet.flags & 4 ? vel.z : 0
385
)
386
// If flag is set, then the corresponding value is relative, else it is absolute
387
pos.set(
388
packet.flags & 1 ? (pos.x + packet.x) : packet.x,
389
packet.flags & 2 ? (pos.y + packet.y) : packet.y,
390
packet.flags & 4 ? (pos.z + packet.z) : packet.z
391
)
392
newYaw = (packet.flags & 8 ? conv.toNotchianYaw(bot.entity.yaw) : 0) + packet.yaw
393
newPitch = (packet.flags & 16 ? conv.toNotchianPitch(bot.entity.pitch) : 0) + packet.pitch
394
}
395
396
bot.entity.yaw = conv.fromNotchianYaw(newYaw)
397
bot.entity.pitch = conv.fromNotchianPitch(newPitch)
398
bot.entity.onGround = false
399
400
if (bot.supportFeature('teleportUsesOwnPacket')) {
401
bot._client.write('teleport_confirm', { teleportId: packet.teleportId })
402
}
403
sendPacketPositionAndLook(pos, newYaw, newPitch, bot.entity.onGround)
404
405
shouldUsePhysics = true
406
bot.jumpTicks = 0
407
lastSentYaw = bot.entity.yaw
408
lastSentPitch = bot.entity.pitch
409
410
bot.emit('forcedMove')
411
})
412
413
bot.waitForTicks = async function (ticks) {
414
if (ticks <= 0) return
415
await new Promise(resolve => {
416
const tickListener = () => {
417
ticks--
418
if (ticks === 0) {
419
bot.removeListener('physicsTick', tickListener)
420
resolve()
421
}
422
}
423
424
bot.on('physicsTick', tickListener)
425
})
426
}
427
428
bot.on('mount', () => { shouldUsePhysics = false })
429
bot.on('respawn', () => { shouldUsePhysics = false })
430
bot.on('login', () => {
431
shouldUsePhysics = false
432
if (doPhysicsTimer === null) {
433
lastPhysicsFrameTime = performance.now()
434
doPhysicsTimer = setInterval(doPhysics, PHYSICS_INTERVAL_MS)
435
}
436
})
437
bot.on('end', cleanup)
438
}
439
440