Rudimentary AI Modding

Delnar_Ersike

Prince
Joined
Jan 1, 2007
Messages
497
Location
localhost
I've been working on an AI mod for Civ5 for quite a while now. Though the mod's primary component is my modified DLL, it also has plenty of supplementary XML modifications that help quite a bit.
Since I don't own a copy of Beyond Earth, I downloaded the demo a few hours ago and had a look at the XML files out of curiosity. Turns out 99% of my XML modifications would work for Beyond Earth.
Though my AI tweaks through XML are fairly basic, they would still help quite a bit (especially where science progress is concerned), and I don't want people to be left without it.

If anyone who owns a copy of Beyond Earth is interested, I could send over the necessary XML files. All you'd need to do is package the mod via Beyond Earth's ModBuddy, you don't even have to send it back to me (what use have I for a mod package when I don't own the game?). My only request is that if you do make it available online, give credit where credit is due.

EDIT:
A Dropbox link to a .zip containing everything can be found here.
Note that I don't know how well a lot of my changes translate into CivBE, since I don't know how the AI was changed. However, if the XML AI files are any indication, the answer is probably "not much".
 
A Dropbox link to a .zip containing everything can be found here.
Note that I don't know how well a lot of my changes translate into CivBE, since I don't know how the AI was changed. However, if the XML AI files are any indication, the answer is probably "not much".
 
I'll test it tomorrow, and tweak if necessary.

Would you prefer it to be uploaded to the workshop as a single file, or merged with my Beyond Balance mod (which is aiming to become a sort of community patch)? It is entirely up to you, and either way you will be fully credited.
 
As I said, do whatever you want with it.
Having it only available as part of a gameplay-changing, balance mod doesn't necessarily seem like a good idea though, since I can see some people preferring vanilla gameplay and/or the gameplay of another mod that conflicts with yours. This is why I split my original Artificial Unintelligence mod into two versions: the lite version is for people who want to play with other mods, but don't want to miss out on AI features/fixes that would not cause compatibility issues.
 
Very nice work on the mod mate :goodjob: I have seen some very intelligent moves from the AI in my very hard game. During a war the AI bombed my cities with massive air attacks and then went all in with their ground forces. Conquered one city after another of mine and I lost lol. Yet, was happy to see this mod is doing a great job in improving the AI.
 
Don't attribute too many improvements to my mod. For example, none of the actual movement logic was changed, nor any logic relating to airstrikes. Instead, almost all of my changes affect how the AI behaves when you aren't looking: what buildings they build, how they manage their cities, how aggressively do they create settlers, etc. If the AI is pulling "smart moves" with my mod, they were probably programmed to pull those moves from the start, it's just that they could never get to the technological/production point of executing those moves using Firaxis' default AI parameters.
 
Thank a lot for your work Delnar, I am being litteraly crushed by the AI in my Soyuz game (whereas I had a strong army), the result you have achieved is huge!

The AI do moves I have never seen, it is really good. It is quite surprising to see what can be done by only tweaking parameters in xml files. A very good surprise indeed.

The AI still do one stupid thing in its tactical logic : sometime it moves a land unit in sea for no obvious reason, allowing me to kill it quite easily. Except that thing, the tactical AI behave far better with your modded xml.

Thanks a lot Delnar!

:)

PS: Any chance to see a conversion of your Civ5 'magic' dll for BE?
 
Don't attribute to many improvements to my mod. For example, none of the actual movement logic was changed, nor any logic relating to airstrikes. Instead, almost all of my changes affect how the AI behaves when you aren't looking: what buildings they build, how they manage their cities, how aggressively do they create settlers, etc. If the AI is pulling "smart moves" with my mod, they were probably programmed to pull those moves from the start, it's just that they could never get to the technological/production point of executing those moves using Firaxis' default AI parameters.

It's great to see that you're so honest about what your mod does and doesn't. Had some bad experiences regarding this sort of thing in the past. A lot of people want to see an improvement and wrongly attribute effects to a mod that are either purely coincidental or just something they didn't pay attention to before.
 
The AI still do one stupid thing in its tactical logic : sometime it moves a land unit in sea for no obvious reason, allowing me to kill it quite easily. Except that thing, the tactical AI behave far better with your modded xml.
I can actually think of a lot holes in the tactical AI (eg. not being able to move and shoot with the same unit in one turn, not being able to move with a unit after it has attacked when it has the necessary promotion), but yeah, the one you mentioned is probably the most visible. Assuming Firaxis didn't change too much AI-related code from Civ5 to BE, I think I know the cause, but it requires DLL modding to fix: the problem primarily lies with the "Tactical Map Analysis" code, where the AI does not check for cities (or ranged units that can get in range and then shoot) when determining whether or not a water tile is safe to embark to.
Fixing the problem also has its issues: checking for cities is simple, but checking for ranged units that might be able to attack a tile can get quite computationally expensive if you want to be precise, especially since the AI is going to run the same code for every tile in a dominance zone (these are small sections of the map that get brought to the AI's attention whenever it's at war and can get quite big) on every turn. Less accurate calculations still get the job done though and at a much faster pace to boot, hence why I opted for them in my Civ5 DLL (for now).

PS: Any chance to see a conversion of your Civ5 'magic' dll for BE?
That would require Firaxis to release the DLL source code first. If Civ5 is any indication, they probably won't release it until their first expansion is out at least.
The other problem is that it would require me to spend quite a bit of time with BE's source code (I've spent over two months with Civ5's, and I've barely even glanced at the production and diplomacy code and only know about 25% of what's going on with the unit/army management code), and I'm frankly not interested enough in BE. I honestly doubt I'll ever be even after the expansions are out, seeing how the new Firaxis team transitioned from Civ5 to BE (fixing some mistakes from Civ5, but still moving along the same design trajectory and making new, much larger mistakes as a result). Heh, and to think that Civ4's Firaxis-developed SciFi mod, Final Frontier, was released for free and barely had half the problems BE has...
That said, the source code for my Civ5 DLL should hopefully be all uploaded to GitHub by the time the BE source code is published, so nothing would stop enterprising modders from migrating my Civ5 code to BE, possibly including their own improvements as well.

It's great to see that you're so honest about what your mod does and doesn't. Had some bad experiences regarding this sort of thing in the past. A lot of people want to see an improvement and wrongly attribute effects to a mod that are either purely coincidental or just something they didn't pay attention to before.
It's mainly to help feedback-wise. If people know the scope of the mod, they'll report issues/bugs in a much more helpful fashion, knowing what behavior can be attributed to the mod and what's just the unmodded code acting in an unexpected fashion. Plus it also minimizes disappointment for newcomers: if the comments promise a miracle and the mod doesn't deliver on that promise, they'll be left disappointed.
 
If you do get to making further changes to BE, please try to improve aircraft behavior. As it stands in BE, AI aircraft only do airstrikes, no interception or sweeps. Also, if the AI could use Rocktopuses somehow (I know that involves new mechanics and all) that would be super nice. Thanks for doing this by the way.
 
If you do get to making further changes to BE, please try to improve aircraft behavior. As it stands in BE, AI aircraft only do airstrikes, no interception or sweeps. Also, if the AI could use Rocktopuses somehow (I know that involves new mechanics and all) that would be super nice. Thanks for doing this by the way.

Sadly, this is once again something I know how to fix but cannot because I don't have access to the game's DLL code. Firaxis' code for interceptions is highly flawed, since, among other things, it relies on dominance zones around an enemy city, which only get created if the AI is attacking that city. As for air sweeps, the answer to why the AI doesn't do them is simple: Firaxis didn't program them to be able to do them. Seriously, there is no AI function for air sweeps.
 
Sadly, this is once again something I know how to fix but cannot because I don't have access to the game's DLL code. Firaxis' code for interceptions is highly flawed, since, among other things, it relies on dominance zones around an enemy city, which only get created if the AI is attacking that city. As for air sweeps, the answer to why the AI doesn't do them is simple: Firaxis didn't program them to be able to do them. Seriously, there is no AI function for air sweeps.

*sigh* thanks for that. Really wish they'd change that for the next updates.:(
 
Has this been uploaded as a standalone on the Steam BE Workshop yet? If not I can publish it. I suspect it will get a lot of downloads... even if there are some bugs from porting, people are really thirsty for a better AI opponent.
 
No, I don't think it has been uploaded yet as a standalone mod; AFAIK, albie's added it to his mod and has received positive feedback about it, but that's about it. As I mentioned, feel free to do whatever you please with the files, so long as you give credit where credit is due if you decide to upload it to Steam Workshop.

Note that I've made a bit of progress with my mod's Lite version since, so I may update the files with a slightly newer version later (changes are to a single multi-unit formation's composition and 9 tactical moves that now allow other operations to recruit units from them). Also, from what I've heard, a few XML-tweakable values need even more tweaking in BE to fix behaviors that are caused by BE's design (eg. worker automation overvaluing terrascapes because BE uses Civ5's worker automation code, which gets real wonky for worker-buildable improvements that generate large amounts of culture yield, and especially wonky if those improvements cost maintenance); unfortunately, since I don't own the game, I can't pinpoint and fix these BE-specific issues myself, but that shouldn't stop anyone else from trying.
 
Ok in that case I'll try to publish it to the workshop later today. I'll make it clear in the desc that it's your mod and I'm just posting it on your behalf (and keeping up with updates).

There are definitely some big issues with AI worker automation. There is a good thread about it here http://forums.civfanatics.com/showthread.php?t=538615

To summarize, there seem to be three big issues:
AIs don't have a strong enough FLAVOR_TILE_IMPROVEMENT
AIs don't build enough workers (since more are needed in BE than Civ5)
Flavors for individual tile improvement types have not been set (allegedly leading AIs to spam terrascapes)

I can try pumping up these values and setting the improvement flavors to see what happens.

I have read elsewhere that there's also a bug with AIs not understanding build times, so they start an improvement but stop building it 1 turn before it finishes. Can't confirm that.

One question - can you set negative flavors? Like a negative FLAVOR_ENERGY for terrascapes, to show they're super expensive?
 
So I ran through the files quickly, and looks like it should port well.

Few changes I made
- Commented FLAVOR_NUKE related lines in GrandStrategies since BE doesn't have nukes (it's defined in game files but all AIs have bias set to 0)
- Caught a few instances in EconomicStrategies where FLAVOR_GOLD had to be changed to FLAVOR_ENERGY

Not sure what to do with AICITYSTRATEGY_HAVE_TRAINING_FACILITY as there are no buildings in BE that give unit XP. The game files have defined it like so:

Spoiler :

<Row>
<Type>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</Type>
<Permanent>true</Permanent>
<Advisor>MILITARY</Advisor> <AdvisorCounsel>TXT_KEY_AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AdvisorCounsel>
</Row>

....

<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_OFFENSE</FlavorType>
<Flavor>6</Flavor>
</Row>
<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_DEFENSE</FlavorType>
<Flavor>6</Flavor>
</Row>
<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_RANGED</FlavorType>
<Flavor>2</Flavor>
</Row>
<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_TILE_IMPROVEMENT</FlavorType>
<Flavor>-3</Flavor>
</Row>
<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_EXPANSION</FlavorType>
<Flavor>-3</Flavor>
</Row>
<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_GROWTH</FlavorType>
<Flavor>-3</Flavor>
</Row>
<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_HEALTH</FlavorType>
<Flavor>-3</Flavor>
</Row>
<Row>
<AICityStrategyType>AICITYSTRATEGY_HAVE_TRAINING_FACILITY</AICityStrategyType>
<FlavorType>FLAVOR_RECON</FlavorType>
<Flavor>-6</Flavor>
</Row>


It looks like the AI always believes they have a barracks in the city, even though there aren't any in the game. I'm not sure how that affects behaviour.
 
"Permanent" only means that the strategy is active forever once it becomes active once, not that's it permanently switched on. For example, the "Is coastal city" strategy is also marked as permanent.

Improvements do not use the flavor system, so while you can set negative flavors for stuff, you cannot apply flavors to individual improvements. Instead, you have to modify BUILDER_TASKING defines (from GlobalAIDefines.xml) in such a way as to promote the type of behavior you're looking for: for example, if you set BUILDER_TASKING_BASELINE_ADDS_CULTURE to 0 (default is 100), the AI will no longer apply a flat scoring bonus to improvements that add culture yield (the actual amount is +[Culture Yield Change]*[parameter value]). The other five parameters that significantly alter improvement choice are the BUILDER_TASKING_PLOT_EVAL_MULTIPLIER ones: improvements' chop instruction base scores are calculated via (Yield Change)*(Multiplier Value for Yield)*(Leader Flavor), where (Leader Flavor) is Growth for food, Production for hammers, Energy for energy, Science for beakers, and Culture for culture. For the building of actual improvements, only build time and build cost (flat gold cost, not maintenance cost) are considered.
Yeah, I know the scoring is quite crude and doesn't take possible future yields into consideration, but you need DLL access to alter any of it, especially since none of the worker automation code has Lua hooks.
 
Ah I see. Sounds like I'd need to make some conceptual decisions about what improvements/yields to prioritize then -- not sure I'd want to get involved in that.

What about the priority for building more workers? Is that a simple priority I can set, or does the AI calculate a certain number of workers needed per city?
 
Worker priority is ultimately determined the same way all unit priority is determined: via the sum of (unit flavor)*(leader flavor) for all individual flavors (in the case of workers, they only have one flavor by default, FLAVOR_TILE_IMPROVEMENT, but if a unit/building has multiple flavors, they all count). This means there are various ways you can approach trying to fix worker priority: Firaxis controls it primarily through a few City Strategies (applied individually to each City), Economic Strategies, and Military Strategies that add or subtract to or from FLAVOR_TILE_IMPROVEMENT (or its naval counterpart), but you shouldn't limit yourself to only tweaking those strategies. Examples of other methods that might work (though some may have unintended side-effects): adding new flavors to the worker unit, changing leader flavors for worker flavor, adding worker flavor changers to Grand Strategies, adding negative worker flavor values to certain units or buildings, etc. Just keep in mind that simple, flat flavor boosts are active permanently, so act with caution: eg. if you increase AI leaders' worker flavor value, that increased value is used throughout the entire game, both early on when one worker is enough, and later on when the AI is being weighed down by worker maintenance costs.

Note that as far as I know, you cannot add any new strategies, since they all rely on their own, individual DLL checker functions (hence the nebulous parameter names).

Some of the "conceptual" changes are pretty much no-brainers: for example, Civ5 only had two worker improvements that added culture, and both had fairly rubbish yields otherwise, while a lot of CivBE's improvements will generate at least one culture after a certain tech, so altering the baseline culture priority to 0 or 1 is a no-brainer. The multipliers, on the other hand, are all about ratios: since all improvements get their (yield change)*(flavor) multiplied, you can fine-tune the multipliers to generate the ratios you want. For example, Artificial Unintelligence Lite sets the multipliers to 6/6/3/3/2 (vanilla is 2/2/1/1/1), which effectively makes culture yield less valuable, but you can set things to 6/6/4/3/2 for higher energy value to make up for maintenance or 5/6/3/3/2 for lower food value to make up for health issues.
Unfortunately, there are no parameters that account for an improvement's gold maintenance or health effects, because Firaxis didn't bother to actually improve their shoddy AI routines from Civ5 BNW (which already saw very little improvement from G&K and vanilla Civ5).

EDIT: Had a closer look at the code, and it turns out the multiplier values are only applied when a feature is going to be removed (eg. Jungle or Forest chop), not when an improvement will be built.
 
Top Bottom