Skip to content

Commit

Permalink
Add pf2e-perception flag clearing on status changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eligarf committed Sep 25, 2024
1 parent 04d7467 commit 61c063f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v1.7.0
* If override is allowed, remove any pf2e-perception flags on a token if actor gets a condition item change to `hidden`, `undetected`, or `unnoticed`. This typically comes from using 'Assign Status Effects' on the token HUD; I use it to quickly clear out pf2e-perception's mix of hidden/undetected states after a token attacks by setting then clearing `hidden` in the token HUD.

# v1.6.0
* Add a setting to disable the requirement that PCs have an active `Avoid Notice` exploration activity to hide at combat start.

Expand Down
76 changes: 56 additions & 20 deletions esmodules/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ Hooks.once('init', () => {
const perceptionApi = game.modules.get(PERCEPTION_ID)?.api;
const useUnnoticed = game.settings.get(MODULE_ID, 'useUnnoticed');
const revealTokens = game.settings.get(MODULE_ID, 'removeGmHidden');
const overridePerception = game.settings.get(MODULE_ID, 'override');
const computeCover = game.settings.get(MODULE_ID, 'computeCover');
const overridePerception = perceptionApi && game.settings.get(MODULE_ID, 'override');
const computeCover = perceptionApi && game.settings.get(MODULE_ID, 'computeCover');
const requireActivity = game.modules.get(MODULE_ID, 'requireActivity');
let nonAvoidingPcs = [];

Expand Down Expand Up @@ -291,7 +291,7 @@ Hooks.once('init', () => {
}
const lastMessage = await game.messages.get(messages.pop()._id);
let content = renderInitiativeDice(lastMessage.rolls[0]);
content += `<div><span>${game.i18n.localize("pf2e-avoid-notice.nonHider")}</span></div>`
content += `<div><span>${game.i18n.localize("pf2e-avoid-notice.nonHider")}</span></div>`;
await lastMessage.update({ content });
}

Expand Down Expand Up @@ -332,6 +332,41 @@ function migrate(moduleVersion, oldVersion) {
return moduleVersion;
}

Hooks.once('ready', () => {

async function clearPf2ePerceptionFlags(item, options, userId) {
// Only do stuff if we are changing hidden, undetected, or unnoticed in perception
const perceptionApi = game.modules.get(PERCEPTION_ID)?.api;
if (!perceptionApi) return;
const overridePerception = game.settings.get(MODULE_ID, 'override');
if (!overridePerception) return;
if (item?.type !== 'condition' || !['hidden', 'undetected', 'unnoticed'].includes(item?.system?.slug)) return;

// Get the token on the current scene
const token = options.parent?.parent ?? canvas.scene.tokens.find((t) => t.actorId === options.parent.id);
if (!token) return;

// Remove any ids that perception is tracking if there are any
const perceptionData = token.flags?.[PERCEPTION_ID]?.data;
if (!Object.keys(perceptionData).length) return;
let tokenUpdate = {};
for (let id in perceptionData) {
tokenUpdate[`flags.${PERCEPTION_ID}.data.-=${id}`] = true;
}
const updates = [{ _id: token.id, ...tokenUpdate }];
await canvas.scene.updateEmbeddedDocuments("Token", updates);
}

Hooks.on("deleteItem", async (item, options, userId) => {
await clearPf2ePerceptionFlags(item, options, userId);
});

Hooks.on("createItem", async (item, options, userId) => {
await clearPf2ePerceptionFlags(item, options, userId);
});

});

Hooks.once('setup', () => {
const module = game.modules.get(MODULE_ID);
const moduleVersion = module.version;
Expand Down Expand Up @@ -364,24 +399,25 @@ Hooks.once('setup', () => {
});

const perception = game.modules.get(PERCEPTION_ID)?.active;
if (perception) {
game.settings.register(MODULE_ID, 'override', {
name: game.i18n.localize(`${MODULE_ID}.override.name`),
hint: game.i18n.localize(`${MODULE_ID}.override.perceptionHint`),
scope: 'world',
config: true,
type: Boolean,
default: true,
});

game.settings.register(MODULE_ID, 'override', {
name: game.i18n.localize(`${MODULE_ID}.override.name`),
hint: game.i18n.localize(`${MODULE_ID}.override.hint`),
scope: 'world',
config: perception,
type: Boolean,
default: true,
});

game.settings.register(MODULE_ID, 'computeCover', {
name: game.i18n.localize(`${MODULE_ID}.computeCover.name`),
hint: game.i18n.localize(`${MODULE_ID}.computeCover.hint`),
scope: 'world',
config: perception,
type: Boolean,
default: false,
});
game.settings.register(MODULE_ID, 'computeCover', {
name: game.i18n.localize(`${MODULE_ID}.computeCover.name`),
hint: game.i18n.localize(`${MODULE_ID}.computeCover.hint`),
scope: 'world',
config: true,
type: Boolean,
default: false,
});
}

game.settings.register(MODULE_ID, 'schema', {
name: game.i18n.localize(`${MODULE_ID}.schema.name`),
Expand Down
2 changes: 1 addition & 1 deletion languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"override": {
"name": "Override",
"hint": "Allow 'PF2e Avoid Notice' to override any existing PF2e-perception visibility flags on a token"
"perceptionHint": "Allow 'PF2e Avoid Notice' to override any existing 'PF2e-perception' visibility flags on a token"
},
"computeCover": {
"name": "Compute Cover at Combat Start",
Expand Down

0 comments on commit 61c063f

Please sign in to comment.