-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathalwayshp.js
623 lines (541 loc) · 24.9 KB
/
alwayshp.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import { registerSettings } from "./settings.js";
export let debug = (...args) => {
if (debugEnabled > 1) console.log("DEBUG: alwayshp | ", ...args);
};
export let log = (...args) => console.log("alwayshp | ", ...args);
export let warn = (...args) => {
if (debugEnabled > 0) console.warn("alwayshp | ", ...args);
};
export let error = (...args) => console.error("alwayshp | ", ...args);
export let i18n = key => {
return game.i18n.localize(key);
};
export let setting = key => {
return game.settings.get("always-hp", key);
};
export let patchFunc = (prop, func, type = "WRAPPER") => {
let nonLibWrapper = () => {
const oldFunc = eval(prop);
eval(`${prop} = function (event) {
return func.call(this, ${type != "OVERRIDE" ? "oldFunc.bind(this)," : ""} ...arguments);
}`);
}
if (game.modules.get("lib-wrapper")?.active) {
try {
libWrapper.register("always-hp", prop, func, type);
} catch (e) {
nonLibWrapper();
}
} else {
nonLibWrapper();
}
}
export class AlwaysHP extends Application {
tokenname = '';
tokenstat = '';
tokentemp = '';
tokentooltip = '';
color = "";
valuePct = null;
tempPct = null;
static get defaultOptions() {
let pos = game.user.getFlag("always-hp", "alwayshpPos");
return foundry.utils.mergeObject(super.defaultOptions, {
id: "always-hp",
template: "modules/always-hp/templates/alwayshp.html",
classes: ["always-hp"],
popOut: true,
resizable: false,
top: pos?.top || 60,
left: pos?.left || (($('#board').width / 2) - 150),
width: 300,
});
}
async _render(force, options) {
let that = this;
return super._render(force, options).then((html) => {
$('h4', this.element)
.empty()
.addClass('flexrow')
.append($('<div>').addClass('character-name').html(this.tokenname))
.append($('<div>').addClass('token-stats flexrow').attr('title', this.tokentooltip).html((this.tokentemp ? `<div class="stat temp">${this.tokentemp}</div>` : '') + (this.tokenstat ? `<div class="stat" style="background-color:${this.color}">${this.tokenstat}</div>` : '')));
delete ui.windows[that.appId];
this.refreshSelected();
});
}
async close(options) {
if (options?.properClose) {
super.close(options);
game.AlwaysHP.app = null;
}
}
getData() {
return {
tokenname: this.tokenname
};
}
getResourceValue(resource) {
return (resource instanceof Object ? resource.value : resource);
}
getResourceMax(resource) {
return (resource instanceof Object ? resource.max : null);
}
getResValue(resource, property = "value", defvalue = null) {
return (resource instanceof Object ? resource[property] : defvalue) ?? 0;
}
async changeHP(value, active) {
if (setting("wounds-system")) {
switch(value.value) {
case 'zero':
value.value = 'full';
break;
case 'full':
value.value = 'zero';
break;
}
}
let actors = canvas.tokens.controlled.flatMap((t) => {
if (t.actor?.type == "group") {
return Array.from(t.actor?.system.members);
} else
return t.actor;
});
for (let a of actors) {
if (!a || !(a instanceof Actor))
continue;
let tValue = foundry.utils.duplicate(value);
let resourcename = (setting("resourcename") || (game.system?.primaryTokenAttribute ?? game.data?.primaryTokenAttribute) || 'attributes.hp');
let resource = foundry.utils.getProperty(a, `system.${resourcename}`);
if (tValue.value == 'zero')
tValue.value = this.getResValue(resource, "value", resource) + this.getResValue(resource, "temp");
if (value.value == 'full')
tValue.value = (resource instanceof Object ? resource.value - resource.max : resource);
let defeatedStatus = CONFIG.specialStatusEffects.DEFEATED;
if (active != undefined && setting("add-defeated")) {
let status = CONFIG.statusEffects.find(e => e.id === defeatedStatus);
let effect = game.system.id == "pf2e" ? game.settings.get("pf2e", "deathIcon") : a && status ? status : CONFIG.controlIcons.defeated;
const exists = a.statuses.has(effect.id);
if (exists != active)
await a.toggleStatusEffect(effect.id, { active: (active == 'toggle' ? !exists : active) });
}
if (active === false && setting("clear-savingthrows")) {
a.update({
"system.attributes.death.failure": 0,
"system.attributes.death.success": 0
});
}
log('applying damage', a, tValue);
if (tValue.value != 0) {
await this.applyDamage(a, tValue);
}
};
this.refreshSelected();
}
async applyDamage(actor, amount, multiplier = 1) {
let { value, target } = amount;
let updates = {};
let resourcename = (setting("resourcename") || (game.system?.primaryTokenAttribute ?? game.data?.primaryTokenAttribute) || 'attributes.hp');
let resource = foundry.utils.getProperty(actor, `system.${resourcename}`);
if (resource instanceof Object) {
value = Math.floor(parseInt(value) * multiplier);
// Deduct damage from temp HP first
if (resource.hasOwnProperty("tempmax") && target == "max") {
const dm = (resource.tempmax ?? 0) - value;
updates[`system.${resourcename}.tempmax`] = dm;
} else {
let dt = 0;
let tmpMax = 0;
if (resource.hasOwnProperty("temp")) {
const tmp = parseInt(resource.temp) || 0;
dt = (value > 0 || target == 'temp') && target != 'regular' && target != 'max' ? Math.min(tmp, value) : 0;
// Remaining goes to health
tmpMax = parseInt(resource.tempmax) || 0;
updates[`system.${resourcename}.temp`] = tmp - dt;
}
// Update the Actor
if (target != 'temp' && target != 'max' && dt >= 0) {
let change = (value - dt);
const dh = Math.clamp(resource.value - change, (game.system.id == 'D35E' || game.system.id == 'pf1' ? -2000 : 0), resource.max + tmpMax);
updates[`system.${resourcename}.value`] = dh;
}
}
} else {
let val = Math.floor(parseInt(resource));
updates[`system.${resourcename}`] = (val - value);
}
return await actor.update(updates);
}
sendMessage(dh, dt) {
const speaker = ChatMessage.getSpeaker({ user: game.user.id });
let messageData = {
user: game.user.id,
speaker: speaker,
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
whisper: ChatMessage.getWhisperRecipients("GM").map(u => u.id),
content: `${actor.name} has changed HP by: ${dt + dh}` + (dt != 0 ? `<small><br/>Temporary: ${dt}<br/>HP: ${dh}</small>` : '')
};
ChatMessage.create(messageData);
}
refreshSelected() {
this.valuePct = null;
this.tokenstat = "";
this.tokentemp = "";
this.tokentooltip = "";
$('.character-name', this.element).removeClass("single");
if (canvas.tokens?.controlled.length == 0)
this.tokenname = "";
else if (canvas.tokens?.controlled.length == 1) {
let a = canvas.tokens.controlled[0].actor;
if (!a)
this.tokenname = "";
else {
$('.character-name', this.element).addClass("single");
let resourcename = setting("resourcename");
let resource = foundry.utils.getProperty(a, `system.${resourcename}`);
let value = this.getResValue(resource, "value", resource);
let max = this.getResValue(resource, "max");
if (setting("wounds-system")) value = max - value;
let temp = this.getResValue(resource, "temp");
let tempmax = this.getResValue(resource, "tempmax");
// Differentiate between effective maximum and displayed maximum
const effectiveMax = Math.max(0, max + tempmax);
let displayMax = max + (tempmax > 0 ? tempmax : 0);
// Allocate percentages of the total
const tempPct = Math.clamp(temp, 0, displayMax) / displayMax;
const valuePct = Math.clamp(value, 0, effectiveMax) / displayMax;
this.valuePct = valuePct;
this.tempPct = tempPct;
const color = [(1 - (this.valuePct / 2)), this.valuePct, 0];
this.color = `rgba(${parseInt(color[0] * 255)},${parseInt(color[1] * 255)},${parseInt(color[2] * 255)}, 0.7)`;
this.tokenname = canvas.tokens.controlled[0]?.name ?? canvas.tokens.controlled[0]?.data?.name;
this.tokenstat = value;
this.tokentemp = temp;
this.tokentooltip = `HP: ${value}, Temp: ${temp}, Max: ${max}`;
}
}
else {
this.tokenname = `${i18n("ALWAYSHP.Multiple")} <span class="count">${canvas.tokens.controlled.length}</span>`;
}
this.changeToken();
}
addDeathST(save, value) {
if (canvas.tokens.controlled.length == 1) {
let a = canvas.tokens.controlled[0].actor;
if (!a)
return;
let prop = a.system.attributes.death;
prop[save ? 'success' : 'failure'] = Math.max(0, Math.min(3, prop[save ? 'success' : 'failure'] + value));
let updates = {};
updates["system.attributes.death." + (save ? 'success' : 'failure')] = prop[save ? 'success' : 'failure'];
canvas.tokens.controlled[0].actor.update(updates);
this.changeToken();
}
}
changeToken() {
$('.character-name', this.element).html(this.tokenname);
$('.token-stats', this.element).attr('title', this.tokentooltip).html((this.tokentemp ? `<div class="stat temp">${this.tokentemp}</div>` : '') + (this.tokenstat ? `<div class="stat" style="background-color:${this.color}">${this.tokenstat}</div>` : ''));
let actor = (canvas.tokens.controlled.length == 1 ? canvas.tokens.controlled[0].actor : null);
let data = actor?.system;
let showST = (actor != undefined && game.system.id == "dnd5e" && data?.attributes?.hp?.value == 0 && actor?.hasPlayerOwner && setting("show-savingthrows"));
$('.death-savingthrow', this.element).css({ display: (showST ? 'inline-block' : 'none') });
if (showST && data.attributes.death) {
$('.death-savingthrow.fail > div', this.element).each(function (idx) { $(this).toggleClass('active', idx < data.attributes.death.failure) });
$('.death-savingthrow.save > div', this.element).each(function (idx) { $(this).toggleClass('active', idx < data.attributes.death.success) });
}
$('.resource', this.element).toggle(canvas.tokens.controlled.length == 1 && this.valuePct != undefined);
if (this.valuePct != undefined) {
$('.resource .bar', this.element).css({ width: (this.valuePct * 100) + '%', backgroundColor: this.color });
$('.resource .temp-bar', this.element).toggle(this.tempPct > 0).css({ width: (this.tempPct * 100) + '%' });
}
}
get getValue() {
let value = $('#alwayshp-hp', this.element).val();
let result = { value: value };
if (value.indexOf("r") > -1 || value.indexOf("R") > -1) {
result.target = "regular";
result.value = result.value.replace('r', '').replace('R', '');
}
if (value.indexOf("t") > -1 || value.indexOf("T") > -1) {
result.target = "temp";
result.value = result.value.replace('t', '').replace('T', '');
}
if (value.indexOf("m") > -1 || value.indexOf("M") > -1) {
result.target = "max";
result.value = result.value.replace('m', '').replace('M', '');
}
result.value = parseInt(result.value);
if (isNaN(result.value))
result.value = 1;
return result;
}
clearInput() {
if (setting("clear-after-enter"))
$('#alwayshp-hp', this.element).val('');
}
getChangeValue(perc) {
let change = "";
if (canvas.tokens.controlled.length == 1 && canvas.tokens.controlled[0].actor?.type != "group") {
const actor = canvas.tokens.controlled[0].actor;
if (!actor)
return;
let resourcename = (setting("resourcename") || (game.system.primaryTokenAttribute ?? game.system.data.primaryTokenAttribute) || 'attributes.hp');
let resource = foundry.utils.getProperty(actor, `system.${resourcename}`);
if (resource.hasOwnProperty("max")) {
let max = this.getResValue(resource, "max");
let tempmax = this.getResValue(resource, "tempmax");
const effectiveMax = Math.max(0, max + tempmax);
let val = Math.floor(parseInt(effectiveMax * perc));
if (val >= 0)
val++;
change = val - Math.floor(parseInt(resource.value));
}
}
return change;
}
activateListeners(html) {
super.activateListeners(html);
let that = this;
html.find('#alwayshp-btn-dead').click(ev => {
ev.preventDefault();
if (ev.shiftKey == true)
this.changeHP({ value: 0 }, 'toggle');
else {
log('set character to dead');
this.changeHP({ value: 'zero' }, true);
this.clearInput();
}
}).contextmenu(ev => {
ev.preventDefault();
log('set character to hurt');
this.changeHP({ value: 'zero' });
this.clearInput();
});
html.find('#alwayshp-btn-hurt').click(ev => {
ev.preventDefault();
log('set character to hurt');
let value = this.getValue;
if (value.value != '') {
value.value = Math.abs(value.value);
if (setting("wounds-system")) value.value = value.value * -1;
this.changeHP(value);
}
this.clearInput();
});
html.find('#alwayshp-btn-heal').click(ev => {
ev.preventDefault();
log('set character to heal');
let value = this.getValue;
if (value.value != '') {
value.value = -Math.abs(value.value);
if (setting("wounds-system")) value.value = value.value * -1;
this.changeHP(value, false);
}
this.clearInput();
});
html.find('#alwayshp-btn-fullheal').click(ev => {
ev.preventDefault();
log('set character to fullheal');
this.changeHP({ value: 'full' }, false);
this.clearInput();
}).contextmenu(ev => {
ev.preventDefault();
log('set character to heal');
this.changeHP({ value: 'full' });
this.clearInput();
});
if (setting('double-click')) {
html.find('#alwayshp-btn-hurt').dblclick(ev => {
ev.preventDefault();
log('set character to hurt');
this.changeHP({ value: 'zero' });
this.clearInput();
});
html.find('#alwayshp-btn-heal').dblclick(ev => {
ev.preventDefault();
log('set character to heal');
this.changeHP({ value: 'full' });
this.clearInput();
});
}
html.find('#alwayshp-hp').focus(ev => {
ev.preventDefault();
let elem = ev.target;
if (elem.setSelectionRange) {
elem.focus();
elem.setSelectionRange(0, $(elem).val().length);
} else if (elem.createTextRange) {
var range = elem.createTextRange();
range.collapse(true);
range.moveEnd('character', $(elem).val().length);
range.moveStart('character', 0);
range.select();
}
}).keypress(ev => {
if (ev.which == 13) {
let value = this.getValue;
if (value.value != '' && value.value != 0) {
ev.preventDefault();
let rawvalue = $('#alwayshp-hp', this.element).val();
if (setting("wounds-system"))
value.value = (rawvalue.startsWith('+') || (!rawvalue.startsWith('-') && !setting("no-sign-negative")) ? Math.abs(value.value) : -Math.abs(value.value));
else
value.value = (rawvalue.startsWith('+') || (!rawvalue.startsWith('-') && !setting("no-sign-negative")) ? -Math.abs(value.value) : Math.abs(value.value));
this.changeHP(value); //Heal with a + but everything else is a hurt
this.clearInput();
}
}
});
html.find('.death-savingthrow').click(ev => {
ev.preventDefault();
log('add death saving throw');
this.addDeathST($(ev.currentTarget).hasClass('save'), 1);
}).contextmenu(ev => {
ev.preventDefault();
log('remove death saving throw');
this.addDeathST($(ev.currentTarget).hasClass('save'), -1);
});
html.find('.resource').mousemove(ev => {
if (!setting("allow-bar-click"))
return;
let perc = ev.offsetX / $(ev.currentTarget).width();
if (setting("wounds-system")) perc = 1 - perc;
let change = this.getChangeValue(perc);
if (setting("wounds-system")) change = change * -1;
$('.bar-change', html).html(change);
log("resource change");
}).click(ev => {
if (!setting("allow-bar-click"))
return;
let perc = ev.offsetX / $(ev.currentTarget).width();
if (setting("wounds-system")) perc = 1 - perc;
let change = this.getChangeValue(perc);
this.changeHP({ value: -change, target: 'regular' });
$('.bar-change', html).html('');
});
html.find('.bar-change').mousemove(ev => {
ev.preventDefault;
ev.stopPropagation();
log("bar change");
});
}
}
Hooks.on('init', () => {
registerSettings();
game.keybindings.register('always-hp', 'toggle-key', {
name: 'ALWAYSHP.toggle-key.name',
hint: 'ALWAYSHP.toggle-key.hint',
editable: [],
onDown: () => {
game.AlwaysHP.toggleApp();
},
});
game.keybindings.register('always-hp', 'focus-key', {
name: 'ALWAYSHP.focus-key.name',
hint: 'ALWAYSHP.focus-key.hint',
editable: [],
onDown: () => {
if (!game.AlwaysHP.app)
game.AlwaysHP.app = new AlwaysHP().render(true);
else
game.AlwaysHP.app.bringToTop();
$('#alwayshp-hp', game.AlwaysHP.app.element).focus();
},
});
game.AlwaysHP = {
app: null,
toggleApp: (show = 'toggle') => {
if (show == 'toggle') show = !game.AlwaysHP.app;
if (show && !game.AlwaysHP.app) {
game.AlwaysHP.app = new AlwaysHP().render(true);
} else if (!show && game.AlwaysHP.app)
game.AlwaysHP.app.close({ properClose: true });
},
refresh: () => {
if (game.AlwaysHP.app)
game.AlwaysHP.app.refreshSelected();
}
};
});
Hooks.on('ready', () => {
let r = document.querySelector(':root');
r.style.setProperty('--ahp-heal-dark', setting("heal-dark"));
r.style.setProperty('--ahp-heal-light', setting("heal-light"));
r.style.setProperty('--ahp-hurt-dark', setting("hurt-dark"));
r.style.setProperty('--ahp-hurt-light', setting("hurt-light"));
if ((setting("show-option") == 'on' || (setting("show-option") == 'toggle' && setting("show-dialog"))) && (setting("load-option") == 'everyone' || (setting("load-option") == 'gm' == game.user.isGM)))
game.AlwaysHP.toggleApp(true);
if (setting("show-option") == "combat" && game.combats.active && game.combats.active.started && !game.AlwaysHP)
game.AlwaysHP.toggleApp(true);
if (!game.modules.get('monks-combat-details')?.active && !game.modules.get('monks-enhanced-journal')?.active && !game.modules.get('monks-common-display')?.active) {
patchFunc("Draggable.prototype._onDragMouseUp", async function (wrapped, ...args) {
try {
if (this.app.constructor._getInheritanceChain) {
for (const cls of this.app.constructor._getInheritanceChain()) {
Hooks.callAll(`dragEnd${cls.name}`, this.app, this.app.position);
}
} else {
Hooks.callAll(`dragEnd${this.app.constructor.name}`, this.app, this.app.position);
}
} catch (e) { }
return wrapped(...args);
});
}
});
Hooks.on('controlToken', () => {
if (setting("show-option") == "token") {
if (canvas.tokens.controlled.length == 0) // delay a second to make sure we aren't selecting a new token
window.setTimeout(() => { if (canvas.tokens.controlled.length == 0) game.AlwaysHP.toggleApp(false); }, 100);
else if (!game.AlwaysHP.app)
game.AlwaysHP.toggleApp(true);
else
game.AlwaysHP.refresh();
} else
game.AlwaysHP.refresh();
});
Hooks.on('updateActor', (actor, data) => {
//log('Updating actor', actor, data);
if (canvas.tokens.controlled.length == 1
&& canvas.tokens.controlled[0]?.actor?.id == actor.id
&& (foundry.utils.getProperty(data, "system.attributes.death") != undefined || foundry.utils.getProperty(data, `system.${setting("resourcename") }`))) {
game.AlwaysHP.refresh();
}
});
Hooks.on('updateCombat', (combat, data) => {
if (setting("show-option") == "combat") {
game.AlwaysHP.toggleApp(game.combats.active && game.combats.active.started);
}
});
Hooks.on('deleteCombat', (combat, data) => {
if (setting("show-option") == "combat") {
game.AlwaysHP.toggleApp(game.combats.active && game.combats.active.started);
}
});
Hooks.on('dragEndAlwaysHP', (app) => {
game.user.setFlag("always-hp", "alwayshpPos", { left: app.position.left, top: app.position.top });
})
Hooks.on("getSceneControlButtons", (controls) => {
if (setting("show-option") == 'toggle' && (setting("load-option") == 'everyone' || (setting("load-option") == 'gm' == game.user.isGM))) {
let tokenControls = controls.find(control => control.name === "token")
tokenControls.tools.push({
name: "toggledialog",
title: "ALWAYSHP.toggledialog",
icon: "fas fa-briefcase-medical",
toggle: true,
active: setting('show-dialog'),
onClick: (toggled) => {
game.settings.set('always-hp', 'show-dialog', toggled);
game.AlwaysHP.toggleApp(toggled);
}
});
}
});
Hooks.on("renderSettingsConfig", (app, html, user) => {
$("input[name='always-hp.heal-dark']", html).replaceWith(`
<color-picker name="always-hp.heal-light" value="${setting('heal-light') || '#15838d'}"></color-picker>
<color-picker name="always-hp.heal-dark" value="${setting('heal-dark') || '#4dd0e1'}"></color-picker>
`);
$("input[name='always-hp.hurt-dark']", html).replaceWith(`
<color-picker name="always-hp.hurt-light" value="${setting('hurt-light') || '#ff6400'}"></color-picker>
<color-picker name="always-hp.hurt-dark" value="${setting('hurt-dark') || '#ff0000'}"></color-picker>
`);
});