-
Notifications
You must be signed in to change notification settings - Fork 309
/
helpers.ts
564 lines (492 loc) · 22.5 KB
/
helpers.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import { ActorPF2e, ActorProxyPF2e } from "@actor";
import { ItemPF2e, MeleePF2e } from "@item";
import { CheckRollContextFlag } from "@module/chat-message/index.ts";
import { ZeroToTwo } from "@module/data.ts";
import { MigrationList, MigrationRunner } from "@module/migration/index.ts";
import { MigrationRunnerBase } from "@module/migration/runner/base.ts";
import {
extractDegreeOfSuccessAdjustments,
extractModifierAdjustments,
extractModifiers,
extractNotes,
extractRollSubstitutions,
extractRollTwice,
} from "@module/rules/helpers.ts";
import { eventToRollParams } from "@scripts/sheet-util.ts";
import { CheckPF2e, CheckRoll, CheckRollContext } from "@system/check/index.ts";
import { DamagePF2e, DamageRollContext } from "@system/damage/index.ts";
import { DamageRoll } from "@system/damage/roll.ts";
import { WeaponDamagePF2e } from "@system/damage/weapon.ts";
import { AttackRollParams, DamageRollParams } from "@system/rolls.ts";
import { ErrorPF2e, getActionGlyph, getActionIcon, signedInteger, sluggify } from "@util";
import * as R from "remeda";
import { AttackTraitHelpers } from "./creature/helpers.ts";
import { DamageRollFunction, TraitViewData } from "./data/base.ts";
import { ActorSourcePF2e } from "./data/index.ts";
import { CheckModifier, ModifierPF2e, StatisticModifier, adjustModifiers } from "./modifiers.ts";
import { NPCStrike } from "./npc/data.ts";
import { AuraEffectData, DamageRollContextParams } from "./types.ts";
/** Reset and rerender a provided list of actors. Omit argument to reset all world and synthetic actors */
async function resetActors(actors?: Iterable<ActorPF2e>, { rerender = true } = {}): Promise<void> {
actors ??= [
game.actors.contents,
game.scenes.contents.flatMap((s) => s.tokens.contents).flatMap((t) => t.actor ?? []),
].flat();
for (const actor of actors) {
actor.reset();
if (rerender) actor.render();
}
game.pf2e.effectPanel.refresh();
// If expired effects are automatically removed, the actor update cycle will reinitialize vision
const refreshScenes =
game.settings.get("pf2e", "automation.effectExpiration") &&
!game.settings.get("pf2e", "automation.removeExpiredEffects");
if (refreshScenes) {
const scenes = new Set(
Array.from(actors)
.flatMap((a) => a.getActiveTokens(false, true))
.flatMap((t) => t.scene)
);
for (const scene of scenes) {
scene.reset();
if (scene.isView) {
canvas.perception.update({ initializeVision: true }, true);
}
}
}
}
async function migrateActorSource(source: PreCreate<ActorSourcePF2e>): Promise<ActorSourcePF2e> {
source.effects = []; // Never
if (!["flags", "items", "system"].some((k) => k in source)) {
// The actor has no migratable data: set schema version and return early
source.system = { schema: { version: MigrationRunnerBase.LATEST_SCHEMA_VERSION } };
}
const lowestSchemaVersion = Math.min(
source.system?.schema?.version ?? MigrationRunnerBase.LATEST_SCHEMA_VERSION,
...(source.items ?? []).map((i) => i!.system?.schema?.version ?? MigrationRunnerBase.LATEST_SCHEMA_VERSION)
);
const tokenDefaults = deepClone(game.settings.get("core", "defaultToken"));
const actor = new ActorProxyPF2e(mergeObject({ prototypeToken: tokenDefaults }, source));
await MigrationRunner.ensureSchemaVersion(actor, MigrationList.constructFromVersion(lowestSchemaVersion));
return actor.toObject();
}
/** Review `removeOnExit` aura effects and remove any that no longer apply */
async function checkAreaEffects(this: ActorPF2e): Promise<void> {
if (!canvas.ready || game.user !== this.primaryUpdater || this.isOfType("party")) {
return;
}
const thisTokens = this.getActiveTokens(true, true);
const toDelete: string[] = [];
const toKeep: string[] = [];
for (const effect of this.itemTypes.effect) {
const auraData = effect.flags.pf2e.aura;
if (!auraData?.removeOnExit) continue;
const auraActor = (await fromUuid(auraData.origin)) as ActorPF2e | null;
const auraToken = auraActor?.getActiveTokens(true, true).shift() ?? null;
const aura = auraToken?.auras.get(auraData.slug);
// Make sure this isn't an identically-slugged aura with different effects
const auraEffectData = auraActor?.auras
.get(auraData.slug)
?.effects.find((e) => e.uuid === effect.sourceId && auraAffectsActor(e, auraActor, this));
for (const token of thisTokens) {
if (auraEffectData && aura?.containsToken(token)) {
toKeep.push(effect.id);
} else {
toDelete.push(effect.id);
}
}
// If no tokens for this actor remain in the scene, always remove the effect
if (thisTokens.length === 0) {
toDelete.push(effect.id);
}
}
// In case there are multiple tokens for this actor, avoid deleting aura effects if at least one token is
// exposed to the aura
const finalToDelete = toDelete.filter((id) => !toKeep.includes(id));
if (finalToDelete.length > 0) {
await this.deleteEmbeddedDocuments("Item", finalToDelete);
}
}
function auraAffectsActor(data: AuraEffectData, origin: ActorPF2e, actor: ActorPF2e): boolean {
return (
(data.includesSelf && origin === actor) ||
(data.affects === "allies" && actor.isAllyOf(origin)) ||
(data.affects === "enemies" && actor.isEnemyOf(origin)) ||
(data.affects === "all" && actor !== origin)
);
}
/** Set a roll option for HP remaining and percentage remaining */
function setHitPointsRollOptions(actor: ActorPF2e): void {
const hp = actor.hitPoints;
if (!hp) return;
actor.flags.pf2e.rollOptions.all[`hp-remaining:${hp.value}`] = true;
const percentRemaining = Math.floor((hp.value / hp.max) * 100);
actor.flags.pf2e.rollOptions.all[`hp-percent:${percentRemaining}`] = true;
}
/** Find the lowest multiple attack penalty for an attack with a given item */
function calculateMAPs(
item: ItemPF2e,
{ domains, options }: { domains: string[]; options: Set<string> | string[] }
): MAPData {
const slugAndLabel = { slug: "multiple-attack-penalty", label: "PF2E.MultipleAttackPenalty" } as const;
const baseMap =
item.isOfType("action", "melee", "weapon") && item.traits.has("agile")
? { ...slugAndLabel, map1: -4, map2: -8 }
: { ...slugAndLabel, map1: -5, map2: -10 };
const optionSet = options instanceof Set ? options : new Set(options);
const maps = item.actor?.synthetics.multipleAttackPenalties ?? {};
const fromSynthetics = domains
.flatMap((d) => maps[d] ?? [])
.filter((p) => p.predicate?.test(optionSet) ?? true)
.map((p): MAPData => ({ slug: baseMap.slug, label: p.label, map1: p.penalty, map2: p.penalty * 2 }));
// Find lowest multiple attack penalty: penalties are negative, so actually looking for the highest value
return [baseMap, ...fromSynthetics].reduce((lowest, p) => (p.map1 > lowest.map1 ? p : lowest));
}
interface MAPData {
slug: "multiple-attack-penalty";
label: string;
map1: number;
map2: number;
}
/** Create roll options pertaining to the active encounter and the actor's participant */
function createEncounterRollOptions(actor: ActorPF2e): Record<string, boolean> {
const encounter = game.ready ? game.combat : null;
if (!encounter?.started) return {};
const participants = encounter.combatants.contents
.filter((c) => typeof c.initiative === "number")
.sort((a, b) => b.initiative! - a.initiative!); // Sort descending by initiative roll result
const participant = actor.combatant;
if (typeof participant?.initiative !== "number" || !participants.includes(participant)) {
return {};
}
const initiativeRoll = Math.trunc(participant.initiative);
const initiativeRank = participants.indexOf(participant) + 1;
const { initiativeStatistic } = participant.flags.pf2e;
const threat = encounter.metrics?.threat;
const numericThreat = { trivial: 0, low: 1, moderate: 2, severe: 3, extreme: 4 }[threat ?? "trivial"];
const entries = (
[
["encounter", true],
[`encounter:threat:${numericThreat}`, !!threat],
[`encounter:threat:${threat}`, !!threat],
[`encounter:round:${encounter.round}`, true],
[`encounter:turn:${Number(encounter.turn) + 1}`, true],
["self:participant:own-turn", encounter.combatant?.actor === actor],
[`self:participant:initiative:roll:${initiativeRoll}`, true],
[`self:participant:initiative:rank:${initiativeRank}`, true],
[`self:participant:initiative:stat:${initiativeStatistic}`, !!initiativeStatistic],
] as const
).filter(([, value]) => !!value);
return Object.fromEntries(entries);
}
/** Whether flanking puts this actor off-guard */
function isOffGuardFromFlanking(target: ActorPF2e, origin: ActorPF2e): boolean {
if (!target?.isOfType("creature")) return false;
const { flanking } = target.attributes;
return !flanking.flankable
? false
: typeof flanking.offGuardable === "number"
? origin.level > flanking.offGuardable
: flanking.offGuardable;
}
/** Create a strike statistic from a melee item: for use by NPCs and Hazards */
function strikeFromMeleeItem(item: MeleePF2e<ActorPF2e>): NPCStrike {
const { ability, isMelee, isThrown } = item;
const { actor } = item;
if (!actor.isOfType("npc", "hazard")) {
throw ErrorPF2e("Attempted to create melee-item strike statistic for non-NPC/hazard");
}
// Conditions and Custom modifiers to attack rolls
const attackSlug = item.slug ?? sluggify(item.name);
const unarmedOrWeapon = item.system.traits.value.includes("unarmed") ? "unarmed" : "weapon";
const meleeOrRanged = isMelee ? "melee" : "ranged";
const domains = [
`${attackSlug}-attack`,
`${item.id}-attack`,
`${unarmedOrWeapon}-attack-roll`,
`${meleeOrRanged}-attack-roll`,
`${meleeOrRanged}-strike-attack-roll`,
"strike-attack-roll",
"attack-roll",
"attack",
"all",
];
if (actor.isOfType("npc")) {
domains.push(`${ability}-attack`, `${ability}-based`);
}
const { synthetics } = actor;
const modifiers: ModifierPF2e[] = [
new ModifierPF2e({
slug: "base",
label: "PF2E.ModifierTitle",
modifier: item.attackModifier,
adjustments: extractModifierAdjustments(synthetics.modifierAdjustments, domains, "base"),
}),
];
modifiers.push(...extractModifiers(synthetics, domains));
modifiers.push(...AttackTraitHelpers.createAttackModifiers({ item }));
const notes = extractNotes(synthetics.rollNotes, domains);
const attackEffects: Record<string, string | undefined> = CONFIG.PF2E.attackEffects;
const additionalEffects = item.attackEffects.map((tag) => {
const items: ItemPF2e<ActorPF2e>[] = actor.items.contents;
const label = attackEffects[tag] ?? items.find((i) => (i.slug ?? sluggify(i.name)) === tag)?.name ?? tag;
return { tag, label };
});
// Apply strike adjustments affecting the attack
for (const adjustment of synthetics.strikeAdjustments) {
adjustment.adjustWeapon?.(item);
}
const baseOptions = new Set(R.compact([isThrown ? "thrown" : null, meleeOrRanged, ...item.system.traits.value]));
const initialRollOptions = new Set([
...baseOptions,
...actor.getRollOptions(domains),
...item.getRollOptions("item"),
]);
const statistic = new StatisticModifier(attackSlug, modifiers, initialRollOptions);
const traitObjects = item.system.traits.value.map(
(t): TraitViewData => ({
name: t,
label: CONFIG.PF2E.npcAttackTraits[t] ?? t,
description: CONFIG.PF2E.traitsDescriptions[t],
})
);
const strike: NPCStrike = mergeObject(statistic, {
label: item.name,
type: "strike" as const,
glyph: getActionGlyph({ type: "action", value: 1 }),
description: item.description,
imageUrl: getActionIcon({ type: "action", value: 1 }),
sourceId: item.id,
attackRollType: item.isRanged ? "PF2E.NPCAttackRanged" : "PF2E.NPCAttackMelee",
additionalEffects,
item,
weapon: item,
traits: traitObjects,
options: Array.from(baseOptions),
variants: [],
success: "",
ready: true,
criticalSuccess: "",
});
strike.breakdown = strike.modifiers
.filter((m) => m.enabled)
.map((m) => `${m.label} ${m.modifier < 0 ? "" : "+"}${m.modifier}`)
.join(", ");
const attackTrait = {
name: "attack",
label: CONFIG.PF2E.featTraits.attack,
description: CONFIG.PF2E.traitsDescriptions.attack,
};
// Multiple attack penalty
const maps = calculateMAPs(item, { domains, options: baseOptions });
const createMapModifier = (prop: "map1" | "map2") => {
return new ModifierPF2e({
slug: maps.slug,
label: maps.label,
modifier: maps[prop],
adjustments: extractModifierAdjustments(synthetics.modifierAdjustments, domains, maps.slug),
});
};
const labels = [
`${game.i18n.localize("PF2E.WeaponStrikeLabel")} ${signedInteger(strike.totalModifier)}`,
...(["map1", "map2"] as const).map((prop) => {
const modifier = createMapModifier(prop);
adjustModifiers([modifier], baseOptions);
const penalty = modifier.ignored ? 0 : modifier.value;
return game.i18n.format("PF2E.MAPAbbreviationValueLabel", {
value: signedInteger(strike.totalModifier + penalty),
penalty,
});
}),
];
strike.variants = [null, ...(["map1", "map2"] as const).map(createMapModifier)].map((map, mapIncreases) => ({
label: labels[mapIncreases],
roll: async (params: AttackRollParams = {}): Promise<Rolled<CheckRoll> | null> => {
const attackEffects = actor.isOfType("npc") ? await actor.getAttackEffects(item) : [];
const rollNotes = notes.concat(attackEffects);
params.options ??= [];
// Always add all weapon traits as options
const context = await actor.getCheckContext({
item,
viewOnly: params.getFormula ?? false,
statistic: strike,
target: { token: game.user.targets.first() ?? null },
defense: "armor",
domains,
options: new Set([...baseOptions, ...params.options]),
});
// Check whether target is out of maximum range; abort early if so
if (context.self.item.isRanged && typeof context.target?.distance === "number") {
const maxRange = item.range?.max ?? 10;
if (context.target.distance > maxRange) {
ui.notifications.warn("PF2E.Action.Strike.OutOfRange", { localize: true });
return null;
}
}
const otherModifiers = [map ?? [], context.self.modifiers].flat();
const title = game.i18n.format(
item.isMelee ? "PF2E.Action.Strike.MeleeLabel" : "PF2E.Action.Strike.RangedLabel",
{ weapon: item.name }
);
const check = new CheckModifier("strike", context.self.statistic ?? strike, otherModifiers);
const checkContext: CheckRollContext = {
type: "attack-roll",
identifier: `${item.id}.${attackSlug}.${meleeOrRanged}`,
action: "strike",
title,
actor: context.self.actor,
token: context.self.token,
item: context.self.item,
target: context.target,
damaging: item.dealsDamage,
domains,
options: context.options,
traits: [attackTrait],
notes: rollNotes,
dc: params.dc ?? context.dc,
mapIncreases: mapIncreases as ZeroToTwo,
rollTwice: extractRollTwice(synthetics.rollTwice, domains, context.options),
substitutions: extractRollSubstitutions(synthetics.rollSubstitutions, domains, context.options),
dosAdjustments: extractDegreeOfSuccessAdjustments(synthetics, domains),
};
const roll = await CheckPF2e.roll(check, checkContext, params.event);
if (roll) {
for (const rule of actor.rules.filter((r) => !r.ignored)) {
await rule.afterRoll?.({
roll,
check,
context: checkContext,
domains,
rollOptions: context.options,
});
}
}
return roll;
},
}));
strike.roll = strike.attack = strike.variants[0].roll;
const damageRoll =
(outcome: "success" | "criticalSuccess"): DamageRollFunction =>
async (params: DamageRollParams = {}): Promise<Rolled<DamageRoll> | string | null> => {
const domains = ["all", `${item.id}-damage`, "attack-damage", "strike-damage", "damage"];
const targetToken = params.target ?? game.user.targets.first() ?? null;
const context = await actor.getDamageRollContext({
item,
statistic: strike,
target: { token: targetToken },
viewOnly: params.getFormula ?? false,
domains,
checkContext: params.checkContext,
outcome,
options: new Set([...baseOptions, ...(params.options ?? [])]),
});
if (!context.self.item.dealsDamage && !params.getFormula) {
ui.notifications.warn("PF2E.ErrorMessage.WeaponNoDamage", { localize: true });
return null;
}
const { self, target } = context;
const damageContext: DamageRollContext = {
type: "damage-roll",
sourceType: "attack",
self,
target,
outcome,
options: context.options,
domains,
...eventToRollParams(params.event),
};
// Include MAP increases in case any ability depends on it
if (typeof params.mapIncreases === "number") {
damageContext.mapIncreases = params.mapIncreases;
damageContext.options.add(`map:increases:${params.mapIncreases}`);
}
if (params.getFormula) damageContext.skipDialog = true;
const damage = await WeaponDamagePF2e.fromNPCAttack({
attack: context.self.item,
actor: context.self.actor,
actionTraits: [attackTrait],
proficiencyRank: 1,
context: damageContext,
});
if (!damage) return null;
if (params.getFormula) {
const formula = damage.damage.formula[outcome];
return formula ? new DamageRoll(formula).formula : "";
} else {
return DamagePF2e.roll(damage, damageContext, params.callback);
}
};
strike.damage = damageRoll("success");
strike.critical = damageRoll("criticalSuccess");
return strike;
}
/** Get the range increment of a target for a given weapon */
function getRangeIncrement(attackItem: ItemPF2e<ActorPF2e>, distance: number | null): number | null {
if (!attackItem.isOfType("action", "melee", "weapon")) return null;
const { increment } = attackItem.range ?? {};
return increment && typeof distance === "number" ? Math.max(Math.ceil(distance / increment), 1) : null;
}
/** Determine range penalty for a ranged attack roll */
function calculateRangePenalty(
actor: ActorPF2e,
increment: number | null,
selectors: string[],
rollOptions: Set<string>
): ModifierPF2e | null {
if (!increment || increment === 1) return null;
const slug = "range-penalty";
const modifier = new ModifierPF2e({
label: "PF2E.RangePenalty",
slug,
type: "untyped",
modifier: Math.max((increment - 1) * -2, -12), // Max range penalty before automatic failure
predicate: [{ nor: ["ignore-range-penalty", { gte: ["ignore-range-penalty", increment] }] }],
adjustments: extractModifierAdjustments(actor.synthetics.modifierAdjustments, selectors, slug),
});
modifier.test(rollOptions);
return modifier;
}
/** Whether this actor is of a the "character" type, excluding those from the PF2E Companion Compendia module */
function isReallyPC(actor: ActorPF2e): boolean {
const traits = actor.traits;
return actor.isOfType("character") && !(traits.has("minion") || traits.has("eidolon"));
}
/** Scan the last three chat messages for a check context to match the to-be-created damage context. */
function findMatchingCheckContext(actor: ActorPF2e, params: DamageRollContextParams): CheckRollContextFlag | null {
if (params.viewOnly || !params.target?.token) return null;
const paramsItem = params.item;
if (!paramsItem?.isOfType("melee", "weapon")) return null;
const checkMessage = game.messages.contents
.slice(-3)
.reverse()
.find((message) => {
if (!message.rolls.some((r) => r instanceof CheckRoll)) return false;
if (message.actor?.uuid !== actor.uuid) return false;
if (params.target?.token !== message.target?.token.object) return false;
const messageItem = message.item;
if (!messageItem?.isOfType("melee", "weapon")) return false;
const paramsItemSlug = paramsItem.slug ?? sluggify(paramsItem.name);
const messageItemSlug = messageItem.slug ?? sluggify(messageItem.name);
return !!(
paramsItemSlug === messageItemSlug &&
paramsItem.uuid === messageItem.uuid &&
paramsItem.isMelee === messageItem.isMelee
);
});
return (checkMessage?.flags.pf2e.context ?? null) as CheckRollContextFlag | null;
}
export {
auraAffectsActor,
calculateMAPs,
calculateRangePenalty,
checkAreaEffects,
createEncounterRollOptions,
findMatchingCheckContext,
getRangeIncrement,
isOffGuardFromFlanking,
isReallyPC,
migrateActorSource,
resetActors,
setHitPointsRollOptions,
strikeFromMeleeItem,
};