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.

366 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. if(CONFIG.PF2E.spellCategories) 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,button[data-action="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. // If the previous and current descriptions match, assume no changes are needed and skip this
  178. const prevDesc = existingEntry?.getFlag(moduleID, "prevDescription");
  179. if (prevDesc === description) return;
  180. for (let i = 0; i < slotLevels.length; i++) {
  181. const regex = new RegExp(`${slotLevels[i]}.*@(UUID|Compendium).*\n`);
  182. const match = description.match(regex);
  183. if (!match) continue;
  184. const strs = match[0].match(/(@UUID\[Compendium\.|@Compendium\[)(.*?)]({.*?})?/g);
  185. for (const str of strs) {
  186. const UUID = str.split('[')[1].split(']')[0].replace('Compendium.', '');
  187. const spell = await fromUuid("Compendium." + UUID);
  188. if (!spell || spell?.type !== 'spell') continue;
  189. let spellClone;
  190. if (spell.id) spellClone = spell.clone({ 'system.location.heightenedLevel': i });
  191. else {
  192. const { pack, _id } = spell;
  193. const spellFromPack = await game.packs.get(pack)?.getDocuments().find(s => s.id === _id);
  194. spellClone = spellFromPack.clone({ 'system.location.heightenedLevel': i });
  195. }
  196. spells.push(spellClone);
  197. }
  198. }
  199. if (!spells.length) { // fallback
  200. const UUIDs = description.match(/(@UUID\[Compendium\.|@Compendium\[)(.*?)].*?}/g);
  201. if (!UUIDs) return;
  202. for (const str of UUIDs) {
  203. const UUID = str.split('[')[1].split(']')[0].replace('Compendium.', '');
  204. const spell = await fromUuid("Compendium." + UUID);
  205. if (!spell || spell?.type !== 'spell') continue;
  206. if (spell.id) spells.push(spell);
  207. else {
  208. const { pack, _id } = spell;
  209. const spellFromPack = await game.packs.get(pack)?.getDocuments().find(s => s.id === _id);
  210. if (spellFromPack) spells.push(spellFromPack);
  211. }
  212. }
  213. }
  214. if (!spells.length) return;
  215. if (!existingEntry) {
  216. const highestMentalAbilityValue = Math.max(...Object.keys(actor.abilities).filter(abi => ['cha', 'int', 'wis'].includes(abi)).map(abi => actor.abilities[abi].value));
  217. // picking best mental ability; not always correct, but it's a good rule of thumb
  218. const bestMentalAbility = Object.keys(actor.abilities).find(abi => actor.abilities[abi].value === highestMentalAbilityValue);
  219. // rule of thumb for tradition is to pick whatever exists in other spellcasting entries
  220. const mostCommonTradition = mostCommonInList(actor.spellcasting.map(se => se?.system?.tradition.value).filter(se => !!se));
  221. const createData = {
  222. type: 'spellcastingEntry',
  223. name: stave.name,
  224. system: {
  225. prepared: {
  226. value: 'charge'
  227. },
  228. ability: {
  229. value: bestMentalAbility
  230. },
  231. tradition: {
  232. value: mostCommonTradition
  233. },
  234. showSlotlessLevels: {
  235. value: false
  236. }
  237. },
  238. flags: {
  239. [moduleID]: {
  240. staveID: stave.id,
  241. charges: getHighestSpellslot(actor),
  242. prevDescription: description
  243. }
  244. }
  245. }
  246. const [spellcastingEntry] = await actor.createEmbeddedDocuments('Item', [createData]);
  247. for (const spell of spells) await spellcastingEntry.addSpell(spell);
  248. } else {
  249. for (const spell of existingEntry.spells) await spell.delete();
  250. for (const spell of spells) await existingEntry.addSpell(spell);
  251. await existingEntry.setFlag(moduleID, "prevDescription", description);
  252. }
  253. }
  254. function getHighestSpellslot(actor) {
  255. let charges = 0;
  256. actor.spellcasting.contents.forEach(entry => {
  257. if (!entry?.flags || entry.flags[moduleID]) return;
  258. let i = 0;
  259. Object.values(entry.system.slots).forEach(slot => {
  260. if (slot.max && charges < i) charges = i;
  261. i++;
  262. });
  263. });
  264. return charges;
  265. }
  266. async function spellcastingEntry_cast(wrapped, spell, options) {
  267. if (!spell.spellcasting.flags[moduleID] || spell.isCantrip) return wrapped(spell, options);
  268. options.consume = false;
  269. if (options[`${moduleID}Override`]) return wrapped(spell, options);
  270. const { actor } = spell;
  271. const charges = spell.spellcasting.getFlag(moduleID, 'charges');
  272. if (options[`${moduleID}Spontaneous`]) {
  273. if (!charges) return ui.notifications.warn('You do not have enough stave charges to cast this spell.');
  274. const select = document.createElement('select');
  275. select.style.width = '100%';
  276. select.style['margin-bottom'] = '5px';
  277. for (const entry of actor.spellcasting.filter(e => e.system)) {
  278. if (entry.system.prepared.value !== 'spontaneous') continue;
  279. select.innerHTML += `<optgroup label="${entry.name}">`;
  280. for (let i = parseInt(options.level); i < 12; i++) {
  281. const currentSlotLevel = Object.values(entry.system.slots)[i];
  282. const { value, max } = currentSlotLevel;
  283. if (value) select.innerHTML += `<option value="${entry.id}-${i}">Level ${i} Slot (${value}/${max})</option>`;
  284. }
  285. select.innerHTML += `</optgroup>`;
  286. }
  287. if (!select.length) return ui.notifications.warn('You do not have any Spontaneous spell slots available to cast this spell.');
  288. await Dialog.prompt({
  289. title: 'Use Spell Slot?',
  290. content: select.outerHTML,
  291. label: 'Consume Spell Slot',
  292. callback: async ([html]) => {
  293. const select = html.querySelector('select');
  294. const [entryID, selectedLevel] = select.value.split('-');
  295. const entry = actor.spellcasting.get(entryID);
  296. const currentSlots = entry.system.slots[`slot${selectedLevel}`].value;
  297. await actor.spellcasting.get(entryID).update({ [`system.slots.slot${selectedLevel}.value`]: currentSlots - 1 });
  298. await spell.spellcasting.setFlag(moduleID, 'charges', charges - 1);
  299. options[`${moduleID}Override`] = true;
  300. return spell.spellcasting.cast(spell, options);
  301. },
  302. rejectClose: false,
  303. options: { width: 250 }
  304. });
  305. } else {
  306. if (spell.level > charges) return ui.notifications.warn('You do not have enough stave charges to cast this spell.');
  307. await spell.spellcasting.setFlag(moduleID, 'charges', charges - spell.level);
  308. return wrapped(spell, options);
  309. }
  310. }