Skip to content

Commit

Permalink
beta new math calculation...
Browse files Browse the repository at this point in the history
  • Loading branch information
p4535992 committed Nov 22, 2023
1 parent bd09667 commit c7e9b32
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 219 deletions.
106 changes: 68 additions & 38 deletions src/scripts/ArmsReachHelper.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { checkElevation, error, getElevationPlaceableObject, warn } from "./lib/lib.mjs";
import { checkElevation, error, getElevationPlaceableObject, getTokenHeightPatched, warn } from "./lib/lib.mjs";
import { ArmsreachData } from "./ArmsReachModels.mjs";
import CONSTANTS from "./constants.mjs";
import { GeometricUtils } from "./lib/GeometricTokenUtils.mjs";

/**
* @href https://stackoverflow.com/questions/30368632/calculate-distance-on-a-grid-between-2-points
Expand All @@ -16,6 +15,7 @@ export const computeDistanceBetweenCoordinates = function (armsreachData, select
const hPlaceable = armsreachData.h;
const centerX = armsreachData.centerX;
const centerY = armsreachData.centerY;
const centerZ = armsreachData.centerZ;
const placeableObjectData = armsreachData.placeableObjectData;

const unitSize = canvas.dimensions.distance; //canvas.grid?.grid?.options.dimensions.distance;
Expand All @@ -34,6 +34,7 @@ export const computeDistanceBetweenCoordinates = function (armsreachData, select
id: armsreachData.id,
centerX: centerX,
centerY: centerY,
centerZ: centerZ,
placeableObjectData: placeableObjectData,
});
// TODO TO TEST
Expand Down Expand Up @@ -506,6 +507,7 @@ export const getPlaceableDoorCenter = function (placeable) {
const documentName = placeable?.wall.document ? placeable?.wall.document.documentName : placeable.documentName;
const centerX = placeable.center ? placeable.center.x : x;
const centerY = placeable.center ? placeable.center.y : y;
const centerZ = placeable instanceof Token ? getTokenHeightPatched(placeable) : placeable.z;
const placeableObjectData = placeable?.wall.document ? placeable?.wall.document : placeable;
return {
x: x,
Expand All @@ -516,6 +518,7 @@ export const getPlaceableDoorCenter = function (placeable) {
id: id,
centerX: centerX,
centerY: centerY,
centerZ: centerZ,
placeableObjectData: placeableObjectData,
};
};
Expand All @@ -534,6 +537,7 @@ export const getPlaceableCenter = function (placeable) {
const documentName = placeable?.document ? placeable?.document.documentName : placeable.documentName;
const centerX = placeable.center ? placeable.center.x : x;
const centerY = placeable.center ? placeable.center.y : y;
const centerZ = placeable instanceof Token ? getTokenHeightPatched(placeable) : placeable.z;
const placeableObjectData = placeable.document ? placeable.document : placeable;
return {
x: x,
Expand All @@ -544,6 +548,7 @@ export const getPlaceableCenter = function (placeable) {
id: id,
centerX: centerX,
centerY: centerY,
centerZ: centerZ,
placeableObjectData: placeableObjectData,
};
};
Expand Down Expand Up @@ -693,6 +698,7 @@ export const globalInteractionDistanceUniversal = function (placeableObjectSourc
};

function grids_between_placeable_and_placeable(aArmsReachData, bArmsReachData) {
// TODO not sure about this....
return Math.floor(distance_between_placeable_rect(aArmsReachData, bArmsReachData) / canvas.grid?.size) + 1;
}

Expand Down Expand Up @@ -734,63 +740,87 @@ function distance_between_placeable_rect(p1ArmsReachData, p2ArmsReachData) {
}

function units_between_placeable_and_placeable(aArmsReachData, bArmsReachData) {
// const range = canvas.lighting?.globalLight ? Infinity : sourceToken.vision.radius;
// const range = sourceToken.vision.radius;
// if (range === 0) return false;
// if (range === Infinity) return true;
const tokensSizeAdjust = (Math.min(bArmsReachData.w, bArmsReachData.h) || 0) / Math.SQRT2;
let dist =
(getUnitTokenDistUniversal(aArmsReachData, bArmsReachData) * canvas.dimensions?.size) /
canvas.dimensions?.distance -
const tokensSizeAdjust =
bArmsReachData instanceof Token ? (Math.min(bArmsReachData.w, bArmsReachData.h) || 0) / Math.SQRT2 : 0;
const dist =
(getUnitTokenDistUniversal(aArmsReachData, bArmsReachData) * canvas.dimensions.size) / canvas.dimensions.distance -
tokensSizeAdjust;
// return dist <= range;
const unitSize = canvas.dimensions?.distance || 5;
const unitGridSize = canvas.grid?.size || 50;
dist = (Math.floor(dist) / unitGridSize) * unitSize;
// const unitSize = canvas.dimensions?.distance || 5;
// const unitGridSize = canvas.grid?.size || 50;
// dist = (Math.floor(dist) / unitGridSize) * unitSize;
return dist;
}

function getUnitTokenDistUniversal(aArmsReachData, bArmsReachData) {
const unitsToPixel = canvas.dimensions?.size / canvas.dimensions?.distance;
const x1 = aArmsReachData.centerX;
const y1 = aArmsReachData.centerY;
const z1 = getElevationPlaceableObject(aArmsReachData.placeableObjectData) * unitsToPixel;
const z1 = aArmsReachData.centerZ;
const x2 = bArmsReachData.centerX;
const y2 = bArmsReachData.centerY;
const z2 = getElevationPlaceableObject(bArmsReachData.placeableObjectData) * unitsToPixel;
const d = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) + Math.pow(z2 - z1, 2)) / unitsToPixel;
return d;
const z2 = bArmsReachData.centerZ;
// Add support for 3D Preview Model
if (game.Levels3DPreview?._active) {
const placeable1Vector = {
x: x1,
y: y1,
z: z1 * unitsToPixel,
};

const placeable2Vector = {
x: x2,
y: y2,
z: z2 * unitsToPixel,
};
const d = Math.hypot(
placeable1Vector.x - placeable2Vector.x,
placeable1Vector.y - placeable2Vector.y,
placeable1Vector.z - placeable2Vector.z
);
return d;
} else {
const d =
Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) + Math.pow((z2 - z1) * unitsToPixel, 2)) / unitsToPixel;
return d;
}
}

// ================================================

function getUnitTokenDist(token, placeableObjectTargetArmsReachData) {
const unitsToPixel = canvas.dimensions?.size / canvas.dimensions?.distance;
const x1 = token.center.x;
const y1 = token.center.y;
const z1 = getElevationPlaceableObject(token) * unitsToPixel;
const x1 = token.vision.x;
const y1 = token.vision.y;
const z1 = getTokenHeightPatched(token);
const x2 = placeableObjectTargetArmsReachData.centerX;
const y2 = placeableObjectTargetArmsReachData.centerY;
const z2 = getElevationPlaceableObject(placeableObjectTargetArmsReachData.placeableObjectData) * unitsToPixel;
const d = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) + Math.pow(z2 - z1, 2)) / unitsToPixel;
return d;
}

/**
* Find out if a token is in the range of a particular object
* @param {Object} token - a token
* @param {Object} objectTargetPlaceableObject - a tile/drawing/light/note
* @returns {Boolean} - true if in range, false if not
**/
export function isTokenInRange(objectSourcePlaceableObject, objectTargetPlaceableObject) {
if (game.modules.get("levels")?.active) {
let rangeTop = objectTargetPlaceableObject.document.getFlag("levels", "rangeTop");
let rangeBottom = objectTargetPlaceableObject.document.getFlag("levels", "rangeBottom");
if (!rangeTop && rangeTop !== 0) rangeTop = Infinity;
if (!rangeBottom && rangeBottom !== 0) rangeBottom = -Infinity;
const elevation = getElevationPlaceableObject(objectSourcePlaceableObject);
return elevation <= rangeTop && elevation >= rangeBottom;
const z2 = placeableObjectTargetArmsReachData.centerZ;
// Add support for 3D Preview Model
if (game.Levels3DPreview?._active) {
const placeable1Vector = {
x: x1,
y: y1,
z: z1 * unitsToPixel,
};

const placeable2Vector = {
x: x2,
y: y2,
z: z2 * unitsToPixel,
};
const d = Math.hypot(
placeable1Vector.x - placeable2Vector.x,
placeable1Vector.y - placeable2Vector.y,
placeable1Vector.z - placeable2Vector.z
);
return d;
} else {
// TODO maybe some other integration
return true;
const d =
Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) + Math.pow((z2 - z1) * unitsToPixel, 2)) / unitsToPixel;
return d;
}
}
2 changes: 2 additions & 0 deletions src/scripts/ArmsReachModels.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export class ArmsreachData {
centerX = 0;
// number:
centerY = 0;
// number
centerZ = 0;
// any:
placeableObjectData = {};
}
Loading

0 comments on commit c7e9b32

Please sign in to comment.