AI:Introduction
From OpenTTD
Development
AI Programming
- API Documentation
- Introduction
- info.nut file
- Basics
- Using the pathfinder
- Saving / Loading data
- Things you need to know
- Squirrel
- Lists
- Coping with OTTD errors
- Trams
- AI Libraries
AIs
To start, the language to write your AI in, is Squirrel. Squirrel is a very simple and C++ like scripting-language. For an introduction in Squirrel check the Squirrel Introduction page. The reason we picked Squirrel, is that it looks very similar to C++.
In this introduction we take you through the process of creating a basic AI. While doing so, we will explain to you the very basics you need to create an AI and an AI in OpenTTD. For advanced AI handling we suggest you to buy a good book. The end-result of this tutorial can be found back in the 'example' directories.
Contents |
[edit] Creating your own AI
Of course you want to start with your own AI.
A general rule of the thumb is to write extensive documentation for your AI, so after a few days you can easily remember what the functions are supposed to do. Another way to help with this is to split the AI into smaller, logical files.
[edit] Squirrel
- Create a new directory in
bin/ai/(source-builds) orai/(binary-builds). For exampleMyNewAI - Create a file called
info.nut. This is important as the Squirrel loader looks formain.nutin all subdirectories. If it doesn't findinfo.nut, the directory is considered to be something else, and your AI won't be loaded. - Open
info.nutand then continue reading the next section.
[edit] Register
You want to let OpenTTD know you have an AI that wants to take over a company. For this, you need to add a class that extends AIInfo:
class MyNewAI extends AIInfo {
function GetAuthor() { return "Newbie AI Writer"; }
function GetName() { return "MyNewAI"; }
function GetDescription() { return "An example AI by following the tutorial at http://wiki.openttd.org/"; }
function GetVersion() { return 1; }
function GetDate() { return "2007-03-17"; }
function CreateInstance() { return "MyNewAI"; }
}
What this does is tell OpenTTD how your AI is called, its version, and other general information. After this you need to create an instance of the class. Squirrel:
/* Tell the core we are an AI */ RegisterAI(MyNewAI());
This is enough to initialise your AI. But, we are not quite there yet to give it a first run. We don't have a real AI yet, only a piece of code that tells what your AI is, and who wrote it. So, let's make a class that will be our AI.
[edit] Your First AI
To do this, create a file main.nut in the same dir, and make a class that extends AIController.
class MyNewAI extends AIController {
function Start();
}
function MyNewAI::Start()
{
while (true) {
AILog.Info("I am a very new AI with a ticker called MyNewAI and I am at tick " + this.GetTick());
this.Sleep(50);
}
}
The first block creates the class extending AIController.The second blocks creates a Start() function, which is called once when your AI is started. If you exit from this function, your AI dies. Therefore we create a while() which never stops.
You don't need to worry about your AI clogging the game by eating all the CPU of your computer (and thus stopping the game if self from running), the engine itself will sleep every X opcodes which forces fair scheduling and allows the game to run smoothly. You can however put the AI to sleep manually by calling the Sleep() command. You will see when we start this AI in a moment, that you get a print line in your console every 50 ticks.
So, let's start our AI. As AIs are currently picked randomly from all AIs that are registered, it can be a bit hard to get your AI started if you're just relying on chance. Later there will be added a GUI that can control this, but for now you have to add the following section in your openttd.cfg:
[ai_players] none = MyNewAI =
Each line correspond to a company slot. So in this case when an AI is started as second player, it will try to load MyNewAI. If the AI doesn't exist, it will select an AI randomly. Other companies will use AI randomly selected. If you want to force MyNewAI for all companies, just add more
MyNewAI =
lines. You can of course force other AIs using this way.
Additionally, on Windows Vista you will need to run openttd.exe as administrator. Either right click and choose "Run as Administrator" each time, or turn off UAC for Administrators (Instructions). If OpenTTD is not run as administrator the AI directories will be invisible to the executable, and loading any AI will result in an "The AI named 'XYZ' is unknown or not suitable" error.
Make sure to change your difficulty settings so that AIs start immediately, and add 1 or more AIs. Soon you will see a new company starting, hopefully with your AI attached. If you open the AI Debug Window, which can be found under the QuestionMark icon, you will see a nice line printing we are active. You can also manually start your newly created AI by typing the command
start_ai MyNewAI
into the in-game console (opens with ` key)
Any errors that are found, both compile-time and run-time, are send to the AI Debug Window.
You can also redirect the output to the in-game console by opening in-game console console and typing
developer 2
Enable output of print statements from your AI by typing
debug_level ai=4
So, there you have your first AI. Of course now we need to add functions. For this we advise you continue to the next chapter Basics. You only have to know that all your code should go in Start(). One more thing you should know: never create global variable. Keep everything in your class instance. But this is just a house-rule and if you don't know what it means, it doesn't really matter at this point.
