NoCarGoal SCP Client (AI Library)

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
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

NoCarGoal SCP Client (AI Library)

Post by Zuu » 21 Jul 2013 17:40

SCPClient_NoCarGoal is a library that contains a Script Communication Protocol (SCP) client for AIs that wish to get information about NoCarGoal goals.

AI authors can just read the NoCarGoal readme and use SCP themself, or use this library that offers a simple API to focus on the cargoes where the NoCarGoal transport goal has not yet been reached.

Minimal SCPLib version: 45

Usage

1. Import SCPLib and SCPClient_NoCarGoal: (please refer to bananas for information on what is the currently last library versions)

Code: Select all

import("Library.SCPLib", "SCPLib", 45);
import("Library.SCPClient_NoCarGoal", "SCPClient_NoCarGoal", 1);
2. Create a global reference to NoCarGoal lib (you can use any name, it doesn't even need to be global if you don't wish)

Code: Select all

g_no_car_goal <- null;
3. Add class member 'scp' to the main class. (you can use any name or even locate it elsewhere)

4. Add to your Init procedure: (where SHORT_NAME is your 4 letter short name and SELF_VERSION is your script version)

Code: Select all

if(AIController.GetSetting("scp_enabled")) {
	this.scp = SCPLib(SHORT_NAME, SELF_VERSION);
	this.scp.SCPLogging_Info(Log.IsLevelAccepted(Log.LVL_DEBUG)); // Change this if you don't use SuperLib log system
	this.scp.SCPLogging_Error(true);
} else {
	this.scp = null;
}
// Always create g_no_car_goal which will just act as if no NoCarGoal
// gs has been found if this.scp is null.
g_no_car_goal = SCPClient_NoCarGoal(this.scp);
5. In your main loop add:

Code: Select all

// Read incoming SCP messages (up to 5 per loop)
if(this.scp != null) {
	this.scp.SCPLogging_Info(Log.IsLevelAccepted(Log.LVL_DEBUG));
	for(local j = 0; j < 5 && this.scp.Check(); j++){}
}
6. Add scp setting to your info.nut:

Code: Select all

AddSetting({name = "scp_enabled", 
		description = "Enable AI-GS communication - with support for NoCarGoal (SCP library)", 
		easy_value = 1, 
		medium_value = 1, 
		hard_value = 1, 
		custom_value = 1, 
		flags = CONFIG_BOOLEAN});
7. In your code to select which connections to consider, use g_no_car_goal.* to adjust the weighing of the "best" connection so that it will favour the cargoes where the transport goal has not yet been reached. Note that it may be useful to allow your AI to build other connections too, as sometimes the goal connections are not the most profitable ones.

Second note, not all goal cargoes will be primary cargoes. So you may need to either employ the idea that any player that want to beat you must build up that infrastructure and accept that you will usually not win gold, or consider also to build cargo connections that eventually will give industry output that can be used to fulfill a goal.

8. As bananas dependency, you must select both SCPLib and SCPClient_NoCarGoal. Note that SCPClient_NoCarGoal do not depend or import SCPLib. This is because if you want to use several SCPClient libraries in the same AI it would be a hell if they depended too closely on different SCPLib versions. If the AI depend+import SCPLib, then it is enough that all SCP clients work with that SCPLib version.


In the steps I have included how to add SCPLib into your AI. If you already use SCPLib you should pass your already existing SCPLib instance to the SCPClient_NoCarGoal constructor.


Edit: made it more clear that the variable names used are examples.
Last edited by Zuu on 22 Jul 2013 08:17, edited 1 time in total.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)

Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: NoCarGoal SCP Client (AI Library)

Post by Kogut » 22 Jul 2013 05:43

Why AIAI is used as name for something? I hope that there is a possibility to use name that is not the same as my AI, this collision may end quite confusing.

Why creation of variables with fixed names and locations (scp, g_no_car_goal) is required? I really dislike it, especially as addition support for more GameScripts will require additional names. I expected that I will create self-contained object not something that requires specific global variables.

Is it safe to remove this sign at top of the map? I have yet to investigate it but I hope that it is doable to request data and immediately remove this weird thing. Or do it properly, via story book for AIs.

Code: Select all

 and SELF_VERSION is your script version
Is it script as in game script, script as in AI version or is it something else?

Assuming that it is AI version - why GetVersion from info.nut refuses to work in main.nut?

And is it really necessary to provide this data? It seems to be unused anywhere, except in log statements.

minor problem in SCPClientNoCarGoal-1\main.nut:

Code: Select all

	 * @note If you call this method to early, it will return false even if
"to early" should be "too early"

minor problem in Script_Communication_for_AI-45\main.nut:

Code: Select all

	SCPLog.Info("Script Communcation Protocol: version: "+_SCPLib_Share.LibVersion+" - API: "+_SCPLib_Share.LibAPIVersion+"
"Communcation" should be "Communication"
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD

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

Re: NoCarGoal SCP Client (AI Library)

Post by Zuu » 22 Jul 2013 08:15

Several of your comments are related to SCP and not this library. Also some of your comments need a clear case in order for me to be able to answer them accurately. Note that SCP has a thread of its own and is a separate project.

Kogut wrote:Why AIAI is used as name for something? I hope that there is a possibility to use name that is not the same as my AI, this collision may end quite confusing.
Please provide me with a case where this is used.

Kogut wrote:Why creation of variables with fixed names and locations (scp, g_no_car_goal) is required? I really dislike it, especially as addition support for more GameScripts will require additional names. I expected that I will create self-contained object not something that requires specific global variables.
It is not required to call it scp or g_no_car_goal. You can chose whichever names you want. The instructions above is an example on how to incorperate the NoCarGoal SCPClient in your AI. What you call your AI-side variables is up to you. The library is a self-contained class and you can even import it to any name you like. All it want is the SCP instance passed to its constructor and that you follow the guidelines of how to use SCP (create it and call Check in your mainloop)
Kogut wrote:Is it safe to remove this sign at top of the map? I have yet to investigate it but I hope that it is doable to request data and immediately remove this weird thing. Or do it properly, via story book for AIs.
No! The signs on that tile is used for communication between AIs and GSs. You can write your AI such that it call scp.Check() also while building routes etc. if you want to ensure that signs with messages from the GS to your AI get removed as quick as possible.

If you don't like this side effect, you can unfortunately not use any SCP based communication library.

The story book cannot be used for AI-GS communication. The AI has no API to read the story book.

Kogut wrote:

Code: Select all

 and SELF_VERSION is your script version
Is it script as in game script, script as in AI version or is it something else?

Assuming that it is AI version - why GetVersion from info.nut refuses to work in main.nut?

And is it really necessary to provide this data? It seems to be unused anywhere, except in log statements.
It was included when SCP was developed, but I don't remember why. This comment is really a comment on SCP and not this library. If you feel its unnecessary, make a suggestion in the SCP thread.

Its an OpenTTD limitation that GetVersion() and GetShortName() can't be used. I don't know how hard it would be to change this. The version number can safely be put into a global variable defined in version.nut that you include in both main.nut and info.nut. ShortName cannot be handled this way. This is because when you upload your AI to bananas, it will validate that you have a proper short name. This validator is a regex and not a squirrel intrepretor which is why a variable cannot be used to output your short name. On the other hand, the short name will likely not change ever for your AI. (note: be restrictive on what you place in version.nut as it will be included when OpenTTD scans all AIs+GSs when OpenTTD starts up)

Kogut wrote:minor problem in SCPClientNoCarGoal-1\main.nut:

Code: Select all

	 * @note If you call this method to early, it will return false even if
"to early" should be "too early"

minor problem in Script_Communication_for_AI-45\main.nut:

Code: Select all

	SCPLog.Info("Script Communcation Protocol: version: "+_SCPLib_Share.LibVersion+" - API: "+_SCPLib_Share.LibAPIVersion+"
"Communcation" should be "Communication"
Please post SCP suggestions in the SCP thread. Here they may get lost.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)

Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: NoCarGoal SCP Client (AI Library)

Post by Kogut » 22 Jul 2013 11:19

OK, so only this one is relevant.

minor problem in SCPClientNoCarGoal-1\main.nut:

Code: Select all

	 * @note If you call this method to early, it will return false even if
"to early" should be "too early"

Thanks for help. Here is commit introducing basic use of this library in AIAI (maybe it will be useful for other people making AIs?): https://github.com/Bulwersator/AIAI/com ... eab1b48ddd
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD

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

Re: NoCarGoal SCP Client (AI Library)

Post by Zuu » 22 Jul 2013 12:05

Code: Select all

+      this.library.SCPLogging_Info(Info);
I would recommend passing 'true' or 'false' rather than a pointer to a method/class. As the docs doesn't specify that it accept a pointer to method/class that it will evaluate, I would suggest passing a boolean value. Link to docs: http://wiki.openttd.org/SCPLib_doc#SCPL ... 8enable.29

From what I can see in your commit, 'Info' is a method and not a plain variable.


Thanks for the report on the "to" => "too" issue. I've fixed it locally, but will not release a fix for just this issue.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)

Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: NoCarGoal SCP Client (AI Library)

Post by Kogut » 22 Jul 2013 12:48

Ops. Fixed and thanks for link to documentation (I somehow missed it before).
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 4 guests