From 98e8d09f62dd1da4914bd8a5f5396388267602ee Mon Sep 17 00:00:00 2001 From: "Laetitia (O-01-67)" <68935009+O-01-67@users.noreply.github.com> Date: Tue, 14 Feb 2023 10:00:29 -0500 Subject: [PATCH] Save optimizations * use 0 instead of null in JSON (off by default) * round temps to 3 digits (on by default) requires console to change --- mods/save_loading.js | 48 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/mods/save_loading.js b/mods/save_loading.js index fc3892c3..7e0a28fc 100644 --- a/mods/save_loading.js +++ b/mods/save_loading.js @@ -1,16 +1,57 @@ var modName = "mods/save_loading.js"; +function zeroToNull(val) { + if(val === 0) { return null }; + return val; +}; + +if(!localStorage.saveSettings) { + localStorage.setItem("saveSettings", '{"shortenNulls":false,"roundTemps":true}'); +}; + +saveSettings = JSON.parse(localStorage.saveSettings); + +function epsilonRound(num,precision) { + return Math.round((num + Number.EPSILON) * (10 ** precision)) / (10 ** precision); +}; + function getSimulationState() { var simulationState = { //currentPixels: currentPixels, - pixelMap: pixelMap, + pixelMap: structuredClone(pixelMap), width: width, height: height, pixelSize: pixelSize, settings: settings, - version: 0, + version: 1, enabledMods: localStorage.enabledMods, }; + nulls: if(saveSettings.shortenNulls) { + if(!structuredClone) { + alert("Your browser does not support structuredClone, which is needed to shorten nulls to zeroes without corrupting the current pixelMap."); + console.error("shortenNulls: structuredClone not supported"); + break nulls; + }; + for(i = 0; i < simulationState.pixelMap.length; i++) { + var column = simulationState.pixelMap[i]; + for(j = 0; j < column.length; j++) { + if(column[j] === null || typeof(column[j]) === "undefined") { + column[j] = 0; + }; + }; + }; + }; + if(saveSettings.roundTemps) { + for(i = 0; i < simulationState.pixelMap.length; i++) { + var column = simulationState.pixelMap[i]; + for(j = 0; j < column.length; j++) { + var pixel = column[j]; + if(pixel?.temp) { + pixel.temp = epsilonRound(pixel.temp,3); + }; + }; + }; + }; return simulationState; }; @@ -192,6 +233,9 @@ function importJsonState(json) { height = json.height; pixelSize = json.pixelSize; //currentPixels = json.currentPixels; + for(i = 0; i < json.pixelMap.length; i++) { + json.pixelMap[i] = json.pixelMap[i].map(x => zeroToNull(x)); + }; pixelMap = json.pixelMap; if(json.settings) { settings = json.settings;