Advertisement
Guest User

Untitled

a guest
Aug 16th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. // Define the staggered effect
  2. const staggeredEffect = {
  3. label: "Staggered",
  4. icon: "icons/svg/sword.svg",
  5. origin: null,
  6. disabled: false,
  7. duration: { rounds: 2, startRound: game.combat?.round, startTime: game.time.worldTime },
  8. flags: {
  9. custom: { staggeredHookId: null }
  10. },
  11. changes: [
  12. // Apply a -2 penalty to specific skills
  13. { key: 'system.skills.ref.brawling.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  14. { key: 'system.skills.ref.melee.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  15. { key: 'system.skills.ref.smallblades.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  16. { key: 'system.skills.ref.staffspear.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  17. { key: 'system.skills.ref.swordsmanship.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  18. { key: 'system.skills.dex.archery.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  19. { key: 'system.skills.dex.crossbow.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  20. { key: 'system.skills.will.spellcast.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  21. { key: 'system.skills.will.resistmagic.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
  22. { key: 'system.skills.ref.dodge.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 }
  23. ]
  24. };
  25.  
  26. // Function to remove the staggered effect and restore stats
  27. async function removeStaggeredEffect(actor, effectId) {
  28. console.log(`Removing ongoing effect: ${staggeredEffect.label}`);
  29.  
  30. await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
  31.  
  32. // Inform the user
  33. ChatMessage.create({
  34. content: `${actor.name} has recovered from being staggered.`,
  35. speaker: ChatMessage.getSpeaker({ actor: actor })
  36. });
  37. }
  38.  
  39. // Toggle staggered effect
  40. async function toggleStaggeredEffect() {
  41. const token = canvas.tokens.controlled[0];
  42. if (!token) {
  43. ui.notifications.warn("Please select a token first.");
  44. return;
  45. }
  46.  
  47. const actor = token.actor;
  48.  
  49. // Check if the staggered effect is already applied
  50. let existingEffect = actor.effects.find(e => e.label === staggeredEffect.label);
  51.  
  52. if (existingEffect) {
  53. await removeStaggeredEffect(actor, existingEffect.id);
  54.  
  55. // Remove the combat hook
  56. const hookId = existingEffect.flags.custom?.staggeredHookId;
  57. if (hookId) {
  58. Hooks.off("updateCombat", hookId);
  59. console.log(`Removed combat hook with ID: ${hookId}`);
  60. }
  61. } else {
  62. console.log(`Applying ongoing effect: ${staggeredEffect.label}`);
  63. staggeredEffect.origin = actor.uuid; // Set the origin to the actor's UUID
  64.  
  65. // Create the effect on the actor
  66. const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [staggeredEffect]);
  67. const effectId = createdEffect[0].id;
  68.  
  69. // Set up a hook to remove the effect at the start of the next turn
  70. const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
  71. const combatant = combat.combatants.find(c => c.tokenId === token.id);
  72.  
  73. // Check if it's the start of the token's next turn
  74. if (combatant && combat.turns[combat.turn].tokenId === token.id) {
  75. console.log(`Removing staggered effect for ${token.name}`);
  76. removeStaggeredEffect(actor, effectId);
  77. }
  78. });
  79.  
  80. console.log(`Created combat hook with ID: ${hookId}`);
  81.  
  82. // Store the hook ID in the effect so we can remove it later
  83. try {
  84. await actor.updateEmbeddedDocuments("ActiveEffect", [{
  85. _id: effectId,
  86. "flags.custom.staggeredHookId": hookId
  87. }]);
  88. } catch (error) {
  89. console.error("Failed to update flag 'custom' with hookId.", error);
  90. }
  91.  
  92. // Inform the user
  93. ChatMessage.create({
  94. content: `${actor.name} is staggered and takes -2 to Attack Skills and Defensive Skills!`,
  95. speaker: ChatMessage.getSpeaker({ actor: actor })
  96. });
  97. }
  98. }
  99.  
  100. // Run the toggle function
  101. toggleStaggeredEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement