-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathartichron.mjs
366 lines (319 loc) · 12.4 KB
/
artichron.mjs
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
import {registerSockets} from "./module/helpers/sockets.mjs";
import {SYSTEM} from "./module/helpers/config.mjs";
import * as documents from "./module/documents/_module.mjs";
import * as migrations from "./module/helpers/migrations.mjs";
import * as utils from "./module/helpers/utils.mjs";
import activities from "./module/documents/activity/_module.mjs";
import applications from "./module/applications/_module.mjs";
import canvas from "./module/documents/canvas/_module.mjs";
import dice from "./module/dice/_module.mjs";
import elements from "./module/elements/_module.mjs";
import fields from "./module/documents/fields/_module.mjs";
import registerEnrichers from "./module/helpers/enrichers.mjs";
import registerSettings from "./module/helpers/settings.mjs";
// Custom elements.
for (const element of Object.values(elements)) {
window.customElements.define(element.tagName, element);
}
/* -------------------------------------------------- */
/* Define module structure */
/* -------------------------------------------------- */
globalThis.artichron = {
activities: activities,
applications: applications,
canvas: canvas,
config: SYSTEM,
dataModels: documents.dataModels,
dice: dice,
documents: documents.documentClasses,
elements: elements,
fields: fields,
migrations: migrations,
tooltips: new applications.TooltipsArtichron(),
utils: utils
};
/* -------------------------------------------------- */
/* Init hook */
/* -------------------------------------------------- */
Hooks.once("init", function() {
registerSettings();
registerEnrichers();
registerSockets();
CONFIG.Canvas.dispositionColors.CONTROLLED = 2502655;
CONFIG.ActiveEffect.legacyTransferral = false;
// Record Configuration Values
CONFIG.SYSTEM = SYSTEM;
CONFIG.Token.hudClass = applications.TokenHUDArtichron;
CONFIG.Canvas.rulerClass = canvas.RulerArtichron;
// Hook up document classes.
for (const [k, v] of Object.entries(documents.documentClasses)) {
CONFIG[k].documentClass = v;
}
// Hook up system data types.
for (const [key, dm] of Object.entries(documents.dataModels)) {
Object.assign(CONFIG[key].dataModels, dm);
for (const [k, v] of Object.entries(dm)) {
CONFIG[key].typeIcons[k] = v.metadata.icon;
}
}
// Hook up dice types.
CONFIG.Dice.rolls[0] = dice.RollArtichron;
CONFIG.Dice.rolls.push(dice.DamageRoll);
CONFIG.Dice.Roll = dice.RollArtichron;
CONFIG.Dice.DamageRoll = dice.DamageRoll;
// Set system combat tracker application.
CONFIG.ui.combat = applications.CombatTrackerArtichron;
// Register sheet application classes
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("artichron", applications.HeroSheet, {
makeDefault: true,
label: "ARTICHRON.SHEET.ACTOR.Hero",
types: ["hero"]
});
Actors.registerSheet("artichron", applications.MonsterSheet, {
makeDefault: true,
label: "ARTICHRON.SHEET.ACTOR.Monster",
types: ["monster"]
});
Actors.registerSheet("artichron", applications.MerchantSheet, {
makeDefault: true,
label: "ARTICHRON.SHEET.ACTOR.Merchant",
types: ["merchant"]
});
Actors.registerSheet("artichron", applications.PartySheet, {
makeDefault: true,
label: "ARTICHRON.SHEET.ACTOR.Party",
types: ["party"]
});
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("artichron", applications.ItemSheetArtichron, {
makeDefault: true,
label: "ARTICHRON.SHEET.ITEM.Base",
types: ["weapon", "shield", "spell", "armor"]
});
Items.registerSheet("artichron", applications.ItemSheetAmmunition, {
makeDefault: true,
label: "ARTICHRON.SHEET.ITEM.Ammunition",
types: ["ammo"]
});
Items.registerSheet("artichron", applications.ItemSheetElixir, {
makeDefault: true,
label: "ARTICHRON.SHEET.ITEM.Elixir",
types: ["elixir"]
});
Items.registerSheet("artichron", applications.ItemSheetPart, {
makeDefault: true,
label: "ARTICHRON.SHEET.ITEM.Part",
types: ["part"]
});
DocumentSheetConfig.unregisterSheet(ActiveEffect, "core", ActiveEffectConfig);
DocumentSheetConfig.registerSheet(ActiveEffect, "artichron", applications.ActiveEffectSheetArtichron, {
makeDefault: true,
label: "ARTICHRON.SHEET.EFFECT.Base"
});
// Set up conditions.
CONFIG.statusEffects = [];
for (const [id, config] of Object.entries(SYSTEM.STATUS_CONDITIONS)) {
CONFIG.statusEffects.push({...config, id: id, _id: utils.staticId(id)});
}
CONFIG.specialStatusEffects.DEFEATED = "defeated";
CONFIG.specialStatusEffects.BURROW = "underground";
CONFIG.specialStatusEffects.FLY = "flying";
CONFIG.specialStatusEffects.HOVER = "levitating";
});
/* -------------------------------------------------- */
/* Setup hook */
/* -------------------------------------------------- */
Hooks.once("setup", function() {
Handlebars.registerHelper({
batteryProgress: batteryProgress,
effectEntry: effectEntry,
inventoryItem: inventoryItem,
progressClock: progressClock,
thresholdBar: thresholdBar
});
artichron.tooltips.observe();
applications.TooltipsArtichron.activateListeners();
});
/* -------------------------------------------------- */
/* i18nInit hook */
/* -------------------------------------------------- */
Hooks.once("i18nInit", function() {
// Localize all strings in the global system configuration object.
const localize = (o, k, v) => {
const type = foundry.utils.getType(v);
if ((type === "string") && v.startsWith("ARTICHRON")) {
o[k] = game.i18n.localize(v);
} else if (type === "Object") {
for (const [x, y] of Object.entries(v)) {
localize(v, x, y);
}
} else if (type === "Array") {
for (const k of v) if (foundry.utils.getType(k) === "Object") {
for (const [u, w] of Object.entries(k)) {
localize(k, u, w);
}
}
}
};
for (const [k, v] of Object.entries(CONFIG.SYSTEM)) {
localize(CONFIG.SYSTEM, k, v);
}
// Localize data models.
for (const model of Object.values(artichron.fields.ArmorRequirementData.TYPES)) {
Localization.localizeDataModel(model);
const meta = model.metadata;
model.metadata = foundry.utils.mergeObject(meta, {
label: game.i18n.localize(meta.label),
hint: game.i18n.localize(meta.hint)
}, {inplace: false});
}
for (const model of Object.values(artichron.fields.ProgressionData.TYPES)) {
Localization.localizeDataModel(model);
}
// Localize party actor clocks schema.
const clocks = CONFIG.Actor.dataModels.party.schema.getField("clocks").constructor.TYPES;
for (const cls of Object.values(clocks)) Localization.localizeDataModel(cls);
// Localize activities.
for (const model of Object.values(artichron.activities)) Localization.localizeDataModel(model);
// Localize formula models.
Localization.localizeDataModel(artichron.fields.FormulaModel);
Localization.localizeDataModel(artichron.fields.DamageFormulaModel);
// Explicitly localize this embedded data model due to unknown reasons.
for (const v of Object.values(CONFIG.Item.dataModels)) {
const schema = v.schema.getField("attributes.levels");
if (schema) Localization.localizeDataModel({schema: schema});
}
});
/* -------------------------------------------------- */
/* Ready hook */
/* -------------------------------------------------- */
Hooks.once("ready", function() {
Hooks.on("hotbarDrop", function(bar, data, slot) {
if (data.type === "ActiveEffect") {
createEffectMacro(data, slot);
return false;
}
if (data.type === "Item") {
createItemMacro(data, slot);
return false;
}
});
setupCarousel();
});
/* -------------------------------------------------- */
/* Sidebar diretories */
/* -------------------------------------------------- */
Hooks.on("getActorDirectoryEntryContext", (directory, options) => {
options.push({
name: "ARTICHRON.ContextMenu.Directory.AssignPrimaryParty",
icon: "<i class='fa-solid fa-fw fa-medal'></i>",
condition: ([li]) => {
const actor = game.actors.get(li.dataset.documentId);
const current = game.settings.get("artichron", "primaryParty")?.actor;
return game.user.isGM && (actor.type === "party") && (actor !== current);
},
callback: ([li]) => game.settings.set("artichron", "primaryParty", {actor: game.actors.get(li.dataset.documentId)}),
group: "system"
}, {
name: "ARTICHRON.ContextMenu.Directory.RemovePrimaryParty",
icon: "<i class='fa-solid fa-fw fa-times'></i>",
condition: ([li]) => {
const actor = game.actors.get(li.dataset.documentId);
const current = game.settings.get("artichron", "primaryParty")?.actor;
return game.user.isGM && (actor === current);
},
callback: ([li]) => game.settings.set("artichron", "primaryParty", {actor: null}),
group: "system"
});
});
Hooks.on("renderActorDirectory", (directory, [html]) => {
const current = game.settings.get("artichron", "primaryParty")?.actor?.id;
if (!current) return;
const entry = html.querySelector(`[data-document-id="${current}"]`);
if (entry) entry.classList.add("primary-party");
});
/* -------------------------------------------------- */
/**
* Create a new carousel combat tracker and render it.
*/
async function setupCarousel() {
const cls = applications.CombatCarousel;
const app = new cls();
app.render({force: true});
}
/* -------------------------------------------------- */
/* Hotbar macros */
/* -------------------------------------------------- */
/**
* Create a Macro from an ActiveEffect drop.
* Get an existing macro if one exists, otherwise create a new one.
* @param {object} data The dropped data.
* @param {number} slot The hotbar slot to use.
*/
async function createEffectMacro(data, slot) {
const effect = await ActiveEffect.implementation.fromDropData(data);
const command = `artichron.utils.macro.toggleEffect("${effect.name}");`;
const name = game.i18n.format("ARTICHRON.MACRO.ToggleEffect", {name: effect.name});
let macro = game.macros.find(m => (m.name === name) && (m.command === command));
if (!macro) {
macro = await Macro.implementation.create({
name: name,
type: "script",
img: effect.img,
command: command
});
}
game.user.assignHotbarMacro(macro, slot);
}
/* -------------------------------------------------- */
/**
* Create a Macro from an Item drop.
* Get an existing macro if one exists, otherwise create a new one.
* @param {object} data The dropped data.
* @param {number} slot The hotbar slot to use.
*/
async function createItemMacro(data, slot) {
const item = await Item.implementation.fromDropData(data);
const command = `artichron.utils.macro.useItem("${item.name}", event);`;
const name = game.i18n.format("ARTICHRON.MACRO.UseItem", {name: item.name});
let macro = game.macros.find(m => (m.name === name) && (m.command === command));
if (!macro) {
macro = await Macro.implementation.create({
name: name,
type: "script",
img: item.img,
command: command
});
}
game.user.assignHotbarMacro(macro, slot);
}
/* -------------------------------------------------- */
/* Handlebars helpers */
/* -------------------------------------------------- */
function inventoryItem(item, options) {
const element = elements.InventoryItemElement.create({...options.hash, item});
return new Handlebars.SafeString(element.outerHTML);
}
function effectEntry(effect, options) {
const element = elements.EffectEntryElement.create({...options.hash, effect});
return new Handlebars.SafeString(element.outerHTML);
}
function thresholdBar(options) {
const element = elements.ThresholdBarElement.create(options.hash);
return new Handlebars.SafeString(element.outerHTML);
}
function batteryProgress(field, options) {
let {min, max, name, value, step, ...inputConfig} = options.hash;
min ??= field.min;
max ??= field.max;
name ??= field.fieldPath;
value ??= field.initial;
step ??= 1;
const element = elements.BatteryProgressElement.create({...inputConfig, min, max, value, step, name});
return new Handlebars.SafeString(element.outerHTML);
}
function progressClock(options) {
const element = elements.ProgressClockElement.create(options.hash);
return new Handlebars.SafeString(element.outerHTML);
}