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: 3247
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

Now, just one more folder to do before we get onto the terrors of teams and build conditions... Time to think about upgrades, so go to your "###_Upgrade" folder. Upgrades are a bit tricky, because the decision to upgrade or not will depend on a lot of things. I'll show you some simple ones, and from there on it's more or less up to you to decide how you want to make your AI decide to upgrade.

The most simple type of upgrade is one where the AI upgrades when it has enough money, and the building it needs to build the upgrade: (Conditions in Player/Owns and Player/Money, action in Player/AI)

TYPE: [nsAD][ENH]
NAME: ###_Upgrade [Upgrade]"
              
Code

*** IF ***
     Player '<This Player>' has Greater Than or Equal To 1 unit or structure of type '[Building You Need]'
    *AND* 5000 is Less Than or Equal the number of credits possessed by Player '<This Player>'
*** THEN ***
Have AI Player '<This Player>' build this upgrade: Upgrade '[Upgrade]'


Of course, you can set the price cut-off to whatever you want, but it's a good idea to set it well above the cost of the upgrade to give the AI spare cash. You don't want them bankrupting themselves then finding they need to replace a building.

You can set upgrades to be bought with more specific conditions too. For example, if you have something that improves the armour of a unit, you could make the AI check to see if it has more than, say, 5 of the unit... and if it does (and has enough cash, and the building required) it'll upgrade.

Save your bloody map!

####Part 4####

Now we get onto setting up things that help out our teams of units. Lets go to the "###_Team Behaviour" folder to start. This folder contains lots of simple generic behaviour that is very important to the AI. You'll also see how some earlier parts of the AI find their use here.

First add this script: (Action in Team/Guard)

TYPE: [SAD][ENH]
NAME: "###_B_Guard Base"
              
Code

*** IF ***
    True.
*** THEN ***
Team '<This Team>' begins guarding area '[Skirmish]MyOuterPerimeter'


This is just a simple script that is used to tell the AI to guard whilst it's building teams.

Now this: (Condition in Team/Attacked and actions in Team/Transport and Team/Hunt)

TYPE: [SAD][ENH]
NAME: "###_B_Hunt"
              
Code

*** IF ***
     Team '<This Team>' has been attacked by Player '<This Player's Enemy>'
*** THEN ***
Team '<This Team>' unload.
Team '<This Team>' begins hunting.


This is quite self explanatory. The unload is added in to make the AI empty any transports in the team.

Here's another simple one: (Actions in Team/Stop and Team/Guard)

TYPE: [SAD][ENH]
NAME: "###_B_Emergency Defend"
              
Code

*** IF ***
     Flag named '###_Enemy In My Base' IS TRUE
*** THEN ***
Team '<This Team>' stops.
Team '<This Team>' begins guarding area '[Skirmish]MyInnerPerimeter'


The stop command is used to make the AI team stop whatever it is doing.. be that moving, attacking, whatever, and the guard makes the team run back home to fight off the invaders.

Now this:

TYPE: [SAD][ENH]
NAME: "###_B_Game Lost Return Home"
              
Code

*** IF ***
     Flag named '###_I Am Crippled' IS TRUE
*** THEN ***
Team '<This Team>' begins guarding area '[Skirmish]MyInnerPerimeter'


This script is kinda something I did a bit different from ZH. The standard AI sells all it's buildings when it's screwed, I thought it might be better to have it put up a last stand.

Now for the opposite of this script:

TYPE: [SAnd][ENH]
NAME: "###_B_Game Won Finish Off"
              
Code

*** IF ***
     Flag named '###_Enemy Crippled' IS TRUE
*** THEN ***
Team '<This Team>' begins hunting.


I think it explains itself quite well... The AI will simply hunt down any remaining stragglers that the enemy still has.

Next: (Action in Team/Garrison)

TYPE: [SAnd][ENH]
NAME: "###_B_Garrison"
              
Code

*** IF ***
    True.
*** THEN ***
Set Flag named '###_INFANTRY BUILDING' to FALSE
Team '<This Team>' garrison a nearby building.


This is an interesting one. The script is called by a team as soon as it has finished building, telling it to run off and garrison something nearby. Hence the action that sets the team building flag to false.

This next one is a tad more complex, but is very important: (Condition in Player/Special Power)

TYPE: [SAnd][ENH]
NAME: "###_B_Scatter"
              
Code

*** IF ***
    Player Player '<This Player's Enemy>' starts using Special power '[Area Damage Power 1]'.
*** OR ***
    Player Player '<This Player's Enemy>' starts using Special power '[Area Damage Power 2]'.
*** OR ***
    Player Player '<This Player's Enemy>' starts using Special power '[Area Damage Power 3]'.
*** THEN ***
Team '<This Team>' begins hunting.


Now, the powers you should list here are the ones that an enemy would throw at a large cluster of units, things like MOAB's, Carpet Bombs and so on. You should also list superweapons, not just generals powers. Naturally, this list will get very long if you have a lot of powers and abilities, so it might be best to remove some that are less threatening if you have lots... Otherwise the AI will end up disrupting its plans constantly.

Now the last one: (Condition in Team/Area... "Team has units in area" and Action in Team/AI)

TYPE: [SAnd][ENH]
NAME: "###_B_Team Succeed"
              
Code

*** IF ***
     Team '<This Team>' has one or more units in area '[Skirmish]EnemyInnerPerimeter' (Surfaces Allowed: Ground or Air).
*** THEN ***
Increase the AI priority forTeam '<This Team>' by its Success Priority Increase amount.


This script is another very important one. It tells the AI what it should consider a successful team. This is a key part in how the AI decides which units to send where, basically if a team succeeds, the AI will be more inclined to send more units that way, and possibly a duplicate team. The success condition should not be something overly complex, and as such a team being able to make it into the inside of an enemy base is good enough.

Now we're going to the folder "###_Team Behaviour_Combat Zone"

This folder controls some special teams that are used to guard the middle of the map, intercept enemy groups and join up with the AI's attack forces should reinforcements be needed. These teams behave like a pool of units that the AI can draw from in times of need, and when not in need they will take care of themselves. These teams are especially useful in the early and later stages of a game, early on to defend, and later on to add to the finishing blow as the AI flows over your defences like a terrible tide of slaughter.

First add this: (I won't tell you where the scripts are, you should have a good enough idea by now :p)

TYPE: [SAnd][ENH]
NAME: "###_BCZ_Guard"
              
Code

*** IF ***
    True.
*** THEN ***
Set Flag named '###_VEHICLE BUILDING' to FALSE
Set Flag named '###_INFANTRY BUILDING' to FALSE
Team '<This Team>' begins guarding area 'CombatZone'


Now, my Combat Zone teams are a combination of troops and vehicles, hence why I have the infantry and vehicle set to false actions. If you wanted, you could split this into two, one for infantry and another for vehicles.

Next: (Condition in Player/Area and action in Team/Hunt)

TYPE: [SAnd][ENH]
NAME: "###_BCZ_Join Attack"
              
Code

*** IF ***
     Player '<This Player>' has Greater Than 5000 within area area '[Skirmish]EnemyInnerPerimeter'
*** THEN ***
Team '<This Team>' begins hunting.


Basically, if the player has lots of stuff inside the enemy base, these guys will join in with the fun and lend their firepower to the fray.

TYPE: [SAnd][ENH]
NAME: "###_BCZ_Flee"
              
Code

*** IF ***
     Player '<This Player's Enemy>' has Greater Than 15000 within area area 'CombatZone'
*** THEN ***
Team '<This Team>' begins guarding area '[Skirmish]MyOuterPerimeter'


Uh oh... run!

We also need to make our team go back to duty once the threat is removed:

TYPE: [SAnd][ENH]
NAME: "###_BCZ_Return To Guard"
              
Code

*** IF ***
     Player '<This Player's Enemy>' has Less Than 2000 within area area 'CombatZone'
*** THEN ***
Team '<This Team>' begins guarding area 'CombatZone'


Well, that should do it for the Combat Zone. We're going to the next folder down now, "###_Team Behaviour_Priorities"

This folder has the behaviour scripts that assign the priority lists we set up all the way back in part 1. These scripts are exceedingly simple, and are all pretty much the same.

(Action in AttackPrioritySet/ApplySet)

TYPE: [SAD][ENH]
NAME: "###_BP_Generic"
              
Code

*** IF ***
    True.
*** THEN ***
Have Team '<This Team>' use Attack priority set '###_Generic'.


You need one of these for each set you have defined. If you made 7 sets, you need 7 of these scripts in this folder, one for each.

Return to top