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.

330 lines
14 KiB

1 year ago
  1. class CompanionManager extends FormApplication {
  2. constructor(actor) {
  3. super();
  4. this.actor = actor;
  5. }
  6. static get defaultOptions() {
  7. return {
  8. ...super.defaultOptions,
  9. title: game.i18n.localize("AE.dialogs.companionManager.title"),
  10. id: "companionManager",
  11. template: `modules/automated-evocations/templates/companionmanager.hbs`,
  12. resizable: true,
  13. width: 300,
  14. height: window.innerHeight > 400 ? 400 : window.innerHeight - 100,
  15. dragDrop: [{ dragSelector: null, dropSelector: null }],
  16. };
  17. }
  18. static get api() {
  19. return {
  20. dnd5e: {
  21. getSummonInfo(args, spellLevel) {
  22. const spellDC = args[0].assignedActor?.system.attributes.spelldc || 0;
  23. return {
  24. level: (args[0].spellLevel || spellLevel) - spellLevel,
  25. maxHP: args[0].assignedActor?.system.attributes.hp.max || 1,
  26. modifier: args[0].assignedActor?.system.abilities[args[0].assignedActor?.system.attributes.spellcasting]?.mod,
  27. dc: spellDC,
  28. attack: {
  29. ms: spellDC - 8 + args[0].assignedActor?.system.bonuses.msak.attack,
  30. rs: spellDC - 8 + args[0].assignedActor?.system.bonuses.rsak.attack,
  31. mw: args[0].assignedActor?.system.bonuses.mwak.attack,
  32. rw: args[0].assignedActor?.system.bonuses.rwak.attack,
  33. },
  34. };
  35. },
  36. },
  37. };
  38. }
  39. getData() {
  40. return {};
  41. }
  42. async activateListeners(html) {
  43. html.find("#companion-list").before(`<div class="searchbox"><input type="text" class="searchinput" placeholder="Drag and Drop an actor to add it to the list."></div>`);
  44. this.loadCompanions();
  45. html.on("input", ".searchinput", this._onSearch.bind(this));
  46. html.on("click", "#remove-companion", this._onRemoveCompanion.bind(this));
  47. html.on("click", "#summon-companion", this._onSummonCompanion.bind(this));
  48. html.on("click", ".actor-name", this._onOpenSheet.bind(this));
  49. html.on("dragstart", "#companion", async (event) => {
  50. event.originalEvent.dataTransfer.setData("text/plain", event.currentTarget.dataset.elid);
  51. });
  52. html.on("dragend", "#companion", async (event) => {
  53. event.originalEvent.dataTransfer.setData("text/plain", event.currentTarget.dataset.elid);
  54. });
  55. }
  56. _onSearch(event) {
  57. const search = $(event.currentTarget).val();
  58. this.element.find(".actor-name").each(function () {
  59. if ($(this).text().toLowerCase().includes(search.toLowerCase())) {
  60. $(this).parent().slideDown(200);
  61. } else {
  62. $(this).parent().slideUp(200);
  63. }
  64. });
  65. }
  66. _canDragDrop() {
  67. return true;
  68. }
  69. _canDragStart() {
  70. return true;
  71. }
  72. async _onDrop(event) {
  73. let data;
  74. try {
  75. data = JSON.parse(event.dataTransfer.getData("text/plain"));
  76. } catch {
  77. data = event.dataTransfer.getData("text/plain");
  78. }
  79. const li = this.element.find(`[data-elid="${data}"]`);
  80. if (li.length && !$(event.target).hasClass("nodrop")) {
  81. let target = $(event.target).closest("li");
  82. if (target.length && target[0].dataset.elid != data) {
  83. $(li).remove();
  84. target.before($(li));
  85. }
  86. }
  87. if (!data.type === "Actor") return;
  88. //const actor = await fromUuid(data.uuid)
  89. this.element.find("#companion-list").append(await this.generateLi({ id: data.uuid }));
  90. this.saveData();
  91. }
  92. async _onSummonCompanion(event) {
  93. this.minimize();
  94. const animation = $(event.currentTarget.parentElement.parentElement).find(".anim-dropdown").val();
  95. const aId = event.currentTarget.dataset.aid;
  96. const actor = game.actors.get(aId) || (await fromUuid(aId));
  97. const duplicates = $(event.currentTarget.parentElement.parentElement).find("#companion-number-val").val();
  98. const tokenData = await actor.getTokenData({ elevation: _token?.data?.elevation ?? 0 });
  99. if (this.range) this.drawRange(this.range);
  100. const posData = await warpgate.crosshairs.show({
  101. size: Math.max((Math.max(tokenData.width, tokenData.height) * (tokenData.texture.scaleX + tokenData.texture.scaleY)) / 2, 0.5),
  102. icon: "modules/automated-evocations/assets/black-hole-bolas.webp",
  103. label: "",
  104. });
  105. this.clearRange();
  106. if (!posData || posData.cancelled) {
  107. this.maximize();
  108. return;
  109. }
  110. if (typeof AECONSTS.animationFunctions[animation].fn == "string") {
  111. this.evaluateExpression(game.macros.getName(AECONSTS.animationFunctions[animation].fn).command, posData, tokenData);
  112. } else {
  113. AECONSTS.animationFunctions[animation].fn(posData, tokenData);
  114. }
  115. await this.wait(AECONSTS.animationFunctions[animation].time);
  116. //get custom data macro
  117. const customTokenData = (await this.evaluateExpression(game.macros.getName(`AE_Companion_Macro(${actor.name})`)?.command, { summon: actor, spellLevel: this.spellLevel || 0, duplicates: duplicates, assignedActor: this.caster || game.user.character || _token.actor })) || {};
  118. customTokenData.elevation = posData?.flags?.levels?.elevation ?? _token?.document?.elevation ?? 0;
  119. customTokenData.elevation = parseFloat(customTokenData.elevation);
  120. tokenData.elevation = customTokenData.elevation;
  121. let isCompendiumActor = false;
  122. if (!tokenData.actor) {
  123. isCompendiumActor = true;
  124. tokenData.updateSource({ actorId: Array.from(game.actors).find((a) => !a.prototypeToken?.actorLink).id });
  125. }
  126. Hooks.on("preCreateToken", (tokenDoc, td) => {
  127. td ??= {};
  128. td.elevation = customTokenData.elevation;
  129. tokenDoc.updateSource({ elevation: customTokenData.elevation });
  130. });
  131. Hooks.callAll("automated-evocations.preCreateToken", { tokenData: tokenData, customTokenData: customTokenData, posData: posData, actor: actor, spellLevel: this.spellLevel || 0, duplicates: duplicates, assignedActor: this.caster || game.user.character || _token.actor });
  132. const tokens = await warpgate.spawnAt({ x: posData.x, y: posData.y }, tokenData, customTokenData || {}, {}, { duplicates });
  133. if (tokens.length && isCompendiumActor) {
  134. for (const t of tokens) {
  135. const tokenDocument = canvas.tokens.get(t).document;
  136. await tokenDocument.update({ delta: actor.toObject() });
  137. }
  138. }
  139. const postSummon = {
  140. assignedActor: this.caster || game?.user?.character || _token?.actor,
  141. spellLevel: this.spellLevel || 0,
  142. duplicates: duplicates,
  143. warpgateData: customTokenData || {},
  144. summon: actor,
  145. tokenData: tokenData,
  146. posData: posData,
  147. summonedTokens: tokens,
  148. };
  149. console.log("Automated Evocations Summoning:", postSummon);
  150. Hooks.callAll("automated-evocations.postSummon", postSummon);
  151. if (game.settings.get(AECONSTS.MN, "autoclose")) this.close();
  152. else this.maximize();
  153. }
  154. async drawRange() {
  155. const token = _token;
  156. if (!token) return;
  157. this.rangeData = {
  158. token,
  159. };
  160. const rangeGraphics = new PIXI.Graphics();
  161. rangeGraphics
  162. .lineStyle(1, 0x000000, 1)
  163. .beginFill(0x000000, 0.1)
  164. .drawCircle(0, 0, this.range * (canvas.scene.dimensions.size / canvas.scene.dimensions.distance))
  165. .endFill();
  166. if (this.image) {
  167. const texture = this.image ? await loadTexture(this.image) : null;
  168. //create a matrix so that the texture is centered and fills the circle
  169. const matrix = new PIXI.Matrix();
  170. matrix.translate(-texture.width / 2, -texture.height / 2);
  171. matrix.scale((2 * this.range * (canvas.scene.dimensions.size / canvas.scene.dimensions.distance)) / texture.width, (2 * this.range * (canvas.scene.dimensions.size / canvas.scene.dimensions.distance)) / texture.height);
  172. rangeGraphics
  173. .beginTextureFill({ texture: texture, matrix, alpha: 0.3 })
  174. .drawCircle(0, 0, this.range * (canvas.scene.dimensions.size / canvas.scene.dimensions.distance))
  175. .endFill();
  176. }
  177. rangeGraphics.position.set((canvas.scene.dimensions.size * token.document.width) / 2, (canvas.scene.dimensions.size * token.document.height) / 2);
  178. rangeGraphics.zIndex = -100;
  179. //add as first child so that it is behind the token
  180. token.addChildAt(rangeGraphics, 0);
  181. this.rangeData.graphics = rangeGraphics;
  182. }
  183. async clearRange() {
  184. if (!this.rangeData) return;
  185. this.rangeData.token.removeChild(this.rangeData.graphics);
  186. this.rangeData.graphics.destroy();
  187. delete this.rangeData;
  188. }
  189. async evaluateExpression(expression, ...args) {
  190. if (!expression) return null;
  191. const AsyncFunction = async function () {}.constructor;
  192. const fn = new AsyncFunction("args", $("<span />", { html: expression }).text());
  193. try {
  194. return await fn(args);
  195. } catch (e) {
  196. ui.notifications.error("There was an error in your macro syntax. See the console (F12) for details");
  197. console.error(e);
  198. return undefined;
  199. }
  200. }
  201. async _onRemoveCompanion(event) {
  202. Dialog.confirm({
  203. title: game.i18n.localize("AE.dialogs.companionManager.confirm.title"),
  204. content: game.i18n.localize("AE.dialogs.companionManager.confirm.content"),
  205. yes: () => {
  206. event.currentTarget.parentElement.remove();
  207. this.saveData();
  208. },
  209. no: () => {},
  210. defaultYes: false,
  211. });
  212. }
  213. async _onOpenSheet(event) {
  214. const actorId = event.currentTarget.parentElement.dataset.aid;
  215. const actor = game.actors.get(actorId) || (await fromUuid(actorId));
  216. if (actor) {
  217. actor.sheet.render(true);
  218. }
  219. }
  220. async loadCompanions() {
  221. let data = this.actor && (this.actor.getFlag(AECONSTS.MN, "isLocal") || game.settings.get(AECONSTS.MN, "storeonactor")) ? this.actor.getFlag(AECONSTS.MN, "companions") || [] : game.user.getFlag(AECONSTS.MN, "companions");
  222. if (data) {
  223. for (let companion of data) {
  224. this.element.find("#companion-list").append(await this.generateLi(companion));
  225. }
  226. }
  227. }
  228. async generateLi(data) {
  229. const actor = game.actors.get(data.id) || game.actors.getName(data.id) || (await fromUuid(data.id));
  230. if (!actor) return "";
  231. const uuid = actor.uuid;
  232. const restricted = game.settings.get(AECONSTS.MN, "restrictOwned");
  233. if (restricted && !actor.isOwner) return "";
  234. let $li = $(`
  235. <li id="companion" class="companion-item" data-aid="${uuid}" data-elid="${randomID()}" draggable="true">
  236. <div class="summon-btn">
  237. <img class="actor-image" src="${actor.img}" alt="">
  238. <div class="warpgate-btn" id="summon-companion" data-aid="${uuid}"></div>
  239. </div>
  240. <span class="actor-name">${actor.name}</span>
  241. <div class="companion-number"><input type="number" min="1" max="99" class="fancy-input" step="1" id="companion-number-val" value="${data.number || 1}"></div>
  242. <select class="anim-dropdown">
  243. ${this.getAnimations(data.animation)}
  244. </select>
  245. <i id="remove-companion" class="fas fa-trash"></i>
  246. </li>
  247. `);
  248. // <i id="advanced-params" class="fas fa-edit"></i>
  249. return $li;
  250. }
  251. getAnimations(anim) {
  252. let animList = "";
  253. for (let [group, animations] of Object.entries(AECONSTS.animations)) {
  254. const localGroup = game.i18n.localize(`AE.groups.${group}`);
  255. animList += `<optgroup label="${localGroup == `AE.groups.${group}` ? group : localGroup}">`;
  256. for (let a of animations) {
  257. animList += `<option value="${a.key}" ${a.key == anim ? "selected" : ""}>${a.name}</option>`;
  258. }
  259. animList += "</optgroup>";
  260. }
  261. return animList;
  262. }
  263. async wait(ms) {
  264. return new Promise((resolve) => setTimeout(resolve, ms));
  265. }
  266. async saveData() {
  267. let data = [];
  268. for (let companion of this.element.find(".companion-item")) {
  269. data.push({
  270. id: companion.dataset.aid,
  271. animation: $(companion).find(".anim-dropdown").val(),
  272. number: $(companion).find("#companion-number-val").val(),
  273. });
  274. }
  275. this.actor && (this.actor.getFlag(AECONSTS.MN, "isLocal") || game.settings.get(AECONSTS.MN, "storeonactor")) ? this.actor.setFlag(AECONSTS.MN, "companions", data) : game.user.setFlag(AECONSTS.MN, "companions", data);
  276. Hooks.callAll("automated-evocations.saveCompanionData", { actor: this.actor, data: data });
  277. }
  278. close(noSave = false) {
  279. if (!noSave) this.saveData();
  280. super.close();
  281. }
  282. }
  283. class SimpleCompanionManager extends CompanionManager {
  284. constructor(summonData,spellLevel,actor, range, image) {
  285. super();
  286. this.caster = actor;
  287. this.summons = summonData;
  288. this.spellLevel = spellLevel
  289. this.range = range;
  290. this.image = image;
  291. }
  292. async activateListeners(html) {
  293. for (let summon of this.summons) {
  294. this.element.find("#companion-list").append(await this.generateLi(summon));
  295. }
  296. html.on("click", "#summon-companion", this._onSummonCompanion.bind(this));
  297. html.on("click", ".actor-name", this._onOpenSheet.bind(this));
  298. }
  299. _onDrop(event) {}
  300. close() {
  301. super.close(true);
  302. }
  303. }