forked from foundryvtt/pf2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.ts
195 lines (163 loc) · 6.35 KB
/
document.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { ActorPF2e } from "@actor";
import { AbstractEffectPF2e, EffectBadge } from "@item/abstract-effect/index.ts";
import { UserPF2e } from "@module/user/index.ts";
import { createDamageFormula, parseTermsFromSimpleFormula } from "@system/damage/formula.ts";
import { AfflictionDamageTemplate, BaseDamageData, DamagePF2e, DamageRollContext } from "@system/damage/index.ts";
import { DamageRoll } from "@system/damage/roll.ts";
import { DegreeOfSuccess } from "@system/degree-of-success.ts";
import { ErrorPF2e } from "@util";
import { AfflictionFlags, AfflictionSource, AfflictionSystemData } from "./data.ts";
class AfflictionPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends AbstractEffectPF2e<TParent> {
constructor(source: object, context?: DocumentConstructionContext<TParent>) {
super(source, context);
if (BUILD_MODE === "production") {
throw ErrorPF2e("Affliction items are not available in production builds");
}
}
override get badge(): EffectBadge {
const label = game.i18n.format("PF2E.Item.Affliction.Stage", { stage: this.stage });
return {
type: "counter",
value: this.stage,
max: this.maxStage,
label,
};
}
get stage(): number {
return this.system.stage;
}
get maxStage(): number {
return Object.keys(this.system.stages).length || 1;
}
override async increase(): Promise<void> {
if (this.stage === this.maxStage) return;
const stage = Math.min(this.maxStage, this.system.stage + 1);
await this.update({ system: { stage } });
}
override async decrease(): Promise<void> {
const stage = this.system.stage - 1;
if (stage === 0) {
await this.delete();
return;
}
await this.update({ system: { stage } });
}
override prepareBaseData(): void {
super.prepareBaseData();
this.system.stage = Math.clamped(this.system.stage, 1, this.maxStage);
// Set certain defaults
for (const stage of Object.values(this.system.stages)) {
for (const condition of Object.values(stage.conditions)) {
condition.linked ??= true;
}
}
}
/** Retrieves the damage for a specific stage */
getStageDamage(stage: number): AfflictionDamage | null {
const stageData = Object.values(this.system.stages).at(stage - 1);
const base: BaseDamageData[] = [];
for (const data of Object.values(stageData?.damage ?? {})) {
const { formula, type: damageType, category } = data;
const terms = parseTermsFromSimpleFormula(formula);
base.push({ terms, damageType, category: category ?? null });
}
if (!base.length) return null;
try {
const { formula, breakdown } = createDamageFormula({
base,
modifiers: [],
dice: [],
ignoredResistances: [],
});
const roll = new DamageRoll(formula);
const stageLabel = game.i18n.format("PF2E.Item.Affliction.Stage", { stage: this.stage });
const template: AfflictionDamageTemplate = {
name: `${this.name} - ${stageLabel}`,
damage: { roll, breakdown },
notes: [],
materials: [],
traits: this.system.traits.value,
modifiers: [],
};
// Context isn't used for affliction damage rolls, but we still need it for creating messages
const context: DamageRollContext = {
type: "damage-roll",
sourceType: "save",
outcome: "failure",
domains: [],
options: new Set(),
self: null,
};
return { template, context };
} catch (err) {
console.error(err);
}
return null;
}
async createStageMessage(): Promise<void> {
const actor = this.actor;
if (!actor) return;
const currentStage = Object.values(this.system.stages).at(this.stage - 1);
if (!currentStage) return;
const damage = this.getStageDamage(this.stage);
if (damage) {
const { template, context } = damage;
await DamagePF2e.roll(template, context);
}
}
protected override async _preUpdate(
changed: DeepPartial<this["_source"]>,
options: DocumentModificationContext<TParent>,
user: UserPF2e
): Promise<boolean | void> {
const duration = changed.system?.duration;
if (typeof duration?.unit === "string" && !["unlimited", "encounter"].includes(duration.unit)) {
if (duration.value === -1) duration.value = 1;
}
return super._preUpdate(changed, options, user);
}
protected override _onCreate(
data: AfflictionSource,
options: DocumentModificationContext<TParent>,
userId: string
): void {
super._onCreate(data, options, userId);
this.createStageMessage();
}
override _onUpdate(
changed: DeepPartial<this["_source"]>,
options: DocumentModificationContext<TParent>,
userId: string
): void {
super._onUpdate(changed, options, userId);
// If the stage changed, perform stage change events
if (changed.system?.stage && game.user === this.actor?.primaryUpdater) {
this.createStageMessage();
}
}
async rollRecovery(): Promise<void> {
if (!this.actor) return;
const save = this.actor.saves?.[this.system.save.type];
if (save) {
const result = await save.roll({
dc: { value: this.system.save.value },
extraRollOptions: this.getRollOptions("item"),
});
if ((result?.degreeOfSuccess ?? 0) >= DegreeOfSuccess.SUCCESS) {
this.decrease();
} else {
this.increase();
}
}
}
}
interface AfflictionPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends AbstractEffectPF2e<TParent> {
flags: AfflictionFlags;
readonly _source: AfflictionSource;
system: AfflictionSystemData;
}
interface AfflictionDamage {
template: AfflictionDamageTemplate;
context: DamageRollContext;
}
export { AfflictionPF2e };