[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: 1264
Joined: 15 Feb 2003 17:32
Location: Villefranche-sur-Saône, 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 68 times
Last edited by MagicBuzz on 20 Jul 2020 20:25, edited 1 time in total.

Firrel
Engineer
Engineer
Posts: 36
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: 1245
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.

Firrel
Engineer
Engineer
Posts: 36
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: 1264
Joined: 15 Feb 2003 17:32
Location: Villefranche-sur-Saône, 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: 1264
Joined: 15 Feb 2003 17:32
Location: Villefranche-sur-Saône, 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!

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 4 guests