This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
lr-hd-healing.js
346 lines (303 loc) · 12.9 KB
/
lr-hd-healing.js
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
import HDLongRestDialog from "./new-long-rest.js";
import { libWrapper } from "./lib/libWrapper/shim.js";
Hooks.on("init", () => {
game.settings.register("long-rest-hd-healing", "recovery-mult-hitpoints", {
name: "Hit Points Recovery Fraction",
hint: "The fraction missing hit points to recover on a long rest.",
scope: "world",
config: true,
type: String,
choices: {
none: "None (default)",
quarter: "Quarter",
half: "Half",
full: "Full",
},
default: "none",
});
game.settings.register("long-rest-hd-healing", "recovery-mult", {
name: "Hit Dice Recovery Fraction",
hint: "The fraction of hit dice to recover on a long rest.",
scope: "world",
config: true,
type: String,
choices: {
none: "None",
quarter: "Quarter",
half: "Half (default)",
full: "Full",
},
default: "half",
});
game.settings.register("long-rest-hd-healing", "recovery-rounding", {
name: "Hit Dice Recovery Rounding",
hint: "How to round the number of hit dice recovered.",
scope: "world",
config: true,
type: String,
choices: {
down: "Round down (default)",
up: "Round up",
},
default: "down",
});
game.settings.register("long-rest-hd-healing", "recovery-mult-resources", {
name: "Resources Recovery Fraction",
hint: "The fraction of resources to recover on a long rest.",
scope: "world",
config: true,
type: String,
choices: {
none: "None",
quarter: "Quarter",
half: "Half",
full: "Full (default)",
},
default: "full",
});
game.settings.register("long-rest-hd-healing", "recovery-mult-spells", {
name: "Spell Slots Recovery Fraction",
hint: "The fraction of spell slots to recover on a long rest (pact slots excluded).",
scope: "world",
config: true,
type: String,
choices: {
none: "None",
quarter: "Quarter",
half: "Half",
full: "Full (default)",
},
default: "full",
});
game.settings.register("long-rest-hd-healing", "recovery-mult-uses-others", {
name: "Item Uses Recovery Fraction",
hint: "The fraction of item uses (items, consumables, etc.) to recover on a long rest.",
scope: "world",
config: true,
type: String,
choices: {
none: "None",
quarter: "Quarter",
half: "Half",
full: "Full (default)",
},
default: "full",
});
game.settings.register("long-rest-hd-healing", "recovery-mult-uses-feats", {
name: "Feat uses Recovery Fraction",
hint: "The fraction of feat uses to recover on a long rest.",
scope: "world",
config: true,
type: String,
choices: {
none: "None",
quarter: "Quarter",
half: "Half",
full: "Full (default)",
},
default: "full",
});
game.settings.register("long-rest-hd-healing", "recovery-mult-day", {
name: "Daily uses Recovery Fraction",
hint: "The fraction of daily uses to recover on a long rest (items with the \"Day\" recovery setting).",
scope: "world",
config: true,
type: String,
choices: {
none: "None",
quarter: "Quarter",
half: "Half",
full: "Full (default)",
},
default: "full",
});
patch_newLongRest();
patch_getRestHitPointRecovery();
patch_getRestHitDiceRecovery();
patch_getRestResourceRecovery();
patch_getRestSpellRecovery();
patch_getRestItemUsesRecovery();
});
function patch_newLongRest() {
libWrapper.register(
"long-rest-hd-healing",
"CONFIG.Actor.entityClass.prototype.longRest",
async function patchedLongRest(...args) {
let { chat=true, dialog=true, newDay=true } = args[0] ?? {};
const hd0 = this.data.data.attributes.hd;
const hp0 = this.data.data.attributes.hp.value;
// Before spending hit dice, recover a fraction of missing hit points (if applicable)
const hitPointsRecoveryMultSetting = game.settings.get("long-rest-hd-healing", "recovery-mult-hitpoints");
const hitPointsRecoveryMultiplier = determineLongRestMultiplier(hitPointsRecoveryMultSetting);
if (hitPointsRecoveryMultiplier) {
const maxHP = this.data.data.attributes.hp.max;
const recoveredHP = Math.floor((maxHP - hp0) * hitPointsRecoveryMultiplier);
await this.update({ "data.attributes.hp.value": hp0 + recoveredHP });
}
// Maybe present a confirmation dialog
if (dialog) {
try {
newDay = await HDLongRestDialog.hdLongRestDialog({ actor: this });
} catch (err) {
return;
}
}
const dhd = this.data.data.attributes.hd - hd0;
const dhp = this.data.data.attributes.hp.value - hp0;
return this._rest(chat, newDay, true, dhd, dhp);
},
"OVERRIDE",
);
}
function patch_getRestHitPointRecovery() {
libWrapper.register(
"long-rest-hd-healing",
"CONFIG.Actor.entityClass.prototype._getRestHitPointRecovery",
function patched_getRestHitPointRecovery(wrapped, ...args) {
const currentHP = this.data.data.attributes.hp.value;
const result = wrapped(...args);
// Undo changes to hp from wrapped function
result.updates["data.attributes.hp.value"] = currentHP;
result.hitPointsRecovered = 0;
return result;
},
"WRAPPER",
);
}
function patch_getRestHitDiceRecovery() {
libWrapper.register(
"long-rest-hd-healing",
"CONFIG.Actor.entityClass.prototype._getRestHitDiceRecovery",
function patched_getRestHitDiceRecovery(wrapped, ...args) {
const { maxHitDice=undefined } = args[0] ?? {};
const recoveryHDMultSetting = game.settings.get("long-rest-hd-healing", "recovery-mult");
const recoveryHDMultiplier = determineLongRestMultiplier(recoveryHDMultSetting);
if (recoveryHDMultiplier === 0) return { updates: [], hitDiceRecovered: 0 };
const recoveryHDRoundSetting = game.settings.get("long-rest-hd-healing", "recovery-rounding");
const recoveryHDRoundingFn = recoveryHDRoundSetting === "down" ? Math.floor : Math.ceil;
const totalHitDice = this.data.data.details.level;
const hitDiceToRecover = Math.clamped(recoveryHDRoundingFn(totalHitDice * recoveryHDMultiplier), 1, maxHitDice ?? totalHitDice);
return wrapped({ maxHitDice: hitDiceToRecover });
},
"MIXED",
);
}
function patch_getRestResourceRecovery() {
libWrapper.register(
"long-rest-hd-healing",
"CONFIG.Actor.entityClass.prototype._getRestResourceRecovery",
function patched_getRestResourceRecovery(...args) {
const { recoverShortRestResources=true, recoverLongRestResources=true } = args[0] ?? {};
const resourcesRecoveryMultSetting = game.settings.get("long-rest-hd-healing", "recovery-mult-resources");
const resourcesRecoveryMultiplier = determineLongRestMultiplier(resourcesRecoveryMultSetting);
if (resourcesRecoveryMultiplier === 0) return {};
let updates = {};
for ( let [k, r] of Object.entries(this.data.data.resources) ) {
if (Number.isNumeric(r.max)) {
if (recoverShortRestResources && r.sr) {
updates[`data.resources.${k}.value`] = Number(r.max);
} else if (recoverLongRestResources && r.lr) {
let recoverResources = Math.max(Math.floor(r.max * resourcesRecoveryMultiplier), 1);
updates[`data.resources.${k}.value`] = Math.min(r.value + recoverResources, r.max);
}
}
}
return updates;
},
"OVERRIDE",
);
}
function patch_getRestSpellRecovery() {
libWrapper.register(
"long-rest-hd-healing",
"CONFIG.Actor.entityClass.prototype._getRestSpellRecovery",
function patched_getRestSpellRecovery(wrapped, ...args) {
const { recoverPact=true, recoverSpells=true } = args[0] ?? {};
const spellsRecoveryMultSetting = game.settings.get("long-rest-hd-healing", "recovery-mult-spells");
const spellsRecoveryMultiplier = determineLongRestMultiplier(spellsRecoveryMultSetting);
// Defer to the original method for recovering pact slots
const results = wrapped({ recoverPact, recoverSpells: false });
if (!recoverSpells || spellsRecoveryMultiplier === 0) return results;
// But overwrite the logic for recovering other spell slots
for ( let [k, v] of Object.entries(this.data.data.spells) ) {
if (!v.override && !v.max) continue;
let spellMax = v.override || v.max;
let recoverSpells = Math.max(Math.floor(spellMax * spellsRecoveryMultiplier), 1);
results[`data.spells.${k}.value`] = Math.min(v.value + recoverSpells, spellMax);
}
return results;
},
"WRAPPER",
);
}
function patch_getRestItemUsesRecovery() {
libWrapper.register(
"long-rest-hd-healing",
"CONFIG.Actor.entityClass.prototype._getRestItemUsesRecovery",
function patched_getRestItemUsesRecovery(wrapped, ...args) {
const { recoverShortRestUses=true, recoverLongRestUses=true, recoverDailyUses=true } = args[0] ?? {};
const featsUsesRecoveryMultSetting = game.settings.get("long-rest-hd-healing", "recovery-mult-uses-feats");
const featsUsesRecoveryMultiplier = determineLongRestMultiplier(featsUsesRecoveryMultSetting);
const othersUsesRecoveryMultSetting = game.settings.get("long-rest-hd-healing", "recovery-mult-uses-others");
const othersUsesRecoveryMultiplier = determineLongRestMultiplier(othersUsesRecoveryMultSetting);
const dayRecoveryMultSetting = game.settings.get("long-rest-hd-healing", "recovery-mult-day");
const dayRecoveryMultiplier = determineLongRestMultiplier(dayRecoveryMultSetting);
const results = wrapped({ recoverShortRestUses, recoverLongRestUses: false, recoverDailyUses: false });
for ( let item of this.items ) {
_recoverItemUses(
item,
recoverLongRestUses, recoverDailyUses,
featsUsesRecoveryMultiplier, othersUsesRecoveryMultiplier, dayRecoveryMultiplier,
results,
);
}
return results;
},
"WRAPPER",
);
function _recoverItemUses(
item,
recoverLongRestUses, recoverDailyUses,
featsUsesRecoveryMultiplier, othersUsesRecoveryMultiplier, dayRecoveryMultiplier,
results,
) {
const itemData = item.data.data;
if (itemData.uses) {
if (recoverLongRestUses && itemData.uses.per === "lr") {
const mult = item.type === "feat" ? featsUsesRecoveryMultiplier : othersUsesRecoveryMultiplier;
_recoverUses(item.id, itemData.uses.value, itemData.uses.max, mult, results);
} else if (recoverDailyUses && itemData.uses.per === "day") {
_recoverUses(item.id, itemData.uses.value, itemData.uses.max, dayRecoveryMultiplier, results);
}
} else if (recoverLongRestUses && itemData.recharge && itemData.recharge.value) {
results.push({ _id: item.id, "data.recharge.charged": true });
}
}
function _recoverUses(itemId, usesCurrentValue, usesMax, multiplier, results) {
if (multiplier === 0) return;
let amountToRecover = Math.max(Math.floor(usesMax * multiplier), 1);
let newValue = Math.min(usesCurrentValue + amountToRecover, usesMax);
results.push({ _id: itemId, "data.uses.value": newValue });
}
}
// Recover the multiplier based on setting
function determineLongRestMultiplier(multSetting) {
let recoveryMultiplier = 1;
switch (multSetting) {
case "none":
recoveryMultiplier = 0;
break;
case "quarter":
recoveryMultiplier = 0.25;
break;
case "half":
recoveryMultiplier = 0.5;
break;
case "full":
recoveryMultiplier = 1.0;
break;
default:
throw new Error(`Unable to parse recovery multiplier setting, got "${multSetting}".`);
}
return recoveryMultiplier;
}