-
Notifications
You must be signed in to change notification settings - Fork 18
/
torchlight.js
1052 lines (994 loc) · 39.9 KB
/
torchlight.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Stephen Hurd
* ----------------------------------------------------------------------------
* <[email protected]> updated this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return, but only if
* you promise to buy one for Stephen as well. Philippe Krait
* ----------------------------------------------------------------------------
* <[email protected]> updated this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return, but
* only if you promise to buy one for Stephen and Philippe as well.
* Alan Davies
* ----------------------------------------------------------------------------
*/
class TorchLight {
static async addTorchLightButtons(app, html, data) {
// Visually and functionally enable a torchlight button
function enableTorchlightButton(tbutton) {
// Remove the disabled status, if any
tbutton.find('i').removeClass('fa-disabled');
// Install a click handler if one is not already bound
if (!tbutton.hasClass('clickBound')) {
tbutton.click(async (ev) => onButtonClick(ev, tbutton));
tbutton.addClass('clickBound');
}
}
// Visually and functionally disable a torchlight button
function disableTorchlightButton(tbutton) {
tbutton.find('i').addClass('fa-disabled');
tbutton.off('click');
tbutton.removeClass('clickBound');
tbutton.removeClass('active');
}
// Enable or disable buttons according to parameters
function enableRelevantButtons() {
// Stores if checks need to be made to enable buttons
let noCheck = game.system.id !== 'dnd5e';
if (!noCheck)
noCheck = (data.isGM && !game.settings.get("torchlight", "dmAsPlayer")) || !game.settings.get("torchlight", "checkAvailability");
if (noCheck || canCastLight())
enableTorchlightButton(tbuttonLight);
else
disableTorchlightButton(tbuttonLight);
if (noCheck || (hasItemInInventory("Oil (flask)") && (hasItemInInventory("Lantern, Hooded") || hasItemInInventory("Lantern, Bullseye"))))
enableTorchlightButton(tbuttonLantern);
else
disableTorchlightButton(tbuttonLantern);
if (noCheck || hasItemInInventory("Torch"))
enableTorchlightButton(tbuttonTorch);
else
disableTorchlightButton(tbuttonTorch);
}
async function onButtonClick(ev, tbutton) {
//console.log("Clicked on a Button.");
ev.preventDefault();
ev.stopPropagation();
// Are we dealing with the Light Button
if (tbutton === tbuttonLight) {
// Check if the token has the light spell on
if (statusLight) {
// The token has the light spell on
console.log("Clicked on the light button when the light is on.");
statusLight = false;
await app.object.document.setFlag("torchlight", "statusLight", false);
tbuttonLight.removeClass("active");
// Light is inactive, enable the relevant light sources according to parameters
enableRelevantButtons();
// Restore the initial light source
updateTokenLighting(
app.object.document.getFlag("torchlight", "InitialBrightRadius"),
app.object.document.getFlag("torchlight", "InitialDimRadius"),
app.object.document.getFlag("torchlight", "InitialLightColor"),
app.object.document.getFlag("torchlight", "InitialColorIntensity"),
app.object.document.getFlag("torchlight", "InitialLightAngle"),
app.object.document.getFlag("torchlight", "InitialAnimationType"),
app.object.document.getFlag("torchlight", "InitialAnimationSpeed"),
app.object.document.getFlag("torchlight", "InitialAnimationIntensity"),
);
} else {
// The token does not have the light spell on
console.log("Clicked on the light button when the light is off.");
statusLight = true;
await app.object.document.setFlag("torchlight", "statusLight", true);
tbuttonLight.addClass("active");
// Light is active, disable the other light sources
disableTorchlightButton(tbuttonLantern);
disableTorchlightButton(tbuttonTorch);
// Store the lighting for later restoration
await storeTokenLighting();
// Enable the Light Source according to the type
// "torch" / "pulse" / "chroma" / "wave" / "fog" / "sunburst" / "dome"
// "emanation" / "hexa" / "ghost" / "energy" / "roiling" / "hole"
let nBright = game.settings.get("torchlight", "lightBrightRadius");
let nDim = game.settings.get("torchlight", "lightDimRadius");
let nType = game.settings.get("torchlight", "lightType");
switch (nType){
case "Type0":
updateTokenLighting(nBright,nDim, "#ffffff", 0.5, 360, "none", 5, 5);
break;
case "Type1":
updateTokenLighting(nBright,nDim, "#ffffff", 0.5, 360, "torch", 5, 5);
break;
case "Type2":
updateTokenLighting(nBright,nDim, "#ffffff", 0.5, 360, "chroma", 5, 5);
break;
case "Type3":
updateTokenLighting(nBright,nDim, "#ffffff", 0.5, 360, "pulse", 5, 5);
break;
case "Type4":
updateTokenLighting(nBright,nDim, "#ffffff", 0.5, 360, "ghost", 5, 5);
break;
case "Type5":
updateTokenLighting(nBright,nDim, "#ffffff", 0.5, 360, "emanation", 5, 5);
break;
case "Type6":
updateTokenLighting(nBright,nDim, "#ff0000", 0.5, 360, "torch", 5, 5);
break;
case "Type7":
updateTokenLighting(nBright,nDim, "#ff0000", 0.5, 360, "chroma", 5, 5);
break;
case "Type8":
updateTokenLighting(nBright,nDim, "#ff0000", 0.5, 360, "pulse", 5, 5);
break;
case "Type9":
updateTokenLighting(nBright,nDim, "#ff0000", 0.5, 360, "ghost", 5, 5);
break;
case "Type10":
updateTokenLighting(nBright,nDim, "#ff0000", 0.5, 360, "emanation", 5, 5);
break;
case "Type11":
updateTokenLighting(nBright,nDim, "#00ff00", 0.5, 360, "torch", 5, 5);
break;
case "Type12":
updateTokenLighting(nBright,nDim, "#00ff00", 0.5, 360, "chroma", 5, 5);
break;
case "Type13":
updateTokenLighting(nBright,nDim, "#00ff00", 0.5, 360, "pulse", 5, 5);
break;
case "Type14":
updateTokenLighting(nBright,nDim, "#00ff00", 0.5, 360, "ghost", 5, 5);
break;
case "Type15":
updateTokenLighting(nBright,nDim, "#00ff00", 0.5, 360, "emanation", 5, 5);
break;
case "TypeC":
updateTokenLighting(nBright,nDim,
game.settings.get("torchlight", "customLightColor"),
game.settings.get("torchlight", "customLightColorIntensity"),
360,
game.settings.get("torchlight", "customLightAnimationType"),
game.settings.get("torchlight", "customLightAnimationSpeed"),
game.settings.get("torchlight", "customLightAnimationIntensity"));
break;
}
}
// Or are we dealing with the Lantern Button
} else if (tbutton === tbuttonLantern) {
// Check if the token has the lantern on
if (statusLantern) {
// The token has the light spell on
console.log("Clicked on the lantern button when the lantern is on.");
statusLantern = false;
await app.object.document.setFlag("torchlight", "statusLantern", false);
tbuttonLantern.removeClass("active");
// Lantern is inactive, enable the relevant light sources according to parameters
enableRelevantButtons();
// Restore the initial light source
updateTokenLighting(
app.object.document.getFlag("torchlight", "InitialBrightRadius"),
app.object.document.getFlag("torchlight", "InitialDimRadius"),
app.object.document.getFlag("torchlight", "InitialLightColor"),
app.object.document.getFlag("torchlight", "InitialColorIntensity"),
app.object.document.getFlag("torchlight", "InitialLightAngle"),
app.object.document.getFlag("torchlight", "InitialAnimationType"),
app.object.document.getFlag("torchlight", "InitialAnimationSpeed"),
app.object.document.getFlag("torchlight", "InitialAnimationIntensity"),
);
} else {
// The token does not have the lantern on
console.log("Clicked on the lantern when the lantern is off.");
// Checks whether the character can consume an oil flask
if (consumeItem("Oil (flask)")) {
statusLantern = true;
await app.object.document.setFlag("torchlight", "statusLantern", true);
tbuttonLantern.addClass("active");
// Lantern is active, disable the other light sources
disableTorchlightButton(tbuttonLight);
disableTorchlightButton(tbuttonTorch);
// Store the lighting for later restoration
await storeTokenLighting();
// Enable the Lantern Source according to the type
let nBright = game.settings.get("torchlight", "lanternBrightRadius");
let nDim = game.settings.get("torchlight", "lanternDimRadius");
let nType = game.settings.get("torchlight", "lanternType");
switch (nType){
case "Type0":
updateTokenLighting(nBright,nDim, "#a2642a", 0.7, 360, "none", 10, 7);
break;
case "Type1":
updateTokenLighting(nBright,nDim, "#a2642a", 0.7, 360, "torch", 10, 7);
break;
case "Type2":
updateTokenLighting(nBright,nDim, "#a2642a", 0.5, 360, "torch", 10, 5);
break;
case "Type3":
updateTokenLighting(nBright,nDim, "#a2642a", 0.3, 360, "torch", 10, 3);
break;
case "Type4":
updateTokenLighting(5,5, "#a2642a", 0.7, 360, "torch", 10, 7);
break;
case "Type5":
updateTokenLighting(5,5, "#a2642a", 0.5, 360, "torch", 10, 5);
break;
case "Type6":
updateTokenLighting(5,5, "#a2642a", 0.3, 360, "torch", 10, 3);
break;
case "Type7":
updateTokenLighting(nBright*2,nDim*2, "#a2642a", 0.7, 60, "torch", 10, 7);
break;
case "Type8":
updateTokenLighting(nBright*2,nDim*2, "#a2642a", 0.5, 60, "torch", 10, 5);
break;
case "Type9":
updateTokenLighting(nBright*2,nDim*2, "#a2642a", 0.3, 60, "torch", 10, 3);
break;
case "TypeC":
updateTokenLighting(nBright,nDim,
game.settings.get("torchlight", "customLanternColor"),
game.settings.get("torchlight", "customLanternColorIntensity"),
360,
game.settings.get("torchlight", "customLanternAnimationType"),
game.settings.get("torchlight", "customLanternAnimationSpeed"),
game.settings.get("torchlight", "customLanternAnimationIntensity"));
break;
}
} else {
// There is no oil to consume, signal and disable the button
ChatMessage.create({
user: game.user._id,
speaker: game.actors.get(data.actorId),
content: "No Oil (flask) in Inventory !"
});
disableTorchlightButton(tbuttonLantern);
}
}
// Or are we dealing with the Torch Button
} else if (tbutton === tbuttonTorch) {
// Check if the token has the torch on
if (statusTorch) {
// The token has the torch on
console.log("Clicked on the torch button when the torch is on.");
statusTorch = false;
await app.object.document.setFlag("torchlight", "statusTorch", false);
tbuttonTorch.removeClass("active");
// Torch is inactive, enable the relevant light sources according to parameters
enableRelevantButtons();
// Restore the initial light source
updateTokenLighting(
app.object.document.getFlag("torchlight", "InitialBrightRadius"),
app.object.document.getFlag("torchlight", "InitialDimRadius"),
app.object.document.getFlag("torchlight", "InitialLightColor"),
app.object.document.getFlag("torchlight", "InitialColorIntensity"),
app.object.document.getFlag("torchlight", "InitialLightAngle"),
app.object.document.getFlag("torchlight", "InitialAnimationType"),
app.object.document.getFlag("torchlight", "InitialAnimationSpeed"),
app.object.document.getFlag("torchlight", "InitialAnimationIntensity"),
);
} else {
// The token does not have the torch on
console.log("Clicked on the torch when the torch is off.");
// Checks whether the character can consume a torch
if (consumeItem("Torch")) {
statusTorch = true;
await app.object.document.setFlag("torchlight", "statusTorch", true);
tbuttonTorch.addClass("active");
// Torch is active, disable the other light sources
disableTorchlightButton(tbuttonLight);
disableTorchlightButton(tbuttonLantern);
// Store the lighting for later restoration
await storeTokenLighting();
// Enable the Torch Source according to the type
let nBright = game.settings.get("torchlight", "torchBrightRadius");
let nDim = game.settings.get("torchlight", "torchDimRadius");
let nType = game.settings.get("torchlight", "torchType");
switch (nType){
case "Type0":
updateTokenLighting(nBright,nDim, "#a2642a", 0.7, 360, "none", 5, 7);
break;
case "Type1":
updateTokenLighting(nBright,nDim, "#a2642a", 0.7, 360, "torch", 5, 7);
break;
case "Type2":
updateTokenLighting(nBright,nDim, "#a2642a", 0.5, 360, "torch", 5, 5);
break;
case "Type3":
updateTokenLighting(nBright,nDim, "#a2642a", 0.3, 360, "torch", 5, 3);
break;
case "Type4":
updateTokenLighting(nBright,nDim, "#a22a2a", 0.7, 360, "torch", 5, 7);
break;
case "Type5":
updateTokenLighting(nBright,nDim, "#a22a2a", 0.5, 360, "torch", 5, 5);
break;
case "Type6":
updateTokenLighting(nBright,nDim, "#a22a2a", 0.3, 360, "torch", 5, 3);
break;
case "Type7":
updateTokenLighting(nBright,nDim, "#822aa2", 0.7, 360, "torch", 5, 7);
break;
case "Type8":
updateTokenLighting(nBright,nDim, "#822aa2", 0.5, 360, "torch", 5, 5);
break;
case "Type9":
updateTokenLighting(nBright,nDim, "#822aa2", 0.3, 360, "torch", 5, 3);
break;
case "TypeC":
updateTokenLighting(nBright,nDim,
game.settings.get("torchlight", "customTorchColor"),
game.settings.get("torchlight", "customTorchColorIntensity"),
360,
game.settings.get("torchlight", "customTorchAnimationType"),
game.settings.get("torchlight", "customTorchAnimationSpeed"),
game.settings.get("torchlight", "customTorchAnimationIntensity"));
break;
}
} else {
// There is no torch to consume, signal and disable the button
ChatMessage.create({
user: game.user._id,
speaker: game.actors.get(data.actorId),
content: "No Torch in Inventory !"
});
disableTorchlightButton(tbuttonTorch);
}
}
}
}
// Update the relevant light parameters of a token
function updateTokenLighting(brightLight, dimLight, lightColor, colorIntensity, lightAngle, animationType, animationSpeed, animationIntensity) {
app.object.document.update({
brightLight: brightLight,
dimLight: dimLight,
lightColor: lightColor,
lightAlpha: colorIntensity ** 2,
lightAngle: lightAngle,
lightAnimation: {
type: animationType,
speed: animationSpeed,
intensity: animationIntensity
}
});
}
// Store the initial status of illumination for the token to restore if all light sources are extinguished
async function storeTokenLighting() {
let promises = [];
const tokenData = app.object.data;
promises.push(app.object.document.setFlag("torchlight", "InitialBrightRadius", tokenData.brightLight));
promises.push(app.object.document.setFlag("torchlight", "InitialDimRadius", tokenData.dimLight));
promises.push(app.object.document.setFlag("torchlight", "InitialLightColor",
tokenData.lightColor ? tokenData.lightColor.toString(16).padStart(6, 0) : null));
promises.push(app.object.document.setFlag("torchlight", "InitialColorIntensity", Math.sqrt(tokenData.lightAlpha)));
promises.push(app.object.document.setFlag("torchlight", "InitialLightAngle", tokenData.lightAngle));
promises.push(app.object.document.setFlag("torchlight", "InitialAnimationType", tokenData.lightAnimation.type ?? null));
promises.push(app.object.document.setFlag("torchlight", "InitialAnimationSpeed", tokenData.lightAnimation.speed));
promises.push(app.object.document.setFlag("torchlight", "InitialAnimationIntensity", tokenData.lightAnimation.intensity));
/*
Promise.all(promises).then(_ => {
console.log("Stored brightRadius:" + app.object.document.getFlag("torchlight", "InitialBrightRadius"));
console.log("Stored dimRadius:" + app.object.document.getFlag("torchlight", "InitialDimRadius"));
console.log("Stored lightColor:" + app.object.document.getFlag("torchlight", "InitialLightColor"));
console.log("Stored lightAlpha:" + app.object.document.getFlag("torchlight", "InitialColorIntensity"));
console.log("Stored lightAngle:" + app.object.document.getFlag("torchlight", "InitialLightAngle"));
console.log("Stored animation.type:" + app.object.document.getFlag("torchlight", "InitialAnimationType"));
console.log("Stored animation.speed:" + app.object.document.getFlag("torchlight", "InitialAnimationSpeed"));
console.log("Stored animation.intensity:" + app.object.document.getFlag("torchlight", "InitialAnimationIntensity"));
});*/
return Promise.all(promises);
}
// Define all three buttons
//let tbuttonLight = $(`<div class="control-icon torchlight"><i class="fas fa-sun"></i></div>`);
//let tbuttonLantern = $(`<div class="control-icon torchlight"><i class="fas fa-lightbulb"></i></div>`);
//let tbuttonTorch = $(`<div class="control-icon torchlight"><i class="fas fa-fire"></i></div>`);
let tbuttonLight = $(`<div class="control-icon torchlight" title="Toggle Light Spell"><i class="fas fa-sun"></i></div>`);
let tbuttonLantern = $(`<div class="control-icon torchlight" title="Toggle Lantern"><i class="fas fa-lightbulb"></i></div>`);
let tbuttonTorch = $(`<div class="control-icon torchlight" title="Toggle Torch"><i class="fas fa-fire"></i></div>`);
// Get the position of the column
let position = game.settings.get('torchlight', 'position');
// Create the column
let buttonsdiv = $(`<div class="col torchlight-column-${position}"></div>`);
/*
// Wrap the previous icons
let newdiv = '<div class="torchlight-container"></div>';
html.find('.col.left').wrap(newdiv);
// Add the column
html.find('.col.left').before(buttonsdiv);
*/
// Wrap the previous icons
let newdiv = '<div class="torchlight-container"></div>';
html.find('.col.left').before(newdiv);
// Add the column
html.find('.torchlight-container').prepend(buttonsdiv);
console.log("Initialisation");
// Get the status of the three types of lights
let statusLight = app.object.document.getFlag("torchlight", "statusLight");
//console.log("Initial statusLight:" + statusLight);
if (statusLight == undefined || statusLight == null) {
statusLight = false;
await app.object.document.setFlag("torchlight", "statusLight", false);
}
let statusLantern = app.object.document.getFlag("torchlight", "statusLantern");
//console.log("Initial statusLantern:" + statusLantern);
if (statusLantern == undefined || statusLantern == null) {
statusLantern = false;
await app.object.document.setFlag("torchlight", "statusLantern", false);
}
let statusTorch = app.object.document.getFlag("torchlight", "statusTorch");
//console.log("Initial statusTorch:" + statusTorch);
if (statusTorch == undefined || statusTorch == null) {
statusTorch = false;
await app.object.document.setFlag("torchlight", "statusTorch", false);
}
//console.log("Initialised statusLight:" + statusLight);
//console.log("Initialised statusLantern:" + statusLantern);
//console.log("Initialised statusTorch:" + statusTorch);
// Initial button state when the HUD comes up
if (statusLight) tbuttonLight.addClass("active");
if (statusLantern) tbuttonLantern.addClass("active");
// Check the permissions to manage the lights
if (data.isGM === true || game.settings.get("torchlight", "playerActivation") === true) {
// If the a specific light is on, enable only that light otherwise enable all three of them
if (statusLight) {
enableTorchlightButton(tbuttonLight);
disableTorchlightButton(tbuttonLantern);
disableTorchlightButton(tbuttonTorch);
tbuttonLight.addClass("active");
} else if (statusLantern) {
disableTorchlightButton(tbuttonLight);
enableTorchlightButton(tbuttonLantern);
disableTorchlightButton(tbuttonTorch);
tbuttonLantern.addClass("active");
} else if (statusTorch) {
disableTorchlightButton(tbuttonLight);
disableTorchlightButton(tbuttonLantern);
enableTorchlightButton(tbuttonTorch);
tbuttonTorch.addClass("active");
} else
enableRelevantButtons();
} else {
// If no permission exists, disable all the buttons
//tbuttonLight.find('i').addClass('fa-disabled');
//tbuttonLantern.find('i').addClass('fa-disabled');
//tbuttonTorch.find('i').addClass('fa-disabled');
disableTorchlightButton(tbuttonLight);
disableTorchlightButton(tbuttonLantern);
disableTorchlightButton(tbuttonTorch);
}
// Returns true if the character can use the Light spell
// This also returns true if the game system is not D&D 5e...
function canCastLight() {
let actor = game.actors.get(data.actorId);
if (actor === undefined)
return false;
let hasLight = false;
actor.data.items.forEach(item => {
if (item.type === 'spell') {
if (item.name === 'Light')
hasLight = true;
}
});
return hasLight;
}
// Returns true if the character has a specific item in his inventory
// This also returns true if the game system is not D&D 5e...
function hasItemInInventory(itemToCheck) {
let actor = game.actors.get(data.actorId);
if (actor === undefined)
return false;
let hasItem = false;
actor.data.items.forEach(item => {
if (item.name.toLowerCase() === itemToCheck.toLowerCase()) {
if (item.data.quantity > 0)
hasItem = true;
}
});
return hasItem;
}
// Returns true if either the character does not need to consume an item
// or if he can indeed consume it (and it is actually consumed)
function consumeItem(itemToCheck) {
let consume = game.system.id !== 'dnd5e';
if (!consume)
consume = (data.isGM && !game.settings.get("torchlight", "dmAsPlayer")) ||
!game.settings.get("torchlight", "checkAvailability") ||
!game.settings.get("torchlight", "consumeItem");
if (!consume) {
let actor = game.actors.get(data.actorId);
if (actor === undefined)
return false;
let hasItem = false;
actor.data.items.forEach((item, offset) => {
if (item.name.toLowerCase() === itemToCheck.toLowerCase()) {
if (item.data.quantity > 0) {
hasItem = true;
actor.updateOwnedItem({"_id": actor.data.items[offset]._id, "data.quantity": actor.data.items[offset].data.quantity - 1});
}
}
});
consume = hasItem
}
return consume;
}
/*
* Returns the first GM id.
*/
function firstGM() {
let i;
for (i=0; i<game.users.entities.length; i++) {
if (game.users.entities[i].data.role >= 4 && game.users.entities[i].active)
return game.users.entities[i].data._id;
}
ui.notifications.error("No GM available for Dancing Lights!");
}
async function sendRequest(req) {
req.sceneId = canvas.scene._id
req.tokenId = app.object.id;
if (!data.isGM) {
req.addressTo = firstGM();
game.socket.emit("module.torch", req);
}
else {
TorchLight.handleSocketRequest(req);
}
}
// Finally insert the buttons in the column
html.find('.col.torchlight-column-'+position).prepend(tbuttonTorch);
html.find('.col.torchlight-column-'+position).prepend(tbuttonLantern);
html.find('.col.torchlight-column-'+position).prepend(tbuttonLight);
}
static async handleSocketRequest(req) {
if (req.addressTo === undefined || req.addressTo === game.user._id) {
let scn = game.scenes.get(req.sceneId);
let tkn = scn.data.tokens.find(({_id}) => _id === req.tokenId);
let dltoks=[];
switch(req.requestType) {
case 'removeDancingLights':
scn.data.tokens.forEach(tok => {
if (tok.actorId === tkn.actorId &&
tok.name === 'Dancing Light' &&
tok.dimLight === 20 &&
tok.brightLight === 10) {
//let dltok = canvas.tokens.get(tok._id);
dltoks.push(scn.getEmbeddedEntity("Token", tok._id)._id);
}
});
await scn.deleteEmbeddedEntity("Token", dltoks);
break;
}
}
}
}
Hooks.on('ready', () => {
Hooks.on('renderTokenHUD', (app, html, data) => { TorchLight.addTorchLightButtons(app, html, data) });
Hooks.on('renderControlsReference', (app, html, data) => {
html.find('div').first().append('<h3>TorchLight</h3><ol class="hotkey-list"><li><h4>'+
game.i18n.localize("torchlight.turnOffAllLights")+
'</h4><div class="keys">'+
game.i18n.localize("torchlight.holdCtrlOnClick")+
'</div></li></ol>');
});
game.socket.on("module.torch", request => {
TorchLight.handleSocketRequest(request);
});
});
Hooks.once("init", () => {
game.settings.register('torchlight', 'position', {
name: game.i18n.localize("torchlight.position.name"),
hint: game.i18n.localize("torchlight.position.hint"),
scope: "world",
config: true,
type: String,
default: "left",
choices: {
"left": game.i18n.localize("torchlight.position.left"),
"right": game.i18n.localize("torchlight.position.right"),
"top": game.i18n.localize("torchlight.position.top"),
"bottom": game.i18n.localize("torchlight.position.bottom"),
}
});
game.settings.register("torchlight", "playerActivation", {
name: game.i18n.localize("torchlight.playerActivation.name"),
hint: game.i18n.localize("torchlight.playerActivation.hint"),
scope: "world",
config: true,
default: true,
type: Boolean
});
if (game.system.id === 'dnd5e') {
game.settings.register("torchlight", "checkAvailability", {
name: game.i18n.localize("torchlight.checkAvailability.name"),
hint: game.i18n.localize("torchlight.checkAvailability.hint"),
scope: "world",
config: true,
default: true,
type: Boolean
});
game.settings.register("torchlight", "consumeItem", {
name: game.i18n.localize("torchlight.consumeItem.name"),
hint: game.i18n.localize("torchlight.consumeItem.hint"),
scope: "world",
config: true,
default: true,
type: Boolean
});
game.settings.register("torchlight", "dmAsPlayer", {
name: game.i18n.localize("torchlight.dmAsPlayer.name"),
hint: game.i18n.localize("torchlight.dmAsPlayer.hint"),
scope: "world",
config: true,
default: false,
type: Boolean
});
}
// Light Parameters
game.settings.register("torchlight", "lightBrightRadius", {
name: game.i18n.localize("torchlight.lightBrightRadius.name"),
hint: game.i18n.localize("torchlight.lightBrightRadius.hint"),
scope: "world",
config: true,
default: 20,
type: Number
});
game.settings.register("torchlight", "lightDimRadius", {
name: game.i18n.localize("torchlight.lightDimRadius.name"),
hint: game.i18n.localize("torchlight.lightDimRadius.hint"),
scope: "world",
config: true,
default: 40,
type: Number
});
game.settings.register('torchlight', 'lightType', {
name: game.i18n.localize("torchlight.lightType.name"),
hint: game.i18n.localize("torchlight.lightType.hint"),
scope: "world",
config: true,
type: String,
default: "Type1",
choices: {
"Type0": game.i18n.localize("torchlight.lightType.type0"),
"Type1": game.i18n.localize("torchlight.lightType.type1"),
"Type2": game.i18n.localize("torchlight.lightType.type2"),
"Type3": game.i18n.localize("torchlight.lightType.type3"),
"Type4": game.i18n.localize("torchlight.lightType.type4"),
"Type5": game.i18n.localize("torchlight.lightType.type5"),
"Type6": game.i18n.localize("torchlight.lightType.type6"),
"Type7": game.i18n.localize("torchlight.lightType.type7"),
"Type8": game.i18n.localize("torchlight.lightType.type8"),
"Type9": game.i18n.localize("torchlight.lightType.type9"),
"Type10": game.i18n.localize("torchlight.lightType.type10"),
"Type11": game.i18n.localize("torchlight.lightType.type11"),
"Type12": game.i18n.localize("torchlight.lightType.type12"),
"Type13": game.i18n.localize("torchlight.lightType.type13"),
"Type14": game.i18n.localize("torchlight.lightType.type14"),
"Type15": game.i18n.localize("torchlight.lightType.type15"),
"TypeC": game.i18n.localize("torchlight.lightType.typeC"),
}
});
game.settings.register("torchlight", "customLightColor", {
name: game.i18n.localize("torchlight.lightType.customColor.name"),
hint: game.i18n.localize("torchlight.lightType.customColor.hint"),
scope: "world",
config: true,
restricted: false,
type: String,
default: "#a2642a"
});
game.settings.register("torchlight", "customLightColorIntensity", {
name: game.i18n.localize("torchlight.lightType.customIntensity.name"),
hint: game.i18n.localize("torchlight.lightType.customIntensity.hint"),
scope: "world",
config: true,
restricted: true,
type: Number,
default: 0.5,
range: {
min: 0.0,
step: 0.05,
max: 1,
}
});
game.settings.register('torchlight', 'customLightAnimationType', {
name: game.i18n.localize("torchlight.lightType.customAnimationType.name"),
hint: game.i18n.localize("torchlight.lightType.customAnimationType.hint"),
scope: "world",
config: true,
type: String,
default: "none",
choices: {
"none": game.i18n.localize("torchlight.animationType.none"),
"torch": game.i18n.localize("torchlight.animationType.torch"),
"pulse": game.i18n.localize("torchlight.animationType.pulse"),
"chroma": game.i18n.localize("torchlight.animationType.chroma"),
"wave": game.i18n.localize("torchlight.animationType.wave"),
"fog": game.i18n.localize("torchlight.animationType.fog"),
"sunburst": game.i18n.localize("torchlight.animationType.sunburst"),
"dome": game.i18n.localize("torchlight.animationType.dome"),
"emanation": game.i18n.localize("torchlight.animationType.emanation"),
"hexa": game.i18n.localize("torchlight.animationType.hexa"),
"ghost": game.i18n.localize("torchlight.animationType.ghost"),
"energy": game.i18n.localize("torchlight.animationType.energy"),
"roiling": game.i18n.localize("torchlight.animationType.roiling"),
"hole": game.i18n.localize("torchlight.animationType.hole"),
}
});
game.settings.register("torchlight", "customLightAnimationSpeed", {
name: game.i18n.localize("torchlight.lightType.customAnimationSpeed.name"),
hint: game.i18n.localize("torchlight.lightType.customAnimationSpeed.hint"),
scope: "world",
config: true,
restricted: true,
type: Number,
default: 5,
range: {
min: 1,
step: 1,
max: 10,
}
});
game.settings.register("torchlight", "customLightAnimationIntensity", {
name: game.i18n.localize("torchlight.lightType.customAnimationIntensity.name"),
hint: game.i18n.localize("torchlight.lightType.customAnimationIntensity.hint"),
scope: "world",
config: true,
restricted: true,
type: Number,
default: 5,
range: {
min: 1,
step: 1,
max: 10,
}
});
// Lantern Parameters
game.settings.register("torchlight", "lanternBrightRadius", {
name: game.i18n.localize("torchlight.lanternBrightRadius.name"),
hint: game.i18n.localize("torchlight.lanternBrightRadius.hint"),
scope: "world",
config: true,
default: 20,
type: Number
});
game.settings.register("torchlight", "lanternDimRadius", {
name: game.i18n.localize("torchlight.lanternDimRadius.name"),
hint: game.i18n.localize("torchlight.lanternDimRadius.hint"),
scope: "world",
config: true,
default: 40,
type: Number
});
game.settings.register('torchlight', 'lanternType', {
name: game.i18n.localize("torchlight.lanternType.name"),
hint: game.i18n.localize("torchlight.lanternType.hint"),
scope: "world",
config: true,
type: String,
default: "Type1",
choices: {
"Type0": game.i18n.localize("torchlight.lanternType.type0"),
"Type1": game.i18n.localize("torchlight.lanternType.type1"),
"Type2": game.i18n.localize("torchlight.lanternType.type2"),
"Type3": game.i18n.localize("torchlight.lanternType.type3"),
"Type4": game.i18n.localize("torchlight.lanternType.type4"),
"Type5": game.i18n.localize("torchlight.lanternType.type5"),
"Type6": game.i18n.localize("torchlight.lanternType.type6"),
"Type7": game.i18n.localize("torchlight.lanternType.type7"),
"Type8": game.i18n.localize("torchlight.lanternType.type8"),
"Type9": game.i18n.localize("torchlight.lanternType.type9"),
"TypeC": game.i18n.localize("torchlight.lanternType.typeC"),
}
});
game.settings.register("torchlight", "customLanternColor", {
name: game.i18n.localize("torchlight.lanternType.customColor.name"),
hint: game.i18n.localize("torchlight.lanternType.customColor.hint"),
scope: "world",
config: true,
restricted: false,
type: String,
default: "#a2642a"
});
game.settings.register("torchlight", "customLanternColorIntensity", {
name: game.i18n.localize("torchlight.lanternType.customIntensity.name"),
hint: game.i18n.localize("torchlight.lanternType.customIntensity.hint"),
scope: "world",
config: true,
restricted: true,
type: Number,
default: 0.5,
range: {
min: 0.0,
step: 0.05,
max: 1,
}
});
game.settings.register('torchlight', 'customLanternAnimationType', {
name: game.i18n.localize("torchlight.lanternType.customAnimationType.name"),
hint: game.i18n.localize("torchlight.lanternType.customAnimationType.hint"),
scope: "world",
config: true,
type: String,
default: "none",
choices: {
"none": game.i18n.localize("torchlight.animationType.none"),
"torch": game.i18n.localize("torchlight.animationType.torch"),
"pulse": game.i18n.localize("torchlight.animationType.pulse"),
"chroma": game.i18n.localize("torchlight.animationType.chroma"),
"wave": game.i18n.localize("torchlight.animationType.wave"),
"fog": game.i18n.localize("torchlight.animationType.fog"),
"sunburst": game.i18n.localize("torchlight.animationType.sunburst"),
"dome": game.i18n.localize("torchlight.animationType.dome"),
"emanation": game.i18n.localize("torchlight.animationType.emanation"),
"hexa": game.i18n.localize("torchlight.animationType.hexa"),
"ghost": game.i18n.localize("torchlight.animationType.ghost"),
"energy": game.i18n.localize("torchlight.animationType.energy"),
"roiling": game.i18n.localize("torchlight.animationType.roiling"),
"hole": game.i18n.localize("torchlight.animationType.hole"),
}
});
game.settings.register("torchlight", "customLanternAnimationSpeed", {
name: game.i18n.localize("torchlight.lanternType.customAnimationSpeed.name"),
hint: game.i18n.localize("torchlight.lanternType.customAnimationSpeed.hint"),
scope: "world",
config: true,
restricted: true,
type: Number,
default: 5,
range: {
min: 1,
step: 1,
max: 10,
}
});
game.settings.register("torchlight", "customLanternAnimationIntensity", {
name: game.i18n.localize("torchlight.lanternType.customAnimationIntensity.name"),
hint: game.i18n.localize("torchlight.lanternType.customAnimationIntensity.hint"),
scope: "world",
config: true,
restricted: true,
type: Number,
default: 5,
range: {
min: 1,
step: 1,
max: 10,
}
});
if (game.system.id === 'dnd5e') {
game.settings.register("torchlight", "nameConsumableLantern", {
name: game.i18n.localize("torchlight.nameConsumableLantern.name"),
hint: game.i18n.localize("torchlight.nameConsumableLantern.hint"),
scope: "world",
config: true,
default: "Oil (flask)",
type: String
});
}
// Torch Parameters
game.settings.register("torchlight", "torchBrightRadius", {
name: game.i18n.localize("torchlight.torchBrightRadius.name"),
hint: game.i18n.localize("torchlight.torchBrightRadius.hint"),
scope: "world",
config: true,
default: 20,
type: Number
});
game.settings.register("torchlight", "torchDimRadius", {
name: game.i18n.localize("torchlight.torchDimRadius.name"),
hint: game.i18n.localize("torchlight.torchDimRadius.hint"),
scope: "world",
config: true,
default: 40,
type: Number
});
game.settings.register('torchlight', 'torchType', {
name: game.i18n.localize("torchlight.torchType.name"),
hint: game.i18n.localize("torchlight.torchType.hint"),
scope: "world",
config: true,
type: String,
default: "Type1",
choices: {
"Type0": game.i18n.localize("torchlight.torchType.type0"),
"Type1": game.i18n.localize("torchlight.torchType.type1"),
"Type2": game.i18n.localize("torchlight.torchType.type2"),
"Type3": game.i18n.localize("torchlight.torchType.type3"),
"Type4": game.i18n.localize("torchlight.torchType.type4"),
"Type5": game.i18n.localize("torchlight.torchType.type5"),
"Type6": game.i18n.localize("torchlight.torchType.type6"),
"Type7": game.i18n.localize("torchlight.torchType.type7"),
"Type8": game.i18n.localize("torchlight.torchType.type8"),
"Type9": game.i18n.localize("torchlight.torchType.type9"),
"TypeC": game.i18n.localize("torchlight.torchType.typeC"),
}
});
game.settings.register("torchlight", "customTorchColor", {
name: game.i18n.localize("torchlight.torchType.customColor.name"),
hint: game.i18n.localize("torchlight.torchType.customColor.hint"),
scope: "world",
config: true,
restricted: false,
type: String,
default: "#a2642a"
});
game.settings.register("torchlight", "customTorchColorIntensity", {
name: game.i18n.localize("torchlight.torchType.customIntensity.name"),
hint: game.i18n.localize("torchlight.torchType.customIntensity.hint"),
scope: "world",
config: true,
restricted: true,
type: Number,
default: 0.5,
range: {
min: 0.0,
step: 0.05,
max: 1,
}
});
game.settings.register('torchlight', 'customTorchAnimationType', {
name: game.i18n.localize("torchlight.torchType.customAnimationType.name"),
hint: game.i18n.localize("torchlight.torchType.customAnimationType.hint"),
scope: "world",
config: true,
type: String,
default: "none",
choices: {
"none": game.i18n.localize("torchlight.animationType.none"),
"torch": game.i18n.localize("torchlight.animationType.torch"),
"pulse": game.i18n.localize("torchlight.animationType.pulse"),
"chroma": game.i18n.localize("torchlight.animationType.chroma"),
"wave": game.i18n.localize("torchlight.animationType.wave"),
"fog": game.i18n.localize("torchlight.animationType.fog"),
"sunburst": game.i18n.localize("torchlight.animationType.sunburst"),