-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathphysics.js
More file actions
313 lines (266 loc) · 9.9 KB
/
physics.js
File metadata and controls
313 lines (266 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
const { Vec3 } = require('vec3')
const assert = require('assert')
const math = require('../math')
const conv = require('../conversions')
const { performance } = require('perf_hooks')
const { createDoneTask, createTask } = require('../promise_utils')
const { Physics, PlayerState } = require('prismarine-physics')
module.exports = inject
const PI = Math.PI
const PI_2 = Math.PI * 2
const PHYSICS_INTERVAL_MS = 50
const PHYSICS_TIMESTEP = PHYSICS_INTERVAL_MS / 1000
function inject (bot, { physicsEnabled }) {
const world = { getBlock: (pos) => { return bot.blockAt(pos, false) } }
const physics = Physics(bot.registry, world)
const positionUpdateSentEveryTick = bot.supportFeature('positionUpdateSentEveryTick')
bot.jumpQueued = false
bot.jumpTicks = 0 // autojump cooldown
const controlState = {
forward: false,
back: false,
left: false,
right: false,
jump: false,
sprint: false,
sneak: false
}
let lastSentYaw = null
let lastSentPitch = null
let doPhysicsTimer = null
let lastPhysicsFrameTime = null
let shouldUsePhysics = false
bot.physicsEnabled = physicsEnabled ?? true
const lastSent = {
x: 0,
y: 0,
z: 0,
yaw: 0,
pitch: 0,
onGround: false,
time: 0
}
// This function should be executed each tick (every 0.05 seconds)
// How it works: https://gafferongames.com/post/fix_your_timestep/
let timeAccumulator = 0
function doPhysics () {
const now = performance.now()
const deltaSeconds = (now - lastPhysicsFrameTime) / 1000
lastPhysicsFrameTime = now
timeAccumulator += deltaSeconds
while (timeAccumulator >= PHYSICS_TIMESTEP) {
if (bot.physicsEnabled && shouldUsePhysics) {
physics.simulatePlayer(new PlayerState(bot, controlState), world).apply(bot)
bot.emit('physicsTick')
bot.emit('physicTick') // Deprecated, only exists to support old plugins. May be removed in the future
}
updatePosition(PHYSICS_TIMESTEP)
timeAccumulator -= PHYSICS_TIMESTEP
}
}
function cleanup () {
clearInterval(doPhysicsTimer)
doPhysicsTimer = null
}
function sendPacketPosition (position, onGround) {
// sends data, no logic
const oldPos = new Vec3(lastSent.x, lastSent.y, lastSent.z)
lastSent.x = position.x
lastSent.y = position.y
lastSent.z = position.z
lastSent.onGround = onGround
bot._client.write('position', lastSent)
bot.emit('move', oldPos)
}
function sendPacketLook (yaw, pitch, onGround) {
// sends data, no logic
const oldPos = new Vec3(lastSent.x, lastSent.y, lastSent.z)
lastSent.yaw = yaw
lastSent.pitch = pitch
lastSent.onGround = onGround
bot._client.write('look', lastSent)
bot.emit('move', oldPos)
}
function sendPacketPositionAndLook (position, yaw, pitch, onGround) {
// sends data, no logic
const oldPos = new Vec3(lastSent.x, lastSent.y, lastSent.z)
lastSent.x = position.x
lastSent.y = position.y
lastSent.z = position.z
lastSent.yaw = yaw
lastSent.pitch = pitch
lastSent.onGround = onGround
bot._client.write('position_look', lastSent)
bot.emit('move', oldPos)
}
function deltaYaw (yaw1, yaw2) {
let dYaw = (yaw1 - yaw2) % PI_2
if (dYaw < -PI) dYaw += PI_2
else if (dYaw > PI) dYaw -= PI_2
return dYaw
}
function updatePosition (dt) {
// If you're dead, you're probably on the ground though ...
if (!bot.isAlive) bot.entity.onGround = true
// Increment the yaw in baby steps so that notchian clients (not the server) can keep up.
const dYaw = deltaYaw(bot.entity.yaw, lastSentYaw)
const dPitch = bot.entity.pitch - (lastSentPitch || 0)
// Vanilla doesn't clamp yaw, so we don't want to do it either
const maxDeltaYaw = dt * physics.yawSpeed
const maxDeltaPitch = dt * physics.pitchSpeed
lastSentYaw += math.clamp(-maxDeltaYaw, dYaw, maxDeltaYaw)
lastSentPitch += math.clamp(-maxDeltaPitch, dPitch, maxDeltaPitch)
const yaw = Math.fround(conv.toNotchianYaw(lastSentYaw))
const pitch = Math.fround(conv.toNotchianPitch(lastSentPitch))
const position = bot.entity.position
const onGround = bot.entity.onGround
// Only send a position update if necessary, select the appropriate packet
const positionUpdated = lastSent.x !== position.x || lastSent.y !== position.y || lastSent.z !== position.z
const lookUpdated = lastSent.yaw !== yaw || lastSent.pitch !== pitch
if (positionUpdated && lookUpdated && bot.isAlive) {
sendPacketPositionAndLook(position, yaw, pitch, onGround)
} else if (positionUpdated && bot.isAlive) {
sendPacketPosition(position, onGround)
} else if (lookUpdated && bot.isAlive) {
sendPacketLook(yaw, pitch, onGround)
} else if (performance.now() - lastSent.time >= 1000) {
// Send a position packet every second, even if no update was made
sendPacketPosition(position, onGround)
lastSent.time = performance.now()
} else if (positionUpdateSentEveryTick && bot.isAlive) {
// For versions < 1.12, one player packet should be sent every tick
// for the server to update health correctly
bot._client.write('flying', { onGround: bot.entity.onGround })
}
}
bot.physics = physics
bot.setControlState = (control, state) => {
assert.ok(control in controlState, `invalid control: ${control}`)
assert.ok(typeof state === 'boolean', `invalid state: ${state}`)
if (controlState[control] === state) return
controlState[control] = state
if (control === 'jump' && state) {
bot.jumpQueued = true
} else if (control === 'sprint') {
bot._client.write('entity_action', {
entityId: bot.entity.id,
actionId: state ? 3 : 4,
jumpBoost: 0
})
} else if (control === 'sneak') {
bot._client.write('entity_action', {
entityId: bot.entity.id,
actionId: state ? 0 : 1,
jumpBoost: 0
})
}
}
bot.getControlState = (control) => {
assert.ok(control in controlState, `invalid control: ${control}`)
return controlState[control]
}
bot.clearControlStates = () => {
for (const control in controlState) {
bot.setControlState(control, false)
}
}
bot.controlState = {}
for (const control of Object.keys(controlState)) {
Object.defineProperty(bot.controlState, control, {
get () {
return controlState[control]
},
set (state) {
bot.setControlState(control, state)
return state
}
})
}
let lookingTask = createDoneTask()
bot.on('move', () => {
if (!lookingTask.done && Math.abs(deltaYaw(bot.entity.yaw, lastSentYaw)) < 0.001) {
lookingTask.finish()
}
})
bot._client.on('explosion', explosion => {
// TODO: emit an explosion event with more info
if (bot.physicsEnabled && bot.game.gameMode !== 'creative') {
bot.entity.velocity.x += explosion.playerMotionX
bot.entity.velocity.y += explosion.playerMotionY
bot.entity.velocity.z += explosion.playerMotionZ
}
})
bot.look = async (yaw, pitch, force) => {
if (!lookingTask.done) {
lookingTask.finish() // finish the previous one
}
lookingTask = createTask()
// this is done to bypass certain anticheat checks that detect the player's sensitivity
// by calculating the gcd of how much they move the mouse each tick
const sensitivity = conv.fromNotchianPitch(0.15) // this is equal to 100% sensitivity in vanilla
const yawChange = Math.round((yaw - bot.entity.yaw) / sensitivity) * sensitivity
const pitchChange = Math.round((pitch - bot.entity.pitch) / sensitivity) * sensitivity
bot.entity.yaw += yawChange
bot.entity.pitch += pitchChange
if (force) {
lastSentYaw = yaw
lastSentPitch = pitch
return
}
await lookingTask.promise
}
bot.lookAt = async (point, force) => {
const delta = point.minus(bot.entity.position.offset(0, bot.entity.height, 0))
const yaw = Math.atan2(-delta.x, -delta.z)
const groundDistance = Math.sqrt(delta.x * delta.x + delta.z * delta.z)
const pitch = Math.atan2(delta.y, groundDistance)
await bot.look(yaw, pitch, force)
}
// player position and look (clientbound)
bot._client.on('position', (packet) => {
bot.entity.height = 1.62
bot.entity.velocity.set(0, 0, 0)
// If flag is set, then the corresponding value is relative, else it is absolute
const pos = bot.entity.position
pos.set(
packet.flags & 1 ? (pos.x + packet.x) : packet.x,
packet.flags & 2 ? (pos.y + packet.y) : packet.y,
packet.flags & 4 ? (pos.z + packet.z) : packet.z
)
const newYaw = (packet.flags & 8 ? conv.toNotchianYaw(bot.entity.yaw) : 0) + packet.yaw
const newPitch = (packet.flags & 16 ? conv.toNotchianPitch(bot.entity.pitch) : 0) + packet.pitch
bot.entity.yaw = conv.fromNotchianYaw(newYaw)
bot.entity.pitch = conv.fromNotchianPitch(newPitch)
bot.entity.onGround = false
if (bot.supportFeature('teleportUsesOwnPacket')) {
bot._client.write('teleport_confirm', { teleportId: packet.teleportId })
// Force send an extra packet to be like vanilla client
sendPacketPositionAndLook(pos, newYaw, newPitch, bot.entity.onGround)
}
sendPacketPositionAndLook(pos, newYaw, newPitch, bot.entity.onGround)
shouldUsePhysics = true
bot.entity.timeSinceOnGround = 0
lastSentYaw = bot.entity.yaw
if (doPhysicsTimer === null) {
lastPhysicsFrameTime = performance.now()
doPhysicsTimer = setInterval(doPhysics, PHYSICS_INTERVAL_MS)
}
bot.emit('forcedMove')
})
bot.waitForTicks = async function (ticks) {
if (ticks <= 0) return
await new Promise(resolve => {
const tickListener = () => {
ticks--
if (ticks === 0) {
bot.removeListener('physicsTick', tickListener)
resolve()
}
}
bot.on('physicsTick', tickListener)
})
}
bot.on('mount', () => { shouldUsePhysics = false })
bot.on('respawn', () => { shouldUsePhysics = false })
bot.on('end', cleanup)
}