SuperLib: Helper, Direction, Tile, ... libraries

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

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

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Update - Version 14
  • Fix possible hang up in Tile::GetBridgeAboveStart (Kogut)
  • New Money functions from Kogut/Brumi
  • Fix: Road.BuildMagicDTRSInTown did only return the correct depot location for stop_length = 2 (Runner11)
Thank you Kogut and Runner11 for your provided bug reports and help with the changes included in this version.
Attachments
SuperLib-v14.tar
(260 KiB) Downloaded 145 times
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: SuperLib: Helper, Direction, Tile, ... libraries

Post by Kogut »

Kogut wrote: Inflate function may be used in function _SuperLib_Road::ConvertRailCrossingToBridge

Code: Select all

	/* Now we know it is possible to bridge/tunnel the rail from tile_before to tile_after */
	
	// Make sure we can afford the construction
	AICompany.SetLoanAmount(AICompany.GetMaxLoanAmount());
	if (AICompany.GetBankBalance(AICompany.COMPANY_SELF) < 20000) //<--Here
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Kogut »

YA bug

Code: Select all

function _SuperLib_Money::Inflate(money)
{
	return money * GetInflationRate() / 100;
}
should be replaced with

Code: Select all

function _SuperLib_Money::Inflate(money)
{
	return money * _SuperLib_Money.GetInflationRate() / 100;
}
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: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Update - version 15

Changes:
  • Use Money.Inflate in Road.ConvertRailCrossingToBridge (Kogut)
  • Fix bug in Money.Inflate (Kogut)
Attachments
SuperLib-v15.tar
(260 KiB) Downloaded 137 times
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Update - version 16 + NoGo edition

Changes
  • Removed all usages of HasNext etc. and replaced with foreach loops.
  • Simplified Airport::GetAirportTile a bit (Yexo)
SuperLib-v16.tar
Standard AI edition
(254.5 KiB) Downloaded 137 times
NoGo/GameScript edition
None of these changes are really large. However, the first one was necessary to get the GameScirpt version of SuperLib to work. For simplicity I now decided to keep both editions at the same version. Later the GameScript edition might get its own separate version number if there is a general interest for it.

The NoGo edition is basically just a copy of SuperLib where AI in AIMap, AILog etc. has been renamed to GSMap, GSLog etc. Thus some parts might not work correctly for Game Scripts. In that case it is valuable that people report back this so that the functions can be fixed or removed from the Game Script port of SuperLib. For this reason the GameScript edition is currently limited to these sub-libraries:
  • Direction
  • Helper
  • Industry
  • Log
  • Scorelist
  • Tile
  • Town
If anyone want to try more sub libraries they can grab the NoGo translation script and modify the white list so that it includes more sub libraries. If you find out anything useful by doing this, please post back.

For now I keep the NoGo edition attached here, but ideally it should probably get a home of its own. Below you also find a demo that demonstrates how to include SuperLib in a GameScript.
NoGoSuperLib-v16.zip
GameScript edition. Unzip and place the folder in your GameScript folder. Mind that your GameScript will need to use the GPL2 license as you are inlining GPL2 code in your script.
(21.26 KiB) Downloaded 142 times
NoGoSuperLibDemo.zip
Requires a NoGo OpenTTD binary. Includes NoGoSuperLib-v16. Unpack and place in the "game" folder.
(22.55 KiB) Downloaded 150 times
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: SuperLib: Helper, Direction, Tile, ... libraries

Post by Kogut »

It may be nice to add GetMailCargo function, clone of the GetPAXCargo

helper.nut
new function declaration:

Code: Select all

	/* Get the cargo ID of the mail cargo */
	static function GetMailCargo();
new function

Code: Select all

function _SuperLib_Helper::GetMailCargo()
{
	if(!AICargo.IsValidCargo(_SuperLib_Helper_private_mail_cargo))
	{
		local cargo_list = AICargoList();
		cargo_list.Valuate(AICargo.HasCargoClass, AICargo.CC_MAIL);
		cargo_list.KeepValue(1);
		cargo_list.Valuate(AICargo.GetTownEffect);
		cargo_list.KeepValue(AICargo.TE_MAIL);

		if(cargo_list.Count() > 1) // In theory it is possible to create newgrf with multiple mail cargos
		{
			// Check which mail cargo that has biggest availability in the biggest town
			local town_list = AITownList();
			town_list.Valuate(AITown.GetPopulation);
			town_list.KeepTop(1);

			local top_town = town_list.Begin();
			local town_tile = AITown.GetLocation(top_town);
			if(AITown.IsValidTown(top_town))
			{
				foreach(cargo_id, _ in cargo_list)
				{
					local radius = 5;
					local acceptance = AITile.GetCargoAcceptance(town_tile, cargo_id, 1, 1, radius);

					cargo_list.SetValue(cargo_id, acceptance);
				}

				// Keep the most accepted mail cargo
				cargo_list.Sort(AIAbstractList.SORT_BY_VALUE, AIAbstractList.SORT_DESCENDING);
				cargo_list.KeepTop(1);
			}
		}

		if(!AICargo.IsValidCargo(cargo_list.Begin()))
		{
			_SuperLib_Log.Error("Mail cargo do not exist", _SuperLib_Log.LVL_INFO);
			return -1;
		}

		// Remember the cargo id of mail
		_SuperLib_Helper_private_mail_cargo = cargo_list.Begin();
		return cargo_list.Begin();
	}

	return _SuperLib_Helper_private_mail_cargo;
}
Additional private variable (after "_SuperLib_Helper_private_pax_cargo <- -1;")

Code: Select all

_SuperLib_Helper_private_mail_cargo <- -1;
There is significant code duplication but creating function to check which cargo has biggest availability in the biggest town may be overkill.
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: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Update - v17

This update moves from OpenTTD API version 1.0 to 1.1. This has been necessary in order to allow AIs that use this library to declare to OpenTTD that they require 1.1. Because when an AI declare that it needs 1.1, it also loses eg. AIAbstractList which does not only affect the AI but also all libraries that it uses.

For a full list of API changes from 1.0 to 1.1 see the documentation: http://noai.openttd.org/docs/1.1.3/ai__ ... _8hpp.html
Attachments
SuperLib-v17.tar
(254.5 KiB) Downloaded 139 times
NoGoSuperLib-v17.zip
Not tested yet
(20.8 KiB) Downloaded 138 times
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by teshiron »

In _SuperLib_Road::ConvertRailCrossingToBridge(), could you please change it not to get a max loan every time? Or at least use _SuperLib_Money::RestoreLoan() afterward? This is in road.nut, line 1254.

My AI makes loan payments in small amounts over time, and if it constantly gets reset to max loan each time I replace a level crossing, it'll never get paid off.

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

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Sounds like a valid request. I will take a look at it when I have time to do that.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by MinchinWeb »

Hey Zuu,

I'm working on releasing a GameScript version of WmDOT (my AI) that uses some of the functions of your SuperLib. Would you be able to release a "proper" library version, now that the NoGo branch supports libraries? I think the only thing currently missing is the "library.nut" file.

As well, in the first post, you have a link to a SVN server, but it returns a 404 error.

Thanks for your coding work!
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by planetmaker »

MinchinWeb wrote:As well, in the first post, you have a link to a SVN server, but it returns a 404 error.
The NoAI projects were migrated to the DevZone in order to only need maintain one 3rd-party project site: http://dev.openttdcoop.org/projects/superlib
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Update - version 18

Changelog
  • Road.ConvertRailCrossingToBridge does now restore the loan before it returns
  • Road.ConvertRailCrossingToBridge does now only take a loan if bank balance is less than twice the maximum loan
  • Include library.nut in the NoGo edition
  • Nogo edition is now offered as .tar so that you don't need to unpack it to use it. Just place it in your game/library directory and use the exact same import syntax as for AIs.

    Code: Select all

    import("util.superlib", "SuperLib", 18);
  • (Automatically) include *.txt in the AI edition
SuperLib-v18.tar
AI edition
(260 KiB) Downloaded 129 times
SuperLib-nogo-v18.tar
Game Script edition
(82 KiB) Downloaded 133 times

Answers to previous questions
MinchinWeb wrote:I'm working on releasing a GameScript version of WmDOT (my AI) that uses some of the functions of your SuperLib. Would you be able to release a "proper" library version, now that the NoGo branch supports libraries? I think the only thing currently missing is the "library.nut" file.
Yes it's possible - see above :-)
MinchinWeb wrote:As well, in the first post, you have a link to a SVN server, but it returns a 404 error.
I've updated the link in the first post to point to the URL that planetmaker pointed out. Thanks for noticing me about this problem.
teshiron wrote:In _SuperLib_Road::ConvertRailCrossingToBridge(), could you please change it not to get a max loan every time? Or at least use _SuperLib_Money::RestoreLoan() afterward? This is in road.nut, line 1254.

My AI makes loan payments in small amounts over time, and if it constantly gets reset to max loan each time I replace a level crossing, it'll never get paid off.

Thanks!
I hope that the two changes I made are good enough. It will restore the loan afterwards, and if you have plenty of money it will not even take a loan.
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: SuperLib: Helper, Direction, Tile, ... libraries

Post by Kogut »

Kogut wrote:It may be nice to add GetMailCargo function, clone of the GetPAXCargo

-clicky snip-
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: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Kogut wrote:
Kogut wrote:It may be nice to add GetMailCargo function, clone of the GetPAXCargo

-clicky snip-
Sorry, I missed that. Thanks for reminding me.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Update - version 19

Changelog
  • Add: Helper.GetMailCargo() (thanks Kogut)
SuperLib-v19.tar
(260 KiB) Downloaded 139 times
SuperLib-nogo-v19.tar
(83 KiB) Downloaded 136 times
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Version 19 for NoGo/GameScript has now been uploaded to bananas. Because I selected the wrong license at first, I had to make 19.1. In your code you still import version 19. The .1 is just displayed in bananas.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
peter1138
OpenTTD Developer
OpenTTD Developer
Posts: 1729
Joined: 30 Mar 2005 09:43

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by peter1138 »

Zuu wrote:Mind that your GameScript will need to use the GPL2 license as you are inlining GPL2 code in your script.
This is only relevant if you wish to distribute your script.
He's like, some kind of OpenTTD developer.
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by planetmaker »

Quite seriously, Zuu could just release his library under the agpl :-)
11Runner
Engineer
Engineer
Posts: 92
Joined: 01 Sep 2011 19:23
Location: Oregon, USA

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by 11Runner »

The Helper.SetSign() method does not return the SignID of the created sign. Could that be implemented?

EDIT:

I apologize, I did not look at your post on the CityDomination thread closely enough. I still think returning the SignID might be something to look into...
Image

SynTrans - A Synaptic Networking AI for OpenTTD
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: SuperLib: Helper, Direction, Tile, ... libraries

Post by Zuu »

Since the function is a void now, it shouldn't break anything if it is changed to return the sign id on success or -1 if no sign is placed.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 8 guests