113 lines
3.3 KiB
JavaScript
113 lines
3.3 KiB
JavaScript
let youthAgeModificators = function (abilityScores) {
|
||
abilityScores["Strength"] -= 2;
|
||
abilityScores["Intelligence"] -= 2;
|
||
abilityScores["Wisdom"] -= 2;
|
||
return abilityScores;
|
||
};
|
||
|
||
let adultAgeModificators = function (abilityScores) {
|
||
/* No adjustments for Adult */
|
||
return abilityScores;
|
||
};
|
||
|
||
let middleAgeModificators = function (abilityScores) {
|
||
abilityScores["Strength"] -= 2;
|
||
abilityScores["Dexterity"] -= 2;
|
||
abilityScores["Constitution"] -= 2;
|
||
return abilityScores;
|
||
};
|
||
|
||
let oldAgeModificators = function (abilityScores) {
|
||
abilityScores["Strength"] -= 4;
|
||
abilityScores["Dexterity"] -= 4;
|
||
abilityScores["Constitution"] -= 4;
|
||
abilityScores["Charisma"] -= 2;
|
||
return abilityScores;
|
||
};
|
||
|
||
let ancientAgeModificators = function (abilityScores) {
|
||
abilityScores["Strength"] -= 6;
|
||
abilityScores["Dexterity"] -= 6;
|
||
abilityScores["Constitution"] -= 6;
|
||
abilityScores["Charisma"] -= 4;
|
||
return abilityScores;
|
||
};
|
||
|
||
// Low Fantasy: реалистичный мир, где доля взрослых выше, а возрастные категории распределены ближе к реальности.
|
||
let ages = {
|
||
Youth: {
|
||
chance: 15.0,
|
||
explanation: "equivalent to human 13 – 17",
|
||
abilities_modificator: youthAgeModificators,
|
||
},
|
||
Adult: {
|
||
chance: 50.0,
|
||
explanation: "equivalent to human 18 – 35",
|
||
abilities_modificator: adultAgeModificators,
|
||
},
|
||
MiddleAged: {
|
||
chance: 20.0,
|
||
explanation: "equivalent to human 36 – 55",
|
||
abilities_modificator: middleAgeModificators,
|
||
},
|
||
Old: {
|
||
chance: 10.0,
|
||
explanation: "equivalent to human 56 – 75",
|
||
abilities_modificator: oldAgeModificators,
|
||
},
|
||
Ancient: {
|
||
chance: 5.0,
|
||
explanation: "equivalent to human 76 – 95",
|
||
abilities_modificator: ancientAgeModificators,
|
||
},
|
||
};
|
||
|
||
function getAgeAndModifyAbilities(abilityScores) {
|
||
// Клонируем словарь abilityScores, чтобы не изменять оригинал
|
||
let clonedAbilityScores = Object.assign({}, abilityScores);
|
||
|
||
// Получаем сумму шансов для нормализации
|
||
let totalChance = Object.values(ages).reduce(
|
||
(sum, { chance }) => sum + chance,
|
||
0
|
||
);
|
||
|
||
// Генерируем случайное значение от 0 до 100
|
||
let randomValue = Math.random() * 100;
|
||
|
||
let cumulativeChance = 0;
|
||
let selectedAgeGroup = null;
|
||
|
||
for (let [
|
||
ageGroup,
|
||
{ chance, explanation, abilities_modificator },
|
||
] of Object.entries(ages)) {
|
||
// Нормализуем шанс текущей возрастной категории
|
||
let normalizedChance = (chance / totalChance) * 100;
|
||
cumulativeChance += normalizedChance;
|
||
|
||
if (randomValue <= cumulativeChance) {
|
||
selectedAgeGroup = {
|
||
ageCategory: ageGroup,
|
||
ageExplanation: explanation,
|
||
abilities_modificator,
|
||
};
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!selectedAgeGroup) {
|
||
throw new Error("Age group selection failed.");
|
||
}
|
||
|
||
// Применяем модификаторы к клонированным способностям
|
||
let { ageCategory, ageExplanation, abilities_modificator } = selectedAgeGroup;
|
||
let agedAbilityScores = abilities_modificator(clonedAbilityScores);
|
||
|
||
return [ageCategory, ageExplanation, agedAbilityScores];
|
||
}
|
||
|
||
dv.ages = {
|
||
getAgeAndModifyAbilities,
|
||
};
|