All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.8 KiB

1 year ago
  1. import { MODULE_SCOPE, TOP_KEY, BOTTOM_KEY } from "./const.js";
  2. export function getTokenLOSheight(token) {
  3. let losDiff;
  4. let divideBy = WallHeight._isLevelsAutoCover && token.document.flags.levelsautocover?.ducking ? 3 : 1;
  5. if (WallHeight._autoLosHeight) {
  6. losDiff =
  7. token.document.flags[MODULE_SCOPE]?.tokenHeight ||
  8. canvas.scene.dimensions.distance *
  9. Math.max(token.document.width, token.document.height) *
  10. ((Math.abs(token.document.texture.scaleX) + Math.abs(token.document.texture.scaleY)) / 2);
  11. } else {
  12. losDiff = token.document.flags[MODULE_SCOPE]?.tokenHeight || WallHeight._defaultTokenHeight;
  13. }
  14. return token.document.elevation + losDiff / divideBy;
  15. }
  16. export function getAdvancedLighting(document){
  17. return game.settings.get(MODULE_SCOPE, 'globalAdvancedLighting') || document.getFlag(MODULE_SCOPE, "advancedLighting")
  18. }
  19. export function getWallBounds(wall) {
  20. if(wall.document) wall = wall.document;
  21. const top = wall.flags[MODULE_SCOPE]?.[TOP_KEY] ?? Infinity;
  22. const bottom = wall.flags[MODULE_SCOPE]?.[BOTTOM_KEY] ?? -Infinity;
  23. return { top, bottom }
  24. }
  25. export function getLevelsBounds(document){
  26. const top = document.flags?.levels?.rangeTop ?? Infinity;
  27. const bottom = document.flags?.levels?.rangeBottom ?? -Infinity;
  28. return { top, bottom }
  29. }
  30. export function getSceneSettings(scene) {
  31. let advancedVision = scene.flags[MODULE_SCOPE]?.advancedVision ?? true;
  32. return {advancedVision};
  33. }
  34. export async function _old_migrateData(scene){
  35. if(!scene) scene = canvas.scene;
  36. const walls = Array.from(scene.walls);
  37. const updates = [];
  38. for (const wall of walls) {
  39. const oldTop = wall.document.flags?.wallHeight?.wallHeightTop;
  40. const oldBottom = wall.document.flags?.wallHeight?.wallHeightBottom;
  41. if ((oldTop !== null && oldTop !== undefined) || (oldBottom !== null && oldBottom !== undefined)) {
  42. const update = {
  43. _id: wall.id,
  44. flags: {
  45. "wall-height": {
  46. top: oldTop,
  47. bottom: oldBottom,
  48. },
  49. "-=wallHeight": null
  50. },
  51. };
  52. updates.push(update);
  53. }
  54. }
  55. if(updates.length <= 0) return false;
  56. await scene.updateEmbeddedDocuments("Wall", updates);
  57. console.log("Wall Height - Migrated " + updates.length + " walls to new Wall Height data structure in scene " + scene.name);
  58. return true;
  59. }
  60. export async function migrateData(scene){
  61. if(!scene) scene = canvas.scene;
  62. const walls = Array.from(scene.walls);
  63. const updates = [];
  64. for (const wall of walls) {
  65. const oldTop = wall.flags?.wallHeight?.wallHeightTop;
  66. const oldBottom = wall.flags?.wallHeight?.wallHeightBottom;
  67. if ((oldTop !== null && oldTop !== undefined) || (oldBottom !== null && oldBottom !== undefined)) {
  68. const update = {
  69. _id: wall.id,
  70. flags: {
  71. "wall-height": {
  72. top: oldTop,
  73. bottom: oldBottom,
  74. },
  75. "-=wallHeight": null
  76. },
  77. };
  78. if(wall.flags['token-attacher']){
  79. const oldOffsetTop = wall.flags?.['token-attacher']?.offset?.elevation?.flags?.wallHeight?.wallHeightTop;
  80. const oldOffsetBottom = wall.flags?.['token-attacher']?.offset?.elevation?.flags?.wallHeight?.wallHeightBottom;
  81. if ((oldTop !== null && oldTop !== undefined) || (oldBottom !== null && oldBottom !== undefined)) {
  82. setProperty(update, `flags.token-attacher.offset.elevation.flags.wall-height`,{
  83. top: oldOffsetTop,
  84. bottom: oldOffsetBottom
  85. });
  86. setProperty(update, `flags.token-attacher.offset.elevation.flags.-=wallHeight`, null);
  87. }
  88. }
  89. updates.push(update);
  90. }
  91. }
  92. if(updates.length <= 0) return false;
  93. await scene.updateEmbeddedDocuments("Wall", updates,{'token-attacher': {update:true}});
  94. ui.notifications.notify("Wall Height - Migrated " + updates.length + " walls to new Wall Height data structure in scene " + scene.name);
  95. console.log("Wall Height - Migrated " + updates.length + " walls to new Wall Height data structure in scene " + scene.name);
  96. return true;
  97. }
  98. export async function migrateTokenHeight(){
  99. ui.notifications.notify("Wall Height - Migrating Token Height from Levels...");
  100. const scenes = Array.from(game.scenes);
  101. for(let scene of scenes){
  102. await migrateTokenHeightInScene(scene);
  103. }
  104. const updates = [];
  105. const actors = Array.from(game.actors);
  106. for(const actor of actors){
  107. const oldTokenHeight = actor.document.token.flags?.levels?.tokenHeight
  108. if (oldTokenHeight) {
  109. const update = {
  110. _id: actor.id,
  111. "token.flags.wall-height.tokenHeight": oldTokenHeight,
  112. };
  113. updates.push(update);
  114. }
  115. }
  116. await Actor.updateDocuments(updates)
  117. ui.notifications.notify("Wall Height - Migrated Token Height from Levels. You can migrate again with the provided macro if needeed.");
  118. ui.notifications.warn("Global settings for Token Height need to be manually set in Wall Height module settings!", {permanent: true});
  119. }
  120. async function migrateTokenHeightInScene(scene){
  121. const tokens = Array.from(scene.tokens);
  122. const updates = [];
  123. for (const token of tokens) {
  124. const oldTokenHeight = token.document.flags?.levels?.tokenHeight
  125. if (oldTokenHeight) {
  126. const update = {
  127. _id: token.id,
  128. flags: {
  129. "wall-height": {
  130. tokenHeight: oldTokenHeight,
  131. },
  132. },
  133. };
  134. updates.push(update);
  135. }
  136. }
  137. if(updates.length <= 0) return false;
  138. await scene.updateEmbeddedDocuments("Token", updates);
  139. return true;
  140. }