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

Unit spawn help

Discussion in 'Rhye's and Fall Modmods' started by Baldyr, Dec 5, 2009.

  1. Baldyr

    Baldyr "Hit It"

    Joined:
    Dec 5, 2009
    Messages:
    5,530
    Location:
    Sweden
    I'm a lurker of some time who finally registered on these boards to ask you guys for advice.

    So, I have finally taken the plunge and started modding civ again, after a 10 year hiatus. I'm working within the RFC framework as the mod is pretty much doing everything I ever wanted from a game of Civ. But of course it has to be modded to suit my own preferences and the historical scenarios I like to focus on.

    The thing is that I really don't know any Python or XML (or any other programming language) but I brave on nonetheless, testing things and learning new stuff as I go along. I pretty much succeeded with the XML stuff I wanted to implement.

    So, as for my first question. Since I don't know any Python I try to follow the in-code comments and plain logic as to what the code actually does. Some things are pretty straight forward but some other stuff I can't, for the life of me, figure out. Since I'd like to add more unit spawns I really need to understand this bit of code (Barbs.py, line 366):
    Spoiler :

    if (iTurn % iPeriod == iRest):

    So I understand that iTurn is the current game turn, and iPeriod and iRest are numerical variables. I suspect that this function is creating an interval between spawns, but i really can't seem to fathom what the "%" character does in order to achieve this. (There is a relationship between the game turn and the iPeriod value, and the units are created if they match the iRest variable.)

    Thanks for any help with this - more questions are likely to follow since I've finally registered.:D

    disclaimer: English is my third language so I apologize for any typos and the like.
     
  2. Baldyr

    Baldyr "Hit It"

    Joined:
    Dec 5, 2009
    Messages:
    5,530
    Location:
    Sweden
    Nobody really knows elementary (or so I suppose) Python scripting? I tried to look it up myself in a guide but really found nothing explaining the % character. Help, please?:(
     
  3. sedna17

    sedna17 Chieftain

    Joined:
    Mar 12, 2008
    Messages:
    1,091
    This is the character for mod or modulo (http://en.wikipedia.org/wiki/Modulo_operation). So basically this makes the spawn periodic with period iPeriod.
     
  4. Baldyr

    Baldyr "Hit It"

    Joined:
    Dec 5, 2009
    Messages:
    5,530
    Location:
    Sweden
    Thank you very much! :) Now I stand face to face with my old arch enemy - math. :p

    If I'm getting this right, then all divisors (iPeriod) result in a series of reminders (iRest) of 0, 1, 2, 3 and so fort until it reaches iPeriod numbers and then starts all over. Why, then, isn't the iRest variable a constant, say 0 or 1, for all spawns? Because any given number (in the interval created) is hit every so often in a periodic way, say every fifth turn for a iPeriod of 5. There would be no difference if the iPeriod value was any number from 0 to 4.

    The only reason, that I can think of, is that the iRest value can be used to make the spawn occur a specified turn, say a historical date for an barbarian invasion. (Or not hit on a specified date.)

    Am I correct or is there in fact more to it?
     
  5. sedna17

    sedna17 Chieftain

    Joined:
    Mar 12, 2008
    Messages:
    1,091
    That's all there is to it. Specifically, the code for barb spawning often starts a bunch of different spawns at the same start check date, so iRest just sort of staggers them out so that they show up on different turns. For this reason iRest values are often primes (so that they don't end up coinciding even after multiple cycles).
     
  6. Baldyr

    Baldyr "Hit It"

    Joined:
    Dec 5, 2009
    Messages:
    5,530
    Location:
    Sweden
    Aha! I can't thank you enough for taking the time to explain this for me. Math is the reason I never pursued computer programming when I was toying with it as a kid. When Civ3 came along I kinda realized I was screwed, because you could really dig in there in regard to modding. And not to mention Civ4...

    So, on to my next question, which hopefully someone will be able to sort out for me. What exactly is the difference between NO_UNITAI and UNITAI_ATTACK? I do realize that the second one will make for a more aggressive unit, but what exactly happens when a unit with one of these settings spawns?

    My best guess would be that UNITAI_ATTACK will make a Barbarian unit seek out something to attack - and attack it. While NO_UNITAI would be more case dependent - if there's nothing to attack at the spawn location - the unit will either be stationary - or maybe be disbanded? Would the unit automatically attack though, if another unit came along or if it would spawn next to one?
     
  7. embryodead

    embryodead Caliph

    Joined:
    Jan 1, 2003
    Messages:
    5,179
    Location:
    basement
    NO_UNITAI is lack of plan, UNITAI_ATTACK is just one of many specific AI plans.

    A unit spawned with UNITAI_ATTACK will generally be used to attack units in enemy territory.

    From what I've seen, a unit spawned with NO_UNITAI will immediately switch to a real plan, depending on XML settings (in CIV4UnitInfos.xml) and AI situation. You can find all the plans looking at that XML file, there's quite a few, though most are self-explanatory. I think in RFC only Barbs and Settler-Galleys are spawned with pre-set UNITAIs.
     
  8. Baldyr

    Baldyr "Hit It"

    Joined:
    Dec 5, 2009
    Messages:
    5,530
    Location:
    Sweden
    Yeah, I've seen in World Builder that units can have a variety of AI settings. I'm especially interested in what happens with these two specific settings in RFC, what effect this has on the spawned units. One would think that it wasn't necessary to assign any AI setting at all, or that the attack variety would be sufficient.

    Probably it's result of playtesting and I think I will have to do the same.

    Does the AI ever switch agendas for units if one specific has been assigned? Or does it that only if no agenda is preset? What would cause a Barbarian unit to ha have a change of heart?
     
  9. embryodead

    embryodead Caliph

    Joined:
    Jan 1, 2003
    Messages:
    5,179
    Location:
    basement
    Barbarians are spawned with unit UNITAI_ATTACK because that makes them most suicidal I guess and RFC just follows the convention from the original code, where all Barbarians are spawned with UNITAI_ATTACK (or *_SEA for sea units). Note to sedna17: this may be relevant to crusades in RFCE, shouldn't the crusaders spawn with ...ATTACK_CITY and not ...ATTACK?

    Galleys & Settlers have pre-set UNITAI because otherwise they get stupid (the galley switches to troop transport and drops the settler off to capital).

    When spawning civs it's best to let the AI decide what it needs, or some units may get stuck (just theorizing here).

    Regarding your last question, I was curious as well so I looked at the code quickly, and it seems the AI will change sometimes. The scenarios I found:
    - Sea and air units switch often depending on AI needs (exploration, settling, war etc.)
    - Units that are assigned to guarding cities will switch to CITY_DEFENSE if they have city defense bonus > 0
    - Various offensive plans are re-assigned to relevant units when an offensive mission is planned
    - There's some code to make sure that a stack-leading unit's UnitAI remains the same when a new unit takes his place, I guess to make sure an offensive stack doesn't turn back just because the first unit died
    - Barbarians don't change UnitAITypes
     
  10. 3Miro

    3Miro Chieftain

    Joined:
    Apr 23, 2008
    Messages:
    3,480
    Location:
    Knoxville USA
    The AI logic is complicated. There are many parameters included, such as number of cities, strength of the opponent, patch from the unit to the opponent and so on and so forth. Attack AI would make the unit more likely to attack and no AI would force the AI to "think" on what is the best way to use the unit (i.e. it may decide to use it as a city defender for example). However, what exactly the AI would do, cannot be generally determined without more knowledge of the specific situation.

    Basically AI Attack means the unit is more likely to attack, but should not be considered deterministic in any way.
     

Share This Page