@Daemian:
Let me offer some more insight and advice about how to calculate distances in 3D space:
You basically have two 3D vectors that tell you the location of each entity
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)
To get the vector that represents the distance between them, you simply need to calculate the (locTo - locFrom) vector, and get its length.
The subtraction part is easy:
locDelta = (locTo - locFrom) = (x2 - x1, y2 - y1, z2 - z1) = (x3, y3, z3)
(That corresponds the r-r' vector in the image below.)
Finally, you get the length by calculating
dist = Sqrt(x3^2 + y3^2 + z3^2)
Where ^2 stands for squared.
(See the image below to see why the length of a vector is calculated in this way - it's just the Pythagorean theorem.)
Note that there's no need for absolute value, since squared quantities are always positive.
What you're doing will generally not give you the correct distance - you also start with two vectors:
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)
But then you do this (I use
|| to denote abs value):
dist1 = Sqrt(|y1|^2 + |Sqrt(|x1|^2 + |z1|^2)|^2);
which is basically the same as this, since you take the square of the square root:
dist1 = Sqrt(x1^2 + y1^2 + z1^2);
Which is the distance
from the origin to the first entity.
Then you calculate
dist2 = Sqrt(|y2|^2 + |Sqrt(|x2|^2 + |z2|^2)|^2);
which equivalent to:
dist2 = Sqrt(x2^2 + y2^2 + z2^2);
That's the distance
from the origin to the second entity.
With that, you have two distances, without any information about the directions.
Then you do this:
dist = Max(dist1, dist2) - Min(dist1, dist2)
which is the same as
dist = |dist2 - dist1|
This will only give you the amount their distances differ from the origin, which will match the actual distance between the two objects only if they are on the same line (ray) from the origin. For example, if they are at different angles with respect to the origin, but at the same distance from the origin, the result would be 0, even though there's a non-zero distance between the objects themselves.
BTW, when programming/scripting, if speed is important, you want to do as few Sqrt operations as possible, because these are computationally pretty expensive; on the other hand, + and * are relatively cheep. Also, it's the game will take less time to calculate a*a then MathPow(a, 2), so if it's just the square, people often write a*a, and use the math function for higher powers.
So, with regard to all that, the code should look something like this:
float GetEntitiesDistance ( string &in fromEntity, string &in toEntity )
{
float x1 = GetEntityPosX( fromEntity );
float y1 = GetEntityPosY( fromEntity );
float z1 = GetEntityPosZ( fromEntity );
float x2 = GetEntityPosX( toEntity );
float y2 = GetEntityPosY( toEntity );
float z2 = GetEntityPosZ( toEntity );
float x3 = x2 - x1;
float y3 = y2 - y1;
float z3 = z2 - z1;
float distance = MathSqrt(x3*x3 + y3*y3 + z3*z3);
return distance;
}