2021-03-25 00:14:56 +01:00
|
|
|
import { Grammar } from "./grammar.js";
|
2021-03-29 18:08:18 +02:00
|
|
|
import { Misc } from "./misc.js";
|
2021-03-25 00:14:56 +01:00
|
|
|
|
2021-01-08 22:23:50 +01:00
|
|
|
const competenceTroncs = [["Esquive", "Dague", "Corps à corps"],
|
|
|
|
["Epée à 1 main", "Epée à 2 mains", "Hache à 1 main", "Hache à 2 mains", "Lance", "Masse à 1 main", "Masse à 2 mains"]];
|
|
|
|
|
2021-03-29 18:08:18 +02:00
|
|
|
const xp_par_niveau = [5, 5, 5, 10, 10, 10, 10, 15, 15, 15, 15, 20, 20, 20, 20, 30, 30, 40, 40, 60, 60, 100, 100, 100, 100, 100, 100, 100, 100, 100];
|
|
|
|
const niveau_max = xp_par_niveau.length - 10;
|
2021-03-17 01:21:37 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
const limitesArchetypes = [
|
|
|
|
{ "niveau": 0, "nombreMax": 100, "nombre": 0 },
|
|
|
|
{ "niveau": 1, "nombreMax": 10, "nombre": 0 },
|
|
|
|
{ "niveau": 2, "nombreMax": 9, "nombre": 0 },
|
|
|
|
{ "niveau": 3, "nombreMax": 8, "nombre": 0 },
|
|
|
|
{ "niveau": 4, "nombreMax": 7, "nombre": 0 },
|
|
|
|
{ "niveau": 5, "nombreMax": 6, "nombre": 0 },
|
|
|
|
{ "niveau": 6, "nombreMax": 5, "nombre": 0 },
|
|
|
|
{ "niveau": 7, "nombreMax": 4, "nombre": 0 },
|
|
|
|
{ "niveau": 8, "nombreMax": 3, "nombre": 0 },
|
|
|
|
{ "niveau": 9, "nombreMax": 2, "nombre": 0 },
|
|
|
|
{ "niveau": 10, "nombreMax": 1, "nombre": 0 },
|
|
|
|
{ "niveau": 11, "nombreMax": 1, "nombre": 0 }
|
|
|
|
];
|
2021-01-21 00:05:22 +01:00
|
|
|
|
2021-03-15 00:07:53 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
const categorieCompetences = {
|
2021-03-29 14:41:25 +02:00
|
|
|
"generale": { base: -4, label: "Générales" },
|
|
|
|
"particuliere": { base: -8, label: "Particulières" },
|
|
|
|
"specialisee": { base: -11, label: "Spécialisées" },
|
|
|
|
"connaissance": { base: -11, label: "Connaissances" },
|
|
|
|
"draconic": { base: -11, label: "Draconics" },
|
|
|
|
"melee": { base: -6, label: "Mêlée" },
|
|
|
|
"tir": { base: -8, label: "Tir" },
|
|
|
|
"lancer": { base: -8, label: "Lancer" }
|
2021-03-15 00:07:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:20:01 +01:00
|
|
|
const compendiumCompetences = {
|
|
|
|
"personnage": "foundryvtt-reve-de-dragon.competences",
|
|
|
|
"creature": "foundryvtt-reve-de-dragon.competences-creatures",
|
|
|
|
"entite": "foundryvtt-reve-de-dragon.competences-entites"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-21 00:05:22 +01:00
|
|
|
function _buildCumulXP() {
|
|
|
|
let cumulXP = { "-11": 0 };
|
|
|
|
let cumul = 0;
|
2021-03-29 18:08:18 +02:00
|
|
|
for (let i = 0; i <= xp_par_niveau.length; i++) {
|
2021-01-21 00:05:22 +01:00
|
|
|
let level = i - 10;
|
2021-03-29 18:08:18 +02:00
|
|
|
cumul += xp_par_niveau[i];
|
2021-01-21 00:05:22 +01:00
|
|
|
cumulXP[level] = cumul;
|
|
|
|
}
|
|
|
|
return cumulXP;
|
|
|
|
}
|
|
|
|
|
|
|
|
const competence_xp_cumul = _buildCumulXP();
|
2021-01-08 22:23:50 +01:00
|
|
|
|
2020-12-12 21:58:44 +01:00
|
|
|
export class RdDItemCompetence extends Item {
|
|
|
|
|
2021-03-25 17:51:03 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-03-19 22:20:01 +01:00
|
|
|
static actorCompendium(actorType) {
|
|
|
|
return compendiumCompetences[actorType];
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:51:03 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-03-15 00:07:53 +01:00
|
|
|
static getCategorieCompetences() {
|
|
|
|
return categorieCompetences;
|
|
|
|
}
|
2021-03-25 17:51:03 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-03-17 01:21:37 +01:00
|
|
|
static getNiveauBase(category) {
|
2021-04-04 18:37:16 +02:00
|
|
|
return categorieCompetences[category].base;
|
2021-03-15 00:07:53 +01:00
|
|
|
}
|
2021-03-25 17:51:03 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-03-17 01:21:37 +01:00
|
|
|
static getLabelCategorie(category) {
|
2021-03-15 00:07:53 +01:00
|
|
|
return categorieCompetences[category].label;
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:08:18 +02:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static getCategorie(competence) {
|
|
|
|
return Misc.data(competence)?.data.categorie;
|
|
|
|
}
|
|
|
|
static isDraconic(competence) {
|
2021-11-20 01:04:22 +01:00
|
|
|
return Misc.data(competence)?.data.categorie == 'draconic';
|
2021-03-29 18:08:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static getVoieDraconic(competences, voie) {
|
2021-11-23 02:11:24 +01:00
|
|
|
return RdDItemCompetence.findCompetence(competences.filter(it => RdDItemCompetence.isDraconic(it)), voie);
|
2021-03-18 23:55:26 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 21:58:44 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-08 22:23:50 +01:00
|
|
|
static isCompetenceArme(competence) {
|
2021-03-29 18:08:18 +02:00
|
|
|
switch (Misc.templateData(competence).categorie) {
|
2021-01-08 22:23:50 +01:00
|
|
|
case 'melee':
|
2021-03-29 18:08:18 +02:00
|
|
|
return Misc.data(competence).name != 'Esquive';
|
2021-01-08 22:23:50 +01:00
|
|
|
case 'tir':
|
|
|
|
case 'lancer':
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-12 21:58:44 +01:00
|
|
|
}
|
2021-01-08 22:23:50 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2020-12-12 21:58:44 +01:00
|
|
|
static isArmeUneMain(competence) {
|
2021-03-29 18:08:18 +02:00
|
|
|
return Misc.data(competence)?.name.toLowerCase().includes("1 main");
|
2021-01-08 22:23:50 +01:00
|
|
|
}
|
|
|
|
static isArme2Main(competence) {
|
2021-03-29 18:08:18 +02:00
|
|
|
return Misc.data(competence)?.name.toLowerCase().includes("2 main");
|
2020-12-12 21:58:44 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:23:50 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-20 21:54:09 +01:00
|
|
|
static isMalusEncombrementTotal(competence) {
|
2021-03-29 18:08:18 +02:00
|
|
|
return Misc.data(competence)?.name.toLowerCase().match(/(natation|acrobatie)/);
|
2020-12-20 21:54:09 +01:00
|
|
|
}
|
2021-01-01 22:25:32 +01:00
|
|
|
|
2021-01-08 22:23:50 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-03-22 23:36:46 +01:00
|
|
|
static getListTronc(compName) {
|
2021-01-08 22:23:50 +01:00
|
|
|
for (let troncList of competenceTroncs) {
|
|
|
|
for (let troncName of troncList) {
|
|
|
|
if (troncName == compName)
|
|
|
|
return troncList;
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 23:36:46 +01:00
|
|
|
return [];
|
2021-01-08 22:23:50 +01:00
|
|
|
}
|
2021-03-17 01:21:37 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static computeTotalXP(competences) {
|
|
|
|
const total = competences.map(c => RdDItemCompetence.computeXP(c))
|
2021-03-29 18:08:18 +02:00
|
|
|
.reduce(Misc.sum(), 0);
|
2021-03-17 01:21:37 +01:00
|
|
|
const economieTronc = RdDItemCompetence.computeEconomieXPTronc(competences);
|
|
|
|
return total - economieTronc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static computeXP(competence) {
|
2021-03-29 18:08:18 +02:00
|
|
|
const itemData = Misc.data(competence);
|
|
|
|
const factor = itemData.name.includes('Thanatos') ? 2 : 1; // Thanatos compte double !
|
|
|
|
const xpNiveau = RdDItemCompetence.computeDeltaXP(itemData.data.base, itemData.data.niveau ?? itemData.data.base);
|
|
|
|
const xp = itemData.data.xp ?? 0;
|
|
|
|
const xpSort = itemData.data.xp_sort ?? 0;
|
2021-03-17 01:21:37 +01:00
|
|
|
return factor * (xpNiveau + xp) + xpSort;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static computeEconomieXPTronc(competences) {
|
|
|
|
return competenceTroncs.map(
|
|
|
|
list => list.map(name => RdDItemCompetence.findCompetence(competences, name))
|
2021-11-23 02:11:24 +01:00
|
|
|
// calcul du coût xp jusqu'au niveau 0 maximum
|
|
|
|
.map(it => RdDItemCompetence.computeDeltaXP(it?.data.base ?? -11, Math.min(it?.data.niveau ?? -11, 0)))
|
|
|
|
.sort(Misc.ascending())
|
|
|
|
.splice(0, list.length - 1) // prendre toutes les valeurs sauf l'une des plus élevées
|
|
|
|
.reduce(Misc.sum(), 0)
|
2021-03-29 18:08:18 +02:00
|
|
|
).reduce(Misc.sum(), 0);
|
2021-03-17 01:21:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static computeDeltaXP(from, to) {
|
|
|
|
RdDItemCompetence._valideNiveau(from);
|
|
|
|
RdDItemCompetence._valideNiveau(to);
|
|
|
|
return competence_xp_cumul[to] - competence_xp_cumul[from];
|
|
|
|
}
|
|
|
|
|
2021-01-08 22:23:50 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static computeCompetenceXPCost(competence) {
|
2021-03-29 18:08:18 +02:00
|
|
|
const compData = Misc.data(competence);
|
|
|
|
let xp = RdDItemCompetence.getDeltaXp(compData.data.base, compData.data.niveau ?? compData.data.base);
|
|
|
|
xp += compData.data.xp ?? 0;
|
|
|
|
if (compData.name.includes('Thanatos')) xp *= 2; /// Thanatos compte double !
|
|
|
|
xp += compData.data.xp_sort ?? 0;
|
2021-01-08 22:23:50 +01:00
|
|
|
return xp;
|
|
|
|
}
|
|
|
|
|
2021-02-06 23:51:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-21 00:05:22 +01:00
|
|
|
static computeEconomieCompetenceTroncXP(competences) {
|
|
|
|
let economie = 0;
|
2021-01-08 22:23:50 +01:00
|
|
|
for (let troncList of competenceTroncs) {
|
2021-01-21 00:05:22 +01:00
|
|
|
let list = troncList.map(name => RdDItemCompetence.findCompetence(competences, name))
|
2021-03-29 18:08:18 +02:00
|
|
|
.sort(Misc.descending(c => Misc.templateData(c).niveau)); // tri du plus haut au plus bas
|
2021-03-22 20:10:37 +01:00
|
|
|
list.splice(0, 1); // ignorer la plus élevée
|
2021-03-29 18:08:18 +02:00
|
|
|
list.map(c => Misc.templateData(c)).forEach(tplData => {
|
|
|
|
economie += RdDItemCompetence.getDeltaXp(tplData.base, Math.min(tplData.niveau, 0));
|
2021-01-21 00:05:22 +01:00
|
|
|
});
|
2021-01-08 22:23:50 +01:00
|
|
|
}
|
2021-01-21 00:05:22 +01:00
|
|
|
return economie;
|
2021-01-08 22:23:50 +01:00
|
|
|
}
|
2021-01-21 00:05:22 +01:00
|
|
|
|
2021-03-25 17:51:03 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-11-23 02:11:24 +01:00
|
|
|
static levelUp(itemData, stressTransforme) {
|
2021-03-22 20:10:37 +01:00
|
|
|
itemData.data.xpNext = RdDItemCompetence.getCompetenceNextXp(itemData.data.niveau);
|
2021-11-23 02:11:24 +01:00
|
|
|
const xpManquant = itemData.data.xpNext - itemData.data.xp;
|
|
|
|
itemData.data.isLevelUp = xpManquant <= 0;
|
|
|
|
itemData.data.isStressLevelUp = (xpManquant > 0 && stressTransforme >= xpManquant && itemData.data.niveau < itemData.data.niveau_archetype);
|
|
|
|
itemData.data.stressXpMax = 0;
|
|
|
|
if (xpManquant > 0 && stressTransforme > 0 && itemData.data.niveau < itemData.data.niveau_archetype) {
|
|
|
|
itemData.data.stressXpMax = Math.min(xpManquant , stressTransforme);
|
|
|
|
}
|
2021-03-17 01:21:37 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:51:03 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-03-22 20:10:37 +01:00
|
|
|
static isVisible(itemData) {
|
|
|
|
return Number(itemData.data.niveau) != RdDItemCompetence.getNiveauBase(itemData.data.categorie);
|
2021-03-17 01:21:37 +01:00
|
|
|
}
|
2021-03-22 20:10:37 +01:00
|
|
|
|
2021-11-26 01:02:44 +01:00
|
|
|
static nomContientTexte(itemData, texte) {
|
|
|
|
return Grammar.toLowerCaseNoAccent(itemData.name).includes(Grammar.toLowerCaseNoAccent(texte))
|
|
|
|
}
|
|
|
|
|
2021-03-17 01:21:37 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-03-22 20:10:37 +01:00
|
|
|
static isNiveauBase(itemData) {
|
|
|
|
return Number(itemData.data.niveau) == RdDItemCompetence.getNiveauBase(itemData.data.categorie);
|
2021-03-17 01:21:37 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:23:50 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-11-20 01:04:22 +01:00
|
|
|
static findCompetence(list, idOrName, options = {}) {
|
|
|
|
options = mergeObject(options, {
|
|
|
|
filter: it => RdDItemCompetence.isCompetence(it),
|
|
|
|
description: 'compétence',
|
|
|
|
});
|
|
|
|
return list.find(it => it.id == idOrName && RdDItemCompetence.isCompetence(it))
|
|
|
|
?? Misc.findFirstLike(idOrName, list, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static findCompetences(list, name) {
|
|
|
|
return Misc.findAllLike(name, list, { filter: it => RdDItemCompetence.isCompetence(it), description: 'compétence' });
|
|
|
|
}
|
|
|
|
|
|
|
|
static isCompetence(item) {
|
|
|
|
return item.type == 'competence' || item.type == 'competencecreature';
|
2021-01-08 22:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static getCompetenceNextXp(niveau) {
|
2021-01-21 00:05:22 +01:00
|
|
|
return RdDItemCompetence.getCompetenceXp(niveau + 1);
|
|
|
|
}
|
|
|
|
|
2021-02-06 23:51:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-21 00:05:22 +01:00
|
|
|
static getCompetenceXp(niveau) {
|
|
|
|
RdDItemCompetence._valideNiveau(niveau);
|
2021-03-29 18:08:18 +02:00
|
|
|
return niveau < -10 ? 0 : xp_par_niveau[niveau + 10];
|
2021-01-21 00:05:22 +01:00
|
|
|
}
|
|
|
|
|
2021-02-06 23:51:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-21 00:05:22 +01:00
|
|
|
static getDeltaXp(from, to) {
|
|
|
|
RdDItemCompetence._valideNiveau(from);
|
|
|
|
RdDItemCompetence._valideNiveau(to);
|
|
|
|
return competence_xp_cumul[to] - competence_xp_cumul[from];
|
|
|
|
}
|
|
|
|
|
2021-02-06 23:51:04 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-03-22 20:10:37 +01:00
|
|
|
static _valideNiveau(niveau) {
|
2021-03-29 18:08:18 +02:00
|
|
|
if (niveau < -11 || niveau > niveau_max) {
|
|
|
|
console.warn(`Niveau ${niveau} en dehors des niveaux de compétences: [-11, ${niveau_max} ]`);
|
2021-01-21 00:05:22 +01:00
|
|
|
}
|
2021-01-08 22:23:50 +01:00
|
|
|
}
|
2021-03-22 20:10:37 +01:00
|
|
|
|
2021-03-17 01:21:37 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static computeResumeArchetype(competences) {
|
|
|
|
const archetype = RdDItemCompetence.getLimitesArchetypes();
|
2021-03-29 18:08:18 +02:00
|
|
|
competences.map(it => Math.max(0, Misc.templateData(it).niveau_archetype))
|
|
|
|
.forEach(niveau => {
|
|
|
|
archetype[niveau] = archetype[niveau] ?? { "niveau": niveau, "nombreMax": 0, "nombre": 0 };
|
|
|
|
archetype[niveau].nombre = (archetype[niveau]?.nombre ?? 0) + 1;
|
|
|
|
});
|
2021-03-17 01:21:37 +01:00
|
|
|
return archetype;
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:10:37 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static getLimitesArchetypes() {
|
|
|
|
return duplicate(limitesArchetypes);
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:58:44 +01:00
|
|
|
}
|