wynnbuilder-forked-for-changes/utils.js

114 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-01-07 06:41:41 +00:00
// Permutations in js reference (also cool algorithm):
// https://stackoverflow.com/a/41068709
function perm(a){
if (a.length == 0) return [[]];
var r = [[a[0]]],
t = [],
s = [];
if (a.length == 1) return r;
for (var i = 1, la = a.length; i < la; i++){
for (var j = 0, lr = r.length; j < lr; j++){
r[j].push(a[i]);
t.push(r[j]);
for(var k = 1, lrj = r[j].length; k < lrj; k++){
for (var l = 0; l < lrj; l++) s[l] = r[j][(k+l)%lrj];
t[t.length] = s;
s = [];
}
}
r = t;
t = [];
}
return r;
}
function setText(id, text) {
document.getElementById(id).textContent = text;
}
2021-01-07 06:41:41 +00:00
function setHTML(id, html) {
document.getElementById(id).innerHTML = html;
}
function setValue(id, value) {
2021-01-07 10:23:54 +00:00
let el = document.getElementById(id);
el.value = value;
el.dispatchEvent(new Event("change"));
2021-01-07 06:41:41 +00:00
}
2021-01-07 08:34:31 +00:00
2021-01-08 20:17:37 +00:00
function getValue(id) {
return document.getElementById(id).value;
}
2021-01-07 08:34:31 +00:00
// Base 64 encoding tools
// https://stackoverflow.com/a/27696695
2021-01-07 10:23:54 +00:00
// Modified for fixed precision
2021-01-07 08:34:31 +00:00
Base64 = (function () {
var digitsStr =
// 0 8 16 24 32 40 48 56 63
// v v v v v v v v v
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-";
var digits = digitsStr.split('');
var digitsMap = {};
for (var i = 0; i < digits.length; i++) {
digitsMap[digits[i]] = i;
}
return {
2021-01-07 10:23:54 +00:00
fromIntV: function(int32) {
2021-01-07 08:34:31 +00:00
var result = '';
while (true) {
result = digits[int32 & 0x3f] + result;
int32 >>>= 6;
if (int32 === 0)
break;
}
return result;
},
2021-01-07 10:23:54 +00:00
fromIntN: function(int32, n) {
var result = '';
for (let i = 0; i < n; ++i) {
result = digits[int32 & 0x3f] + result;
2021-01-09 09:09:47 +00:00
int32 >>= 6;
2021-01-07 10:23:54 +00:00
}
return result;
},
2021-01-07 08:34:31 +00:00
toInt: function(digitsStr) {
var result = 0;
var digits = digitsStr.split('');
for (var i = 0; i < digits.length; i++) {
result = (result << 6) + digitsMap[digits[i]];
}
return result;
2021-01-07 10:23:54 +00:00
},
2021-01-09 09:09:47 +00:00
toIntSigned: function(digitsStr) {
var result = 0;
var digits = digitsStr.split('');
if (digits[0] && (digitsMap[digits[0]] & 0x20)) {
result = -1;
}
for (var i = 0; i < digits.length; i++) {
result = (result << 6) + digitsMap[digits[i]];
}
return result;
}
2021-01-07 08:34:31 +00:00
};
})();
// Base64.fromInt(-2147483648); // gives "200000"
// Base64.toInt("200000"); // gives -2147483648
2021-01-10 10:02:23 +00:00
/*
Turns a raw stat and a % stat into a final stat on the basis that - raw and >= 100% becomes 0 and + raw and <=-100% becomes 0.
Pct would be 0.80 for 80%, -1.20 for 120%, etc
*/
function rawToPct(raw, pct){
final = 0;
if (raw < 0){
final = (Math.min(0, raw - (raw * pct) ));
}else if(raw > 0){
final = (Math.max(0, raw + (raw * pct)));
}else{ //do nothing - final's already 0
}
return final;
}