Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add entityAtCursor function #2077

Merged
merged 11 commits into from
Aug 7, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/plugins/ray_trace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Vec3 } = require('vec3')
const { RaycastIterator } = require('prismarine-world').iterators

module.exports = (bot) => {
function getViewDirection (pitch, yaw) {
Expand All @@ -23,4 +24,29 @@ module.exports = (bot) => {

return bot.world.raycast(eyePosition, viewDirection, maxDistance, matcher)
}

bot.entityAtCursor = (maxDistance = 3.5) => {
const entities = Object.values(bot.entities)
.filter(entity => entity.type !== 'object' && entity.username !== bot.username && entity.position.distanceSquared(bot.entity.position) <= maxDistance ** 2)
.sort((entity1, entity2) => entity1.position.distanceSquared(bot.entity.position) - entity2.position.distanceSquared(bot.entity.position))

const dir = new Vec3(-Math.sin(bot.entity.yaw) * Math.cos(bot.entity.pitch), Math.sin(bot.entity.pitch), -Math.cos(bot.entity.yaw) * Math.cos(bot.entity.pitch))
const iterator = new RaycastIterator(bot.entity.position.offset(0, bot.entity.height, 0), dir.normalize(), maxDistance)

let pos = iterator.next()
while (pos) {
for (let i = 0; i < entities.length; i++) {
Karang marked this conversation as resolved.
Show resolved Hide resolved
Chitasa marked this conversation as resolved.
Show resolved Hide resolved
const entity = entities[i]
const w = entity.width / 2

const shapes = [[-w, 0, -w, w, entity.height + (entity.type === 'player' ? 0.18 : 0), w]]

if (iterator.intersect(shapes, entity.position)) {
return entities[i]
}
}
pos = iterator.next()
}
return null
}
}