ds4/src/module/actor/actor.ts

288 lines
12 KiB
TypeScript
Raw Normal View History

import { ModifiableDataBaseTotal } from "../common/common-data";
2021-01-25 01:09:51 +01:00
import { DS4 } from "../config";
2020-12-29 00:33:43 +01:00
import { DS4Item } from "../item/item";
import { ItemType } from "../item/item-data";
import { createCheckRoll } from "../rolls/check-factory";
2021-01-26 03:55:18 +01:00
import { DS4ActorData } from "./actor-data";
import { Check, DS4ActorPreparedData } from "./actor-prepared-data";
2020-12-23 18:23:26 +01:00
2021-02-07 11:51:36 +01:00
/**
* The Actor class for DS4
*/
export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData> {
2021-01-25 01:09:51 +01:00
/** @override */
prepareData(): void {
this.data = duplicate(this._data) as DS4ActorPreparedData;
2021-01-25 01:09:51 +01:00
if (!this.data.img) this.data.img = CONST.DEFAULT_TOKEN;
if (!this.data.name) this.data.name = "New " + this.entity;
this.prepareBaseData();
this.prepareEmbeddedEntities();
2021-02-16 16:34:23 +01:00
this.applyActiveEffectsToBaseData();
2021-01-25 01:09:51 +01:00
this.prepareDerivedData();
this.applyActiveEffectsToDerivedData();
this.prepareFinalDerivedData();
2021-01-25 01:09:51 +01:00
}
/** @override */
prepareBaseData(): void {
const data = this.data;
2021-02-16 16:34:23 +01:00
data.data.rolling = {
minimumFumbleResult: 20,
maximumCoupResult: 1,
};
const attributes = data.data.attributes;
Object.values(attributes).forEach(
(attribute: ModifiableDataBaseTotal<number>) => (attribute.total = attribute.base + attribute.mod),
);
const traits = data.data.traits;
Object.values(traits).forEach(
(trait: ModifiableDataBaseTotal<number>) => (trait.total = trait.base + trait.mod),
);
}
2021-02-16 16:34:23 +01:00
applyActiveEffectsToBaseData(): void {
2021-02-18 13:24:52 +01:00
// reset overrides because our variant of applying active effects does not set them, it only adds overrides
this.overrides = {};
this.applyActiveEffectsFiltered(
(change) =>
!this.derivedDataProperties.includes(change.key) &&
!this.finalDerivedDataProperties.includes(change.key),
);
2021-01-25 01:09:51 +01:00
}
applyActiveEffectsToDerivedData(): void {
this.applyActiveEffectsFiltered((change) => this.derivedDataProperties.includes(change.key));
2021-01-25 01:09:51 +01:00
}
/**
* Apply ActiveEffectChanges to the Actor data which are caused by ActiveEffects and satisfy the given predicate.
*
2021-02-08 03:38:50 +01:00
* @param predicate - The predicate that ActiveEffectChanges need to satisfy in order to be applied
2021-01-25 01:09:51 +01:00
*/
applyActiveEffectsFiltered(predicate: (change: ActiveEffectChange) => boolean): void {
const overrides: Record<string, unknown> = {};
2021-01-25 01:09:51 +01:00
// Organize non-disabled effects by their application priority
const changes = this.effects.reduce(
(changes: Array<ActiveEffectChange & { effect: ActiveEffect<DS4Actor> }>, e) => {
if (e.data.disabled) return changes;
const item = this._getOriginatingItemOfActiveEffect(e);
if (item?.isNonEquippedEuipable()) return changes;
const factor = item?.activeEffectFactor ?? 1;
return changes.concat(
e.data.changes.filter(predicate).flatMap((c) => {
const duplicatedChange = duplicate(c);
duplicatedChange.priority = duplicatedChange.priority ?? duplicatedChange.mode * 10;
return Array(factor).fill({
...duplicatedChange,
effect: e,
});
}),
);
},
[],
);
2021-01-25 01:09:51 +01:00
changes.sort((a, b) => a.priority - b.priority);
// Apply all changes
for (const change of changes) {
const result = change.effect.apply(this, change);
if (result !== null) overrides[change.key] = result;
}
// Expand the set of final overrides
2021-02-18 13:24:52 +01:00
this.overrides = expandObject({ ...flattenObject(this.overrides), ...overrides });
2021-01-25 01:09:51 +01:00
}
protected _getOriginatingItemOfActiveEffect(effect: ActiveEffect<DS4Actor>): DS4Item | undefined {
2021-02-16 16:34:23 +01:00
return this.items.find((item) => item.uuid === effect.data.origin) ?? undefined;
}
/**
* Apply transformations to the Actor data after effects have been applied to the base data.
* @override
*/
2020-12-23 18:23:26 +01:00
prepareDerivedData(): void {
this._prepareCombatValues();
this._prepareChecks();
2020-12-23 16:52:20 +01:00
}
/**
* The list of properties that are derived from others, given in dot notation.
*/
2021-01-25 01:09:51 +01:00
get derivedDataProperties(): Array<string> {
return Object.keys(DS4.i18n.combatValues)
.map((combatValue) => `data.combatValues.${combatValue}.total`)
.concat(
Object.keys(DS4.i18n.checks)
.filter((check) => check !== "defend")
.map((check) => `data.checks.${check}`),
);
2021-01-25 01:09:51 +01:00
}
/**
* Apply final transformations to the Actor data after all effects have been applied.
*/
prepareFinalDerivedData(): void {
this.data.data.combatValues.hitPoints.max = this.data.data.combatValues.hitPoints.total;
this.data.data.checks.defend = this.data.data.combatValues.defense.total;
}
/**
2021-02-16 16:34:23 +01:00
* The list of properties that are completely derived (i.e. {@link ActiveEffect}s cannot be applied to them),
* given in dot notation.
*/
get finalDerivedDataProperties(): string[] {
return ["data.combatValues.hitPoints.max", "data.checks.defend"];
}
/**
* The list of item types that can be owned by this actor.
*/
get ownableItemTypes(): Array<ItemType> {
switch (this.data.type) {
case "character":
return [
"weapon",
"armor",
"shield",
"equipment",
2021-02-21 03:40:54 +01:00
"loot",
"spell",
"talent",
"racialAbility",
"language",
"alphabet",
];
case "creature":
2021-02-21 03:40:54 +01:00
return ["weapon", "armor", "shield", "equipment", "loot", "spell", "specialCreatureAbility"];
default:
2021-01-18 19:03:08 +01:00
return [];
}
}
/**
* Checks whether or not the given item type can be owned by the actor.
2021-02-07 11:51:36 +01:00
* @param itemType - The item type to check
*/
canOwnItemType(itemType: ItemType): boolean {
return this.ownableItemTypes.includes(itemType);
}
/**
* Prepares the combat values of the actor.
*/
protected _prepareCombatValues(): void {
const data = this.data.data;
const armorValueOfEquippedItems = this._calculateArmorValueOfEquippedItems();
2021-02-16 16:34:23 +01:00
data.combatValues.hitPoints.base = data.attributes.body.total + data.traits.constitution.total + 10;
data.combatValues.defense.base =
data.attributes.body.total + data.traits.constitution.total + armorValueOfEquippedItems;
data.combatValues.initiative.base = data.attributes.mobility.total + data.traits.agility.total;
data.combatValues.movement.base = data.attributes.mobility.total / 2 + 1;
data.combatValues.meleeAttack.base = data.attributes.body.total + data.traits.strength.total;
data.combatValues.rangedAttack.base = data.attributes.mobility.total + data.traits.dexterity.total;
data.combatValues.spellcasting.base =
data.attributes.mind.total + data.traits.aura.total - armorValueOfEquippedItems;
data.combatValues.targetedSpellcasting.base =
data.attributes.mind.total + data.traits.dexterity.total - armorValueOfEquippedItems;
Object.values(data.combatValues).forEach(
(combatValue: ModifiableDataBaseTotal<number>) => (combatValue.total = combatValue.base + combatValue.mod),
);
}
/**
* Calculates the total armor value of all equipped items.
*/
protected _calculateArmorValueOfEquippedItems(): number {
return this.items
2021-02-05 03:42:42 +01:00
.map((item) => {
if (item.data.type === "armor" || item.data.type === "shield") {
return item.data.data.equipped ? item.data.data.armorValue : 0;
} else {
return 0;
}
})
.reduce((a, b) => a + b, 0);
}
/**
* Prepares the check target numbers of checks for the actor.
*/
protected _prepareChecks(): void {
const data = this.data.data;
data.checks = {
appraise: data.attributes.mind.total + data.traits.intellect.total,
changeSpell: data.attributes.mind.total + data.traits.intellect.total,
climb: data.attributes.mobility.total + data.traits.strength.total,
communicate: data.attributes.mind.total + data.traits.dexterity.total,
decipherScript: data.attributes.mind.total + data.traits.intellect.total,
defend: 0, // assigned in prepareFinalDerivedData as it must always match data.combatValues.defense.total and is not changeable by effects
defyPoison: data.attributes.body.total + data.traits.constitution.total,
disableTraps: data.attributes.mind.total + data.traits.dexterity.total,
featOfStrength: data.attributes.body.total + data.traits.strength.total,
flirt: data.attributes.mind.total + data.traits.aura.total,
haggle: data.attributes.mind.total + Math.max(data.traits.intellect.total, data.traits.intellect.total),
hide: data.attributes.mobility.total + data.traits.agility.total,
jump: data.attributes.mobility.total + data.traits.agility.total,
knowledge: data.attributes.mind.total + data.traits.intellect.total,
openLock: data.attributes.mind.total + data.traits.dexterity.total,
perception: Math.max(data.attributes.mind.total + data.traits.intellect.total, 8),
pickPocket: data.attributes.mobility.total + data.traits.dexterity.total,
readTracks: data.attributes.mind.total + data.traits.intellect.total,
resistDisease: data.attributes.body.total + data.traits.constitution.total,
ride: data.attributes.mobility.total + Math.max(data.traits.agility.total, data.traits.aura.total),
search: Math.max(data.attributes.mind.total + data.traits.intellect.total, 8),
sneak: data.attributes.mobility.total + data.traits.agility.total,
startFire: data.attributes.mind.total + data.traits.dexterity.total,
swim: data.attributes.mobility.total + data.traits.strength.total,
wakeUp: data.attributes.mind.total + data.traits.intellect.total,
workMechanism:
data.attributes.mind.total + Math.max(data.traits.dexterity.total, data.traits.intellect.total),
};
}
/**
* Handle how changes to a Token attribute bar are applied to the Actor.
* This only differs from the base implementation by also allowing negative values.
* @override
*/
2021-01-26 03:55:18 +01:00
async modifyTokenAttribute(attribute: string, value: number, isDelta = false, isBar = true): Promise<this> {
const current = getProperty(this.data.data, attribute);
// Determine the updates to make to the actor data
let updates: Record<string, number>;
if (isBar) {
if (isDelta) value = Math.min(Number(current.value) + value, current.max);
updates = { [`data.${attribute}.value`]: value };
} else {
if (isDelta) value = Number(current) + value;
updates = { [`data.${attribute}`]: value };
}
// Call a hook to handle token resource bar updates
const allowed = Hooks.call("modifyTokenAttribute", { attribute, value, isDelta, isBar }, updates);
return allowed !== false ? this.update(updates) : this;
}
/**
* Roll for a given check.
* @param check The check to perform
*/
async rollCheck(check: Check): Promise<void> {
await createCheckRoll(this.data.data.checks[check], {
rollMode: game.settings.get("core", "rollMode") as Const.DiceRollMode, // TODO(types): Type this setting in upstream
maximumCoupResult: this.data.data.rolling.maximumCoupResult,
minimumFumbleResult: this.data.data.rolling.minimumFumbleResult,
flavor: game.i18n.format("DS4.ActorCheckFlavor", { actor: this.name, check: DS4.i18n.checks[check] }),
});
}
}