# Distances in RAND

Discussion in 'Rhye's and Fall RAND' started by onedreamer, Aug 7, 2010.

1. ### onedreamerDragon

Joined:
Oct 21, 2004
Messages:
6,580
Location:
Torino - Italia
Distances for UHV conditions in RAND are calculated on one axis only. The highest distance on the x or y axis will be taken into account as the distance between two cities (normally a city and the current capital). If you found a city 7 tiles east, 4 north from Moscow, distance will be 7 and it will not count towards the UHV condition. This is RAND's formula:

Code:
```def calculateDistance(self, x1, y1, x2, y2):
dx = abs(x2-x1)
dy = abs(y2-y1)
return max(dx, dy)
```
But there is already a function in Civ4 to calculate plot distances that does take into account both the x and y axis. Two diagonal moves will count 3, the first will count 1 and the second will count 2, just like in Dungeons&Dragons. Here's the formula:

Code:
```inline int plotDistance(int iX1, int iY1, int iX2, int iY2)
{
iDX = xDistance(iX1, iX2);
iDY = yDistance(iY1, iY2);
return (std::max(iDX, iDY) + (std::min(iDX, iDY) / 2));
}
```
In the example above, distance would be 9 (7+ 4/2). plotDistance is used to calculate Trade Routes base values.

What do you think about using the second formula?

2. ### Wessel V1Emperor

Joined:
Sep 10, 2007
Messages:
1,443
Location:
Hamlet south of Amsterdam
It's better but not perfect either. As you know, the actual distance would be (7^2 + 2^2)/2 = 7.3 (I believe, without a calculator), which is quite far from 9. So, the distance is shaped like a 45 degrees tilted square, in stead of a circle. It is easier for the player though.

3. ### sercer88Emperor

Joined:
Oct 1, 2006
Messages:
1,446
Location:
MO, USA
um, sorry to be nit-picking, but it's the square root of 7^2 + 4^2, which is just over 8... still not nine, but nine is actually closer...

Code:
```inline int plotDistance(int iX1, int iY1, int iX2, int iY2)
{
iDX = xDistance(iX1, iX2);
iDY = yDistance(iY1, iY2);
[U]return (std::max(iDX, iDY) + (std::min(iDX, iDY) / 3));[/U]
}```
instead... or does the code need to return a number ending in x.0 or x.5?
If not, then we could actually use the REAL formula for distance, rather than some rough calculation.

In this example, the distance returned would be 8.3

4. ### Wessel V1Emperor

Joined:
Sep 10, 2007
Messages:
1,443
Location:
Hamlet south of Amsterdam
Of course you are right, just missed the number. Square root, that was the term I was looking for, thanks.

5. ### Rhye's and Fall creator

Joined:
May 23, 2001
Messages:
9,753
Location:
Japan / South America
the meaning of that function is plot distance. You can reach the city in 7 moves. I don't need any other value

6. ### onedreamerDragon

Joined:
Oct 21, 2004
Messages:
6,580
Location:
Torino - Italia
plotDistance is different, what is currently in use is movement distance.
In terms of distances between cities for calculations of trade routes values and maintenance, plotdistance is used, not movement distance. So I thought it should be used for the UHV conditions as well.