294 lines
11 KiB
JavaScript
294 lines
11 KiB
JavaScript
var modName = "mods/spouts.js";
|
|
var runAfterAutogenMod = "mods/runAfterAutogen and onload restructure.js";
|
|
|
|
if(enabledMods.includes(runAfterAutogenMod)) {
|
|
urlParams = new URLSearchParams(window.location.search);
|
|
|
|
if(urlParams.get('spoutIncludeRandom') !== null) { //if the variable exists at all
|
|
spoutIncludeRandom = true
|
|
} else { //if it doesn't (and it returns null)
|
|
spoutIncludeRandom = false
|
|
}
|
|
|
|
function _randomInt(max) {
|
|
if(max >= 0) {
|
|
return Math.floor(Math.random() * (max + 1))
|
|
} else {
|
|
return 0 - Math.floor(Math.random() * (Math.abs(max) + 1))
|
|
}
|
|
}
|
|
|
|
excludedSpoutElements = ["ketchup", "liquid_cloner", "fire_cloner"]
|
|
includedSpouts = ["ketchup_spout", "spout", "udder", "torch", "sun"]
|
|
|
|
//Generator function
|
|
|
|
function tryJoin(stringOrArray,joiner) {
|
|
//console.log(`tryJoin: ${stringOrArray}`);
|
|
if(typeof(stringOrArray) === "string") {
|
|
//console.log("tryJoin: String");
|
|
return stringOrArray;
|
|
} else if(Array.isArray(stringOrArray)) {
|
|
//console.log("tryJoin: Array");
|
|
return stringOrArray.join(joiner);
|
|
} else {
|
|
throw new TypeError(`Unexpected type: ${typeof(stringOrArray)}`);
|
|
};
|
|
};
|
|
|
|
function bound(number,lowerBound,upperBound) {
|
|
return Math.min(upperBound,Math.max(lowerBound,number));
|
|
};
|
|
|
|
function rgbStringToObject(string,doRounding=true,doBounding=true) { //turns rgb() to {r,g,b} with no bounds/rounding/NaN checking
|
|
//console.log("Splitting string into object");
|
|
string = string.split(",");
|
|
if( (!string[0].startsWith("rgb(")) || (!string[2].endsWith(")")) ) {
|
|
throw new Error("Color must start with \"rgb(\" and end with \")\"");
|
|
};
|
|
var red = parseFloat(string[0].substring(4));
|
|
var green = parseFloat(string[1]);
|
|
var blue = parseFloat(string[2].slice(0,-1));
|
|
//console.log(`Colors loaded (${red}, ${green}, ${blue})`);
|
|
if(doRounding) {
|
|
red = Math.round(red);
|
|
green = Math.round(green);
|
|
blue = Math.round(blue);
|
|
//console.log(`Colors rounded to (${red}, ${green}, ${blue})`);
|
|
};
|
|
if(doBounding) {
|
|
red = bound(red,0,255)
|
|
green = bound(green,0,255)
|
|
blue = bound(blue,0,255)
|
|
//console.log(`Colors bounded to (${red}, ${green}, ${blue})`);
|
|
};
|
|
//console.log("String split: outputs " + red + ", " + green + ", " + blue + ".");
|
|
return {r: red, g: green, b: blue};
|
|
};
|
|
|
|
function sumArray(array) { //Sum of array numbers
|
|
return array.reduce((partialSum, a) => partialSum + a, 0);
|
|
};
|
|
|
|
function averageArray(array) { //Average of array numbers
|
|
return sumArray(array) / array.length;
|
|
};
|
|
|
|
function _rgbHexCatcher(color) { //Hex triplet to rgb(), while rgb() is untouched
|
|
//console.log("Logged color for _rgbHexCatcher: " + color);
|
|
//I have no idea if this runs before or after parsing hex triplets to rgb() values, so I'm going to handle both (by making everything rgb() and then making it hex at the end)
|
|
if(typeof(color) === "undefined") {
|
|
//console.log("Warning: An element has an undefined color. Unfortunately, due to how the code is structured, I can't say which one.");
|
|
color = "#FF00FF";
|
|
};
|
|
if(color.length < 10) {
|
|
//console.log("Short string detected, likely a hex triplet");
|
|
if(!color.startsWith("#")) {
|
|
color = "#" + color;
|
|
};
|
|
var object = hexToRGB(color);
|
|
return `rgb(${object.r},${object.g},${object.b})`
|
|
} else {
|
|
//console.log("Non-triplet detected");
|
|
return color;
|
|
};
|
|
};
|
|
|
|
function averageRgbPrefixedColorArray(colorArray,returnObject=false) { //array of rgb()s to single rgb() of average color
|
|
//console.log("Averaging started");
|
|
var reds = [];
|
|
var greens = [];
|
|
var blues = [];
|
|
for(k = 0; k < colorArray.length; k++) {
|
|
//console.log("Average function: Executing catcher on " + colorArray);
|
|
var color = _rgbHexCatcher(colorArray[k]);
|
|
//console.log("Logged color for aRPCA: " + color);
|
|
color = color.split(",");
|
|
var red = parseFloat(color[0].substring(4));
|
|
reds.push(red)
|
|
var green = parseFloat(color[1]);
|
|
greens.push(green)
|
|
var blue = parseFloat(color[2].slice(0,-1));
|
|
blues.push(blue)
|
|
};
|
|
redAverage = Math.round(averageArray(reds));
|
|
greenAverage = Math.round(averageArray(greens));
|
|
blueAverage = Math.round(averageArray(blues));
|
|
var output;
|
|
returnObject ? output = {r: redAverage, g: greenAverage, b: blueAverage} : output = `rgb(${redAverage},${greenAverage},${blueAverage})`;
|
|
//console.log("Averaging finished, product: " + output);
|
|
return output;
|
|
};
|
|
|
|
//Standalone generator
|
|
function generateSpout(spoutElements,isAfterScriptLoading=false) {//it can be a single element, though
|
|
//To specify an array spout, have the array be inside another array.
|
|
/*For reasons related to how element colors are loaded, if this function is being run from a JS mod file, isAfterScriptLoading should be false.
|
|
Otherwise, you'll get TypeErrors for some reason when trying to place your spout. If this is being run after the game has loaded (e.g. in the console),
|
|
then isAfterScriptLoading should be true or you might also get TypeErrors (this latter case was a bit inconsistent when I tested it, but
|
|
the former case wasn't. **isAfterScriptLoading must be false when this function is run from a JS mod file**.*/
|
|
if(typeof(spoutElements) === "string") { //it should be an array, so string check
|
|
//console.log("String detected");
|
|
if(spoutElements.includes(",")) { //comma-separated string?
|
|
//console.log("Splitting string to array");
|
|
spoutElements = spoutElements.split(","); //,SS to array
|
|
} else {
|
|
//console.log("Wrapping string in array");
|
|
spoutElements = [spoutElements]; //single string to array
|
|
};
|
|
};
|
|
for(aaf = 0; aaf < spoutElements.length; aaf++) {
|
|
var elementOfSpout = spoutElements[aaf];
|
|
var startColor;
|
|
var randomExcl = 0;
|
|
//console.log("randomExcl set")
|
|
//console.log(elementOfSpout);
|
|
|
|
var spoutName;
|
|
|
|
if(typeof(elementOfSpout === "string")) { //comma separated string check
|
|
if(elementOfSpout.includes(",")) { //if it is
|
|
elementOfSpout = elementOfSpout.split(","); //to array
|
|
elementOfSpout = elementOfSpout.filter(function(e) { //strip nonexistent elements
|
|
return typeof(elements[e]) === "object";
|
|
});
|
|
};
|
|
};
|
|
if(Array.isArray(elementOfSpout)) {
|
|
spoutName = `${elementOfSpout.join("_")}_spout`; //auto placer element name
|
|
|
|
//array case color concatenator and excludeRandom handler
|
|
startColor = [];
|
|
//console.log(elementOfSpout);
|
|
for(ll = 0; ll < elementOfSpout.length; ll++) {
|
|
if(typeof(elements[elementOfSpout[ll]].excludeRandom !== "undefined")) { //if excludeRandom exists (prevent TypeError)
|
|
if(elements[elementOfSpout[ll]].excludeRandom) { //it it's true
|
|
randomExcl = 1; //the whole array spout is excluded
|
|
//console.log("array nyet" + elementOfSpout);
|
|
};
|
|
};
|
|
//console.log(elementOfSpout[ll]);
|
|
startColor = startColor.concat(elements[elementOfSpout[ll]].color);
|
|
};
|
|
} else { //they should all be strings, so here
|
|
spoutName = `${elementOfSpout}_spout`; //auto placer element name
|
|
startColor = elements[elementOfSpout].color;
|
|
if(typeof(elements[elementOfSpout].excludeRandom !== "undefined")) { //if excludeRandom exists (prevent TypeError)
|
|
if(elements[elementOfSpout].excludeRandom) { //it it's true
|
|
//console.log("nyet " + elementOfSpout);
|
|
randomExcl = 1; //the spout is excluded
|
|
} else {
|
|
//console.log("allow " + elementOfSpout);
|
|
randomExcl = 0;
|
|
};
|
|
};
|
|
};
|
|
//Color gen
|
|
if(Array.isArray(startColor)) { //Average arrays, make colors rgb()
|
|
startColor = averageRgbPrefixedColorArray(startColor);
|
|
} else {
|
|
startColor = _rgbHexCatcher(startColor);
|
|
};
|
|
|
|
var newColorObject = rgbStringToObject(startColor);
|
|
|
|
//End color gen
|
|
|
|
//The spout
|
|
|
|
//console.log(elementOfSpout);
|
|
var firstInfo, firstTemp;
|
|
if(Array.isArray(elementOfSpout)) {
|
|
firstInfo = elements[elementOfSpout[0]];
|
|
firstTemp = airTemp;
|
|
if(typeof(firstInfo.temp) !== "undefined") {
|
|
firstTemp = firstInfo.temp;
|
|
};
|
|
} else {
|
|
firstInfo = elements[elementOfSpout];
|
|
firstTemp = airTemp;
|
|
if(typeof(firstInfo.temp) !== "undefined") {
|
|
firstTemp = firstInfo.temp;
|
|
};
|
|
};
|
|
|
|
elementOfSpout = tryJoin(elementOfSpout,",");
|
|
|
|
//console.log(elementOfSpout);
|
|
|
|
elements[spoutName] = {
|
|
color: startColor,
|
|
insulate: true,
|
|
colorObject: newColorObject,
|
|
behavior: [
|
|
["XX",`CR:${elementOfSpout}`,"XX"],
|
|
[`CR:${elementOfSpout}`,"XX",`CR:${elementOfSpout}`],
|
|
["XX",`CR:${elementOfSpout}`,"XX"]
|
|
],
|
|
category: "spouts",
|
|
temp: firstTemp,
|
|
hardness: 1,
|
|
};
|
|
if(!randomExcl) {
|
|
if(typeof(spoutChoices) === "undefined") {
|
|
spoutChoices = []
|
|
};
|
|
if(!spoutChoices.includes(spoutName)) {
|
|
spoutChoices.push(spoutName);
|
|
};
|
|
}
|
|
if(spoutIncludeRandom) {
|
|
randomExcl ? elements[spoutName].excludeRandom = true : elements[spoutName].excludeRandom = false;
|
|
} else {
|
|
elements[spoutName].excludeRandom = true;
|
|
};
|
|
};
|
|
};
|
|
|
|
runAfterAutogen(function() {
|
|
liquidArray = Object.keys(elements).filter(function(e) {
|
|
return (elements[e].state == "liquid" || elements[e].state == "gas" || elements[e].movable) && !excludedSpoutElements.includes(elements[e]);
|
|
});
|
|
/*for(i = 0; i < liquidArray.length; i++) {
|
|
elements[`${liquidArray[i]}_spout`] = {
|
|
color: elements[liquidArray[i]].color,
|
|
colorObject: elements[liquidArray[i]].colorObject,
|
|
behavior: [
|
|
["XX",`CR:${liquidArray[i]}`,"XX"],
|
|
[`CR:${liquidArray[i]}`,"XX",`CR:${liquidArray[i]}`],
|
|
["XX",`CR:${liquidArray[i]}`,"XX"]
|
|
],
|
|
category: "spouts",
|
|
temp: elements[liquidArray[i]].temp,
|
|
hardness: 1,
|
|
};
|
|
if(spoutIncludeRandom) {
|
|
elements[liquidArray[i]].excludeRandom ? elements[`${liquidArray[i]}_spout`].excludeRandom = true : elements[`${liquidArray[i]}_spout`].excludeRandom = false;
|
|
} else {
|
|
elements[`${liquidArray[i]}_spout`].excludeRandom = true;
|
|
};
|
|
};
|
|
spoutChoices = Object.keys(elements).filter(function(e) {
|
|
return elements[e].category == "spouts" || includedSpouts.includes(elements[e]);
|
|
});
|
|
spoutChoices = spoutChoices.filter(function(e) {
|
|
return !elements[e.slice(0,-6)].excludeRandom;
|
|
});*/
|
|
generateSpout(liquidArray,false);
|
|
});
|
|
|
|
elements.random_spout = {
|
|
color: ["#3e5f8a","#a334ec","#ea96f9","#a6ecf6","#70ebc8","#d9286b","#7eed91","#a18b30"],
|
|
behavior: behaviors.WALL,
|
|
category: "special",
|
|
excludeRandom: true,
|
|
tick: function(pixel) {
|
|
changePixel(pixel,spoutChoices[Math.floor(Math.random() * spoutChoices.length)])
|
|
},
|
|
};
|
|
} else {
|
|
alert(`The ${runAfterAutogenMod} mod is required and has been automatically inserted (reload for this to take effect).`)
|
|
enabledMods.splice(enabledMods.indexOf(modName),0,runAfterAutogenMod)
|
|
localStorage.setItem("enabledMods", JSON.stringify(enabledMods));
|
|
};
|