Page 1 of 2

Town Demand patch

Posted: 10 Oct 2011 13:40
by Korenn
Town Demand
As an other step towards getting a town growth challenge setting in trunk, I've written a patch that adds new town demand settings. For those of you who don't know, check the Spinoff link in my signature. In a nutshell: the aim is to add a goal to playing OpenTTD, in particular the growing of towns.

When one of the town demand patch settings is chosen, it changes the demands of towns to the given selection. The demand is enforced on all towns on the map, regardless of location or climate.

A new setting is added in Economy / Towns, called Town Demand. There are 8 settings that can be chosen, beyond the default behaviour:
  • Food
  • Food and Water
  • Goods and Valuables
  • Food, Water and Goods
  • Food, Water, Goods and Valuables
  • Food, Water, Goods, Valuables and TE_DEMAND1 (displayed as 'Other' in the menu)
  • Food, Water, Goods, Valuables, TE_DEMAND1 and TE_DEMAND2
  • TE_DEMAND1 and TE_DEMAND2
Choosing one of these settings will overwrite the town demands for all towns in the game world, regardless of climate. This means that without a cargo newgrf, only setting 3 will allow towngrowth in Temperate, and settings 2, 4 and 5 will not grow in Arctic. Settings 6, 7 and 8 are specifically added for cargo newgrfs, to allow additional complexity to towngrowth at the newgrf-author's wish. Ofcourse, the names here are just the default cargoes. By changing the town effect in a newgrf, you can change demand to any cargo you want.

Code details
The patch introduces three new town effect values for cargoes.
  • TE_VALUABLES attributed to Valuables, Diamonds and Gold, substitute type 0x0A
  • TE_DEMAND1, needs setting by a cargo newgrf, substitute type 0x19
  • TE_DEMAND2, needs setting by a cargo newgrf, substitute type 0x1D
TE_DEMAND1 and TE_DEMAND2 are not usuable without a cargo newgrf. I had to pick some values to assign for the substitute type, so I chose two semi-random cargo slots beyond the default cargoes, which in FIRS map to Petrol and Building Materials. If these town effects are demanded without a suitable cargo available, it will be displayed as 'Unknown required'

Current cargo demand formulas:
  • TE_FOOD: t->population / 100;
  • TE_WATER: max(((population / 150) - 5), 0);
  • TE_GOODS: max(((population / 200) - 10), 0);
  • TE_VALUABLES: max(((population / 300) - 10), 0);
  • TE_DEMAND1: max(((population / 350) - 15), 0);
  • TE_DEMAND2: max(((population / 400) - 20), 0);
Eventually, the goal is to add a newgrf callback to allow grf makers to define their own formulas for a town effect, given a town (help needed). In the mean time, this implementation works a treat.

Notes:
* with town demands on, towns are limited to 6,553,500 population for settings 1,2 and 4-7. For setting 3 the limit is 13,109,000 and for setting 8 it is 22,942,500. These limits are due to the cargo amount being stored in a 16 bit unsigned integer, so delivering more cargo will just roll over to 0 and fail to meet the demand. The numbers are the maximum population that can be serviced by 65535 units of cargo delivered, using the above formulas.
* in Toyland, Toys now have effect TE_WATER, to supplement Fizzy Drinks (TE_FOOD) and Candy (TE_GOODS). This allows playing with settings 1, 2 and 4 in toyland without newgrfs. Due to the bad building zone assignments in Toyland, growing big cities there is quite a challenge!

Diff versus r23012
updated binary: now with lang files (!)

Re: Town Demand patch

Posted: 10 Oct 2011 13:40
by Korenn
A while back, I wrote an industry grf that matches the OCS industries. I've updated those to map to the right town effects, allowing play up to setting 5 in any climate.
temperate with tgc grf.png
temperate with tgc grf.png (94.53 KiB) Viewed 11783 times
tgc grf.zip
two grfs, add the industries after the cargo.
(14.24 KiB) Downloaded 289 times

Re: Town Demand patch

Posted: 10 Oct 2011 13:41
by Korenn
Personally I play a lot with FIRS, so I've also written a small FIRS conversion grf that changes the FIRS cargo substitute types to the above values. Include this one after FIRS and it will allow play up to setting 6.
FIRS with conversion grf.png
FIRS with conversion grf.png (28.24 KiB) Viewed 11782 times

Re: Town Demand patch

Posted: 10 Oct 2011 18:49
by Kogut

Re: Town Demand patch

Posted: 10 Oct 2011 19:49
by Korenn
I know, I've had considerable discussion about that with the devs already :)

But even if its present form doesn't make it into trunk, if enough people like the current patch that might motivate the devs to finish the features needed to rewrite this patch to newgrf support.

If people play this patch, please give feedback!

Re: Town Demand patch

Posted: 10 Oct 2011 19:51
by lawton27
Korenn wrote:But even if its present form doesn't make it into trunk, if enough people like the current patch that might motivate the devs to finish the features needed to rewrite this patch to newgrf support.
Allow me to show my approval of this patch :) A good idea indeed.

Re: Town Demand patch

Posted: 11 Oct 2011 07:21
by Korenn
So as not to exclude people who prefer ECS, I've also written a grf that makes ECS compatible with this patch:
ECS in tropical.png
ECS in tropical.png (37.51 KiB) Viewed 11601 times
6 Cargoes are defined, which means all settings can be played. It requires the Town, Chemical and Agricultural Vectors so that the cargoes are valid, and Houses so that there is somewhere they can be shipped. Include after the ECS grfs.

Unfortunately it appears that in George's Vectors 'Water Supplies' are only spawned in Tropical, which means any setting that includes water will not allow town growth in Temperate or Arctic in random games. Addendum: apparently they do spawn, but only at the highest elevation level, which doesn't normally occur enough.

Re: Town Demand patch

Posted: 11 Oct 2011 07:59
by ziond
Korenn wrote:... Unfortunately it appears that in George's Vectors 'Water Supplies' are only spawned in Tropical, which means any setting that includes water will not allow town growth in Temperate or Arctic in random games.
As wiki says:
http://www.tt-wiki.net/wiki/ECS_Town_Vector._Water_supply wrote:Build at least 15 levels above sea level
So you need to have a mountain :)

Re: Town Demand patch

Posted: 11 Oct 2011 09:09
by Korenn
ziond wrote:
http://www.tt-wiki.net/wiki/ECS_Town_Vector._Water_supply wrote:Build at least 15 levels above sea level
So you need to have a mountain :)
Oh, really? That's... curious, to say the least. That won't really happen in practice unless you play with a heightmap.

Re: Town Demand patch

Posted: 15 Oct 2011 07:07
by Korenn
I updated the windows binary package, it was missing the lang files! Curious how 11 people downloaded it but didn't say a word about it not working ;)

Re: Town Demand patch

Posted: 15 Oct 2011 07:15
by Lord Aro
missing lang files?

you should use 'make bundle' to stop that from happening

Re: Town Demand patch

Posted: 15 Oct 2011 07:27
by Korenn
Lord Aro wrote:missing lang files?

you should use 'make bundle' to stop that from happening
No I shouldn't, because that involves installing and setting up MingW or similar. Unless you know of a way to do that from VS.

Re: Town Demand patch

Posted: 15 Oct 2011 07:43
by planetmaker
Korenn wrote:
Lord Aro wrote:missing lang files?

you should use 'make bundle' to stop that from happening
No I shouldn't, because that involves installing and setting up MingW or similar. Unless you know of a way to do that from VS.
Lord Aro is totally right. Lang files are specific for each version and should to be part of distributions. And also building and shipping a bundle you make sure you don't violate the licenses by not shipping the required license information.

Re: Town Demand patch

Posted: 15 Oct 2011 08:04
by Korenn
planetmaker wrote:Lord Aro is totally right. Lang files are specific for each version and should to be part of distributions.
... what? I just uploaded a new version because I forgot the lang files. I wasn't claiming I shouldn't add them.

I'll add the GPL files to the zip.

Re: Town Demand patch

Posted: 15 Oct 2011 08:51
by FooBar
Whenever I build in MSVS I use a virtual linux to do the make bundle.
But then I already had the virtual linux for grf development. Still, if you don't want mingw or msys...

Re: Town Demand patch

Posted: 02 Nov 2011 16:40
by Kogut
Unfortunately towns are still growing without deliveries, also in game using binary from first post, without newgrfs. (for example with food required in arctic)

Re: Town Demand patch

Posted: 02 Nov 2011 20:40
by Kogut
This problem was caused by "t->act_food - GetTownCargoDemand(t, TE_FOOD)) < 0" checks. "t->act_food - GetTownCargoDemand(t, TE_FOOD))" is unsigned int :roll:!

So I added conversion to int in following places:

Code: Select all

		if (IsTownEffectRequired(TE_FOOD) && int(t->act_food - GetTownCargoDemand(t, TE_FOOD)) < 0) return;
		if (IsTownEffectRequired(TE_WATER) && int(t->act_water - GetTownCargoDemand(t, TE_WATER)) < 0) return;
		if (IsTownEffectRequired(TE_GOODS) && int(t->act_goods - GetTownCargoDemand(t, TE_GOODS)) < 0) return;
		if (IsTownEffectRequired(TE_VALUABLES) && int(t->act_valuables - GetTownCargoDemand(t, TE_VALUABLES)) < 0) return;
		if (IsTownEffectRequired(TE_DEMAND1) && int(t->act_demand1 - GetTownCargoDemand(t, TE_DEMAND1)) < 0) return;
		if (IsTownEffectRequired(TE_DEMAND2) && int(t->act_demand2 - GetTownCargoDemand(t, TE_DEMAND2)) < 0) return;
Now it works. On demand I will upload binary (merged with cargodist).

Re: Town Demand patch

Posted: 02 Nov 2011 20:57
by Rubidium
A construct such as "a - b < 0" can also be rewritten to "a < b" which is both shorter and easier to understand (at least for me).

Re: Town Demand patch

Posted: 03 Nov 2011 09:56
by ZxBiohazardZx
Rubidium wrote:A construct such as "a - b < 0" can also be rewritten to "a < b" which is both shorter and easier to understand (at least for me).
only if both a and b are real or int, in a bool case this is not always the case?

1 - 0 < 0 is an OR construct, not an AND, he checks for true in a AND b if im not mistaken in a weird way

Re: Town Demand patch

Posted: 03 Nov 2011 10:04
by planetmaker
in a boolean case you should use the boolean specific operators and and comparisons and not the ones dedicated to int or float numbers.