UNPKG

@nxg-org/mineflayer-physics-util

Version:

Provides functionality for more accurate entity and projectile tracking.

169 lines (168 loc) 7.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CheapEnchantments = exports.CheapEffects = exports.DefaultPlayer = void 0; exports.makeSupportFeature = makeSupportFeature; exports.applyMdToNewEntity = applyMdToNewEntity; exports.isEntityUsingItem = isEntityUsingItem; exports.whichHandIsEntityUsing = whichHandIsEntityUsing; exports.whichHandIsEntityUsingBoolean = whichHandIsEntityUsingBoolean; exports.getStatusEffectNamesForVersion = getStatusEffectNamesForVersion; exports.getEnchantmentNamesForVersion = getEnchantmentNamesForVersion; exports.getBetweenRectangle = getBetweenRectangle; exports.getLookingVector = getLookingVector; const mineflayer_util_plugin_1 = require("@nxg-org/mineflayer-util-plugin"); const features_json_1 = __importDefault(require("../physics/info/features.json")); const vec3_1 = require("vec3"); function makeSupportFeature(mcData) { return (feature) => features_json_1.default.some(({ name, versions }) => name === feature && versions.includes(mcData.version.majorVersion)); } exports.DefaultPlayer = { displayName: "Player", height: 1.8, width: 0.6, type: "player", name: "player", id: -1 }; function applyMdToNewEntity(ctx, entityType = exports.DefaultPlayer, options = {}) { var _a, _b; //entityType.category // entityType.internalId const tmp = new ctx.entityConstructor(-1); tmp.displayName = entityType.displayName; tmp.height = (_a = entityType.height) !== null && _a !== void 0 ? _a : 0; tmp.width = (_b = entityType.width) !== null && _b !== void 0 ? _b : 0; tmp.type = entityType.type; tmp.name = entityType.name; Object.assign(tmp, options); return tmp; } var CheapEffects; (function (CheapEffects) { CheapEffects[CheapEffects["JUMP_BOOST"] = 0] = "JUMP_BOOST"; CheapEffects[CheapEffects["SPEED"] = 1] = "SPEED"; CheapEffects[CheapEffects["SLOWNESS"] = 2] = "SLOWNESS"; CheapEffects[CheapEffects["DOLPHINS_GRACE"] = 3] = "DOLPHINS_GRACE"; CheapEffects[CheapEffects["SLOW_FALLING"] = 4] = "SLOW_FALLING"; CheapEffects[CheapEffects["LEVITATION"] = 5] = "LEVITATION"; CheapEffects[CheapEffects["BLINDNESS"] = 6] = "BLINDNESS"; })(CheapEffects || (exports.CheapEffects = CheapEffects = {})); var CheapEnchantments; (function (CheapEnchantments) { CheapEnchantments[CheapEnchantments["DEPTH_STRIDER"] = 0] = "DEPTH_STRIDER"; CheapEnchantments[CheapEnchantments["SWIFT_SNEAK"] = 1] = "SWIFT_SNEAK"; CheapEnchantments[CheapEnchantments["SOUL_SPEED"] = 2] = "SOUL_SPEED"; })(CheapEnchantments || (exports.CheapEnchantments = CheapEnchantments = {})); function getMetadataIndex(supportFeature) { return supportFeature("itemUsageMetadata6") ? 6 : 8; } function isEntityUsingItem(entity, supportFeature) { return entity.metadata[getMetadataIndex(supportFeature)] > 1; } function whichHandIsEntityUsing(entity, supportFeature) { return entity.metadata[getMetadataIndex(supportFeature)] > 2 ? "off-hand" : "hand"; } function whichHandIsEntityUsingBoolean(entity, supportFeature) { return entity.metadata[getMetadataIndex(supportFeature)] > 2; } function getStatusEffectNamesForVersion(supportFeature) { if (supportFeature("effectNamesAreRegistryNames")) { // seems to not matter right now. return { jumpBoostEffectName: "JumpBoost", speedEffectName: "Speed", slownessEffectName: "Slowness", dolphinsGraceEffectName: "DolphinsGrace", slowFallingEffectName: "SlowFalling", levitationEffectName: "Levitation", blindnessEffectName: "Blindnesss" }; } else { return { jumpBoostEffectName: "JumpBoost", speedEffectName: "Speed", slownessEffectName: "Slowness", dolphinsGraceEffectName: "DolphinsGrace", slowFallingEffectName: "SlowFalling", levitationEffectName: "Levitation", blindnessEffectName: "Blindnesss" }; } } // lol. In case of expansion, yk. function getEnchantmentNamesForVersion(supportFeature) { return { depthStriderEnchantmentName: "depth_strider", swiftSneakEnchantmentName: "swift_sneak", soulSpeedEnchantmentName: "soul_speed" }; } function getBetweenRectangle(src, dest) { const outerAABB = new mineflayer_util_plugin_1.AABB(Math.min(src.minX, dest.minX), Math.min(src.minY, dest.minY), Math.min(src.minZ, dest.minZ), Math.max(src.maxX, dest.maxX), Math.max(src.maxY, dest.maxY), Math.max(src.maxZ, dest.maxZ)); //Math.max() only good for length, otherwise leave because we want good shit. const innerAABBWidth = outerAABB.maxX - outerAABB.minX - (src.maxX - src.minX) - (dest.maxX - dest.minX); const innerAABBLength = outerAABB.maxZ - outerAABB.minZ - (src.maxZ - src.minZ) - (dest.maxZ - dest.minZ); const innerAABBHeight = outerAABB.maxY - outerAABB.minY - (src.maxY - src.minY) - (dest.maxY - dest.minY); //hm... could make a new AABB representing inner here. const outerCenter = outerAABB.getCenter(); const wFlip = Math.sign(innerAABBWidth); const hFlip = Math.sign(innerAABBHeight); const lFlip = Math.sign(innerAABBLength); const innerAABB = new mineflayer_util_plugin_1.AABB(outerCenter.x - (wFlip * innerAABBWidth) / 2, outerCenter.y - (hFlip * innerAABBHeight) / 2, outerCenter.z - (lFlip * innerAABBLength) / 2, outerCenter.x + (wFlip * innerAABBWidth) / 2, outerCenter.y + (hFlip * innerAABBHeight) / 2, outerCenter.z + (lFlip * innerAABBLength) / 2); return innerAABB; } function getLookingVector(entity) { // given a yaw pitch, we need the looking vector // yaw is right handed rotation about y (up) starting from -z (north) // pitch is -90 looking down, 90 looking up, 0 looking at horizon // lets get its coordinate system. // let x' = -z (north) // let y' = -x (west) // let z' = y (up) // the non normalized looking vector in x', y', z' space is // x' is cos(yaw) // y' is sin(yaw) // z' is tan(pitch) // substituting back in x, y, z, we get the looking vector in the normal x, y, z space // -z = cos(yaw) => z = -cos(yaw) // -x = sin(yaw) => x = -sin(yaw) // y = tan(pitch) // normalizing the vectors, we divide each by |sqrt(x*x + y*y + z*z)| // x*x + z*z = sin^2 + cos^2 = 1 // so |sqrt(xx+yy+zz)| = |sqrt(1+tan^2(pitch))| // = |sqrt(1+sin^2(pitch)/cos^2(pitch))| // = |sqrt((cos^2+sin^2)/cos^2(pitch))| // = |sqrt(1/cos^2(pitch))| // = |+/- 1/cos(pitch)| // = 1/cos(pitch) since pitch in [-90, 90] // the looking vector is therefore // x = -sin(yaw) * cos(pitch) // y = tan(pitch) * cos(pitch) = sin(pitch) // z = -cos(yaw) * cos(pitch) const yaw = entity.yaw; const pitch = entity.pitch; const sinYaw = Math.sin(yaw); const cosYaw = Math.cos(yaw); const sinPitch = Math.sin(pitch); const cosPitch = Math.cos(pitch); const lookX = -sinYaw * cosPitch; const lookY = sinPitch; const lookZ = -cosYaw * cosPitch; const lookDir = new vec3_1.Vec3(lookX, lookY, lookZ); return { yaw, pitch, sinYaw, cosYaw, sinPitch, cosPitch, lookX, lookY, lookZ, lookDir }; }