View Full Version : Python Testing Distance


ClassicThunder
Jan 05, 2007, 08:31 PM
I need to make sure that an area has a plot that is over 8 spaces away from the starting plot. How should I do this? I thought of getting every x and y value of every plot then testing it against the starting plot but I can't figure out how to get the indexes of the plots in a certain area.

Any help is greatly appreciated.

ollj
Jan 06, 2007, 01:29 AM
Theres a TON of distance functions in the CvGamecore.dll .
Checking every single plot for distance to one plot is unnecessarily slow especially because every CyPlot has his own x and y value.

check CyPlot and CyMap python classes (http://civilization4.net/files/modding/PythonAPI_v160/)
CyPlot:
BOOL at(INT iX, INT iY)
If the CyPlot exist it exists at (iX:iY)
...Should be easy to find the air distance between 2 coordinates, checking for x and y broder-wrapping needs two IFs.

For a basic approach try...
CyMap:
INT calculatePathDistance(CyPlot pSource, CyPlot pDest)
finds the shortest passable path between two CyPlots and returns its length, or returns -1 if no such path exists. Note: the path must be all-land or all-water.

Someone might rewrite this function or the function that calculates maintenance cost from capital city distance.

Lord Olleus
Jan 06, 2007, 11:06 AM
This is slightly off topic, but this is a function I wrote which finds the shortest distance between plotA and plotB, and then returns every plot on that line. It ignores all terain.

def PathList (pStartingPlot, pEndPlot):
iX = pStartingPlot.getX()
iY = pStartingPlot.getY()
jX = pEndPlot.getX()
jY = pEndPlot.getY()
PathList = []
pMap = gc.getMap()
PathList.append(pStartingPlot)

while iX != jX or iY != jY:
if iX > jX:
iX -= 1

elif iX < jX:
iX += 1

if iY > jY:
iY -= 1

elif iY < jY:
iY += 1

pLoopPlot = pMap.plot(iX, iY)
PathList.append(pLoopPlot)

return PathList


</self advertising>