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.

119 lines
8.5 KiB

1 year ago
  1. function movementSpeed(token, type) {
  2. //handles speeds for non vehicles
  3. if (token.actor.type === "character" && type === 'land'){type = 'land-speed'}
  4. if (token.actor.type !== "vehicle"){
  5. let findSpeed = token?.actor?.system?.attributes?.speed?.otherSpeeds.find(e => e.type == type) ?? 0;
  6. if(findSpeed?.total !== undefined){
  7. return {speed: findSpeed?.total > 0 ? findSpeed?.total : parseFloat(findSpeed?.breakdown?.match(/\d+(\.\d+)?/)[0]), type: type} //if a matching speed if found returns it.
  8. } else if (token.actor.system.attributes?.speed?.total !== 0 && isNaN(token.actor.system.attributes?.speed?.total) == false){
  9. //If the speed in question wasn't found above, and the speed total isn't 0 (happens to some npcs who's speed is stored in value instead) returns the speed total. And the type, for NPCs that may be set as something other than land.
  10. return {speed: parseFloat(token.actor.system.attributes?.speed?.total) ?? 0, type: token.actor.system.attributes?.speed?.type ?? 'default' }
  11. } else {
  12. return {speed:Math.max([parseFloat(token.actor.system.attributes?.speed?.breakdown?.match(/\d+(\.\d+)?/)[0]), parseFloat(token.actor.system.attributes?.speed?.otherSpeeds[0]?.total), 0].filter(Number)), type: 'special' } //pulls out the speed for the value string in the event that the total was 0. In the event that both the total and value for the land speed are 0, falls back on the first other speed total, should one not exist speed will be 0.
  13. };
  14. //handles speeds for vehicles because they're different.
  15. } else if (token.actor.type === "vehicle"){
  16. return {speed:parseFloat(token.actor.system.details?.speed), type: 'default'}
  17. }
  18. };
  19. //This function handles determining the type of movment.
  20. function getMovementType(token){
  21. const tokenElevation = token?.document?.elevation; //Gives us a way to check if a token is flying
  22. var movementType = 'land';
  23. //This logic gate handles flight, burrowing and swimming, if the automatic movment switching is on.
  24. if (game.settings.get("pf2e-dragruler", "auto")) {
  25. if(tokenElevation > 0) {var movementType = 'fly'}; //if elevated fly
  26. if (tokenElevation < 0){var movementType = 'burrow'}; //if below ground burrow.
  27. if (game.modules.get("enhanced-terrain-layer")?.active){
  28. const tokenPosition = [token.document.x, token.document.y];
  29. if(canvas.terrain.terrainFromPixels(tokenPosition[0],tokenPosition[1])[0]?.document?.environment === 'aquatic' || canvas.terrain.terrainFromPixels(tokenPosition[0],tokenPosition[1])[0]?.document?.environment === 'water'|| canvas.terrain.terrainFromPixels(tokenPosition[0],tokenPosition[1])[0]?.document?.obstacle === 'water'){var movementType = 'swim'}
  30. } //switches to swim speed, if the token starts the movement in water or aquatic terrain.
  31. };
  32. //This logic gate handles flight and swimming, if the scene environment based movement switching is on.
  33. if (game.settings.get("pf2e-dragruler", "scene") === true && game.modules.get("enhanced-terrain-layer")?.active) {
  34. if(canvas.scene.getFlag('enhanced-terrain-layer', 'environment') === 'sky') {var movementType = 'fly'}; //checks if the scene is set to have a default environment of sky. If so, uses fly speed.
  35. if(canvas.scene.getFlag('enhanced-terrain-layer', 'environment') === 'aquatic'){var movementType = 'swim'}; //checks if the scene is set to have a default environment of aquatic. If so, uses swim speed.
  36. };
  37. if(token.actor.flags.pf2e?.movement?.burrowing === true){var movementType = 'burrow'} //switches to burrowing if the burrow effect is applied to the actor.
  38. if(token.actor.flags.pf2e?.movement?.climbing === true){var movementType = 'climb'} //switches to climbing if the climb effect is applied to the actor.
  39. if(token.actor.flags.pf2e?.movement?.swimming === true){var movementType = 'swim'} //switches to swimming if the swim effect is applied to the actor.
  40. if(token.actor.flags.pf2e?.movement?.flying === true){var movementType = 'fly'} //switches to flying if the fly effect is applied to the actor.
  41. if(token.actor.flags.pf2e?.movement?.walking === true){var movementType = 'land'}
  42. return movementType;
  43. }
  44. function conditionFacts (tokenConditions, conditionSlug) {
  45. let condition = tokenConditions.find(e => e.slug == conditionSlug)
  46. return {condition:condition?.slug, active:condition?.isActive, value: condition?.value }
  47. }
  48. //determines how many actions a token should start with.
  49. export function actionCount (token){
  50. let numactions = 3 //Set the base number of actions to the default 3.
  51. const conditions = token.actor.items.filter(item => item.type === 'condition')
  52. const stunned = conditionFacts(conditions, "stunned");
  53. const slowed = conditionFacts(conditions, "slowed");
  54. if(conditionFacts(conditions, "quickened").active){
  55. numactions = numactions + 1};
  56. // Self explanatory. If a token is quickened increases the number of actions.}
  57. if (slowed.active || stunned.active) {
  58. numactions = numactions - Math.max ((slowed.value ?? 0), (stunned.value ?? 0))}
  59. //if you've got the stunned or slowed condition reduces the number of actions you can take by the larger of the two values, since stunned overrides slowed, but actions lost to stunned count towards the slowed count.
  60. if (conditionFacts(conditions, "immobilized").active || conditionFacts(conditions, "paralyzed").active || conditionFacts(conditions, "petrified").active || conditionFacts(conditions, "unconscious").active) {
  61. numactions = 0
  62. //if you've got the immobilized, paralyzed or petrified condition sets the number of move actions you can take to 0. This also handles restrained as restrained gives a linked immobilized condition.
  63. }
  64. if (numactions < 0) {numactions = 0};
  65. //You can't have less than 0 actions, if you've managed to get to less than 0, due to being stunned 4 for example, set the number of actions you get to take to 0
  66. return numactions
  67. };
  68. //This function handles tracking how far a token has moved, allowing for drag ruler to consider movements less than a full movement speed as complete.
  69. export function movementTracking (token){
  70. const movementType = getMovementType(token);
  71. const baseSpeed = movementSpeed(token, movementType).speed; //gets the base speed for the token, based on current movement type.
  72. var usedActions = 0;
  73. if (game.combats.active && game.settings.get("pf2e-dragruler", "partialMovements") && game.settings.get("drag-ruler", "enableMovementHistory")){
  74. const combat = game.combats.active
  75. const combatant = combat.getCombatantByToken(token.id);
  76. var moveHistory = combatant.flags?.dragRuler?.passedWaypoints
  77. if (moveHistory == undefined){
  78. var A1 = baseSpeed;
  79. var A2 = baseSpeed*2;
  80. var A3 = baseSpeed*3;
  81. var A4 = baseSpeed*4;
  82. } else{
  83. const P1 = moveHistory[0]?.dragRulerVisitedSpaces[moveHistory[0]?.dragRulerVisitedSpaces?.length-1||0]?.distance;
  84. let moves1 = (P1-baseSpeed) > 0 ? 1:0;
  85. moves1=(P1-2*baseSpeed)>0 ? 2:moves1;
  86. const P2 = moveHistory[1]?.dragRulerVisitedSpaces[moveHistory[1]?.dragRulerVisitedSpaces?.length-1||0]?.distance;
  87. let moves2 = (P2-baseSpeed) > 0 ? 1:0;
  88. const P3 = moveHistory[2]?.dragRulerVisitedSpaces[moveHistory[2]?.dragRulerVisitedSpaces?.length-1||0]?.distance;
  89. const P4 = moveHistory[3]?.dragRulerVisitedSpaces[moveHistory[3]?.dragRulerVisitedSpaces?.length-1||0]?.distance;
  90. var A1 = Math.min(P1 || baseSpeed, baseSpeed);
  91. var A2 = Math.min(baseSpeed+A1, (P1-baseSpeed) > 0 ? P1:P2+A1 || baseSpeed*2)
  92. var A3 = Math.min(baseSpeed+A2, (P2-baseSpeed) > 0 ? P2+A1:P3+A2 || baseSpeed*3, (P1-baseSpeed*2) > 0 ? P1:baseSpeed*3, moves1==1?(P2||baseSpeed)+A2:baseSpeed*3)
  93. var A4 = Math.min(baseSpeed+A3, (P3-baseSpeed) > 0 ? P3+A2:P4+A3 || baseSpeed*4, (P2-baseSpeed*2) > 0 ? P2+A1:baseSpeed*4, (P1-baseSpeed*3) > 0 ? P1:baseSpeed*4, moves1==2?(P2||baseSpeed)+A2:baseSpeed*4, moves2==1?(P3||baseSpeed)+A3:baseSpeed*4)
  94. }
  95. }else{
  96. //sets the range for each movement as either how far the token moved for that action, or to how far the token moved for the previous action, plus the base speed.
  97. var A1 = baseSpeed;
  98. var A2 = baseSpeed*2;
  99. var A3 = baseSpeed*3;
  100. var A4 = baseSpeed*4;
  101. }game.settings.register("pf2e-dragruler", "offTurnMovement", {
  102. name: "Ignore Off Turn Movement",
  103. hint: "Requires movement history to be enabled. Automatically resets movement history at the start of each actor's turn in combat, meaning any movement performed off turn as a reaction won't effect the movement history, on your turn.",
  104. scope: "world",
  105. config: true,
  106. type: Boolean,
  107. default: false
  108. })
  109. return {A1: A1, A2: A2, A3: A3, A4: A4, usedActions:usedActions, type:movementType}
  110. };