AI:Library

From OpenTTD
Revision as of 10:30, 11 June 2008 by TrueBrain (Talk | contribs)

Jump to: navigation, search


Contents

Libraries

To avoid code-duplication, and every AI creator doing the same work over and over, there is the library system. There are, of course, two sides on this: creating a library, and using a library.

Using Existing Libraries

To use an existing library, you just have to load it into your AI. Doing this is very simple. They layout of the library directory (bin/ai/library/):

category1/
  some_implementation/
category2/
  some_other_implementation/
queue/
  priority_queue/

Say you want to use the Priority Queue, you simply do:

import("queue.priority_queue", "PriorityQueue", 2);

The first parameter is the category and the implementation, divided with a dot ('.'). The next parameter is the name the library you import will have, in this case PriorityQueue. It can be anything you like, as long as it doesn't exist yet. The last parameter is the version you expect the library to be. To get the current version of a library, open library.nut from the corresponding directory.

The version check is very important. If you expect version 1, but on some users computer the library is in version 2, your AI will refuse to load. This is a good thing, as a new version means something changed with the existing functions, and your AI will most likely act up if you would use it. This early problem detection system should avoid many conflicts in the future.

Now how to use the library? Simple:

local pq = PriorityQueue();
pq.Insert(2);

Of course it depends on the library which parameters should be given to the constructor, and which functions exist. You'll have to open queue/priority_queue/main.nut to see all the documentation.

Creating a library

Creating a library is like creating an AI. You create a directory in a category in bin/ai/library, and you put in it a file called 'library.nut'. Now this is almost identical with 'info.nut', as you put in your AI directory, but instead of extending AIInfo, it extends AILibrary, and instead of calling RegisterAI, you need to call RegisterLibrary. All makes sense, not? An example:

class PriorityQueue extends AILibrary {
       function GetAuthor()      { return "OpenTTD NoAI Developers Team"; }
       function GetName()        { return "Priority Queue"; }
       function GetDescription() { return "An implementation of a Priority Queue"; }
       function GetVersion()     { return 2; }
       function GetDate()        { return "2008-06-10"; }
       function CreateInstance() { return "PriorityQueue"; }
}
RegisterLibrary(PriorityQueue());

Now what you need to do is to create a file called 'main.nut'. It should contain a class you named at CreateInstance(), like in your AI. Then the same rules apply as an AI. require() works as you expect, and you can define your class how ever you like.

There are some things you can not do:

  • Register 2 libraries in one library.nut
  • Define more than 1 class in the main.nut (or any required file). However, you can define sub-classes. Say you need an extra class in PriorityQueue. You can name it PriorityQueue.Subclass.

Standard libraries

There are several libraries for common tasks bundled together with NoAI. These have been prepared by 'OpenTTD NoAI Developers Team'.

Category: Graph

Aystar

This is an implementation of AyStar pathfinding algorithm. It solves graphs by finding the fastest route from one point to the other.

Required libraries: queue.binary_heap

Category: Queue

Binary Heap

This is an implementation of binary heap data structure.

Priority Queue

This is an implementation of priority queue data structure.

Personal tools