The Great Apple
Big Cheese
The function I use to return the shortest distance to the nearest city is as follows:
It will return the nearest distance from the given plot to the nearest city of the given player in plots. If the nearest city doesn't exist it returns -1, and you can handle this however you like.
Comparing it to Kael's method, I'd say it was probably better at handling larger distances. There are also X-Y wrapping issues with Kael's code (it won't pick up a city if it has to wrap over the edge of the map).
BTW: AI is the tough one. You'd have to fiddle with the SDK to get it to avoid distances which are too far away from a city.
Now... the other question about path blockages. You'd have to use a much cleverer funtion for finding the shortest distance to the nearest city - the best way to do it would be to use a proper path-finding algorithm. For this, I'd suggest you take a look at 12monkeys' A* Pathfinder. It might take quite a bit of work to convert it to do what you want it to though.
Oh, and Kael - I think I posted a piece of code on the Python sample code thread which is a function that is similar to CyMap().plot(), but takes wrapping into account, if you are interested.
Code:
# Gets the distance to the nearest city
def getNearestCityDist(self, pPlot, iPlayer):
cityList = PyPlayer(iPlayer).getCityList()
lCity = []
for city in cityList:
cPlot = city.plot()
iDist = self.getDistance(cPlot, pPlot)
lCity.append(iDist)
if lCity == []:
return -1
else:
return min(lCity)
# Simple function to find the shortest distance between two plots
def getDistance(self, plot1, plot2):
iX = abs(plot1.getX() - plot2.getX())
iY = abs(plot1.getY() - plot2.getY())
if CyMap().isWrapX():
if iX > CyMap().getGridWidth() / 2:
iX = CyMap().getGridWidth() - iX
if CyMap().isWrapY():
if iY > CyMap().getGridHeight() / 2:
iY = CyMap().getGridHeight() - iY
return max(iX, iY)
Comparing it to Kael's method, I'd say it was probably better at handling larger distances. There are also X-Y wrapping issues with Kael's code (it won't pick up a city if it has to wrap over the edge of the map).
BTW: AI is the tough one. You'd have to fiddle with the SDK to get it to avoid distances which are too far away from a city.
Now... the other question about path blockages. You'd have to use a much cleverer funtion for finding the shortest distance to the nearest city - the best way to do it would be to use a proper path-finding algorithm. For this, I'd suggest you take a look at 12monkeys' A* Pathfinder. It might take quite a bit of work to convert it to do what you want it to though.
Oh, and Kael - I think I posted a piece of code on the Python sample code thread which is a function that is similar to CyMap().plot(), but takes wrapping into account, if you are interested.