The 3rd Age

The Rhovanion Alliance

The Rhovanion Alliance

A most anticipated mod that adds 4 new factions as well as editing the old ones

Button for The 3rd AgeButton for The Dwarf HoldsButton for The Elven AllianceButton for Helm's Deep Last HopeButton for GothmogtheOrcButton for BFME+Button for The Four AgesButton for HDR HeadquartersButton for Middle Earth CenterButton for Project Perfect Mod

Become an affiliate!


Quick Lists

Top Rated Popular New Updated Last Comments Users

Register and log in to move these advertisements down

In-depth AI Coding

Avatar of Sulherokhh


Category: Code
Level: Expert
Created: Wednesday October 3, 2007 - 6:28
Updated: Friday August 27, 2010 - 19:26
Views: 26767
Summary: The beginning of a comprehensive guide to AI-modding for BfME2


Staff says


Members say




20 votes

Page 1 2 3 4 5 6 7 8 9 10 11
C. Introduction to the SkimishAI of BfMe2.

Before i start going into detail how to adjust the Skirmish AI to suit your needs, let me take this chance to ramble a bit about the way the SkirmishAI for BfMe2 (henceforth called SkirmishAI_2) actually works (or rather how i think it works), largely by comparing it to the SkirmishAI for BfMe1 (henceforth called SkirmishAI_1).

This is going to be rather unpractical stuff and only useful if you are going to explore the limits of this hard-coded bitch like i have. From this point on i will also start to tag my findings with notes like 'mostly verified' or 'mostly speculative' to indicate the degree to which i have reached a conclusion regarding the unexplained workings of the settings (including the occasional definite no-go crash; falsification of a theory has also proven helpful).

So, if you are looking for the hands-on how-to stuff, just skip to the next page ...

The SkirmishAI of BfMe1: SkirmishAI_1.

Some of you may already have some experience scripting the SkirmishAI for BfMe1, either to account for new units and powers, to repair scripts EA left broken for no good reason, for single player maps or (most ambitiosly) for adding a complete new faction. Well, then you already have a good idea how it works. For those who haven't i will now go into a little more detail to illuminate the next sections better.
The WB-scripts are basically a very primitive programming language. I am a bit reminded of early versions of pascal, only even more primitive. It has only global variables and constants amd no open functions to speak of (unless you create your own and are really careful not to reference them in the wrong place).

Each PLAYER (be it a human player, an enemy faction or one of the neutral PLAYERS) basically gets assigned a pack full of scripts. All scripts designated as 'active' and 'non-subroutine' are run from top to bottom every frame (1/5th of a second) or every X seconds (variable setting), one at a time, checking their conditions and then do (or don't do) a set of value assignments or a call of a subroutine or whatever. To top it all, it has a serious problem with getting and keeping an overview. organizing the scripts into appropriately named folders is mandatory. Naming the scripts and variables is even more important. So, when starting to write or adjust scripts, it is easy to lose sight of the overall picture, unless one already has read and understood practically all of them, a daunting organizational feat. But if that is done succesfully, the modder has the marvelous opportunity to practically control ALL of the AI! Wonderful indeed!

The SkirmishAI of BfMe2: SkirmishAI_2.

Compared to SkirmishAI_1, SkirmishAI_2 is simpler, less flexible, faster to adjust, more bossy, less transparent and more prone to drastic malfunctions that are hard to fix because of the other traits i just listed. The settings are annotated in an oversimplified manner, if at all, but obviously sufficient for the EA geeks who i assume know exactly what those settings do. I could only experiment and see what happend.
The results of those experiments will be covered in as much detail as useful in the next few pages. But here i will outline my assumptions on what EA's programmers where thinking when designing SkirmishAI_2.

First, the new AI was supposed to by more flexible when adding new stuff. The whole group of Hero/Unit/SpellbookBehavior-scripts has been streamlined and can now be referenced in code, directly inside the relevant object that is supposed to use this stuff. Sadly, only the most basic behaviors have been converted from script to code. EA should have done a more comprehensive job. Still, the modules that are there work well. I listed most of them on page 3 (AISpecialPowerUpdate). One side effect was to make the objectlists-library obsolete, which also impacts the next point.

Second, target acquisition was to be simplified. That was largely succesful. Gone are the AttackPriority lists. Lo and behold, the general AttackChains have arrived! They are not much better though. And there is only a limited number of them available which is sad. But the number should be sufficient for most mods' unit variety. I surmise they are also serving a second purpose when assigning priorities for team building, but i can't be 100% sure. (see below for the general SkirmishAI settings)

Third, economy and teambuilding was to be streamlined and 'approximated' by code. The result is a desaster. Here if nowhere else it is very easy to make the AI either go on a rampage, doing one thing and one thing only, or freeze entirely when attempting something it can't do to the exclusion of everything else. Experimentation has also shown that EA tried to implement a random faction strategy, but largely failed to explain how it works (or even to use it the way they thought it should be used!).

This leads to the modder making an adjustment that seems to work great, but then finding out it only works great 20% of the time and is just crap 80% of the time. So mostly the now stressed modder will try to adjust values so that SkirmishAI_2 will do one thing good, 100% of the time, or invest an insane amount time with juggling numbers and testing them ingame to balance it all.
And all of that without even be able to read anything going on the the program! With SkirmishAI_1's scripts you could always see what was going on ingame by starting it using '-scriptdebug2', an awesomely useful (and needed) tool! One thing this option does show, even for BfMe2: teambuilding. In the most basic sense, it only shows the type of team the AI attempts to build, not what routines it runs once it has been created. SkirmishAI_2 can now build 3 or 4 different teams (of contents that can only be estimated), that have a fixed set of very, very simple assignments, much like the teams of BfMe1, only much worse. This is where the FarmRushingBot rears it's ugly head!

Fourth, basebuilding was to be more understandable. Gone are the assignments of scouting teams or teams of buildplots to raise this or that structure according to actual needs. Strategic building my ass! All is geared to build barracks and strike fast at the enemy (read: farms), ignoring all other concerns. Together with the new TeamBuilder, this makes for a fast-paced but very boring game. The only beautiful thing is the new base templates. You can basically devise several different bases, complete with where you want what and when, even multiple optional sets (if you have the time to construct them) or even specific ones for specifc maps and starting positions (very needed for fortress maps). What you can't do is make the AI choose a setup or even a single building according to what it actually needs to counter the current enemy. That makes me really sad.

ResourceStructure building as well as the building of walls (or rather wallhubs) by porters is handled twofold. First, The hardcoded AI creates porter teams, which then rush out to build what is needed (from the base templates). Second, depending on priorities (to be set in the base setup for building inside the base or to be assigned somewhere else for farms and hubs) it rushes out and builds. If you make a single mistake either the porters go and spend all their money on farms (standard EA setting) or all their money on building the base. Hard to balance. The walls actually DO work pretty well, apart from the fact that building the wallhubs nicely (much less strategically sensible) is really tricky. But EA simply ignored placing the hub templates on all maps. They did it on one or two but then just stopped because it was so tedious to test and everyone was rushing farms anyway. No point in building a defense, right? One missed opportunity.

At the very least, one thing they didn't take out (much less refine). Spell acquisition works the same way it worked before, using scripts, randomly (and according to difficulty level) choosing a purchase path. At least here a modder can still insert scripts that count enemy units according to type (if one has the endurance to update the object lists) or insert any other condition to choose spells according to need. Spell execution is handled just like unit specialpowers though.

Further thoughts on SkirmishAI_2.

So, how should one approach setting up a good AI for BfMe2? It all has to do with setting the right priorities and do LOTS of experiments (see the rest of the tutorial). But there are a few alternatives if one is really adventureous. Take a look at the general settings in the file 'data/default/skirmishai.ini', specifically the following section, annotated by yours truly.

---- Mostly Verified ----
SkirmishAIData TheSkirmishAIData
; System Toggles
DisableBaseBuilding = No

If yes, AI doesn't use base templates for building, effectively having porter teams only build farms and wallhubs, and maybe another fortress
DisableEconomyBuilding = No

If yes, AI doesn't send out porter teams to build farms at map-farm-template locations.
DisableUnitBuilding = No

If yes, AI doesn't auto-produce units. NOTE: This is different from the script-toggle for unit build. SkirmishAI_2 produces units if it can pay for them. It doesn't need to have a desire (priority) for a team containing the produced unit. In fact, if you disable everything but this, the fortress will still spit out heroes that stand around and do nothing.BUT: Need can influence the build-order.
DisableScienceUpgrading = No

If yes, AI doesn't purchase upgrades for structures (PLAYER or OBJECT based doesn't matter)
DisableUnitUpgrading = No

If yes, AI doesn't purchase upgrades for troops.
DisableTacticalAI = No

If yes, AI doesn't use AISpecialPowerUpdate for triggering a SpecialPower.
DisableTeamBuilding = No

If yes, AI doesn't build ATTACK teams, DEFENSE teams, EXPLORE teams, or OPPORTUNITY teams.
DisableWallBuilding = No

If yes, AI doesn't send out porter teams to build wallhubs at map-wallhub-template locations.

Well, these toggles alone give a glimpse of what is still possible with SkirmishAI_2. One of the main concerns i had in the past was the bossy nature of the AI. It took me ages to find the right settings so that the initial porters didn't run cross country building farms, but to make them build the base first.

All of the things the AI now does are listed above. They all run on priorities whose calculation routines elude me and everybody. They can only be estimated and tested, because all you can set is a range and sometimes a rate of change (per unspecified period of time). Some of the parts work quite well, but some are just broken.

Adding TeamBuilding scripts breaks on the AI like waves on a rock. To repair it, switch off UnitBuilding and TeamBuilding, Adapt working Teams from SkirmishAI_1, include the scripts they use (of which some will probably not work, unless you disable TacticalAI) Don't forget to add some hunters for TheOneRing.

One type of Team can already be constituted. You can make teams from wall-stretches, outposts, the spellbook etc, and they can employ the same scripts from SkirmishAI_1 that where used to assess threat, check for money, initiate repairs, buy upgrades, etc.

The Real Hero will just toogle ALL SETTINGS to YES! Start building the AI from scratch (copy-paste from SkirmishAI_1 then adjust) but will need to find unique ways of getting the AI to build wallhubs and other stuff with porters (using the AI-objects on the maps as tactical markers). New scripts using the new porter are already included in WB2.

Before going on a venture like this, please try and make a faction AI for BfMe1 first, to make you understand how it could work. Go, grab the latest beta of The Elven Alliance and create an ElvenSkirmishAI! They need one badly!

That's it for now. All the rest of the pages will pick out choice pieces and help you tackle the most stupid AI that i have ever worked with.


Display order: Newest first | Page: 1, 2

Prolong - Thursday July 29, 2010 - 9:21

Very nice guide, even though it never got finished there is still a ton of great information here.

Elrond99 - Sunday February 15, 2009 - 7:52

Thanks for updating the Tutorial
It´s great to hear that it´s possible to make a new AI from scratch

Can´t wait for section F, custom AI scripts, that sounds really interesting
Especially since I know D and E already ;)

Sulherokhh (Team Chamber Member) - Saturday February 14, 2009 - 21:19

After 'D. The Faction Base and Fortress', i guess.
If you are asking for a date, you are asking the wrong guy. I don't have one. I work on modding related stuff mostly when i find the time and the mood strikes me. It's supposed to be fun, right? :)

Edit: But i might do it before the fortress and base setup. Depends on, well, which kind of work is going to be most interesting to me at the time. Scripts should be easier to do then the Basebuilding stuff, but a bit more to write as well, at least if i am going to do it right. I am starting to ramble... :O

jakonic - Saturday February 14, 2009 - 12:05

when will be finished Spell Purchase Scripts???please answer

Sulherokhh (Team Chamber Member) - Thursday November 29, 2007 - 17:26

I sure will...

Edit: Do you have any particular requests? If i have done it already, it shouldn't be hard to post a solution here. If it's not, chances are that i was going to look into it anyway. Except for the general skirmishsetup and bases, since those will require extesive explanations which i was planning to do anyway when i find the time.

So shoot! :)

Rob38 (Team Chamber Member) - Thursday November 29, 2007 - 11:00

This is by far one of my favorite tutorials on T3A! Please continue to add more :)

Sulherokhh (Team Chamber Member) - Wednesday October 3, 2007 - 23:07

I am glad you can put it to use, Rob! Your feedback means a lot to me. :D

Rob38 (Team Chamber Member) - Wednesday October 3, 2007 - 22:27

Amazing! I also found a way to recognize if a player is controlled by the AI, but this looks to be a much easier method to use. Thank you for all your wonderful knowledge as there is some really cool stuff in here.

Sulherokhh (Team Chamber Member) - Wednesday October 3, 2007 - 19:18

I'll split it up. Let's see what would be a good way. - Edit: Done. I hope you like it.

Crashdoc - Wednesday October 3, 2007 - 16:48

Nice findings and interesting ways to use them. Thanks for sharing the knowledge!

Go to top


"One site to rule them all, one site to find them,
one site to host them all, and on the network bind them."