diff --git a/changelog.md b/changelog.md index 4ee1884..87db74e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,7 @@ # The Modding Tree changelog: +- NaN is now handled more intelligently. + ### v2.1.4 - 10/25/20 - Added an infobox component. Thank you to thepaperpilot for this contribution! - Layer type is now optional, and defaults to "none". diff --git a/js/game.js b/js/game.js index 058d09a..861c3c9 100644 --- a/js/game.js +++ b/js/game.js @@ -1,6 +1,5 @@ var player; var needCanvasUpdate = true; -var NaNalert = false; var gameEnded = false; // Don't change this @@ -319,13 +318,6 @@ function gameLoop(diff) { if (layers[layer].achievements) updateAchievements(layer) } - if (player.hasNaN&&!NaNalert) { - clearInterval(interval); - player.autosave = false; - NaNalert = true; - - alert("We have detected a corruption in your save. Please visit one of the discords in the info panel for help.") - } } function hardReset() { @@ -358,5 +350,6 @@ var interval = setInterval(function() { if (needCanvasUpdate) resizeCanvas(); updateTemp(); gameLoop(diff) + fixNaNs() ticking = false }, 50) diff --git a/js/layerSupport.js b/js/layerSupport.js index b01f9e9..2711b3e 100644 --- a/js/layerSupport.js +++ b/js/layerSupport.js @@ -1,5 +1,9 @@ var layers = {} +const decimalZero = new Decimal(0) +const decimalOne = new Decimal(1) +const decimalNaN = new Decimal(NaN) + function layerShown(layer){ return layers[layer].layerShown(); } diff --git a/js/temp.js b/js/temp.js index 9ee51c3..2c885a1 100644 --- a/js/temp.js +++ b/js/temp.js @@ -1,4 +1,5 @@ var tmp = {} +var NaNalert = false; // Tmp will not call these var activeFunctions = [ @@ -88,7 +89,21 @@ function updateTempData(layerData, tmpData) { updateTempData(layerData[item], tmpData[item]) } else if (isFunction(layerData[item]) && !activeFunctions.includes(item)){ - Vue.set(tmpData, item, layerData[item]()) + let value = layerData[item]() + if (value !== value || value === decimalNaN){ + if (NaNalert === true || confirm ("Invalid value found in tmp, named '" + item + "'. Please let the creator of this mknow! Would you like to try to auto-fix the save and keep going?")){ + NaNalert = true + value = (value !== value ? 0 : decimalZero) + } + else { + clearInterval(interval); + player.autosave = false; + NaNalert = true; + } + } + + + Vue.set(tmpData, item, value) } } } diff --git a/js/utils.js b/js/utils.js index 824460c..8471f3b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -241,6 +241,38 @@ function load() { loadVue(); } + +function fixNaNs() { + NaNcheck(player) +} + +function NaNcheck(data) { + for (item in data){ + if (data[item] == null) { + } + else if (Array.isArray(data[item])) { + NaNcheck(data[item]) + } + else if (data[item] !== data[item] || data[item] === decimalNaN){ + if (NaNalert === true || confirm ("Invalid value found in player, named '" + item + "'. Please let the creator of this mknow! Would you like to try to auto-fix the save and keep going?")){ + NaNalert = true + data[item] = (data[item] !== data[item] ? 0 : decimalZero) + } + else { + clearInterval(interval); + player.autosave = false; + NaNalert = true; + } + } + else if (data[item] instanceof Decimal) { // Convert to Decimal + } + else if ((!!data[item]) && (data[item].constructor === Object)) { + NaNcheck(data[item]) + } + } +} + + function exportSave() { let str = btoa(JSON.stringify(player))