Save optimizations

* use 0 instead of null in JSON (off by default)
* round temps to 3 digits (on by default)
requires console to change
This commit is contained in:
Laetitia (O-01-67) 2023-02-14 10:00:29 -05:00 committed by GitHub
parent fc08940a81
commit 98e8d09f62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 2 deletions

View File

@ -1,16 +1,57 @@
var modName = "mods/save_loading.js"; 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() { function getSimulationState() {
var simulationState = { var simulationState = {
//currentPixels: currentPixels, //currentPixels: currentPixels,
pixelMap: pixelMap, pixelMap: structuredClone(pixelMap),
width: width, width: width,
height: height, height: height,
pixelSize: pixelSize, pixelSize: pixelSize,
settings: settings, settings: settings,
version: 0, version: 1,
enabledMods: localStorage.enabledMods, 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; return simulationState;
}; };
@ -192,6 +233,9 @@ function importJsonState(json) {
height = json.height; height = json.height;
pixelSize = json.pixelSize; pixelSize = json.pixelSize;
//currentPixels = json.currentPixels; //currentPixels = json.currentPixels;
for(i = 0; i < json.pixelMap.length; i++) {
json.pixelMap[i] = json.pixelMap[i].map(x => zeroToNull(x));
};
pixelMap = json.pixelMap; pixelMap = json.pixelMap;
if(json.settings) { if(json.settings) {
settings = json.settings; settings = json.settings;