2021-07-13 02:03:10 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-08-25 03:31:30 +02:00
|
|
|
import { mathEvaluator } from "../expression-evaluation/evaluator";
|
2022-11-04 21:47:18 +01:00
|
|
|
import { getGame } from "../utils/utils";
|
2021-07-13 02:03:10 +02:00
|
|
|
|
2022-11-17 00:12:29 +01:00
|
|
|
/**
|
|
|
|
* @typedef {object} ItemEffectConfig
|
|
|
|
* @property {boolean} [applyToItems] Whether or not to apply this effect to owned items instead of the actor
|
|
|
|
* @property {string} [itemName] Only apply this effect to items with this name
|
|
|
|
* @property {string} [condition] Only apply this effect to items where this condition is fullfilled
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} DS4ActiveEffectFlags
|
|
|
|
* @property {ItemEffectConfig} [itemEffectConfig] Configuration for applying this effect to owned items
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Record<string, unknown>} ActiveEffectFlags
|
|
|
|
* @property {DS4ActiveEffectFlags} [ds4] Flags for DS4
|
|
|
|
*/
|
2021-08-19 03:04:40 +02:00
|
|
|
|
2021-07-13 02:03:10 +02:00
|
|
|
export class DS4ActiveEffect extends ActiveEffect {
|
2021-07-23 17:32:26 +02:00
|
|
|
/**
|
|
|
|
* A fallback icon that can be used if no icon is defined for the effect.
|
|
|
|
*/
|
|
|
|
static FALLBACK_ICON = "icons/svg/aura.svg";
|
|
|
|
|
2021-07-23 12:29:01 +02:00
|
|
|
/**
|
|
|
|
* A cached reference to the source document to avoid recurring database lookups
|
2022-11-17 00:12:29 +01:00
|
|
|
* @type {foundry.abstract.Document | undefined | null}
|
|
|
|
* @protected
|
2021-07-23 12:29:01 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
source = undefined;
|
2021-07-23 12:29:01 +02:00
|
|
|
|
2021-08-19 03:04:40 +02:00
|
|
|
/**
|
|
|
|
* Whether or not this effect is currently surpressed.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @type {boolean}
|
2021-08-19 03:04:40 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
get isSurpressed() {
|
2021-08-19 03:04:40 +02:00
|
|
|
const originatingItem = this.originatingItem;
|
|
|
|
if (!originatingItem) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return originatingItem.isNonEquippedEuipable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The item which this effect originates from if it has been transferred from an item to an actor.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @return {import("./item/item").DS4Item | undefined}
|
2021-08-19 03:04:40 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
get originatingItem() {
|
2022-11-03 21:41:44 +01:00
|
|
|
if (!(this.parent instanceof Actor)) {
|
2021-08-19 03:04:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-23 12:45:53 +02:00
|
|
|
const itemIdRegex = /Item\.([a-zA-Z0-9]+)/;
|
2022-11-21 03:00:46 +01:00
|
|
|
const itemId = this.origin?.match(itemIdRegex)?.[1];
|
2021-08-19 03:04:40 +02:00
|
|
|
if (!itemId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return this.parent.items.get(itemId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of times this effect should be applied.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @type {number}
|
2021-08-19 03:04:40 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
get factor() {
|
2021-08-19 03:04:40 +02:00
|
|
|
return this.originatingItem?.activeEffectFactor ?? 1;
|
|
|
|
}
|
|
|
|
|
2022-11-17 00:12:29 +01:00
|
|
|
/** @override */
|
|
|
|
apply(document, change) {
|
2022-11-21 03:00:46 +01:00
|
|
|
change.value = Roll.replaceFormulaData(change.value, document);
|
2021-07-13 02:03:10 +02:00
|
|
|
try {
|
2022-10-31 22:58:04 +01:00
|
|
|
change.value = DS4ActiveEffect.safeEval(change.value).toString();
|
2021-07-13 02:03:10 +02:00
|
|
|
} catch (e) {
|
|
|
|
// this is a valid case, e.g., if the effect change simply is a string
|
|
|
|
}
|
2022-08-25 03:31:30 +02:00
|
|
|
return super.apply(document, change);
|
2021-07-13 02:03:10 +02:00
|
|
|
}
|
2021-07-20 02:35:55 +02:00
|
|
|
|
|
|
|
/**
|
2021-07-23 12:29:01 +02:00
|
|
|
* Gets the current source name based on the cached source object.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @returns {Promise<string>} The current source name
|
2021-07-23 12:29:01 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
async getCurrentSourceName() {
|
2021-07-23 12:29:01 +02:00
|
|
|
const game = getGame();
|
|
|
|
const origin = await this.getSource();
|
|
|
|
if (origin === null) return game.i18n.localize("None");
|
|
|
|
return origin.name ?? game.i18n.localize("Unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-22 14:15:39 +01:00
|
|
|
* Gets the source document for this effect. Uses the cached {@link DS4ActiveEffect#source} if it has already been
|
2021-07-23 12:29:01 +02:00
|
|
|
* set.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @protected
|
|
|
|
* @returns {Promise<foundry.abstract.Document | null>}
|
2021-07-20 02:35:55 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
async getSource() {
|
2021-07-23 12:29:01 +02:00
|
|
|
if (this.source === undefined) {
|
2022-11-21 03:00:46 +01:00
|
|
|
this.source = this.origin != null ? await fromUuid(this.origin) : null;
|
2021-07-23 12:29:01 +02:00
|
|
|
}
|
|
|
|
return this.source;
|
2021-07-20 02:35:55 +02:00
|
|
|
}
|
2021-08-19 03:04:40 +02:00
|
|
|
|
|
|
|
/**
|
2022-11-21 03:00:46 +01:00
|
|
|
* Create a new {@link DS4ActiveEffect} using default values.
|
2021-08-19 03:04:40 +02:00
|
|
|
*
|
2022-11-17 00:12:29 +01:00
|
|
|
* @param {import("./item/item").DS4Item | import("./actor/actor").DS4Actor} parent The parent of the effect.
|
2022-11-21 03:00:46 +01:00
|
|
|
* @returns {Promise<DS4ActiveEffect | undefined>} A promise that resolved to the created effect or udifined of the
|
|
|
|
* creation was prevented.
|
2021-08-19 03:04:40 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
static async createDefault(parent) {
|
2021-08-19 03:04:40 +02:00
|
|
|
const createData = {
|
|
|
|
label: getGame().i18n.localize(`DS4.NewEffectLabel`),
|
|
|
|
icon: this.FALLBACK_ICON,
|
|
|
|
};
|
2021-09-22 12:32:18 +02:00
|
|
|
|
|
|
|
return this.create(createData, { parent, pack: parent.pack ?? undefined });
|
2021-08-19 03:04:40 +02:00
|
|
|
}
|
2022-10-31 22:58:04 +01:00
|
|
|
|
2022-11-17 00:12:29 +01:00
|
|
|
/**
|
|
|
|
* Safely evaluate a mathematical expression.
|
|
|
|
* @param {string} expression The expression to evaluate
|
|
|
|
* @returns {number | `${number | boolean}`} The numeric result of the expression
|
|
|
|
* @throws If the expression could not be evaluated or did not produce a numeric resilt
|
|
|
|
*/
|
|
|
|
static safeEval(expression) {
|
2022-10-31 22:58:04 +01:00
|
|
|
const result = mathEvaluator.evaluate(expression);
|
|
|
|
if (!Number.isNumeric(result)) {
|
|
|
|
throw new Error(`mathEvaluator.evaluate produced a non-numeric result from expression "${expression}"`);
|
|
|
|
}
|
2022-11-17 00:12:29 +01:00
|
|
|
return result;
|
2022-10-31 22:58:04 +01:00
|
|
|
}
|
2022-08-25 03:31:30 +02:00
|
|
|
|
2022-11-03 21:41:44 +01:00
|
|
|
/**
|
|
|
|
* Apply the given effects to the gicen Actor or item.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @param {import("./item/item").DS4Item | import("./actor/actor").DS4Actor} document The Actor or Item to which to apply the effects
|
|
|
|
* @param {DS4ActiveEffect[]} effetcs The effects to apply
|
|
|
|
* @param {(change: EffectChangeData) => boolean} [predicate=() => true] Apply only changes that fullfill this predicate
|
2022-11-03 21:41:44 +01:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
static applyEffetcs(document, effetcs, predicate = () => true) {
|
|
|
|
/** @type {Record<string, unknown>} */
|
|
|
|
const overrides = {};
|
2022-11-03 21:41:44 +01:00
|
|
|
|
|
|
|
// Organize non-disabled and -surpressed effects by their application priority
|
|
|
|
const changesWithEffect = effetcs.flatMap((e) => e.getFactoredChangesWithEffect(predicate));
|
|
|
|
changesWithEffect.sort((a, b) => (a.change.priority ?? 0) - (b.change.priority ?? 0));
|
|
|
|
|
|
|
|
// Apply all changes
|
|
|
|
for (const changeWithEffect of changesWithEffect) {
|
2022-12-02 00:00:39 +01:00
|
|
|
if (!changeWithEffect.change.key) continue;
|
|
|
|
const changes = changeWithEffect.effect.apply(document, changeWithEffect.change);
|
|
|
|
Object.assign(overrides, changes);
|
2022-11-03 21:41:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expand the set of final overrides
|
|
|
|
document.overrides = foundry.utils.expandObject({
|
|
|
|
...foundry.utils.flattenObject(document.overrides),
|
|
|
|
...overrides,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-25 03:31:30 +02:00
|
|
|
/**
|
|
|
|
* Get the array of changes for this effect, considering the {@link DS4ActiveEffect#factor}.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @param {(change: EffectChangeData) => boolean} [predicate=() => true] An optional predicate to filter which changes should be considered
|
|
|
|
* @returns {EffectChangeDataWithEffect[]} The array of changes from this effect, considering the factor.
|
|
|
|
* @protected
|
2022-08-25 03:31:30 +02:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
getFactoredChangesWithEffect(predicate = () => true) {
|
2022-11-21 03:00:46 +01:00
|
|
|
if (this.disabled || this.isSurpressed) {
|
2022-08-25 03:31:30 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-11-21 03:00:46 +01:00
|
|
|
return this.changes.filter(predicate).flatMap((change) => {
|
2022-08-25 03:31:30 +02:00
|
|
|
change.priority = change.priority ?? change.mode * 10;
|
2022-11-17 00:12:29 +01:00
|
|
|
return Array(this.factor).fill({ effect: this, change });
|
2022-08-25 03:31:30 +02:00
|
|
|
});
|
|
|
|
}
|
2021-07-13 02:03:10 +02:00
|
|
|
}
|
2022-08-25 03:31:30 +02:00
|
|
|
|
2022-11-17 00:12:29 +01:00
|
|
|
/**
|
|
|
|
* @typedef {object} EffectChangeDataWithEffect
|
|
|
|
* @property {DS4ActiveEffect} effect
|
|
|
|
* @property {EffectChangeData} change
|
|
|
|
*/
|
2022-11-21 03:00:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} EffectChangeData
|
|
|
|
* @property {string} key The attribute path in the Actor or Item data which the change modifies
|
|
|
|
* @property {string} value The value of the change effect
|
|
|
|
* @property {number} mode The modification mode with which the change is applied
|
|
|
|
* @property {number} priority The priority level with which this change is applied
|
|
|
|
*/
|