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 37

Changelog:

Code: Select all

Fix:
- Crash if Direction::GetDirString was supplied with an invalid direction

Feature:
- New method Direction::GetDirectionToTileApprox which gets the direction from
  tile1 to tile 2, and always returns a direction even if not exactly on one
  of the 8 DIR_* directions. (Thanks to R2dical for the implementation of this 
  method)
- New method Helper::Trim(string): removes leading/trailing spaces of string
- New methods to get/print SuperLib version (in Helper sub library)

As usual, the new version is available on bananas. If you want, you can also clone the hg repository and then find version 37 as commit 125:ac6b5ab6def9. This will get you the AI version of the library. To get the GS version, you need to run the nogo_translator.py script. Be aware that you probably need to tweak the dest_dir path on line 135 of the script, before running it on your computer (or create an empty NoGoSuperLib directory one step up from the hg clone directory)
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

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

Post by R2dical »

Zuu wrote: Changelog:
Code:

Fix:
- Crash if Direction::GetDirString was supplied with an invalid direction

Feature:
- New method Direction::GetDirectionToTileApprox which gets the direction from
tile1 to tile 2, and always returns a direction even if not exactly on one
of the 8 DIR_* directions. (Thanks to R2dical for the implementation of this
method)
- New method Helper::Trim(string): removes leading/trailing spaces of string
- New methods to get/print SuperLib version (in Helper sub library)
Cool! Thanks for the update.
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

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

Post by Xycor »

Hello, I've just started trying to use superlib.

I have a simple two station system with a bus. I'm trying to use the Station.IsCargoSupplied function but getting a crash involving Tile. The attached screenshot has the stack. Source code and savegame are attached.

Any suggestions would be appreciated.

This is on the 1.4 RC1 version. I haven't tested other versions.

Thanks

Edit: Removed an extra bug in the attached source that was left over trying to figure out the bug.
Attachments
SuperLibTile.png
(310.34 KiB) Downloaded 8 times
AI Test.sav
Save file used for testing
(56.87 KiB) Downloaded 170 times
OrdersAI.zip
(10.61 KiB) Downloaded 158 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 »

Thanks for your report. It is a bug in the library that you can work around by importing Tile to global scope until I have time to fix it.

Add this just after importing SuperLib.

Code: Select all

Tile <- SuperLib.Tile;
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

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

Post by Xycor »

Thanks. I gave that a shot. SuperLib was missing GSIndustry next. I can workaround for now as long as I manually send a vehicle to each station so it generates cargo first.
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 »

To solve GSIndustry bug you can do:

Code: Select all

GSIndustry <- AIIndustry
But thanks for telling me about the issue so that I can make a proper fix to it.
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 38

Changes
  • Fix problems where you need to import eg. Tile sub library to not have the library crash. If I didn't miss anything while grepping for all sub libraries through the code, all these problems should have been eliminated.
I'm afraid this problem could appear again if I forget upon adding new stuff to the library. An automated test would be good, but needs some work to be able to not give false errors on comments and string content. So no automated test for now.

Personally I often end up most of the core stuff in SuperLib in my scripts which means I seldom hit these problems myself. So thanks for the report once again.


Edit: Xycor: I fail to find any reference to GSIndustry in SuperLib. So I would be interesting if you can reproduce this error again and still think it is the fault of SuperLib.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

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

Post by Xycor »

Thanks for the fix. I'll remove my workarounds and test the new version.

The Station.IsCargoSupplied function that seems to work for everything but passengers and mail. Is a town considered an industry?

Is it possible to append two AILists if I supplement the Station.IsCargoSupplied with another list of stations within a town?
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 »

Looking at the implementation, Station.IsCargoSupplied does only regard industries. Town houses are not considered.
Xycor wrote:Is it possible to append two AILists if I supplement the Station.IsCargoSupplied with another list of stations within a town?

Code: Select all

list_a = AIList();
// Add stuff to list a
list_b = AIList();
// Add stuff to list b
list_b.AddList(list_a);
// List b will contain entries from both list a and b.
I hope I understood your question right.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Xycor
Engineer
Engineer
Posts: 29
Joined: 17 Mar 2014 01:28

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

Post by Xycor »

Thanks. I missed that.

The version bump did fix the missing reference issue for me too.
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 »

Well, I saw that the documentation is not very clear about the limitation that the method only regards industries. So your question is a valid one. If you have a complement function for towns I could include that in SuperLib. Depending on the speed impact, it may still be wise to keep them separate and add a third method that call both for the cases when you need to call both.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

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

Post by R2dical »

Some SuperLib bug (version 37 btw):

In Helper.Nut,
line 654: Helper.GetPAXCargo() must be _SuperLib_Helper.GetPAXCargo()
line 655: Helper.GetMailCargo() must be _SuperLib_Helper.GetMailCargo()
line 666: Helper.GetPAXCargo() must be _SuperLib_Helper.GetPAXCargo()
line 667: Helper.GetMailCargo() must be _SuperLib_Helper.GetMailCargo()

May be more such errors in this file, as I only use the one function in my WIP code so far :)
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

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

Post by krinn »

R2dical wrote:Some SuperLib bug (version 37 btw):
In Helper.Nut,
line 654: Helper.GetPAXCargo() must be _SuperLib_Helper.GetPAXCargo()
...
You're late R2dical :)
http://dev.openttdcoop.org/projects/sup ... helper.nut
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

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

Post by R2dical »

krinn wrote:You're late R2dical :)
http://dev.openttdcoop.org/projects/sup ... helper.nut
Lol, fixed in v38, what are the chances :lol:
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,
It seems that there is left out code from being fixed in ver.38
That is station.nut line 101

Code: Select all

/*static*/ function _SuperLib_Station::CostToDemolishStation(station_id)
{
	local station_tiles = AITileList_StationType(station_id, AIStation.STATION_ANY);
	local cost_to_clear = Builder.CostToClearTiles(station_tiles);
	return cost_to_clear;
}

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 »

Hello fanioz,

Thanks for the report. I fixed the bug and here is now version 39.

Update - Version 39
  • Add: Tile.CostToClearTiles
  • Fix: Station.CostToDemolishStation referenced function in PAXLink not yet ported to SuperLib
Updated versions has been uploaded to bananas.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Simons Mith
Transport Coordinator
Transport Coordinator
Posts: 326
Joined: 14 Jan 2010 23:45

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

Post by Simons Mith »

Minor bit of feedback - I'm using Superlib for a trivial gamescript, and the available log settings are

static LVL_INFO = 1; // main info. eg what it is doing
static LVL_SUB_DECISIONS = 2; // sub decisions - eg. reasons for not doing certain things etc.
static LVL_DEBUG = 3; // debug prints - debug prints during carrying out actions

I would have anticipated LVL_2 to be termed LVL_VERBOSE. Would you mind adding that please?
As you could have several statics without doing any harm perhaps even add MAJOR_DECISIONS
as a second level 1 log level and ALL_DECISIONS as a second level three. Nevertheless, 'DECISIONS'
log levels of any kind are idiosyncratic, whereas the 'VERBOSE' term is very widely known and used.

Thanks!
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

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

Post by Alberth »

But aren't all these levels about verbosity?
Level 2 is verbose w.r.t level 1, level 3 is verbose w.r.t. level 2.

If you use level 2 as VERBOSE, how can there be a level 3 which is more verbose than verbose?


You can easily work around your problem. Just make your own constants, and use those instead of the superlib ones.
Being a retired OpenTTD developer does not mean I know what I am doing.
User avatar
yorg
Engineer
Engineer
Posts: 18
Joined: 19 May 2008 21:48
Location: Peterborough, Ontario

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

Post by yorg »

Hi zuu,

Thanks so much for sharing SuperLib with us - I've gotten back into playing around with writing an AI in OpenTTD (after a first crack at it back when NoAI was still its own experimental branch) and it's a huge help in handling a lot of the low-level things an AI needs to do all the time.

I'm having an issue with road pathfinding in SuperLib version 39 (downloaded through BaNaNaS). It seems like, no matter what number of maximum iterations I set when trying to find a route with RoadBuilder, the pathfinder doesn't time out. With logging set to LVL_DEBUG, I always see "PF loops used: 0" and I never see the expected "SuperLib: Finding path took to long -> abort" message from RoadPathFinder in the log.

From digging around a bit, it seems like accumulated_iterations never gets incremented in _SuperLib_RoadPathFinder::FindPath - I'm not sure if I'm doing something wrong, or if it's a bug, so I thought I'd bring it to your attention.

EDIT:

Played with this a bit more - if I add

Code: Select all

this.accumulated_iterations += num_iterations_to_run;
at line 372 in roadpathfinder.nut, iterations are tracked properly as expected, but then _SuperLib_RoadBuilder::DoPathfinding returns false after the first try, as at line 177 it breaks out of the for loop when any non-false value is returned - that's easy enough to fix by changing the condition to only be non-false non-null values:

Code: Select all

if(path) break; // found path?
After changing this, the second try crashes the AI with the following error:

Code: Select all

dbg: [script] [1] [S] Your script made an error: You are not allowed to change parameters of a running pathfinder.
dbg: [script] [1] [S]
dbg: [script] [1] [S] *FUNCTION [_set()] pathfinder.road-4\main.nut line [84]
dbg: [script] [1] [S] *FUNCTION [InitializePath()] library\SuperLib-40\roadpathfinder.nut line [320]
dbg: [script] [1] [S] *FUNCTION [DoPathfinding()] library\SuperLib-40\roadbuilder.nut line [162]
...
This is where I get lost - I can hack around this by re-instantiating the pathfinder inside the while loop in that function, but that breaks something else that I don't quite understand. Things seem to work fine, but in this case when _SuperLib_RoadBuilder::ConnectTiles calls itself recursively, the second call seems to just report success without building anything, causing the route to be unfinished without throwing an error.

I've attached a copy of SuperLib with the changes I made during this troubleshooting, in case it's helpful - I incremented the version to 40 during this so it wouldn't break anything else I have installed that uses v39.

EDIT 2:

Aha, I think I figured it out - the issue I mentioned in the previous edit occurs then ConnectTiles recurses in the case where DoPathfinding() made it to the second try - in that case, the path that's generated goes from connect_tile2 to connect_tile1. As a result, the path that's sent to the inner call of ConnectTiles ends up being the part of the route that's already built, which of course succeeds. I was able to get around this by actually flipping the values of connect_tile1 and connect_tile2 after the first pathfinding fails, rather than just flipping the order - this version of the library is attached as SuperLib-edit2.tar.gz.
Attachments
SuperLib-edit.tar.gz
(74.21 KiB) Downloaded 155 times
SuperLib-edit2.tar.gz
(74.17 KiB) Downloaded 151 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 »

Hello,

Thanks a lot for your report and trying to resolve this issue. I have prepared a patch which I think is equivalent to your changes. Maybe you could take a look at it as you seem to have some case for reproducing the problem?

For the future, it is helpful to post changes in the form of a patch. Though your detailed explanations in your post what you did was sure a help.

If you haven't made a patch before, here is how you do it: To create a patch, you can add all files to a new local git repo, or clone the HG repo from devzone: http://dev.openttdcoop.org/projects/superlib/repository. Then make your changes and use 'hg diff > my.patch' or 'git diff > my.patch' to create the patch file. If you are new to both hg and git, most people outside of OpenTTD uses git, so better invest your time in learning git than hg.


Edit: forgot to attach the actual patch file
Attachments
39.patch
(1.92 KiB) Downloaded 153 times
Last edited by Zuu on 24 Feb 2016 19:58, edited 1 time in total.
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 3 guests