[Python] Easier way to do this?

Dryhad

Prince
Joined
Feb 23, 2006
Messages
451
Ok, there's two things I want to do and I do know how to do them, but the way I know is messy and complicated. I want to know if there's some python functions I'm missing that with do these things without having to resort to woefully inefficient loops and nested ifs.

1. Given a religion, I want to find the missionary for that religion (i.e. the FreeUnitClass in that religion's XML entry)
2. I want to be able to destroy all units of a particular unitclass without having to cycle through every unit and check.

Is this possible, or am I stuck with my inefficient design?
 
Ok, there's two things I want to do and I do know how to do them, but the way I know is messy and complicated. I want to know if there's some python functions I'm missing that with do these things without having to resort to woefully inefficient loops and nested ifs.

1. Given a religion, I want to find the missionary for that religion (i.e. the FreeUnitClass in that religion's XML entry)
2. I want to be able to destroy all units of a particular unitclass without having to cycle through every unit and check.

Is this possible, or am I stuck with my inefficient design?

Your going to have to loop.
 
Yes, you'll have to loop. But to make it as efficient as possible, move the code that looks up the unit type/class number that you seek outside the loop. For example:

PHP:
types = [ gc.getInfoTypeForString("UNIT_ARCHER"),
          gc.getInfoTypeForString("UNIT_AXEMAN") ]
for each player:
  for each unit:
    if unit.getUnitType() in types:
      # kill unit
 
Ok, so a loop to kill them. How about the second one? Surely there's a way to look up the FreeUnitClass of a religion without endless nested ifs?
 
CvReligionInfo has getFreeUnitClass() that returns an int.
CvUnitClassInfo has getDefaultUnitIndex() that returns an int.

So my educated guess would be

PHP:
gc = CyGlobalContext()
relInfo = gc.getReligionInfo(gc.getInfoTypeForString("RELIGION_SCIENTOLOGY"))
classInfo = gc.getUnitClassInfo(relInfo.getFreeUnitClass())
unitInfo = gc.getUnitInfo(classInfo.getDefaultUnitIndex())

print unitInfo.getDescription()
--> "L. Ron Hubbard"
 
Top Bottom