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 3.72 KB | None | 0 0
  1. // Define the poison effect
  2. const poisonEffect = {
  3. label: "Poison",
  4. icon: "icons/svg/poison.svg", // Updated icon path
  5. origin: null,
  6. disabled: false,
  7. duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
  8. flags: {
  9. custom: { poisonDamageHookId: null }
  10. },
  11. changes: []
  12. };
  13.  
  14. // Function to apply poison damage
  15. async function applyPoisonDamage(actor) {
  16. const poisonDamage = 3;
  17.  
  18. // Reduce the actor's HP by poison damage
  19. const newHP = Math.max(actor.system.derivedStats.hp.value - poisonDamage, 0);
  20. await actor.update({
  21. 'system.derivedStats.hp.value': newHP
  22. });
  23.  
  24. // Inform the user
  25. ChatMessage.create({
  26. content: `${actor.name} takes ${poisonDamage} poison damage!`,
  27. speaker: ChatMessage.getSpeaker({ actor: actor })
  28. });
  29. }
  30.  
  31. // Function to prompt endurance check
  32. function promptEnduranceCheck(actor) {
  33. ChatMessage.create({
  34. content: `${actor.name}, you must make a DC 15 Endurance check to shake off the poison effect.`,
  35. speaker: ChatMessage.getSpeaker({ actor: actor })
  36. });
  37. }
  38.  
  39. // Function to apply or remove poison effect
  40. async function togglePoisonEffect() {
  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 poison effect is already applied
  50. let existingEffect = actor.effects.find(e => e.label === poisonEffect.label);
  51.  
  52. if (existingEffect) {
  53. console.log(`Removing ongoing effect: ${poisonEffect.label}`);
  54.  
  55. // Remove the combat hook
  56. try {
  57. const hookId = existingEffect.flags.custom?.poisonDamageHookId;
  58. if (hookId) {
  59. Hooks.off("updateCombat", hookId);
  60. console.log(`Removed combat hook with ID: ${hookId}`);
  61. }
  62. } catch (error) {
  63. console.error("Failed to get flag 'custom' or flag does not exist.", error);
  64. }
  65.  
  66. // Remove the effect from the actor
  67. await actor.deleteEmbeddedDocuments("ActiveEffect", [existingEffect.id]);
  68. } else {
  69. console.log(`Applying ongoing effect: ${poisonEffect.label}`);
  70. poisonEffect.origin = actor.uuid; // Set the origin to the actor's UUID
  71.  
  72. // Create the effect on the actor
  73. const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [poisonEffect]);
  74. const effectId = createdEffect[0].id;
  75.  
  76. // Set up a hook to apply poison damage at the start of the affected token's turn
  77. const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
  78. const combatant = combat.combatants.find(c => c.tokenId === token.id);
  79.  
  80. // If it's the start of the token's turn
  81. if (combatant && combatant.tokenId === combat.current.tokenId && combat.turns[combat.turn].tokenId === token.id) {
  82. console.log(`Applying poison damage to ${token.name}`);
  83.  
  84. // Apply poison damage
  85. applyPoisonDamage(actor);
  86.  
  87. // Prompt for Endurance check
  88. promptEnduranceCheck(actor);
  89. }
  90. });
  91.  
  92. console.log(`Created combat hook with ID: ${hookId}`);
  93.  
  94. // Store the hook ID in the effect so we can remove it later
  95. try {
  96. await actor.updateEmbeddedDocuments("ActiveEffect", [{
  97. _id: effectId,
  98. "flags.custom.poisonDamageHookId": hookId
  99. }]);
  100. } catch (error) {
  101. console.error("Failed to update flag 'custom' with hookId.", error);
  102. }
  103. }
  104. }
  105.  
  106. // Run the toggle function
  107. togglePoisonEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement