NoAI Branch - An AI Framework

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

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

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

griffin71 wrote:TrueLight, I have a request for a function in the AIVehicle class:

Code: Select all

EngineID AIVehicle::GetEngineId(CargoID cargo, uint SequenceNum)
It should return an engine ID, or null.
SequenceNum will be an integer number, starting with zero. If only 1 engine is available for the cargoID, then that EngineID should be returned if Sequencenum == 0; if more are available, they should be returned when SequenceNum ==1, 2, ...; just as many as there are engines available.
In addition, a function that returns the Engine specs should be present, so that the AI can make a choice for the engine. (I just want to keep in mind that in the future, our great artists will almost surely design their own vehicle sets, and more than 1 good engine may become available for a certain type of cargo.)
Are there any plans for implementing this?

(I'm trying to derive some kind of empirical relation between engine type and how much hills slow them down. This is a preparation to the extension of my pathfinder to work in hills.)

Thanks!
This won't be done for a (long) while by me, and for this reason: currently we have FindBestXXXEngine, which finds the best engine for a given vehicle-type. It checks if it can refit the vehicle to the requested cargo, so it for sure can carry the cargo. As it is very similar to your request, I rather continue expending the API with ships, trains, tunnels, ... then to add such functions, which are code-wise rather hard :) (and used not that much by most of the AIs)

Of course you are free to create it yourself and send in your patch!
The only thing necessary for the triumph of evil is for good men to do nothing.
griffin71
Traffic Manager
Traffic Manager
Posts: 142
Joined: 31 Mar 2007 13:11
Location: Amsterdam

Re: NoAI Branch - An AI Framework

Post by griffin71 »

OK, thanks!
A game worth playing is a game worth modding :-)
User avatar
Nickman
Engineer
Engineer
Posts: 66
Joined: 27 Jun 2006 23:07

Re: NoAI Branch - An AI Framework

Post by Nickman »

I've been working a bit on a bus AI but I have a problem which I just can't figure out...
In the attachment you will find my AI. For now it would be able to build drive trough busstations but it can't seem to find correct spots even though it should.

At line 183, all my tiles give false, but when I go and check the tiles on the map, there are tiles that should give me true...

What am I doing wrong???

Greetz ;).
Attachments
TestAI.zip
(3.5 KiB) Downloaded 90 times
SwedishPenguin
Engineer
Engineer
Posts: 1
Joined: 03 Aug 2007 15:25
Location: Stockholm, Sweden

Re: NoAI Branch - An AI Framework

Post by SwedishPenguin »

I'm not familiar with this API or any of the code in this game, but looking through your code, this strikes me as odd. Might not be the cause of your problems though.
In TestAI::BuildBusStopsInTown at line 121:

Code: Select all

	local TownPopulation = AITown.GetLocation(town);
Should be

Code: Select all

	local TownPopulation = AITown.GetPopulation(town);
User avatar
Nickman
Engineer
Engineer
Posts: 66
Joined: 27 Jun 2006 23:07

Re: NoAI Branch - An AI Framework

Post by Nickman »

Thank you for finding that error, I just forgot to change it while copy pasting some things :).

It wasn't the source of my problem though, but I found out what it was (thanks to TrueLife). I had to set a patch to be able to build drive trough stations on town roads :). I didn't know that even existed :D.
User avatar
MagicBuzz
Tycoon
Tycoon
Posts: 1354
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

Re: NoAI Branch - An AI Framework

Post by MagicBuzz »

I just tested the lastest build, and I think there are a few issues :
- Some AI players have fluctuating profit, while they don't do anything
- Some AI players remains to "zero" revenue while they are running vehicles (and the vehicles gain money)
- I always get an assertion error when exiting the game

Is that normal the AI runs only planes ? I thought it was able to run buses as well (?)
yoyo1505
Engineer
Engineer
Posts: 32
Joined: 11 Aug 2007 16:21

Re: NoAI Branch - An AI Framework

Post by yoyo1505 »

Does anyone remember the setting on TTDPatch whereby one could change how far forward AI was analysing (AI recursion factor I think it was called)? This made their routes more sensible and efficient, and the more powerful the computer you were using the higher the value could be set at.

I haven't noticed this feature on OpenTTD (unless it's just embedded in the programming somewhere). I have noticed that most of the time the AI routes do seem more straight line than they used to be. Is it still around? If not, can we revive it?
dynaxo
Engineer
Engineer
Posts: 30
Joined: 25 Dec 2004 11:51
Location: Germany

Re: NoAI Branch - An AI Framework

Post by dynaxo »

Hi,
I just discovered this framework and I must say I love it. But while testing my ai I noticed something strange.
- AITileListBuildable tells me that water is buildable. Is it supposed to do so?
- My ai could build an airport direct onto the water. Of course it got destroyed immediately by the water. That was really fun to watch but maybe the framework should deny building on water.

Another question to squirrel:
How do I include a file with a class in it? I wanted to structurize my ai into different classes that are stored in different files but I couldn't find a way to do so.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: NoAI Branch - An AI Framework

Post by Zuu »

dynaxo wrote:Another question to squirrel:
How do I include a file with a class in it? I wanted to structurize my ai into different classes that are stored in different files but I couldn't find a way to do so.
Way back when I was writing on my Clueless AI I asked the same question. If I recall correctly this is the reson:
* OTTD don't use squirrel standard libraries due to some issues I do not remember.
-> This include the std functionality to use multiple files.

* Note: Some (by squirrel official documentation) documented std-functions have been manually implemented in OpenTTD such as some math-functions.


So for now (unless I've missed something during the last few months) you have to live with having everything in a single file. My Clueless AI is 1000+ lines in a single file. A tip to make life a bit easier is to investigate in what bookmarking capabilities your text editor of choice have.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

It is possible to include other files. As the documentation states (http://wiki.openttd.org/index.php/AI:Main_Page): "To include files inside your main.nut, you can use require(). Require() works relative from the current file, so it should be easy to use."

It does a bit more then the std of Squirrel does, as it is relative to your current file.

About your other question: sadly enough, water _is_ buildable. I understand that it is not wanted behavior of AITileListBuildable, so that behavior will be changed soon, but building the airport on water will always be possible. This has to do with some internals of OpenTTD. The first version didn't allow building on water, but that resulted in the simple fact that you couldn't build docks and locks and all other water-based structures. The only way to allow that, is to allow all buildings to build on water. In effect, you can build airports on water, which directly get flood ;) But, as AITileListBuildable won't return 'true' for water-tiles after my changes (which can take a while before they hit the branch, sorry, low on time ;)), that shouldn't be a real problem :)
The only thing necessary for the triumph of evil is for good men to do nothing.
dynaxo
Engineer
Engineer
Posts: 30
Joined: 25 Dec 2004 11:51
Location: Germany

Re: NoAI Branch - An AI Framework

Post by dynaxo »

To include files inside your main.nut, you can use require()
cool, thank you, I'll try it. I only read the Squirrel reference manual at http://squirrel-lang.org/doc/squirrel2.html. Interestingly theres no note of this function ?(
But, as AITileListBuildable won't return 'true' for water-tiles after my changes (which can take a while before they hit the branch, sorry, low on time ;)), that shouldn't be a real problem :)
That's good to know.
Thanks to both of you.
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

dynaxo wrote:
To include files inside your main.nut, you can use require()
cool, thank you, I'll try it. I only read the Squirrel reference manual at http://squirrel-lang.org/doc/squirrel2.html. Interestingly theres no note of this function ?(
Clearly you missed something really important here: it is NOT a Squirrel function. It is a function we created for you, as the similar function provided by Squirrel doesn't work relative from the current file, but relative from the executable, which is, of course, useful for us.
The only thing necessary for the triumph of evil is for good men to do nothing.
dynaxo
Engineer
Engineer
Posts: 30
Joined: 25 Dec 2004 11:51
Location: Germany

Re: NoAI Branch - An AI Framework

Post by dynaxo »

TrueLight wrote: Clearly you missed something really important here: it is NOT a Squirrel function. It is a function we created for you, as the similar function provided by Squirrel doesn't work relative from the current file, but relative from the executable, which is, of course, useful for us.
Oh, I thought the similar Squirrel function to include files is also called "require" but would simply behave in another way.
I guess I understand now. Thanks.
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Re: NoAI Branch - An AI Framework

Post by GeekToo »

Just svn'ed the branch and tried the WrightAI, works like a charm :)

But, as stated above, there's room for improvement, and I'd like to give it a try.

This gave me the following idea: the best way to test bots is to let them compete against each other, so when I think I've made an improvement, I'd like to test it against the original one.

Is this possible? I know I can start multiple instances of the same AI, but is it possible to start two or more different AI's
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

GeekToo wrote:Just svn'ed the branch and tried the WrightAI, works like a charm :)

But, as stated above, there's room for improvement, and I'd like to give it a try.

This gave me the following idea: the best way to test bots is to let them compete against each other, so when I think I've made an improvement, I'd like to test it against the original one.

Is this possible? I know I can start multiple instances of the same AI, but is it possible to start two or more different AI's
Do not use the '-a' option when starting, and it randomizes over all available AIs, so that is possible, yes. This of course isn't the easiest way, and we are working on a GUI that makes it easier. But, that can take a while :)
The only thing necessary for the triumph of evil is for good men to do nothing.
dynaxo
Engineer
Engineer
Posts: 30
Joined: 25 Dec 2004 11:51
Location: Germany

Re: NoAI Branch - An AI Framework

Post by dynaxo »

When loading a game I lose the content of all variables I used before saving the game. How do I have to deal with that?
Will there ever be a function where I can save certain values into the savegamefile so I can reload them after loading or do I have to recalculate these values every time I load?
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: NoAI Branch - An AI Framework

Post by TrueBrain »

dynaxo wrote:When loading a game I lose the content of all variables I used before saving the game. How do I have to deal with that?
Will there ever be a function where I can save certain values into the savegamefile so I can reload them after loading or do I have to recalculate these values every time I load?
There will be a save/load function in the future, but for now: don't load savegames with NoAI ;)
The only thing necessary for the triumph of evil is for good men to do nothing.
dynaxo
Engineer
Engineer
Posts: 30
Joined: 25 Dec 2004 11:51
Location: Germany

Re: NoAI Branch - An AI Framework

Post by dynaxo »

Hi,
I was in need of the MoveOrder command to restructure my orderlist, so I included it myself. I attached the patch if anyone else wants to use it.
Attachments
MoveOrder.patch
(2.76 KiB) Downloaded 76 times
dynaxo
Engineer
Engineer
Posts: 30
Joined: 25 Dec 2004 11:51
Location: Germany

Re: NoAI Branch - An AI Framework

Post by dynaxo »

Also I think I found an error. The command SkipVehicleOrder never worked for me. So I looked into the code and found the following:

Code: Select all

return this->DoCommand(0, vehicle_id, 0, CMD_SKIP_TO_ORDER);
That means, tile=0, p1=vehicle_id, p2=0

The function being used is:

Code: Select all

/** Goto order of order-list.
 * @param tile unused
 * @param flags operation to perform
 * @param p1 The ID of the vehicle which order is skipped
 * @param p2 the selected order to which we want to skip
 */
CommandCost CmdSkipToOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
So it tries to skip from order 0 to 0 which will always fail if we are at order 0 and it will alway 'skip' to order 0 else.
Personally I'd like it if I could skip to a given order instead of skipping one at a time.

Edit:
I really needed that function so I changed SkipVehicleOrder to SkipToVehicleOrder, so you can tell the function to which order you want to skip.
I attached the patch if you want to use it too.
Attachments
SkipToVehicleOrder.patch
(2.55 KiB) Downloaded 72 times
User avatar
GeekToo
Tycoon
Tycoon
Posts: 961
Joined: 03 Jun 2007 22:22

Re: NoAI Branch - An AI Framework

Post by GeekToo »

I have been experimenting with AI's already present in this thread to get to know squirrel, and I think that nice work has been done. But I found that each of the AIs focusses on a certain area, and is not very mature in other areas, so I started integrating the parts that are useful.
My first goal is to create an AI that uses buses to make money, and makes Wright sweat a little, but that will still take a while.

Also I did find the following bug, don't know if it is already known:

When I run OTTD with my AI, and put it in the background: Ctrl-Z and then : bg command I get the following assertion:

ottdsvn/branches/noai/noai/src/ai/ai_threads.cpp:423: virtual void AIThread_MT::SwitchToThis(AIThread::ThreadState): Assertion `this->state == SUSPENDED' failed.

I'm running on Gentoo Linux, has anyone seen this before, or is it my script?
Locked

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests