This code will give you all the plots in the "fat cross" around a city. I assume this is what you mean by the radius? You'd have to alter it sligtly for cities with not enough culture to get the full cross.
Code:
pPlot = pCity.plot()
iPlotX = pPlot.getX()
iPlotY = pPlot.getY()
for iXLoop in range(iPlotX - 2, iPlotX + 3):
for iYLoop in range(iPlotY - 2, iPlotY + 3):
# Removes tiles not in city radius
if ((iXLoop != 2) and (iYLoop != 2)) or \
((iXLoop != -2) and (iYLoop != 2)) or \
((iXLoop != 2) and (iYLoop != -2)) or \
((iXLoop != -2) and (iYLoop != -2)):
plot = CyMap.getPlot(iXLoop, iYLoop)
Here's some pseudocode, though you'll probably have to look up the API references: (you'd also have to add a few more checks to ignore the corners of the square in order to get the "fat cross" radius, but the idea is the same)
Code:
def isEnemyInRadius(city, radius):
for rx in range(-radius, radius):
for ry in range(-radius, radius):
x = city.x + rx
y = city.y + ry
plot = gc.getMap().getPlot(x, y)
units = plot.getUnits()
for u in units:
player = u.getPlayer()
if ( player.isAtWar(me) ):
return true
Edit: Just noticed that someone beat me to it, so between these two posts, you should have all you need
pCity = # the city you want to check
player=PyPlayer(CyGame().getActivePlayer())
for i in range(gc.getMAX_PLAYERS()):
if player.getTeam().isAtWar(gc.getPlayer(i).getTeam()):
if pCity.isVisible(gc.getPlayer(i).getTeam(), false):
# enemy in sight of pCity
# I'm not sure if this includes enemy cities
What also should work is :
Code:
pCity = # the city you want to check
pPlot = pCity.plot()
for i in range(gc.getMAX_PLAYERS()):
if pPlot.isVisibleEnemyUnit(gc.getPlayer(i)):
# enemy in sight of the city plot
pCity = # the city you want to check
player=PyPlayer(CyGame().getActivePlayer())
for i in range(gc.getMAX_PLAYERS()):
if player.getTeam().isAtWar(gc.getPlayer(i).getTeam()):
if pCity.isVisible(gc.getPlayer(i).getTeam(), false):
# enemy in sight of pCity
# I'm not sure if this includes enemy cities
What also should work is :
Code:
pCity = # the city you want to check
pPlot = pCity.plot()
for i in range(gc.getMAX_PLAYERS()):
if pPlot.isVisibleEnemyUnit(gc.getPlayer(i)):
# enemy in sight of the city plot
Hmmm... can you explain how the isVisible tag works? If this works then it doesn't seem to work the way I would expect it to. I would have thought it would return true if a unit sitting on that plot could see an enemy unit, rather then anything to do with city radii.
It works that way, that it returns true, if the i-player object is visbile to the pCity object.
In this special case "pCity.isVisible(gc.getPlayer(i).getTeam(), false)" does check, if there is any object of the player (or better of the team he belongs to), is within the visible area of the city. As I said, this may include also a city of the player.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.