-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathFunctions3.js
3822 lines (3612 loc) · 162 KB
/
Functions3.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
// Copy all the attributes of a field to another field (or even swap between the two)
// excl is an object with optional attributes { userName : true, submitName : true, readonly : true, noCalc : true, defaultValue : true }
function copyField(fldFromName, fldToName, excl, swap) {
var fldTo = tDoc.getField(fldToName);
var fldFrom = tDoc.getField(fldFromName);
if (!fldTo || !fldFrom || fldTo.type !== fldFrom.type) return;
if (!excl) excl = {};
// a function to do the actual copying
var copy = function(fromObj, toObj, justObj) {
if (fromObj.type == "checkbox") {
if (justObj) {
toObj.isBoxCheckVal = fromObj.isBoxChecked(0);
toObj.isBoxChecked = function() { return saveTo.isBoxCheckVal; };
toObj.type = "checkbox";
} else {
toObj.checkThisBox(0, fromObj.isBoxChecked(0));
}
} else if (excl.defaultValue && fromObj.value === fromObj.defaultValue) {
toObj.value = toObj.defaultValue;
if (justObj) toObj.type = "text";
} else {
toObj.value = fromObj.value;
if (justObj) toObj.type = "text";
}
if (!excl.userName) toObj.userName = fromObj.userName;
if (!excl.submitName) toObj.submitName = fromObj.submitName;
if (!excl.readonly) toObj.readonly = fromObj.readonly;
if (fromObj.type == "text" && !excl.noCalc && !justObj) {
toObj.setAction("Calculate", toObj.submitName);
}
}
// If swapping the fields, first save the fldTo attributes to a separate object
if (swap) {
var saveTo = {};
copy(fldTo, saveTo, true);
}
// Apply the attributes to the fldTo
copy(fldFrom, fldTo);
// If swapping the fields, now apply the fldTo attributes to the fldFrom from the object
if (swap) copy(saveTo, fldFrom);
}
// a function to get the different versions of names used
function GetFeatureType(type, bNoDefaultReturn, bSingularReturn) {
var theReturn = bNoDefaultReturn ? false : bSingularReturn ? "class" : "classes";
switch (type.toLowerCase()) {
case "classes":
case "class":
theReturn = bSingularReturn ? "class" : "classes";
break;
case "backgrounds":
case "background":
theReturn = "background";
break;
case "background features":
case "background feature":
theReturn = "background feature";
break;
case "races":
case "race":
theReturn = "race";
break;
case "feats":
case "feat":
theReturn = bSingularReturn ? "feat" : "feats";
break;
case "magicitems":
case "magicitem":
case "magic item":
case "magic items":
case "items":
case "item":
theReturn = bSingularReturn ? "item" : "items";
break;
case "magic":
theReturn = "magic";
break;
};
return theReturn;
}
/* ---- ApplyFeatureAttributes ----
A function to handle all the common attributes a feature can have
Input:
type - the type of thing being processed
STRING "class", "race", "feat", or "item"
fObjName - the object name; array only for class/race with features
if type="feat" or type="item":
STRING
if type="class" or type="race":
ARRAY [STRING: class/race-name, STRING: feature-name]
// for a race, if the feature-name is also the race-name, the parent race object will be used
lvlA - old and new level and a true/false to force updating regardless of old-level
ARRAY [INTEGER: old-level, INTEGER: new-level, BOOLEAN: force-apply]
choiceA - child object names of overriding choice
ARRAY [STRING: old-choice, STRING: new-choice, STRING: "only","change"]
// if 'only-choice' is set to true, it is viewed as an extra-choice and just the child features will be used (e.g. Warlock Invocations)
forceNonCurrent - the parent object name if the sheet is to use the original list object and not the CurrentXXX (CurrentRace, CurrentClasses)
STRING
Examples:
ApplyFeatureAttributes("feat", "grappler", [0,1,false], false, false);
ApplyFeatureAttributes("class", ["warlock","pact boon"], [4,4,true], ["pact of the blade","pact of the chain","change"], false); // change from Pact of the Blade to Pact of the Chain
ApplyFeatureAttributes("class", ["warlock","eldritch invocations"], [0,4,true], ["","devil's sight","only"], false); // add Devil's Sight
ApplyFeatureAttributes("class", ["warlock","eldritch invocations"], [15,0,true], ["devil's sight","","only"], false); // remove Devil's Sight
*/
function ApplyFeatureAttributes(type, fObjName, lvlA, choiceA, forceNonCurrent) {
if (!IsNotReset) return; //stop this function on a reset
// validate input
if (!lvlA) lvlA = [0,1,false];
if (!choiceA) choiceA = ["","",false];
type = type.toLowerCase();
// base variables
var FeaChoice = "", FeaOldChoice = "", tipNmExtra = "";
var aParent = fObjName;
var lvlH = Math.max(lvlA[0], lvlA[1]), lvlL = Math.min(lvlA[0], lvlA[1]);
var defaultUnits = What("Unit System") === "imperial";
var choiceLimFeaTooltip;
// the function to run an eval string/function
var runEval = function(evalThing, attributeName, ignoreUnits) {
if (!evalThing) return;
try {
var convertUnits = false;
if (typeof evalThing == 'string') {
var convertUnits = !defaultUnits && !ignoreUnits && !(/ConvertTo(Metric|Imperial)/).test(evalThing);
if (convertUnits) evalThing = ConvertToMetric(evalThing, 0.5);
eval(evalThing);
} else if (typeof evalThing == 'function') {
evalThing(lvlA, choiceA);
}
} catch (error) {
// the error could be caused by the ConvertToMetric function, so try it without to see if that works
if (convertUnits) {
runEval(evalThing, attributeName, true);
return;
}
var eText = "The " + attributeName + " from '" + fObjName + (aParent ? "' of the '" + aParent : "") + "' " + type + " produced an error! Please contact the author of the feature to correct this issue and please include this error message:\n " + error;
for (var e in error) eText += "\n " + e + ": " + error[e];
console.println(eText);
console.show();
}
}
// the function to run all regular level-independent attributes
// addIt = true to add things and addIt = false to remove things
var useAttr = function(uObj, addIt, skipEval, objNm) {
var uniqueObjNm = objNm == undefined ? fObjName : fObjName + objNm; // has to be unique
var tipNm = displName;
var useSpCasting = objNm && (type === "feat" || type === "magic item") && !CurrentSpells[aParent] ? aParent + "_-_" + objNm : aParent;
if (cnt > 1) {
tipNm += " (" + cnt + ")";
if (cntCh > 1) uniqueObjNm += " (" + cntCh + ")";
}
if (!uObj.name || displName !== uObj.name) {
if (type === "feat" || type === "magic item") {
if (uObj.name) {
tipNm = uObj.name;
} else if (objNm && fObj.choices) {
for (var j = 0; j < fObj.choices.length; j++) {
if (fObj.choices[j].toLowerCase() == objNm) {
tipNm = displName + " [" + fObj.choices[j] + "]";
break;
}
}
}
if (cntCh > 1) tipNm += " (" + cntCh + ")";
if (addIt) choiceLimFeaTooltip = tipNm;
} else if (uObj.name) {
tipNm = displName + ": " + uObj.name;
}
}
var tipNmF = tipNm + (tipNmExtra ? " " + tipNmExtra : "");
// we should add the options for weapons/armours/ammos before adding the item itself
// but we should be removing them only after removing the item itself
var addListOptions = function() {
if (uObj.armorOptions) processArmorOptions(addIt, tipNm, uObj.armorOptions, type === "magic item");
if (uObj.ammoOptions) processAmmoOptions(addIt, tipNm, uObj.ammoOptions, type === "magic item");
if (uObj.weaponOptions) processWeaponOptions(addIt, tipNm, uObj.weaponOptions, type === "magic item");
if (uObj.creatureOptions) processCreatureOptions(addIt, tipNm, uObj.creatureOptions);
}
// run eval or removeeval first
var evalType = addIt ? "eval" : "removeeval";
if (!skipEval && uObj[evalType]) runEval(uObj[evalType], evalType);
if (uObj.calcChanges) addEvals(uObj.calcChanges, tipNmF, addIt, type);
if (uObj.savetxt) SetProf("savetxt", addIt, uObj.savetxt, tipNmF);
if (uObj.speed) SetProf("speed", addIt, uObj.speed, tipNmF);
if (uObj.addMod) processMods(addIt, tipNmF, uObj.addMod);
if (uObj.saves) processSaves(addIt, tipNmF, uObj.saves);
if (uObj.toolProfs) processTools(addIt, tipNmF, uObj.toolProfs);
if (uObj.languageProfs) processLanguages(addIt, tipNmF, uObj.languageProfs);
if (uObj.vision) processVision(addIt, tipNmF, uObj.vision);
if (uObj.dmgres) processResistance(addIt, tipNmF, uObj.dmgres);
if (uObj.action) processActions(addIt, tipNmF, uObj.action, uObj.limfeaname ? uObj.limfeaname : uObj.name);
if (uObj.extraLimitedFeatures) processExtraLimitedFeatures(addIt, tipNmF, uObj.extraLimitedFeatures);
if (uObj.extraAC) processExtraAC(addIt, tipNmF, uObj.extraAC, uObj.name);
if (uObj.toNotesPage) processToNotesPage(addIt, uObj.toNotesPage, type, uObj, fObj, [tipNm, displName, fObjName, aParent]);
if (uObj.carryingCapacity) SetProf("carryingcapacity", addIt, uObj.carryingCapacity, tipNmF);
if (uObj.advantages) processAdvantages(addIt, tipNmF, uObj.advantages);
// --- backwards compatibility --- //
var abiScoresTxt = uObj.scorestxt ? uObj.scorestxt : uObj.improvements ? uObj.improvements : false;
if (uObj.scores || abiScoresTxt) {
processStats(addIt, type, tipNm, uObj.scores, abiScoresTxt, false, uObj.scoresMaximum, uObj.scoresMaxLimited);
} else if (uObj.scoresMaximum) {
processStats(addIt, type, tipNm, uObj.scoresMaximum, abiScoresTxt, "maximums");
}
if (uObj.scoresOverride) processStats(addIt, type, tipNm, uObj.scoresOverride, abiScoresTxt, "overrides");
// spellcasting
if (uObj.spellcastingBonus) processSpBonus(addIt, uniqueObjNm, uObj.spellcastingBonus, type, aParent, objNm, forceNonCurrent ? true : false);
if (CurrentSpells[useSpCasting] && (uObj.spellFirstColTitle || uObj.spellcastingExtra || uObj.spellChanges || uObj.spellcastingExtraApplyNonconform !== undefined)) {
var aCast = CurrentSpells[useSpCasting];
if (uObj.spellFirstColTitle) {
aCast.firstCol = addIt ? uObj.spellFirstColTitle : false;
if (!uObj.spellcastingBonus && !uObj.spellcastingExtra && !uObj.spellChanges) {
SetStringifieds('spells');
CurrentUpdates.types.push("spells");
}
}
if (uObj.spellcastingExtra || uObj.spellcastingExtraApplyNonconform !== undefined) {
processSpellcastingExtra(addIt, useSpCasting, fObj.minlevel, tipNmF, uObj.spellcastingExtra, uObj.spellcastingExtraApplyNonconform);
}
if (uObj.spellChanges) processSpChanges(addIt, tipNmF, uObj.spellChanges, useSpCasting);
}
if (!uObj.spellcastingBonus && addIt && CurrentSpells[useSpCasting] && type !== "classes" && type !== "race" && (uObj.spellcastingAbility !== undefined || uObj.fixedDC || uObj.fixedSpAttack || uObj.allowUpCasting !== undefined || uObj.magicItemComponents !== undefined)) {
// will already have been processed if uObj has `spellcastingBonus`
var aCast = CurrentSpells[useSpCasting];
if (uObj.spellcastingAbility !== undefined) {
aCast.ability = ReturnSpellcastingAbility(useSpCasting, uObj.spellcastingAbility);
aCast.abilityToUse = getSpellcastingAbility(useSpCasting);
}
if (uObj.fixedDC) aCast.fixedDC = Number(uObj.fixedDC);
if (uObj.fixedSpAttack) aCast.fixedSpAttack = Number(uObj.fixedSpAttack);
if (uObj.allowUpCasting !== undefined) aCast.allowUpCasting = uObj.allowUpCasting;
if (uObj.magicItemComponents !== undefined) aCast.magicItemComponents = uObj.magicItemComponents;
SetStringifieds('spells');
CurrentUpdates.types.push("spells");
};
if (uObj.spellcastingBonusElsewhere) processSpellcastingBonusElsewhere(addIt, type, tipNm, uniqueObjNm, uObj.spellcastingBonusElsewhere);
if (addIt) addListOptions(); // add weapon/armour/ammo/creature option(s)
// --- backwards compatibility --- //
// armor and weapon proficiencies
var weaponProf = uObj.weaponProfs ? uObj.weaponProfs : (/^(class|feat)$/).test(type) && uObj.weapons ? uObj.weapons : uObj.weaponprofs ? uObj.weaponprofs : false;
if (weaponProf) processWeaponProfs(addIt, tipNmF, weaponProf);
var armorProf = uObj.armorProfs ? uObj.armorProfs : uObj.armor ? uObj.armor : false;
if (armorProf) processArmourProfs(addIt, tipNmF, armorProf);
// --- backwards compatibility --- //
// armor, shield, and weapon additions
var aWeaponsAdd = uObj.weaponsAdd ? uObj.weaponsAdd : type == "race" && uObj.weapons ? uObj.weapons : false;
if (aWeaponsAdd) processAddWeapons(addIt, aWeaponsAdd);
var anArmorAdd = uObj.armorAdd ? uObj.armorAdd : uObj.addarmor ? uObj.addarmor : false;
if (anArmorAdd) processAddArmour(addIt, anArmorAdd);
if (uObj.shieldAdd) processAddShield(addIt, uObj.shieldAdd, uObj.weight);
if (uObj.ammoAdd) processAddAmmo(addIt, uObj.ammoAdd);
// --- backwards compatibility --- //
// skills additions
var skillsTxt = uObj.skillstxt ? uObj.skillstxt : uObj.skills && type == "feat" && !isArray(uObj.skills) ? uObj.skills : false;
if (skillsTxt) skillsTxt = skillsTxt.replace(/^[\r\n]{2,}.+: ?|[;.]$/g, '');
var skills = uObj.skills && (type != "feat" || (type == "feat" && isArray(uObj.skills))) ? uObj.skills : false;
if (skills || skillsTxt) processSkills(addIt, tipNmF, skills, skillsTxt);
// companion additions
if (uObj.creaturesAdd) processAddCompanions(addIt, tipNmF, uObj.creaturesAdd);
// magic item additions
if (uObj.magicitemsAdd) processAddMagicItems(addIt, uObj.magicitemsAdd);
// options for other class extrachoices
if (uObj.bonusClassExtrachoices) processBonusClassExtraChoices(addIt, type, uObj.bonusClassExtrachoices);
if (!addIt) addListOptions(); // remove weapon/armour/ammo/creature option(s)
};
// set the main variables, determined by type
switch (GetFeatureType(type)) {
case "classes":
type = "class";
aParent = fObjName[0];
fObjName = fObjName[1];
var useClObj = forceNonCurrent && ClassList[forceNonCurrent] && ClassList[forceNonCurrent].features[fObjName] ? ClassList[forceNonCurrent] :
forceNonCurrent && ClassSubList[forceNonCurrent] && ClassSubList[forceNonCurrent].features[fObjName] ? ClassSubList[forceNonCurrent] :
CurrentClasses[aParent];
var fObj = useClObj.features[fObjName];
var displName = fObjName.indexOf("subclassfeature") == -1 ? useClObj.name : useClObj.fullname ? useClObj.fullname : forceNonCurrent && useClObj.subname ? useClObj.subname : useClObj.name;
// --- backwards compatibility --- //
// also create some variables that (old) eval scripts tend to use
var oldClassLvl = {}; oldClassLvl[aParent] = lvlA[0];
var newClassLvl = {}; newClassLvl[aParent] = lvlA[1];
var ClassLevelUp = {}; ClassLevelUp[aParent] = [lvlA[1] >= lvlA[0], lvlL, lvlH];
break;
case "race":
type = "race";
aParent = fObjName[0];
fObjName = fObjName[1];
var fObj = aParent == fObjName && !CurrentRace.features[fObjName] ?
(forceNonCurrent ? RaceList[forceNonCurrent] : CurrentRace) :
forceNonCurrent && RaceList[forceNonCurrent] && RaceList[forceNonCurrent].features[fObjName] ?
RaceList[forceNonCurrent].features[fObjName] : CurrentRace.features[fObjName];
var displName = CurrentRace.name;
break;
case "background":
type = "background";
var fObj = forceNonCurrent && BackgroundList[fObjName] ? BackgroundList[fObjName] : CurrentBackground;
var displName = fObj.name;
tipNmExtra = "(background)";
break;
case "background feature":
type = "background feature";
var fObj = BackgroundFeatureList[fObjName];
var displName = fObjName.capitalize();
tipNmExtra = "(background feature)";
break;
case "feats":
type = "feat";
var fObj = FeatsList[fObjName];
var displName = fObj.name;
tipNmExtra = "(feat)";
break;
case "items":
type = "magic item";
var fObj = MagicItemsList[fObjName];
var displName = fObj.name;
tipNmExtra = "(magic item)";
break;
};
if (!fObj) {
console.println("The '" + fObjName + (aParent ? "' of the '" + aParent : "") + "' " + type + " could not be found! Please contact the author of the feature to correct this issue.");
console.show();
return false;
};
if (fObj.minlevel && fObj.minlevel > lvlH) return false; // no reason to continue with this function
// Are we to do anything with the feature?
var CheckLVL = lvlA[2] ? true : fObj.minlevel ? fObj.minlevel <= lvlH && fObj.minlevel > lvlL : lvlL == 0;
// Add (true) or remove (false) the feature's attributes?
var AddFea = fObj.minlevel ? fObj.minlevel <= lvlA[1] : 0 < lvlA[1];
// Get the choice, if any choices exist, it was selected in the past, and not entered into this function
if (!choiceA[1] && !choiceA[2] && fObj.choices) {
choiceA[1] = GetFeatureChoice(type, aParent, aParent !== fObjName ? fObjName : "", false);
if (choiceA[1] && !choiceA[0]) choiceA[0] = choiceA[1];
}
// --- backwards compatibility --- //
// First do the eval attribute of the main object, as it might change things for the choice
var skipMainEval = false;
if (fObj.choices && !choiceA[2] && CheckLVL && AddFea && fObj.eval && (typeof fObj.eval == "string") && (/Fea(Old)?Choice/).test(fObj.eval)) {
runEval(fObj.eval, "eval");
skipMainEval = true;
// redo the choice array, as the eval might have changed it
if (FeaOldChoice) choiceA[0] = FeaOldChoice;
if (FeaChoice) choiceA[1] = FeaChoice;
}
// Select a default choice, if applicable
if (fObj.choices && !choiceA[0] && !choiceA[1] && !choiceA[2] && CheckLVL && AddFea && fObj.defaultChoice && fObj[fObj.defaultChoice]) {
choiceA[1] = fObj.defaultChoice.toLowerCase();
}
// set the choice objects, if any
var cOldObj = choiceA[0] && fObj[choiceA[0]] ? fObj[choiceA[0]] : false;
var cNewObj = choiceA[1] && fObj[choiceA[1]] ? fObj[choiceA[1]] : false;
var cJustChange = (/change|update/).test(choiceA[2]) && cOldObj && cNewObj && choiceA[0] != choiceA[1];
var cOnly = ((AddFea && cNewObj) || (!AddFea && cOldObj)) && (/only/).test(choiceA[2]);
// Now if there was a choice, and this is a feat or an item, check for duplicates
var cnt = 0, cntCh = 0;
if (type === "feat" || type === "magic item") {
var checkObj = type === "feat" ? CurrentFeats : CurrentMagicItems;
for (var i = 0; i < checkObj.known.length; i++) {
if (checkObj.known[i] == fObjName) {
cnt++;
if ((choiceA[0] && checkObj.choices[i] == choiceA[0]) || (choiceA[1] && checkObj.choices[i] == choiceA[1])) cntCh++;
}
}
}
// get the level-dependent attributes for the current and old levels
var Fea = GetLevelFeatures(fObj, lvlA[1], cNewObj ? choiceA[1] : false, lvlA[0], cOldObj ? choiceA[0] : cOnly ? choiceA[1] : false, cOnly);
// add some of the current variables to this object, so it is given in the return
Fea.CheckLVL = CheckLVL;
Fea.AddFea = AddFea;
Fea.Choice = choiceA[1];
Fea.ChoiceOld = choiceA[0];
// First do the level-dependent limited feature, if any of them changed or we are supposed to add/remove them
if ((CheckLVL || Fea.changed) && (Fea.UseOld || Fea.UseCalcOld || Fea.Use || Fea.UseCalc)) {
// remove the limited feature entry if it is no longer applicable
if (lvlA[0] && (!AddFea || ((Fea.UseOld || Fea.UseCalcOld) && (Fea.UseName !== Fea.UseNameOld || (!Fea.Use && !Fea.UseCalc) || (/unlimited|\u221E/i).test(Fea.Use))))) {
RemoveFeature(Fea.UseNameOld ? Fea.UseNameOld : Fea.UseName, lvlA[1] === 0 && !fObj.limfeaAddToExisting ? "" : Fea.UseOld, "", "", tooltipName, "", Fea.UseCalcOld);
Fea.UseOld = 0;
}
// add the limited feature entry if it changed or added for the first time
if (AddFea && (Fea.UseCalc || Fea.Use) && !(/unlimited|\u221E/i).test(Fea.Use)) {
var tooltipName = choiceLimFeaTooltip ? choiceLimFeaTooltip : displName + (fObj.tooltip ? fObj.tooltip : displName !== fObj.name ? ": " + fObj.name : "");
var oldUsages = !Fea.UseOld && fObj.limfeaAddToExisting ? "bonus" : Fea.UseOld;
AddFeature(Fea.UseName, Fea.Use, Fea.Add ? " (" + Fea.Add + ")" : "", Fea.Recov, tooltipName, oldUsages, Fea.UseCalc, Fea.AltRecov);
}
}
// Then do all the level-independent attributes, only if this is mandated by the level change
if (CheckLVL) {
// do the main object if not only interested in the choice, but without the eval as we just did that already
if (!choiceA[2]) useAttr(fObj, AddFea, skipMainEval);
// if we are are changing the choice or removing the feature, now remove the old choice
//if (cJustChange || (!AddFea && cOldObj)) {
if (cOldObj && (cJustChange || !AddFea)) {
SetFeatureChoice(type, aParent, aParent !== fObjName ? fObjName : "", false, cOnly ? choiceA[0] : "");
useAttr(cOldObj, false, false, choiceA[0]);
}
// if we are changing the choice or adding the feature, now add the new choice
//if (cJustChange || cOnly || (AddFea && cNewObj)) {
if (cNewObj && AddFea) {
SetFeatureChoice(type, aParent, aParent !== fObjName ? fObjName : "", AddFea ? choiceA[1] : "", cOnly ? choiceA[1] : "");
useAttr(cNewObj, true, false, choiceA[1]);
}
}
// changeeval always at the end and regardless of AddFea or CheckLVL
if (!cOnly && fObj.changeeval) runEval(fObj.changeeval, 'changeeval');
if (cOldObj && cOldObj.changeeval) runEval(cOldObj.changeeval, 'changeeval');
if (cNewObj && cNewObj.changeeval) runEval(cNewObj.changeeval, 'changeeval');
// if this is a class feature (and not doing an extrachoice), always check if we need to update the dependencies
if (type == "class" && !cOnly) {
if (choiceA[1] && fObj.choiceDependencies) processClassFeatureChoiceDependencies(lvlA, aParent, fObjName, choiceA[1]);
if (fObj.autoSelectExtrachoices) processClassFeatureExtraChoiceDependencies(lvlA, aParent, fObjName, fObj);
if (fObj.choiceSetsExtrachoices) applyExtrachoicesOfChoice(aParent, fObjName, choiceA, false);
}
// return the level-dependent attributes so it doesn't have to be queried again
return Fea;
}
// a function to apply the first-level attributes of a class object
// AddRemove - can be boolean (true = add all feature, false = remove all features)
// or can be an Array of [oldsubclass, newsubclass]
function ApplyClassBaseAttributes(AddRemove, aClass, primaryClass) {
// declare some variables
var parentCl = ClassList[aClass];
var oldSubCl = ClassSubList[AddRemove[0]];
var newSubCl = ClassSubList[AddRemove[1]];
var n = primaryClass ? 0 : 1; // for backwards compatibility
var nAttr = primaryClass ? "primary" : "secondary";
// a way to see if we should process the attribute or not
var checkIfIn = function(nObj, inclObj, attrA, noN, exclObj) {
if (!attrA[1]) attrA[1] = "nonExistentAttributeName";
if (!nObj[attrA[0]] && !nObj[attrA[1]]) {
// if the first object doesn't have either attribute, just stop
return [false];
}
var useAttr = nObj[attrA[0]] ? attrA[0] : attrA[1];
var exclAttr = exclObj && exclObj[attrA[0]] ? attrA[0] : attrA[1];
var subAttr = noN ? 0 : isArray(nObj[useAttr]) ? n : nAttr;
if (
(!noN && !nObj[useAttr][subAttr]) || // the first object has an attribute, but not the right sub-attribute, so stop
( exclObj && exclObj[exclAttr] && ( noN || (!noN && exclObj[exclAttr][subAttr]) ) ) // stop if the attribute (+ right sub-attribute) exists in the excluding object
) {
return [false];
} else if (!inclObj) {
// there is no object to test against, so continue with the first object
return [true, useAttr, subAttr];
}
var useAttr2 = inclObj[attrA[0]] ? attrA[0] : inclObj[attrA[1]] ? attrA[1] : false;
return !useAttr2 || !inclObj[useAttr2][noN ? 0 : isArray(inclObj[useAttr2]) ? n : nAttr] ? [false] : [true, useAttr, subAttr];
}
// loop through the attributes and apply them
var processAttributes = function (uObj, addIt, tipNmF, ifInObj, ifNotInObj) {
// saves, if primary class
if (primaryClass && checkIfIn(uObj, ifInObj, ['saves'], true)[0]) processSaves(addIt, tipNmF, uObj.saves);
// skills
var doSkills = checkIfIn(uObj, ifInObj, ['skills', 'skillstxt'], false, ifNotInObj);
if (doSkills[0]) {
var oSkills = false;
var oSkillsTxt = false;
if (doSkills[1] === "skillstxt") {
// no 'skills' attribute, only 'skillstxt'
oSkillsTxt = uObj.skillstxt[doSkills[2]];
} else if (doSkills[1] === "skills" && uObj.skillstxt) {
// both 'skills' and 'skillstxt' attributes
oSkills = uObj.skills[doSkills[2]];
oSkillsTxt = isArray(uObj.skillstxt) ? uObj.skillstxt[n] : uObj.skillstxt[nAttr];
} else if (doSkills[2] == n && !isArray(uObj.skills[n]) && SkillsList.abbreviations.indexOf(uObj.skills[n]) == -1 && SkillsList.names.indexOf(uObj.skills[n]) == -1) {
// --- backwards compatibility --- //
// the class has skillstxt as skills attribute (pre v13)
oSkillsTxt = uObj.skills[n].replace(/^( |\n)*.*: |\;$|\.$/g, '');
} else {
// no 'skillstxt' attribute, only 'skills'
oSkills = uObj.skills[doSkills[2]];
}
processSkills(addIt, tipNmF, oSkills, oSkillsTxt);
}
// weapon proficiencies ('weapons' attribute for backwards compatibility)
var doWeapons = checkIfIn(uObj, ifInObj, ['weaponProfs', 'weapons'], false, ifNotInObj);
if (doWeapons[0]) processWeaponProfs(addIt, tipNmF, uObj[doWeapons[1]][doWeapons[2]]);
// armour proficiencies ('armor' attribute for backwards compatibility)
var doArmour = checkIfIn(uObj, ifInObj, ['armorProfs', 'armor'], false, ifNotInObj);
if (doArmour[0]) processArmourProfs(addIt, tipNmF, uObj[doArmour[1]][doArmour[2]]);
// tool proficiencies
var doTools = checkIfIn(uObj, ifInObj, ['toolProfs'], false, ifNotInObj);
if (doTools[0]) processTools(addIt, tipNmF, uObj.toolProfs[doTools[2]]);
// spellcasting extra array
if (CurrentSpells[aClass] && checkIfIn(uObj, ifInObj, ['spellcastingExtra'], true, ifNotInObj)[0]) {
processSpellcastingExtra(addIt, aClass, 0, "", uObj.spellcastingExtra, uObj.spellcastingExtraApplyNonconform);
}
}
if (!isArray(AddRemove)) { // do the class (and subclass) attributes in full
var newSubCl = classes.known[aClass].subclass ? ClassSubList[classes.known[aClass].subclass] : false;
// do the AddRemove for the class attributes that are not in the subclass
processAttributes(parentCl, AddRemove, parentCl.name, false, newSubCl);
// do the AddRemove for the whole subclass
if (newSubCl) processAttributes(newSubCl, AddRemove, newSubCl.subname);
} else if (!AddRemove[0] && AddRemove[1]) { // adding a subclass while previously none was there
// first remove everything that is in the base class and also in the subclass
processAttributes(parentCl, false, parentCl.name, newSubCl);
// then add everything from the subclass
processAttributes(newSubCl, true, newSubCl.subname);
} else if (AddRemove[0] && !AddRemove[1]) { // removing a subclass, going back to just the class
// first remove everything that is in the subclass
processAttributes(oldSubCl, false, oldSubCl.subname);
// then add everything from the class that is also in the subclass
processAttributes(parentCl, true, parentCl.name, oldSubCl);
} else if (AddRemove[0] && AddRemove[1]) { // changing subclasses
// first remove everything that is in old subclass
processAttributes(oldSubCl, false, oldSubCl.subname);
// then add everything from class that is also in old subclass, but not in new subclass
processAttributes(parentCl, true, parentCl.name, oldSubCl, newSubCl);
// next remove everything that is in class and also in new subclass, but not in old subclass
processAttributes(parentCl, false, parentCl.name, newSubCl, oldSubCl);
// lastly add everything from new subclass
processAttributes(newSubCl, true, newSubCl.subname);
}
}
// a function to set the choice for something (choice = objectname) or remove it (choice = false)
// put the objectname in extra for extrachoices (both when adding and when removing)
function SetFeatureChoice(type, objNm, feaNm, choice, extra) {
choice = choice ? choice.toLowerCase() : false;
extra = extra ? extra.toLowerCase() : false;
type = GetFeatureType(type);
if (type === "items" || type === "feats") return;
if (!CurrentFeatureChoices[type]) CurrentFeatureChoices[type] = {};
if (!choice) { // remove the choice
if (!CurrentFeatureChoices[type][objNm]) return;
var lookin = feaNm ? CurrentFeatureChoices[type][objNm][feaNm] : CurrentFeatureChoices[type][objNm];
if (!lookin) return;
if (extra) {
if (lookin.extrachoices) {
lookin.extrachoices.splice(lookin.extrachoices.indexOf(extra), 1);
if (lookin.extrachoices.length == 0) delete lookin.extrachoices;
}
} else if (lookin.choice) {
delete lookin.choice;
}
CurrentFeatureChoices = CleanObject(CurrentFeatureChoices); // remove any remaining empty objects
} else { // add the choice
if (!CurrentFeatureChoices[type][objNm]) CurrentFeatureChoices[type][objNm] = {};
if (feaNm && !CurrentFeatureChoices[type][objNm][feaNm]) CurrentFeatureChoices[type][objNm][feaNm] = {};
var touse = feaNm ? CurrentFeatureChoices[type][objNm][feaNm] : CurrentFeatureChoices[type][objNm];
if (extra) {
if (!touse.extrachoices) touse.extrachoices = [];
if (touse.extrachoices.indexOf(extra) == -1) touse.extrachoices.push(extra);
} else {
touse.choice = choice;
}
}
SetStringifieds("choices");
}
// a function to return the feature choice(s); if extra==true, returns array
function GetFeatureChoice(type, objNm, feaNm, extra) {
var theReturn = extra ? [] : "";
type = GetFeatureType(type);
if (CurrentFeatureChoices[type] && CurrentFeatureChoices[type][objNm] && (!feaNm || CurrentFeatureChoices[type][objNm][feaNm])) {
var useObj = feaNm ? CurrentFeatureChoices[type][objNm][feaNm] : CurrentFeatureChoices[type][objNm];
var foundSel = extra ? useObj.extrachoices : useObj.choice;
if (foundSel) theReturn = foundSel.slice();
}
return theReturn;
}
// save bonus extrachoices for (other) class features
function processBonusClassExtraChoices(bAddRemove, sType, aItems) {
if (!isArray(aItems)) aItems = [aItems];
for (var i = 0; i < aItems.length; i++) {
var oItem = aItems[i];
var sClass = oItem["class"];
var sFea = oItem["feature"];
var iBonus = oItem["bonus"];
var sSubclass = oItem["subclass"] ? oItem["subclass"] : "mainClass";
if (!sClass || !sFea || !iBonus || isNaN(iBonus) || iBonus < 0 || sSubclass === undefined) continue;
if (bAddRemove) {
// add, but not if it already exists and we're importing
try {
if (!IsNotImport && CurrentFeatureChoices.bonus[sClass][sSubclass][sFea]) continue;
} catch(e) {};
if (!CurrentFeatureChoices.bonus) CurrentFeatureChoices.bonus = {};
if (!CurrentFeatureChoices.bonus[sClass]) CurrentFeatureChoices.bonus[sClass] = {};
if (!CurrentFeatureChoices.bonus[sClass][sSubclass]) CurrentFeatureChoices.bonus[sClass][sSubclass] = {};
if (!CurrentFeatureChoices.bonus[sClass][sSubclass][sFea]) CurrentFeatureChoices.bonus[sClass][sSubclass][sFea] = 0;
CurrentFeatureChoices.bonus[sClass][sSubclass][sFea] += iBonus;
} else if (CurrentFeatureChoices.bonus && CurrentFeatureChoices.bonus[sClass] && CurrentFeatureChoices.bonus[sClass][sSubclass] && CurrentFeatureChoices.bonus[sClass][sSubclass][sFea]) {
// remove
CurrentFeatureChoices.bonus[sClass][sSubclass][sFea] -= iBonus;
if (CurrentFeatureChoices.bonus[sClass][sSubclass][sFea] <= 0) delete CurrentFeatureChoices.bonus[sClass][sSubclass][sFea];
CurrentFeatureChoices = CleanObject(CurrentFeatureChoices); // remove any remaining empty objects
}
}
SetStringifieds("choices");
// not a class feature, so set the visibility for the class menu
if (GetFeatureType(sType) !== "classes") ClassMenuVisibility();
}
// get the number of extra choices for a specific class feature
function getBonusClassExtraChoiceNr(sClass, sFeaNm) {
var iReturn = 0;
if (CurrentFeatureChoices.bonus && CurrentFeatureChoices.bonus[sClass]) {
var oThis = CurrentFeatureChoices.bonus[sClass];
if (oThis.mainClass && oThis.mainClass[sFeaNm]) iReturn += oThis.mainClass[sFeaNm];
var sCurSubclass = classes.known[sClass] && classes.known[sClass].subclass ? classes.known[sClass].subclass : false;
if (oThis[sCurSubclass] && oThis[sCurSubclass][sFeaNm]) iReturn += oThis[sCurSubclass][sFeaNm];
}
return iReturn;
}
// return an object with bonus class features that are not normally accessible
function getBonusClassExtraChoices(bIgnoreNotInMenu) {
var aReturn = [];
if (!CurrentFeatureChoices.bonus) return aReturn;
for (var sClass in CurrentFeatureChoices.bonus) {
var oClassList = ClassList[sClass];
if (!oClassList) continue; // class doesn't exist
var oClass = CurrentFeatureChoices.bonus[sClass];
var iClassLvl = classes.known[sClass] && classes.known[sClass].level ? classes.known[sClass].level : 0;
var sCurSubclass = classes.known[sClass] && classes.known[sClass].subclass ? classes.known[sClass].subclass : false;
for (var sSubclass in oClass) {
var bIsMainClass = sSubclass === "mainClass";
if (!bIsMainClass && !ClassSubList[sSubclass]) continue; // subclass doesn't exist
var oSubclass = oClass[sSubclass];
var bTestLvl = iClassLvl && (bIsMainClass || sSubclass === sCurSubclass) && CurrentClasses[sClass];
var oUseObj = bTestLvl ? CurrentClasses[sClass].features : bIsMainClass ? oClassList.features : ClassSubList[sSubclass].features;
for (var sFeaNm in oSubclass) {
// see if the feature exists and if it isn't already available for the character or if the features are not meant to be displayed in the menu
if (!oUseObj[sFeaNm] || !oUseObj[sFeaNm].extrachoices || (!bIgnoreNotInMenu && oUseObj[sFeaNm].extrachoicesNotInMenu) || (bTestLvl && oUseObj[sFeaNm].minlevel <= iClassLvl)) continue;
// feature is not available for the character, so return its object
aReturn.push({
"class" : sClass,
"subclass" : bIsMainClass ? false : sSubclass,
"feature" : sFeaNm,
"bonus" : oSubclass[sFeaNm]
});
}
}
}
return aReturn;
}
// a function to get all currently selected fighting styles
function GetFightingStyleSelection() {
var fndObj = {};
if (CurrentFeatureChoices.classes) {
for (var aClass in CurrentFeatureChoices.classes) {
var clObj = CurrentFeatureChoices.classes[aClass];
var clNm = !CurrentClasses[aClass] ? "Unknown" : CurrentClasses[aClass].fullname.indexOf(CurrentClasses[aClass].name) == -1 ? CurrentClasses[aClass].fullname : CurrentClasses[aClass].name;
for (var aFea in clObj) {
var feaObj = clObj[aFea];
var feaNm = CurrentClasses[aClass] && CurrentClasses[aClass].features[aFea] ? CurrentClasses[aClass].features[aFea].name : aFea.capitalize();
if (typeof feaObj == "object" && feaObj.choice && (/fighting style/i).test(aFea + feaNm)) fndObj[feaObj.choice] = ["classes", aClass, "\t (selected: " + clNm + " - " + feaNm + ")"];
}
}
}
for (var i = 0; i < CurrentFeats.known.length; i++) {
var sFeat = CurrentFeats.known[i];
var oFeat = FeatsList[sFeat];
if (!sFeat || !oFeat || !(/fighting style/i).test(oFeat.descriptionFull)) continue;
var sFeatChoice = CurrentFeats.choices[CurrentFeats.known.indexOf(sFeat)];
if (sFeatChoice) fndObj[sFeatChoice] = ["feats", sFeat, "\t (selected: " + oFeat.name + " - " + sFeatChoice.capitalize() + ")"];
};
return fndObj;
}
// a function to get a string of class feature choices just like how they use to be prior to v13 with the "Class Feature Remember" field
function classFeaChoiceBackwardsComp() {
var chc = CurrentFeatureChoices.classes;
if (!chc) return "";
var returnStr = "";
for (var aClass in chc) {
for (var aFea in chc[aClass]) {
var fea = chc[aClass][aFea];
if (fea.choice) returnStr += [aClass, aFea, fea.choice].toString();
}
}
return returnStr;
}
// a function to return the spellcasting ability, ask the user which to use if it is an array
function ReturnSpellcastingAbility(sCast, vAbility, bAbilitySave) {
if (vAbility === undefined || (!isArray(vAbility) && isNaN(vAbility) && bAbilitySave)) return 0;
if (!isArray(vAbility)) return !isNaN(vAbility) || /^(class|race)$/i.test(vAbility) ? vAbility : 0;
var sCastName = CurrentSpells[sCast] ? CurrentSpells[sCast].name : sCast;
var aAbiOptions = [], oAbiRef = {};
vAbility.sort();
for (var i = 0; i < vAbility.length; i++) {
if (isNaN(vAbility[i]) && !/^(class|race)$/i.test(vAbility[i])) continue;
var sAbiTxt = !isNaN(vAbility[i]) ? AbilityScores.names[vAbility[i] - 1] : vAbility[i].toLowerCase() === "race" ? "Race: the same as the racial spellcasting ability score." : "Class: the same as the highest class spellcasting ability score.";
if (aAbiOptions.indexOf(sAbiTxt) === -1) {
aAbiOptions.push(sAbiTxt);
oAbiRef[sAbiTxt] = vAbility[i];
}
}
if (aAbiOptions.length < 2) return aAbiOptions.length ? aAbiOptions[0] : 0;
var sType = bAbilitySave ? "Ability Save DC" : "Spellcasting Ability";
var sTypeLC = sType.replace("A", "a").replace("S", "s");
var sTypePage = bAbilitySave ? "first page" : "spell sheet once you generate the spell sheet";
var sAsk = AskUserOptions("Select " + sType + " for " + sCastName, "The " + sTypeLC + " for " + sCastName + " can be one of multiple options. It is up to you to select which the sheet will use from now on.", aAbiOptions, "radio", true, 'You can always change the ' + sTypeLC + ' for ' + sCastName + ' on the ' + sTypePage + '. What you select here is the "default" ' + sTypeLC + '.');
if (oAbiRef[sAsk]) {
return oAbiRef[sAsk];
} else {
return oAbiRef[aAbiOptions[0]];
}
}
// a function to create the CurrentSpells global variable entry
function CreateCurrentSpellsEntry(type, fObjName, aChoice, forceNonCurrent) {
type = GetFeatureType(type);
var sTypeSingular = GetFeatureType(type, false, true);
var fObjP = false;
var setCSobj = function(oName) {
if (!CurrentSpells[oName]) {
CurrentSpells[oName] = {bonus : {}};
CurrentUpdates.types.push("spells");
}
return CurrentSpells[oName];
};
switch (type.toLowerCase()) {
case "classes":
var fObj = forceNonCurrent && !CurrentClasses[fObjName] ? ClassList[fObjName] : CurrentClasses[fObjName];
if (!fObj) return false;
var aClass = classes.known[fObjName].name;
var aSubClass = classes.known[fObjName].subclass;
var sObj = setCSobj(fObjName);
sObj.shortname = ClassList[aClass].spellcastingFactor || !aSubClass ? ClassList[aClass].name : ClassSubList[aSubClass].fullname ? ClassSubList[aSubClass].fullname : ClassSubList[aSubClass].subname;
sObj.level = classes.known[fObjName].level;
sObj.name = !forceNonCurrent ? fObj.fullname : !aSubClass ? sObj.shortname : ClassSubList[aSubClass].fullname ? ClassSubList[aSubClass].fullname : ClassList[aClass].name + " (" + ClassSubList[aSubClass].subname + ")";
if (sObj.typeSp == undefined) sObj.typeSp = "";
sObj.refType = sTypeSingular;
break;
case "race":
var fObj = CurrentRace;
var sObj = setCSobj(fObjName);
sObj.name = fObj.name;
sObj.typeSp = sTypeSingular;
sObj.level = fObj.level;
sObj.refType = sTypeSingular;
break;
case "feats":
var fObj = FeatsList[fObjName];
if (aChoice && fObj[aChoice]) {
fObj = FeatsList[fObjName][aChoice];
fObjP = FeatsList[fObjName];
fObjName += "_-_" + aChoice;
}
var sObj = setCSobj(fObjName);
sObj.name = fObj.name + " (feat)";
sObj.typeSp = sTypeSingular;
sObj.refType = sTypeSingular;
break;
case "items":
var fObj = MagicItemsList[fObjName];
if (aChoice && fObj[aChoice]) {
fObj = MagicItemsList[fObjName][aChoice];
fObjP = MagicItemsList[fObjName];
fObjName += "_-_" + aChoice;
}
var sObj = setCSobj(fObjName);
sObj.name = MagicItemGetShortestName(fObj) + " (item)";
sObj.typeSp = sTypeSingular;
sObj.refType = sTypeSingular;
break;
default:
return false;
};
if (aChoice && (type == "items" || type == "feats") && !fObj.name && fObjP && fObjP.choices) {
for (var j = 0; j < fObjP.choices.length; j++) {
if (fObjP.choices[j].toLowerCase() == aChoice) {
sObj.name = fObjP.name + " [" + fObjP.choices[j] + "]" + " (" + sObj.typeSp+ ")";
break;
}
}
}
if (!sObj.ability) sObj.ability = ReturnSpellcastingAbility(fObjName, fObj.spellcastingAbility ? fObj.spellcastingAbility : fObj.abilitySave ? fObj.abilitySave : 0);
if (!sObj.fixedDC && fObj.fixedDC) sObj.fixedDC = Number(fObj.fixedDC);
if (!sObj.fixedSpAttack && fObj.fixedSpAttack) sObj.fixedSpAttack = Number(fObj.fixedSpAttack);
if (sObj.allowUpCasting === undefined && fObj.allowUpCasting !== undefined) sObj.allowUpCasting = fObj.allowUpCasting;
if (sObj.magicItemComponents === undefined && fObj.magicItemComponents !== undefined) sObj.magicItemComponents = fObj.magicItemComponents;
if (fObjP) {
if (!sObj.ability) sObj.ability = ReturnSpellcastingAbility(fObjName, fObjP.spellcastingAbility ? fObjP.spellcastingAbility : fObjP.abilitySave ? fObjP.abilitySave : 0);
if (!sObj.fixedDC && fObjP.fixedDC) sObj.fixedDC = Number(fObjP.fixedDC);
if (!sObj.fixedSpAttack && fObjP.fixedSpAttack) sObj.fixedSpAttack = Number(fObjP.fixedSpAttack);
if (sObj.allowUpCasting === undefined && fObjP.allowUpCasting !== undefined) sObj.allowUpCasting = fObjP.allowUpCasting;
if (sObj.magicItemComponents === undefined && fObjP.magicItemComponents !== undefined) sObj.magicItemComponents = fObjP.magicItemComponents;
}
if (!sObj.abilityToUse) sObj.abilityToUse = getSpellcastingAbility(fObjName);
return sObj;
}
// process a spellcastingBonus feature
function processSpBonus(AddRemove, srcNm, spBon, type, parentName, choice, forceNonCurrent, forceUseSpName) {
type = GetFeatureType(type);
var useSpName = forceUseSpName ? forceUseSpName : choice && (type === "feats" || type === "items") ? parentName + "_-_" + choice : parentName;
var sObj = CurrentSpells[useSpName];
if (!AddRemove && !sObj) return; // nothing to remove
// do something with the spellcastingBonus object
if (!AddRemove) { // removing the entry
delete sObj.bonus[srcNm];
// now see if the bonus object is empty and if so, delete the whole entry
if (!sObj.factor && !sObj.list && ObjLength(sObj.bonus) == 0) delete CurrentSpells[useSpName];
} else { // adding the entry
// create the spellcasting object if it doesn't yet exist
if (!CurrentSpells[useSpName]) sObj = CreateCurrentSpellsEntry(type, parentName, choice, forceNonCurrent);
if (!sObj) return; // failed to create CurrentSpells entry, so stop now
if (!isArray(spBon)) spBon = [spBon];
sObj.bonus[srcNm] = spBon;
// see if this wants to change the spellcasting ability
var spFeatItemLvl, spAbility, spFixedDC, spFixedSpAttack, spAllowUpCasting, spMagicItemComponents;
for (var i = 0; i < spBon.length; i++) {
if (!spFeatItemLvl && spBon[i].times && isArray(spBon[i].times)) spFeatItemLvl = true;
if (spBon[i].spellcastingAbility) spAbility = spBon[i].spellcastingAbility;
if (spBon[i].fixedDC) spFixedDC = spBon[i].fixedDC;
if (spBon[i].fixedSpAttack) spFixedSpAttack = spBon[i].fixedSpAttack;
if (spBon[i].allowUpCasting !== undefined) spAllowUpCasting = spBon[i].allowUpCasting;
if (spBon[i].magicItemComponents !== undefined) spMagicItemComponents = spBon[i].magicItemComponents;
}
if (spAbility) {
sObj.ability = ReturnSpellcastingAbility(useSpName, spAbility);
sObj.abilityToUse = getSpellcastingAbility(useSpName);
}
if (spFixedDC) sObj.fixedDC = spFixedDC;
if (spFixedSpAttack) sObj.fixedSpAttack = spFixedSpAttack;
if (spAllowUpCasting !== undefined) sObj.allowUpCasting = spAllowUpCasting;
if (spMagicItemComponents !== undefined) sObj.magicItemComponents = spMagicItemComponents;
// if concerning a feat or item, set the level only if the spellcastingBonus needs it
if (/feat|item/i.test(sObj.typeSp) && spFeatItemLvl) sObj.level = Math.max(Number(What("Character Level")), 1);
}
SetStringifieds('spells');
CurrentUpdates.types.push("spells");
}
// process the spellChanges attribute
function processSpChanges(AddRemove, srcNm, spChng, spObjName) {
var spCast = CurrentSpells[spObjName];
var changeHead = "Changes by " + srcNm;
if (!spCast || (!AddRemove && !spCast.spellAttrOverride)) return; // nothing to do
if (AddRemove) { // adding
if (!spCast.spellAttrOverride) spCast.spellAttrOverride = {};
for (var aSpell in spChng) {
if (!spCast.spellAttrOverride[aSpell]) spCast.spellAttrOverride[aSpell] = { changesObj : {} };
var spObj = spCast.spellAttrOverride[aSpell];
if (spChng[aSpell].changes) spObj.changesObj[changeHead] = "\n \u2022 " + spChng[aSpell].changes;
for (var key in spChng[aSpell]) {
if (key == "changes") continue;
spObj[key] = spChng[aSpell][key];
}
}
} else { // removing
for (var aSpell in spChng) {
var spObj = spCast.spellAttrOverride[aSpell];
if (!spObj || !spObj.changesObj[changeHead]) continue;
for (var key in spChng[aSpell]) {
if (key == "changes") continue;
delete spObj[key];
}
delete spObj.changesObj[changeHead];
// now maybe delete this spellAttrOverride entry if its changesObj is empty
if (!ObjLength(spObj.changesObj)) delete spCast.spellAttrOverride[aSpell];
}
// now maybe delete the whole spellAttrOverride if it is empty
if (!ObjLength(spCast.spellAttrOverride)) delete spCast.spellAttrOverride;
}
SetStringifieds('spells');
CurrentUpdates.types.push("spells");
}
// process the spellcastingExtra and spellcastingExtraApplyNonconform attributes
function processSpellcastingExtra(AddRemove, spObjName, lvl, name, spExtra, spNonconform) {
var aCast = CurrentSpells[spObjName];
if ( !aCast || (!spExtra && spNonconform === undefined) ) return;
// --- backwards compatibility --- //
var hasNonconform = spNonconform !== undefined ? spNonconform : spExtra && spExtra[100] === "AddToKnown" ? true : undefined;
var objNm = ("0" + (lvl ? lvl : 0)).substr(-2) + (name ? name.toLowerCase() : "");
var arrDo = [
[spExtra, "extra"],
[hasNonconform, "extraSpecial"]
];
for (var i = 0; i < arrDo.length; i++) {
if (arrDo[i][0] === undefined) continue;
var toSet = arrDo[i][0];
var useNm = arrDo[i][1];
var remNm = arrDo[i][1] + "Rem";
if (!aCast[remNm]) aCast[remNm] = {};
if (AddRemove) {
// Add
aCast[remNm][objNm] = toSet;
} else if (aCast[remNm][objNm]) {
// Remove
delete aCast[remNm][objNm];
}
// get the highest entry (not the latest, but highest level)
var keysArrSort = Object.keys(aCast[remNm]).sort().reverse();
aCast[useNm] = aCast[remNm][keysArrSort[0]];
}
SetStringifieds('spells');
CurrentUpdates.types.push("spells");
}
// Allow something to add a bonus spell to another spellcasting feature (e.g. magic item adds spells to spellbook of the wizard)
/*
var a = {spellcastingBonusElsewhere : {
addTo : "wizard", // Exact CurrentSpells match (e.g. class/race name), partial match (e.g. subclass/feat/magic item name), or type "class(es)", "race", "feat", "magic item"
spellcastingBonus : [], // regular spellcastingBonus attribute
addToKnown : [], // array of SpellsList entries to add to known spells or spellbook
// addToKnown must adhere to the normal restrictions of the spell list available. if you want to go beyond that you'll also have to change the spell list with calcChanges.spellList
countsTowardsKnown : true // set to true if the spells added should be subtracted from the total spells known
}
}
*/
function processSpellcastingBonusElsewhere(bAddRemove, sType, sSrcNm, sUniqueSrcNm, oSpElse) {
if (!oSpElse.addTo || typeof oSpElse.addTo !== "string" || (!oSpElse.spellcastingBonus && !oSpElse.addToKnown)) return;
sType = GetFeatureType(sType);
oSpElse.addTo = oSpElse.addTo.toLowerCase();
// First find which CurrentSpells object we should add this to
var sSpMain = oSpElse.addTo;
if (bAddRemove) {
// When adding the
if (!CurrentSpells[sSpMain]) {
// Not a perfect match for an existing CurrentSpells entry name
var aMatches = [];
// See if it is defined as a broad type (e.g. "class", "race", "feat")
var sTypeAddTo = GetFeatureType(oSpElse.addTo, true, true);
// Otherwise we'll test against the (object) name of the CurrentSpells entry
var rxAddTo = RegExp(oSpElse.addTo, "i");
// Iterate through the CurrentSpells entries to see which match
for (var sCast in CurrentSpells) {
var oCast = CurrentSpells[sCast];
// Test if the type or the name are a match
// And even if it is a match, ignore if doing `addToKnown`, as that will only work for spellcasting with known spells or a spellbook (e.g. not only bonus spells)
if ((oCast.refType === sTypeAddTo || (!sTypeAddTo && rxAddTo.test(sCast + oCast.name))) && (!oSpElse.addToKnown || oCast.typeSp !== sTypeAddTo)) {
aMatches.push(sCast);
}
}
if (aMatches.length === 1) {
// Only a single match, so use that one
sSpMain = aMatches[0];
} else if (aMatches.length > 1) {
// Multiple matches, ask the player which to use
var oRefCasts = {}, aCastNames = [];
for (var i = 0; i < aMatches.length; i++) {
var sCastName = CurrentSpells[aMatches[i]].name;
if (oSpElse.addToKnown && CurrentSpells[aMatches[i]].typeSp === "list") sCastName += " (will add only cantrips to known spells)";
aCastNames.push(sCastName);
oRefCasts[aMatches[i]] = sCastName;
}
var sUserSelect = AskUserOptions(
"Which spellcasting to add " + sSrcNm + " spells to",
'The spells gained from "' + sSrcNm + '" are meant to be automatically added to a "' + oSpElse.addTo + '" spellcasting entry. Several entries are a match, thus it is up to you to decide which of these to add the spells to.',
aCastNames, "radio", true,
"You can't change what you select here other than by removing " + sSrcNm + ", and then selecting it again.");
sSpMain = oRefCasts[sUserSelect];
}
}