forked from RPGKenL/Roll20_API_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgen.js
More file actions
3444 lines (3188 loc) · 104 KB
/
cgen.js
File metadata and controls
3444 lines (3188 loc) · 104 KB
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
/**
*
* Copyright (C) 2015 Ken L.
* Licensed under the GPL Version 3 license.
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Andy W.
* Shu Zong C.
* Carlos R. L. Rodrigues
*
*
* This script is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* This script is designed to parse stat blocks from the pathfinder PRD which are
* formatted in a very particular way. Know that there will be edge cases which
* are not handled due to the large varity of conditions.
*
* It is charactersheet agnostic as it simply creates ability fields and marcos for
* the provided statblock. It is also light-weight simply (if enabled) linking or
* reminding a GM of information or abilities a creature has.
*
* If copying stat blocks from PDFs or other Paizo or Paizo compatible sources,
* ensure that they fit the PRD specified layout (1 line per ability etc), when
* in doubt, copy it into a text editor and confirm what you copied fits the PRD
* format.
*
* If all else fails, it's probably an edge case I didn't consider; or the stat
* block is malformed. Feel free to patch up the holes and commit to the git-hub to
* make the script better for everyone.
*
* https://github.com/Roll20KenL/Roll20_API_Scripts/blob/master/cgen.js
*
*/
var CreatureGenPF = (function() {
'use strict';
var version = 1.32,
author = "Ken L.",
contributers = "Andy W., Shu Zong C., Carlos R. L. Rodrigues",
debugLvl = 1,
locked = false,
unitPixels = 70,
workStart = 0,
workDelay = 250,
workList = [],
dmesg,
warn,
creName,
character;
var termEnum = Object.freeze({
GENERAL : 1,
MONABILITY: 2,
SPELL: 3,
FEAT: 4,
SQ: 5,
SA: 6,
SKILL: 7
});
var bonusEnum = Object.freeze({
SCALAR: 1,
SIGN: 2
});
var urlCondEnum = Object.freeze({
FULL: "FULL",
LABEL: "LABEL"
});
var atkEnum = Object.freeze({
TITLE: 'TITLE',
ATTACK: 'ATTACK',
DAMAGE: 'DAMAGE'
});
/**
* Various fields that can be changed for customization
*
*/
var fields = {
defaultName: "Creature",
charDataPrefix: "CGen_",
publicName: "@{CGen_name}",
publicEm: "/emas ",
publicAtkEm: "/as Attack ",
publicDmgEm: "/as Damage ",
publicAnn: "/desc ",
privWhis: "/w GM ",
menuWhis: "/w GM ",
resultWhis: "/w GM ",
urlTermGeneral: '<a href="http://www.google.com/cse?cx=006680642033474972217%3A6zo0hx_wle8&q=<<FULL>>"><span style="color: #FF0000; font-weight: bold;"><<LABEL>></span></a>',
//urlTermGeneral: '<a href="http://www.d20pfsrd.com/system/app/pages/search?scope=search-site&q=<<FULL>>"><span style="color: #FF0000; font-weight: bold;"><<LABEL>></span></a>',
urlTermMonAbility: '<a href="http://paizo.com/pathfinderRPG/prd/additionalMonsters/universalMonsterRules.html#<<FULL>>"><span style="color: #2F2F2F; font-weight: bold;"><<FULL>></span></a>',
urlTermSpell: '<a href="http://www.d20pfsrd.com/magic/all-spells/<<0>>/<<FULL>>"><span style="color: #2E39FF; font-weight: bold;"><<LABEL>></span></a>',
urlTermFeat: '<a href="http://www.google.com/cse?cx=006680642033474972217%3A6zo0hx_wle8&q=<<FULL>>"><span style="color: #29220A; font-weight: bold;"><<FULL>></span></a>',
urlTermSQ: "", // unused
urlTermSA: "", // unused
summoner: undefined,
shortAtkRiders: false // unused
};
/**
* div border styles, customize it for your game!
*
* <span [[format text injected here]]> penguins </span>
* <img src="[[image link]]">"
*/
var design = {
feedbackName: 'Ken L.',
feedbackImg: 'https://s3.amazonaws.com/files.d20.io/images/3466065/uiXt3Zh5EoHDkXmhGUumYQ/thumb.jpg?1395313520',
errorImg: 'https://s3.amazonaws.com/files.d20.io/images/7545187/fjEEs0Jvjz1uy3mGN5A_3Q/thumb.png?1423165317',
warningImg: 'https://s3.amazonaws.com/files.d20.io/images/7926480/NaBmVmKe94rdzXwVnLq0-w/thumb.png?1424965188',
successImg: 'https://s3.amazonaws.com/files.d20.io/images/7545189/5BR2W-XkmeVyXNsk-C8Z6g/thumb.png?1423165325',
skillTitleFmt: 'style="color: #000000; font-weight: bold; font-size: 125%;"',
skillLabelFmt: 'style="color: #44824C; font-weight: bold; font-size: 110%;"',
skillTopImg: 'https://s3.amazonaws.com/files.d20.io/images/7816000/-QKJOJF6UFmDAEypDGeXcw/thumb.png?1424460624',
skillMidImg: 'https://s3.amazonaws.com/files.d20.io/images/7816002/nKEWcTQ2VjcRJqrfiMHg_w/thumb.png?1424460630',
skillBotImg: 'https://s3.amazonaws.com/files.d20.io/images/7816003/lcNufMB1JeLmdDVPJ0KFdQ/thumb.png?1424460634',
spellBookTitleFmt: 'style="color: #000000; font-weight: bold; font-size: 125%;"',
spellBookLabelFmt: 'style="color: #44824C; font-weight: bold; font-size: 110%;"',
spellBookTopImg: 'https://s3.amazonaws.com/files.d20.io/images/7834797/n4P8Ys6gHoKmVlBTRYoi9Q/thumb.png?1424540093',
spellBookMidImg: 'https://s3.amazonaws.com/files.d20.io/images/7834799/LOJaPHkZcUDyekpMnm0-Cg/thumb.png?1424540096',
spellBookMidOvr: 'https://s3.amazonaws.com/files.d20.io/images/7836432/fi7Bw5cRaMfq0fjBZZ7ggg/thumb.png?1424544220',
spellBookBotImg: 'https://s3.amazonaws.com/files.d20.io/images/7834800/ORBbrVuMO7Ns2UoPRmvfRg/thumb.png?1424540099',
spellTitleFmt: 'style="color: #000000; font-weight: bold; font-size: 125%;"',
spellLabelFmt: 'style="color: #44824C; font-weight: bold; font-size: 110%;"',
spellTopImg: 'https://s3.amazonaws.com/files.d20.io/images/7382439/dJ-jaPdm6kEtl9lE__ve-g/thumb.png?1422405849',
spellMidImg: 'https://s3.amazonaws.com/files.d20.io/images/7382437/FYBtfPgkj4b3Q_hLkUk5Gg/thumb.png?1422405847',
spellBotImg: 'https://s3.amazonaws.com/files.d20.io/images/7382435/DXm7UUdLKNnF6ktFfVojWA/thumb.png?1422405843',
specialTitleFmt: 'style="color: #000000; font-weight: bold; font-size: 125%;"',
specialLabelFmt: 'style="color: #44824C; font-weight: bold; font-size: 110%;"',
specialTopImg: 'https://s3.amazonaws.com/files.d20.io/images/7816913/PsSBFlCv3IlPzGP0usiVBw/thumb.png?1424464282',
specialMidImg: 'https://s3.amazonaws.com/files.d20.io/images/7816781/0g1qpdJ80DTvpSHqQ2-7oA/thumb.png?1424463781',
specialBotImg: 'https://s3.amazonaws.com/files.d20.io/images/7816784/aZ1OFXDsO2BUM5G9oiXpgw/thumb.png?1424463791',
atkTitleFmt: 'style="color: #000000; font-weight: bold; font-size: 125%;"',
atkLabelFmt: 'style="color: #44824C; font-weight: bold; font-size: 110%;"',
atkTopImg: 'https://s3.amazonaws.com/files.d20.io/images/7816913/PsSBFlCv3IlPzGP0usiVBw/thumb.png?1424464282',
atkMidImg: 'https://s3.amazonaws.com/files.d20.io/images/7816781/0g1qpdJ80DTvpSHqQ2-7oA/thumb.png?1424463781',
atkBotImg: 'https://s3.amazonaws.com/files.d20.io/images/7816784/aZ1OFXDsO2BUM5G9oiXpgw/thumb.png?1424463791',
atkMenuTitleFmt: 'style="color: #000000; font-weight: bold; font-size: 125%;"',
atkMenuLabelFmt: 'style="color: #44824C; font-weight: bold; font-size: 110%;"',
atkMenuTopImg: 'https://s3.amazonaws.com/files.d20.io/images/7816912/WWQxo9xFhc-vETYw79f0wA/thumb.png?1424464275',
atkMenuMidImg: 'https://s3.amazonaws.com/files.d20.io/images/7816781/0g1qpdJ80DTvpSHqQ2-7oA/thumb.png?1424463781',
atkMenuBotImg: 'https://s3.amazonaws.com/files.d20.io/images/7816783/m3UhKz4plb_0TB1kUOAI1Q/thumb.png?1424463787',
genTitleFmt: 'style="color: #000000; font-weight: bold; font-size: 125%;"',
genLabelFmt: 'style="color: #44824C; font-weight: bold; font-size: 110%;"',
genTopImg: 'https://s3.amazonaws.com/files.d20.io/images/7816014/jpO1FF9pA7Ipi__sFQAAMw/thumb.png?1424460680',
genMidImg: 'https://s3.amazonaws.com/files.d20.io/images/7816016/0XT65Rctt1imgamKQUO6sg/thumb.png?1424460684',
genBotImg: 'https://s3.amazonaws.com/files.d20.io/images/7816023/T6uTWEX4aMDL-78lhSLanw/thumb.png?1424460752',
};
var atkTemplate = '<div class="img" style="column-gap: 0; line-height: 0; position: relative; text-align: center;">'
+ '<img src="'+"https://s3.amazonaws.com/files.d20.io/images/7926600/1gnY_LsAt4UHnkJmI3PAlA/thumb.png?1424966062"+'">'
+ '</div>'
+ '<div style="background-image: url('+"https://s3.amazonaws.com/files.d20.io/images/7926601/79qaHPngD_d9MFVlrNTB8w/thumb.png?1424966067"+'); background-repeat: repeat-y; background-position: center center; text-align: center; padding-left: 7px; padding-right: 7px;">'
+ '<div>'
+ '<div style="colspan: 2; color: #FFFFFF; font-style: italic; text-shadow: -1px -1px 1px #000, 1px -1px 1px #000, -1px 1px 1px #000, 1px 1px 1px #000; padding-top: 4px; padding-bottom: 4px; margin-left: auto; margin-right: auto; width: 160px">'
+ '<span style="font-weight: bold;"><<TITLE>></span>'
+ '</div>'
+ '<div style="color: #FFFFFF; text-shadow: -1px -1px 1px #000, 1px -1px 1px #000, -1px 1px 1px #000, 1px 1px 1px #000; padding-top: 4px; padding-bottom: 4px; display: block; font-weight: bold;">'
+ '<table style="width: 140px; text-align: left; margin-left: auto; margin-right: auto; border-spacing: 5;">'
+ '<tr>'
+ '<td>' + 'Attack:' + '</td>'
+ '<td>' + '<<ATTACK>>' + '</td>'
+ '</tr>'
+ '<tr>'
+ '<td>' + 'Damage:' + '</td>'
+ '<td>' + '<<DAMAGE>>' + '</td>'
+ '</tr>'
+ '</table>'
+' </div>'
+ '</div>'
+ '</div>'
+ '<div class="img" style="column-gap: 0; line-height: 0; position: relative; text-align: center;">'
+ '<img src="'+"https://s3.amazonaws.com/files.d20.io/images/7926602/lMjFWWNyFpUlbk80HqkeOQ/thumb.png?1424966073"+'">'
+ '</div>';
var menuTemplate = {
boundryImg: _.template('<div class="img" style="column-gap: 0; line-height: 0; position: relative; text-align: center;">'
+ '<img src="<%= imgLink %>">'
+ '</div>'),
titleFmt: _.template('<div>'
+ '<span <%= style %> >'
+ '<%= title %>'
+ '</span>'
+ '</div>'),
midDiv: _.template('<div style="background-image: url('
+ '<%= imgLink %>'
+ '); background-repeat: repeat-y; background-position: center center; text-align: center;">'),
/*
midDivOverlay: _.template('<div style="background: url(<%= imgLink %>) center center no-repeat; pointer-events: none;'
+ 'position: absolute; left: 0; top: 0; width: 200px; height: 200px;'
+ ' text-align: center;">'),
*/
midDivOverlay: _.template('<div style="background: '
+ 'url(<%= imgLinkOverlay %>) no-repeat center center,'
+ 'url(<%= imgLink %>) repeat-y center center;'
+ ' text-align: center; min-height: 150px;">'
+ '<table style="width: 100%; height: 100%; text-align: center; vertical-align: middle; min-height: 150px;">'
+ '<tr><td style="text-align: center; vertical-align: middle;">'),
midButton: _.template('<div style="text-align: center;">'
+ '<%= \'<span \'+ (riders ? (\'class="showtip tipsy" title="\' + riders + \'"\') : \'\') + \' style="font-weight: bold;">\' %>'
+ '<%= \'<a href="!\' + \'&\'+\'#37\' + \';{\'+creName+\'|\'+abName+\'}">\'+btnName+\'</a>\' %>'
+ '</span>'
+ '</div>'),
midButtonFree: _.template('<%= \'<span \'+ (riders ? (\'class="showtip tipsy" title="\' + riders + \'"\') : \'\') + \' style="font-weight: bold;">\' %>'
+ '<%= \'<a href="!\' + \'&\'+\'#37\' + \';{\'+creName+\'|\'+abName+\'}">\'+btnName+\'</a>\' %>'
+ '</span>'),
midLink: _.template('<div style="text-align: center;">'
+ '<%= \'<span \'+ (riders ? (\'class="showtip tipsy" title="\' + riders + \'"\') : \'\') + \' style="font-weight: bold;">\' %>'
+ '<%= link %>'
+ '</span>'
+ '</div>'),
midLinkFree: _.template('<%= \'<span \'+ (riders ? (\'class="showtip tipsy" title="\' + riders + \'"\') : \'\') + \' style="font-weight: bold;">\''
+ '+ link +'
+ '\'</span>\' %>'),
midLinkCaption: _.template('<div style="text-align: center;">'
+ '<%= link %>'
+ '<%= (riders ? (\'<span style="color: #2B2B2B; font-weight: lighter; font-style: italic; font-size: 70%;">\' + riders + \'</span>\'):"") %>'
+ '</div>'),
midLeadText: _.template('<%= \'<span \'+ (riders ? (\'class="showtip tipsy" title="\' + riders + \'" style="font-weight: bold; color:#FFFFFF; text-shadow: -1px -1px 1px #000, 1px -1px 1px #000, -1px 1px 1px #000, 1px 1px 1px #000;">\') : \' style="font-weight: bold; color:#000000;">\') %>'
+ '<%= label %>'
+ '</span> <span style="color: #000000;"><%= text %></span>'),
midText: _.template('<%= \'<span \'+ (riders ? (\'style="font-weight: bold; color:#FFFFFF; text-shadow: -1px -1px 1px #000, 1px -1px 1px #000, -1px 1px 1px #000, 1px 1px 1px #000;" class="showtip tipsy" title="\' + riders + \'"\') : \' style="color: #000000;"\') + \'>\' %>'
+ '<%= text %>'
+'</span>'),
};
/**
* Select design template
*/
var selectDesignTemplate = function(name, quiet) {
var flag;
if (typeof(CGTmp) !== "undefined") {
try {
for (var tmp in CGTmp.designTmp) {
if (tmp.toLowerCase() === name) {
design = CGTmp.designTmp[tmp];
if (!quiet) {
sendFeedback('<span style="color: #653200; font-weight: bold;">Design template:</span> <b style="color: #009C26;">' + tmp + '</b> has been '
+ 'configured for future tokens generated.</span>');
}
state.cgen_design = tmp;
flag = true;
break;
}
}
if (!flag)
{sendFeedback('<span style="color: #990004;">Design template: \'' + name + '\' does not exist</span>');}
} catch (e) {
log(e);
}
} else {
sendFeedback('<span style="color: #990004;">No CreatureGen templates found, '
+ 'did you load the additional templates script?</span>');
}
return flag;
};
/**
* Select attack template
*/
var selectAttackTemplate = function(name, quiet) {
var flag;
if (typeof(CGTmp) !== "undefined") {
try {
for (var tmp in CGTmp.attackTmp) {
if (tmp.toLowerCase() === name) {
fields.tmpAtk = CGTmp.attackTmp[tmp];
if (!quiet) {
sendFeedback('<span style="color: ##155391; font-weight: bold;">Attack template:</span> <b style="color: #009C26;">' + tmp + '</b> has been '
+ 'configured for future tokens generated.</span>');
}
state.cgen_attack = tmp;
flag = true;
break;
}
}
if (!flag)
{sendFeedback('<span style="color: #990004;">Attack template: \'' + name + '\' does not exist</span>');}
} catch (e) {
log(e);
}
} else {
sendFeedback('<span style="color: #990004;">No CreatureGen templates found, '
+ 'did you load the additional templates script?</span>');
}
return flag;
};
/**
* Object fix to resolve firebase errors
*
* @author Shu Zong C.
*/
var fixNewObject = function(obj) {
var p = obj.changed._fbpath;
var new_p = p.replace(/([^\/]*\/){4}/, "/");
obj.fbpath = new_p;
return obj;
};
/**
* scan in information from token notes
*
* @contribuitor Andy W.
*/
var scan = function(token) {
var charSheet;
var data;
var rawData;
var dispData = "";
if (!token) {
throw "No Token selected";
}
rawData = token.get("gmnotes");
creLog('RAW: ' + rawData);
if (!rawData)
{throw "no token notes";}
data = rawData.split(/%3Cbr%3E|\\n|<br>/);
//clean out all other data except text
for (var i = data.length; i >= 0; i--) {
if (data[i]) {
data[i] = cleanString(data[i]).trim();
if (!data[i].match(/[^\s]/)) {
data.splice(i,1);
}
}
}
// Essential parameters for object creation (mainly the name)
parseEssential(data);
// if character name already exists, close out.
if (findObjs({
_type: "character",
name: creName,
}).length > 0) {
addWarning('Character \''+creName+'\' already exists.');
throw 'Character \''+creName+'\' already exists.';
}
dispData = formatDisplay(data);
charSheet = createObj("character", {
avatar: token.get("imgsrc"),
name: creName,
gmnotes: '',
archived: false,
inplayerjournals: '',
controlledby: ''
});
charSheet = fixNewObject(charSheet);
if (!charSheet) {
throw "ERROR: could not create character sheet";
}
if (fields.summoner) {
charSheet.set('bio','<br clear="both">' + dispData);
}
token.set("represents",charSheet.get('_id'));
charSheet.set('gmnotes',dispData);
character = charSheet;
// warn on image source
if (charSheet.get('avatar') === '') {
addWarning('Unable to set avatar to character journal, only images you\'ve '
+ 'uploaded yourself are viable during creation. Auto-population '
+ '<i>(drag-drop population)</i> will not be possible without an avatar image.'
+ ' You can still upload an avatar manually, or drag an image into the avatar field'
+ ' from the image-search.');
}
// parse up our data set.
var specials;
try {
parseCore(data);
specials = parseSpecials(data);
parseAttacks(data,specials);
parseSpells(data);
parseExtra(data,specials);
prepToken(token,charSheet);
} catch (e) {
log("ERROR when parsing");
throw e;
}
}
/**
* Format display of stat-block
*/
var formatDisplay = function(datum) {
if (!datum)
{return undefined;}
var content = '';
_.each(datum, function(e,i,l) {
creLog('('+i+') ' + e,1);
if (e.match('DEFENSE')
|| e.match('OFFENSE')
|| e.match('TACTICS')
|| e.match('BASE STATISTICS')
|| e.match('STATISTICS')
|| e.match('ECOLOGY')
|| e.match('SPECIAL ABILITIES'))
{content += '<div style="font-size: 112%; border-bottom: 1px solid black; border-top: 1px solid black; margin-top: 8px;">'+e+'</b></div>';}
else if (e.match(/\(Ex\)|\(Su\)|\(Sp\)/i))
{content += '<div>' + e + '</div>';}
else
{content += e+'<br>';}
});
return content;
};
/**
* Prep the token.
* Asynchronous
*/
var prepToken = function(token,character) {
if (!token || !character)
{return undefined;}
var name,AC,hp,prep;
var charId = character.get('_id');
hp = getAttribute("hp", charId);
AC = getAttribute("AC", charId);
prep = getAttribute("CGEN", charId);
name = character.get('name');
// Fast vs cb delay
if (token.get('gmnotes')) {
if (hp && AC && name && prep
&& (prep.get('current').match(/true/i))) {
hp = hp.get('current');
AC = AC.get('current');
token.set('bar1_value',hp);
token.set('bar1_max', hp);
token.set('bar3_value',AC);
token.set('name',name);
token.set('showname',true);
token.set('light_hassight',true);
resizeToken(token,character);
}
} else {
character.get('gmnotes',function(notes) {
if (hp && AC && name && prep
&& (prep.get('current').match(/true/i))
&& notes && (notes !== '')) {
hp = hp.get('current');
AC = AC.get('current');
token.set('bar1_value',hp);
token.set('bar1_max', hp);
token.set('bar3_value',AC);
token.set('name',name);
token.set('showname',true);
token.set('light_hassight',true);
token.set('gmnotes', notes
.replace(/<div[^<>]*>/g,'')
.replace(/<\/div>/g,'<br>'));
resizeToken(token,character);
}
});
}
};
/**
* Resize Token based on size attribute
*/
var resizeToken = function(token,character) {
var charId = character.get('_id');
var unitSize = 1;
var pageScale = getObj('page',token.get('_pageid')).get('snapping_increment');
var tsize = parseInt(unitPixels*(pageScale===0 ? 1:pageScale));
var size = getAttribute("Size", charId);
if (!size)
{return;}
switch (size.get('current')) {
case 'Fine':
case 'Diminutive':
case 'Tiny':
case 'Small':
case 'Medium':
break;
case 'Large':
unitSize = 2;
break;
case 'Huge':
unitSize = 3;
break;
case 'Gargantuan':
unitSize = 4;
break;
case 'Colossal':
unitSize = 6;
break;
default:
creLog('resizeToken: Bad size \''+size+'\' ');
}
token.set('width',tsize*unitSize);
token.set('height',tsize*unitSize);
};
var parseEssential = function(data) {
/* names are tricky as we delimit on CR, last occurance of CR
which has numbers after it TODO use a regex which is shorter*/
var namefield = data[0];
var delimiter_idx =namefield.lastIndexOf("CR");
var fuzzyfield = namefield.substring(delimiter_idx,namefield.length);
if ((delimiter_idx <= 0) || !(fuzzyfield.match(/\d+|—/g)))
{delimiter_idx = namefield.length;}
var name = namefield.substring(0,delimiter_idx);
name = name.trim().toLowerCase();
creName = (fields.summoner ? (fields.summoner.get('_displayname')+'\'s ' + name):name);
fields.publicName = (fields.summoner ? creName:fields.publicName);
};
/**
* parse core attributes, AC, HP, etc
*/
var parseCore = function(data) {
if (!data)
{return;}
if (fields.summoner) {
character.set('controlledby',fields.summoner.get('_id'));
character.set('inplayerjournals',fields.summoner.get('_id'));
}
var charId = character.get('_id');
var line = "";
var lineStartFnd = 0;
var lineEndFnd = data.length;
var termChars = [';',','];
var rc = -1;
// core attribute fields
var initAttr = "Init";
var primeAttr = ["Str","Dex","Con","Int","Wis","Cha"];
var minorAttr = ["Base Atk","CMB","CMD"];
var defAttr = ["AC","touch","flat-footed","hp"];
var saveAttr = ["Fort","Ref","Will"];
var hp=0,AC=0,tAC=0,ffAC=0;
//Flag that this token will be prepped, can be removed by the user
addAttribute("CGEN",'true','',charId);
addAttribute("name",fields.defaultName,'',charId);
// Init (TODO mythic Init)
line = getLineByName(initAttr,data);
rc = getValueByName(initAttr,line,termChars);
addAttribute(initAttr,rc,rc,charId);
// prime attributes
lineStartFnd = getLineNumberByName("STATISTICS",data);
lineEndFnd = getLineNumberByName("SPECIAL ABILITIES",data);
line = getLineByName("Str",data,lineStartFnd,lineEndFnd);
addAttrList(data,line,primeAttr,lineStartFnd,termChars,charId);
// minor attributes
line = getLineByName("Base Atk",data,lineStartFnd,lineEndFnd);
addAttrList(data,line,minorAttr,lineStartFnd,termChars,charId);
// defense attributes
lineStartFnd = getLineNumberByName("DEFENSE",data);
lineEndFnd = getLineNumberByName("OFFENSE",data);
line = getLineByName("AC",data,lineStartFnd,lineEndFnd);
addAttrList(data,line,defAttr,lineStartFnd,termChars,charId);
// save attributes
line = getLineByName("Fort",data,lineStartFnd,lineEndFnd);
addAttrList(data,line,saveAttr,lineStartFnd,termChars,charId);
//format attributes:
hp = formatAttribute("hp",0,charId);
creLog("parsecore: HP: " + hp,1);
AC = formatAttribute("AC",0,charId);
creLog("parsecore: AC: " + AC,1);
tAC = formatAttribute("touch",0,charId);
creLog("parsecore: touch AC: " + tAC,1);
ffAC = formatAttribute("flat-footed",0,charId);
creLog("parsecore: flat-foot AC: " + ffAC,1);
// determine size
var size = parseSize(data);
creLog("parsecore: size: " + size,1);
addAttribute('Size',size,size,charId);
// save riders
if ((rc=line.indexOf(';')) !== -1) {
rc = '('+line.substring(rc+1)+')';
} else {rc = "";}
addAttributeRoll("INIT",initAttr,false,true,charId,"&{tracker}");
addAttributeRoll("F","Fort",false,true,charId,rc);
addAttributeRoll("R","Ref",false,true,charId,rc);
addAttributeRoll("W","Will",false,true,charId,rc);
};
/**
* Parse Size of the creature
* TODO modify getLineByName and getLineNumberByName to allow regex
*/
var parseSize = function(data) {
var retval = 'Medium';
var lineEndFnd = getLineNumberByName('DEFENSE',data);
var space = getLineByName('Space',data,getLineNumberByName('OFFENSE',data),getLineNumberByName('STATISTICS',data));
creLog('parseSize: space is ' + space);
if (space) {
space = getValueByName('Space',space,[';',',']);
space = getBonusNumber(space,bonusEnum.SCALAR);
space = parseInt(space);
if (isNaN(space))
{return;}
space = space/5;
switch(space) {
case 1:
retval = 'Medium';
break;
case 2:
retval = 'Large';
break;
case 3:
retval = 'Huge';
break;
case 4:
retval = 'Gargantuan';
break;
case 6:
retval = 'Colossal';
break;
default:
retval = 'Medium';
break;
}
} else if (getLineByName('Fine',data,0,lineEndFnd) || getLineByName('fine',data,0,lineEndFnd)) {
retval = 'Fine';
} else if (getLineByName('Diminutive',data,0,lineEndFnd) || getLineByName('diminutive',data,0,lineEndFnd)) {
retval = 'Diminutive';
} else if (getLineByName('Tiny',data,0,lineEndFnd) || getLineByName('tiny',data,0,lineEndFnd)) {
retval = 'Tiny';
} else if (getLineByName('Small',data,0,lineEndFnd) || getLineByName('small',data,0,lineEndFnd)) {
retval = 'Small';
} else if (getLineByName('Medium',data,0,lineEndFnd) || getLineByName('Medium',data,0,lineEndFnd)) {
retval = 'Medium';
} else if (getLineByName('Large',data,0,lineEndFnd) || getLineByName('Large',data,0,lineEndFnd)) {
retval = 'Large';
} else if (getLineByName('Huge',data,0,lineEndFnd) || getLineByName('huge',data,0,lineEndFnd)) {
retval = 'Huge';
} else if (getLineByName('Gargantuan',data,0,lineEndFnd) || getLineByName('gargantuan',data,0,lineEndFnd)) {
retval = 'Gargantuan';
} else if (getLineByName('Colossal',data,0,lineEndFnd) || getLineByName('colossal',data,0,lineEndFnd)) {
retval = 'Colossal';
}
return retval;
};
/**
* parse special attacks, if the statblock has 'riders' which give
* details on the special abilities, then include it as part of the marco.
* TODO: add option for verbrocity during generaton.
*/
var parseSpecials = function(data) {
var retval = {};
var charId = character.get('_id');
var line = "";
var lineStartFnd = 0;
var lineEndFnd = data.length;
var re, saName, abName, sAtks, sAtkStr,
action, actionStr, spList, hasSAtks;
line = getLineByName("Special Attacks",data);
if (line) {
line = line.replace("Special Attacks","");
sAtks = line.split(/,(?![^\(\)]*\))/);
while (sAtks.length > 0) {
if (!sAtks[0] || !sAtks[0].match(/[^\s]+/)) {
sAtks.shift();
continue;
}
saName = sAtks[0].match(/\b[^\d\(\)\+]+/g);
if (saName) {
saName = saName[0].trim();
sAtkStr += saName;
if (!retval[saName])
{retval[saName] = new Array(sAtks[0].trim());}
else
{retval[saName].push(sAtks[0].trim());}
creLog("parseSpecials " + sAtks[0] + " saName: " + saName,1);
}
sAtks.shift();
}
}
/* TODO add in nextLine support for cases where the special ability
name is found on the following line(s) */
lineStartFnd = getLineNumberByName("SPECIAL ABILITIES",data);
if (lineStartFnd) {
for (var i = lineStartFnd; i < data.length; ++i) {
line = data[i];
if (line.match(/\(Su\)|\(Ex\)|\(Sp\)/i)) {
saName = line.substring(0,line.indexOf('(')).toLowerCase().trim();
re = new RegExp(saName,'ig');
action = "!\n" + fields.menuWhis +
line.replace(re,getTermLink(saName,termEnum.GENERAL));
if (!retval[saName])
{retval[saName] = new Array(line.trim());}
else
{retval[saName].push(line.trim());}
addAbility("SA-"+saName,"",action,false,charId);
}
}
}
/* If there is a special attack, that is a special attack not ability,
then it is unique and should get its own ability as well as long-rider
if one exists.*/
spList = "!\n" + fields.menuWhis
+ menuTemplate.boundryImg({imgLink: design.specialTopImg})
+ menuTemplate.midDiv({imgLink: design.specialMidImg})
+ '<div style="text-align:center">'
+ menuTemplate.titleFmt({
style: design.specialTitleFmt,
title: creName
})
+ menuTemplate.titleFmt({
style: design.specialLabelFmt,
title: 'Special Attacks'
})
+'</div>';
/*
// DEPRECATED (exclude special attacks that are melee/ranged riders)
// insure we have melee or ranged
line = getLineByName("Melee",data)
+ getLineByName("Ranged",data) + '';
*/
if (sAtkStr) {
_.every(_.keys(retval), function(sAtks) {
if (/*!line.match(sAtks) &&*/ (sAtkStr.indexOf(sAtks) !== -1)) {
hasSAtks = true;
abName = "SP-" + sAtks;
abName = abName.replace(/\s/g,"-");
action = "!\n" + fields.resultWhis;
_.every(retval[sAtks], function(rider) {
creLog("SP rider: " + rider,3);
re = new RegExp(sAtks,"ig");
actionStr = "<div>"+getFormattedRoll(rider)+"</div>";
action += actionStr.replace(re,getTermLink(sAtks,termEnum.GENERAL));
return true;
});
creLog("SP action: " + action,3);
addAbility(abName,"",action,false,charId);
spList = spList
+ menuTemplate.midButton({
riders: undefined,
creName: creName,
abName: abName,
btnName: sAtks
});
}
return true;
});
}
if (hasSAtks) {
spList = spList
+ '</div>'
+ menuTemplate.boundryImg({imgLink: design.specialBotImg});
addAbility("Specials",'',spList,false,charId);
}
return retval;
};
/**
* parse melee and ranged attacks, if there are special attack riders,
* then we will append the marco text
*/
var parseAttacks = function(data,specials) {
var charId = character.get('_id');
var line = "";
var lineStartFnd = 0;
var lineEndFnd = data.length;
var atkMenu, hasSAtk = false, CMB, riders;
atkMenu = fields.menuWhis
+ menuTemplate.boundryImg({imgLink: design.atkMenuTopImg})
+ menuTemplate.midDiv({imgLink: design.atkMenuMidImg})
+ '<div style="text-align:center">'
+ menuTemplate.titleFmt({
style: design.atkMenuTitleFmt,
title: creName
})
+ menuTemplate.titleFmt({
style: design.atkMenuLabelFmt,
title: 'Attacks'
})
+'</div>';
lineStartFnd = getLineNumberByName("OFFENSE",data);
lineEndFnd = getLineNumberByName("TACTICS",data);
if (!lineEndFnd)
{lineEndFnd = getLineNumberByName("STATISTICS",data);}
try {
line = getLineByName("Melee",data,lineStartFnd,lineEndFnd);
if (line) {
formatAttacks(line,"Melee",charId,"ATK",specials);
atkMenu = atkMenu
+ menuTemplate.midButton({
riders: undefined,
creName: creName,
abName: 'ATK',
btnName: 'Melee'
});
}
line = getLineByName("Ranged",data,lineStartFnd,lineEndFnd);
if (line) {
formatAttacks(line,"Ranged",charId,"RNG",specials);
atkMenu = atkMenu
+ menuTemplate.midButton({
riders: undefined,
creName: creName,
abName: 'RNG',
btnName: 'Ranged'
});
}
hasSAtk = findObjs({
_type: "ability",
name: "Specials",
_characterid: charId
});
if (hasSAtk.length > 0) {
atkMenu = atkMenu
+ menuTemplate.midButton({
riders: undefined,
creName: creName,
abName: 'Specials',
btnName: 'Specials'
});
}
lineStartFnd = getLineNumberByName("STATISTICS",data);
line = getLineByName("CMB",data,lineStartFnd);
if (line) {
CMB = getValueByName("CMB",line,[',',';']);
riders = CMB.match(/\(.+\)/);
addAttributeRoll("CMB","CMB",false,false,charId,(riders ? riders:''));
atkMenu = atkMenu
+ menuTemplate.midButton({
riders: riders,
creName: creName,
abName: 'CMB',
btnName: 'CMB'
});
}
} catch (e) {
log ("ERROR when parsing attacks: ");
throw e;
}
atkMenu = atkMenu
+ '</div>'
+ menuTemplate.boundryImg({imgLink: design.atkMenuBotImg});
addAbility("Attacks",'',atkMenu,true,charId);
};
/** parse out spells the marco will spit them out, possibly even link
* them to a known PRD search engine.
*/
var parseSpells = function(data) {
var charId = character.get('_id');
var lineEndFnd = data.length;
var casterType, attrName;
var rc, line = "";
var termChars = [';',','];
lineEndFnd = getLineNumberByName("TACTICS",data);
if (!lineEndFnd)
{lineEndFnd = getLineNumberByName("STATISTICS",data);}
formatSpells("Spell-Like Abilities",data,lineEndFnd,termEnum.GENERAL,"SLA");
formatSpells("Spells Known",data,lineEndFnd,termEnum.SPELL);
formatSpells("Spells Prepared",data,lineEndFnd,termEnum.SPELL);
formatSpells("Extracts Prepared",data,lineEndFnd,termEnum.SPELL);
};
/**
* Generic Parse assuming CSV on the line.
*
*/
var parseGeneric = function(generic,data,type,start,end) {
if (!generic || !type)
{return undefined;}
if (!start)
{start = 0;}
if (!end)
{end = data.length;}
var charId = character.get('_id');
var genName, genRiders, genAry,
idx, genList, abName, genLabel;
var lineStartFnd = 0;
var rc, line = "";
var termChars = [';'];
genList = "!\n" + fields.menuWhis
+ menuTemplate.boundryImg({imgLink: design.genTopImg})
+ menuTemplate.midDiv({imgLink: design.genMidImg})
+ '<div style="text-align:center">'
+ menuTemplate.titleFmt({
style: design.genTitleFmt,
title: creName
})
+ menuTemplate.titleFmt({
style: design.genLabelFmt,
title: generic
})
+'</div>';
line = getLineByName(generic,data,start,end);
line = getValueByName(generic,line,termChars);
creLog("parseGeneric: " + line,1);
if (line) {
line = line.replace(generic,"");
genAry = line.split(/,(?![^\(\)]*\))/);
if (genAry) {
_.every(genAry, function(elemGen) {
if ((idx=elemGen.indexOf("(")) !== -1) {
genName = elemGen.substring(0,idx).trim();
genRiders = elemGen.substring(idx).trim();
} else {
genName = elemGen.trim();
genRiders = undefined;
}
genName = formatSuperSubScript(genName);
genList = genList
+ menuTemplate.midLink({
riders: genRiders,
link: getTermLink(genName,type)
});
return true;
});
genList = genList
+ '</div>'
+ menuTemplate.boundryImg({imgLink: design.genBotImg});
addAbility(generic,'',genList,false,charId);
}
}
};
/** parse out skills the marco will spit them out, possibly even link
* them to a known PRD search engine.
*/
var parseSkills = function(data) {
var charId = character.get('_id');
var lineStartFnd = 0;
var lineEndFnd = data.length;
var skillName, skillRiders, skillAry,
skillList, abName, skillLabel,
parts, abStr, racialBonus, racialAry,
racialRiders;
var rc, line = "";
var termChars = [';',','];
skillList = "!\n" + fields.menuWhis
+ menuTemplate.boundryImg({imgLink: design.skillTopImg})
+ menuTemplate.midDiv({imgLink: design.skillMidImg})
+ '<div style="text-align:center">'
+ menuTemplate.titleFmt({
style: design.skillTitleFmt,
title: creName
})
+ menuTemplate.titleFmt({
style: design.skillLabelFmt,
title: 'Skills'
})
+'</div>';
lineStartFnd = getLineNumberByName("STATISTICS",data);