Skip to content

Commit

Permalink
feat: convert math.js to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
PeenScreeker committed Dec 2, 2023
1 parent a5fab9a commit 97f1a59
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 57 deletions.
31 changes: 16 additions & 15 deletions scripts/hud/cgaz.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -638,7 +638,7 @@ class Cgaz {

// arrow
if (this.primeArrowEnable) {
if (getSizeSquared(wishDir) > 0) {
if (getSizeSquared2D(wishDir) > 0) {
let arrowAngle =
wishAngle -
Math.atan2(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
62 changes: 20 additions & 42 deletions scripts/util/math.js → scripts/util/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
* 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;
}

Expand All @@ -28,8 +29,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
Expand All @@ -47,67 +48,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;
Expand All @@ -116,14 +100,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) {
Expand Down

0 comments on commit 97f1a59

Please sign in to comment.