• Civ7 is already available! Happy playing :).

Unit AI XML Code Question

Cecil Lizard

Chieftain
Joined
Oct 29, 2010
Messages
72
Location
Norwich, Norfolk, UK
If you add a unit which can do something a, say, great scientist can do, that is, build a building, is it ok under <DefaultUnitAI> in the XML code to give it UNITAI_SCIENTIST, even if it can't do the other things a great scientist can do?
 
Not speaking from practical experience, but, given how the AI code is organized, I think this should be fine. AI_scientistMove, for example, the function associated with UNITAI_SCIENTIST, tries (in a particular order) all subroutines corresponding to the abilities that the game expects Great Scientists to have. So it's kind of modular; none of those have to succeed. And a Great Scientist e.g. not being able to start a Golden Age because two GP are needed should be no different from the unit not having a Golden Age ability at all. The AI code generally checks the legality of any choice before assigning a mission to a unit.
 
Thanks! Now I just need to work out how to tell the ai to build the unit; I was surprised to find out it isn't like the buildings. Unlike the buildings, they have no flavors, even though they have a favors tag, they're all just empty. Am I missing something?
 
If the AI doesn't somehow get the unit for free, getting AI cities to train it seems tricky. The unit flavor value is, apparently, only used for Great People (getDiscoveryTech in CvGameUtils.cpp) and solely to determine which techs they can discover; CvCityAI ignores it (no call to the respective getFlavorValue function). Worse, cities only consider producing units of specific unit AI types, well, of most of them, but the GP AI types aren't among those (AI_chooseProduction, AI_chooseUnit, AI_bestUnitAI in CvCityAI.cpp). Moreover, the AI code really figures out on its own what unit AI types a unit is suitable for. The AI types in XML force the AI to take a particular type into consideration, but, if the unit doesn't have the proper stats and abilities, the AI will probably prefer more suitable alternatives (CvPlayerAI::AI_unitValue). And, even if you make your unit e.g. also a decent scout, – if the AI chooses to produce it for that purpose, it'll also receive UNITAI_EXPLORE upon completion and will behave only according to that role.

Maybe modders who are used to working with just XML have figured out some (least bad) workaround for these issues. But Nexus's answer in the Quick Modding thread is not encouraging, seeing also that he mods based on Rise of Mankind - so it seems that AI mods have not added AI support for producing unusual units either. Through DLL changes, code for training UNITAI_SCIENTIST could of course be added and, through Python, it's also possible to just tell an AI city under specific, perhaps somewhat clumsy conditions to switch its production to any particular production order – or to change the unit AI type of a unit on the map (assuming that it has some sort of dual role that will make the AI interested in producing it). As for overriding the city production, there was a recent thread about that ... here. Quoting those posts should restore the indentation of the Python code. Perhaps prudent to test if UNITAI_SCIENTIST really results in the desired behavior (WorldBuilder can assign unit AI types) before putting effort into Python scripting.
 
Hah, unfortunately the whole reason I was trying to add a constructable unit that would build a building in the first place was a workaround for adding something else using just using XML. I wanted to add a building that you can only build if running a certain civic. So, I thought I would add a dummy building which couldn't be built in any manner (and doesn't do anything), make that building required to build a unit, make it a special building, then make the civic in question do what Organized Religion does with Monasteries, remove the building requirement from the dummy building, and then make the unit itself be used to build the real building I actually wanted which actually did something. If I was to go use DLL anyway I expect the sensible thing to do would be to do the effect I want directly.
 
Yes, a civic requirement for buildings ought to be easier to do in C++/Python (and no doubt has been done by mods). Just through XML ... maybe a random event that always occurs while running the civic. If that could give all cities a particular dummy building, which then enables the proper building. And then a different event to remove the dummy buildings. But I don't think an XML-only event can add a building to every city. Going via a free specialist from the civic to a special kind of Great Person also doesn't go anywhere useful ultimately, I think.
 
That looks like a "A New Dawn" addition, by DarkLunaPhantom specifically according to the AND building schema file. Could just go with AND's PrereqCivic tag then.
 
Oh, right, I see. Let the events provide/ remove a kind of Stonehenge in the capital and have the actual building require the (so-to-speak) Monument. Sounds like it should work. Of course confusing at the UI level, but so was the original plan with the pseudo-GP.
 
I'm just getting round to doing this. I've been looking at the event and event trigger pages on modiki, and it seems to imply that the ClearEvents tag for events can reset event triggers as well as events. I'm adding it like that, but I wanted to check that is correct.
 
It's the first I read about that tag, so I can't really answer. In the DLL I see that there is some distinction between an event having occurred and a trigger having fired; looks complicated.

I've been wondering if the event that grants the Stonehenge-like building can really be made to trigger just once, right after adopting the civic and again just once after abandoning it. Can an event trigger at all from not having a particular civic? All just through XML, that is. I've never done anything with events really. Maybe the question about ClearEvents is really aimed at resolving these issues. Well, I mostly just wanted to voice a caveat about my earlier "sounds like it should work."
 
Ah for being triggered by not a having a particular civic, what I'm planning is having an event for each of the civics in the category of the civic I want there to be an extra building for. Its slavery so I'm just going to have an event for each of the other 4 Labor civics, each one removing the Stonehenge-like building and setting the event for the slavery civic to not be done, whilst that one will set each of those ones to not be done.
 
Top Bottom