forked from foundryvtt/dnd5e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnd5e.mjs
334 lines (289 loc) · 11.4 KB
/
dnd5e.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
/**
* The DnD5e game system for Foundry Virtual Tabletop
* A system for playing the fifth edition of the world's most popular role-playing game.
* Author: Atropos
* Software License: MIT
* Content License: https://www.dndbeyond.com/attachments/39j2li89/SRD5.1-CCBY4.0License.pdf
* Repository: https://github.com/foundryvtt/dnd5e
* Issue Tracker: https://github.com/foundryvtt/dnd5e/issues
*/
// Import Configuration
import DND5E from "./module/config.mjs";
import registerSystemSettings from "./module/settings.mjs";
// Import Submodules
import * as applications from "./module/applications/_module.mjs";
import * as canvas from "./module/canvas/_module.mjs";
import * as dataModels from "./module/data/_module.mjs";
import * as dice from "./module/dice/_module.mjs";
import * as documents from "./module/documents/_module.mjs";
import * as enrichers from "./module/enrichers.mjs";
import * as migrations from "./module/migration.mjs";
import * as utils from "./module/utils.mjs";
import {ModuleArt} from "./module/module-art.mjs";
/* -------------------------------------------- */
/* Define Module Structure */
/* -------------------------------------------- */
globalThis.dnd5e = {
applications,
canvas,
config: DND5E,
dataModels,
dice,
documents,
enrichers,
migrations,
utils
};
/* -------------------------------------------- */
/* Foundry VTT Initialization */
/* -------------------------------------------- */
Hooks.once("init", function() {
globalThis.dnd5e = game.dnd5e = Object.assign(game.system, globalThis.dnd5e);
console.log(`DnD5e | Initializing the DnD5e Game System - Version ${dnd5e.version}\n${DND5E.ASCII}`);
// Record Configuration Values
CONFIG.DND5E = DND5E;
CONFIG.ActiveEffect.documentClass = documents.ActiveEffect5e;
CONFIG.Actor.documentClass = documents.Actor5e;
CONFIG.Item.documentClass = documents.Item5e;
CONFIG.Token.documentClass = documents.TokenDocument5e;
CONFIG.Token.objectClass = canvas.Token5e;
CONFIG.time.roundTime = 6;
CONFIG.Dice.DamageRoll = dice.DamageRoll;
CONFIG.Dice.D20Roll = dice.D20Roll;
CONFIG.MeasuredTemplate.defaults.angle = 53.13; // 5e cone RAW should be 53.13 degrees
CONFIG.ui.combat = applications.combat.CombatTracker5e;
game.dnd5e.isV10 = game.release.generation < 11;
// Register System Settings
registerSystemSettings();
// Validation strictness.
if ( game.dnd5e.isV10 ) _determineValidationStrictness();
// Configure module art.
game.dnd5e.moduleArt = new ModuleArt();
// Remove honor & sanity from configuration if they aren't enabled
if ( !game.settings.get("dnd5e", "honorScore") ) delete DND5E.abilities.hon;
if ( !game.settings.get("dnd5e", "sanityScore") ) delete DND5E.abilities.san;
// Configure trackable & consumable attributes.
_configureTrackableAttributes();
_configureConsumableAttributes();
// Patch Core Functions
Combatant.prototype.getInitiativeRoll = documents.combat.getInitiativeRoll;
// Register Roll Extensions
CONFIG.Dice.rolls.push(dice.D20Roll);
CONFIG.Dice.rolls.push(dice.DamageRoll);
// Hook up system data types
const modelType = game.dnd5e.isV10 ? "systemDataModels" : "dataModels";
CONFIG.Actor[modelType] = dataModels.actor.config;
CONFIG.Item[modelType] = dataModels.item.config;
CONFIG.JournalEntryPage[modelType] = dataModels.journal.config;
// Register sheet application classes
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("dnd5e", applications.actor.ActorSheet5eCharacter, {
types: ["character"],
makeDefault: true,
label: "DND5E.SheetClassCharacter"
});
Actors.registerSheet("dnd5e", applications.actor.ActorSheet5eNPC, {
types: ["npc"],
makeDefault: true,
label: "DND5E.SheetClassNPC"
});
Actors.registerSheet("dnd5e", applications.actor.ActorSheet5eVehicle, {
types: ["vehicle"],
makeDefault: true,
label: "DND5E.SheetClassVehicle"
});
Actors.registerSheet("dnd5e", applications.actor.GroupActorSheet, {
types: ["group"],
makeDefault: true,
label: "DND5E.SheetClassGroup"
});
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("dnd5e", applications.item.ItemSheet5e, {
makeDefault: true,
label: "DND5E.SheetClassItem"
});
DocumentSheetConfig.registerSheet(JournalEntryPage, "dnd5e", applications.journal.JournalClassPageSheet, {
label: "DND5E.SheetClassClassSummary",
types: ["class"]
});
// Preload Handlebars helpers & partials
utils.registerHandlebarsHelpers();
utils.preloadHandlebarsTemplates();
enrichers.registerCustomEnrichers();
});
/**
* Determine if this is a 'legacy' world with permissive validation, or one where strict validation is enabled.
* @internal
*/
function _determineValidationStrictness() {
dataModels.SystemDataModel._enableV10Validation = game.settings.get("dnd5e", "strictValidation");
}
/**
* Update the world's validation strictness setting based on whether validation errors were encountered.
* @internal
*/
async function _configureValidationStrictness() {
if ( !game.user.isGM ) return;
const invalidDocuments = game.actors.invalidDocumentIds.size + game.items.invalidDocumentIds.size
+ game.scenes.invalidDocumentIds.size;
const strictValidation = game.settings.get("dnd5e", "strictValidation");
if ( invalidDocuments && strictValidation ) {
await game.settings.set("dnd5e", "strictValidation", false);
game.socket.emit("reload");
foundry.utils.debouncedReload();
}
}
/**
* Configure explicit lists of attributes that are trackable on the token HUD and in the combat tracker.
* @internal
*/
function _configureTrackableAttributes() {
const common = {
bar: [],
value: [
...Object.keys(DND5E.abilities).map(ability => `abilities.${ability}.value`),
...Object.keys(DND5E.movementTypes).map(movement => `attributes.movement.${movement}`),
"attributes.ac.value", "attributes.init.total"
]
};
const creature = {
bar: [...common.bar, "attributes.hp", "spells.pact"],
value: [
...common.value,
...Object.keys(DND5E.skills).map(skill => `skills.${skill}.passive`),
...Object.keys(DND5E.senses).map(sense => `attributes.senses.${sense}`),
"attributes.spelldc"
]
};
CONFIG.Actor.trackableAttributes = {
character: {
bar: [...creature.bar, "resources.primary", "resources.secondary", "resources.tertiary", "details.xp"],
value: [...creature.value]
},
npc: {
bar: [...creature.bar, "resources.legact", "resources.legres"],
value: [...creature.value, "details.cr", "details.spellLevel", "details.xp.value"]
},
vehicle: {
bar: [...common.bar, "attributes.hp"],
value: [...common.value]
},
group: {
bar: [],
value: []
}
};
}
/**
* Configure which attributes are available for item consumption.
* @internal
*/
function _configureConsumableAttributes() {
CONFIG.DND5E.consumableResources = [
...Object.keys(DND5E.abilities).map(ability => `abilities.${ability}.value`),
"attributes.ac.flat",
"attributes.hp.value",
...Object.keys(DND5E.senses).map(sense => `attributes.senses.${sense}`),
...Object.keys(DND5E.movementTypes).map(type => `attributes.movement.${type}`),
...Object.keys(DND5E.currencies).map(denom => `currency.${denom}`),
"details.xp.value",
"resources.primary.value", "resources.secondary.value", "resources.tertiary.value",
"resources.legact.value", "resources.legres.value",
"spells.pact.value",
...Array.fromRange(Object.keys(DND5E.spellLevels).length - 1, 1).map(level => `spells.spell${level}.value`)
];
}
/* -------------------------------------------- */
/* Foundry VTT Setup */
/* -------------------------------------------- */
/**
* Prepare attribute lists.
*/
Hooks.once("setup", function() {
CONFIG.DND5E.trackableAttributes = expandAttributeList(CONFIG.DND5E.trackableAttributes);
game.dnd5e.moduleArt.registerModuleArt();
// Apply custom compendium styles to the SRD rules compendium.
if ( !game.dnd5e.isV10 ) {
const rules = game.packs.get("dnd5e.rules");
rules.applicationClass = applications.journal.SRDCompendium;
}
});
/* --------------------------------------------- */
/**
* Expand a list of attribute paths into an object that can be traversed.
* @param {string[]} attributes The initial attributes configuration.
* @returns {object} The expanded object structure.
*/
function expandAttributeList(attributes) {
return attributes.reduce((obj, attr) => {
foundry.utils.setProperty(obj, attr, true);
return obj;
}, {});
}
/* --------------------------------------------- */
/**
* Perform one-time pre-localization and sorting of some configuration objects
*/
Hooks.once("i18nInit", () => utils.performPreLocalization(CONFIG.DND5E));
/* -------------------------------------------- */
/* Foundry VTT Ready */
/* -------------------------------------------- */
/**
* Once the entire VTT framework is initialized, check to see if we should perform a data migration
*/
Hooks.once("ready", function() {
if ( game.dnd5e.isV10 ) {
// Configure validation strictness.
_configureValidationStrictness();
// Apply custom compendium styles to the SRD rules compendium.
const rules = game.packs.get("dnd5e.rules");
rules.apps = [new applications.journal.SRDCompendium(rules)];
}
// Wait to register hotbar drop hook on ready so that modules could register earlier if they want to
Hooks.on("hotbarDrop", (bar, data, slot) => {
if ( ["Item", "ActiveEffect"].includes(data.type) ) {
documents.macro.create5eMacro(data, slot);
return false;
}
});
// Determine whether a system migration is required and feasible
if ( !game.user.isGM ) return;
const cv = game.settings.get("dnd5e", "systemMigrationVersion") || game.world.flags.dnd5e?.version;
const totalDocuments = game.actors.size + game.scenes.size + game.items.size;
if ( !cv && totalDocuments === 0 ) return game.settings.set("dnd5e", "systemMigrationVersion", game.system.version);
if ( cv && !isNewerVersion(game.system.flags.needsMigrationVersion, cv) ) return;
// Perform the migration
if ( cv && isNewerVersion(game.system.flags.compatibleMigrationVersion, cv) ) {
ui.notifications.error("MIGRATION.5eVersionTooOldWarning", {localize: true, permanent: true});
}
migrations.migrateWorld();
});
/* -------------------------------------------- */
/* Canvas Initialization */
/* -------------------------------------------- */
Hooks.on("canvasInit", gameCanvas => {
gameCanvas.grid.diagonalRule = game.settings.get("dnd5e", "diagonalMovement");
SquareGrid.prototype.measureDistances = canvas.measureDistances;
});
/* -------------------------------------------- */
/* Other Hooks */
/* -------------------------------------------- */
Hooks.on("renderChatMessage", documents.chat.onRenderChatMessage);
Hooks.on("getChatLogEntryContext", documents.chat.addChatMessageContextOptions);
Hooks.on("renderChatLog", (app, html, data) => documents.Item5e.chatListeners(html));
Hooks.on("renderChatPopout", (app, html, data) => documents.Item5e.chatListeners(html));
Hooks.on("getActorDirectoryEntryContext", documents.Actor5e.addDirectoryContextOptions);
/* -------------------------------------------- */
/* Bundled Module Exports */
/* -------------------------------------------- */
export {
applications,
canvas,
dataModels,
dice,
documents,
enrichers,
migrations,
utils,
DND5E
};