All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
16 KiB

  1. export const moduleID = 'pf2e-staves';
  2. export const lg = x => console.log(x);
  3. const mostCommonInList = (arr) => {
  4. return arr.sort((a,b) =>
  5. arr.filter(v => v===a).length
  6. - arr.filter(v => v===b).length
  7. ).pop();
  8. }
  9. Hooks.once('init', () => {
  10. // Add Charge spell type.
  11. CONFIG.PF2E.spellCategories.charge = 'Charge';
  12. CONFIG.PF2E.preparationType.charge = 'Charge';
  13. // Patch spellcastingEntry#cast to use charges instead of spell slots for staves.
  14. libWrapper.register(moduleID, 'CONFIG.PF2E.Item.documentClasses.spellcastingEntry.prototype.cast', spellcastingEntry_cast, 'MIXED');
  15. });
  16. // When stave added to a character, also create corresponding spellcasting entry.
  17. Hooks.on('createItem', async (weapon, options, userID) => {
  18. if (!weapon.actor) return;
  19. if (userID !== game.user.id) return;
  20. const traits = weapon.system.traits?.value;
  21. const isStave = traits?.includes('magical') && traits?.includes('staff');
  22. const isCoda = traits?.includes('coda') && traits?.includes('staff');
  23. if (!isStave && !isCoda) return;
  24. return createStaveSpellcastingEntry(weapon, weapon.actor);
  25. });
  26. // When stave updated on a character, create spellcasting entry if none found. Update existing entry if found.
  27. Hooks.on('updateItem', async (weapon, update, options, userID) => {
  28. if (!weapon.actor) return;
  29. if (userID !== game.user.id) return;
  30. const traits = weapon.system.traits?.value;
  31. const isStave = traits?.includes('magical') && traits?.includes('staff');
  32. const isCoda = traits?.includes('coda') && traits?.includes('staff');
  33. if (!isStave && !isCoda) return;
  34. const { actor } = weapon;
  35. const existingStaveEntry = actor.spellcasting.find(s => s.flags && s.flags[moduleID]?.staveID === weapon?.id);
  36. return createStaveSpellcastingEntry(weapon, actor, existingStaveEntry);
  37. });
  38. // Delete spellcastingEntry associated with stave.
  39. Hooks.on('preDeleteItem', (weapon, options, userID) => {
  40. const traits = weapon.system.traits?.value;
  41. const isStave = traits?.includes('magical') && traits?.includes('staff');
  42. const isCoda = traits?.includes('coda') && traits?.includes('staff');
  43. if (!isStave && !isCoda) return;
  44. const { actor } = weapon;
  45. const spellcastingEntries = actor.items.filter(i => i.type === 'spellcastingEntry');
  46. const spellcastingEntry = spellcastingEntries.find(i => i.getFlag(moduleID, 'staveID') === weapon.id);
  47. if (spellcastingEntry) spellcastingEntry.delete();
  48. });
  49. // Implement charge spellcasting rules on character sheet.
  50. // Hooks.on('renderCharacterSheetPF2e', (sheet, [html], sheetData) => {
  51. Hooks.on('renderCreatureSheetPF2e', (sheet, [html], sheetData) => {
  52. const actor = sheet.object;
  53. const isPC = actor.type === 'character';
  54. const spellcastingLis = html.querySelectorAll('li.spellcasting-entry');
  55. for (const li of spellcastingLis) {
  56. const spellcastingEntry = actor.spellcasting.get(li.dataset.containerId);
  57. if (spellcastingEntry?.system?.prepared?.value !== 'charge') continue;
  58. let chargeEl;
  59. if (isPC) {
  60. chargeEl = document.createElement('section');
  61. chargeEl.innerHTML = `
  62. <h4 class='skill-name spellcasting'>Charges</h4>
  63. <input class="${moduleID}-charges" type="number" value="${spellcastingEntry.getFlag(moduleID, 'charges')}" placeholder="0">
  64. <a class="${moduleID}-charge"><i class="fas fa-redo"></i></a>
  65. `;
  66. } else {
  67. chargeEl = document.createElement('div');
  68. chargeEl.classList.add('inline-field');
  69. chargeEl.innerHTML = `
  70. <label>Charges</label>
  71. <input class="dc-input modifier adjustable" type="number" value="${spellcastingEntry.getFlag(moduleID, 'charges')}" placeholder="0">
  72. <a class="${moduleID}-charge"><i class="fas fa-redo"></i></a>
  73. `;
  74. }
  75. // Charge input.
  76. chargeEl.querySelector('input').addEventListener('focus', ev => {
  77. ev.currentTarget.select();
  78. });
  79. chargeEl.querySelector('input').addEventListener('change', async ev => {
  80. const { target } = ev;
  81. const charges = target.value;
  82. const clampedCharges = Math.max(0, charges);
  83. target.value = clampedCharges;
  84. await spellcastingEntry.setFlag(moduleID, 'charges', clampedCharges);
  85. });
  86. // Charge stave prompt.
  87. chargeEl.querySelector('a').addEventListener('click', async ev => {
  88. let options = ``;
  89. for (const li of spellcastingLis) {
  90. const spellcastingEntry = actor.spellcasting.get(li.dataset.containerId);
  91. if (spellcastingEntry?.system?.prepared?.value !== 'prepared') continue;
  92. const preppedSpells = [];
  93. for (const spellLi of li.querySelectorAll('li.item.spell')) {
  94. if (spellLi.dataset.expendedState === 'true' || !parseInt(spellLi.dataset.slotLevel)) continue;
  95. const spell = actor.items.get(spellLi.dataset.itemId);
  96. const { entryId, slotLevel, slotId } = spellLi.dataset;
  97. preppedSpells.push({
  98. name: spell.name,
  99. entryId,
  100. slotLevel,
  101. slotId
  102. });
  103. }
  104. if (preppedSpells.length) {
  105. options += `<optgroup label="${spellcastingEntry.name}">`
  106. for (const spell of preppedSpells) {
  107. options += `<option data-entry-id="${spell.entryId}" data-slot-level="${spell.slotLevel}" data-slot-id="${spell.slotId}">${spell.name} (+${spell.slotLevel})</option>`
  108. }
  109. options += `</optgroup>`;
  110. }
  111. }
  112. if (options) options = `<option></option>` + options;
  113. const content = options
  114. ? `
  115. <div style="font-size: var(--font-size-13);">Expend spell slot for extra charges?</div>
  116. <select style="width: 100%; margin-bottom: 5px;">
  117. ${options}
  118. </select>
  119. `
  120. : null;
  121. await Dialog.prompt({
  122. title: 'Charge Stave?',
  123. content,
  124. label: 'Charge',
  125. callback: async ([dialogHtml]) => {
  126. const charges = getHighestSpellslot(actor);
  127. const select = dialogHtml.querySelector('select');
  128. if (!select || !select?.selectedIndex) return spellcastingEntry.setFlag(moduleID, 'charges', charges);
  129. const selectedSpellOption = select.options[select.selectedIndex];
  130. const { entryId, slotLevel, slotId } = selectedSpellOption.dataset;
  131. const entry = actor.items.get(entryId);
  132. entry.setSlotExpendedState(slotLevel, slotId, true);
  133. return spellcastingEntry.setFlag(moduleID, 'charges', charges + parseInt(slotLevel));
  134. },
  135. rejectClose: false,
  136. options: { width: 250 }
  137. });
  138. });
  139. const characterHeader = li.querySelector('div.statistic-values');
  140. const npcHeader = li.querySelector('h4.name');
  141. if (isPC) characterHeader.appendChild(chargeEl);
  142. else npcHeader.after(chargeEl);
  143. // Add spontaneous spellcasting rules to Cast button right click.
  144. const castButtons = li.querySelectorAll('button.cast-spell');
  145. castButtons.forEach(button => {
  146. button.addEventListener('contextmenu', () => {
  147. const spellLi = button.closest('li.item.spell');
  148. const { itemId, slotLevel, slotId, entryId } = spellLi.dataset;
  149. const collection = actor.spellcasting.collections.get(entryId, { strict: true });
  150. const spell = collection.get(itemId, { strict: true });
  151. collection.entry.cast(spell, { level: slotLevel, [`${moduleID}Spontaneous`]: true });
  152. });
  153. });
  154. // Add .slotless-level-toggle button.
  155. const slotToggleButton = document.createElement('a');
  156. slotToggleButton.title = 'Toggle visibility of spell levels without slots';
  157. slotToggleButton.classList.add('skill-name', 'slotless-level-toggle');
  158. slotToggleButton.innerHTML = `<i class="fas fa-list-alt"></i>`;
  159. slotToggleButton.addEventListener('click', async ev => {
  160. ev.preventDefault();
  161. const spellcastingID = $(ev.currentTarget).parents(".item-container").attr("data-container-id") ?? "";
  162. if (!spellcastingID) return;
  163. const spellcastingEntry = actor.items.get(spellcastingID);
  164. const bool = !(spellcastingEntry?.system?.showSlotlessLevels || {}).value;
  165. await spellcastingEntry.update({
  166. "system.showSlotlessLevels.value": bool,
  167. });
  168. });
  169. const itemControls = li.querySelector('div.item-controls');
  170. itemControls.prepend(slotToggleButton);
  171. }
  172. });
  173. async function createStaveSpellcastingEntry(stave, actor, existingEntry = null) {
  174. const spells = [];
  175. const description = stave.system.description.value;
  176. const slotLevels = ['Cantrips?', '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th'];
  177. for (let i = 0; i < slotLevels.length; i++) {
  178. const regex = new RegExp(`${slotLevels[i]}.*@(UUID|Compendium).*\n`);
  179. const match = description.match(regex);
  180. if (!match) continue;
  181. const strs = match[0].match(/(@UUID\[Compendium\.|@Compendium\[)(.*?)]({.*?})?/g);
  182. for (const str of strs) {
  183. const UUID = str.split('[')[1].split(']')[0].replace('Compendium.', '');
  184. const spell = await fromUuid("Compendium." + UUID);
  185. if (!spell || spell?.type !== 'spell') continue;
  186. let spellClone;
  187. if (spell.id) spellClone = spell.clone({ 'system.location.heightenedLevel': i });
  188. else {
  189. const { pack, _id } = spell;
  190. const spellFromPack = await game.packs.get(pack)?.getDocuments().find(s => s.id === _id);
  191. spellClone = spellFromPack.clone({ 'system.location.heightenedLevel': i });
  192. }
  193. spells.push(spellClone);
  194. }
  195. }
  196. if (!spells.length) { // fallback
  197. const UUIDs = description.match(/(@UUID\[Compendium\.|@Compendium\[)(.*?)].*?}/g);
  198. if (!UUIDs) return;
  199. for (const str of UUIDs) {
  200. const UUID = str.split('[')[1].split(']')[0].replace('Compendium.', '');
  201. const spell = await fromUuid("Compendium." + UUID);
  202. if (!spell || spell?.type !== 'spell') continue;
  203. if (spell.id) spells.push(spell);
  204. else {
  205. const { pack, _id } = spell;
  206. const spellFromPack = await game.packs.get(pack)?.getDocuments().find(s => s.id === _id);
  207. if (spellFromPack) spells.push(spellFromPack);
  208. }
  209. }
  210. }
  211. if (!spells.length) return;
  212. if (!existingEntry) {
  213. const highestMentalAbilityValue = Math.max(...Object.keys(actor.abilities).filter(abi => ['cha', 'int', 'wis'].includes(abi)).map(abi => actor.abilities[abi].value));
  214. // picking best mental ability; not always correct, but it's a good rule of thumb
  215. const bestMentalAbility = Object.keys(actor.abilities).find(abi => actor.abilities[abi].value === highestMentalAbilityValue);
  216. // rule of thumb for tradition is to pick whatever exists in other spellcasting entries
  217. const mostCommonTradition = mostCommonInList(actor.spellcasting.map(se => se?.system?.tradition.value).filter(se => !!se));
  218. const createData = {
  219. type: 'spellcastingEntry',
  220. name: stave.name,
  221. system: {
  222. prepared: {
  223. value: 'charge'
  224. },
  225. ability: {
  226. value: bestMentalAbility
  227. },
  228. tradition: {
  229. value: mostCommonTradition
  230. },
  231. showSlotlessLevels: {
  232. value: false
  233. }
  234. },
  235. flags: {
  236. [moduleID]: {
  237. staveID: stave.id,
  238. charges: getHighestSpellslot(actor)
  239. }
  240. }
  241. }
  242. const [spellcastingEntry] = await actor.createEmbeddedDocuments('Item', [createData]);
  243. for (const spell of spells) await spellcastingEntry.addSpell(spell);
  244. } else {
  245. for (const spell of existingEntry.spells) await spell.delete();
  246. for (const spell of spells) await existingEntry.addSpell(spell);
  247. }
  248. }
  249. function getHighestSpellslot(actor) {
  250. let charges = 0;
  251. actor.spellcasting.forEach(entry => {
  252. if (!entry?.flags || entry.flags[moduleID]) return;
  253. let i = 0;
  254. Object.values(entry.system.slots).forEach(slot => {
  255. if (slot.max && charges < i) charges = i;
  256. i++;
  257. });
  258. });
  259. return charges;
  260. }
  261. async function spellcastingEntry_cast(wrapped, spell, options) {
  262. if (!spell.spellcasting.flags[moduleID] || spell.isCantrip) return wrapped(spell, options);
  263. options.consume = false;
  264. if (options[`${moduleID}Override`]) return wrapped(spell, options);
  265. const { actor } = spell;
  266. const charges = spell.spellcasting.getFlag(moduleID, 'charges');
  267. if (options[`${moduleID}Spontaneous`]) {
  268. if (!charges) return ui.notifications.warn('You do not have enough stave charges to cast this spell.');
  269. const select = document.createElement('select');
  270. select.style.width = '100%';
  271. select.style['margin-bottom'] = '5px';
  272. for (const entry of actor.spellcasting.filter(e => e.system)) {
  273. if (entry.system.prepared.value !== 'spontaneous') continue;
  274. select.innerHTML += `<optgroup label="${entry.name}">`;
  275. for (let i = parseInt(options.level); i < 12; i++) {
  276. const currentSlotLevel = Object.values(entry.system.slots)[i];
  277. const { value, max } = currentSlotLevel;
  278. if (value) select.innerHTML += `<option value="${entry.id}-${i}">Level ${i} Slot (${value}/${max})</option>`;
  279. }
  280. select.innerHTML += `</optgroup>`;
  281. }
  282. if (!select.length) return ui.notifications.warn('You do not have any Spontaneous spell slots available to cast this spell.');
  283. await Dialog.prompt({
  284. title: 'Use Spell Slot?',
  285. content: select.outerHTML,
  286. label: 'Consume Spell Slot',
  287. callback: async ([html]) => {
  288. const select = html.querySelector('select');
  289. const [entryID, selectedLevel] = select.value.split('-');
  290. const entry = actor.spellcasting.get(entryID);
  291. const currentSlots = entry.system.slots[`slot${selectedLevel}`].value;
  292. await actor.spellcasting.get(entryID).update({ [`system.slots.slot${selectedLevel}.value`]: currentSlots - 1 });
  293. await spell.spellcasting.setFlag(moduleID, 'charges', charges - 1);
  294. options[`${moduleID}Override`] = true;
  295. return spell.spellcasting.cast(spell, options);
  296. },
  297. rejectClose: false,
  298. options: { width: 250 }
  299. });
  300. } else {
  301. if (spell.level > charges) return ui.notifications.warn('You do not have enough stave charges to cast this spell.');
  302. await spell.spellcasting.setFlag(moduleID, 'charges', charges - spell.level);
  303. return wrapped(spell, options);
  304. }
  305. }