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

####Part 3####

Before you open worldbuilder, you'll need to make sure you've set up your AIData.INI file. This file lives in INI/Default and consists of three main chunks:

The first defines some basic constants used by the AI. Ignore these, you can fiddle if you want, but I don't advise it.

The Next is the skillset section where the generals powers used by each side are defined. The bog standard America one looks like this:

              
Code

SideInfo America
    ResourceGatherersEasy     = 2
    ResourceGatherersNormal = 2
    ResourceGatherersHard     = 2
    BaseDefenseStructure1     = AmericaPatriotBattery

    SkillSet1
     Science = SCIENCE_PaladinTank
     Science = SCIENCE_StealthFighter
     Science = SCIENCE_A10ThunderboltMissileStrike1
     Science = SCIENCE_A10ThunderboltMissileStrike2
     Science = SCIENCE_A10ThunderboltMissileStrike3
     Science = SCIENCE_SpectreGunshipSolo
     Science = SCIENCE_DaisyCutter
    End

    SkillSet2
     Science = SCIENCE_PaladinTank
     Science = SCIENCE_StealthFighter
     Science = SCIENCE_Pathfinder
     Science = SCIENCE_Paradrop1
     Science = SCIENCE_Paradrop2
     Science = SCIENCE_Paradrop3
     Science = SCIENCE_DaisyCutter
    End

End


For each of your factions you need to make one of these, make sure you don't give the AI more sciences than it can possibly purchase during the game. The different sets are... different sets that the AI uses. You define which it uses in scripts.

The last chunk is the bit which tells the AI what buildings to build and where. The bog standard USA one is:

              
Code

;Skirmish AI Build List
SkirmishBuildList America
    Structure AmericaCommandCenter
     Location = X:501.22 Y:546.25
     Rebuilds = 0
     Angle = -135.00
     InitiallyBuilt = No
     AutomaticallyBuild = Yes
    END;Structure AmericaCommandCenter
    Structure AmericaPowerPlant
     Location = X:770.43 Y:727.79
     Rebuilds = 0
     Angle = -45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaPowerPlant
    Structure AmericaPowerPlant
     Location = X:759.90 Y:555.84
     Rebuilds = 0
     Angle = -45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaPowerPlant
    Structure AmericaPowerPlant
     Location = X:374.82 Y:371.69
     Rebuilds = 0
     Angle = -45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaPowerPlant
    Structure AmericaPowerPlant
     Location = X:477.11 Y:174.62
     Rebuilds = 0
     Angle = -45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaPowerPlant
    Structure AmericaPowerPlant
     Location = X:189.19 Y:522.00
     Rebuilds = 0
     Angle = -45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaPowerPlant
    Structure AmericaPowerPlant
     Location = X:418.35 Y:827.10
     Rebuilds = 0
     Angle = -45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaPowerPlant
    Structure AmericaAirfield
     Location = X:767.04 Y:376.40
     Rebuilds = 0
     Angle = -135.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaAirfield
    Structure AmericaBarracks
     Location = X:247.52 Y:734.59
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaBarracks
    Structure AmericaParticleCannonUplink
     Location = X:594.59 Y:635.72
     Rebuilds = 0
     Angle = -45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaParticleCannonUplink
    Structure AmericaStrategyCenter
     Location = X:384.36 Y:652.24
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaStrategyCenter
    Structure AmericaSupplyDropZone
     Location = X:515.66 Y:347.88
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaSupplyDropZone
    Structure AmericaSupplyDropZone
     Location = X:637.41 Y:490.20
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaSupplyDropZone
    Structure AmericaSupplyDropZone
     Location = X:488.83 Y:760.27
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaSupplyDropZone
    Structure AmericaSupplyDropZone
     Location = X:322.39 Y:502.03
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaSupplyDropZone
    Structure AmericaWarFactory
     Location = X:650.93 Y:843.45
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaWarFactory
    Structure AmericaWarFactory
     Location = X:259.56 Y:289.71
     Rebuilds = 0
     Angle = -135.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaWarFactory
    Structure AmericaPatriotBattery
     Location = X:676.07 Y:670.51
     Rebuilds = 0
     Angle = 45.00
     InitiallyBuilt = No
     AutomaticallyBuild = No
    END;Structure AmericaPatriotBattery
END;SkirmishBuildList FactionAmerica


Simply copy and pop in your buildings. You don't have to fill every "slot" and if you do, you can make more. Just be careful about locations. Also, don't worry about having things in the list which aren't part of that faction. Again, this file on its own will not do anything. You must specifically tell the AI to try to build something with scripts. Of course, if you try to tell it to build something that it can't, then it won't. In my Only War AI the factions only have a handful of buildings, but the AIData list has all sorts of GLA crap listed, just in case I wanted to add more buildings.

If you've got that ready, it's time to add some more basic bits. Let's start with the "###_Base Building" folder. (Which requires your AIData.INI fie :p)

Here's the first one: (Actions in Team/AI and Skirmish Only/Build)

TYPE: [nsAD][ENH]
NAME: "###_Build Initial"
              
Code

*** IF ***
    True.
*** THEN ***
[???]Start building team Team '###_Initial Builder 2'
Build a building of type '[No-Prerequisite Building 1]'
Build a building of type '[No-Prerequisite Building 2]'


Obviously in yours, you will choose a building instead of what I have in square brackets. Choose buildings that DO NOT HAVE PREREQUISITES (e.g. power plant and barracks) as the AI will run this script instantly, and if you happen to supply more than one builder at start, or have a builder than builds exceedingly fast, the AI will assign that builder to any buildings still waiting. The second construction unit is built using this script too, except you have not made the team that is referred to, hence question marks (you'll see these whenever a script has faults... [???]). Ignore them for now. Mine for Only War looks like this: (The last line is added to make the AI garrison the troops it starts with (Only War starts the player with a builder and a squad of basic Troops) as they're not much use otherwise.)

              
Code

*** IF ***
    True.
*** THEN ***
Start building team Team 'CAD_Initial Builder 2'
Build a building of type 'CadianBarracks'
Build a building of type 'CadianGeneratorium'
Build a building of type 'CadianGeneratorium'
Team 'teamSkirmishCadians' garrison a nearby building.


Now, depending on how complicated your prerequisite system is, you might have quite a few scripts in here. The next one should be the next BASIC building that the player can build once the initial buildings have been constructed.

Script two: (Conditions in Player/Owns and actions in Skirmish Only/Build)

TYPE: [nsAD][ENH]
NAME: "###_Build Initial Warfactory"
              
Code

*** IF ***
     Player '<This Player>' has Greater Than or Equal To 1 unit or structure of type '[No-Prerequisite Building 1]'
    *AND* Player '<This Player>' has Greater Than or Equal To 1 unit or structure of type '[No-Prerequisite Building 2]'
*** THEN ***
Build a building of type '[Prerequisite Building 1]'
Build a building of type '[Prerequisite Building 2]'


Basically starts building once the AI has met the conditions, again, it doesn't matter what you set as conditions here, whether the AI can build it or not is determined by prerequisites in the buildings INI. By splitting them like this, the AI won't give up trying to build something it can't. You can keep adding these things until you have enough scripts for your AI to build a basic base. For example, if you need power to build a refinery, then a refinery to build a tank factory, that's 3 scripts you'll need.

Now, you want to start altering what the AI builds depending on the difficulty level. The reason I said specifically use the first few scripts for basic buildings is that the AI needs to build these structures to be able to play. Everything else is optional, such as defence, advanced buildings, extra barracks/factories.

Speaking of defence:

TYPE: [nsAD][E]
NAME: "###_E_Build Initial Defences"
              
Code

*** IF ***
     Player '<This Player>' has Greater Than or Equal To 1 unit or structure of type '[Prerequisite Building]'
*** THEN ***
Build one additional '[Basic Defensive Building]', on the flank.
Build one additional '[Basic Defensive Building]', on the front.


TYPE: [nsAD][N]
NAME: "###_N_Build Initial Defences"
              
Code

*** IF ***
     Player '<This Player>' has Greater Than or Equal To 1 unit or structure of type '[Prerequisite Building]'
*** THEN ***
Build one additional '[Basic Defensive Building]', on the flank.
Build one additional '[Basic Defensive Building]', on the front.
Build one additional '[Basic Defensive Building]', on the flank.
Build one additional '[Basic Defensive Building]', on the front.


TYPE: [nsAD][H]
NAME: "###_H_Build Initial Defences"
              
Code

*** IF ***
     Player '<This Player>' has Greater Than or Equal To 1 unit or structure of type '[Prerequisite Building]'
*** THEN ***
Build one additional '[Basic Defensive Building]', on the flank.
Build one additional '[Basic Defensive Building]', on the front.
Build one additional '[Basic Defensive Building]', on the flank.
Build one additional '[Basic Defensive Building]', on the front.
Build one additional '[Basic Defensive Building]', on the flank.
Build one additional '[Basic Defensive Building]', on the flank.


As you can tell, this makes the AI build better defence as the difficulty increases. Of course, you can add as many as you want, and add different types. Make sure that your AI isn't trying to build too many things simultaneously, it's a good idea to make sure that your conditions won't result in the AI trying to build, say, extra power, turrets and a war fac at the same time. You want to control the order of construction to make sure the AI doesn't accidentally run out of power too. If you think it will run out, add another power plant to the first building script. (That's why mine for Only War has the AI make 2 power plants). With the Hard one, you can make it build the power in the "###_H_Build Initial Defences" script as it'll probably be the one testing its energy grid.

Return to top