Building an AI

Tutorial for Generals Gen

Avatar of Calamity_Jones

Calamity_Jones

Category: Code
Level: Expert
Created: Wednesday July 29, 2009 - 7:24
Updated: Sunday October 11, 2009 - 8:10
Views: 4414
Summary: A step by step guide to building an AI for your mod.
Rating
  • Staff-
  • Members-
  • Average-

0 votes

Page 1 2 3 4 5 6 7 8
Original Author: Calamity_Jones

3) Counters

The AI needs to know the time! It may sound silly, but the AI needs a timer or counter that tells it what to do and when. The best way to do this is with a counter that has several possible values controlled by corresponding non-deactivating scripts. For example, in my AI the counter detects when the AI has lost a number of units, and eventually loops back to the original value of 1 when it has lost so many units. This counter controlls everything the AI does. For example, the counter reaches 4 when the AI reaches generals level 3 (and thus unlocks new units). When the counter reaches this value, the AI recieves cash, expands its base and begins mass producing its brand new units.

For example, a condition in the build condition script for our rocket launchers could tell the AI to only build them when the counter is at a certain value. If you have a fancy uber tank that specialises in killing other tanks, the counter could determine if the AI is able to build them things, and as soon as it is, stop the AI making rocket soldiers and them instead.

4) Base Building

The AI needs a few scripts that tell it when and what to build. It won't simply build its base automatically, you need to tell it to :p You'll have to customise this based on your mods prerequesits. Assuming that you need power and a barracks to build a warfactory. For example, the first script should be something like "this player builds a Barracks" and a second line that tells it to build a powerplant. Then you set up another script that has a condition to check if the player has a Barracks AND a Power plant with the action of building a warfactory.

5) Setup Scripts

-You need to set up a few things for the AI. All these scripts should be true and deactivate right away. They set up things like flags, timers, counters, priorities and crap like that.

To do priorities you need to first define object types, then make a script that sets for a certain priority, a certain value for a certain object type.

6) Team Building Conditions

-These are scripts that tell the computer to build a team when a condition is met, you need a LOT of build condition scripts for different difficulty levels and numerous teams. You could make a generic team that has an IF (true) condition which would simply make the AI mass produce the exact same team and make it do the exact same thing over and over. This would be very boring so you need to make a shed load of teams and conditions.

The general structure of these scripts should be a condition that relies on the main timer and perhaps some other flags or counters, then a command as the action that tells <This Team> to guard. For example, you could have a script that executes if the player has less than 10 red guard. You could then set this script as the build condition for a team of, say, 6 red guard. Therefore, if the player has less than 10 red guard, it would build that team and continue to do so until it has more than 10 (therefore making the scripts condition false.)

These scripts should be SAnD i.e. Subrouting, Active, non Deactivate. Set the evaluation to a certain number of seconds, NOT per frame as with a good hundred build conditions for the computer to evaluate so many times every second for each AI you'll get unnecessary lag. I generally set the evaluations to somewhere between 1 and 10 seconds, depending on how likley the script is to go true and tell the computer to build the team. For Example, a team that consists of high tech super units that you only have acess to late in the game should evaluate less often as there's less chance you'll get them :D

If the script is SAnD, the computer will check to see if it's true. If it isn't it'll ignore it (unless you set a false behaviour). It'll keep evaluating until it is true, then as soon as it is, it'll produce the team constantly until it is false again.

7) Team behaviours

-These define what a team does when attacked, when made, when destroyed, when idle etc. You need to
make scripts that tell the AI to :
-Send units along certain waypoints to attack the enemy.
-Attack the enemy.
-Respond to threats.
-Avoid Generals Powers.
etc.

8) General Tips

Always use the <This Team> and <This Player> tags, things often go wrong otherwise... For example, if you have a script that says "the player builds an upgrade" instead of having the script say "Player SkirmishUSA build advanced armour upgrade or whatever, have Player <This Player>, as long as it's in the USA root, it will work.

IF False actions on scripts can be used to make "sensors". In other words, the script does something all the time and flips between states depending on the condition. For Example, you could have a condition that counts the enemies money, and see's if it's greater than 10k. The true action could set a flag to true, and the false would set the flag to false. That way you have a sensor that checks the AI's opponents cash situation :p

Return to top