Skip to content

Commit

Permalink
Fix Detect Magic
Browse files Browse the repository at this point in the history
  • Loading branch information
dev7355608 committed Sep 28, 2024
1 parent 1de52f8 commit a4fb59f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
6 changes: 3 additions & 3 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"email": "[email protected]"
}
],
"version": "2.5.1",
"version": "2.5.2",
"compatibility": {
"minimum": "12",
"verified": "12"
Expand Down Expand Up @@ -60,8 +60,8 @@
},
"url": "https://github.com/dev7355608/vision-5e",
"manifest": "https://github.com/dev7355608/vision-5e/releases/latest/download/module.json",
"download": "https://github.com/dev7355608/vision-5e/releases/download/v2.5.1/module.zip",
"changelog": "https://github.com/dev7355608/vision-5e/releases/tag/v2.5.1",
"download": "https://github.com/dev7355608/vision-5e/releases/download/v2.5.2/module.zip",
"changelog": "https://github.com/dev7355608/vision-5e/releases/tag/v2.5.2",
"bugs": "https://github.com/dev7355608/vision-5e/issues",
"readme": "https://raw.githubusercontent.com/dev7355608/vision-5e/main/README.md",
"license": "https://raw.githubusercontent.com/dev7355608/vision-5e/main/LICENSE"
Expand Down
13 changes: 7 additions & 6 deletions scripts/actor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default (Actor) => class extends Actor {
this.statuses.add(CONFIG.specialStatusEffects.INAUDIBLE);
}

if (isMagical(this)) {
if (hasMagicalAura(this)) {
this.statuses.add(CONFIG.specialStatusEffects.MAGICAL);
}

Expand Down Expand Up @@ -173,8 +173,9 @@ const MAGIC_ITEM_TYPES = new Set([
* @param {Item} item
* @returns {boolean}
*/
function isMagicItem(item) {
return MAGIC_ITEM_TYPES.has(item.type) && item.system.validProperties.has("mgc") && item.system.properties.has("mgc");
function isVisibleMagicItem(item) {
return MAGIC_ITEM_TYPES.has(item.type) && item.system.validProperties.has("mgc") && item.system.properties.has("mgc")
&& (item.system.equipped === true || !item.container);
}

/**
Expand All @@ -189,9 +190,9 @@ function isMagicEffect(effect) {
* @param {Actor} actor
* @returns {boolean}
*/
function isMagical(actor) {
// Does the actor carry a magical item or is the actor affected by a spell effect?
return actor.items.some(isMagicItem) || actor.appliedEffects.some(isMagicEffect)
function hasMagicalAura(actor) {
// Does the actor carry a visible magical item or is the actor affected by a spell effect?
return actor.items.some(isVisibleMagicItem) || actor.appliedEffects.some(isMagicEffect)
}

/**
Expand Down
38 changes: 37 additions & 1 deletion scripts/detection-modes/detect-magic.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DetectionModeDetect from "./detect.mjs";
import { DETECTION_LEVELS } from "../const.mjs";

/**
* The detection mode for Detect Magic.
Expand All @@ -8,7 +9,8 @@ export default class DetectionModeDetectMagic extends DetectionModeDetect {
constructor() {
super({
id: "detectMagic",
label: "VISION5E.DetectMagic"
label: "VISION5E.DetectMagic",
imprecise: false,
});
}

Expand All @@ -27,4 +29,38 @@ export default class DetectionModeDetectMagic extends DetectionModeDetect {

return target.document.hasStatusEffect(CONFIG.specialStatusEffects.MAGICAL);
}

/** @override */
_testPoint(visionSource, mode, target, test) {
if (!super._testPoint(visionSource, mode, target, test)) {
return false;
}

const visionSources = canvas.effects.visionSources;

canvas.effects.visionSources = new foundry.utils.Collection();
canvas.effects.visionSources.set(visionSource.sourceId, visionSource);

const detectionModes = visionSource.object.document.detectionModes;
const detectionLevel = target._detectionLevel;

target._detectionLevel = DETECTION_LEVELS.NONE;

visionSource.object.document.detectionModes = detectionModes.filter(
({ id }) => {
const mode = CONFIG.Canvas.detectionModes[id];

return mode && mode !== this && mode.type === DetectionMode.DETECTION_TYPES.SIGHT && !mode.imprecise;
}
);

// Test whether this vision source sees the target
const result = canvas.visibility.testVisibility(test.point, { tolerance: 0, object: target });

target._detectionLevel = detectionLevel;
visionSource.object.document.detectionModes = detectionModes;
canvas.effects.visionSources = visionSources;

return result;
}
}

0 comments on commit a4fb59f

Please sign in to comment.