Some API extension ideas

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
Chruker
Engineer
Engineer
Posts: 49
Joined: 01 Jun 2009 20:13

Some API extension ideas

Post by Chruker » 08 Jul 2009 23:00

I was unable to find a function that returned the design year of a vehicle, so something like:
AIEngine::GetDesignYear (EngineID engine_id)

Also some functions for AIAbstractList:
- A way to get the list items as a squirrel array (discarding the values)
- Counting functions to complement the RemoveAboveValue, RemoveBelowValue, RemoveBetweenValue, RemoveValue

Edit: And ofcourse these constants for sorting direction:
AIAbstractList.SORT_ASCENDING <- true;
AIAbstractList.SORT_DESCENDING <- false;
Last edited by Chruker on 19 Jul 2009 13:34, edited 1 time in total.

Yexo
Tycoon
Tycoon
Posts: 3653
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo » 09 Jul 2009 07:00

Chruker wrote:I was unable to find a function that returned the design year of a vehicle, so something like:
AIEngine::GetDesignYear (EngineID engine_id)
What is the reason for wanting to know the design year? Before that year the engine is unavailable, and unavailable EngineIDs are invalid for the AI. So you would only be able to call GetDesignYear when the engine is available, and that seems to defeat the point.
Also some functions for AIAbstractList:
- A way to get the list items as a squirrel array (discarding the values)
A good idea.
- Counting functions to complement the RemoveAboveValue, RemoveBelowValue, RemoveBetweenValue, RemoveValue
Are you asking for seperate functions (CountAboveValue, CountBelowValue, etc.) or are you asking that RemoveAboveValue should return the number of items removed?

User avatar
Abenhor
Engineer
Engineer
Posts: 33
Joined: 08 Jul 2009 20:06
Location: Spain

Re: Some API extension ideas

Post by Abenhor » 09 Jul 2009 12:29

I'm playing around with the possibilities of NoAI, and my first approach is based on an idea for a model of expansion of small nested networks.
In my experiment I'm exploring the use of helicopters, so undervalued by most players (low benefit). The project is still very immature to anyone who might be interested to see what I have, but no doubt that if I achieve something that seems to me interesting, I'll share it.

In giving orders to the helicopters I found an error when the destination airport is an helistation. The reason is the upper corner of it is an hangar tile, and isn't accepted as a valid destination. I solved it with

Code: Select all

if (AIAirport.IsHangarTile(tile)) { tile += AIMap.GetTileIndex(1,0); }
but I'm asking myself if a new function like

Code: Select all

AIAirport::GetHangarOfAirport(tile)
but for an AirportTile would be interesting to add to the API. Maybe

Code: Select all

AIAirport::GetRunwayOfAirport(tile)
It could solve the same problem with some other new airport type...

Yexo
Tycoon
Tycoon
Posts: 3653
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo » 09 Jul 2009 13:42

Abenhor wrote:In giving orders to the helicopters I found an error when the destination airport is an helistation. The reason is the upper corner of it is an hangar tile, and isn't accepted as a valid destination.
The API does exactly what you tell it to do. You give it a goto-depot order by clicking on the hangar. Your fix is correct.
but for an AirportTile would be interesting to add to the API. Maybe

Code: Select all

AIAirport::GetRunwayOfAirport(tile)
It could solve the same problem with some other new airport type...
But it also clutters the API. And how to name such a function? AirportHelistations don't have a runway at all. What you want is something like: AIAirport::GetNonHangarTileOfAirport(tile / station_id). For now you can easily implemented in squirrel by looping over the airport tiles (using GetAirportWidth / GetAirportHeight).

Chruker
Engineer
Engineer
Posts: 49
Joined: 01 Jun 2009 20:13

Re: Some API extension ideas

Post by Chruker » 09 Jul 2009 14:37

Yexo wrote:
Chruker wrote:I was unable to find a function that returned the design year of a vehicle, so something like:
AIEngine::GetDesignYear (EngineID engine_id)
What is the reason for wanting to know the design year? Before that year the engine is unavailable, and unavailable EngineIDs are invalid for the AI. So you would only be able to call GetDesignYear when the engine is available, and that seems to defeat the point.
I plan to use it to detect if an engine if within phase 2 of its reliability. So ex. only select engine designs that are between 5 and 25 years.
Yexo wrote:
Also some functions for AIAbstractList:
- A way to get the list items as a squirrel array (discarding the values)
A good idea.
- Counting functions to complement the RemoveAboveValue, RemoveBelowValue, RemoveBetweenValue, RemoveValue
Are you asking for seperate functions (CountAboveValue, CountBelowValue, etc.) or are you asking that RemoveAboveValue should return the number of items removed?
Seperate functions. However I was planning to check if the remove request would empty the list, but I think I for that purpose could just use the keeptop(x) function... but I'm sure there are other situations where it could be helpfull (I'm thinking something where looping over the list in squirrel would be much slower than having the engine do the looping)

Chruker
Engineer
Engineer
Posts: 49
Joined: 01 Jun 2009 20:13

Re: Some API extension ideas

Post by Chruker » 09 Jul 2009 21:55

Another one:
AIVehicle::GetReliability(vehicle_id)

To get the current reliability of a vehicle, so that I can determine if it has some trouble finding depots.

Yexo
Tycoon
Tycoon
Posts: 3653
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo » 10 Jul 2009 06:56

Chruker wrote:Another one:
AIVehicle::GetReliability(vehicle_id)

To get the current reliability of a vehicle, so that I can determine if it has some trouble finding depots.
Good one. I'll implement it as soon as I find the time.

Yexo
Tycoon
Tycoon
Posts: 3653
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo » 10 Jul 2009 22:35

AIVehicle::GetReliability is implemented in r16790.

AIEngine::GetDesignYear <- I'm not sure whether this is the best way of exposing this information.
- A way to get the list items as a squirrel array (discarding the values) <- can easily done in squirrel (see utils/ directory in AdmiralAI source code)
- Counting functions to complement the RemoveAboveValue, RemoveBelowValue, RemoveBetweenValue, RemoveValue <- Can be usefull, but at first I see more use in changing the current functions to return the number of deleted / remaining items.
AIAirport::GetRunwayOfAirport(tile) <- for now, just implement it in squirrel

I hope that covers all requested functions in this topic, if not, or if you don't agree with one of the above, please comment.

Chruker
Engineer
Engineer
Posts: 49
Joined: 01 Jun 2009 20:13

Re: Some API extension ideas

Post by Chruker » 10 Jul 2009 23:49

Yexo wrote:AIVehicle::GetReliability is implemented in r16790.
Awesome
Yexo wrote:AIEngine::GetDesignYear <- I'm not sure whether this is the best way of exposing this information.
Thats the best I could think of :-)
Yexo wrote:- A way to get the list items as a squirrel array (discarding the values) <- can easily done in squirrel (see utils/ directory in AdmiralAI source code)
Very easily, it was more to add to the basic AIAbstractList functionality. Main reason I was looking for an API call was for speed. (I have not done any speed testing.)
Yexo wrote:- Counting functions to complement the RemoveAboveValue, RemoveBelowValue, RemoveBetweenValue, RemoveValue <- Can be usefull, but at first I see more use in changing the current functions to return the number of deleted / remaining items.
Returning numbers using the existing functions is also usefull. But as with the above function the main reason for the API calls is for speed.

User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: Some API extension ideas

Post by fanioz » 12 Jul 2009 04:34

No offending. :D Well, I think :
one of Chruker point is something like 'AIAbstractList..CountIfRemoveValue(x)'
without actually remove items.

AFAIK, if something could be done in squirrel it would not go into API, because it has no opcode restriction. thus make the game run slower due to much resources was needed by AI.
for example: play on 512x512 map.
create an AITileList and fill with all tiles of map.
Valuate this list and the game will not run as smooth as if you evaluate this list using for-loop in squirrel.

well, I think game speed is on top off AI speed. and current AIAbstractList still have much power to do it job.

am I right?
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image

Chruker
Engineer
Engineer
Posts: 49
Joined: 01 Jun 2009 20:13

Re: Some API extension ideas

Post by Chruker » 14 Jul 2009 21:24

fanioz wrote:No offending. :D Well, I think :
one of Chruker point is something like 'AIAbstractList..CountIfRemoveValue(x)'
without actually remove items.
Yeah, that was the point.

Chruker
Engineer
Engineer
Posts: 49
Joined: 01 Jun 2009 20:13

Re: Some API extension ideas

Post by Chruker » 15 Jul 2009 00:23

Some better debug information would also be nice. Currently you have to throw an exception to get error messages with line number, file name and call stack.

Squirrel have a function called getstackinfos which returns an array like this:

Code: Select all

{
	func="DoStuff",	//function name
	src="test.nut",	//source file
	line=10,		//line number
	locals = {		//a table containing the local variables
		a=10,
		testy="I'm a string"
	}
}
But that was disabled to avoid AIs being too dependant on squirrel internals, so that updating the AIs to the NAIL would be easier.

Anyway when is NAIL even due?

User avatar
Dustin
Transport Coordinator
Transport Coordinator
Posts: 272
Joined: 07 Dec 2005 19:22

Re: Some API extension ideas

Post by Dustin » 15 Jul 2009 16:27

I think allowing the AI to interact with the game interface to some extent would be helpful for debugging.

There would need to be an interface option in the main menu "bool Allow_AI_To_Use_Interface_Controls"

Proposed methods:

AIGameInterface.PauseGame(uint ticks); //Allows the AI to pause the game for a while to allow the author to examine game state at specific times.
AIGameInterface.CenterScreenOnTile(tile); //Moves the screen to the tile.
AIGameInterface.OpenExtraViewPort(tile); //Opens a viewport centered on the tile.


-Dustin

User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Some API extension ideas

Post by Zutty » 15 Jul 2009 16:31

While I agree some better debugging facilities would be nice, its probably not a great idea to allow AIs control over the UI.
Dustin wrote:AIGameInterface.PauseGame(uint ticks);
Just Sleep() for something like 1000 ticks to give you time to hit puase yourself.
Dustin wrote:AIGameInterface.CenterScreenOnTile(tile);
AIGameInterface.OpenExtraViewPort(tile);
Just place a sign with some text that you'll recongise, and then wait for it to show up in the sign list. Then you can clikc the name in the sign list and the view will centre on it.
PathZilla - A networking AI - Now with tram support.

Yexo
Tycoon
Tycoon
Posts: 3653
Joined: 20 Dec 2007 12:49

Re: Some API extension ideas

Post by Yexo » 15 Jul 2009 17:36

Zutty wrote:While I agree some better debugging facilities would be nice, its probably not a great idea to allow AIs control over the UI.
I agree.

On the other hand, if you really want these functions, it's easy to add them to the API and compile OpenTTD yourself.

User avatar
Dustin
Transport Coordinator
Transport Coordinator
Posts: 272
Joined: 07 Dec 2005 19:22

Re: Some API extension ideas

Post by Dustin » 16 Jul 2009 01:47

Zutty wrote:While I agree some better debugging facilities would be nice, its probably not a great idea to allow AIs control over the UI.
Dustin wrote:AIGameInterface.PauseGame(uint ticks);
Just Sleep() for something like 1000 ticks to give you time to hit puase yourself.
Dustin wrote:AIGameInterface.CenterScreenOnTile(tile);
AIGameInterface.OpenExtraViewPort(tile);
Just place a sign with some text that you'll recongise, and then wait for it to show up in the sign list. Then you can clikc the name in the sign list and the view will centre on it.
Will a sleep run out if I pause the game? What I really want is sort of a debugger effect, so it would step one iteration at a time under my control.

I am doing this stuff what you describe. But I am at the point where the things that I need to see are between long periods of nothing much interesting. I would be nice to leave it to run and have it pause and change focus. So I can, say, do the dishes while the AI runs until it hits an interesting point and stops.

I agree it might not be nice to have the AI mess with the screen while you are using it, so those options would have to be disabled unless you check "Allow AI controll of interface for AI debugging." in the Interface menu.

User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Some API extension ideas

Post by Zuu » 16 Jul 2009 03:09

In my Helper class in PAXLink I have a function that creates a break point. It is called with a tile as argument and it places a "break" sign on that tile and waits until the sign gets removed. In addition it requires debuging to be on and I think also it needs a "enable breakpoints" sign to be present.

The exact text on the signs may not be what I have written as it is out of my mind and not from the code.


If you apply my Filter Sign List patch you can make it so only break point signs appear (or rather all signs that contain "break") in the sign list.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)

User avatar
Dustin
Transport Coordinator
Transport Coordinator
Posts: 272
Joined: 07 Dec 2005 19:22

Re: Some API extension ideas

Post by Dustin » 16 Jul 2009 06:00

Zuu wrote:In my Helper class in PAXLink I have a function that creates a break point. It is called with a tile as argument and it places a "break" sign on that tile and waits until the sign gets removed. In addition it requires debuging to be on and I think also it needs a "enable breakpoints" sign to be present.

The exact text on the signs may not be what I have written as it is out of my mind and not from the code.


If you apply my Filter Sign List patch you can make it so only break point signs appear (or rather all signs that contain "break") in the sign list.
Interesting. I had thought of doing something similar, but didn't want to spend time meta-programming when my AI still has so many rough edges.

User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Some API extension ideas

Post by Zuu » 17 Jul 2009 01:22

Hello,

I would like to determine the age of stations, which is something a player can do with the query tool if he has forgotten how new a station/connection is.

I am trying to write the save/load for CluelessPluss so that the save-function does not do anything and when a save game is loaded it reads stuff from the map and construct the internal data structures from that. Now, one of the things CluelessPlus uses for its internal management is the date a connection was built. I though it would be a good approximation to use the date one of the stations were built. But may opt for picking the age of the oldest vehicle of a connection.

For implementing, one suggestion could be to add a GetConstructionDate(station_id) function to AIStation.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)

Chruker
Engineer
Engineer
Posts: 49
Joined: 01 Jun 2009 20:13

Re: Some API extension ideas

Post by Chruker » 19 Jul 2009 13:35

Added the constants to the list.
AIAbstractList.SORT_ASCENDING <- true;
AIAbstractList.SORT_DESCENDING <- false;

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests