From 6413f1d5bbf17e281db664902c75281e6d82fa3e Mon Sep 17 00:00:00 2001 From: PeenScreeker Date: Thu, 30 Nov 2023 00:07:58 -0500 Subject: [PATCH] WIP math.ts --- scripts/hud/cgaz.js | 31 ++++++++-------- scripts/util/{math.js => math.ts} | 60 ++++++++++--------------------- 2 files changed, 34 insertions(+), 57 deletions(-) rename scripts/util/{math.js => math.ts} (60%) diff --git a/scripts/hud/cgaz.js b/scripts/hud/cgaz.js index c292c771..a7201396 100644 --- a/scripts/hud/cgaz.js +++ b/scripts/hud/cgaz.js @@ -420,24 +420,24 @@ class Cgaz { } const velocity = MomentumPlayerAPI.GetVelocity(); - const speed = getSize(velocity); + const speed = getSize2D(velocity); const stopSpeed = Math.max(speed, MomentumMovementAPI.GetStopspeed()); const dropSpeed = Math.max(speed - stopSpeed * lastMoveData.friction * tickInterval, 0); const speedSquared = speed * speed; const dropSpeedSquared = dropSpeed * dropSpeed; - const velDir = getNormal(velocity, 0.001); + const velDir = getNormal2D(velocity, 0.001); const velAngle = Math.atan2(velocity.y, velocity.x); const wishDir = lastMoveData.wishdir; - const wishAngle = getSizeSquared(wishDir) > 0.001 ? Math.atan2(wishDir.y, wishDir.x) : 0; + const wishAngle = getSizeSquared2D(wishDir) > 0.001 ? Math.atan2(wishDir.y, wishDir.x) : 0; const viewAngle = (MomentumPlayerAPI.GetAngles().y * Math.PI) / 180; const viewDir = { x: Math.cos(viewAngle), y: Math.sin(viewAngle) }; - const forwardMove = Math.round(getDot(viewDir, wishDir)); - const rightMove = Math.round(getCross(viewDir, wishDir)); + const forwardMove = Math.round(getDot2D(viewDir, wishDir)); + const rightMove = Math.round(getCross2D(viewDir, wishDir)); const bIsFalling = lastMoveData.moveStatus === 0; const bHasAirControl = phyMode && floatEquals(wishAngle, viewAngle, 0.01) && bIsFalling; @@ -638,7 +638,7 @@ class Cgaz { // arrow if (this.primeArrowEnable) { - if (getSizeSquared(wishDir) > 0) { + if (getSizeSquared2D(wishDir) > 0) { let arrowAngle = wishAngle - Math.atan2( @@ -924,7 +924,7 @@ class Cgaz { break; case 2: // "target" zones only highlight when moving - if (getSize(MomentumPlayerAPI.GetVelocity()) > this.accelMinSpeed) { + if (getSize2D(MomentumPlayerAPI.GetVelocity()) > this.accelMinSpeed) { let stopPoint, direction; if (left - leftTarget <= 0 && right - leftTarget >= 0) { stopPoint = floatEquals(zone.rightPx, zone.leftPx, 1) @@ -977,13 +977,14 @@ class Cgaz { } static updatePrimeSight(viewDir, viewAngle, targetAngle, boundaryAngle, velAngle, wishDir, wishAngle) { - const cross = getCross(wishDir, viewDir); + const cross = getCross2D(wishDir, viewDir); const inputMode = - Math.round(getSize(wishDir)) * (1 << Math.round(2 * Math.pow(cross, 2))) + (Math.round(cross) > 0 ? 1 : 0); + Math.round(getSize2D(wishDir)) * (1 << Math.round(2 * Math.pow(cross, 2))) + + (Math.round(cross) > 0 ? 1 : 0); const angleOffset = remapAngle(velAngle - wishAngle); const targetOffset = remapAngle(velAngle - viewAngle); - const inputAngle = remapAngle(viewAngle - wishAngle) * getSizeSquared(wishDir); + const inputAngle = remapAngle(viewAngle - wishAngle) * getSizeSquared2D(wishDir); const velocity = MomentumPlayerAPI.GetVelocity(); const gainZonesMap = new Map(); @@ -1038,7 +1039,7 @@ class Cgaz { this.clearZones([this.primeFirstZoneLeft]); } - speedGain = this.findPrimeGain(this.primeFirstZoneLeft, velocity, rotateVector(viewDir, 0.25 * Math.PI)); + speedGain = this.findPrimeGain(this.primeFirstZoneLeft, velocity, rotateVector2D(viewDir, 0.25 * Math.PI)); gainZonesMap.set(this.primeFirstZoneLeft, speedGain); if (speedGain > gainMax) gainMax = speedGain; @@ -1057,7 +1058,7 @@ class Cgaz { this.clearZones([this.primeFirstZoneRight]); } - speedGain = this.findPrimeGain(this.primeFirstZoneRight, velocity, rotateVector(viewDir, -0.25 * Math.PI)); + speedGain = this.findPrimeGain(this.primeFirstZoneRight, velocity, rotateVector2D(viewDir, -0.25 * Math.PI)); gainZonesMap.set(this.primeFirstZoneRight, speedGain); if (speedGain > gainMax) gainMax = speedGain; @@ -1180,18 +1181,18 @@ class Cgaz { static findPrimeGain(zone, velocity, wishDir) { const avgAngle = 0.5 * (zone.leftAngle + zone.rightAngle); - const zoneVector = rotateVector(wishDir, -avgAngle); + const zoneVector = rotateVector2D(wishDir, -avgAngle); const snapProject = { x: Math.round(zoneVector.x * this.primeAccel), y: Math.round(zoneVector.y * this.primeAccel) }; - const newSpeed = getSize({ + const newSpeed = getSize2D({ x: Number(velocity.x) + Number(snapProject.x), y: Number(velocity.y) + Number(snapProject.y) }); - return newSpeed - getSize(velocity); + return newSpeed - getSize2D(velocity); } static updateFirstPrimeZone(target, offset, zone, angles) { diff --git a/scripts/util/math.js b/scripts/util/math.ts similarity index 60% rename from scripts/util/math.js rename to scripts/util/math.ts index 8b493277..04d2af42 100644 --- a/scripts/util/math.js +++ b/scripts/util/math.ts @@ -2,22 +2,21 @@ * Utility functions for Javascript. * Could be ported to C++ and exposed globally in the future. */ +type Vec2D = { x: number; y: number }; +type Vec3D = { x: number; y: number; z: number }; +type Vector = Vec2D | Vec3D; /** * 2D length of input vector - * @param {object} vec - * @returns {number} */ -function getSize(vec) { - return Math.sqrt(getSizeSquared(vec)); +function getSize2D(vec: Vector): number { + return Math.sqrt(getSizeSquared2D(vec)); } /** * 2D length squared of input vector - * @param {object} vec - * @returns {number} */ -function getSizeSquared(vec) { +function getSizeSquared2D(vec: Vector): number { return vec.x * vec.x + vec.y * vec.y; } @@ -28,8 +27,8 @@ function getSizeSquared(vec) { * @param {object} vec * @returns {object} */ -function getNormal(vec, threshold) { - const mag = getSize(vec); +function getNormal2D(vec, threshold): Vec2D { + const mag = getSize2D(vec); const vecNormal = { x: vec.x, y: vec.y @@ -47,67 +46,50 @@ function getNormal(vec, threshold) { /** * Dot product of two vectors. - * @param {object} vec1 - * @param {object} vec2 - * @returns {number} */ -function getDot(vec1, vec2) { +function getDot2D(vec1: Vector, vec2: Vector): number { return vec1.x * vec2.x + vec1.y * vec2.y; } /** * Cross product of two 2D vectors. * Defined as Z-component of resultant vector. - * @param {object} vec1 - * @param {object} vec2 - * @returns {number} */ -function getCross(vec1, vec2) { +function getCross2D(vec1: Vector, vec2: Vector): number { return vec1.x * vec2.y - vec1.y * vec2.x; } /** - * Rotate 2D vector anti-clockwise by specified angle (in radians). - * @param {object} vector - * @param {number} angle - * @returns {object} + * Returns 2D copy of input vector rotated anti-clockwise by specified angle (in radians). */ -function rotateVector(vector, angle) { +function rotateVector2D(vec: Vector, angle: number): Vec2D { const cos = Math.cos(angle); const sin = Math.sin(angle); return { - x: vector.x * cos - vector.y * sin, - y: vector.y * cos + vector.x * sin + x: vec.x * cos - vec.y * sin, + y: vec.y * cos + vec.x * sin }; } /** * Float equals check with threshold. - * @param {number} A - * @param {number} B - * @param {number} threshold - * @returns {boolean} */ -function floatEquals(A, B, threshold) { +function floatEquals(A: number, B: number, threshold: number): boolean { return Math.abs(A - B) < threshold; } /** * Clamp angle to range [-Pi/2, Pi/2] by wrapping - * @param {number} angle - * @returns {number} */ -function wrapToHalfPi(angle) { +function wrapToHalfPi(angle: number): number { return Math.abs(angle) > Math.PI * 0.5 ? wrapToHalfPi(angle - Math.sign(angle) * Math.PI) : angle; } /** * Converts [0, 2Pi) to [-Pi, Pi] - * @param {number} angle - * @returns {number} */ -function remapAngle(angle) { +function remapAngle(angle: number): number { angle += Math.PI; const integer = Math.trunc(angle / (2 * Math.PI)); angle -= integer * 2 * Math.PI; @@ -116,14 +98,8 @@ function remapAngle(angle) { /** * Convert an angle to a projected screen length in pixels. - * @param {number} angle - * @param {number} fov - * @param {number} distance - * @param {number} [scale=1] scale - * @param {number} [projection=0] projection - * @returns {number} */ -function mapAngleToScreenDist(angle, fov, length, scale = 1, projection = 0) { +function mapAngleToScreenDist(angle: number, fov: number, length: number, scale: number = 1, projection: number = 0) { const screenDist = length / scale; if (Math.abs(angle) >= fov) {