Here are some suggestions i find useful and put inside my random map as helper functions.

The "getDirection" function is a bit fuzzy but works (consistent with the unit placement orientation AFAIK).

Might be written more sober though.

I skipped checking the given operands in any way but that might be wise...

// Function to get the distance between 2 points function getDistance(point1, point2) {return Math.pow(Math.pow(point1[0] - point2[0], 2) + Math.pow(point1[1] - point2[1], 2), 1/2)}; // Function to get the distance between 2 points given in seperate coordinates function getDistanceXZ(x1, z1, x2, z2) {return getDistance([x1, z1], [x2, z2])}; // Function to get the direction from one point to another function getDirection(point1, point2) { var vector = [point2[0] - point1[0], point2[1] - point1[1]]; var output = Math.acos(vector[0]/getDistance(point1, point2)); if (vector[1] > 0) {output = PI + (PI - Math.acos(vector[0]/getDistance(point1, point2)))}; return (output + PI/2) % (2*PI); }; // Function to get the direction from one point to another given in seperate coordinates function getDirectionXZ(x1, z1, x2, z2) {return getDirection([x1, z1], [x2, z2])};