[GS] MagicGS : Connects cities and towns by road

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
MagicBuzz
Tycoon
Tycoon
Posts: 1286
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

[GS] MagicGS : Connects cities and towns by road

Post by MagicBuzz »

Hello,

I pressent you MagicGS, my first attempt of Game Script.

It will start at game start, and connect towns using this behaviour:

Step 1: Connect all city together.
Step 2: Connect each town to the nearest city.
Step 3: Connect each town to the nearest neighbor towns.

Parameters allow fine tune:
- Enable or disable each of the 3 steps
- Maximum distance and speed for city to city road
- Maximum distance and speed for city to town road
- Maximum distance and speed for town to town road, and maximum neighbor to connect

Known issues:
- RoadPathFinder from SuperLib is really really slow. I don't know much if I do something wrong, but even on a small map the towns takes several minutes to connect (on a big map, this cas spent several hours!). Good news, you can still play while the GS connects roads.
- RoadPathFinder from SuperLib produces many strange bridges when they are useless. I don't know how to fix that.

Why this GS?
I used to play with City Connector AI, but it has at least two drawbacks:
- it never stops. So after some decades, it builds again and again new roads between the same towns, producing a big mess. This GS will connect town only one time for all, whatever the road is buldozed.
- it is an AI. As a result, the roads belongs to a company... and this company won't let you building over its roads. This is very annoying when you are planning a new rail network and everything goes bad when some highway is built just where you planned to build you rails. With a GS, roads do not belong to anyone, so you can buldoze it as much as you want.

Change log:

Version 1
- Fix: When a connection is impossible, try to connect the next town in the destination list. This fix the problem when the nearest city/town is not reacheable (island, etc.)
- Add: Store already connected town, so don't try to connect them again (speed up connection time as it won't try to reconnect already connected town. Thus this is needed for save/load support)
- Add: Support for save/load and resume connection where it stopped
- Fix: For step 2 and step 3 we need complete list of destinations while for step 1 we reduce the destinations as we don't need to connect cities in the opposite direction

Version 2
- Add many parameters
- GS now use available roads (including NRT newgrf) according to the company "0" (first player in game)
- When a new town is founded it will connect it
- Fix: proper use of return value from path finder
Attachments
MagicGS-v2.tar
(46.5 KiB) Downloaded 230 times
Last edited by MagicBuzz on 20 Jul 2020 20:25, edited 1 time in total.
User avatar
Firrel
Engineer
Engineer
Posts: 93
Joined: 13 Aug 2019 17:06

Re: [GS] MagicGS : Connects cities and towns by road

Post by Firrel »

Hey,

Your script looks interesting so I gave it a try and also looked into your code.

1. In info.nut you have API version 1.11. OpenTTD 1.11 is not released yet, so it will not display in the GS selection screen. The API version is the same as the game version, so change it to 1.10 or lower.
2. In superlib 40, there was "fixed" pathfinding timeout. It will no longer return false when the process is not finished yet, instead it will set error message to PATH_FIND_NO_ERROR and return value to null (link line 377). You can use it for example like this below or check superlib implementation. This will speed up the pathfinding.

Code: Select all

local path = null;
local pf_error = PathFinder.PATH_FIND_NO_ERROR;
PathFinder.SetMaxIterations(10000000);
while (path == null && pf_error == PathFinder.PATH_FIND_NO_ERROR) {
    path = PathFinder.FindPath(100);
    pf_error = PathFinder.GetFindPathError();
}
3. I would not use GSController.Sleep(1) when searching for path. It may be useful for AI to build only when the game is not paused, but this script would benefit by running when paused, so the initialization will be done faster or will not use game time.
4. For the GS settings, it would be better to use CONFIG_NONE because the values are read only in constructor, so it is not possible to change them during game and should not be.
5. Are you planning to connect funded towns?
6. Did you consider using different road types (if available) for different road connections, like dirt road for small towns?
User avatar
jfs
Tycoon
Tycoon
Posts: 1453
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: [GS] MagicGS : Connects cities and towns by road

Post by jfs »

API version 1.11 does exist, it's the current nightly builds. There are currently two additions to the script API over version 1.10, but I haven't examined whether this GS actually depends on any of them: GSPriorityQueue, and push buttons on story book pages.
User avatar
Firrel
Engineer
Engineer
Posts: 93
Joined: 13 Aug 2019 17:06

Re: [GS] MagicGS : Connects cities and towns by road

Post by Firrel »

Ah yes, forgot about nightly build. I tried it on 1.10 and it worked fine.
User avatar
MagicBuzz
Tycoon
Tycoon
Posts: 1286
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

Re: [GS] MagicGS : Connects cities and towns by road

Post by MagicBuzz »

Firrel wrote: 18 Jul 2020 21:09 Hey,

Your script looks interesting so I gave it a try and also looked into your code.
Thank you Firrel to take a look at it.
I really appreciate your feedbacks.
Firrel wrote: 18 Jul 2020 21:09 1. In info.nut you have API version 1.11. OpenTTD 1.11 is not released yet, so it will not display in the GS selection screen. The API version is the same as the game version, so change it to 1.10 or lower.
I took this value from JGR patchpack I'm using, that is based on trunk.
As jfs mentioned, the GS does not use the new features, and I'm pretty sure I could use older: I just took the lastest one. The v2 will use 1.10 as it's the most common currently.
Firrel wrote: 18 Jul 2020 21:09 2. In superlib 40, there was "fixed" pathfinding timeout. It will no longer return false when the process is not finished yet, instead it will set error message to PATH_FIND_NO_ERROR and return value to null (link line 377). You can use it for example like this below or check superlib implementation. This will speed up the pathfinding.

Code: Select all

local path = null;
local pf_error = PathFinder.PATH_FIND_NO_ERROR;
PathFinder.SetMaxIterations(10000000);
while (path == null && pf_error == PathFinder.PATH_FIND_NO_ERROR) {
    path = PathFinder.FindPath(100);
    pf_error = PathFinder.GetFindPathError();
}
3. I would not use GSController.Sleep(1) when searching for path. It may be useful for AI to build only when the game is not paused, but this script would benefit by running when paused, so the initialization will be done faster or will not use game time.
Thank you, I changed the code according to your message.
I will check its behaviour in the next days.
I simply copied the CityConnector's AI code for those lines, and I didn't even understood why there were a Sleep() in the loop, nor in which case the loop was really needed.
Firrel wrote: 18 Jul 2020 21:09 4. For the GS settings, it would be better to use CONFIG_NONE because the values are read only in constructor, so it is not possible to change them during game and should not be.
v2 will fix that
Firrel wrote: 18 Jul 2020 21:09 5. Are you planning to connect funded towns?
6. Did you consider using different road types (if available) for different road connections, like dirt road for small towns?
I did never used funded town feature, so I didn't thought about it. I will try to handle this event. With the current version, you may simply save and reload the game and the GS will find the new town don't have connection yet.
About using NRT, I will take a look at it. Right now I didn't saw API implementation and I saw the GS don't use the same road type according to the date. But still not digged deeper why this behaviour.
User avatar
MagicBuzz
Tycoon
Tycoon
Posts: 1286
Joined: 15 Feb 2003 17:32
Location: Vergezac, France

Re: [GS] MagicGS : Connects cities and towns by road

Post by MagicBuzz »

I implemented most of the fixes and feedback, but I'm waiting a better way to find available roads before uploading v2.

You can find current source code at GitHub:
https://github.com/SylvainDevidal/MagicGS

I will update it soon to process properly funded town (rather than saving/reloading savegame like now).
unidentifiable
Engineer
Engineer
Posts: 3
Joined: 14 Sep 2020 17:22

Re: [GS] MagicGS : Connects cities and towns by road

Post by unidentifiable »

This is awesome, thanks very much!

Can I bother you to add a couple of parameters?

- Toggle for tunnel-making (and/or specify a max tunnel span length)
- Toggle for bridge-making (and/or specify a max bridge span length)

I like that the script auto-connects towns, which makes for a great early-game, but it often does not choose the least-cost option and ends up creating huge bridges or tunnels to get to its destination, which isn't very realistic. Ideally the script should choose the option to connect two towns which costs the least, not which is the shortest (unless they're one and the same :) )

It also seems to fail quite frequently and declare "There definitely is no path".

Thanks for the script!
User avatar
JacobD88
Chief Executive
Chief Executive
Posts: 700
Joined: 16 Aug 2008 17:51
Location: Long Eaton, Nottinghamshire. UK
Contact:

Re: [GS] MagicGS : Connects cities and towns by road

Post by JacobD88 »

Further observations since NRT was implemented

The over-use of Bridges appears to only become an issue when certain road/bridge speeds become available and the script prioritises them as the over-all connection would be "faster" for it - I suspect it is not by design, looking at the script, but one of flaws in the way the script works out "optimal routes" now that can be affected by NRT and the specification, in the script options, of the speeds that are used to link towns-towns, towns-cities and cities-cities.

This can easily be replicated by using something like RaTTRoads and the Total Bridge Renewal Set 1.2 (TBRS) and starting in an early year such as 1800 and a very large map with a lot of towns (so it doesn't run out of things to connect over the years)

As the road and bridge types appear you will see the script pass through using dirt roads at all times when not faced with water (where it will build a bridge) - the behaviour we want. Then it will progress to roads everywhere with regular use of tunnels as cobbled roads appear before returning to a brief period of just road building when the next generation of bridges appear and finally going absolutely mad with bridge building as the combination of faster roads and faster bridges allow it to conclude that obsessive bridge building allows for faster connections

An overly simple solution to this would be to add in a check that says that if the route can be built without a bridge - always use the latest road type available - forcing the script to only use bridges if a route with road cannot be found and only for sections of the connection where placing road is not possible.

Tunnels are a little more complex - unfortunately, except for that niche period of tunnel building in the 1800s It's hard to document "real game" behaviours with this script. I'll continue seeing if it can be "improved" in anyway by what it does over several games. My current though is that it does it's job well during this period, but the lack of tunnel construction later is a little baffling (although it mirrors real life where cuttings became cheaper than tunnels as new excavation equipment became available).

On this front, one of the annoyances of the script at present is that it likes to place road tiles across inclines when moving a couple of tiles in either direction would allow it to follow the terrain better - I do wonder if the script can be extended to allow some terrain modification (up to a certain, specifiable, range of tiles; for example, no more than 1x1, 2x2, 3x3, etc. at any given point) and encouraged to explore possible paths out to a width of xx many tiles of the currently planned route
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests