forked from Sk1mble/modularFate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModularFate.js
526 lines (457 loc) · 18.8 KB
/
ModularFate.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
/**
* The Fate Core game system for Foundry Virtual Tabletop
* Author: Richard Bellingham, partially based on work by Nick van Oosten (NickEast)
* Software License: GNU GPLv3
* Content License:
* This work is based on Fate Core System and Fate Accelerated Edition (found at http://www.faterpg.com/),
* products of Evil Hat Productions, LLC, developed, authored, and edited by Leonard Balsera, Brian Engard,
* Jeremy Keller, Ryan Macklin, Mike Olson, Clark Valentine, Amanda Valentine, Fred Hicks, and Rob Donoghue,
* and licensed for our use under the Creative Commons Attribution 3.0 Unported license
* (http://creativecommons.org/licenses/by/3.0/).
*
* This work is based on Fate Condensed (found at http://www.faterpg.com/), a product of Evil Hat Productions, LLC,
* developed, authored, and edited by PK Sullivan, Ed Turner, Leonard Balsera, Fred Hicks, Richard Bellingham, Robert Hanz, Ryan Macklin,
* and Sophie Lagacé, and licensed for our use under the Creative Commons Attribution 3.0 Unported license (http://creativecommons.org/licenses/by/3.0/).
*
*
* Note to self: New standardised hook signatures:
* preCreate[documentName](document:Document, data:object, options:object, userId:string) {}
* create[documentName](document:Document, options:object, userId: string) {}
* preUpdate[documentName](document:Document, change:object, options:object, userId: string) {}
* update[documentName](document:Document, change:object, options:object, userId: string) {}
* preDelete[documentName](document:Document, options:object, userId: string) {}
* delete[documentName](document:Document, options:object, userId: string) {}
*/
/* -------------------------------- */
/* System initialization */
/* -------------------------------- */
import { ModularFateCharacter } from "./scripts/ModularFateCharacter.js"
import { ExtraSheet } from "./scripts/ExtraSheet.js"
import { Thing } from "./scripts/Thing.js"
import { ModularFateActor } from "./scripts/ModularFateActor.js"
Hooks.on("preCreateActor", async (actor, data, options, userId) => {
if (actor.type == "Thing"){
if (!options.thing){
ui.notifications.error(game.i18n.localize("ModularFate.CantCreateThing"));
return false;
}
}
if (actor.type == "ModularFate"){
if (game.user == game.users.find(e => e.isGM && e.active) || game.user.id === userId){
if (actor?.data?.data?.details?.fatePoints?.refresh === ""){
let modified_data = await initialiseModularFateCharacter(actor);
data.data = modified_data.data;
}
}
}
});
async function initialiseModularFateCharacter (actor) {
//Modifies the data of the supplied actor to add tracks, aspects, etc. from system settings, then returns the data.
let working_data = actor.data.toJSON();
// Logic to set up Refresh and Current
let refresh = game.settings.get("ModularFate", "refreshTotal");
working_data.data.details.fatePoints.refresh = refresh;
working_data.data.details.fatePoints.current = refresh;
let p_skills=working_data.data.skills;
//Check to see what skills the character has compared to the global skill list
var skill_list = game.settings.get("ModularFate","skills");
// This is the number of skills the character has currently.
//We only need to add any skills if this is currently 0,
let skills_to_add = [];
for (let w in skill_list){
let w_skill = skill_list[w];
if (p_skills[w]!=undefined){
} else {
if(w_skill.pc){
skills_to_add.push(w_skill);
}
}
}
if (skills_to_add.length >0){
//Add any skills from the global list that they don't have at rank 0.
skills_to_add.forEach(skill => {
skill.rank=0;
p_skills[skill.name]=skill;
})
}
let aspects = game.settings.get("ModularFate", "aspects");
let player_aspects = duplicate(aspects);
for (let a in player_aspects) {
player_aspects[a].value = "";
}
//Now to store the aspect list to the character
working_data.data.aspects = player_aspects;
//Step one, get the list of universal tracks.
let world_tracks = duplicate(game.settings.get("ModularFate", "tracks"));
let tracks_to_write = working_data.data.tracks;
for (let t in world_tracks) {
let track = world_tracks[t];
if (track.universal == true) {
tracks_to_write[t] = world_tracks[t];
}
}
for (let t in tracks_to_write) {
let track = tracks_to_write[t];
//Add a notes field. This is a bit redundant for stress tracks,
//but useful for aspects, indebted, etc. Maybe it's configurable whether we show the
//notes or not for any given track. LATER NOTE: It is not.
track.notes = "";
//If this box is an aspect when marked, it needs an aspect.name data field.
if (track.aspect == game.i18n.localize("ModularFate.DefinedWhenMarked")) {
track.aspect = {};
track.aspect.name = "";
track.aspect.when_marked = true;
track.aspect.as_name = false;
}
if (track.aspect == game.i18n.localize("ModularFate.AspectAsName")) {
track.aspect = {};
track.aspect.name = "";
track.aspect.when_marked = true;
track.aspect.as_name = false;
}
//Initialise the box array for this track
if (track.boxes > 0) {
let box_values = [];
for (let i = 0; i < track.boxes; i++) {
box_values.push(false);
}
track.box_values = box_values;
}
}
working_data.data.tracks = tracks_to_write;
let tracks = working_data.data.tracks;
let categories = game.settings.get("ModularFate", "track_categories");
//GO through all the tracks, find the ones with boxes, check the number of boxes and linked skills and initialise as necessary.
for (let t in tracks) {
let track = tracks[t];
if (track.universal) {
track.enabled = true;
}
// Check for linked skills and enable/add boxes as necessary.
if (track.linked_skills != undefined && track.linked_skills.length > 0 && Object.keys(working_data.data.skills).length > 0) {
let skills = working_data.data.skills;
let linked_skills = tracks[t].linked_skills;
let box_mod = 0;
for (let i = 0; i < linked_skills.length; i++) {
let l_skill = linked_skills[i].linked_skill;
let l_skill_rank = linked_skills[i].rank;
let l_boxes = linked_skills[i].boxes;
let l_enables = linked_skills[i].enables;
//Get the value of the player's skill
if (working_data.data.skills[l_skill] == undefined){
}else {
let skill_rank = working_data.data.skills[l_skill].rank;
//If this is 'enables' and the skill is too low, disable.
if (l_enables && skill_rank < l_skill_rank) {
track.enabled = false;
}
//If this adds boxes and the skill is high enough, add boxes if not already present.
//Telling if the boxes are already present is the hard part.
//If boxes.length > boxes it means we have added boxes, but how many? I think we need to store a count and add
//or subract them at the end of our run through the linked skills.
if (l_boxes > 0 && skill_rank >= l_skill_rank) {
box_mod += l_boxes;
}
}
} //End of linked_skill iteration
//Now to add or subtract the boxes
//Only if this track works with boxes, though
if (track.boxes > 0 || track.box_values != undefined) {
//If boxes + box_mod is greater than box_values.length add boxes
let toModify = track.boxes + box_mod - track.box_values.length;
if (toModify > 0) {
for (let i = 0; i < toModify; i++) {
track.box_values.push(false);
}
}
//If boxes + box_mod is less than box_values.length subtract boxes.
if (toModify < 0) {
for (let i = toModify; i < 0; i++) {
track.box_values.pop();
}
}
}
}
}
return working_data;
}
Hooks.once('ready', async function () {
if (game.settings.get("ModularFate","run_once") == false){
if (game.user.isGM){
ModularFateConstants.awaitOKDialog(game.i18n.localize("ModularFate.WelcomeTitle"),game.i18n.localize("ModularFate.WelcomeText"),500,250);
game.settings.set("ModularFate","run_once", true)
}
}
})
// Needed to update with token name changes in FU.
Hooks.on('updateToken', (token, data, userId) => {
if (data.hidden != undefined || data.actorData != undefined || data.flags != undefined || data.name!=undefined){
game.system.apps["actor"].forEach(a=> {
a.renderMe(token.id, data, token);
})
}
})
Hooks.on('controlToken', (token, control) => {
game.system.apps["actor"].forEach (a=> {
a.renderMe("controlToken",token.id, control);
})
})
Hooks.on('updateUser',(...args) =>{
game.system.apps["user"].forEach (a=> {
a.renderMe("updateUser");
})
})
Hooks.on('renderPlayerList',(...args) =>{
game.system.apps["user"].forEach (a=> {
a.renderMe("updateUser");
})
})
Hooks.on('updateActor', (actor, data) => {
game.system.apps["actor"].forEach(a=> {
a.renderMe(actor.id, data, actor);
})
})
Hooks.on('updateItem', (item, data) => {
game.system.apps["item"].forEach(a=> {
a.renderMe(item.id, data, item);
})
})
Hooks.on('renderCombatTracker', () => {
game.system.apps["combat"].forEach(a=> {
a.renderMe("renderCombatTracker");
})
})
Hooks.on('updateCombat', (...args) => {
let ags = args;
game.system.apps["combat"].forEach(a=> {
a.renderMe(ags);
})
})
Hooks.on('deleteCombat', (...args) => {
game.system.apps["combat"].forEach(a=> {
a.renderMe("deleteCombat");
})
})
Hooks.on('deleteToken', (...args) => {
game.system.apps["actor"].forEach(a=> {
a.renderMe("deleteToken")
})
})
Hooks.on('createToken', (...args) => {
game.system.apps["actor"].forEach(a=> {
a.renderMe("createToken");
})
})
Hooks.on('updateScene', (...args) => {
game.system.apps["combat"].forEach(a=> {
a.renderMe(args);
})
})
Hooks.once('init', async function () {
CONFIG.Actor.documentClass = ModularFateActor;
CONFIG.fontFamilies.push("Montserrat");
game.settings.register("ModularFate","drawingsOnTop", {
name:game.i18n.localize("ModularFate.DrawingsOnTop"),
hint:game.i18n.localize("ModularFate.DrawingsOnTopHint"),
scope:"world",
config:"true",
type:Boolean,
default:false
})
game.settings.register("ModularFate","fu_actor_avatars", {
name:"Use actor avatars instead of token avatars in Fate Utilities?",
hint:"Whether to use actor avatars instead of token avatars in Fate Utilities' aspect viewer",
scope:"world",
config:false,
default:false,
type:Boolean
})
game.settings.register("ModularFate","fu_combatants_only", {
name:"Display information only for combatants in the current 'encounter' rather than all tokens?",
hint:"Toggle between display of all tokens or just active combatants in Fate Utilities",
scope:"user",
config:false,
default:false,
type:Boolean
})
game.settings.register("ModularFate", "run_once", {
name: "Run Once?",
hint:"Pops up a brief tutorial message on first load of a world with this system",
scope:"world",
config:false,
type: Boolean,
default:true
})
game.settings.register("ModularFate","sheet_template", {
name:game.i18n.localize("ModularFate.DefaultSheetTemplateName"),
hint:game.i18n.localize("ModularFate.DefaultSheetTemplateHint"),
scope:"world",
config:"true",
type:String,
default:'systems/ModularFate/templates/ModularFateSheet.html'
})
game.settings.register("ModularFate","limited_sheet_template", {
name:game.i18n.localize("ModularFate.DefaultLimitedSheetTemplateName"),
hint:game.i18n.localize("ModularFate.DefaultLimitedSheetTemplateHint"),
scope:"world",
config:"true",
type:String,
default:'systems/ModularFate/templates/ModularFateSheet.html'
})
game.system.entityTypes.Item = ["Extra"];
game.system.entityTypes.Actor = ["ModularFate","Thing"]
game.system.apps= {
actor:[],
combat:[],
scene:[],
user:[],
item:[]
}
//On init, we initialise any settings and settings menus and HUD overrides as required.
Actors.unregisterSheet('core', ActorSheet);
Actors.registerSheet("ModularFate", ModularFateCharacter, { types: ["ModularFate"], makeDefault: true });
Actors.registerSheet("Thing" , Thing, {types: ["Thing"]});
// Register Item sheets
Items.registerSheet('fate', ExtraSheet, { types: ['Extra'] });
//Register a setting for the game's current Refresh total
game.settings.register("ModularFate", "refreshTotal", {
name: game.i18n.localize("ModularFate.RefreshTotalName"),
hint: game.i18n.localize("ModularFate.RefreshTotalHint"),
scope: "world",
config: true,
type: Number,
default:3,
onChange: () =>{
for (let app in ui.windows){
if (ui.windows[app]?.object?.type == "Thing" || ui.windows[app]?.object?.type == "ModularFate"){
ui.windows[app]?.render(false);
}
}
}
});
game.settings.register("ModularFate","confirmDeletion", {
name: game.i18n.localize("ModularFate.ConfirmDeletionName"),
hint:game.i18n.localize("ModularFate.ConfirmDeletionHint"),
scope:"user",
config:true,
type:Boolean,
restricted:false,
default:false
});
game.settings.register("ModularFate","exportSettings", {
name: game.i18n.localize("ModularFate.ExportSettingsName"),
scope:"world",
config:true,
type:Boolean,
restricted:true,
default:false,
onChange: value => {
if (value == true && game.user.isGM){
let text = ModularFateConstants.exportSettings();
new Dialog({
title: game.i18n.localize("ModularFate.ExportSettingsDialogTitle"),
content: `<div style="background-color:white; color:black;"><textarea rows="20" style="font-family:Montserrat; width:382px; background-color:white; border:1px solid lightsteelblue; color:black;" id="export_settings">${text}</textarea></div>`,
buttons: {
},
}).render(true);
game.settings.set("ModularFate","exportSettings",false);
}
}
})
game.settings.register("ModularFate","importSettings", {
name: game.i18n.localize("ModularFate.ImportSettingsName"),
scope:"world",
hint:game.i18n.localize("ModularFate.ImportSettingsHint"),
config:true,
type:Boolean,
restricted:true,
default:false,
onChange: async value => {
if (value == true && game.user.isGM){
let text = await ModularFateConstants.getSettings();
ModularFateConstants.importSettings(text);
game.settings.set("ModularFate","importSettings",false);
}
}
})
game.settings.register("ModularFate", "gameTime", {
name: game.i18n.localize("ModularFate.GameTime"),
scope:"world",
config:false,
type:String,
restricted:true,
default:""
})
game.settings.register("ModularFate", "gameNotes", {
name: game.i18n.localize("ModularFate.GameNotes"),
scope:"world",
config:false,
type:String,
restricted:true,
default:""
})
game.settings.register("ModularFate", "gameAspects", {
name: game.i18n.localize("ModularFate.GameTime"),
scope:"world",
config:false,
type:Object,
restricted:true,
default:[]
})
game.settings.register("ModularFate", "fuFontSize", {
name: "Fate Utilities Font Size",
scope:"user",
config:false,
type:Number,
restricted:false,
default:10 //Size in points (pt)
})
game.settings.register("ModularFate", "fuAspectLabelSize", {
name: game.i18n.localize("ModularFate.fuAspectLabelSizeName"),
hint:game.i18n.localize("ModularFate.fuAspectLabelSizeHint"),
scope:"world",
config:true,
type:Number,
restricted:true,
default:0
})
game.settings.register("ModularFate","fuAspectLabelFont", {
name: game.i18n.localize("ModularFate.fuAspectLabelFont"),
hint:game.i18n.localize("ModularFate.fuAspectLabelFontHint"),
scope:"world",
config:true,
type:String,
restricted:true,
choices:CONFIG.fontFamilies,
default:"Montserrat",
})
});
Combatant.prototype._getInitiativeFormula = function () {
let init_skill = game.settings.get("ModularFate","init_skill");
if (init_skill === "None" || init_skill === "Disable") {
return "1d0";
}else {
return `1d0+${this.actor.data.data.skills[init_skill].rank}`;
}
}
// Custom drawings layer with different z index
class CustomDrawingsLayer extends DrawingsLayer {
static get layerOptions() {
const options = super.layerOptions;
options.zIndex = 350;
return options;
}
}
Hooks.on("init", () => {
if (game.settings.get ("ModularFate", "drawingsOnTop")){
// Force Canvas to use the new DrawingsLayer
const existingLayers = Canvas.layers;
existingLayers.drawings = CustomDrawingsLayer;
Object.defineProperty(Canvas, "layers", { get: () => existingLayers });
}
});
Hooks.on("getSceneControlButtons", (controls) => {
if (game.settings.get ("ModularFate", "drawingsOnTop")){
controls.find(c => c.name === "drawings").layer = "CustomDrawingsLayer";
}
})