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 »

Thanks, I didn't knew that.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

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

Post by fanioz »

Hello Zuu, My AI was crashed at : (\superlib-5\money.nut - 152)

function _SuperLib_Money::GetCompanyProfitLastYear()
{
.....
}
Attachments
SuperLib.PNG
SuperLib.PNG (30.47 KiB) Viewed 5932 times
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
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 »

Thanks for your report. I would hope that the API had this value in it (eg that my suggestion to the API thread would be implemented), but otherwise I'll have to use the base value of about 500 and add inflation if that is enabled. Still it's a bug in the library that I did release it before sorting out this constant.

Thanks for your report.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

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

Post by Yexo »

Zuu wrote:Thanks for your report. I would hope that the API had this value in it (eg that my suggestion to the API thread would be implemented), but otherwise I'll have to use the base value of about 500 and add inflation if that is enabled. Still it's a bug in the library that I did release it before sorting out this constant.

Thanks for your report.
I can't find it in the last few pages of the API extension ideas topic. Could you please point out where the request is (so I don't miss similar things) or make a new post in the API extension ideas topic?
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 »

Its in my post from 20 feb 2010 at page 7 in the api suggestion forum.
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 7
  • Bug fix: Use AILog.Warning for Log.Warning calls.
  • Change: Include date in log prints
  • Bug fix: Fixed two fatal bugs in Airport sub library
AIs that use version 6 shouldn't have any problems upgrading to version 7 as no changes affect how the AI interact with the library.
Attachments
SuperLib-v7.tar
(130 KiB) Downloaded 208 times
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 »

Thanks for your Library Zuu - it makes my life way nicer.

I have a bug report: If I cheat to switch companies to my AI and then cheat to add £10,000,000, the MaxPayback function (in the Money sublibrary) stops working. My loan amount doesn't go up, but the loan doesn't disappear either.

To replicate, load up WmDOT, and in the AI Options set 'OpDOT' to 'no go' and the 'Debug Level' to '3' or better. Cheat to the money...
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
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 »

Thanks for your report,

Here is a fixed money.nut, I've verified it using a test AI that do nothing more than calling MakeMaxPayback.

Verification AI "PayBack":

Code: Select all

function PayBack::Start()
{
	while(true)
	{
		AIController.Sleep(1);
		Money.MakeMaximumPayback();
	}
}
I will make a version 8 of the library soon (today or within a few days).
Attachments
money.nut
(6.44 KiB) Downloaded 162 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 8
Fixes the problem in the Money sub library reported above.
Attachments
SuperLib-v8.tar
(130 KiB) Downloaded 162 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 - SuperLib 9
Includes my pathfinder wrapper (SuperLib.RoadPathFinder) + road building code (SuperLib.RoadBuilder).
Attachments
SuperLib-v9.tar
(170 KiB) Downloaded 154 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 - SuperLib 10

Important: moved functions
The following two static functions has moved to a new Road class.
RoadBuilder::ConvertRailCrossingToBridge(rail_tile, prev_tile)
RoadBuilder::RemoveRoadUpToRoadCrossing(start_tile)

Other changes
  • A new class Road with functions for building road stations for towns/industries
  • New functions in Airport for handling of air services
  • Yexo's Valuate and CallFunction functions from AdmiralAI (version 22) has been added to SuperLib. The reason for this is that they are needed by a function in Airport. What differs Yexo's Valuate squirrel function from the one in the API is that it accepts valuator functions that execute DoCommands.
  • A few additional functions here and there
Attachments
SuperLib-v10.tar
(230 KiB) Downloaded 174 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 11
* Airport Upgrading
* Improved handling of when the distant join station setting is off

Version 12
* Read use_rvs, use_planes, use_trains and use_ships settings. Report zero vehicle limit if corresponding vehicle type setting is defined by the AI and is equal to 0.
* Function to get a string explaining which setting a user need to enable in order to enable a given vehicle type. (will require updates whenever the English "translation" changes and is incompatible with users running OpenTTD in a language different from English)

If anyone needs other setting names than "use_rvs", "use_planes", etc. a system that allows defining those names is possible. However, unless someone actually needs it, I will probably not implement it. (for inspiration see eg. the Log system which allow to offer your own function for deciding which log messages that should be hidden or not if you don't want to use the default one.)

Download
Visit bananas: http://bananas.openttd.org/en/ai/
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 13
  • Fixes a fatal error that has existed in the library for a while. In order to trigger it one had to call the function to build a bus stop/truck stop/depot next to road from a scope where "this" would not point to a sub class of AIController. These conditions are as far as I remember never met in CluelessPlus, so the error didn't appear there.
  • Fix: Don't require 8 in production when building road stops. Only acceptance need >= 8. For production there is no such limit in OpenTTD.
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 »

Is it planned to include useful function but unused in PAXlink/Clueless code? I think that it may be interesting to move certain functions from AIAI to this library.

Code: Select all

	// This function comes from SimpleaAI, version 6, written by Brumi
	/**
	* Calculates the percentage of inflation since the start of the game.
	* @return The percentage by which prices have risen since the start of the game.
	* It is 100 if there is no inflation, 200 if prices have doubled, etc.
	*/
	static function GetInflationRate();
	
	// This function comes from AIAI, version 51, written by Kogut
	/**
	* Adjusts any given amount of money for inflation
	*/
	static function Inflate(money);
	
	// This function comes from AIAI, version 51, written by Kogut
	/**
	* Wastes money (may be used to ensure bankruptcy)
	*/
	static function BurnMoney();

function GetInflationRate()
{
	return (100 * AICompany.GetMaxLoanAmount() / AIGameSettings.GetValue("difficulty.max_loan"));
}

function Inflate(money)
{
	return money*GetInflationRate()/100;
}

function BurnMoney()
{
	local empty_tile=null;
	for(local i = 0; i<1000; i++)
	{
		empty_tile = RandomTile();
		if(AITile.IsBuildable(empty_tile))
		{
			while(true)
			{
				AITile.PlantTree(empty_tile);
				AITile.DemolishTile(empty_tile);
			}
		if(!AITile.IsBuildable(empty_tile))continue;
		break;
		}
	}
}
Inflate function may be used for example 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
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 »

I haven't exactly planned to do so, but if you or others offer code that seems resonable to include, I'm not against including it.

Actually, I think it is very nice of you that you offer some code back that can be used to improve SuperLib even more.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
11Runner
Engineer
Engineer
Posts: 92
Joined: 01 Sep 2011 19:23
Location: Oregon, USA

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

Post by 11Runner »

Hello,

It appears that when you use the Road.BuildMagicDTRSInTown function and set the stop_length argument to a value greater than 2, the depot_tile return value is a tile off. The picture included on this post illustrates the problem
Sarham Transport, 12th May 1710.png
Sarham Transport, 12th May 1710.png (59 KiB) Viewed 868 times
If you cant read past my bad photoshopping skills, SuperLib returned the depot tile as 1002,972, while the depot was actually at 1003,972. I have no idea if the problem is related to the way the station is facing or not, or if your Tile.GetTileString function incorrect, but somthing is wrong. Yes, the station in the background is the station in question.
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 »

Thanks 11Runner for your report. Actually, I think it will return a wrong depot tile for any stop_length other than 2. I've fixed the problem in the code respority and it will be included in the next release, which I think will happen when I've also added the code that Kogut posted.


Kogut: Is it intended that your BurnMoney function never will return? You have a while(true) loop in there which has no way of breaking out of the loop. You've also missed the RandomTile function, but no problem, its fairly simple and I've added a SuperLib::Tile::GetRandomTile function that I use for your BurnMoney function.
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 »

Zuu wrote:Is it intended that your BurnMoney function never will return?
:oops:, I copied wrong version - together with pointless tree planting.

Code: Select all

function BurnMoney()
{
   local empty_tile=null;
   for(local i = 0; i<1000; i++)
   {
      empty_tile = RandomTile();
      if(AITile.IsBuildable(empty_tile))
      {
         while(true)
         {
            if(!AITile.DemolishTile(empty_tile)) break;
         }
      if(!AITile.IsBuildable(empty_tile))continue;
      break;
      }
   }
}
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 »

I found a bug: function _SuperLib_Tile::GetBridgeAboveStart is using this for:

Code: Select all

	for (local curr_tile = _SuperLib_Direction.GetAdjacentTileInDirection(tile, bridge_search_direction); 
			AIMap.IsValidTile(curr_tile); 
			curr_tile = _SuperLib_Direction.GetAdjacentTileInDirection(curr_tile, bridge_search_direction))
Unfortunately, result from GetAdjacentTileInDirection is using value from GetTileInDirection, this function is using value from GetTileRelative. Value returned by GetTileRelative is clamped to mapsize, therefore AIMap.IsValidTile(curr_tile); is not working (curr_tile is always valid). I think that it can be replaced by

Code: Select all

	for (local curr_tile = _SuperLib_Direction.GetAdjacentTileInDirection(tile, bridge_search_direction); 
			true;
			curr_tile = _SuperLib_Direction.GetAdjacentTileInDirection(curr_tile, bridge_search_direction))
...
			if(curr_tile == _SuperLib_Direction.GetAdjacentTileInDirection(curr_tile, bridge_search_direction)) break;

to achieve intended result.
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:

Code: Select all

         while(true)
         {
            if(!AITile.DemolishTile(empty_tile)) break;
         }
To my understanding, this loop will never stop for as long as empty_tile is empty. Thus the function will typically not return until the AI has really gone bankrupt. For the purpose of ensuring bankruptcy its probably fine, it's just not documented. So unless someone corrects me, I will add to the docs of this function that it will most likely not return.


Thanks for your other bug report, I'll include that fix.
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 24 guests