Advertisement
Guest User

Untitled

a guest
Aug 16th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. // Define the freeze effect
  2. const freezeEffect = {
  3. label: "Freeze",
  4. icon: "icons/svg/frozen.svg",
  5. origin: null,
  6. disabled: false,
  7. duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
  8. flags: {
  9. custom: { freezeHookId: null }
  10. },
  11. changes: []
  12. };
  13.  
  14. // Function to apply the freeze penalties immediately
  15. async function applyFreezePenalties(actor) {
  16. const spdPenalty = -3;
  17. const refPenalty = -1;
  18.  
  19. // Update SPD and Reflex stats
  20. const currentSpd = actor.system.stats.spd.current;
  21. const currentRef = actor.system.stats.ref.current;
  22.  
  23. console.log(`Current SPD: ${currentSpd}, Current Reflex: ${currentRef}`);
  24.  
  25. const newSpd = Math.max(currentSpd + spdPenalty, 0); // Ensure value does not go negative
  26. const newRef = Math.max(currentRef + refPenalty, 0); // Ensure value does not go negative
  27.  
  28. await actor.update({
  29. 'system.stats.spd.current': newSpd,
  30. 'system.stats.ref.current': newRef
  31. });
  32.  
  33. console.log(`Updated SPD: ${newSpd}, Updated Reflex: ${newRef}`);
  34.  
  35. // Inform the user
  36. ChatMessage.create({
  37. content: `${actor.name} has -3 SPD and -1 Reflex penalty from being frozen!`,
  38. speaker: ChatMessage.getSpeaker({ actor: actor })
  39. });
  40. }
  41.  
  42. // Function to remove the freeze effect
  43. async function removeFreezeEffect(actor, effectId) {
  44. console.log(`Removing ongoing effect: ${freezeEffect.label}`);
  45.  
  46. // Restore the SPD and Reflex stats to their original values
  47. await actor.update({
  48. 'system.stats.spd.current': actor.system.stats.spd.current - (-3),
  49. 'system.stats.ref.current': actor.system.stats.ref.current - (-1)
  50. });
  51.  
  52. // Remove the effect from the actor
  53. await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
  54.  
  55. console.log(`Effect removed and stats restored.`);
  56. }
  57.  
  58. // Function to send a reminder message
  59. function sendReminderMessage(actor) {
  60. ChatMessage.create({
  61. content: `${actor.name}, you are frozen! To break free, make a DC:16 Physique check which takes 1 action.`,
  62. speaker: ChatMessage.getSpeaker({ actor: actor })
  63. });
  64. }
  65.  
  66. // Function to apply or remove the freeze effect
  67. async function toggleFreezeEffect() {
  68. const token = canvas.tokens.controlled[0];
  69. if (!token) {
  70. ui.notifications.warn("Please select a token first.");
  71. return;
  72. }
  73.  
  74. const actor = token.actor;
  75.  
  76. // Check if the freeze effect is already applied
  77. let existingEffect = actor.effects.find(e => e.label === freezeEffect.label);
  78.  
  79. if (existingEffect) {
  80. await removeFreezeEffect(actor, existingEffect.id);
  81.  
  82. // Remove the combat hook
  83. const hookId = existingEffect.flags.custom?.freezeHookId;
  84. if (hookId) {
  85. Hooks.off("updateCombat", hookId);
  86. console.log(`Removed combat hook with ID: ${hookId}`);
  87. }
  88. } else {
  89. console.log(`Applying ongoing effect: ${freezeEffect.label}`);
  90. freezeEffect.origin = actor.uuid; // Set the origin to the actor's UUID
  91.  
  92. // Create the effect on the actor
  93. const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [freezeEffect]);
  94. const effectId = createdEffect[0].id;
  95.  
  96. // Apply the penalties immediately
  97. await applyFreezePenalties(actor);
  98.  
  99. // Set up a hook to send a reminder message at the start of the affected token's turn
  100. const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
  101. const combatant = combat.combatants.find(c => c.tokenId === token.id);
  102.  
  103. // If it's the start of the token's turn
  104. if (combatant && combatant.tokenId === combat.current.tokenId && combat.turns[combat.turn].tokenId === token.id) {
  105. console.log(`Sending freeze reminder message for ${token.name}`);
  106. sendReminderMessage(actor);
  107. }
  108. });
  109.  
  110. console.log(`Created combat hook with ID: ${hookId}`);
  111.  
  112. // Store the hook ID in the effect so we can remove it later
  113. try {
  114. await actor.updateEmbeddedDocuments("ActiveEffect", [{
  115. _id: effectId,
  116. "flags.custom.freezeHookId": hookId
  117. }]);
  118. } catch (error) {
  119. console.error("Failed to update flag 'custom' with hookId.", error);
  120. }
  121. }
  122. }
  123.  
  124. // Run the toggle function
  125. toggleFreezeEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement