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

Post Reply
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 25

Today version 25 have been released:

Features:
  • Add: BuildAirportForIndustry - a function that builds an airport for an industry
  • Feature: Road.FixRoadStopFront - a function that tries to adjust the road infront of a road stop so a depot can be added without blocking the ability to connect it to the world
  • Add: Road.RemoveRoadInfrontOfRemovedRoadStopOrDepot that makes use of RemoveRoadFull unless it may damage used road in which case it falls back to RemoveRoad
Fixes:
  • Fix: remove debug signs that the library spawned under some conditions even without the debug sign setting being active.
  • Fix: Tile.IsBuildOnSlope_Flat returned true on steep slopes
  • Fix: When removing road stations, road bits could be left behind on sloped terrain
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 »

Suggestion for inclusion (helper, added after Abs function, I attached modified file):

Code: Select all

//from Rondje, computes square root of i using Babylonian method
function _SuperLib_Helper::Sqrt(i) 
{ 
	assert(i>=0);
	if (i == 0)
		return 0;   // Avoid divide by zero
	local n = (i / 2) + 1;       // Initial estimate, never low
	local n1 = (n + (i / n)) / 2;
	while (n1 < n) {
		n = n1;
		n1 = (n + (i / n)) / 2;
	}
	return n;
}
Tested, algorithm is OK.

I was also thinking about

Code: Select all

function SafeAddRectangle(list, tile, radius) { //from Rondje
	local x1 = max(1, AIMap.GetTileX(tile) - radius);
	local y1 = max(1, AIMap.GetTileY(tile) - radius);
	
	local x2 = min(AIMap.GetMapSizeX() - 2, AIMap.GetTileX(tile) + radius);
	local y2 = min(AIMap.GetMapSizeY() - 2, AIMap.GetTileY(tile) + radius);
		
	list.AddRectangle(AIMap.GetTileIndex(x1, y1), AIMap.GetTileIndex(x2, y2)); 
}
but it seems that there is no suitable category.
Attachments
helper.nut
(18.53 KiB) Downloaded 218 times
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 discovered that something strange happened with IntToStrFill function - it is declared, but it is never defined. I know that it should be this one:

Code: Select all

function IntToStrFill(int_val, num_digits)
{
	local str = int_val.tostring();
	while(str.len() < num_digits)
		{
		str = "0" + str;
		}
	return str;
}

Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

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

Post by Yexo »

Kogut wrote:Suggestion for inclusion (helper, added after Abs function, I attached modified file):

Code: Select all

function _SuperLib_Helper::Sqrt(i) 
Have you considered using the builtin function "sqrt" instead? Same for the builtin function abs/fabs.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

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

Post by Kogut »

Hm, this is new information. (see http://www.tt-forums.net/viewtopic.php?f=65&t=44893 "Functions like this aren't provided for performance reasons." - SmatZ)

Thanks for info about abs, AFAIK I already use min, max. The built-in sqrt have two problems - returns floats rather integers and I prefer sqrt function to crash at receiving values below 0 rather than return weird values.
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:I discovered that something strange happened with IntToStrFill function - it is declared, but it is never defined. I know that it should be this one:

Code: Select all

function IntToStrFill(int_val, num_digits)
{
	local str = int_val.tostring();
	while(str.len() < num_digits)
		{
		str = "0" + str;
		}
	return str;
}

I see, it probably got there while working on DataStore.EncodeIntegerInStr (and the Decode equivalent). In the end that filling functionality ended up in the Encode function. If you want to use it with base 10, you can do like this:

EncodeIntegerInStr(int_val, len, "0123456789");


Though, it might be useful to include a constant for base 10 or a special function for it given that you can write shorter code for the special case of base 10.
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 »

In version 23, Zuu implemented an estimate multiplier to speed up the pathfinding at the expense of cost effectiveness, but i have been unable to get this to work.

the relavent code I am using is this:

Code: Select all

local builder = RoadBuilder();
builder.Init(point1, point2);
builder.SetEstimateMultiplier(2.0);
local res = builder.ConnectTiles();
The estimate multiplier does not appear to be making any speed difference (and I know because I once used the multiplier in my AI). Am I using it incorrectly? If this is a bug, please fix :)
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 »

Your code looks correct. However there is indeed a error in SuperLib. The public interface class that you use do not pass the estimate multiplier down to the underlying PF class.

I think that I've solved this issue, however there are some other non-committed changes in SuperLib that I'll need to see if any of those are worth to include in a new version or not.

Fix: http://dev.openttdcoop.org/projects/sup ... ab007ada84
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 27

Fixes:
  • The value set via RoadBuilder::SetEstimateMultiplier was not used passed along to the pathfinder. (thanks to 11Runner for reporting this bug)
  • The Airport sub library wrongly assumed that the log class have been imported as "Log".
Other:
  • Replaced the tar building script with a newer one which automatically include the version number in the tar files.
  • Include changelog.txt in version control.
Attachments
SuperLib-v27.tar
SuperLib for AIs
(320 KiB) Downloaded 258 times
NoGoSuperLib-v27.tar
SuperLib for Game Scripts
(319 KiB) Downloaded 239 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 »

I plan to add 3 new methods:
  • Town.IsDeserrtTown(town_id)
  • Town.IsSnowTown(town_id)
  • Helper.GetOpenTTDVersion() - from AILibCommon.
Both IsDesertTown and IsSnowTown will use [AI|GS]Tile.GetTerrainType if it is available, otherwise it will fall back to a fallback method that looks at the buildable and tree-free terrain around the town. This method works quite ok but will give some false positives and false negatives for towns just at the border between desert/snow and grass land. The desert method caches the result but not the snow method as there can be variable snow height.

The fallback method iterates out from the town center first in the 4 main directions. If all four branches give the same result (all snow/dessert or all grass), then that result is used. Otherwise it adds information by searching also in the diagonal directions and compute a total score based on all 8 search directions. If this score is above zero, the town is a snow/desert town.

API detection
I wonder, do anyone remember if it was possible in Squirrel to check if AITile.GetTerrainType is available directly instead of going via the OpenTTD version? Eg. in a way that do not crash on old OpenTTD versions. IIRC .rawin() is not available for classes.
Attachments
town.nut
New town.nut with the planned Town methods.

Attached for review purposes. A new SuperLib in both AI and GS version will come to bananas later.
(5.01 KiB) Downloaded 209 times
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

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

Post by krinn »

API detection
edit: the smart version :)

Code: Select all

if ("GetTerrainType" in AITile) {}
Should works for any openttd versions.
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 »

Thank you krinn. I'll go with this solution instead. It is more reliable than trying to write a filter for OpenTTD versions.
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 28

I've now uploaded version 28 with new methods to detect if a town is a desert/snow town that fallback to a heuristic if used in OpenTTD 1.3.0 or older. There is an library method that you can use to check if the fallback method will be used or not (SuperLib.Town.WillUseTerrainFallbackMethod).

The two new methods are Town.IsDesertTown and Town.IsSnowTown. You can find further documentation here: http://dev.openttdcoop.org/projects/sup ... wn.nut#L25


Note: If your AI/GS uses SuperLib, you need to update your import statement to version 28 for your next upload via bananas Web UI.

This is because in the Web UI you will only be able to select version 28 as dependency. If you want to upload an AI/GS that depend on an old version of SuperLib you will have to use musa for now.
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 29 (GS only)

This release is only for Game Scripts. For AIs, it is still version 28 that is the latest.

Changes:
  • Add: Story.NewStoryPage - a wrapper and atomic method for creating a story page with any number of page elements
  • Add: Story.ShowMessage - show a text message to users. Uses StoryPage if available, otherwise GSGoal.Question.
  • Add: Story.IsStoryPageAvailable - check if the GSStoryPage API is available
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
HGus
Engineer
Engineer
Posts: 121
Joined: 12 May 2013 22:28
Location: Argentina

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

Post by HGus »

Hi, testing SynTrans AI I found that it uses SuperLib v27 to manage airports.

When the first Zeppelin is available, it creates Helidepots, then it checks for aircrafts queue using Airport.GetNumAircraftsInAirportQueue function.

Then an "invalid airport type" crash is triggered.

The function cannot manage heli-kind airport types.

I tracked the error back to PAXlink, I think that AI never use Helis, so the error is never seen there.

I found that SynTrans and CluelessPlus are the only AIs that currently use that function, but I never tested CluelessPlus.

Here my patch code to add at line 1057 of airport.nut

Code: Select all

// Patch
		case AIAirport.AT_HELIPORT:
			holding_rect.AddRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, -2, -4), _SuperLib_Tile.GetTileRelative(airport_tile, 4, 3));
			holding_rect.RemoveRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, -1, -2), _SuperLib_Tile.GetTileRelative(airport_tile, 2, 0)); // remove non-holding airport tiles
			break;
		case AIAirport.AT_HELIDEPOT:
			holding_rect.AddRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, -1, -4), _SuperLib_Tile.GetTileRelative(airport_tile, 3, 1));
			holding_rect.RemoveRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, 0, -3), _SuperLib_Tile.GetTileRelative(airport_tile, 1, -2)); // remove non-holding airport tiles
			break;
		case AIAirport.AT_HELISTATION:
			holding_rect.AddRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, 0, -2), _SuperLib_Tile.GetTileRelative(airport_tile, 9, 1));
			holding_rect.RemoveRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, 0, -2), _SuperLib_Tile.GetTileRelative(airport_tile, 4, -1)); // remove non-holding airport tiles
			break;		
// End of patch		
It still can crash if AIAirport.AT_INVALID goes to default switch, it can happen if an airport has been just destroyed/bulldozed (fault in AI logic, not library)
Attachments
airport.nut.patched.gz
patched airport.nut for choppers
(7.69 KiB) Downloaded 203 times
SynTrans crash report
SynTrans crash report
SynTrans-SuperLib27crash.png (57.99 KiB) Viewed 1192 times
HGus
Engineer
Engineer
Posts: 121
Joined: 12 May 2013 22:28
Location: Argentina

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

Post by HGus »

Here the layouts that I used to write the patch.

At helidepot and helistation, the choppers usually go to hangar, to generate these queues I forced the aircrafts to stop at the hangar entrance.

See that one heli is waiting still at position 4,1 at helistation.
Attachments
helidport layout
helidport layout
heliport queue layout.png (48.96 KiB) Viewed 1192 times
helidepot layout
helidepot layout
helidepot queue layout.png (45.74 KiB) Viewed 1192 times
helistation layout
helistation layout
helistation queue layout.png (58.72 KiB) Viewed 1192 times
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 »

Thank you for your bug report. I will include your fix in the next release which I hope to come soon.

I have an AI for visualizing the movement patterns by painting them using signs. However, this AI do not appear to have followed on to my new current PC. And as you saw, I forgot to run it for heliports.
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 »

For Helistation, why do you remove the tile rect marked with bought land in the sceenshot, but leave the loading bays as part of the holding rect?

IIRC, the purpose of the line that remove tiles from the holding rect is to remove tiles that are used by aircraft on the ground. Most importantly is to not count aircraft in loading bays as aircraft in holding.

On some airports that unfortunately conflicts with the holding pattern in which case the holding tile list will be short of some tiles based on the motivation that missing one or two holding tiles isn't that bad compared to having a on-ground tile which aircraft may stay at for long time.

I've updated your code to this:

Code: Select all

		case AIAirport.AT_HELIPORT:
			holding_rect.AddRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, -2, -4), _SuperLib_Tile.GetTileRelative(airport_tile, 4, 3));
			holding_rect.RemoveTile(_SuperLib_Tile.GetTileRelative(airport_tile, 0, 0)); // remove non-holding airport tiles (landing tile)
			break;

		case AIAirport.AT_HELIDEPOT:
			holding_rect.AddRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, -1, -4), _SuperLib_Tile.GetTileRelative(airport_tile, 3, 1));
			holding_rect.RemoveTile(_SuperLib_Tile.GetTileRelative(airport_tile, 0, 1)); // remove non-holding airport tiles (landing tile)
			break;

		case AIAirport.AT_HELISTATION:
			holding_rect.AddRectangle(_SuperLib_Tile.GetTileRelative(airport_tile, 4, -2), _SuperLib_Tile.GetTileRelative(airport_tile, 9, 1));
			break;
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
HGus
Engineer
Engineer
Posts: 121
Joined: 12 May 2013 22:28
Location: Argentina

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

Post by HGus »

Well, I misunderstood "non-holding airport tiles".

I though that it was the part of the main rectangle that was least probable to have aircrafts in queue, and I count those in land as part of the queue.

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

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

Post by Kogut »

SuperLib 28 is not appearing in BaNaNaS for r25561 and 1.3.2-RC1. 27 appears to be the top available version.
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 2 guests