// ==UserScript==
// @name The Tree Enchanted
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Custom enchancements to TMT
// @author Dimava
// @match *://*/*
// @grant none
// ==/UserScript==
if (globalThis.TREE_LAYERS) void function() {
function __init__() {
q = s => document.querySelector(s);
qq = s => [...document.querySelectorAll(s)];
elm = function elm(sel = '', ...children) {
let el = document.createElement('div');
sel.replace(/([\w-]+)|#([\w-]+)|\.([\w-]+)|\[([^\]=]+)(?:=([^\]]*))\]/g, (s, tag, id, cls, attr, val) => {
if (tag) el = document.createElement(tag);
if (id) el.id = id;
if (cls) el.classList.add(cls);
if (attr) el.setAttribute(attr, val ?? true);
});
for (let f of children.filter(e => typeof e == 'function')) {
let name = f.name || (f + '').match(/\w+/);
if (!name.startsWith('on')) name = 'on' + name;
el[name] = f;
}
el.append(...children.filter(e => typeof e != 'function'));
return el;
}
Object.defineValue = function defineValue(o, p, value) {
if (typeof p == 'function') {
[p, value] = [p.name, p];
}
return Object.defineProperty(o, p, {
value,
configurable: true,
enumerable: false,
writable: true,
});
};
Object.defineValue(Element.prototype, function q(sel) {
return this.querySelector(sel);
});
Object.defineValue(Element.prototype, function qq(sel) {
return [...this.querySelectorAll(sel)];
});
}
window.__init__ || __init__();
let loopReset = false;
let keyzloop = 0;
let keysdown = {};
function onraf() {
if (keysdown.x) {
}
}
void async function() {
while(true) {
await Promise.frame();
onraf();
}
}
onkeydown = async event=>{
// console.log(event)
if (event.key == 'z' || event.key == 'X') {
q('.reset.can')?.click();
}
if (event.key == 'x' || event.key == 'X') {
for (let e of qq(`
.upg.can:not(.reset), .buyable.can:not(.reset),
.canComplete .longUpg
`).reverse()) {
e.click();
}
}
// if (event.key == 'c') {
// if (loopReset) return;
// loopReset = true;
// while (loopReset) {
// await bestReset();
// }
// }
let treeNode = qq('.treeNode.can:not(.ghost)').find(e=>e.innerText.startsWith(event.key))
treeNode?.click();
if (event.key == 'Tab') {
let tabs = qq('.tabButton');
let i = tabs.findIndex(e=>e.innerText == player.subtabs[player.tab].mainTabs) + 1;
if (event.shiftKey) i += tabs.length - 2;
tabs[i % tabs.length].click();
event.preventDefault();
}
}
onkeyup = event => {
if (event.key == 'c') {
loopReset = false;
}
}
Layer = class {
static hasAllUpgrades(layer) {
return Object.values(tmp[layer].upgrades).every(e => !hasUpgrade(layer, e.id));
}
static status(layerId) {
let layer = tmp[layerId];
let ups = Object.values(layer.upgrades || {}).filter(e => e.id);
let ms = Object.values(layer.milestones || {}).filter(e => e.id);
let cha = Object.values(layer.challenges || {}).filter(e => e.id);
// player[layer].challenges[x] < tmp[layer].challenges[x].completionLimit
return {
upgrades: {
total: ups.length,
unlocked: ups.filter(e => e.unlocked || hasUpgrade(layerId, e.id)).length,
done: ups.filter(e => hasUpgrade(layerId, e.id)).length,
available: ups.filter(e => e.unlocked && !hasUpgrade(layerId, e.id) && canAffordUpgrade(layerId, e.id)).length,
},
milestones: {
total: ms.length,
unlocked: ms.filter(e => e.unlocked).length,
done: ms.filter(e => e.done).length,
},
challenges: {
total: cha.length,
unlocked: cha.filter(e => e.unlocked).length,
done: cha.filter(e => player[layerId].challenges[e.id] >= e.completionLimit).length,
active: !!player[layerId].activeChallenge,
canComplete: player[layerId].activeChallenge && canCompleteChallenge(layerId, player[layerId].activeChallenge),
},
}
}
static shortStatus(layerId) {
let s = this.status(layerId);
return {
upgrades: `${s.upgrades.bought}/${s.upgrades.unlocked}/${s.upgrades.total}`,
}
}
static showStars(layerId) {
function star(color, empty) {
return elm(`.statusStar[style=color:${color};]`, typeof empty == 'string' ? empty : empty ? '☆' : '★')
}
let node = q(`.treeNode.${layerId}`);
if (!node) return //console.error(`bode not found: ${layerId}`)
let s = this.status(layerId);
let ups = s.upgrades;
let ms = s.milestones;
let cha = s.challenges;
ups = ups.total && star(ups.available ? 'yellowgreen' : ups.unlocked < ups.total ? 'silver' : 'gold', ups.done < ups.unlocked);
ms = ms.total && star(ms.unlocked < ms.total ? 'silver' : 'gold', ms.done < ms.unlocked);
cha = cha.total && star(cha.active ? 'red' : cha.unlocked < cha.total ? 'silver' : 'gold', cha.done < cha.unlocked && !cha.canComplete);
let container = elm('.sscon',...[cha, ms, ups].filter(Boolean));
let oldContainer = node.q('.sscon');
if (!oldContainer) {
node.append(container);
} else if (oldContainer.outerHTML != container.outerHTML) {
oldContainer.replaceWith(container);
}
}
static showAllStars() {
Object.values(tmp).filter(e=>e.layerShown==true)
.map(e=>this.showStars(e.layer));
}
}
window.layInt && clearInterval(layInt)
layInt=setInterval(()=>Layer.showAllStars(), 200)
q('head').append(elm('style', `
.sscon {
position:absolute;
font-size: 33.333%;
font-family: initial;
text-shadow: 0 0 4px gray, 0 0 3px black;
display: flex;
flex-direction: row-reverse;
}
.statusStar {
display: inline-block;
transform: scale(3);
}
`))
}();