forked from foundryvtt/pf2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc.ts
172 lines (152 loc) · 4.79 KB
/
dc.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
import { ProficiencyRank } from "@item/base/data/index.ts";
import { Rarity } from "./data.ts";
/**
* Implementation of Difficulty Classes https://2e.aonprd.com/Rules.aspx?ID=552
* and variant rule Proficiency Without Level https://2e.aonprd.com/Rules.aspx?ID=1370
*/
type NegativeDCAdjustment = "incredibly-easy" | "very-easy" | "easy" | "normal";
type PositiveDCAdjustment = "normal" | "hard" | "very-hard" | "incredibly-hard";
type DCAdjustment = NegativeDCAdjustment | PositiveDCAdjustment;
const adjustmentScale: DCAdjustment[] = [
"incredibly-easy",
"very-easy",
"easy",
"normal",
"hard",
"very-hard",
"incredibly-hard",
];
const dcAdjustments = new Map<DCAdjustment, number>([
["incredibly-easy", -10],
["very-easy", -5],
["easy", -2],
["normal", 0],
["hard", 2],
["very-hard", 5],
["incredibly-hard", 10],
]);
const dcByLevel = new Map([
[-1, 13],
[0, 14],
[1, 15],
[2, 16],
[3, 18],
[4, 19],
[5, 20],
[6, 22],
[7, 23],
[8, 24],
[9, 26],
[10, 27],
[11, 28],
[12, 30],
[13, 31],
[14, 32],
[15, 34],
[16, 35],
[17, 36],
[18, 38],
[19, 39],
[20, 40],
[21, 42],
[22, 44],
[23, 46],
[24, 48],
[25, 50],
]);
const simpleDCs = new Map<ProficiencyRank, number>([
["untrained", 10],
["trained", 15],
["expert", 20],
["master", 30],
["legendary", 40],
]);
const simpleDCsWithoutLevel = new Map<ProficiencyRank, number>([
["untrained", 10],
["trained", 15],
["expert", 20],
["master", 25],
["legendary", 30],
]);
function rarityToDCAdjustment(rarity: Rarity = "common"): PositiveDCAdjustment {
switch (rarity) {
case "uncommon":
return "hard";
case "rare":
return "very-hard";
case "unique":
return "incredibly-hard";
default:
return "normal";
}
}
function adjustDC(dc: number, adjustment: DCAdjustment = "normal"): number {
return dc + (dcAdjustments.get(adjustment) ?? 0);
}
function adjustDCByRarity(dc: number, rarity: Rarity = "common"): number {
return adjustDC(dc, rarityToDCAdjustment(rarity));
}
interface DCOptions {
proficiencyWithoutLevel?: boolean;
rarity?: Rarity;
}
/** Level-based DCs */
function calculateDC(level: number, { proficiencyWithoutLevel, rarity = "common" }: DCOptions = {}): number {
proficiencyWithoutLevel ??= game.settings.get("pf2e", "proficiencyVariant");
// assume level 0 if garbage comes in. We cast level to number because the backing data may actually have it
// stored as a string, which we can't catch at compile time
const dc = dcByLevel.get(level) ?? 14;
if (proficiencyWithoutLevel) {
// -1 shouldn't be subtracted since it's just
// a creature level and not related to PC levels
return adjustDCByRarity(dc - Math.max(level, 0), rarity);
} else {
return adjustDCByRarity(dc, rarity);
}
}
function calculateSimpleDC(rank: ProficiencyRank, { proficiencyWithoutLevel = false }: DCOptions = {}): number {
if (proficiencyWithoutLevel) {
return simpleDCsWithoutLevel.get(rank) ?? 10;
} else {
return simpleDCs.get(rank) ?? 10;
}
}
function calculateSpellDC(spellLevel: number, { proficiencyWithoutLevel = false }: DCOptions = {}): number {
return calculateDC(spellLevel * 2 - 1, { proficiencyWithoutLevel });
}
/**
* Used to shift DCs around the adjustment table Rarity increases
* the adjustment while Lores reduce it.
* This function determines which adjustment you start from when you
* create a difficulty scale from incredibly easy to very hard
*
* Important: this operation is not associative because
* of the lower and upper bounds
*/
function combineDCAdjustments(first: DCAdjustment, second: DCAdjustment): DCAdjustment {
const startingIndex = adjustmentScale.indexOf(first);
const lowerByIndex = adjustmentScale.indexOf(second);
// -3 because index 3 (normal) is our 0
const resultIndex = Math.min(Math.max(startingIndex + lowerByIndex - 3, 0), 6);
return adjustmentScale[resultIndex];
}
/**
* Given a DC made starting at an adjustment create an array of
* growing difficulties starting from the adjusted position in
* the table at https://2e.aonprd.com/Rules.aspx?ID=555
*/
function createDifficultyScale(dc: number, startAt: DCAdjustment): number[] {
const beginAtIndex = adjustmentScale.indexOf(startAt);
return adjustmentScale.filter((_value, index) => index >= beginAtIndex).map((value) => adjustDC(dc, value));
}
export {
adjustDC,
adjustDCByRarity,
calculateDC,
calculateSimpleDC,
calculateSpellDC,
combineDCAdjustments,
createDifficultyScale,
rarityToDCAdjustment,
};
export type { DCAdjustment, DCOptions, NegativeDCAdjustment, PositiveDCAdjustment };