-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpells.js
136 lines (125 loc) · 3.86 KB
/
Spells.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
var spells = require('./rules/spells.json');
exports.getSpell = function(name) {
for (var i = 0; i < spells.length; i++) {
if (name == spells[i].name) {
return spells[i];
}
}
console.log('Failed to find spell ' + name);
};
// gets the list of spells that should appear in a 'spellbook'
exports.getSpellsForSpellbook = function(c, playerClass) {
var r = [];
var maxLevel = exports.getMaxSpellLevel(c, playerClass);
for (var i = 0; i < spells.length; i++) {
var spell = spells[i];
if (spell.class.indexOf(c.class) != -1 && (spell.level == 'Cantrip' || parseInt(spell.level.substr(0,1)) <= maxLevel)) {
r.push(spell);
}
}
// add in any known spells on the character sheet if not already there
if (c.spells) {
for (var level in c.spells) {
var s = c.spells[level];
for (var i = 0; i < s.length; i++) {
var spell = exports.getSpell(s[i]);
if (!findSpell(r, spell)) {
r.push(spell);
}
}
}
}
// add in any spells from the archetype
if (c.archetype) {
var levelFeatures = playerClass.archetypes[c.archetype].levelFeatures;
for (var i = 0; i < levelFeatures.length; i++) {
var lf = levelFeatures[i];
if (lf.level <= c.level && lf.spells) {
for (var level in lf.spells) {
var sl = lf.spells[level];
for (var j = 0; j < sl.length; j++) {
r.push(exports.getSpell(sl[j]));
}
}
}
}
}
r.sort(function(a, b) {
if (a.name > b.name) {
return 1;
} else if (a.name < b.name) {
return -1;
}
return 0;
});
return r;
};
exports.getMaxSpellLevel = function(c, playerClass) {
var l = 0;
// the max level of spell that can be cast is the highest level you have for a spell slot
for (var i = 0; i < playerClass.levelFeatures.length; i++) {
var lf = playerClass.levelFeatures[i];
if (lf.level <= c.level) {
for (var name in lf.spellSlots) {
if (name != 'cantrips') {
l = Math.max(l, parseInt(name));
}
}
}
}
return l;
};
// gets the list of spells that can be cast
// this may or may not be the same as the spells in the spell book
exports.getCastableSpells = function(c, playerClass) {
return exports.getSpellsForSpellbook(c, playerClass);
};
exports.getSpellslots = function(c, playerClass, level) {
// find the spell slots object
var spellSlots;
for (var i = 0; i < playerClass.levelFeatures.length; i++) {
if (c.level == playerClass.levelFeatures[i].level && playerClass.levelFeatures[i].spellSlots) {
spellSlots = playerClass.levelFeatures[i].spellSlots;
}
}
// if did not find, then look in the archetype
if (!spellSlots && c.archetype) {
var archetype = playerClass.archetypes[c.archetype];
for (var i = 0; i < archetype.levelFeatures.length; i++) {
if (c.level == archetype.levelFeatures[i].level && archetype.levelFeatures[i].spellSlots) {
spellSlots = archetype.levelFeatures[i].spellSlots;
}
}
}
if (!spellSlots) {
return 0;
} else if (level == 0) {
return spellSlots.cantrips;
} else {
return spellSlots[level.toString()];
}
};
// returns true if has feature 'spellcasting'
exports.isSpellcaster = function(c) {
for (var i = 0; i < c.features.length; i++) {
if (c.features[i].label == 'Spellcasting') {
return true;
}
}
return false;
};
exports.castableCantrip = function(c, spell) {
return spell.level == 'Cantrip' && (c.spells.cantrips.indexOf(spell.name) != -1 || isClassSpell(c, spell.name));
};
function isClassSpell(c, spellName) {
// for now just make sure arcane trickster has mage hand
return spellName == 'Mage Hand' && c.class == 'Rogue' && c.archetype == 'Arcane Trickster';
};
function findSpell(list, spell) {
for (var i = 0; i < list.length; i++) {
if (list[i].name == spell.name) {
return true;
}
}
return false;
}