Skip to content

Commit

Permalink
fix: Yet another try at making the wounded/dying handling less wonky.…
Browse files Browse the repository at this point in the history
… Now with extra logging. (Search for dyingLastApplied in the log if you're trying to figure out what goes wrong.)
  • Loading branch information
xdy committed Dec 18, 2023
1 parent 263078b commit 16784fb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
9 changes: 2 additions & 7 deletions src/module/feature/conditionHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,13 @@ export async function handleDyingOnZeroHP(
}

export async function autoRemoveDyingAtGreaterThanZeroHp(actor: ActorPF2e, hpAboveZero: boolean): Promise<boolean> {
let dying = actor.getCondition("dying");
const dying = actor.getCondition("dying");
if (shouldIHandleThis(actor) && dying && !dying.isLocked && hpAboveZero) {
const value = dying?.value || 0;
if (dying && value > 0 && !dying.isLocked) {
const option = String(game.settings.get(MODULENAME, "autoRemoveDyingAtGreaterThanZeroHP"));
if (option.endsWith("ForCharacters") ? ["character", "familiar"].includes(actor.type) : true) {
for (let i = 0; i < Math.max(1, value); i++) {
dying = actor.getCondition("dying");
if (dying && !dying.isLocked) {
await actor.decreaseCondition("dying");
}
}
handleDying(0, 0, actor);
}
}
}
Expand Down
40 changes: 30 additions & 10 deletions src/module/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,35 @@ export function handleDying(dyingCounter: number, originalDyingCounter: number,
const shouldDie = originalDyingCounter + dyingCounter >= actor.system.attributes.dying.max && !defeated;
const shouldBecomeDying = originalDyingCounter + dyingCounter > 0 && !defeated;
if (shouldDie) {
actor.combatant?.toggleDefeated().then();
// Dead, not dying, so clear the flag.
actor.unsetFlag(MODULENAME, "dyingLastApplied").then();
actor.combatant?.toggleDefeated().then(() => {
// Dead, not dying, so clear the flag.
actor
.unsetFlag(MODULENAME, "dyingLastApplied")
.then(() => console.log("dyingLastApplied cleared because dead"));
});
} else if (shouldBecomeDying) {
actor
.increaseCondition("dying", {
value: originalDyingCounter + dyingCounter,
})
.then();
actor.setFlag(MODULENAME, "dyingLastApplied", Date.now()).then();
.then(() => {
const now = Date.now();
return actor
.setFlag(MODULENAME, "dyingLastApplied", now)
.then(() =>
console.log(
`dyingLastApplied set to ${now}, dyingCounter was ${originalDyingCounter} is ${
originalDyingCounter + dyingCounter
}`,
),
);
});
} else {
actor.decreaseCondition("dying", { forceRemove: true }).then();
actor.unsetFlag(MODULENAME, "dyingLastApplied").then();
actor.decreaseCondition("dying", { forceRemove: true }).then(() => {
return actor
.unsetFlag(MODULENAME, "dyingLastApplied")
.then(() => console.log("dyingLastApplied cleared because not dying"));
});
}
}

Expand Down Expand Up @@ -145,10 +161,10 @@ export function createChatMessageHook(message: ChatMessagePF2e) {
if (!String(game.settings.get(MODULENAME, "autoGainDyingIfTakingDamageWhenAlreadyDying")).startsWith("no")) {
const actor = message.actor;
if (actor && shouldIHandleThis(actor)) {
const now = Date.now();
const flag = <number>actor.getFlag(MODULENAME, "dyingLastApplied") || Date.now();

if (message.content?.includes("damage-taken")) {
const now = Date.now();
const flag = <number>actor.getFlag(MODULENAME, "dyingLastApplied") || now;
console.log(`dyingLastApplied is ${flag}, now is ${now}`);
// Ignore this if it occurs within last few seconds of the last time we applied dying
// @ts-ignore
const notTooSoon = !flag?.between(now - 4000, now);
Expand All @@ -172,6 +188,10 @@ export function createChatMessageHook(message: ChatMessagePF2e) {
dyingCounter = dyingCounter + 1;
}
}
console.log(
`Before handleDying dyingLastApplied is ${flag}, now is ${now}, dyingCounter was ${originalDyingCounter} will increase by ${dyingCounter}`,
);

handleDying(dyingCounter, originalDyingCounter, actor);
}
}
Expand Down

0 comments on commit 16784fb

Please sign in to comment.