Advertisement

Base-building AI for RTS games

Started by October 20, 2005 06:25 AM
13 comments, last by d000hg 19 years, 1 month ago
This is the last real thing I need to do for my 4E4 entry. Obviously I'm aware that 11 days isn't much time to implement an RTS AI, so I'm looking for some simple resources on how I might look at this. It's pretty much certain to be based on reasonably simple FSM. For the purpose of the contest, I'm even considering 'cheating' to an extent. For instance I'll script the order that buildings and units be created in for a specific level - is this approach workable. I should stress that I'm not attempting to get amazing next-gen AI - just something the original RTS games like Dune2 or C&C might have used, enough that it is a game. I can't see that an amazing AI would be expected for the contest... So, links to articles and your own experience is sought after. Thanks in advance.
Hmmm... let's see... I did my own clone of Dune 2 once.

The base building - briefly described- was determined by player state and random placing. This is duct-tape and WD40 programming, but I reckon that's what you're looking for here. Should be simple enough to implement in a few days.

Basically I had it count it's resources every now and then, as part of the game loop. Sortof like, If low on credits then call function BuildBuilding("refinery"). If total_army < 20 then build army.

Then I randomize a location nearby the AI starting position, check if it's a buildable location (if not - randomize until one if found). Then place new building there.

So finite game states set which building to build, and then randomization takes care of where to place it. For gun turrets and other fortifications I had it exclude positions close to the base center, and only accept locations which are at the perimiter of the base. For weak buildings I had it favor locations away from the player (yeah, cheating AI knows where the player is). You should also check so you leave some space between buildings so it doesn't clog up and produce barriers.

It'll work well enough.
Good luck!
----------------------~NQ - semi-pro graphical artist and hobbyist programmer
Advertisement
Oh, I forgot one essential part!

- Buildings should be selected based on importance in a build queue.

Like so; each time the AI figures out it needs a specific item, say a soldier, then it gets placed in the "to-build-queue". After that it puts the order to build a tank. This list is worked through as resources become available to build them. Right now both items are on hold until more resources are gathered.

A few seconds later it realizes "oh hell! I don't have any constructors left!" and desperately needs to build one. However, there's 2 items already on the list.

Here's the trick >>> Each item gets tagged with an "priority level". The soldier and tank are of fairly low priority, but the constructor is really important. It shifts the other items down the list, and places the constructor first in line.

So it's important to realise, even though perhaps you have several factories. One barracks and one tank-factory, there is NOT one list for each factory. THEY ALL SHARE THE SAME PRIORITY LIST. One list, built by priority, and worked through as resources become available.

Hope I helped!
----------------------~NQ - semi-pro graphical artist and hobbyist programmer
Yeah those kind of ideas sound like they could be implemented quickly enough. In all likelyhood the AI will know everything about the player which should make it seem a bit brighter.

Thanks.
The approach above can be considered a top-down one.

I implemented an AI in an RTS a while back utilising a bottom-up approach.

Put simply, each constructor acts as a seperate entity, constantly searching for something which the whole needs. Then, that constructor goes ahead and builds it. If helping is allowed, then constructors will look for things to help within a search radius - the same applies for repairing.

The upside to this is that it is very simple to create - it can be scripted quite nicely, and it scales very well. The downside (or another upside) is that you get very organic looking base layouts (messy, in other terms).

I also did the attacking AI in this matter - which with a bit of thought also gives some very interesting tactics.

Oh - and to sort out the most important buildings, I added a weightings system to a randomised object picker - so buildings we need more of come up more often. The weightings can rely on variables too, so resources going down can increase the chances of a resource creation device being built.
The way Total Annihilation does its AI is similar to Source's method.

It has a few safeguard to build resources over anything else if it's really low, but other than that, the AI configuration file gives weights and limits for each unit. The limit is just for a "difficulty" setting, so you might not need to bother. ( An AI that can have 4 plants is easier to beat than one with 10, if it can actually keep them all going. )

When a construction unit finishes, it
1) Checks if more resources are needed, and builds them if needed
2) Might help out with ongoing construction
3) Figures out which units it can build, then randomly picks one, based on the weight.

This is easy and fairly effective, but without the high-level planning it can easily make really dumb bases. ( The defences aren't around the edge; The units can't get out of a certain plant because it built it with the exit facing a cliff, etc. )

One possible consideration is to have a sort of influence map that keeps track of where you "control", based on things like how much damage your units have received, how many units/structures are in the area, etc. Then you could have it more likely to build defences in the only slightly-held area, and weak resource structures in the well-held, safe area.
Advertisement
Hmm, another interesting approach. Communism in action, you might say!
I seem to remember that in TA the enemy base just kept on growing and growing, and it was a long process to win once it got going because all the constructor-bots could rebuild really fast.
WarcraftII on the other hand didn't seem to do this - there'd normally be a big base but it would be localised to a particular part of the map rather than just growing and growing.

Personally I liked the latter approach because to beat TA you ahd to have a massive base yourself, and I couldn't be bothered with managing factories and battles over a such a wide area.

I'm not actually using constructors, but the buildings get build more like in Westwood's games. I suppose that prevents the AI base growing at a geometric rate!
Bases constructed with such a method are extremely tricky to (excuse the unintended pun) totally annihilate - mostly due to the fact that in an ordinary player-controlled side, all manufacturing utilities are in certain areas devoted solely to that type of construction - mostly to aid selection and organisation. It takes a long time to train yourself to on purposely build in an organic and less restrictive way.

The downside is that a well placed superweapon or attack can obliterate an enemies' manufacturing capability. The AI on the other hand has factories spread throughout a map, and can recover far quicker from large-scale attacks.

The outcome is that bases built in this manner give a fun, exciting and satifying game - as did those in the aforementioned TA.

Difficulty level is also easily tweaked - I found that I needed to introduce large delays in each unit's operation to reduce the efficiency - otherwise it tended to completely fill the playing area.
I remember Total Annihilation. It's one of the games I've liked best through the ages.

But the downside of that solution is - as you said - the AI produces very messy bases, which occationally don't function at all.
I imagine you have already constructed a system for how the player issues orders to the units? To save time; create a top-down AI which taps into the system you already created.

It overviews the situation from above (checks the resource level and so on) only once. The individual constructors don't do anything on their own. Just like you can issue "build"-orders to your units; have the AI issue build-orders to its units based on rules.

I'll suggest something I think you could pull off, which would actually work. It employs weights, but is a top-down AI so it doesn't build (too) messy bases.

A working example


Building the AI's strategy:


Write some values like this:

// globals
low_resources = 100
high_resources = 2000
weight_army = 0.6
weight_base = 0.3
weight_defence = 0.3

// unit weights
weight_soldiers = 0.1
weight_tanks = 0.2

It will emply different solutions when resources are low ( < low_resources) and others when they're high ( > high_resources). It will refer to the army and base weights when deciding what category to build. If it notices the army needs to get bigger it will follow the weight for how often to build soldiers.

Basically the base for the rules which the AI uses as strategies in the game. (you should put them in a text file which doesnt need recompilation every time you change it's strategy)

Building the basebuilding engine:


The core object of the base-building AI will be the build-queue. The second most important thing is the building-placer, which will act as a plugin to the build-queue.
The AI checks the status of the game, compares what it sees with the weights from the textfile. The results are stored as items in the build-queue. Each item has a priority. Example:

If soldiers are fewer than the weight then place soldier item in the build queue, with priority 3.
If base is under attack, place soldiers item in the list with priority 2.
If base is under attack and there's no defences, place soldiers item in the list with priority 1.
If no workers are alive then place worker item in the build-queue with priority 2.

This populates the list. Now on to the second section - the function which uses the created build-queue.

The building-placer waits for items to appear on the list. When something have appeared, it waits until there's enough resources to build it. When that is fulfilled it triggers it's main function: placement.
Through checking all the AI units it calculates where the town centre is. From here there's three baby-steps you should take:
1. Select random location around here which is large enough (and empty) for your building. Give an available constructor the order to build that building there.
2. Select a random location, but be picky. For defences, only pick locations around the perimiter. Calculate the direction towards the enemy (the player) and tend to put more defensive buildings there, and the weaker buildings at the other end of the base.
3. Select a random location, but be even pickier. Now also think about which other buildings are already placed. Put an anti-air gun next to each defence-laser, but don't bunch up lasers at one side of the base and the anti-airguns at the other side. Put all resource buildings next to each other, and the refineries near the nearest resource-field.

It's perfectly alright to stop at any one of these levels, evne the first one if you feel you don't have the time for the others.

Hope I got you abit on the way!
----------------------~NQ - semi-pro graphical artist and hobbyist programmer
Hey, thanks for that. I'm printing it off as I leave work!

Your rating has been bumped up another notch. If you're helpful again I've run out of notches!

This topic is closed to new replies.

Advertisement