1
0
Fork 0
mirror of https://github.com/Acamaeda/The-Modding-Tree.git synced 2024-11-24 17:31:50 +00:00
The-Modding-Tree/js/game.js

441 lines
14 KiB
JavaScript
Raw Normal View History

2020-08-19 02:50:24 +00:00
var player;
var needCanvasUpdate = true;
2020-09-10 01:28:47 +00:00
var gameEnded = false;
2020-08-19 02:50:24 +00:00
2020-10-17 04:21:59 +00:00
// Don't change this
const TMT_VERSION = {
2021-06-07 04:29:22 +00:00
tmtNum: "2.6.1",
2021-06-05 00:38:05 +00:00
tmtName: "Fixed Reality"
2020-10-16 15:39:39 +00:00
}
function getResetGain(layer, useType = null) {
let type = useType
2020-12-14 03:23:12 +00:00
if (!useType){
type = tmp[layer].type
if (layers[layer].getResetGain !== undefined)
return layers[layer].getResetGain()
}
2020-10-15 03:09:30 +00:00
if(tmp[layer].type == "none")
return new Decimal (0)
2021-05-11 00:19:29 +00:00
if (tmp[layer].gainExp.eq(0)) return decimalZero
if (type=="static") {
2021-05-11 00:19:29 +00:00
if ((!tmp[layer].canBuyMax) || tmp[layer].baseAmount.lt(tmp[layer].requires)) return decimalOne
2020-10-16 22:45:44 +00:00
let gain = tmp[layer].baseAmount.div(tmp[layer].requires).div(tmp[layer].gainMult).max(1).log(tmp[layer].base).times(tmp[layer].gainExp).pow(Decimal.pow(tmp[layer].exponent, -1))
2021-05-10 21:37:29 +00:00
gain = gain.times(tmp[layer].directMult)
2020-09-14 19:26:10 +00:00
return gain.floor().sub(player[layer].points).add(1).max(1);
} else if (type=="normal"){
2021-05-11 00:19:29 +00:00
if (tmp[layer].baseAmount.lt(tmp[layer].requires)) return decimalZero
2020-10-16 22:45:44 +00:00
let gain = tmp[layer].baseAmount.div(tmp[layer].requires).pow(tmp[layer].exponent).times(tmp[layer].gainMult).pow(tmp[layer].gainExp)
2020-11-29 02:25:53 +00:00
if (gain.gte(tmp[layer].softcap)) gain = gain.pow(tmp[layer].softcapPower).times(tmp[layer].softcap.pow(decimalOne.sub(tmp[layer].softcapPower)))
2021-05-10 21:37:29 +00:00
gain = gain.times(tmp[layer].directMult)
2020-09-13 02:38:32 +00:00
return gain.floor().max(0);
} else if (type=="custom"){
return layers[layer].getResetGain()
} else {
2021-05-11 00:19:29 +00:00
return decimalZero
2020-08-19 02:50:24 +00:00
}
}
function getNextAt(layer, canMax=false, useType = null) {
let type = useType
2020-12-14 03:23:12 +00:00
if (!useType) {
type = tmp[layer].type
if (layers[layer].getNextAt !== undefined)
return layers[layer].getNextAt(canMax)
}
2020-10-15 03:09:30 +00:00
if(tmp[layer].type == "none")
return new Decimal (Infinity)
2020-10-12 22:28:12 +00:00
if (tmp[layer].gainMult.lte(0)) return new Decimal(Infinity)
if (tmp[layer].gainExp.lte(0)) return new Decimal(Infinity)
if (type=="static")
{
2020-10-16 22:45:44 +00:00
if (!tmp[layer].canBuyMax) canMax = false
2021-05-10 21:37:29 +00:00
let amt = player[layer].points.plus((canMax&&tmp[layer].baseAmount.gte(tmp[layer].nextAt))?tmp[layer].resetGain:0).div(tmp[layer].directMult)
2020-10-16 22:45:44 +00:00
let extraCost = Decimal.pow(tmp[layer].base, amt.pow(tmp[layer].exponent).div(tmp[layer].gainExp)).times(tmp[layer].gainMult)
let cost = extraCost.times(tmp[layer].requires).max(tmp[layer].requires)
2020-10-16 22:45:44 +00:00
if (tmp[layer].roundUpCost) cost = cost.ceil()
2020-08-25 22:42:43 +00:00
return cost;
} else if (type=="normal"){
2021-05-10 21:37:29 +00:00
let next = tmp[layer].resetGain.add(1).div(tmp[layer].directMult)
2020-11-29 02:25:53 +00:00
if (next.gte(tmp[layer].softcap)) next = next.div(tmp[layer].softcap.pow(decimalOne.sub(tmp[layer].softcapPower))).pow(decimalOne.div(tmp[layer].softcapPower))
2020-10-12 23:18:20 +00:00
next = next.root(tmp[layer].gainExp).div(tmp[layer].gainMult).root(tmp[layer].exponent).times(tmp[layer].requires).max(tmp[layer].requires)
2020-10-16 22:45:44 +00:00
if (tmp[layer].roundUpCost) next = next.ceil()
2020-08-25 22:42:43 +00:00
return next;
} else if (type=="custom"){
return layers[layer].getNextAt(canMax)
} else {
2021-05-11 00:19:29 +00:00
return decimalZero
}}
2020-08-19 02:50:24 +00:00
2020-12-04 04:19:14 +00:00
function softcap(value, cap, power = 0.5) {
if (value.lte(cap)) return value
else
return value.pow(power).times(cap.pow(decimalOne.sub(power)))
}
// Return true if the layer should be highlighted. By default checks for upgrades only.
function shouldNotify(layer){
2020-10-16 22:45:44 +00:00
for (id in tmp[layer].upgrades){
2021-05-13 05:44:40 +00:00
if (isPlainObject(layers[layer].upgrades[id])){
if (canAffordUpgrade(layer, id) && !hasUpgrade(layer, id) && tmp[layer].upgrades[id].unlocked){
return true
}
}
}
if (player[layer].activeChallenge && canCompleteChallenge(layer, player[layer].activeChallenge)) {
return true
}
2020-12-22 03:09:33 +00:00
2021-05-07 23:24:32 +00:00
if (tmp[layer].shouldNotify)
return true
2020-12-22 03:09:33 +00:00
if (isPlainObject(tmp[layer].tabFormat)) {
for (subtab in tmp[layer].tabFormat){
2021-05-07 23:24:32 +00:00
if (subtabShouldNotify(layer, 'mainTabs', subtab)) {
tmp[layer].trueGlowColor = tmp[layer].tabFormat[subtab].glowColor
2020-12-22 03:09:33 +00:00
return true
2021-05-07 23:24:32 +00:00
}
2020-12-22 03:09:33 +00:00
}
}
for (family in tmp[layer].microtabs) {
for (subtab in tmp[layer].microtabs[family]){
if (subtabShouldNotify(layer, family, subtab)) {
2021-05-07 23:24:32 +00:00
tmp[layer].trueGlowColor = tmp[layer].microtabs[family][subtab].glowColor
2020-12-22 03:09:33 +00:00
return true
}
2020-12-22 03:09:33 +00:00
}
}
2021-01-21 07:58:27 +00:00
2021-05-07 23:24:32 +00:00
return false
2021-01-21 07:58:27 +00:00
}
function canReset(layer)
2020-12-13 02:43:22 +00:00
{
2020-12-14 03:23:12 +00:00
if (layers[layer].canReset!== undefined)
return run(layers[layer].canReset, layers[layer])
else if(tmp[layer].type == "normal")
return tmp[layer].baseAmount.gte(tmp[layer].requires)
else if(tmp[layer].type== "static")
return tmp[layer].baseAmount.gte(tmp[layer].nextAt)
2020-12-14 03:23:12 +00:00
else
2020-10-15 03:09:30 +00:00
return false
}
2020-08-19 02:50:24 +00:00
function rowReset(row, layer) {
for (lr in ROW_LAYERS[row]){
if(layers[lr].doReset) {
2020-10-28 02:04:16 +00:00
Vue.set(player[lr], "activeChallenge", null) // Exit challenges on any row reset on an equal or higher row
2020-12-13 04:32:55 +00:00
run(layers[lr].doReset, layers[lr], layer)
}
else
if(tmp[layer].row > tmp[lr].row && row !== "side" && !isNaN(row)) layerDataReset(lr)
2020-08-19 02:50:24 +00:00
}
}
function layerDataReset(layer, keep = []) {
2021-05-26 05:44:12 +00:00
let storedData = {unlocked: player[layer].unlocked, forceTooltip: player[layer].forceTooltip, noRespecConfirm: player[layer].noRespecConfirm, prevTab:player[layer].prevTab} // Always keep these
for (thing in keep) {
if (player[layer][keep[thing]] !== undefined)
storedData[keep[thing]] = player[layer][keep[thing]]
}
2020-12-22 03:09:33 +00:00
Vue.set(player[layer], "buyables", getStartBuyables(layer))
Vue.set(player[layer], "clickables", getStartClickables(layer))
Vue.set(player[layer], "challenges", getStartChallenges(layer))
2020-11-29 21:28:24 +00:00
layOver(player[layer], getStartLayerData(layer))
player[layer].upgrades = []
player[layer].milestones = []
2020-12-22 03:09:33 +00:00
player[layer].achievements = []
2020-10-21 20:14:42 +00:00
player[layer].challenges = getStartChallenges(layer)
resetBuyables(layer)
2020-12-22 03:09:33 +00:00
2020-10-21 20:14:42 +00:00
if (layers[layer].clickables && !player[layer].clickables)
player[layer].clickables = getStartClickables(layer)
for (thing in storedData) {
player[layer][thing] =storedData[thing]
}
}
function resetBuyables(layer){
if (layers[layer].buyables)
player[layer].buyables = getStartBuyables(layer)
2021-05-11 00:19:29 +00:00
player[layer].spentOnBuyables = decimalZero
}
2020-10-11 20:16:36 +00:00
2020-09-09 22:14:05 +00:00
function addPoints(layer, gain) {
2020-09-14 19:26:10 +00:00
player[layer].points = player[layer].points.add(gain).max(0)
if (player[layer].best) player[layer].best = player[layer].best.max(player[layer].points)
2020-09-14 19:26:10 +00:00
if (player[layer].total) player[layer].total = player[layer].total.add(gain)
2020-09-09 22:14:05 +00:00
}
function generatePoints(layer, diff) {
addPoints(layer, tmp[layer].resetGain.times(diff))
2020-09-09 22:14:05 +00:00
}
2020-09-11 02:15:05 +00:00
var prevOnReset
2020-08-19 02:50:24 +00:00
function doReset(layer, force=false) {
if (tmp[layer].type == "none") return
2020-10-16 22:45:44 +00:00
let row = tmp[layer].row
2020-08-19 02:50:24 +00:00
if (!force) {
if (tmp[layer].baseAmount.lt(tmp[layer].requires)) return;
let gain = tmp[layer].resetGain
2020-10-16 22:45:44 +00:00
if (tmp[layer].type=="static") {
if (tmp[layer].baseAmount.lt(tmp[layer].nextAt)) return;
2020-10-16 22:45:44 +00:00
gain =(tmp[layer].canBuyMax ? gain : 1)
}
2020-10-16 22:45:44 +00:00
if (tmp[layer].type=="custom") {
if (!tmp[layer].canReset) return;
}
if (layers[layer].onPrestige)
2020-12-13 04:32:55 +00:00
run(layers[layer].onPrestige, layers[layer], gain)
addPoints(layer, gain)
updateMilestones(layer)
2020-10-15 01:43:16 +00:00
updateAchievements(layer)
if (!player[layer].unlocked) {
player[layer].unlocked = true;
2020-08-19 02:50:24 +00:00
needCanvasUpdate = true;
2020-09-11 14:21:08 +00:00
2020-10-16 22:45:44 +00:00
if (tmp[layer].increaseUnlockOrder){
lrs = tmp[layer].increaseUnlockOrder
for (lr in lrs)
2020-10-15 03:31:05 +00:00
if (!player[lrs[lr]].unlocked) player[lrs[lr]].unlockOrder++
}
2020-08-19 02:50:24 +00:00
}
2020-09-11 14:21:08 +00:00
2021-05-11 00:19:29 +00:00
tmp[layer].baseAmount = decimalZero // quick fix
2020-08-19 02:50:24 +00:00
}
2020-09-09 22:14:05 +00:00
2021-06-02 20:53:20 +00:00
if (run(layers[layer].resetsNothing, layers[layer])) return
2020-09-14 02:41:42 +00:00
for (layerResetting in layers) {
if (row >= layers[layerResetting].row && (!force || layerResetting != layer)) completeChallenge(layerResetting)
2020-09-14 02:41:42 +00:00
}
2020-09-11 02:15:05 +00:00
2021-06-03 05:04:56 +00:00
prevOnReset = {...player}
2021-05-11 00:19:29 +00:00
player.points = (row == 0 ? decimalZero : getStartPoints())
for (let x = row; x >= 0; x--) rowReset(x, layer)
2021-06-10 04:32:07 +00:00
for (r in OTHER_LAYERS){
rowReset(r, layer)
}
2020-09-11 02:15:05 +00:00
prevOnReset = undefined
2020-12-22 03:09:33 +00:00
player[layer].resetTime = 0
2020-08-26 19:08:00 +00:00
updateTemp()
2020-09-09 02:10:25 +00:00
updateTemp()
2020-08-19 02:50:24 +00:00
}
function resetRow(row) {
if (prompt('Are you sure you want to reset this row? It is highly recommended that you wait until the end of your current run before doing this! Type "I WANT TO RESET THIS" to confirm')!="I WANT TO RESET THIS") return
let pre_layers = ROW_LAYERS[row-1]
let layers = ROW_LAYERS[row]
let post_layers = ROW_LAYERS[row+1]
rowReset(row+1, post_layers[0])
doReset(pre_layers[0], true)
for (let layer in layers) {
2020-10-16 22:45:44 +00:00
player[layer].unlocked = false
if (player[layer].unlockOrder) player[layer].unlockOrder = 0
2020-08-19 02:50:24 +00:00
}
2020-10-17 04:21:59 +00:00
player.points = getStartPoints()
2020-08-21 19:02:34 +00:00
updateTemp();
2020-08-19 02:50:24 +00:00
resizeCanvas();
}
function startChallenge(layer, x) {
2020-10-07 20:41:45 +00:00
let enter = false
if (!player[layer].unlocked) return
if (player[layer].activeChallenge == x) {
completeChallenge(layer, x)
Vue.set(player[layer], "activeChallenge", null)
} else {
2020-10-07 20:41:45 +00:00
enter = true
}
2020-09-14 02:41:42 +00:00
doReset(layer, true)
if(enter) {
Vue.set(player[layer], "activeChallenge", x)
run(layers[layer].challenges[x].onEnter, layers[layer].challenges[x])
}
updateChallengeTemp(layer)
2020-08-26 16:26:46 +00:00
}
function canCompleteChallenge(layer, x)
{
if (x != player[layer].activeChallenge) return
2020-10-16 15:39:39 +00:00
let challenge = tmp[layer].challenges[x]
2020-11-29 21:28:24 +00:00
if (challenge.canComplete !== undefined) return challenge.canComplete
if (challenge.currencyInternalName){
let name = challenge.currencyInternalName
2020-10-16 15:39:39 +00:00
if (challenge.currencyLocation){
return !(challenge.currencyLocation[name].lt(challenge.goal))
}
else if (challenge.currencyLayer){
let lr = challenge.currencyLayer
2020-12-13 02:43:22 +00:00
return !(player[lr][name].lt(challenge.goal))
}
else {
return !(player[name].lt(challenge.goal))
}
}
else {
return !(player.points.lt(challenge.goal))
}
}
function completeChallenge(layer, x) {
var x = player[layer].activeChallenge
2020-09-14 02:41:42 +00:00
if (!x) return
let completions = canCompleteChallenge(layer, x)
if (!completions){
Vue.set(player[layer], "activeChallenge", null)
run(layers[layer].challenges[x].onExit, layers[layer].challenges[x])
2020-10-11 20:42:32 +00:00
return
}
if (player[layer].challenges[x] < tmp[layer].challenges[x].completionLimit) {
needCanvasUpdate = true
player[layer].challenges[x] += completions
player[layer].challenges[x] = Math.min(player[layer].challenges[x], tmp[layer].challenges[x].completionLimit)
2020-12-13 04:32:55 +00:00
if (layers[layer].challenges[x].onComplete) run(layers[layer].challenges[x].onComplete, layers[layer].challenges[x])
2020-09-09 22:14:05 +00:00
}
Vue.set(player[layer], "activeChallenge", null)
run(layers[layer].challenges[x].onExit, layers[layer].challenges[x])
updateChallengeTemp(layer)
2020-09-09 22:14:05 +00:00
}
2020-09-15 19:27:33 +00:00
VERSION.withoutName = "v" + VERSION.num + (VERSION.pre ? " Pre-Release " + VERSION.pre : VERSION.pre ? " Beta " + VERSION.beta : "")
2020-09-14 19:26:10 +00:00
VERSION.withName = VERSION.withoutName + (VERSION.name ? ": " + VERSION.name : "")
2020-09-14 02:41:42 +00:00
2020-12-01 03:36:54 +00:00
function autobuyUpgrades(layer){
if (!tmp[layer].upgrades) return
for (id in tmp[layer].upgrades)
2020-12-02 02:02:08 +00:00
if (isPlainObject(tmp[layer].upgrades[id]) && (layers[layer].upgrades[id].canAfford === undefined || layers[layer].upgrades[id].canAfford() === true))
2020-12-01 03:36:54 +00:00
buyUpg(layer, id)
}
2020-09-10 01:28:47 +00:00
2020-08-19 02:50:24 +00:00
function gameLoop(diff) {
2021-06-02 21:54:07 +00:00
if (isEndgame() || gameEnded){
gameEnded = 1
clearParticles()
}
2020-09-14 19:26:10 +00:00
2021-06-03 14:28:07 +00:00
if (isNaN(diff) || diff < 0) diff = 0
2020-09-10 01:46:36 +00:00
if (gameEnded && !player.keepGoing) {
diff = 0
2021-06-03 05:04:56 +00:00
//player.tab = "gameEnded"
clearParticles()
2020-09-10 01:28:47 +00:00
}
2020-09-14 21:18:02 +00:00
if (maxTickLength) {
let limit = maxTickLength()
if(diff > limit)
diff = limit
}
2020-09-14 22:12:36 +00:00
addTime(diff)
2020-10-11 22:38:54 +00:00
player.points = player.points.add(tmp.pointGen.times(diff)).max(0)
2020-10-21 17:03:39 +00:00
2021-05-07 13:47:25 +00:00
for (let x = 0; x <= maxRow; x++){
2020-10-21 17:03:39 +00:00
for (item in TREE_LAYERS[x]) {
2020-10-28 02:04:16 +00:00
let layer = TREE_LAYERS[x][item]
2020-12-22 03:09:33 +00:00
player[layer].resetTime += diff
2020-11-08 04:34:53 +00:00
if (tmp[layer].passiveGeneration) generatePoints(layer, diff*tmp[layer].passiveGeneration);
2020-10-21 21:43:05 +00:00
if (layers[layer].update) layers[layer].update(diff);
2020-10-21 17:03:39 +00:00
}
2020-08-29 19:45:18 +00:00
}
2020-09-14 02:41:42 +00:00
2020-10-21 17:03:39 +00:00
for (row in OTHER_LAYERS){
for (item in OTHER_LAYERS[row]) {
2020-10-28 02:04:16 +00:00
let layer = OTHER_LAYERS[row][item]
2020-12-22 03:09:33 +00:00
player[layer].resetTime += diff
2020-11-08 04:34:53 +00:00
if (tmp[layer].passiveGeneration) generatePoints(layer, diff*tmp[layer].passiveGeneration);
2020-10-21 21:43:05 +00:00
if (layers[layer].update) layers[layer].update(diff);
2020-10-21 17:03:39 +00:00
}
}
2021-05-07 13:47:25 +00:00
for (let x = maxRow; x >= 0; x--){
2020-10-21 17:03:39 +00:00
for (item in TREE_LAYERS[x]) {
2020-10-28 02:04:16 +00:00
let layer = TREE_LAYERS[x][item]
2020-11-08 04:34:53 +00:00
if (tmp[layer].autoPrestige && tmp[layer].canReset) doReset(layer);
2020-10-21 17:03:39 +00:00
if (layers[layer].automate) layers[layer].automate();
2021-05-18 01:51:36 +00:00
if (tmp[layer].autoUpgrade) autobuyUpgrades(layer)
2020-10-21 17:03:39 +00:00
}
}
for (row in OTHER_LAYERS){
for (item in OTHER_LAYERS[row]) {
2020-10-28 02:04:16 +00:00
let layer = OTHER_LAYERS[row][item]
2020-11-08 04:34:53 +00:00
if (tmp[layer].autoPrestige && tmp[layer].canReset) doReset(layer);
2020-10-21 17:03:39 +00:00
if (layers[layer].automate) layers[layer].automate();
player[layer].best = player[layer].best.max(player[layer].points)
if (tmp[layer].autoUpgrade) autobuyUpgrades(layer)
2020-10-21 17:03:39 +00:00
}
}
2020-08-26 03:32:27 +00:00
for (layer in layers){
if (layers[layer].milestones) updateMilestones(layer);
2020-10-15 01:43:16 +00:00
if (layers[layer].achievements) updateAchievements(layer)
2020-09-09 02:10:25 +00:00
}
2020-08-26 03:32:27 +00:00
2020-08-19 02:50:24 +00:00
}
2021-06-07 03:45:32 +00:00
function hardReset(resetOptions) {
2020-08-19 02:50:24 +00:00
if (!confirm("Are you sure you want to do this? You will lose all your progress!")) return
2020-12-01 01:55:04 +00:00
player = null
2021-06-07 03:45:32 +00:00
if(resetOptions) options = null
2021-06-07 21:54:29 +00:00
save(true);
2020-08-19 02:50:24 +00:00
window.location.reload();
}
2020-09-15 00:45:31 +00:00
var ticking = false
2020-08-19 02:50:24 +00:00
var interval = setInterval(function() {
2020-08-31 18:33:29 +00:00
if (player===undefined||tmp===undefined) return;
2020-09-15 00:45:31 +00:00
if (ticking) return;
2020-09-10 01:39:08 +00:00
if (gameEnded&&!player.keepGoing) return;
2020-09-15 00:45:31 +00:00
ticking = true
let now = Date.now()
2020-09-15 19:27:33 +00:00
let diff = (now - player.time) / 1e3
2021-05-14 01:40:51 +00:00
let trueDiff = diff
2020-09-15 19:27:33 +00:00
if (player.offTime !== undefined) {
2020-11-29 02:25:53 +00:00
if (player.offTime.remain > modInfo.offlineLimit * 3600) player.offTime.remain = modInfo.offlineLimit * 3600
2020-09-15 19:27:33 +00:00
if (player.offTime.remain > 0) {
let offlineDiff = Math.max(player.offTime.remain / 10, diff)
player.offTime.remain -= offlineDiff
diff += offlineDiff
}
if (!player.offlineProd || player.offTime.remain <= 0) player.offTime = undefined
2020-09-01 00:39:33 +00:00
}
2020-09-15 19:27:33 +00:00
if (player.devSpeed) diff *= player.devSpeed
2020-09-15 00:45:31 +00:00
player.time = now
2020-10-30 23:40:48 +00:00
if (needCanvasUpdate){ resizeCanvas();
needCanvasUpdate = false;
}
tmp.scrolled = document.getElementById('treeTab') && document.getElementById('treeTab').scrollTop > 30
2020-08-21 19:02:34 +00:00
updateTemp();
updateOomps(diff);
updateWidth()
2021-05-10 01:39:15 +00:00
updateTabFormats()
2020-09-09 22:14:05 +00:00
gameLoop(diff)
2020-10-27 21:41:35 +00:00
fixNaNs()
2021-05-14 01:40:51 +00:00
adjustPopupTime(trueDiff)
updateParticles(trueDiff)
2020-09-15 00:45:31 +00:00
ticking = false
2020-08-19 22:12:31 +00:00
}, 50)
setInterval(function() {needCanvasUpdate = true}, 500)