This repository has been archived by the owner on Aug 15, 2022. It is now read-only.
/
fovConvert.ts
144 lines (134 loc) 路 4.63 KB
/
fovConvert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { Command } from "discord-akairo";
import type { Message } from "discord.js";
import { getObject } from "../../array";
import { atan, tan, pi } from "mathjs";
export default class FOVConvertCommand extends Command {
constructor() {
super("fovconvert", {
aliases: ["fov-convert", "film-convert", "convert-fov"],
description: {
content:
"Converts a FOV value from one game or film notation to another",
usage:
"<fov> <input game | FILM notation> <output game | FILM notation> <aspect ratio>",
flags: "-dp <output decimal places>",
examples: ["fovconvert 90 source 16MF9 16:9"],
},
args: [
{
id: "fov",
type: "number",
prompt: true,
},
{
id: "inFOVT",
prompt: true,
},
{
id: "outFOVT",
prompt: true,
},
{
id: "aspect",
type: /^\d{1,4}:\d{1,4}$/g,
prompt: true,
},
{
id: "dp",
type: "number",
match: "option",
flag: ["-dp", "dp:", "dp"],
default: 3,
},
],
argumentDefaults: {
prompt: {
start: `Invalid command usage. The \`fovconvert\` command's accepted format is \`fovconvert <fov> <input game | FILM> <output game | FILM> <aspect ratio>\`. Use \`help fovconvert\` for more information`,
time: 1,
retries: 0,
},
},
});
}
async exec(
message: Message,
args: {
fov: number;
inFOVT: string;
outFOVT: string;
aspect: { match: string[] };
dp: number;
}
): Promise<Message | undefined> {
if (
(!getObject(args.inFOVT, "afovt") &&
!/^hm[lfi]$|vm[lfi]$|\d{1,2}m[lfi]\d{1,2}$/i.test(args.inFOVT)) ||
(!getObject(args.outFOVT, "afovt") &&
!/^hm[lfi]$|vm[lfi]$|\d{1,2}m[lfi]\d{1,2}$/i.test(args.outFOVT))
) {
return message.util?.reply(
`Invalid command usage. The \`fovconvert\` command's accepted format is \`fovconvert <fov> <input game | FILM> <output game | FILM> <aspect ratio>\`. Use \`help fovconvert\` for more information`
);
}
const getFOVT = (game: string) => {
return (getObject(game, "afovt") ? getObject(game, "afovt") || "" : game)
.toString()
.toLowerCase();
};
const convertFOV = (from: number, to: number, fov: number) =>
(atan((to / from) * tan((fov * pi) / 360)) * 360) / pi;
const fovtAspect = (fovt: string) =>
Number(fovt.split(/m[l|f|i]/i)[0]) / Number(fovt.split(/m[l|f|i]/i)[1]);
const fovtEndsWith = (fovt: string) => fovt.split(/m/i)[1][0];
const fovtStartsWith = (fovt: string) => fovt[0];
const argAspect =
Number(args.aspect.match.toString().split(":")[0] || "") /
Number(args.aspect.match.toString().split(":")[1] || "");
function findFOV(fovt: string, fov: number) {
if (fovtEndsWith(fovt) === "l" && !isNaN(Number(fovtStartsWith(fovt)))) {
return {
vfov: convertFOV(fovtAspect(fovt), 1, fov),
hfov: convertFOV(fovtAspect(fovt), argAspect, fov),
};
} else if (fovtEndsWith(fovt) === "l" && fovtStartsWith(fovt) === "h") {
return { vfov: convertFOV(argAspect, 1, fov), hfov: fov };
} else if (fovtEndsWith(fovt) === "l" && fovtStartsWith(fovt) === "v") {
return { hfov: fov, vfov: convertFOV(argAspect, 1, fov) };
} else if (fovtEndsWith(fovt) === "f") {
if (argAspect >= fovtAspect(fovt)) {
return { hfov: fov, vfov: convertFOV(argAspect, 1, fov) };
} else {
return {
hfov: convertFOV(fovtAspect(fovt), argAspect, fov),
vfov: convertFOV(fovtAspect(fovt), 1, fov),
};
}
} else if (fovtEndsWith(fovt) === "i") {
if (argAspect > fovtAspect(fovt)) {
return {
hfov: convertFOV(fovtAspect(fovt), argAspect, fov),
vfov: convertFOV(fovtAspect(fovt), 1, fov),
};
} else if (argAspect < fovtAspect(fovt)) {
return {
hfov: fov,
vfov: convertFOV(argAspect, 1, fov),
};
} else {
return { hfov: fov, vfov: convertFOV(argAspect, 1, fov) };
}
}
}
const output =
fovtStartsWith(getFOVT(args.outFOVT)) === "v"
? findFOV(
getFOVT(args.outFOVT),
findFOV(getFOVT(args.inFOVT), args.fov)?.hfov || 0
)?.vfov
: findFOV(
getFOVT(args.outFOVT),
findFOV(getFOVT(args.inFOVT), args.fov)?.hfov || 0
)?.hfov;
return message.util?.reply(output?.toFixed(args.dp));
}
}