From 8d61d3571553d6dba6398e2c445c8ecb445f56ea Mon Sep 17 00:00:00 2001 From: theripper93 Date: Tue, 5 Apr 2022 14:19:59 +0200 Subject: [PATCH] a --- lang/en.json | 8 ++++-- packs/macros.db | 1 + scripts/patches.js | 8 ++++-- scripts/utils.js | 45 ++++++++++++++++++++++++++++++++ scripts/wall-height.js | 59 +++++++++++++++++++++++++----------------- 5 files changed, 93 insertions(+), 28 deletions(-) diff --git a/lang/en.json b/lang/en.json index 598d258..c96847d 100644 --- a/lang/en.json +++ b/lang/en.json @@ -19,6 +19,10 @@ "settings.enableTooltip.name": "Enable Tooltip", "settings.enableTooltip.hint": "Enables the tooltip on the walls layer, showing top and bottom dimensions of walls. If you currently have the wall layer up, you should refresh or move to another scene and back to refresh.", "settings.migrateOnStartup.name": "Migrate Wall-Height Data On Startup", - "settings.migrateOnStartup.hint": "Migrates wall-height data from the old wall-height data structure to the new one. This is only necessary once but you can enable this settings and refresh the page if you need to run it again." + "settings.migrateOnStartup.hint": "Migrates wall-height data from the old wall-height data structure to the new one. This is only necessary once but you can enable this settings and refresh the page if you need to run it again.", + "settings.autoLOSHeight.name": "Automatic Token Height", + "settings.autoLOSHeight.hint": "Calculate token height automatically via token size and scale.\nToken height will be calculated for any token without a custom height value or with a custom height set to 0. Default token height will be ignored.", + "settings.defaultLosHeight.name": "Default Token Height", + "settings.defaultLosHeight.hint": "Height of a token (where the eyes of that creature are) in units. Default 6." } -} +} \ No newline at end of file diff --git a/packs/macros.db b/packs/macros.db index a8fad5d..32eff63 100644 --- a/packs/macros.db +++ b/packs/macros.db @@ -3,3 +3,4 @@ {"name":"Set Elevation","type":"script","author":"Cspu3pxFr7tu7SU7","img":"icons/svg/mountain.svg","scope":"global","command":"let applyChanges = false;\n\nif(args && args.length>0){\nconsole.log(args);\n let enterval=parseFloat(args[0]);\n let exitval= args.length>1?parseFloat(args[1]):null;\n let elevation=0;\n if(event && event === MLT.ENTER)\n elevation = enterval;\n else if(event ** event === MLT.LEAVE)\n elevation = exitval;\n token.document.update({\n \"elevation\": elevation\n });\n}\nelse\n{\n new Dialog({\n title: `Token Elevation Changer`,\n content: `\n
\n
\n \n \n
\n
\n `,\n buttons: {\n yes: {\n icon: \"\",\n label: `Apply Changes`,\n callback: () => applyChanges = true\n },\n no: {\n icon: \"\",\n label: `Cancel Changes`\n },\n },\n default: \"yes\",\n close: async (html) => {\n if (applyChanges) {\n for ( let token of canvas.tokens.controlled ) {\n let elevation = html.find('[name=\"token-elevation\"]')[0].value || \"0\";\n await token.document.update({\n \"elevation\": parseFloat(elevation)\n });\n\n }\n }\n }\n }).render(true);\n}","folder":null,"sort":0,"permission":{"default":0,"M6L6STuBiXTluEz7":3,"Cspu3pxFr7tu7SU7":3},"flags":{"core":{"sourceId":"Macro.CpL3YgGgMfvubvuq"}},"_id":"EnrKiUU4syNsPvKx"} {"name":"Wall Height - Reduce Top By One","type":"script","author":"Cspu3pxFr7tu7SU7","img":"icons/svg/dice-target.svg","scope":"global","command":"WallHeight.removeOneToWalls()","folder":null,"sort":0,"permission":{"default":0,"Cspu3pxFr7tu7SU7":3},"flags":{"core":{"sourceId":"Macro.M0klOUeUmDepn4v2"}},"_id":"q9sxsxABjshPkK0V"} {"name":"Wall Height - Migrate Compendiums","type":"script","author":"Cspu3pxFr7tu7SU7","img":"icons/svg/dice-target.svg","scope":"global","command":"WallHeight.migrateCompendiums()","folder":null,"sort":0,"permission":{"default":0,"Cspu3pxFr7tu7SU7":3},"flags":{"core":{"sourceId":"Macro.h3qyEgom3RjIgvq7"}},"_id":"uPDEi0D43DJ8ifBf"} +{"name":"Wall Height - Migrate Token Height","type":"script","author":"Cspu3pxFr7tu7SU7","img":"icons/svg/dice-target.svg","scope":"global","command":"WallHeight.migrateTokenHeight()","folder":null,"sort":0,"permission":{"default":0,"Cspu3pxFr7tu7SU7":3},"flags":{"core":{"sourceId":"Macro.BhqF3Fbsqgu0Dy0s"}},"_id":"bKDBWpXkOxfB65Wq"} diff --git a/scripts/patches.js b/scripts/patches.js index 7658306..2e3465f 100644 --- a/scripts/patches.js +++ b/scripts/patches.js @@ -1,4 +1,4 @@ -import { getWallBounds,getSceneSettings,migrateData,getLevelsBounds,getAdvancedLighting } from "./utils.js"; +import { getWallBounds,getSceneSettings,migrateData,getLevelsBounds,getAdvancedLighting,migrateTokenHeight } from "./utils.js"; const MODULE_ID = "wall-height"; @@ -125,6 +125,10 @@ class WallHeightUtils{ return true; } + async migrateTokenHeight(){ + return await migrateTokenHeight(); + } + async migrateData(scene){ return await migrateData(scene); } @@ -227,7 +231,7 @@ export function registerWrappers() { const elevation = WallHeight.currentTokenElevation; if (elevation == null || !advancedVision) return wrapped(...args); const { top, bottom } = getWallBounds(wall); - if (!(elevation >= bottom && !(elevation - top >= 0))) return false; + if (!(elevation >= bottom && elevation <= top)) return false; return wrapped(...args); } diff --git a/scripts/utils.js b/scripts/utils.js index 51d5953..bffb5eb 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -103,4 +103,49 @@ export async function migrateData(scene){ ui.notifications.notify("Wall Height - Migrated " + updates.length + " walls to new Wall Height data structure in scene " + scene.name); console.log("Wall Height - Migrated " + updates.length + " walls to new Wall Height data structure in scene " + scene.name); return true; +} + +export async function migrateTokenHeight(){ + ui.notifications.notify("Wall Height - Migrating Token Height from Levels..."); + const scenes = Array.from(game.scenes); + for(let scene of scenes){ + await migrateTokenHeightInScene(scene); + } + const updates = []; + const actors = Array.from(game.actors); + for(const actor of actors){ + const oldTokenHeight = actor.data.token.flags?.levels?.tokenHeight + if (oldTokenHeight) { + const update = { + _id: actor.id, + "token.flags.wall-height.tokenHeight": oldTokenHeight, + }; + updates.push(update); + } + } + await Actor.updateDocuments(updates) + ui.notifications.notify("Wall Height - Migrated Token Height from Levels. You can migrate again with the provided macro if needeed."); + ui.notifications.warn("Global settings for Token Height need to be manually set in Wall Height module settings!", {permanent: true}); +} + +async function migrateTokenHeightInScene(scene){ + const tokens = Array.from(scene.tokens); + const updates = []; + for (const token of tokens) { + const oldTokenHeight = token.data.flags?.levels?.tokenHeight + if (oldTokenHeight) { + const update = { + _id: token.id, + flags: { + "wall-height": { + tokenHeight: oldTokenHeight, + }, + }, + }; + updates.push(update); + } + } + if(updates.length <= 0) return false; + await scene.updateEmbeddedDocuments("Token", updates); + return true; } \ No newline at end of file diff --git a/scripts/wall-height.js b/scripts/wall-height.js index ec26dfd..122195e 100644 --- a/scripts/wall-height.js +++ b/scripts/wall-height.js @@ -26,6 +26,10 @@ Hooks.once("init",()=>{ Hooks.once("ready", ()=>{ if(!game.user.isGM) return; if(game.settings.get(MODULE_ID, 'migrateOnStartup')) WallHeight.migrateAll(); + if(game.settings.get(MODULE_ID, 'migrateTokenHeight')) { + WallHeight.migrateTokenHeight(); + game.settings.set(MODULE_ID, 'migrateTokenHeight',false) + } }) Hooks.on("hoverWall",(wall, hovered)=>{ @@ -66,30 +70,30 @@ function registerSettings() { default: true }); - - game.settings.register(MODULE_ID, "defaultLosHeight", { - name: game.i18n.localize(`${MODULE_SCOPE}.settings.defaultLosHeight.name`), - hint: game.i18n.localize(`${MODULE_SCOPE}.settings.defaultLosHeight.hint`), - scope: "world", - config: true, - type: Number, - default: 6, - onChange: () => { - WallHeight.cacheSettings(); - }, - }); + game.settings.register(MODULE_ID, "autoLOSHeight", { + name: game.i18n.localize(`${MODULE_SCOPE}.settings.autoLOSHeight.name`), + hint: game.i18n.localize(`${MODULE_SCOPE}.settings.autoLOSHeight.hint`), + scope: "world", + config: true, + type: Boolean, + default: true, + onChange: () => { + WallHeight.cacheSettings(); + }, + }); - game.settings.register(MODULE_ID, "autoLOSHeight", { - name: game.i18n.localize(`${MODULE_SCOPE}.settings.autoLOSHeight.name`), - hint: game.i18n.localize(`${MODULE_SCOPE}.settings.autoLOSHeight.hint`), - scope: "world", - config: true, - type: Boolean, - default: false, - onChange: () => { - WallHeight.cacheSettings(); - }, - }); + + game.settings.register(MODULE_ID, "defaultLosHeight", { + name: game.i18n.localize(`${MODULE_SCOPE}.settings.defaultLosHeight.name`), + hint: game.i18n.localize(`${MODULE_SCOPE}.settings.defaultLosHeight.hint`), + scope: "world", + config: true, + type: Number, + default: 6, + onChange: () => { + WallHeight.cacheSettings(); + }, + }); game.settings.register(MODULE_ID, 'globalAdvancedLighting', { name: game.i18n.localize(`${MODULE_SCOPE}.settings.globalAdvancedLighting.name`), @@ -108,6 +112,13 @@ function registerSettings() { type: Boolean, default: true }); + + game.settings.register(MODULE_ID, 'migrateTokenHeight', { + scope: 'world', + config: false, + type: Boolean, + default: true + }); } Hooks.on("renderWallConfig", (app, html, data) => { @@ -225,7 +236,7 @@ Hooks.on("renderTokenConfig", (app, html, data) => { const distance = game.i18n.localize(`${MODULE_SCOPE}.distance`); let newHtml = `
- +