UNPKG

@nxg-org/mineflayer-tracker

Version:

Provides functionality for more accurate entity and projectile tracking.

185 lines (184 loc) 7.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromNotchianPitchByte = exports.fromNotchianYawByte = exports.toNotchianPitch = exports.toNotchianYaw = void 0; exports.euclideanMod = euclideanMod; exports.toRadians = toRadians; exports.toDegrees = toDegrees; exports.fromNotchianYaw = fromNotchianYaw; exports.fromNotchianPitch = fromNotchianPitch; exports.fromNotchVelocity = fromNotchVelocity; exports.pointToYawAndPitch = pointToYawAndPitch; exports.dirToYawAndPitch = dirToYawAndPitch; exports.getTargetDistance = getTargetDistance; exports.getTargetYaw = getTargetYaw; exports.getPremonition = getPremonition; exports.degreesToRadians = degreesToRadians; exports.radiansToDegrees = radiansToDegrees; exports.getVox = getVox; exports.getVoy = getVoy; exports.getVo = getVo; exports.getGrades = getGrades; exports.vectorMagnitude = vectorMagnitude; exports.VoToVox = VoToVox; exports.yawPitchAndSpeedToDir = yawPitchAndSpeedToDir; exports.notchianVel = notchianVel; exports.applyVec3Gravity = applyVec3Gravity; exports.movingTowards = movingTowards; exports.movingAt = movingAt; exports.lookingAt = lookingAt; exports.lookingAtXZ = lookingAtXZ; exports.lookingAtFromRay = lookingAtFromRay; exports.lookingAtFromSegment = lookingAtFromSegment; const mineflayer_util_plugin_1 = require("@nxg-org/mineflayer-util-plugin"); const vec3_1 = require("vec3"); const { getEntityAABB } = mineflayer_util_plugin_1.AABBUtils; const PI = Math.PI; const PI_2 = Math.PI * 2; const TO_RAD = PI / 180; const TO_DEG = 1 / TO_RAD; const FROM_NOTCH_BYTE = 360 / 256; // From wiki.vg: Velocity is believed to be in units of 1/8000 of a block per server tick (50ms) const FROM_NOTCH_VEL = 1 / 8000; const toNotchianYaw = (yaw) => toDegrees(PI - yaw); exports.toNotchianYaw = toNotchianYaw; const toNotchianPitch = (pitch) => toDegrees(-pitch); exports.toNotchianPitch = toNotchianPitch; const fromNotchianYawByte = (yaw) => fromNotchianYaw(yaw * FROM_NOTCH_BYTE); exports.fromNotchianYawByte = fromNotchianYawByte; const fromNotchianPitchByte = (pitch) => fromNotchianPitch(pitch * FROM_NOTCH_BYTE); exports.fromNotchianPitchByte = fromNotchianPitchByte; function euclideanMod(numerator, denominator) { const result = numerator % denominator; return result < 0 ? result + denominator : result; } function toRadians(degrees) { return TO_RAD * degrees; } function toDegrees(radians) { return TO_DEG * radians; } function fromNotchianYaw(yaw) { return euclideanMod(PI - toRadians(yaw), PI_2); } function fromNotchianPitch(pitch) { return euclideanMod(toRadians(-pitch) + PI, PI_2) - PI; } function fromNotchVelocity(vel) { return new vec3_1.Vec3(vel.x * FROM_NOTCH_VEL, vel.y * FROM_NOTCH_VEL, vel.z * FROM_NOTCH_VEL); } function pointToYawAndPitch(org, point) { const delta = point.minus(org); return dirToYawAndPitch(delta); } function dirToYawAndPitch(dir) { const yaw = Math.atan2(dir.x, dir.z) + Math.PI; const groundDistance = Math.sqrt(dir.x * dir.x + dir.z * dir.z); const pitch = Math.atan2(dir.y, groundDistance); return { yaw: yaw, pitch: pitch }; } function getTargetDistance(origin, destination) { const xDistance = Math.pow(origin.x - destination.x, 2); const zDistance = Math.pow(origin.z - destination.z, 2); const hDistance = Math.sqrt(xDistance + zDistance); const yDistance = destination.y - origin.y; const distance = Math.sqrt(Math.pow(yDistance, 2) + xDistance + zDistance); return { distance, hDistance, yDistance, }; } function getTargetYaw(origin, destination) { const xDistance = destination.x - origin.x; const zDistance = destination.z - origin.z; const yaw = Math.atan2(xDistance, zDistance) + Math.PI; return yaw; } function getPremonition(startPosition, targetPosition, speed, totalTicks) { totalTicks = totalTicks + Math.ceil(totalTicks / 10); const newTarget = targetPosition; for (let i = 0; i < totalTicks; i++) { newTarget.add(speed); } const distances = getTargetDistance(startPosition, newTarget); return { distances, newTarget, }; } function degreesToRadians(degrees) { const pi = Math.PI; return degrees * (pi / 180); } function radiansToDegrees(radians) { const pi = Math.PI; return radians * (180 / pi); } function getVox(Vo, Alfa, Resistance = 0) { return Vo * Math.cos(Alfa) - Resistance; } function getVoy(Vo, Alfa, Resistance = 0) { return Vo * Math.sin(Alfa) - Resistance; } function getVo(Vox, Voy, G) { return Math.sqrt(Math.pow(Vox, 2) + Math.pow(Voy - G, 2)); // New Total Velocity - Gravity } function getGrades(Vo, Voy, Gravity) { return radiansToDegrees(Math.asin((Voy - Gravity) / Vo)); } function vectorMagnitude(vec) { return Math.sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); } function VoToVox(vec, mag) { return mag ? Math.sqrt(mag * mag - vec.y * vec.y) : Math.sqrt(Math.pow(vectorMagnitude(vec), 2) - vec.y * vec.y); } //Scuffed. function yawPitchAndSpeedToDir(yaw, pitch, speed) { return new vec3_1.Vec3(-Math.sin(yaw) * Math.cos(pitch), Math.sin(pitch), -Math.cos(yaw) * Math.cos(pitch)).scale(speed); } // TODO: make it not throw NaN. function notchianVel(vec, Vo, Vox) { if (Vo && Vox) { return { Vo, Vox, vel: new vec3_1.Vec3(vec.x * (Vox / Vo), vec.y, vec.z * (Vox / Vo)) }; } else if (Vo) { const Vox = VoToVox(vec, Vo); return { Vo, Vox, vel: new vec3_1.Vec3(vec.x * (Vox / Vo), vec.y, vec.z * (Vox / Vo)) }; } else { // console.log(vec) const Vo = vectorMagnitude(vec); const Vox = VoToVox(vec); // console.log(Math.pow(Vo, 2), vec.y * vec.y) return { Vo, Vox, vel: new vec3_1.Vec3(vec.x * (Vox / Vo), vec.y, vec.z * (Vox / Vo)) }; } } function applyVec3Gravity(currentVel, gravity) { return currentVel.plus(gravity); } function movingTowards(origin, destination, velocity) { return origin.distanceTo(destination) < origin.plus(velocity).distanceTo(destination); } function movingAt(origin, destination, velocity, maxOffset) { return Math.abs(dirToYawAndPitch(velocity.normalize()).yaw - getTargetYaw(origin, destination)) < maxOffset; } function lookingAt(origin, target, maxDistance) { var _a; const dir = yawPitchAndSpeedToDir(origin.yaw, origin.pitch, 1); return lookingAtFromRay(origin.position.offset(0, origin.height, 0), mineflayer_util_plugin_1.AABBUtils.getEntityAABBRaw({ position: target.position, height: target.height, width: (_a = target.width) !== null && _a !== void 0 ? _a : 0.6 }), dir, maxDistance); } function lookingAtXZ(origin, target, maxOffset) { return Math.abs(origin.yaw - getTargetYaw(origin.position, target.position)) < maxOffset; } function lookingAtFromRay(origin, target, dir, maxDistance) { if (!maxDistance) { return !!target.intersectsRay(origin, dir); } else { const res = target.intersectsRay(origin, dir); return res ? origin.distanceTo(res) < maxDistance : false; } } function lookingAtFromSegment(origin, target, endpoint) { return target.intersectsSegment(origin, endpoint); }