1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

[Python] Easier way to do this?

Discussion in 'Civ4 - SDK/Python' started by Dryhad, May 22, 2008.

  1. Dryhad

    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?
     
  2. Kael

    Kael Deity

    Joined:
    May 6, 2002
    Messages:
    17,401
    Location:
    Ohio
    Your going to have to loop.
     
  3. EmperorFool

    EmperorFool Deity

    Joined:
    Mar 2, 2007
    Messages:
    9,633
    Location:
    Mountain View, California
    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
     
  4. Dryhad

    Dryhad Prince

    Joined:
    Feb 23, 2006
    Messages:
    451
    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?
     
  5. EmperorFool

    EmperorFool Deity

    Joined:
    Mar 2, 2007
    Messages:
    9,633
    Location:
    Mountain View, California
    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"
     
  6. Dryhad

    Dryhad Prince

    Joined:
    Feb 23, 2006
    Messages:
    451
    Thank you, you are a lifesaver. No more AIs exploiting loopholes!
     

Share This Page