Library System

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Library System

Post by TrueBrain »

As of today (and in the nightly binary which is compiled in 2 hours), we proudly introduce to you: a library system. To give a bit insight information:

2 days ago we had a long talk how we should introduce a pathfinder in an 'extra' package, as I described in a few posts earlier. After some debating we came to the conclusion that there is absolutely no fair way to do that in C++. Although a pathfinder would operate faster in C++, the difference isn't big enough. So we decided to make a library framework in Squirrel, where people can put their general libraries, and an easy way to access them. I just committed exactly that. In the coming days / weeks / months, we will be adding some things we think is useful, under which some queues, but also a basic pathfinder, and more of such tools all AIs will want to use.

Some positive things about this library system: it doesn't force you to use any of them. You can always write your own. It allows multiple implementations of the same thing, like more than one pathfinder. It is portable. It is secure.

Now how does it work? I will write a wiki page about it soon. Basically:

in bin/ai/library there are dirs, 'categories'. In a category there is a dir, the library itself. In that dir is a file called 'library.nut', similar to 'info.nut'. It gives some basic information about the library, and is loaded on OpenTTD startup. The idea is equal to 'info.nut' in all ways, but you don't do: RegisterAI, you do: RegisterLibrary. Easy as pie ;)

Now from your AI you can run:

Code: Select all

import("categorie.name", "LocalName", version);
Mind that it is 'import', not 'require'. You use 'require' to load your own local files.

The LocalName is how you want to class to be called in your script. This is useful, as there will be multiple implementation of the same thing. By changing one line, you can switch between those implementations, without changing everything in your AI. Also, it avoids a lot of name collision, and other problems of that order.

The version part makes sure you talk to library you expect. Say you made your AI for sets.priority_queue version 1, but in the library-dir of a user is version 2. Your AI will now fail to load on his machine, as there might be a mismatch in operations. This is an early problem detection, which hopefully avoids a lot of problems. So, make sure to fill in the right version ;)

I think that sums it up pretty well.. any questions, feel free to ask. Sorry that I had to post this in this topic again, as it is becoming unreadable, but we are trying to get our own NoAI subforum, which hopefully makes this easier on all of us ;) (http://www.tt-forums.net/viewtopic.php?f=21&t=37891)

Ps: if any of you have a library you think should be included as official library (in SVN), please send it to me. Sending in any library for inclusion means you give up the rights of the library, and it becomes GPL copyrighted to OpenTTD.
The only thing necessary for the triumph of evil is for good men to do nothing.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: NoAI Branch - An AI Framework

Post by Zutty »

TrueLight wrote:As of today (and in the nightly binary which is compiled in 2 hours), we proudly introduce to you: a library system. To give a bit insight information:

2 days ago we had a long talk how we should introduce a pathfinder in an 'extra' package, as I described in a few posts earlier. After some debating we came to the conclusion that there is absolutely no fair way to do that in C++. Although a pathfinder would operate faster in C++, the difference isn't big enough. So we decided to make a library framework in Squirrel, where people can put their general libraries, and an easy way to access them. I just committed exactly that. In the coming days / weeks / months, we will be adding some things we think is useful, under which some queues, but also a basic pathfinder, and more of such tools all AIs will want to use.

Some positive things about this library system: it doesn't force you to use any of them. You can always write your own. It allows multiple implementations of the same thing, like more than one pathfinder. It is portable. It is secure.

Now how does it work? I will write a wiki page about it soon. Basically:

in bin/ai/library there are dirs, 'categories'. In a category there is a dir, the library itself. In that dir is a file called 'library.nut', similar to 'info.nut'. It gives some basic information about the library, and is loaded on OpenTTD startup. The idea is equal to 'info.nut' in all ways, but you don't do: RegisterAI, you do: RegisterLibrary. Easy as pie ;)

Now from your AI you can run:

Code: Select all

import("categorie.name", "LocalName", version);
Mind that it is 'import', not 'require'. You use 'require' to load your own local files.

The LocalName is how you want to class to be called in your script. This is useful, as there will be multiple implementation of the same thing. By changing one line, you can switch between those implementations, without changing everything in your AI. Also, it avoids a lot of name collision, and other problems of that order.

The version part makes sure you talk to library you expect. Say you made your AI for sets.priority_queue version 1, but in the library-dir of a user is version 2. Your AI will now fail to load on his machine, as there might be a mismatch in operations. This is an early problem detection, which hopefully avoids a lot of problems. So, make sure to fill in the right version ;)

I think that sums it up pretty well.. any questions, feel free to ask. Sorry that I had to post this in this topic again, as it is becoming unreadable, but we are trying to get our own NoAI subforum, which hopefully makes this easier on all of us ;) (http://www.tt-forums.net/viewtopic.php?f=21&t=37891)

Ps: if any of you have a library you think should be included as official library (in SVN), please send it to me. Sending in any library for inclusion means you give up the rights of the library, and it becomes GPL copyrighted to OpenTTD.
Nice! I'm glad I never got around to actully posting the code now!! I'm going to need a little time to re-work the stuff I had planned to out in there then.
PathZilla - A networking AI - Now with tram support.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 8 guests