Renommage des fichiers
This commit is contained in:
109
javascript/planetGeneration.js
Normal file
109
javascript/planetGeneration.js
Normal file
@@ -0,0 +1,109 @@
|
||||
// Imports
|
||||
const stat = require("./planetStat.js");
|
||||
|
||||
// Simple functions to get random values
|
||||
const getRandomValue = (min, max) => {return Math.floor(Math.random() * (max - min + 1)) + min;}
|
||||
const Random = (array) => {return array[Math.floor(Math.random() * array.length)];}
|
||||
const RandomPlanet = (planet) => {
|
||||
const keys = Object.keys(planet);
|
||||
return keys[Math.floor(Math.random() * keys.length)];
|
||||
}
|
||||
|
||||
// Main function generating planets
|
||||
const GeneratePlanet = () => {
|
||||
const planetTypeGeneration = RandomPlanet(stat.planetType);
|
||||
const planetName = Random(stat.planetNamePrefix) + Random(stat.planetNameSuffix);
|
||||
const planetPopulationPeople = Random(stat.planetType[planetTypeGeneration]["populationType"]);
|
||||
const planetPopulationNumber = stat.planetType[planetTypeGeneration]["population"] * getRandomValue(50, 150) / 99.9;
|
||||
|
||||
// Elements and resources generation
|
||||
// Elements
|
||||
const planetElements = stat.planetType[planetTypeGeneration].distributedElements;
|
||||
let elementsDescription = '';
|
||||
for (let element in planetElements) {
|
||||
elementsDescription += `
|
||||
${element}: ${planetElements[element].toFixed(1)}%`;
|
||||
}
|
||||
// Resources
|
||||
const planetResources = stat.planetType[planetTypeGeneration].distributedResources;
|
||||
let resourcesDescription = '';
|
||||
for (let resource in planetResources) {
|
||||
resourcesDescription += `
|
||||
${resource}: ${planetResources[resource].toFixed(3)}%`;
|
||||
}
|
||||
|
||||
// Description = planet formatting
|
||||
const planetDescription =
|
||||
// Keep this formatting, or else play with the console.log of func PlanetLoop
|
||||
(`
|
||||
Planète : ${planetName}
|
||||
|
||||
Type : ${planetTypeGeneration}
|
||||
|
||||
Production :
|
||||
${elementsDescription}
|
||||
|
||||
Ressources naturelles :
|
||||
${resourcesDescription}
|
||||
|
||||
Population :
|
||||
|
||||
${planetPopulationNumber.toFixed(3)} milliards
|
||||
Majoritairement ${planetPopulationPeople}`);
|
||||
return planetDescription;
|
||||
}
|
||||
|
||||
// Generating planets in loop
|
||||
const PlanetLoop = (number) => {
|
||||
let count = 1;
|
||||
while (count <= number) {
|
||||
const planet = GeneratePlanet();
|
||||
console.log(planet + `\n\n------------------------------`);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply distribution to the planet types
|
||||
const distributeElementsRandomly = (elements) => {
|
||||
if (elements.length === 0) return {};
|
||||
if (elements.length === 1) return { [elements[0]]: 100 }
|
||||
|
||||
// Generate random percentages for each element except the last one
|
||||
let percentages = Array.from({ length: elements.length - 1 }, () => Math.random());
|
||||
|
||||
// Sort the random numbers and use them to determine the boundaries of the segments
|
||||
percentages.sort((a, b) => a - b);
|
||||
|
||||
// Convert these sorted random numbers into percentage segments
|
||||
const distributedPercentages = percentages.map((p, i) => {
|
||||
if (i === 0) return p * 100; // First segment from 0 to the first random number
|
||||
else return (percentages[i] - percentages[i - 1]) * 100; // Segments between random numbers
|
||||
});
|
||||
|
||||
// The last segment goes from the last random number to 1
|
||||
distributedPercentages.push((1 - percentages[elements.length - 2]) * 100);
|
||||
|
||||
return Object.fromEntries(elements.map((element, index) => [element, distributedPercentages[index]]));
|
||||
}
|
||||
|
||||
const distributePlanetTypesRandomly = (planetType) => {
|
||||
for (const key in planetType) {
|
||||
if (Array.isArray(planetType[key]["elements"])) {
|
||||
const elements = planetType[key]["elements"];
|
||||
const distributedElements = distributeElementsRandomly(elements);
|
||||
planetType[key].distributedElements = distributedElements;
|
||||
}
|
||||
|
||||
if (Array.isArray(planetType[key]["resources"])) {
|
||||
const resources = planetType[key]["resources"];
|
||||
const distributedResources = distributeElementsRandomly(resources);
|
||||
planetType[key].distributedResources = distributedResources;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply distribution to your planet types
|
||||
distributePlanetTypesRandomly(stat.planetType);
|
||||
|
||||
// Modules exports
|
||||
exports.PlanetLoop = PlanetLoop;
|
||||
Reference in New Issue
Block a user