Skip to content

Commit

Permalink
Add support for metric units
Browse files Browse the repository at this point in the history
  • Loading branch information
dev7355608 committed Aug 16, 2023
1 parent 853f508 commit 93efa50
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ A token gains ...
- _See Invisibility_ if the actor has an active effect with the name `See Invisibility`[\*](#translations).
- _Witch Sight_ if the actor has a feat with the name `Invocation: Witch Sight`[\*](#translations).

By default all tokens have hearing range of 30 feet. The default hearing range can be configured in the settings.
By default all tokens have hearing range of 30 feet. The default hearing range can be configured in the module settings.

If you use metric units, you need to change the units in the module settings.

---

Expand Down
3 changes: 2 additions & 1 deletion lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"VISION5E.SeeInvisibility": "Unsichtbares sehen",
"VISION5E.WitchSight": "Hexensicht",
"VISION5E.Settings.DefaultHearingRangeN": "Standard-Hörweite",
"VISION5E.Settings.DefaultHearingRangeL": "Legt fest, wie weit Spielfiguren standardmäßig hören können. Erfordert Neuladen."
"VISION5E.Settings.DefaultHearingRangeL": "Legt fest, wie weit Spielfiguren standardmäßig hören können. Erfordert Neuladen.",
"VISION5E.Settings.UnitsL": "Legt fest, welche Längeneinheit benutzt wird. Erfordert Neuladen."
}
3 changes: 2 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"VISION5E.SeeInvisibility": "See Invisibility",
"VISION5E.WitchSight": "Witch Sight",
"VISION5E.Settings.DefaultHearingRangeN": "Default Hearing Range",
"VISION5E.Settings.DefaultHearingRangeL": "Configure how far tokens can hear by default. Requires the world to be reloaded."
"VISION5E.Settings.DefaultHearingRangeL": "Configure how far tokens can hear by default. Requires the world to be reloaded.",
"VISION5E.Settings.UnitsL": "Configure which unit of length is used. Requires the world to be reloaded."
}
3 changes: 2 additions & 1 deletion lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"VISION5E.SeeInvisibility": "Détection de l'invisibilité",
"VISION5E.WitchSight": "Vision sorcière",
"VISION5E.Settings.DefaultHearingRangeN": "Distance d'audition par défaut",
"VISION5E.Settings.DefaultHearingRangeL": "Configurez la distance jusqu'où les tokens peuvent entendre par défaut. Un redémarrage du monde est nécessaire."
"VISION5E.Settings.DefaultHearingRangeL": "Configurez la distance jusqu'où les tokens peuvent entendre par défaut. Un redémarrage du monde est nécessaire.",
"VISION5E.Settings.UnitsL": "Configurez l'unité de longueur utilisée. Un redémarrage du monde est nécessaire."
}
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": "1.9.2",
"version": "1.10.0",
"compatibility": {
"minimum": "11.299",
"verified": "11.307"
Expand Down Expand Up @@ -57,8 +57,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/v1.9.2/module.zip",
"changelog": "https://github.com/dev7355608/vision-5e/releases/tag/v1.9.2",
"download": "https://github.com/dev7355608/vision-5e/releases/download/v1.10.0/module.zip",
"changelog": "https://github.com/dev7355608/vision-5e/releases/tag/v1.10.0",
"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
23 changes: 22 additions & 1 deletion scripts/automation.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import settings from "./settings.mjs";

const RANGE_REGEX = /\b(\d+)\s+(?:feet|ft.?)\b/i;
const RANGE_REGEX = /\b([1-9]\d*)\b/i;

const effectMapping = new Map();
const featMapping = new Map();

function feetToMeters(ft) {
return ft >= 1e15 ? 1e15 : ft * 3 / 10;
}

function convertRanges(data) {
if (settings.metric) {

if (typeof data.range === "number") {
data.range = feetToMeters(data.range);
}

if (typeof data.defaultRange === "number") {
data.defaultRange = feetToMeters(data.defaultRange);
}
}
}

function registerEffect(names, data) {
convertRanges(data);

for (const name of names) {
effectMapping.set(name.toLowerCase(), data);
}
Expand All @@ -16,6 +35,8 @@ function getEffect(name) {
}

function registerFeat(names, data) {
convertRanges(data);

for (const name of names) {
featMapping.set(name.toLowerCase(), data);
}
Expand Down
41 changes: 40 additions & 1 deletion scripts/settings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ const settings = {};
export default settings;

Hooks.once("init", () => {
game.settings.register(
"vision-5e",
"units",
{
name: "DND5E.MovementUnits",
hint: "VISION5E.Settings.UnitsL",
scope: "world",
config: true,
requiresReload: true,
type: String,
choices: {
ft: "DND5E.DistFt",
m: "DND5E.DistM"
},
default: "ft"
}
);

game.settings.register(
"vision-5e",
"defaultHearingRange",
Expand All @@ -24,21 +42,42 @@ Hooks.once("init", () => {
} else {
settings.defaultHearingRange = 0;
}

settings.metric = game.settings.get("vision-5e", "units") === "m";
});

Hooks.on("renderSettingsConfig", (app, html) => {
if (!game.user.isGM) {
return;
}

const units = game.settings.get("vision-5e", "units");
const defaultHearingRange = html[0].querySelector(`input[name="vision-5e.defaultHearingRange"]`);

defaultHearingRange.value ||= 0;
defaultHearingRange.min = 0;
defaultHearingRange.step = 0.01;
defaultHearingRange.required = true;
defaultHearingRange.placeholder = "0";
defaultHearingRange.dataset.units = units;

html[0].querySelector(`[data-setting-id="vision-5e.defaultHearingRange"]`).classList.add("slim");
html[0].querySelector(`[data-setting-id="vision-5e.defaultHearingRange"] label`)
.insertAdjacentHTML("beforeend", ` <span class="units">(ft)</span>`);
.insertAdjacentHTML("beforeend", ` <span class="units">(${units})</span>`);
html[0].querySelector(`select[name="vision-5e.units"]`).addEventListener("change", (event) => {
const newUnits = event.target.value;
const oldUnits = defaultHearingRange.dataset.units;

if (oldUnits !== newUnits) {
defaultHearingRange.dataset.units = newUnits;

if (newUnits === "m") {
defaultHearingRange.value = (defaultHearingRange.value * 3 / 10).toNearest(defaultHearingRange.step);
} else {
defaultHearingRange.value = (defaultHearingRange.value * 10 / 3).toNearest(defaultHearingRange.step);
}

html[0].querySelector(`[data-setting-id="vision-5e.defaultHearingRange"] label > span.units`).innerHTML = `(${newUnits})`;
}
});
});

0 comments on commit 93efa50

Please sign in to comment.