-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtoken-document.mjs
137 lines (113 loc) · 4.97 KB
/
token-document.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { convertUnits, fromFeet } from "./utils.mjs";
export default (TokenDocument) => class extends TokenDocument {
/** @override */
prepareBaseData() {
super.prepareBaseData();
this._prepareSight();
// Sorting is necessary for patched CanvasVisibility#testVisibility
this.detectionModes.sort((a, b) => {
a = CONFIG.Canvas.detectionModes[a.id];
b = CONFIG.Canvas.detectionModes[b.id];
return (b.important ?? false) - (a.important ?? false)
|| (a.imprecise ?? false) - (b.imprecise ?? false)
|| (b.id === this.sight.detectionMode) - (a.id === this.sight.detectionMode)
|| (b.priority ?? 0) - (a.priority ?? 0);
});
}
/** @override */
_prepareDetectionModes() {
this.detectionModes = [];
if (!this.sight.enabled) {
return;
}
for (const { id, enabled, range } of this._source.detectionModes) {
if (!(id in CONFIG.Canvas.detectionModes)) {
continue;
}
this.detectionModes.push({ id, enabled, range });
}
const sceneUnits = this.parent?.grid.units || "";
if (this.actor) {
const actorUnits = this.actor.system.attributes?.senses?.units ?? "ft";
for (const [id, range] of Object.entries(this.actor.detectionModes)) {
if (!this.detectionModes.find((mode) => mode.id === id)) {
this.detectionModes.push({
id,
enabled: true,
range: convertUnits(range, actorUnits, sceneUnits),
});
}
}
} else {
if (!this.detectionModes.find((mode) => mode.id === "lightPerception")) {
this.detectionModes.push({ id: "lightPerception", enabled: true, range: null });
}
}
if (this.hasStatusEffect(CONFIG.specialStatusEffects.ETHEREAL)) {
const maxRange = fromFeet(60, sceneUnits);
for (const mode of this.detectionModes) {
if (mode.range !== null) {
mode.range = Math.min(mode.range, maxRange);
} else {
mode.range = maxRange;
}
}
}
}
/** @protected */
_prepareSight() {
if (!this.sight.enabled) {
this.sight.range = 0;
this.sight.angle = 360;
this.sight.visionMode = "darkvision";
this.sight.detectionMode = "basicSight";
this.sight.color = null;
this.sight.attenuation = 0;
this.sight.brightness = 0;
this.sight.saturation = 0;
this.sight.contrast = 0;
return;
}
let detectionMode = VISION_TO_DETECTION_MODE_MAPPING[this._source.sight.visionMode];
if (detectionMode && (detectionMode = this.detectionModes.find((mode) => mode.id === detectionMode && mode.enabled && mode.range > 0))) {
this.sight.range = detectionMode.range;
this.sight.visionMode = this._source.sight.visionMode;
this.sight.detectionMode = detectionMode.id;
} else {
this.sight.range = 0;
this.sight.visionMode = "basic";
this.sight.detectionMode = "basicSight";
for (const [visionMode, detectionMode] of Object.entries(VISION_TO_DETECTION_MODE_MAPPING)) {
const mode = this.detectionModes.find((mode) => mode.id === detectionMode);
if (!mode || !mode.enabled || this.sight.range >= mode.range) {
continue;
}
this.sight.range = mode.range;
this.sight.visionMode = visionMode;
this.sight.detectionMode = detectionMode;
}
}
this.sight.angle = this._source.sight.angle;
if (this.sight.visionMode === "basic") {
this.sight.visionMode = "darkvision";
this.sight.color = null;
this.sight.attenuation = 0;
this.sight.brightness = -1;
this.sight.saturation = -1;
this.sight.contrast = 0;
} else {
const { color, attenuation, brightness, saturation, contrast } = CONFIG.Canvas.visionModes[this.sight.visionMode].vision.defaults;
this.sight.color = color !== undefined ? color : this._source.sight.color !== null ? Color.from(this._source.sight.color) : null;
this.sight.attenuation = attenuation !== undefined ? attenuation : this._source.sight.attenuation;
this.sight.brightness = brightness !== undefined ? brightness : 0;
this.sight.saturation = saturation !== undefined ? saturation : 0;
this.sight.contrast = contrast !== undefined ? contrast : 0;
}
}
};
const VISION_TO_DETECTION_MODE_MAPPING = {
truesight: "seeAll",
blindsight: "blindsight",
devilsSight: "devilsSight",
darkvision: "basicSight",
};