forked from Roll20/roll20-character-sheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeroLabImport.js
307 lines (290 loc) · 15.2 KB
/
HeroLabImport.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
// Thanks to HoneyBadger for creating the original import script.
// His script was written for importing DDI 4e .monster files
// This script is for using Custom XML Output from Hero Lab
// The Hero Lab plugin for Castles & Crusades was written by BoomerET
// This script was adapted from HoneyBadger's script by BoomerET
// VARIABLE & FUNCTION DECLARATIONS
var AddAttribute = AddAttribute || {};
on("chat:message", function (msg) {
// Exit if not an api command
if (msg.type != "api") return;
// Get the API Chat Command
msg.who = msg.who.replace(" (GM)", "");
msg.content = msg.content.replace("(GM) ", "");
var command = msg.content.split(" ", 1);
if (command == "!createPC") {
if (!msg.selected) return;
var n = msg.content.split(" ", 2);
var Token = getObj("graphic", n[1])
if (Token.get("subtype") != "token") return;
if (Token.get("gmnotes").indexOf("xml") == -1) return;
// REPLACE SPECIAL CHARACTERS StatBlock = StatBlock.replace(//g, "");
var StatBlock = Token.get("gmnotes");
StatBlock = StatBlock.replace(/%20/g, " "); // Replace %20 with a space
StatBlock = StatBlock.replace(/%22/g, "'"); // Replace %22 (quotation) with '
StatBlock = StatBlock.replace(/%26lt/g, "<"); // Replace %26lt with <
StatBlock = StatBlock.replace(/%26gt/g, ">"); // Replace %26gt with >
StatBlock = StatBlock.replace(/%26amp/g, "&"); // Replace ampersand
StatBlock = StatBlock.replace(/%27/g, "'"); // Replace %27 with '
StatBlock = StatBlock.replace(/%28/g, "("); // Replace %28 with (
StatBlock = StatBlock.replace(/%29/g, ")"); // Replace %29 with )
StatBlock = StatBlock.replace(/%2C/g, ","); // Replace %2C with ,
StatBlock = StatBlock.replace(/%3A/g, ":"); // Replace %3A with :
StatBlock = StatBlock.replace(/%3B/g, ""); // Remove %3B (semi-colon)
StatBlock = StatBlock.replace(/%3Cbr/g, ""); // Remove carriage returns
StatBlock = StatBlock.replace(/%3D/g, "="); // Replace %3D with =
StatBlock = StatBlock.replace(/%3E/g, ""); // Remove %3E (???)
StatBlock = StatBlock.replace(/%3F/g, "?"); // Replace %3F with ?
StatBlock = StatBlock.replace(/\s{2,}/g, " "); // Replace multiple spaces with one space
StatBlock = StatBlock.replace(/%u2019/g, "'"); // Replace %u2019 with '
// END SPECIAL CHARACTER REPLACEMENT or REMOVAL
var CharacterName = StatBlock.match(/<character.*name=\'(.*)\' play.*/)[1];
// CHECK FOR DUPLICATE CHARACTERS
var CheckSheet = findObjs({
_type: "character",
name: CharacterName
});
// DO NOT CREATE IF SHEET EXISTS
if (CheckSheet.length > 0) {
sendChat("ERROR", "This character already exists.");
return;
}
// CREATE CHARACTER SHEET & LINK TOKEN TO SHEET
var Character = createObj("character", {
avatar: Token.get("imgsrc"),
name: CharacterName,
gmnotes: Token.get("gmnotes"),
archived: false
});
// GET LEVEL, ROLE, & XP
var CharLevel = parseInt(StatBlock.match(/<class name=\'.*\' level=\'(\d+)\'\/>/)[1]);
var CharClass = StatBlock.match(/<class name=\'(.*)\' level=\'\d*\'\/>/)[1];
var CharXP = parseInt(StatBlock.match(/<xp total=\'(\d*)\' next=\'\d*\'\/>/)[1]);
var CharXPnext = parseInt(StatBlock.match(/<xp total=\'\d*\' next=\'(\d*)\'\/>/)[1]);
var CharRace = StatBlock.match(/<race name=\'(.*)\'\/><class/)[1];
var CharAlignment = StatBlock.match(/<alignment text=\'(.*)\'\/><size/)[1];
var CharMovement = parseInt(StatBlock.match(/<charmove.*value=\'(\d*)\'\/><\/pers/)[1]);
var CharHPwounds = parseInt(StatBlock.match(/<hitpoints total=\'\d+' wounds=\'(\d+)\'/)[1]);
var CharHP = parseInt(StatBlock.match(/<hitpoints total=\'(\d+)\'/)[1]);
var CharBtH = parseInt(StatBlock.match(/<bth value=\'(-?\d+?)\'/)[1]);
var CharHPcurrent = CharHP - CharHPwounds;
AddAttribute("CharacterName", CharacterName, Character.id);
AddAttribute("Class", CharClass, Character.id);
AddAttribute("Experience", CharXP, Character.id);
AddAttribute("NextLevel", CharXPnext, Character.id);
AddAttribute("Race", CharRace, Character.id);
AddAttribute("Alignment", CharAlignment, Character.id);
AddAttribute("Movement", CharMovement, Character.id);
AddAttribute("HPmax", CharHP, Character.id);
AddAttribute("HP", CharHPcurrent, Character.id);
AddAttribute("Level", CharLevel, Character.id);
var myRegex = /<language name='(.*?)'\/>/g;
var matchLanguages = getMatches(StatBlock, myRegex, 1);
var CharLanguages = matchLanguages.join();
AddAttribute("Languages", CharLanguages, Character.id);
myRegex = /<attribute name='(.*?)' value='(\d+?)' bonus='(-?\d+?)' primary='(\d+?)'/g;
var matchAttributes = getMatches(StatBlock, myRegex, 1);
var matchAttrValue = getMatches(StatBlock, myRegex, 2);
var matchAttrMod = getMatches(StatBlock, myRegex, 3);
var matchPrimary = getMatches(StatBlock, myRegex, 4);
for (var i = 0; i < matchAttributes.length; i++) {
AddAttribute(matchAttributes[i], matchAttrValue[i], Character.id);
AddAttribute(matchAttributes[i] + "Pri", matchPrimary[i], Character.id);
AddAttribute(matchAttributes[i] + "Mod", matchAttrMod[i], Character.id);
if (matchAttributes[i] == "Wisdom") {
if ((CharClass == "Cleric") || (CharClass == "Paladin")) {
AddAttribute("TurnUndeadTurned", "d12 + " + matchAttrMod[i], Character.id);
} else {
AddAttribute("TurnUndeadTurned", "Not a Cleric/Paladin", Character.id);
AddAttribute("TurnUndeadCheck", "Not a Cleric/Paladin", Character.id);
}
}
}
myRegex = /<defense name='(.*?)' defense='(-?\d+?)' equipped='(.*?)' type='(.*?)'/g;
var matchArmorName = getMatches(StatBlock, myRegex, 1);
var matchArmorValue = getMatches(StatBlock, myRegex, 2);
var matchArmorEquipped = getMatches(StatBlock, myRegex, 3);
var matchArmorType = getMatches(StatBlock, myRegex, 4);
var totalAC = 10;
for (var j = 0; j < matchArmorName.length; j++) {
if ((matchArmorEquipped[j] == 'yes') && (matchArmorType[j] == 'armor')) {
AddAttribute("ArmorAC", matchArmorValue[j], Character.id);
totalAC += parseInt(matchArmorValue[j]);
AddAttribute("ArmorWorn", matchArmorName[j], Character.id);
}
if ((matchArmorEquipped[j] == 'yes') && (matchArmorType[j] == 'shield')) {
AddAttribute("ShieldAC", matchArmorValue[j], Character.id);
totalAC += parseInt(matchArmorValue[j]);
AddAttribute("ShieldWorn", matchArmorName[j], Character.id);
}
if ((matchArmorEquipped[j] == 'yes') && (matchArmorType[j] == 'helm')) {
AddAttribute("HelmWorn", matchArmorName[j], Character.id);
}
}
AddAttribute("AC", totalAC, Character.id);
// Abilities
myRegex = /<ability name=\'(.*?)\' value=\'(-?\d+?)\' bonus=\'(-?\d+?)\' primary=\'-?\d+?\' attrabil=\'(.*?)\' clorra=\'(.*?)\'/g;
var weapSpecialization = "";
var abilityName = getMatches(StatBlock, myRegex, 1);
var abilityValue = getMatches(StatBlock, myRegex, 2);
var abilityBonus = getMatches(StatBlock, myRegex, 3);
var abilityAttribute = getMatches(StatBlock, myRegex, 4);
var abilityType = getMatches(StatBlock, myRegex, 5);
var numClassAbilities = 0;
var numRaceAbilities = 0;
for (var l = 0; l < abilityName.length; l++) {
if (abilityType[l] == "class") {
if (abilityAttribute[l] == "") {
AddAttribute("repeating_classabilities_" + numClassAbilities.toString() + "_ClassAbility", abilityName[l], Character.id);
} else {
AddAttribute("repeating_classabilities_" + numClassAbilities.toString() + "_ClassAbility", abilityName[l] + " (" + abilityAttribute[l].substring(0,3) + ")", Character.id);
}
numClassAbilities += 1;
if (abilityName[l].match(/Weapon Specialization/)) {
var wepRegex = /\((.*?)\)/g;
weapSpecialization = wepRegex.exec(abilityName[l]);
}
} else if (abilityType[l] == "race") {
AddAttribute("repeating_raceabilities_" + numRaceAbilities.toString() + "_RaceAbility", abilityName[l], Character.id);
numRaceAbilities += 1;
}
}
myRegex = /<attack name='(.*?)' damage='(.*?)' dambon='(-?\d+?)' hitbon='(-?\d+?)' magicdam='(-?\d+?)' magichit='(-?\d+?)' equipped='(.*?)'.*?<description>(.*?) weapon<\/description>/g;
var weapName = getMatches(StatBlock, myRegex, 1);
var weapDamage = getMatches(StatBlock, myRegex, 2);
var weapDamBonus = getMatches(StatBlock, myRegex, 3);
var weapHitBonus = getMatches(StatBlock, myRegex, 4);
var weapMagicDam = getMatches(StatBlock, myRegex, 5);
var weapMagicHit = getMatches(StatBlock, myRegex, 6);
var weapEquipped = getMatches(StatBlock, myRegex, 7);
var weapType = getMatches(StatBlock, myRegex, 8);
var weapHitMisc = 0;
var weapDmgMisc = 0;
var weapHitTotal = 0;
var weapDmgTotal = 0;
var weapHitMod = 0;
var weapDmgMod = 0;
var DexMod = 0;
var StrMod = 0;
for (var k = 0; k < Math.min(weapName.length, 5); k++) {
if (weapSpecialization[1] == weapName[k]) {
weapDmgMisc += 1;
weapHitMisc += 1;
}
AddAttribute("WeaponName_" + (k+1).toString(), weapName[k], Character.id);
AddAttribute("WeaponDmg_" + (k+1).toString(), weapDamage[k], Character.id);
AddAttribute("WeaponBth_" + (k+1).toString(), CharBtH, Character.id);
AddAttribute("WeaponDmgMagic_" + (k+1).toString(), weapMagicDam[k], Character.id);
AddAttribute("WeaponHitMagic_" + (k+1).toString(), weapMagicHit[k], Character.id);
AddAttribute("WeaponDmgMisc_" + (k+1).toString(), weapDmgMisc, Character.id);
AddAttribute("WeaponHitMisc_" + (k+1).toString(), weapHitMisc, Character.id);
StrMod = parseInt(getAttrByName(Character.id, "StrengthMod"));
DexMod = parseInt(getAttrByName(Character.id, "DexterityMod"));
if (weapType[k] == "Melee") {
AddAttribute("WeaponDmgMod_" + (k+1).toString(), StrMod, Character.id);
AddAttribute("WeaponHitMod_" + (k+1).toString(), StrMod, Character.id);
weapHitTotal = parseInt(CharBtH) + parseInt(weapMagicHit[k]) + weapHitMisc + StrMod;
weapDmgTotal = parseInt(weapMagicDam[k]) + weapDmgMisc + parseInt(StrMod);
} else if (weapType[k] == "Ranged") {
AddAttribute("WeaponHitMod_" + (k+1).toString(), DexMod, Character.id);
weapHitTotal = parseInt(CharBtH) + parseInt(weapMagicHit[k]) + weapHitMisc + DexMod;
weapDmgTotal = parseInt(weapMagicDam[k]) + weapDmgMisc;
}
AddAttribute("WeaponHitTotal_" + (k+1).toString(), weapHitTotal, Character.id);
AddAttribute("WeaponDmgTotal_" + (k+1).toString(), weapDmgTotal, Character.id);
weapHitMisc = 0;
weapDmgMisc = 0;
weapHitTotal = 0;
weapDmgTotal = 0;
StrMod = 0;
DexMod = 0;
}
myRegex = /<sense name='(.*?)'><desc/g;
var senseNames = getMatches(StatBlock, myRegex, 1);
for (var m = 0; m < senseNames.length; m++) {
AddAttribute("repeating_senses_" + m.toString() + "_Senses", senseNames[m], Character.id);
}
var whatTypeSpells = "N";
if ((CharClass == "Cleric") || (CharClass == "Druid")) {
whatTypeSpells = "D";
} else if ((CharClass == "Wizard") || (CharClass == "Illusionist")) {
whatTypeSpells = "A";
}
if ((whatTypeSpells == "A") || (whatTypeSpells == "D")) {
myRegex = /<availablelevel(\d) value=\'(\d+)\'/g;
var spellLvl = getMatches(StatBlock, myRegex, 1);
var totalSpells= getMatches(StatBlock, myRegex, 2);
var IntVal = parseInt(getAttrByName(Character.id, "Intelligence"));
var WisVal = parseInt(getAttrByName(Character.id, "Wisdom"));
for (var n = 0; n < 10; n++) {
if (n == 0) {
AddAttribute("numspells_0", totalSpells[n].toString(), Character.id);
AddAttribute("bonusspells_0", "0", Character.id);
} else if (n ==1) {
if (((whatTypeSpells == "D") && (WisVal < 13)) || ((whatTypeSpells == "A") && (IntVal < 13))) {
AddAttribute("numspells_1", totalSpells[n].toString(), Character.id);
AddAttribute("bonusspells_1", "0", Character.id);
} else {
AddAttribute("numspells_1", (parseInt(totalSpells[n]) - 1).toString(), Character.id);
AddAttribute("bonusspells_1", "1", Character.id);
}
} else if (n == 2) {
if (((whatTypeSpells == "D") && (WisVal < 16)) || ((whatTypeSpells == "A") && (IntVal < 16))) {
AddAttribute("numspells_2", totalSpells[n].toString(), Character.id);
AddAttribute("bonusspells_2", "0", Character.id);
} else {
if (parseInt(totalSpells[n]) > 0) {
AddAttribute("numspells_2", (parseInt(totalSpells[n]) - 1).toString(), Character.id);
AddAttribute("bonusspells_2", "1", Character.id);
} else {
AddAttribute("numspells_2", (parseInt(totalSpells[n])).toString(), Character.id);
AddAttribute("bonusspells_2", "0", Character.id);
}
}
} else if (n == 3) {
if (((whatTypeSpells == "D") && (WisVal < 18)) || ((whatTypeSpells == "A") && (IntVal < 18))) {
AddAttribute("numspells_3", totalSpells[n].toString(), Character.id);
AddAttribute("bonusspells_3", "0", Character.id);
} else {
if (parseInt(totalSpells[n]) > 0) {
AddAttribute("numspells_3", (parseInt(totalSpells[n]) - 1).toString(), Character.id);
AddAttribute("bonusspells_3", "1", Character.id);
} else {
AddAttribute("numspells_3", (parseInt(totalSpells[n])).toString(), Character.id);
AddAttribute("bonusspells_3", "0", Character.id);
}
}
} else {
AddAttribute("numspells_" + n.toString(), totalSpells[n].toString(), Character.id);
AddAttribute("bonusspells_" + n.toString(), "0", Character.id);
}
}
}
}
});
function AddAttribute(attr, value, charid) {
if (attr === "Hit Points") {
createObj("attribute", {
name: attr,
current: value,
max: value,
characterid: charid
});
} else {
createObj("attribute", {
name: attr,
current: value,
characterid: charid
});
}
return;
}
function getMatches(string, regex, index) {
index || (index = 1);
var matches = [];
var match;
while (match = regex.exec(string)) {
matches.push(match[index]);
}
return matches;
}