Computer Player Strategy Builder Guide
Translations of this material:
- into Russian: Перевод "Computer Player Strategy Builder Guide". 0% translated in draft.
-
Submitted for translation by MaxPv 14.02.2010
Text
Introduction
Age of Empires II: The Conquerors Expansion uses an all-new Artificial Intelligence (AI) Expert System to act as the intelligence behind the computer players. This expert system uses a series of Rules that are tested to cause various actions to take place. In the following sections, you will learn how to make new rules for the AI to follow, how to check game facts to trigger those rules, and how to command the AI to take action based on your instructions.
AI Files are text files that have their extension changed to '.per' (for example, 'Henry Tudor.per'). These files contain the script commands that are used to create a new customized computer player. Use a text editor (one that displays line numbers is very helpful) to create your AI scripts, and copy them into the directory where you installed Age of Empires II, in the AI folder.
An empty text file with an .AI extension (for example, 'Marko.ai') should be created to make an entry appear in the list of computer players and in the scenario editor. The game will try to find a file with a matching .per extension that will be started when you pick that player. In your game's AI directory, you would then have 2 files:
C:\Program Files\Microsoft Games\Age of Empires II\AI\MyAI.ai (This is an empty file that makes an entry in the game's list)
C:\Program Files\Microsoft Games\Age of Empires II\AI\MyAI.per (This file contains your custom AI script of rules and facts)
You must have both a .PER and an .AI file with the same name for your script to function!
Rules
Rules are the basis for the Expert System. There is a list of things we know about the game world, the other players, and so on. These are called facts. We check the facts with rules until a set of conditions exists that we need the computer player to act upon. Actions are what we call those commands that cause things to happen in the game. Examples might be training a unit,
researching a technology, or sending a chat message.
Defining Rules
Rules are defined in the script with the defrule instruction. If the conditions for the rule are met (True), the instructions in that rule are followed. If the conditions for the rule are not met (False), the rule is passed by.
A defrule example:
(defrule
(cheats-enabled)
=>
(some action takes place)
)
Note that the parentheses around the rule are required – though the white-space formatting (spaces, tabs, etc.) is not important.
Rules continue to be evaluated until they are disabled. This is done with the disable-selfcommand
(defrule
(true)
=>
(disable-self)
)
Going through the entire list of rules checking each one is what we call a rules pass. This system is very efficient, the rules may be checked as often as several times a second.
Comment Lines
You will see comments throughout the AI Expert System script files. These comments start with a semicolon (;). For example:
;This line is a comment
(defrule
(food-amount greater-than 75)
=>
(train villager) ;comments at end of line too!
)
.
Any text that follows a semicolon on that line is a comment and is ignored by the AI script.
Once you define the rules, you can then use the all of the available facts and actions in combinations to do any action possible in the game.
Facts
Facts are those things that are tested in the rules. Player information such as how much gold you have, opponent information such as score, or game information such as time or victory conditions are some examples.
Using Facts
Facts are used to enable rules. For example, this will train villagers whenever we have 50 food:
(defrule
(food-amount greater-than 50)
=>
(train villager)
)
See: Rules
Testing Facts
You will see <rel-op> associated with a lot of facts, this is a relative operator and allows you to test the relationship of the fact to another value, an example might be if you wanted the rule to be enabled when a certain amount of wood had been collected:
(defrule
( wood-amount greater-than 1000)
=>
( chat-to-all “I have 1000 wood!”)
)
For more detailed information about parameters, see the "Parameters" section later in this document.
Fact Parameters
Some facts can be tested just by checking them directly. If you want to see if cheats are enabled in the game, you can make a rule like this:
(defrule
(cheats-enabled)
=>
(chat-to-all ”Let the cheating begin!” )
)
Other facts are more complex and require you to supply additional data that is used by the fact information like the civilization <civ> or the map size <map-size> is required. Any fact that lists parameters <example> requires those parameters in order to work correctly.
Actions
Actions are those things you want the AI to do when it executes your rules. Actions can cause the AI player to build a building, train a unit, or send a chat message to a player, for example. Rules enable your computer player to take any action a human player could.
Defrule Command
This command creates a new rule.
Example:
(defrule
(game-time greater-than 30)
=>
(resign)
)
Defconst Command
This command creates a user-defined constant. For more information on defconst, see the "Conditional Loading and User-Defined Constants" section later in this document.
Syntax:
(defconst <constant-name> <value>)
<constant-name> is a user selected name. Use of the same format that the rest of the system uses is recommended but not required (for example, words-separated-with-dashes).
<value> is a valid integer value that will fit in a C++ type short. (For non-programmers, this means that the value can not be less than -32768 or greater than 32767.)
The following example defines a decided number of villagers in the Dark Age: (defconst numdarkagevillagers 22)
The following rule then uses it:
(defrule
(civilianpopulation < numdarkagevillagers)
(cantrain villager)
=>
(train villager)
)
Constants are very handy for naming of goals, goal values, timers, taunts, etc.
Without constants all of these would be just nameless numbers.
Tip: If you group all of your defconsts together in one file, it makes it easy to customize your AI by changing the number that the defconst represents without having to change it everywhere in your file. In the example above, if you referred to num-dark-age-villagers in many places in your AI, you could easily change the defconst to be 12 villagers by changing it in just one place. Load Command The Load command allows you to supply a filename of another script file within your main script file. This akes it easier to organize and re-use parts of your scripts in new ways.
Script language supports loading of script files from script files. Loaded files are in every aspect the same as original script files, so any script file can be loaded by any other script file.
Syntax:
(load "filename")
Load command can be inserted anywhere between the rules. For example:
(defrule ...........................)
(load "Dark Age Economy")
(defrule ...........................)
Notice that the filename does not have path or an extension. The script interpreter automatically adds a path and an extension. A script file being loaded should be placed in the same directory as the file that is loading it. It is important to mention that the load command executes immediately. This means that when a load command is encountered, parsing of the current file is suspended until the load command finishes. At that point parsing resumes, starting with a rule immediately following the load
command. Load commands can be nested (a script that loads another script) up to 10 levels deep. Loading multiple script files from a top-level script file makes computer players' knowledge modular. This approach has a benefit only if the script files loaded do not have overlapping areas
of expertise.
