Umm... Another quick Python question

RogerBacon

King
Joined
Nov 16, 2003
Messages
649
What is the best way to find out which uints are in a city? Currently I plan to use onCityDoTurn. Here is the rest of the logic:

unitList = iPlayer.getUnitList()
for c in range(len(unitList)):
[tab]unit = unitList[c]
[tab]#todo: get city's plot x,y and compare it to each unit's x,y

If this is a good way to do it then I only need help with the syntax for the last line. If I'm totally off track then any suggestions on a better way would be appreciated.

Roger Bacon
 
Your better off getting the plot the city is on and then doing a plot.getUnits() to loop through the units. Check out: http://civilization4.net/files/modding/PythonAPI/index2.html for all the available functions.

I'm assuming you want to know all the units in a speciifc city. If you want to know all the units that are in any city your way seems as good as any other (though I don't know if that syntax is correct or not).
 
Yes, I want to find all of the units in a city. Actually, I want to do that for each city on the map, one by one.

I found this function:
CyPlot getCityIndexPlot(INT iIndex)
for CyCity objects. I assume that will give me the Plot and then I can get the x and y from that plot and compare it to the x and y from each unit. However, I don't know what I need to pass as iIndex.

[Roger Bacon

[Edit]

OK, I have this:
unitList = iPlayer.getUnitList()
for c in range(len(unitList)):
[tab]unit = unitList[c]
[tab]thePlot = pCity.getCityIndexPlot(this)
[tab]#get city's plot x,y and compare it to each unit's x,y
[tab]if ((thePlot.getX() == unit.getX()) and ((thePlot.getY() == unit.getY())):

but I get this error
File "Shogun", line 42
if ((thePlot.getX() == unit.getX()) and ((thePlot.getY() == unit.getY())):
^
SyntaxError: invalid syntax

Any idea why? .getY is a member of CyPlot so I don't see the problem.
 
RogerBacon said:
Yes, I want to find all of the units in a city. Actually, I want to do that for each city on the map, one by one.
Hmm... Are you trying to do a new screen like Civ3 military advisor, listing all units in each city? It's a good idea. If it isn't your idea, then someone can take this idea and try to mod it :goodjob:.
 
OK, it looks like

CyPlot getCityIndexPlot(INT iIndex)

was changed in the last patch to:

getCityIndexPlot(class CyCity {lvalue}, int)

I still have no idea what to put in for class CyCity {lvalue}, int. :(

I didn't see any getUnits() for CyPlot so I will just cycle through all of the units for now. I'll optimize it later if it runs slow.

code so far:
unitList = iPlayer.getUnitList()
for c in range(len(unitList)):
[tab]unit = unitList[c]
[tab]thePlot = pCity.getCityIndexPlot()
[tab]if (thePlot.isCity()):
[tab][tab]#get city's plot x,y and compare it to each unit's x,y
[tab][tab]if ((thePlot.getX() == unit.getX()) and (thePlot.getY() == unit.getY())):

Ramalhão: no, I'm not making a new screen. Nice idea though.

Roger Bacon
 
Code:
for iPlayer in range(gc.getMAX_PLAYERS()):
 pPlayer = gc.getPlayer(iPlayer)
 if (pPlayer.isAlive()):
  for iCity in range(pPlayer.getNumCities()):
   pCity = pPlayer.getCity(iCity)
   for iUnit in range(pCity.plot().getNumUnits):
    pUnit = pCity.plot().getUnit(iUnit)
    # do whatever else you need to do

Should work.

Bh
 
Bhruic said:
Code:
for iPlayer in range(gc.getMAX_PLAYERS()):
 pPlayer = gc.getPlayer(iPlayer)
 if (pPlayer.isAlive()):
  for iCity in range(pPlayer.getNumCities()):
   pCity = pPlayer.getCity(iCity)
   for iUnit in range(pCity.plot().getNumUnits):
    pUnit = pCity.plot().getUnit(iUnit)
    # do whatever else you need to do

Should work.

Bh

Darn you Bhruic, this is so much cleaner than the code I was using! :)
 
Bhruic said:
Code:
for iPlayer in range(gc.getMAX_PLAYERS()):
 pPlayer = gc.getPlayer(iPlayer)
 if (pPlayer.isAlive()):
  for iCity in range(pPlayer.getNumCities()):
   pCity = pPlayer.getCity(iCity)
   for iUnit in range(pCity.plot().getNumUnits):
    pUnit = pCity.plot().getUnit(iUnit)
    # do whatever else you need to do

Should work.

Bh

It sure did. Thanks a lot!

Roger Bacon
 
Back
Top Bottom