Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Function to apply regeneration
- async function applyRegeneration(actor, amount) {
- // Get the current and max HP values
- const currentHP = actor.system.derivedStats.hp.value;
- const maxHP = actor.system.derivedStats.hp.max;
- // Calculate the new HP value
- const newHP = Math.min(currentHP + amount, maxHP);
- // Update the actor's HP
- await actor.update({
- 'system.derivedStats.hp.value': newHP
- });
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} regenerates ${amount} HP!`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to create and display the dialog
- function showRegenerationDialog() {
- new Dialog({
- title: "Apply Regeneration Effect",
- content: `
- <form>
- <div class="form-group">
- <label for="rounds">Number of Rounds:</label>
- <input id="rounds" name="rounds" type="number" min="1" step="1" value="1" />
- </div>
- <div class="form-group">
- <label for="amount">Regeneration Amount:</label>
- <input id="amount" name="amount" type="number" min="1" step="1" value="3" />
- </div>
- </form>
- `,
- buttons: {
- apply: {
- label: "Apply",
- callback: async (html) => {
- const rounds = parseInt(html.find('#rounds').val(), 10);
- const amount = parseInt(html.find('#amount').val(), 10);
- if (isNaN(rounds) || isNaN(amount) || rounds <= 0 || amount <= 0) {
- ui.notifications.error("Invalid input values.");
- return;
- }
- const token = canvas.tokens.controlled[0];
- if (!token) {
- ui.notifications.warn("Please select a token first.");
- return;
- }
- const actor = token.actor;
- // Define the regeneration effect
- const regenerationEffect = {
- name: "Regeneration",
- icon: "icons/svg/regen.svg",
- origin: actor.uuid,
- disabled: false,
- duration: { rounds: rounds, startRound: game.combat?.round, startTime: game.time.worldTime },
- flags: {
- custom: { regenDamageHookId: null }
- },
- changes: []
- };
- // Check if the regeneration effect is already applied
- let existingEffect = actor.effects.find(e => e.name === regenerationEffect.name);
- if (existingEffect) {
- console.log(`Removing ongoing effect: ${regenerationEffect.name}`);
- // Remove the combat hook
- try {
- const hookId = existingEffect.flags.custom?.regenDamageHookId;
- if (hookId) {
- Hooks.off("updateCombat", hookId);
- console.log(`Removed combat hook with ID: ${hookId}`);
- }
- } catch (error) {
- console.error("Failed to get flag 'custom' or flag does not exist.", error);
- }
- // Remove the effect from the actor
- await actor.deleteEmbeddedDocuments("ActiveEffect", [existingEffect.id]);
- } else {
- console.log(`Applying ongoing effect: ${regenerationEffect.name}`);
- regenerationEffect.origin = actor.uuid; // Set the origin to the actor's UUID
- // Create the effect on the actor
- const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [regenerationEffect]);
- const effectId = createdEffect[0].id;
- // Set up a hook to apply regeneration at the start of the affected token's turn
- const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
- const combatant = combat.combatants.find(c => c.tokenId === token.id);
- // If it's the start of the token's turn
- if (combatant && combatant.tokenId === combat.current.tokenId && combat.turns[combat.turn].tokenId === token.id) {
- // Apply regeneration
- applyRegeneration(actor, amount);
- console.log(`Applying regeneration to ${token.name}`);
- }
- });
- 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.regenDamageHookId": hookId
- }]);
- } catch (error) {
- console.error("Failed to update flag 'custom' with hookId.", error);
- }
- }
- }
- },
- cancel: {
- label: "Cancel"
- }
- },
- default: "apply"
- }).render(true);
- }
- // Function to remove expired regeneration effects
- async function removeExpiredRegenerationEffects(actor) {
- const currentRound = game.combat?.round || 0;
- const effects = actor.effects.filter(effect => effect.name === "Regeneration");
- for (const effect of effects) {
- const endRound = effect.duration.startRound + effect.duration.rounds;
- if (currentRound >= endRound) {
- console.log(`Removing expired effect: ${effect.name}`);
- // Remove the combat hook if it exists
- const hookId = effect.flags.custom?.regenDamageHookId;
- if (hookId) {
- Hooks.off("updateCombat", hookId);
- console.log(`Removed combat hook with ID: ${hookId}`);
- }
- // Remove the effect from the actor
- await actor.deleteEmbeddedDocuments("ActiveEffect", [effect.id]);
- }
- }
- }
- // Run the dialog function
- showRegenerationDialog();
- // Periodically remove expired effects (for example, at the start of each round)
- Hooks.on("updateCombat", (combat) => {
- const tokens = canvas.tokens.placeables.filter(t => t.actor);
- for (const token of tokens) {
- removeExpiredRegenerationEffects(token.actor);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement