Page 5 of 9

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

Posted: 29 Sep 2012 18:55
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

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

Posted: 06 Oct 2012 21:10
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.

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

Posted: 06 Oct 2012 21:31
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;
}


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

Posted: 06 Oct 2012 21:44
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.

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

Posted: 07 Oct 2012 05:21
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.

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

Posted: 07 Oct 2012 07:03
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.

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

Posted: 17 Nov 2012 05:41
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 :)

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

Posted: 17 Nov 2012 11:41
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

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

Posted: 17 Nov 2012 14:24
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.

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

Posted: 01 May 2013 09:07
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.

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

Posted: 01 May 2013 09:49
by krinn
API detection
edit: the smart version :)

Code: Select all

if ("GetTerrainType" in AITile) {}
Should works for any openttd versions.

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

Posted: 01 May 2013 10:40
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.

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

Posted: 19 May 2013 11:01
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.

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

Posted: 09 Jun 2013 20:44
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

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

Posted: 17 Jun 2013 23:34
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)

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

Posted: 17 Jun 2013 23:43
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.

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

Posted: 19 Jun 2013 10:47
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.

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

Posted: 21 Jun 2013 11:29
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;

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

Posted: 23 Jun 2013 03:01
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:

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

Posted: 06 Jul 2013 07:27
by Kogut
SuperLib 28 is not appearing in BaNaNaS for r25561 and 1.3.2-RC1. 27 appears to be the top available version.