Town Demand patch

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

User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Town Demand patch

Post 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 (!)
Attachments
town_demand.diff
Diff versus r23012
(32.42 KiB) Downloaded 351 times
no grfs required for play.
no grfs required for play.
toyland.png (32.79 KiB) Viewed 11695 times
win32.zip
Now with license!
(5.32 MiB) Downloaded 326 times
Last edited by Korenn on 15 Oct 2011 08:23, edited 11 times in total.
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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 11696 times
tgc grf.zip
two grfs, add the industries after the cargo.
(14.24 KiB) Downloaded 289 times
Last edited by Korenn on 10 Oct 2011 22:31, edited 4 times in total.
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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 11695 times
Attachments
firstd.zip
FIRS Town Demand grf
(12.54 KiB) Downloaded 307 times
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Town Demand patch

Post by Kogut »

Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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!
User avatar
lawton27
Tycoon
Tycoon
Posts: 1418
Joined: 03 Aug 2009 14:29
Location: Manchester

Re: Town Demand patch

Post 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.
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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 11514 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.
Attachments
ecstd.zip
ECS Town Demand
(12.54 KiB) Downloaded 284 times
Last edited by Korenn on 12 Oct 2011 09:37, edited 1 time in total.
ziond
Engineer
Engineer
Posts: 50
Joined: 04 Mar 2009 10:55
Location: Moscow, Russia

Re: Town Demand patch

Post 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 :)
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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.
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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 ;)
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Town Demand patch

Post by Lord Aro »

missing lang files?

you should use 'make bundle' to stop that from happening
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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.
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Town Demand patch

Post 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.
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Re: Town Demand patch

Post 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.
User avatar
FooBar
Tycoon
Tycoon
Posts: 6553
Joined: 21 May 2007 11:47
Location: The Netherlands
Contact:

Re: Town Demand patch

Post 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...
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Town Demand patch

Post 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)
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Town Demand patch

Post 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).
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: Town Demand patch

Post 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).
ZxBiohazardZx
Tycoon
Tycoon
Posts: 1534
Joined: 14 Mar 2006 12:46
Location: Netherlands

Re: Town Demand patch

Post 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
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Town Demand patch

Post 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.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 40 guests