diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c578b5..76d96f5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # CHANGELOG +## [1.0.1] 2022-04-20 + +### CHANGED + +- Corrected some translations and improved code commenting + +### ADDED + +- Debug setting, so you can see the inner workings of the module and troubleshoot your scenes + ## [1.0.0] 2022-02-28 ### CHANGED diff --git a/languages/walls-have-ears.de.json b/languages/walls-have-ears.de.json index 5272b21..ab0b519 100755 --- a/languages/walls-have-ears.de.json +++ b/languages/walls-have-ears.de.json @@ -3,5 +3,7 @@ "WHE.settings.title": "Wände haben Ohren", "WHE.save": "Speichern", "WHE.settings_disable.title": "Geräuschdämpfung deaktivieren", - "WHE.settings_disable.hint": "Mit diesem Modul können Geräusche gedämpft werden, um das Eintauchen zu verbessern. Sie können diese Funktion jedoch aus Leistungsgründen deaktivieren." + "WHE.settings_disable.hint": "Mit diesem Modul können Geräusche gedämpft werden, um das Eintauchen zu verbessern. Sie können diese Funktion jedoch aus Leistungsgründen deaktivieren.", + "WHE.settings_debug.title": "Aktivieren Sie den Debug-Modus", + "WHE.settings_debug.hint": "Um Fehler in Ihren Szenen zu beheben, aktivieren Sie dies, um den Dämpfungsindex anzuzeigen, wenn Sie Ihre Token auswählen/verschieben." } diff --git a/languages/walls-have-ears.en.json b/languages/walls-have-ears.en.json index 2215b2f..c53d8a2 100755 --- a/languages/walls-have-ears.en.json +++ b/languages/walls-have-ears.en.json @@ -3,5 +3,7 @@ "WHE.settings.title": "Walls Have Ears", "WHE.save": "Save", "WHE.settings_disable.title": "Disable sound muffling", - "WHE.settings_disable.hint": "This module allows for sounds to be muffled for improved inmersion. But you can disable this feature for performance reasons." + "WHE.settings_disable.hint": "This module allows for sounds to be muffled for improved inmersion. But you can disable this feature for performance reasons.", + "WHE.settings_debug.title": "Enable Debug mode", + "WHE.settings_debug.hint": "In order to troubleshoot your scenes, enable this to see the muffling index when selecting/moving your tokens." } diff --git a/languages/walls-have-ears.es.json b/languages/walls-have-ears.es.json index 34dc210..b06d584 100755 --- a/languages/walls-have-ears.es.json +++ b/languages/walls-have-ears.es.json @@ -3,5 +3,7 @@ "WHE.settings.title": "Las paredes escuchan", "WHE.save": "Guardar", "WHE.settings_disable.title": "Desactivar la amortiguación del sonido", - "WHE.settings_disable.hint": "Este módulo permite amortiguar los sonidos para mejorar la inmersión. Pero puede desactivar esta función por motivos de rendimiento." -} \ No newline at end of file + "WHE.settings_disable.hint": "Este módulo permite amortiguar los sonidos para mejorar la inmersión. Pero puede desactivar esta función por motivos de rendimiento.", + "WHE.settings_debug.title": "Activar modo de depuración", + "WHE.settings_debug.hint": "Para resolver problemas en sus escenas, active esto para ver el índice amortiguación de sonidos cuando seleccione o mueva sus tokens." +} diff --git a/languages/walls-have-ears.fr.json b/languages/walls-have-ears.fr.json index 0668367..5ee027e 100755 --- a/languages/walls-have-ears.fr.json +++ b/languages/walls-have-ears.fr.json @@ -1,7 +1,9 @@ { - "WHE.walls-have-ears": "Les murs ont des oreilles", - "WHE.settings.title": "Les murs ont des oreilles", - "WHE.save": "Enregistrer", - "WHE.settings_disable.title": "Désactiver le silencieux", - "WHE.settings_disable.hint": "Ce module permet d'étouffer les sons pour une meilleure intégration. Mais vous pouvez désactiver cette fonctionnalité pour des raisons de performances." -} \ No newline at end of file + "WHE.walls-have-ears": "Les murs ont des oreilles", + "WHE.settings.title": "Les murs ont des oreilles", + "WHE.save": "Enregistrer", + "WHE.settings_disable.title": "Désactiver le silencieux", + "WHE.settings_disable.hint": "Ce module permet d'étouffer les sons pour une meilleure intégration. Mais vous pouvez désactiver cette fonctionnalité pour des raisons de performances.", + "WHE.settings_debug.title": "Activer le mode débogage", + "WHE.settings_debug.hint": "Afin de dépanner vos scènes, activez cette option pour voir l'indice d'étouffement lors de la sélection/déplacement de vos jetons." +} diff --git a/languages/walls-have-ears.pt-BR.json b/languages/walls-have-ears.pt-BR.json index 4284798..539ede3 100755 --- a/languages/walls-have-ears.pt-BR.json +++ b/languages/walls-have-ears.pt-BR.json @@ -1,7 +1,9 @@ { - "WHE.walls-have-ears": "As paredes têm orelhas", - "WHE.settings.title": "As paredes têm orelhas", - "WHE.save": "Salvar", - "WHE.settings_disable.title": "Desativar abafamento de som", - "WHE.settings_disable.hint": "Este módulo permite que os sons sejam abafados para uma melhor imersão. Mas você pode desativar esse recurso por motivos de desempenho." -} \ No newline at end of file + "WHE.walls-have-ears": "As paredes têm orelhas", + "WHE.settings.title": "As paredes têm orelhas", + "WHE.save": "Salvar", + "WHE.settings_disable.title": "Desativar abafamento de som", + "WHE.settings_disable.hint": "Este módulo permite que os sons sejam abafados para uma melhor imersão. Mas você pode desativar esse recurso por motivos de desempenho.", + "WHE.settings_debug.title": "Ativar o modo de depuração", + "WHE.settings_debug.hint": "Para solucionar problemas de suas cenas, ative isso para ver o índice de abafamento ao selecionar/mover seus tokens." +} diff --git a/module.json b/module.json index 513390f..cae57ff 100644 --- a/module.json +++ b/module.json @@ -6,7 +6,7 @@ "authors": [], "url": "https://github.com/SebaSOFT/walls-have-ears", "flags": {}, - "version": "1.0.0", + "version": "1.0.1", "minimumCoreVersion": "9", "compatibleCoreVersion": "9", "scripts": [], @@ -47,7 +47,7 @@ "dependencies": [], "socket": false, "manifest": "https://github.com/SebaSOFT/walls-have-ears/releases/latest/download/module.json", - "download": "https://github.com/SebaSOFT/walls-have-ears/releases/download/1.0.0/module.zip", + "download": "https://github.com/SebaSOFT/walls-have-ears/releases/download/1.0.1/module.zip", "protected": false, "coreTranslation": false } diff --git a/package.json b/package.json index 98369e3..881e848 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "walls-have-ears", - "version": "0.0.4", + "version": "1.0.1", "description": "A simple-as-possible module to muffle sounds that are behind a wall for a player.", "main": "scripts/main.js", "scripts": { diff --git a/scripts/WHE.js b/scripts/WHE.js index ae6fb02..54449dc 100644 --- a/scripts/WHE.js +++ b/scripts/WHE.js @@ -2,19 +2,23 @@ export default class WHE { static MODULE = 'walls-have-ears'; static SETTING_DISABLE = 'client-disable'; + static SETTING_DEBUG = 'client-debug'; static STR_SETTING_DISABLE_TITLE = 'WHE.settings_disable.title'; static STR_SETTING_DISABLE_HINT = 'WHE.settings_disable.hint'; + static STR_SETTING_DEBUG_TITLE = 'WHE.settings_debug.title'; + static STR_SETTING_DEBUG_HINT = 'WHE.settings_debug.hint'; static debug = false; /** * Gets a translated message text * @param msgKey the key that will be translated, see constants + * @param paramMap (Optional) a map of variables that should be in the translated string * @returns {string} the translated key or the key text */ - static getMessageText(msgKey) { - return game.i18n.localize(msgKey); + static getMessageText(msgKey, paramMap = null) { + return (!paramMap) ? game.i18n.localize(msgKey) : game.i18n.format(msgKey, paramMap); } /** diff --git a/scripts/main.js b/scripts/main.js index a4438f9..5496c20 100755 --- a/scripts/main.js +++ b/scripts/main.js @@ -3,6 +3,7 @@ import WHE from './WHE.js'; window.WHE = window.WHE || WHE; +let debugEnabled = false; let wallsSoundsDisabled = true; let listenerToken = null; @@ -29,10 +30,26 @@ Hooks.once('setup', function () { // Get User Options wallsSoundsDisabled = game.settings.get(WHE.MODULE, WHE.SETTING_DISABLE); + debugEnabled = game.settings.get(WHE.MODULE, WHE.SETTING_DEBUG); + WHE.debug = debugEnabled; WHE.logMessage('module setup finished'); }); +/* ------------------------------------ */ +// Settings changed +/* ------------------------------------ */ +Hooks.on('closeSettingsConfig', function () { + WHE.logMessage('updateToken called'); + + // Get User Options + wallsSoundsDisabled = game.settings.get(WHE.MODULE, WHE.SETTING_DISABLE); + debugEnabled = game.settings.get(WHE.MODULE, WHE.SETTING_DEBUG); + WHE.debug = debugEnabled; + + WHE.logMessage('settings reloaded'); +}); + /* ------------------------------------ */ // When ready /* ------------------------------------ */ @@ -77,13 +94,18 @@ Hooks.on('updateWall', (_token, _updateData, _options, _userId) => { /* ------------------------------------ */ Hooks.on('updateAmbientSound', (_ambientSound, _updateData, _options, _userId) => { WHE.logMessage('updateAmbientSound called'); + if (listenerToken) { doTheMuffling(); } }); -// If its a gamemaster, lets get the controlled token +/* ------------------------------------ */ +// When the user starts controlling a token +/* ------------------------------------ */ Hooks.on('controlToken', async (token, selected) => { + WHE.logMessage('controlToken called'); + if (!selected) { WHE.logMessage('No token selected but getting from user'); listenerToken = getActingToken({ @@ -106,7 +128,7 @@ Hooks.on('controlToken', async (token, selected) => { /** * This will create filter nodes and assign to global variables for reuse. * This could be changes in the future as some sounds or sound listening - * events may need different parameters depending the occasion + * events may need different parameters depending on the occasion * * @param context : AudioContext * @param muffling : int @@ -129,6 +151,10 @@ function getAudioMuffler(context, muffling) { return audioMuffler; } +/** + * Loops through the sounds in the scene and estimate if its audible and the eventual + * muffling index, after estimate that, applies the audio filter correspondingly + */ function doTheMuffling() { if (wallsSoundsDisabled) return; @@ -144,7 +170,7 @@ function doTheMuffling() { * @type {AmbientSound[]} */ const ambientSounds = game.canvas.sounds.placeables; - WHE.logMessage('The SOUNDS: ', ambientSounds); + WHE.logMessage('The sounds: ', ambientSounds); if (ambientSounds && ambientSounds.length > 0) { for (let i = 0; i < ambientSounds.length; i++) { const currentAmbientSound = ambientSounds[i]; @@ -155,7 +181,7 @@ function doTheMuffling() { //Added in 0.8.x for Darkness range setting if (!currentAmbientSound.isAudible) { - console.warn('WHE | Sound not Audible for some reason'); + console.warn('WHE | Sound not Audible for (probably is just turned off)'); continue; } if (!soundMediaSource.context) { @@ -170,7 +196,7 @@ function doTheMuffling() { }; const distanceToSound = canvas.grid.measureDistance(tokenPosition, soundPosition); - WHE.logMessage('WHE | Sound ' + i, soundMediaSource, currentSoundRadius, distanceToSound); + WHE.logMessage('Sound ' + i, soundMediaSource, currentSoundRadius, distanceToSound); if (currentSoundRadius < Math.floor(distanceToSound)) { continue; @@ -178,7 +204,6 @@ function doTheMuffling() { const muffleIndex = getMufflingIndex(soundPosition, tokenPosition); if (muffleIndex < 0) { - // clearSound(soundMediaSource.container.gainNode); WHE.logMessage('Sound ' + i, currentAmbientSound, soundMediaSource); continue; } @@ -198,21 +223,18 @@ function doTheMuffling() { clearSound(soundMediaSource.container.gainNode); } } else { - WHE.logMessage('Im FAR AWAY! and IS PLAYING'); - // clearSound(soundMediaSource.container.gainNode); - // continue; + WHE.logMessage('Sound is too far away!'); } } else { // Schedule on start to take into consideration the moment - // the user hasn-t yet interacted with the browser so sound is unavailable - WHE.logMessage('WIll muffle on start'); + // the user hasn't yet interacted with the browser so sound is unavailable + WHE.logMessage('WIll muffle on start if needed'); soundMediaSource.on('start', function (soundSource) { // Muffle as needed if (shouldBeMuffled) { injectFilterIfPossible(soundSource.container.gainNode, audioMuffler); } else { - WHE.logMessage('ON START Should not be muffled'); - // clearSound(soundSource.container.gainNode); + WHE.logMessage('Sound is starting but should not be muffled'); } }); } @@ -254,7 +276,15 @@ function clearSound(sourceNode) { sourceNode.connect(sourceNode.context.destination); } -// Get if ywo points have a wall +/** + * Ray casts the sound and the token and estimate a muffling index + * + * @param number x1 x of the token + * @param number y1 y of the token + * @param number x2 x of the sound + * @param number y2 y of the sound + * @returns number returns the muffling index or -1 if the sound shouldn't be heard + */ function getMufflingIndex({ x: x1, y: y1 }, { x: x2, y: y2 }) { const ray = new Ray({ x: x1, y: y1 }, { x: x2, y: y2 }); @@ -263,18 +293,18 @@ function getMufflingIndex({ x: x1, y: y1 }, { x: x2, y: y2 }) { mode: 'any', }); if (hasSoundOccluded) { - WHE.logMessage('NO ABLE TO HEAR'); + WHE.logMessage('This sound should not be heard (sound proof walls)'); return -1; } // If you dont see it, it's muffled - const sensesCollision = canvas.walls.checkCollision(ray, { + const sensesCollisions = canvas.walls.checkCollision(ray, { type: 'sight', mode: 'all', }); // Then again if terrain collissions exist, you are in the same room - const noTerrainSightCollisions = sensesCollision.filter((impactVertex) => { + const noTerrainSenseCollisions = sensesCollisions.filter((impactVertex) => { const wall = impactVertex?.edges?.first()?.isLimited; return !wall; }); @@ -285,13 +315,18 @@ function getMufflingIndex({ x: x1, y: y1 }, { x: x2, y: y2 }) { mode: 'all', }); + // Present the results + WHE.logMessage('Collision walls (MOVE):', moveCollisions.length); + WHE.logMessage('Collision walls (SENSE):', sensesCollisions.length); + WHE.logMessage('Collision walls (SENSE excl. terrain ):', noTerrainSenseCollisions.length); + // Estimating how much to muffle // See image: - const finalMuffling = Math.floor((noTerrainSightCollisions.length + moveCollisions.length) / 2); + const finalMuffling = Math.floor((noTerrainSenseCollisions.length + moveCollisions.length) / 2); - WHE.logMessage('MOVE SENSE Coll', moveCollisions, sensesCollision); // Account for ethereal walls - if (sensesCollision.length >= 1 && moveCollisions.length === 0) { + if (sensesCollisions.length >= 1 && moveCollisions.length === 0) { + WHE.logMessage('There is at least an ethereal wall'); return 0; } diff --git a/scripts/settings.js b/scripts/settings.js index 532c38b..eca7782 100644 --- a/scripts/settings.js +++ b/scripts/settings.js @@ -14,6 +14,16 @@ Hooks.once('init', async function () { default: false }); + game.settings.register(WHE.MODULE, WHE.SETTING_DEBUG, { + name: WHE.getMessageText(WHE.STR_SETTING_DEBUG_TITLE), + hint: WHE.getMessageText(WHE.STR_SETTING_DEBUG_HINT), + scope: 'client', + config: true, + type: Boolean, + default: false + }); + + // Register custom sheets (if any) // console.log('walls-have-ears | settings registration finished'); });