Industry generation: need help !

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
zifox
Engineer
Engineer
Posts: 47
Joined: 26 Jan 2011 14:43
Location: France

Industry generation: need help !

Post by zifox »

Hi,

i'm like some insights on how the industry count target are computed, because the code doesn't seem to make any sense to me.

I'm struggling with the wanted_inds variable and its uses.
The variable seems to be initialized only in IndustryBuildData::Reset(), where we write the current amount of industries in the map into the higher 16bits, or when loading a save.

In other parts of the code, we fetch that value to see it we need to create industries by comparing the actual industry count and the target "wanted_inds". (SetupTargetCount for example)

My main issue is MonthlyLoop, where we increase the lower 16bits of the variables in a particular check if validated.
Meaning that after doing this a lot of times, we'll actually increase the 16 higher bits.

Snippet:

Code: Select all

	/* To prevent running out of unused industries for the player to connect,
	 * add a fraction of new industries each month, but only if the manager can keep up. */
	uint max_behind = 1 + std::min(99u, ScaleByMapSize(3)); // At most 2 industries for small maps, and 100 at the biggest map (about 6 months industry build attempts).
	if (GetCurrentTotalNumberOfIndustries() + max_behind >= (this->wanted_inds >> 16)) {
		this->wanted_inds += ScaleByMapSize(NEWINDS_PER_MONTH);
	}
From that snippet, I deduce that there is no limit to wanted_inds (except if we overflow). :shock:

This doesn't seem right, and I'm wondering if I missed something, an obfuscated use / modification through pointer arithmetic. Any help is appreciated.

This far, i'm thinking of rewriting this part and:
- Keep the "industry density" parameter for map creation only
- Add a new option "industry creation" for game only in the environment -> industry menu, with three possible values: "Funding only", "Original behavior", "Maintain industry density".

What's your opinion ?
User avatar
jfs
Tycoon
Tycoon
Posts: 1743
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: Industry generation: need help !

Post by jfs »

Well yes, that does seem to be the intention of that code: Slowly increase the number of wanted industries.

But only under a specific condition: There are already many more industries on the map than the current wanted number. That would only happen if the player is funding industry construction.
zifox
Engineer
Engineer
Posts: 47
Joined: 26 Jan 2011 14:43
Location: France

Re: Industry generation: need help !

Post by zifox »

jfs wrote: 10 Oct 2022 07:37 Well yes, that does seem to be the intention of that code: Slowly increase the number of wanted industries.

But only under a specific condition: There are already many more industries on the map than the current wanted number. That would only happen if the player is funding industry construction.
Over time, the condition will always be true, not because the player can create industries, but because we always have that extra (constant) delta added.
If you have 129 industries on the map and (wanted_inds>>16) is equal to 129, meaning the target is reached, the max_behind value will over time slowly increase the target.

And that's very confusing: I can understand the need to slowly increase the target count (though it can be debated), but clearly don't see the point of having wanted_inds catch up with the real industry count. Moreover the comment doesn't match the code: "but only if the manager can keep up", adding the my confusion.

I'll have a look at the history of the file, to understand how the code evolved. From an exterior point of view, there is a mixup somewhere.
User avatar
jfs
Tycoon
Tycoon
Posts: 1743
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: Industry generation: need help !

Post by jfs »

Yes, the comment definitely seems to talk about number of industries being served, rather than number of industries that exist. Question is whether that's a mistake of the comment or of the code.
zifox
Engineer
Engineer
Posts: 47
Joined: 26 Jan 2011 14:43
Location: France

Re: Industry generation: need help !

Post by zifox »

I did a few tests to understand this industry creation issue, and it seems the auto increase of the industry count hasn't been thought for big maps.

On a 256x256 map, each month the wanted_inds variable is raised by 1. Since the lower 16 bits of the variable is used as a fraction, it means you need 65536 months before the "real" industry target count changes. So...never.
In my 2kx2k map, the amount added is 122304. :shock: Yes. That's a lot. That's two industries added to the target count each month. No wonder they were popping like crazy !

Also I found that:
- When creating a new game, wanted_inds is correctly initialized to the actual industry count.
- When loading a scenario, it's not, and left at 0. Lot of catchup to do before actually adding to new industries, that why in my scenario, the game starts to create industries 10 years into the game.


I'll finished to implement my new creation scheme then maybe try to fix the default behavior so it can work on huge maps.

I've done the changes needed to split the parameters in two. One "industry density" when creating map, one "industry creation behavior" during the game. Now all I need to do is tune my calculus.
I've settled on a possible variation of 10-15% around the original industry number, I'm thinking about modulating it with the population variation on the map. Increase in population would mean increase in need, so more industries.
zifox
Engineer
Engineer
Posts: 47
Joined: 26 Jan 2011 14:43
Location: France

Re: Industry generation: need help ! And other dev / contributing questions

Post by zifox »

A new question rises after adding some new options: how do we handle translations ? I have added some options / parameters so I had to create new strings for display, but I'm unable to translate them.
How is translation managed ?
I've seen that the english.txt file is used as master, is there a tool somwhere to automatically generate/update the other language files to match the english one. By match, I mean at least add the new string IDs so we don't have to do it manually, and maybe copy the english string so the file is valid.
I've seen that a web translator exists, but I can't use it until the code is merged ?
Eddi
Tycoon
Tycoon
Posts: 8254
Joined: 17 Jan 2007 00:14

Re: Industry generation: need help ! And other dev / contributing questions

Post by Eddi »

zifox wrote: 17 Oct 2022 09:51I've seen that a web translator exists, but I can't use it until the code is merged ?
the patch should only change english, and let the translators figure out their stuff afterwards.
User avatar
jfs
Tycoon
Tycoon
Posts: 1743
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: Industry generation: need help !

Post by jfs »

The web translator automatically detects that new strings were added to english.txt and asks the translators for other languages to create those too. Strings that were changed in english.txt also get flagged as needing attention in the other translations.
zifox
Engineer
Engineer
Posts: 47
Joined: 26 Jan 2011 14:43
Location: France

Re: Industry generation: need help !

Post by zifox »

Things are coming together, i'm doing some tests to check if the ratio i used are correct and so far, so good.

I've been struggling with a bug, which has disappeared I don't know how, maybe somebody could tell me if something similar already happened ?

I loaded a scenario from an older version, and modified it to set the industry behavior to my liking. Then I played the scenario, and got a crash 1 or 2 second after I removed the pause. The crash happens when drawing tiles, and does not happen if I compile in debug.
I check several times that everything was saved and loaded correctly, no issue found. The problem does not arise when generating a new map, or loading a game saved. Only when starting my recently saved scenario.

I've been wondering if the issue could come from unused farm fields, as I have a lot of them in my scenario, but weirdly, it works if I play my unmodified (old version) scenario. So there is definitely an issue with saving.

To add even my questions: the crash disappeared after a few recompiles, switching from debug to release. ?(
zifox
Engineer
Engineer
Posts: 47
Joined: 26 Jan 2011 14:43
Location: France

Re: Industry generation: need help !

Post by zifox »

I'm almost done testing it, i think i'll create a MR in the coming days.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 5 guests