Skip to content

Commit

Permalink
API for Perceptive
Browse files Browse the repository at this point in the history
  • Loading branch information
Eligarf committed Apr 29, 2024
1 parent 7941c0e commit cb87409
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 35 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v4.0.1
* Stealthy 4 isn't currently compatible with Perceptive, so I added stealthy.bankPerception and bankStealth to help insulate that module from my internal flag details

# v4.0.0
* Big code changes, but games should have a seamless upgrade since no data migrations are required.
* Stealthy now allows the choice of banking stealth and perception rolls to tokens or actors via effects.
Expand Down
20 changes: 18 additions & 2 deletions scripts/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ export default class Engine {
canvas.perception.update({ initializeVision: true }, true);
}

async putRollOnToken(tokenOrActor, skill, value) {
Stealthy.log('putRollOnToken', { tokenOrActor, skill, value });
async bankRollOnToken(tokenOrActor, skill, value) {
Stealthy.log('bankRollOnToken', { tokenOrActor, skill, value });
let token = tokenOrActor;
if (token instanceof Actor) {
token = canvas.tokens.controlled.find((t) => t.actor === tokenOrActor);
Expand All @@ -305,6 +305,22 @@ export default class Engine {
await canvas.scene.updateEmbeddedDocuments("Token", [update]);
}

async bankPerception(token, value) {
if (stealthy.perceptionToActor) {
await this.updateOrCreateSpotEffect(token.actor, { perception: value });
} else {
await this.bankRollOnToken(token, 'perception', value);
}
}

async setBankedStealth(token, value) {
if (stealthy.stealthToActor) {
await this.updateOrCreateHiddenEffect(token.actor, { stealth: value });
} else {
await this.bankRollOnToken(token, 'stealth', value);
}
}

rollPerception() {
canvas.perception.update({ initializeVision: true }, true);
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Hooks.once('setup', () => {
scope: 'client',
config: true,
type: Boolean,
default: true,
default: false,
});

game.settings.register(Stealthy.MODULE_ID, 'spotSecretDoors', {
Expand Down Expand Up @@ -206,7 +206,7 @@ Hooks.on('renderTokenHUD', (tokenHUD, html, app) => {
const token = tokenHUD.object;

if (game.settings.get(Stealthy.MODULE_ID, 'exposure') && !game.modules.get('tokenlightcondition')?.active) {
const exposure = engine.getLightExposure(token) ?? 'dark';
const exposure = engine.getLightExposure(token);
const icon = LIGHT_ICONS[exposure];
const title = game.i18n.localize(`stealthy.exposure.${exposure}`);
html.find(".right").append($(`<div class="control-icon" title="${title}">${icon}</div>`));
Expand Down
34 changes: 27 additions & 7 deletions scripts/stealthy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,34 @@ export class Stealthy {
this.engine.patchFoundry();
Hooks.once('setup', () => {
this.socket = socketlib.registerModule(Stealthy.MODULE_ID);
this.socket.register('TogglePerceptionBanking', Stealthy.TogglePerceptionBanking);
this.socket.register('GetPerceptionBanking', Stealthy.GetPerceptionBanking);
this.socket.register('RefreshPerception', Stealthy.RefreshPerception);
this.socket.register('TogglePerceptionBanking', this.togglePerceptionBanking);
this.socket.register('GetPerceptionBanking', this.getPerceptionBanking);
this.socket.register('RefreshPerception', this.refreshPerception);
});
}

static async TogglePerceptionBanking(toggled) {
getBankedPerception(token) {
const flag = this.engine.getPerceptionFlag(token);
return this.engine.getPerceptionValue(flag);
}

getBankedStealth(token) {
const flag = this.engine.getStealthFlag(token);
return this.engine.getStealthValue(flag);
}

async bankPerception(token, value) {
if (value?.normal === undefined) {
value = { normal: value, disadvantaged: value - 5 };
}
await this.engine.bankPerception(token, value);
}

async bankStealth(token, value) {
await this.engine.bankStealth(token, value);
}

async togglePerceptionBanking(toggled) {
Stealthy.log(`ToggletPerceptionBanking <= ${toggled}`);
stealthy.bankingPerception = toggled;
if (toggled || !game.user.isGM)
Expand All @@ -40,13 +61,12 @@ export class Stealthy {
await canvas.scene.updateEmbeddedDocuments("Token", updates);
}

static RefreshPerception() {
refreshPerception() {
Stealthy.log(`RefreshPerception`);
canvas.perception.update({ initializeVision: true }, true);
}

static async GetPerceptionBanking() {
Stealthy.log(`GetPerceptionBanking => ${stealthy.bankingPerception}`);
async getPerceptionBanking() {
return stealthy.bankingPerception;
}

Expand Down
14 changes: 2 additions & 12 deletions scripts/systems/dnd4e.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ class Engine4e extends Engine {
if (!stealthy.bankingPerception) return;

const token = canvas.tokens.get(message.speaker.token);
if (stealthy.perceptionToActor) {
const actor = token.actor;
await this.updateOrCreateSpotEffect(actor, { perception: message.rolls[0].total });
} else {
await this.putRollOnToken(token, 'perception', message.rolls[0].total);
}
await this.bankPerception(token, message.rolls[0].total);

super.rollPerception();
}
Expand All @@ -69,12 +64,7 @@ class Engine4e extends Engine {
Stealthy.log('rollStealth', { message, options, id });

const token = canvas.tokens.get(message.speaker.token);
if (stealthy.stealthToActor) {
const actor = token.actor;
await this.updateOrCreateHiddenEffect(actor, { stealth: message.rolls[0].total });
} else {
await this.putRollOnToken(token, 'stealth', message.rolls[0].total);
}
await this.bankStealth(token, message.rolls[0].total);

super.rollStealth();
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/systems/dnd5e.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class Engine5e extends Engine {
if (stealthy.perceptionToActor) {
await this.updateOrCreateSpotEffect(actor, { perception });
} else {
await this.putRollOnToken(actor, 'perception', perception);
await this.bankRollOnToken(actor, 'perception', perception);
}

super.rollPerception();
Expand All @@ -247,7 +247,7 @@ class Engine5e extends Engine {
if (stealthy.stealthToActor) {
await this.updateOrCreateHiddenEffect(actor, { stealth: roll.total });
} else {
await this.putRollOnToken(actor, 'stealth', roll.total);
await this.bankRollOnToken(actor, 'stealth', roll.total);
}

super.rollStealth();
Expand Down
12 changes: 2 additions & 10 deletions scripts/systems/pf1.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,7 @@ export class EnginePF1 extends Engine {
if (!stealthy.bankingPerception) return;

const token = canvas.tokens.get(message.speaker.token);
if (stealthy.perceptionToActor) {
await this.updateOrCreateSpotEffect(actor, { perception: message.rolls[0].total });
} else {
await this.putRollOnToken(token, 'perception', message.rolls[0].total);
}
await this.bankPerception(token, message.rolls[0].total);

super.rollPerception();
}
Expand All @@ -218,11 +214,7 @@ export class EnginePF1 extends Engine {
Stealthy.log('rollStealth', { actor, message });

const token = canvas.tokens.get(message.speaker.token);
if (stealthy.stealthToActor) {
await this.updateOrCreateHiddenEffect(actor, { stealth: message.rolls[0].total });
} else {
await this.putRollOnToken(token, 'stealth', message.rolls[0].total);
}
await this.bankStealth(token, message.rolls[0].total);

super.rollStealth();
}
Expand Down

0 comments on commit cb87409

Please sign in to comment.