GS: Industry Constructor

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

R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Thank you once again for your time.

I learned quite a bit about squirrel going through that optimized function krinn, thanks for that. I changed most of my existing script to match (had to avoid some things I don't understand, like the foreach loop - whats with the optional parameter and the "dummy" or "_" sometimes?). Also your idea of encapsulating my town nodes and associated lists in a class, rather than using the 3 public arrays (should be 1 2D array), is intriguing. It could make my other, more accurate, town tile function viable for larger maps as the list won't have to be recreated each loop. Don't know how OpenTTD will handle the large numbers of tiles in memory though, could be up to 6000 per town + large map of 1000+ towns...

And thanks Zuu, that syntax has been elusive for me, no info in the language docs or google. Now I can resume my AI :D

So thanks to you guys advice, and moving to a town size approximation algorithm, this new version is an order of magnitude faster :shock:

A 1024 * 1024 can be created under quota (faster than orig for some reason also) and a 2048 * 2048 in under 5 mins (using normal settings also, thats 3600 industries!) :P
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: GS: Industry Constructor

Post by krinn »

foreach is just a loop
local thatvar = [a,b,c,d];

if you don't care about the member, to only use the value :
foreach (value in thatvar) -> value = a, balue = b...
because thatvar[0]=a where 0 is the member and a the value

care of both
foreach (item, value in thatvar) -> item= 0 value=a, item=1, value=b...

care of only the member, for this people just use _ or dummy or anything to show you don't really care about it
foreach (item, _ in thatvar) -> item = 0, _ = a...

That's computer deal : using ram or using cpu, you have to use one or the other. I think a guy that want play on huge map have the memory to do that, while his cpu ressource remain tight to one core. So more ram eat to lower cpu usage is better for most people, and ones with too few memory certainly cannot play it with or without the script because of the map size.
Let's take the highest cache hit, so your random town function always pickup the same town: looping 3600 times
- no cache: 3600+3600+3600 calls to functions (get house tile, get border tiles, remove bordertiles)
- cache : 3599 addlist calls (get house tile) + 3599 addlist calls (get border tile) + 2 calls for your functions
i don't think an addlist takes more times than any of those 3 functions...

You can save memory if the cache have no usage after the function, just clear it on exit.
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Thanks for the explanation krinn, I understand it now. Looks like it is a great loop method for lists!

I’m going to take a swing at the proposed method using a class for each node to see how it performs. I think it will be useful on large maps, as my current town size approximation error scales with town size, large maps have many more large towns and therefore larger error. Maybe I give the option as a parameter, to use the slow method, and have a threshhold for townsize, hmm.
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Is it possible for a GS (or AI for that matter), to found a town, maybe on a specific tile? I could not find any functions in the nogo/noai docs...

(I'm looking to add town generation using my algorithms :) )
User avatar
Dave
Moderator
Moderator
Posts: 17243
Joined: 26 Dec 2005 20:19
Location: North London

Re: GS: Industry Constructor

Post by Dave »

When using the settings in the screenshot, the script loops looking to place industries around towns. Problem is solved if you enable similar industries per town (the general one, not the town one)

One feature request: Could you lower the bank population minimum to 300-400?
Attachments
scriptbug.png
scriptbug.png (34.9 KiB) Viewed 7094 times
Official TT-Dave Fan Club

Dave's Screenshot Thread! - Albion: A fictional Britain
Flickr


Why be a song when you can be a symphony? r is a...
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Attached is a version with minimum pop of 0 for banks and water towers, only here not on bananas currently. I hope to make the next official release one that can create towns also, no progress with this yet though.

Regarding the stuck town building loop; what map size and town count are you using? I guess you realise that if there are not enough valid towns for all the industries (per industry type and up to max ind per town) then the script will understandably get stuck. If this is the case then it is a bug, I will look what happens in some scenarios to test. Thanks for the report.

FYI, I sacrificed reliability for freedom for the settings, so there are probably many setting combos where the script will get stuck. To deal with this I made many settings changeable on the fly, in game. If you are getting some (hopefully comprehensible) errors you can tweak the settings to give more room for the script to build as it is trying :)
Attachments
IndustryConstructor-3.1.tar
(123 KiB) Downloaded 219 times
User avatar
Dave
Moderator
Moderator
Posts: 17243
Joined: 26 Dec 2005 20:19
Location: North London

Re: GS: Industry Constructor

Post by Dave »

A fair point. I was using PBI with Early Houses on again so probably not your fault!

I've changed all to scattered now which creates interesting layouts with good scope.

The town idea is a very intriguing one and I'm looking forward to seeing it - thanks for this update in the meantime.
Official TT-Dave Fan Club

Dave's Screenshot Thread! - Albion: A fictional Britain
Flickr


Why be a song when you can be a symphony? r is a...
Efcon
Engineer
Engineer
Posts: 2
Joined: 04 Sep 2013 20:57

Re: GS: Industry Constructor

Post by Efcon »

Hello. I recently instaled industry constructor and I got this error. Did the same thing thrice and but the fourth attempt was alright again.
Attachments
tycooncrash.jpg
(710.45 KiB) Downloaded 7 times
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Hi Efcon

The reason for the crash is given in the second warning line of text (warnings are in yellow); because you have started (or loaded) a game with the script but also with industries set in map creation. The setting must be "Funding only" on map creation as described in the Readme. The reason for this, as well as the crash, is to allow the gamescript to take over industry creation from the default not run along side. Your setup has 1019 default industries created by the map generator.

Edit: To clarify in case I have not mentioned it anywhere else, this script requires a new game, or a game with no default industries to work.
Efcon
Engineer
Engineer
Posts: 2
Joined: 04 Sep 2013 20:57

Re: GS: Industry Constructor

Post by Efcon »

I see. Damn. So is there no way to stop The encroachment of power plants ? 321 and counting.
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Well, if you have loaded an existing game with the Game Script which was not started with it then sorry, no as it was not designed to be used like this. I will add some safeguards in the next version.

What are you trying to do? Play an existing game with default industries with this GS which maintains the map industry numbers using the placement algorithms?
If you are just looking to maintain the numbers but don't care about placement then I suggest you use Aphids version of this concept, AFAIK it will work with existing games.

My script was designed to do most of its magic on map creation, the maintenance of numbers is just kind of a feature :P
NicoH
Engineer
Engineer
Posts: 3
Joined: 22 Jan 2014 00:45

Re: GS: Industry Constructor

Post by NicoH »

Good day!

I used this script on a new game start today and got these errors with the default settings of the script:
error1.PNG
error1.PNG (63.11 KiB) Viewed 1059 times
These errors vanish if I set the spawn method for primary industries from cluster to random.

Also while ingame these errors came up:
error2.PNG
error2.PNG (29.12 KiB) Viewed 6238 times
They vanish after about 4 years, but also do the script messages and there aren't any new industries built after that.
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Hey thanks for the report. Are you using any grfs? Especially industry grfs like ECS or FIRS?
NicoH
Engineer
Engineer
Posts: 3
Joined: 22 Jan 2014 00:45

Re: GS: Industry Constructor

Post by NicoH »

These are all grfs I'm currently using:
grf1.png
grf1.png (9.44 KiB) Viewed 6195 times
When I disable all grf, the script executes at a new game start without errors, but left me with maps where only factories, refineries and steel-mill are built. Very odd.
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

So there is a known issue with OpenGfx+ Industries, that needs to be fixed in the grf to work nicely with GS/AI etc. Also IIRC Total Town Replacement Set has the petrol cargo added for towns? If you have this enabled it will cause the errors seen in the log pic you posted. I would need to add specific code to handle this cargo, quite likely for TTRS but not soon. I have only tested with PBI and standard so far...

Another possible issue is that with the Clusters method you need "multiple industries per town" enabled in most cases to get it to work, as otherwise same industries cannot be built so close together.

If you run the script with default OpenTTD on default script settings and methods and still get the empty map then there is some serious issue...

I am busy working on a rewrite of this GS which includes building towns, it also has some features to ask the player for help when situations happen that the script gets stuck. There are so many ways this can happen, as you can see from the bugs :(

So rather than try debug the current version, if you still get errors, I will post the WIP one once it is able to do industries properly like the current one. :)
NicoH
Engineer
Engineer
Posts: 3
Joined: 22 Jan 2014 00:45

Re: GS: Industry Constructor

Post by NicoH »

I noticed, that it only seems to be a map with three industries, because the script takes around a year before it starts to spawn the other industries, but it works after this year. After activating same industries in cities, the cluster method also works. Even if I still get some cluster placement errors (with and without the mentioned grfs), the script seems to run without crashing. :D
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

So I have almost finished a new version of this, it is a complete rewrite and has some new features. Actually I think I may wish to re-release it as a new script because "Industry Constructor" is now just a part of what it does. Is there any official policy regarding re-branding?

This new version is much faster, reliable and handles the many difficulties better. It also can create towns too, using 4 algorithms:

- Random, mimics the standard distribution.
- Scattered, evenly spaced from eachother.
- Clustered, separate groups, customizable.
- Metropolis, similar to "clustered" but has a city in the middle and towns all around, instead of random towns/cities.

Another new feature is messages to the player telling you when things are struggling and hints how to fix (note the Readme is not yet updated to help with this).

Finally a WIP aspect is the ability to generate roads between towns, these will be based on the town algorithm selected so Clusters join eachother, Metropolis joins the city to towns and Scattered/Random joins all on map using a shortest pathfinder (a genetic algorithm written to solve the Travelling Salesman problem here, I might add 8) ). This is not yet finished, but here is the current version with towns and industries generation if anyone wants to check out the towns stuff.
Attachments
MapConstructor.tar
(180.5 KiB) Downloaded 207 times
User avatar
Dave
Moderator
Moderator
Posts: 17243
Joined: 26 Dec 2005 20:19
Location: North London

Re: GS: Industry Constructor

Post by Dave »

Looking forward to this dude.
Official TT-Dave Fan Club

Dave's Screenshot Thread! - Albion: A fictional Britain
Flickr


Why be a song when you can be a symphony? r is a...
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Thanks, yeah this has grown quite a bit more complex than original goals...but what the hey :)

Just had a successful test for roads generation, finally got my Travelling Salesman algorithm working nice. After around 600 or so "evolutions" / generations it can solve a 256^2 map, max cities, to satisfaction in a few hundred ticks. Larger maps will be split into 256^2 bits for performance. Road builder takes long as it was designed for accuracy not speed but can build all roads in 3 months give or take.

Image
Image
Attachments
256^2 map showing shortest path.
256^2 map showing shortest path.
Roads.png (29.14 KiB) Viewed 1056 times
512^2 map showing the 4 smaller chunks.
512^2 map showing the 4 smaller chunks.
BigRoads.png (40.04 KiB) Viewed 1056 times
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GS: Industry Constructor

Post by R2dical »

Pretty much done, odds and ends to tie up and see if there are no bugs. But till I work on this again, here is an unofficial version.
MapConstructor.tar
Feature complete, but probably has bugs. V0.9 I guess.
(290 KiB) Downloaded 231 times
Image
Attachments
An example of Metropolis town generation and road network.
An example of Metropolis town generation and road network.
Metropolis.png (13.5 KiB) Viewed 1055 times
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 6 guests