Welcome to my article about AI Scripting. If you want to know what AI
scripting is, or how to do it, you've come to the right place! This guide
will teach you the basic principles of creating AI scripts, and some more
advanced techniques to improve your abilities. This guide was made with the
intent of teaching people how to make AI for scenaio design purposes, but
hopefully everybody will be able to make use of it! Happy scripting!
Before I get underway with my guide, you'll probably want to
know what the topic at hand is all about. What is AI scripting all about?
Well, AI stands for
means methods that a computer uses to simulate thought. It tells it how to
make decisions based on your interactions, and what to do in certain
situations. Almost every time you make your computer do something, it is
using AI.
What form does it take?
The AI that we will be dealing with in this guide is merely a set of
instructions, typed into a text editor, and used in certain ways to create
effects. That's all you will need to create AI, a text editor (I recommend
NotePad if you are using Windows).
Don't worry if you didn't quite take all that in just yet. AI is a tricky
subject. Here's what we have learned so far:
- AI is the method a computer uses to simulate thought.
- It is created in a text editor.
Game:
Well, at least you know what AI is now. It is used differently
with all things, and you'll probably we itching to find out what it can be
made to do in-game.
In Star Wars: Galactic Battlegrounds, you can create custom AI scripts or
edit existing scripts to edit how the computer plays in-game, i.e. its
stategy. The computer has a default AI script built into it that tells it
how to play, but creating your own script will change that. Do you want the
computer to rush faster? Do you want it to be more agressive? AI scripting
can make that happen. It can also be used in conjunction with the scenario
editor, and interact with triggers to create powerful effects in-game. AI
for SWGB is a ver powerful tool
What you should understand by now:
- AI is the method a computer uses to simulate though.
- It is created in a text editor.
- In SWGB, it tells the computer how to play the game.
- It can be used to create scenario designing effects.
- It is created in a text editor.
It's time to learn how you put these scripts into the game, so
you're all set for when you can create them yourself. It's actually quite
simple to put a created script into effect in the game, but it's a step that
many beginners stumble over for some reason
Right, to get a script into the game, you need a script
understand this yet, it's just to show you an example of a custom script in
a game. First, open up NotePad (I always use it, but if you don't have it
for some reason, a simple, easy to use text editor is the best). Then,
copy/paste the following into your new document:
(defrule
(food-amount >0)
=>
(chat-local-to-self "My first AI script")
)
That is the script we will use for now. I'll explain what it
means later.
Right, now you have your script, you need to save it. However, AI scripts
cannot just be saved as regular text files. You can try it, but it won't
work! We need to save it as a .per file.
What is a .per file?
For the game to understand the script, it needs to be a .per file. If it
isn't, the computer will skip over it and won't think it's a script.
How do you make it into a .per file?
That is easy. When you save your notepad file, save it as myfirstai.per, and
set the file type to "all files." Of course it doesn't have to be
myfirsai.per, it can be immobile.per, agressive.per, bigbuttocks.per, or
absolutely anything you choose, as long as it is something with .per on the
end. The name you give it now will be its name in the game. For now, save it
as myfirstai.per.
Where do I save it to?
You save your .per file into your SWGB/Game/AI folder. If you don't save it
there, it won't work
What next?
You've got your .per file and saved it to the right directory, but there's
one more step that you need to take before the computer will recognise the
AI script in-game. This step, however, is much easier. Open up NotePad and
create a new document. Then, without typing anything, save it as
myfirstai.ai. If you saved your .per file under a different name to
myfirstai, then just save the .ai file under the same name as that, instead.
They must have the same name apart from the .ai and .per endings. Save the
.ai file to the same directory as you did the .per file (SWGB/Game/AI). The
.ai file doesn't at all affect the script, it just tells the computer that
there should be a script called samenameasdotaifile.per in the AI folder.
If you have follwed these steps correctly, your new AI will be all ready for
you to use. To test it, run SWGB. Go to the screen where you configure your
random map settings, and for Player 1, under name, set it to myfirstai.
Then, start a random map, with any settings. Upon entering the game, you
should constantly receive chat messages saying "My first AI Script." If you
do, it worked. If you get an error message, or it doesn't give you chat
messages, then you've done something wrong. Go back and double check all of
the instructions I gave you.
Summary of what you now know:
- AI is the method a computer uses to simulate though.
- It is created in a text editor.
- In SWGB, it tells the computer how to play the game.
- It can be used to create scenario designing effects.
- To make your script work, you need to save a .per and a .ai file to your
Game/AI folder- You test your script by choosing it in the "Player Name" option in the
random map settings. - It is created in a text editor.
So, now you know what an AI script is, and how to install a
custom script, but you don't know how to write them yet
tutorial will teach you just that - how to write AI scripts.
Let's start by defining a rule. An AI script is made up of
many rules that tell the computer how to make decisions. They basically
state: "If
programming experience, that is a simple if-then statement.
Here is how a rule is layed out:
(defrule ; This always starts a rule
(fact) ; A valid fact goes here
=>; Signifies the end of the fact(s), and the start
of the action(s)
(action) ; A valid action goes here. If the fact above is
true, the action will take place.
)
Of course, this isn't a valid rule, as where it says fact and
action, you must replace the text with valid facts and actions (I'll go over
those later). This is just to show you how a rule is set out. The
fact and the beginning of the action(s), and the
rule. Remember,
that into your mind now, and you won't go far wrong. Everything behind the
semi-colon (
type whatever you like after a comment, and you won't get errors. You don't
have to have them, but they are convenient.
Now let's look at a valid rule. We'll use the one you used in your first
script earlier:
(defrule
(food-amount >0)
=>
(chat-local-to-self "My first AI script")
)
For now, I'll tell you that the fact,
means
myself saying "My first AI script")
Now, try and guess what this rule does. Highlight the below text to check
your answer.
If you have more than 0 food (the fact), you will recieve a chat message
from yourself saying "My first AI script" (action)..
Did you get it right? Good
Well, an AI script is made up of almost all rules, so you're not far off
being able to write your own scripts
write facts and actions.
Summary so far for this chapter:
- Rules are the basis of an AI script
- They state that if a fact is true, then an action is performed.
- They always start with (defrule, end with ), and have a =>
in the
middle. - They state that if a fact is true, then an action is performed.
You know what facts are and how they fit into a rule, you just
need to learn how to write them.
Facts are generally layed out like so:
The game stat is something in the game which can be recorded and has a
value. For example,
general rule, all game stats have a dash (-) for spaces. There is a list of
all available game stats below.
The rel-op (standing for Relative-Operator) is the method used to compare
the game stat with the value. These are the rel-ops:
!= (not equal to)
>
<
<
>
Examples:
([game stat] == [ value] ) ; Is true if the game stat is equal to the value.
([game stat] != [ value] ) ; Is true if the game stat is not equal to the
value.
([game stat] > [ value] ) ; Is true if the game stat is greater than the
value.
etc...
The value could be anything fitting to the game stat. For example, the game
stat
tech-level-3, or tech-level-4. Just use whatever applies. In most cases,
it is a number.
So, here are some examples of complete, valid facts:
(food-amount >0)
(current-age != tech-level-1)
(civilian-population == 40)
(civ-selected GUNGANS) ;note - has no rel-op
Of course, there are some facts which are exceptions to the
needs no rel-op or value. The fact
a value of a building. For example,
There are also the facts
always true, and the False fact is always false (duh :)).
Rules can also have more than one fact. It would be set out like
this:
(defrule
(fact 1)
(fact 2)
=>
(action)
)
If you have more than one fact in a rule, all facts must be
true before the action takes place.
Summary of chapter so far:
- Rules are the basis of an AI script
- They state that if a fact is true, then an action is performed.
- They always start with (defrule, end with ), and have a =>
in the middle. - Facts usually have 3 parameters: a game stat, a rel-op, and a
value. - They state that if a fact is true, then an action is performed.
Right, now you know how to create facts, here's a list of
different facts you can use:
true
false
attack-soldier-count <rel-op> < value>
attack-warboat-count <rel-op> < value>
building-available <building>
building-count <rel-op> < value>
building-count-total <rel-op> < value>
building-type-count <building> < rel-op> < value>
building-type-count-total <building> < rel-op> < value>
can-afford-building <building>
can-afford-complete-wall <perimeter> < wall-type>
can-afford-research <research-item>
can-afford-unit <unit>
can-build <building>
can-build-gate <perimeter>
can-build-gate-with-escrow <perimeter>
can-build-wall <perimeter> < wall-type>
can-build-wall-with-escrow <perimeter> < wall-type>
can-build-with-escrow <building>
can-buy-commodity <commodity>
can-research<research-item>
can-research-with-escrow <research-item>
can-sell-commodity <commodity>
can-spy
can-spy-with-escrow
can-train <unit>
can-train-with-escrow <unit>
cc-players-building-count <player-number> < rel-op> < value>
cc-players-building-type-count <player-number> < building> < rel-op>
<value>
cc-players-unit-count <player-number> < rel-op> < value>
cc-players-unit-type-count <player-number> < unit> < rel-op> < value>
cheats-enabled
civ-selected <civ>
civilian-population <rel-op> < value>
commodity-buying-price <commodity> < rel-op> < value>
commodity-selling-price <commodity> < rel-op> < value>
current-age <rel-op> < age>
current-age-time <rel-op> < value>
current-score <rel-op> < value>
death-match-game
defend-soldier-count <rel-op> < value>
defend-warboat-count <rel-op> < value>
difficulty <rel-op> < difficulty>
doctrine <value>
dropsite-min-distance <resource-type> < rel-op> < value>
enemy-buildings-in-town
enemy-captured-relics
escrow-amount <resource-type> < rel-op> < value>
event-detected <event-type> < event-id>
food-amount <rel-op> < value>
game-time <rel-op> < value>
goal <goal-id> < value>
nova-amount <rel-op> < value>
housing-headroom <rel-op> < value>
idle-farm-count <rel-op> < value>
map-size <map-size>
map-type <map-type>
military-population <rel-op> < value>
player-computer <player-number>
player-human <player-number>
player-in-game <player-number>