Advertisement
Guest User

Untitled

a guest
Aug 16th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. // Define the nausea effect
  2. const nauseaEffect = {
  3. name: "Nausea",
  4. icon: "icons/svg/stoned.svg", // Icon for the effect
  5. origin: null,
  6. disabled: false,
  7. duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
  8. flags: {
  9. custom: { nauseaHookId: null }
  10. },
  11. changes: []
  12. };
  13.  
  14. // Function to apply the nausea effect and set up reminders
  15. async function applyNauseaEffect(token, actor) {
  16. console.log(`Applying ongoing effect: ${nauseaEffect.name}`);
  17. nauseaEffect.origin = actor.uuid; // Set the origin to the actor's UUID
  18.  
  19. // Create the effect on the actor
  20. const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [nauseaEffect]);
  21. const effectId = createdEffect[0].id;
  22.  
  23. // Set up a hook to check for nausea every round
  24. const hookId = Hooks.on("updateCombat", async (combat, update, options, userId) => {
  25. const combatant = combat.combatants.find(c => c.tokenId === token.id);
  26.  
  27. // Check if it's the start of the token's turn
  28. if (combatant && combat.current.tokenId === token.id) {
  29. // 33% chance to need to make a check
  30. if (Math.random() < 0.33) {
  31. console.log(`${token.name} must make a nausea check.`);
  32. ChatMessage.create({
  33. content: `${actor.name}, make a BODY check or spend this round vomiting or dry-heaving.`,
  34. speaker: ChatMessage.getSpeaker({ actor: actor })
  35. });
  36. }
  37. }
  38. });
  39.  
  40. console.log(`Created combat hook with ID: ${hookId}`);
  41.  
  42. // Store the hook ID in the effect so we can remove it later
  43. try {
  44. await actor.updateEmbeddedDocuments("ActiveEffect", [{
  45. _id: effectId,
  46. "flags.custom.nauseaHookId": hookId
  47. }]);
  48. } catch (error) {
  49. console.error("Failed to update flag 'custom' with hookId.", error);
  50. }
  51.  
  52. // Notify about the applied effect
  53. ChatMessage.create({
  54. content: `${actor.name} is suffering from nausea and may vomit with a 33% chance.`,
  55. speaker: ChatMessage.getSpeaker({ actor: actor })
  56. });
  57. }
  58.  
  59. // Function to remove the nausea effect and clean up
  60. async function removeNauseaEffect(actor, effectId) {
  61. console.log(`Removing ongoing effect: ${nauseaEffect.name}`);
  62.  
  63. // Remove the nausea effect from the actor
  64. await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
  65.  
  66. // Remove the combat hook
  67. const hookId = actor.effects.find(e => e.name === nauseaEffect.name)?.flags.custom?.nauseaHookId;
  68. if (hookId) {
  69. Hooks.off("updateCombat", hookId);
  70. console.log(`Removed combat hook with ID: ${hookId}`);
  71. }
  72.  
  73. // Inform the user
  74. ChatMessage.create({
  75. content: `${actor.name} is no longer nauseated.`,
  76. speaker: ChatMessage.getSpeaker({ actor: actor })
  77. });
  78. }
  79.  
  80. // Toggle nausea effect
  81. async function toggleNauseaEffect() {
  82. const token = canvas.tokens.controlled[0];
  83. if (!token) {
  84. ui.notifications.warn("Please select a token first.");
  85. return;
  86. }
  87.  
  88. const actor = token.actor;
  89.  
  90. // Check if the nausea effect is already applied
  91. let existingEffect = actor.effects.find(e => e.name === nauseaEffect.name);
  92.  
  93. if (existingEffect) {
  94. await removeNauseaEffect(actor, existingEffect.id);
  95. } else {
  96. await applyNauseaEffect(token, actor);
  97. }
  98. }
  99.  
  100. // Run the toggle function
  101. toggleNauseaEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement