Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define the nausea effect
- const nauseaEffect = {
- name: "Nausea",
- icon: "icons/svg/stoned.svg", // Icon for the effect
- origin: null,
- disabled: false,
- duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
- flags: {
- custom: { nauseaHookId: null }
- },
- changes: []
- };
- // Function to apply the nausea effect and set up reminders
- async function applyNauseaEffect(token, actor) {
- console.log(`Applying ongoing effect: ${nauseaEffect.name}`);
- nauseaEffect.origin = actor.uuid; // Set the origin to the actor's UUID
- // Create the effect on the actor
- const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [nauseaEffect]);
- const effectId = createdEffect[0].id;
- // Set up a hook to check for nausea every round
- const hookId = Hooks.on("updateCombat", async (combat, update, options, userId) => {
- const combatant = combat.combatants.find(c => c.tokenId === token.id);
- // Check if it's the start of the token's turn
- if (combatant && combat.current.tokenId === token.id) {
- // 33% chance to need to make a check
- if (Math.random() < 0.33) {
- console.log(`${token.name} must make a nausea check.`);
- ChatMessage.create({
- content: `${actor.name}, make a BODY check or spend this round vomiting or dry-heaving.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- }
- });
- console.log(`Created combat hook with ID: ${hookId}`);
- // Store the hook ID in the effect so we can remove it later
- try {
- await actor.updateEmbeddedDocuments("ActiveEffect", [{
- _id: effectId,
- "flags.custom.nauseaHookId": hookId
- }]);
- } catch (error) {
- console.error("Failed to update flag 'custom' with hookId.", error);
- }
- // Notify about the applied effect
- ChatMessage.create({
- content: `${actor.name} is suffering from nausea and may vomit with a 33% chance.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to remove the nausea effect and clean up
- async function removeNauseaEffect(actor, effectId) {
- console.log(`Removing ongoing effect: ${nauseaEffect.name}`);
- // Remove the nausea effect from the actor
- await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
- // Remove the combat hook
- const hookId = actor.effects.find(e => e.name === nauseaEffect.name)?.flags.custom?.nauseaHookId;
- if (hookId) {
- Hooks.off("updateCombat", hookId);
- console.log(`Removed combat hook with ID: ${hookId}`);
- }
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} is no longer nauseated.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Toggle nausea effect
- async function toggleNauseaEffect() {
- const token = canvas.tokens.controlled[0];
- if (!token) {
- ui.notifications.warn("Please select a token first.");
- return;
- }
- const actor = token.actor;
- // Check if the nausea effect is already applied
- let existingEffect = actor.effects.find(e => e.name === nauseaEffect.name);
- if (existingEffect) {
- await removeNauseaEffect(actor, existingEffect.id);
- } else {
- await applyNauseaEffect(token, actor);
- }
- }
- // Run the toggle function
- toggleNauseaEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement