[AI] RailwAI

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
RailwAI
Engineer
Engineer
Posts: 40
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

The NoAI framework doesn't support to let an AI know much about available road types such as rattroads. Currently RailwAI assumes there can be 2 types of road (as per default): roads and tramways. I can try to let it work with rattroads, but it will always be a guess: The characteristics of other road types than the defaults can not be determined by an AI, while I want to treat roads and tramways differently.
Edit: my statement might be incorrect starting from openttd 1.10.0, let's wait if it will improve :-)

User avatar
3iff
Tycoon
Tycoon
Posts: 1012
Joined: 21 Oct 2005 09:26
Location: Birmingham, England

Re: [AI] RailwAI

Post by 3iff »

Quite impressed by this. A couple of issues discovered that I'd like to mention. Running version 20 on JGR 29

The AI linked an iron ore mine to a steel mill via roads then proceeded to build 3 truck services within a few months. That's hardly an efficient way to do it as only 2 stations will be getting resources. However, all three services are still active after 10 years - and it does tend to block other companies from setting up a service!

The double tracked rail lines set their signals so trains drive on the right. I always set my trains to drive on the left. I had a quick look at the code and basically didn't understand most of it. I'm guessing it's not an easy thing to change this but if you think it's possible if you could perhaps give me a few pointers so I might change it... I'm NOT asking you to make any changes (I really wouldn't expect you to do so). I'm pretty sure that players who have trains driving on the left are few in number, but I'd like to change it for myself if I possibly can. It only becomes relevant when I take over such a company and I can live with it how it is at the moment.

I do like the fact that there are shared vehicles which makes it so much easier after takeovers. Bigger rail stations are also appreciated.

---------

Actually, on further inspection, the signalling is way more complex than I first thought. There's a strange combination of single, path and one-way stuff. It mostly works except for one instance where a train went the wrong way (on a line that connected another service and the AI ended with 4 trains stuck).

It's also slightly annoying that if a rail service closes that the associated track is left in place - but I can understand the situation when services share some common track.

User avatar
RailwAI
Engineer
Engineer
Posts: 40
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

Thank you for your reaction! Blocking other companies should not be involved in RailwAI, so that should be a problem of the other companies. Sometimes the strategy might be inefficient, but that's part of having random behaviour, everything could be an option. Or do you mean that it tried to start one service for which no cargo is produced, so nothing is being transported?

Changing trains to drive on the left track will be quite complex for sure. The rail construction contains a lot of code that is written to have trains running the right hand track. You could try to change the orientation (and types) of signals being placed on new railway lines, but it might cause deadlocks on two-track sections on single track lines.
The best way would be to change the RailTrackPlan (yes it is a huge bunch of code) in rail.nut and some more other changes. Required changes (as far as I think would be needed):
  • Make sure you understand how the VectorCoordinates work (world.nut).
  • path1 should not go from the right track to the left track, but from the left track to the right track. This can be changed in the pathfinder.InitializePath command (line 686 of rail.nut). Not that all the avoidArea and avoidTiles must be changed too: these are tiles that must be avoided by the pathfinder, basically already reserving some space for the other track.
  • GetTilesLeftFrom must be changed to a function GetTilesRightFrom. This function now returns a tile/direction array left of a given path. Note that it behaves different from corners to the left and corners to the right, because the other track crosses a different number of tiles.
  • BuildPassingSectionsLeftFromTiles must be changed to a function BuildPassingSectionsRightFromTiles
  • If there is more that refers to left or right (variable names or comments), try and check if it must be changed.
  • Disallow for train stations with more than two platforms (numPlatforms in station.nut). Current station exits will cause deadlocks with trains running on the left track, and it is all hardcoded (I still have the idea to refactor it one day). You could also try to change it, but it won't be easy.
Signal usage might look difficult, but some basic guidelines are involved:
  • On double track lines, use simple signals
  • Before switches, place path signals. Use a one-way path signal on the right track to prevent trains running on the left track.
  • Also use path signals (preferably one-way signals) on two-track sections of one track railway lines (note that more than 50% can be double track)
  • If something is changed to an existing railway line (a new bridge connected, a railroad crossing removed/placed, a new switch was placed) replace simple signals there with path signals
When a rail service closes it eventually tries after a while to remove railway track. It is still an open issue on my list that tracks towards a depot are not removed. Currently I'm quite busy in my real-world life, so I hope to be able to update RailwAI again in 2020. :)

User avatar
3iff
Tycoon
Tycoon
Posts: 1012
Joined: 21 Oct 2005 09:26
Location: Birmingham, England

Re: [AI] RailwAI

Post by 3iff »

Thanks for the reply.

I'm not going to attempt any changes to train driveside or signals...way too complex.

As for the 3 stations at one mine, having 2 stations should be a max limit (which might effectively block opposition - that's fine) but having a third pickup station is self defeating - so perhaps being able to detect and prevent that happening would be helpful for that company.

Removing unused track would be helpful as otherwise a smallish map can easily get clogged with redundant track and a takeover or bankruptcy is the only effective way to clear it.

Ikta
Engineer
Engineer
Posts: 1
Joined: 22 Nov 2019 17:59

Re: [AI] RailwAI

Post by Ikta »

Ran into a error trying to load a game today that I was playing last night
Image

User avatar
RailwAI
Engineer
Engineer
Posts: 40
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

Thanks for reporting! The bug looks rather easy to fix, so it will be solved in next release :-)

User avatar
RailwAI
Engineer
Engineer
Posts: 40
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

RailwAI wrote:
31 Oct 2019 20:02
The NoAI framework doesn't support to let an AI know much about available road types such as rattroads. Currently RailwAI assumes there can be 2 types of road (as per default): roads and tramways. I can try to let it work with rattroads, but it will always be a guess: The characteristics of other road types than the defaults can not be determined by an AI, while I want to treat roads and tramways differently.
Edit: my statement might be incorrect starting from openttd 1.10.0, let's wait if it will improve :-)
According to the docs it will be possible with openTTD 1.10, I'll improve RailwAI after relase of 1.10.0

User avatar
RailwAI
Engineer
Engineer
Posts: 40
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

No road types support yet, but version 21 is now available.
The changelog:
  • Trains: became better in building passing loops on single track lines
  • Trains: trains should not have more locomotives than carriages, even when game settings are set to high slope steepness
  • Trains: Remove abandoned railway lines
  • Trains: Better support to use freight trains to transport goods to cities
  • Trains: Put less often signals around short railway bridges
  • Trains: Put more often signals around longer tunnels and bridges
  • Ships: Place more canal tiles around docks, to prevent ships being lost
  • Small changes in rail pathfinder
  • Transport valuables in two directions, also with freight trains
  • Several code speed optimizations
  • Try sometimes to reconnect a road if one vehicle is locked on a single piece of road
  • Have a small preference to transport from newly opened industries
  • Bugfix: error 'avoidArea' does not exist
  • Bugfix: do not accidentally turn around a one-way path signal after building a new railway bridge
  • Bugfix: be able to build a depot on both sides of the track near a train station
  • Bugfix: Always remove the depot, when a new train station plan is cancelled
  • Bugfix: Rail pathfinder could generate illegal routes around bridges
  • Bugfix: Saving a game while building a train station, could cancel the building plans
  • Bugfix: Should not come here in World.Direction

xarick
Transport Coordinator
Transport Coordinator
Posts: 283
Joined: 26 Feb 2015 00:52

Re: [AI] RailwAI

Post by xarick »

Got a crash right in the first year.
Attachments
Unnamed, 1950-12-29.png
(514.81 KiB) Not downloaded yet
RailwAI v21 crash.sav
(113.75 KiB) Downloaded 2 times
Formerly known as Samu

User avatar
RailwAI
Engineer
Engineer
Posts: 40
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

I don't like crashes, so thanks for reporting! This one was very easy to fix, thus you can now download version 22. I also made some improvements after I saw some deadlocked trains in my games. The changelog:
  • Trains: be able to make adjacent bridges double track
  • Trains: Place less often signals around railway bridges
  • Trains: Do not connect railway bridges to station platforms
  • Trains: Build depots near some bridges
  • Bugfix: Do not crash when no train could be bought

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 3 guests