FS#5786 - GS - GSTown.SetGrowthRate fails

Attached to Project: OpenTTD
Opened by Jan Skoch (The_Dude) - Friday, 11 October 2013, 20:14 GMT
Last edited by frosch (frosch) - Tuesday, 12 November 2013, 17:58 GMT
Type Bug
Category Script → Goal/Game script
Status Closed
Assigned To No-one
Operating System All
Severity Medium
Priority Normal
Reported Version 1.3.2
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No


Bug or can be considered a feature request.

When GS controls town growth via GSTown.SetGrowthRate and when GS wants to stop the town growth, currently only done by setting high GR, let's say 1000 days and when GS wants to grow the town again and sets e.g. 10, the town will not grow for at least next 1000 days but will only rebuild houses.

This is cause by the grow_counter (town property), which is not reset when growth is set by GS.

In town_cmd.cpp is the static void UpdateTownGrowRate(Town *t)

prematurely ended with GS
if ((t->growth_rate & TOWN_GROW_RATE_CUSTOM) != 0) {
SetBit(t->flags, TOWN_IS_FUNDED);
SetWindowDirty(WC_TOWN_VIEW, t->index);

while skipping the grow_counter new value assignment which goes after
t->grow_counter = m;

In my opinion this bug can be solved either by
if ((t->growth_rate & TOWN_GROW_RATE_CUSTOM) != 0) {
t->grow_counter = t->growth_rate | ~TOWN_GROW_RATE_CUSTOM;
SetBit(t->flags, TOWN_IS_FUNDED);
SetWindowDirty(WC_TOWN_VIEW, t->index);
or by implementing new GS function that stops town growth more cleanly then setting very high growth rate (which is obscure)
This task depends upon

Closed by  frosch (frosch)
Tuesday, 12 November 2013, 17:58 GMT
Reason for closing:  Implemented
Additional comments about closing:  in r25931, r25967, r25968, r25969
Comment by frosch (frosch) - Monday, 28 October 2013, 11:35 GMT
Possible solution attached.
This adds two speical values ScriptTown::TOWN_GROWTH_NONE and TOWN_GROWTH_NORMAL, which can be used with SetGrowthRate.

It also fixes some weird behaviour of current SetGrowthRate and GetGrowthRate:
- GetGrowthRate now returns TOWN_GROWTH_NONE if the town is not growing, instead of some imaginary value.
- SetGrowthRate reset the custom growth rate if zero was passed to it (undocumented). Now, there is a special value TOWN_GROWTH_NORMAL for that.

I am not sure whether the SetGrowthRate(0) thingie is used by scripts. Maybe it needs a API compatibility script, or we can adjust TOWN_GROWTH_NORMAL to use the value "0". Though that value is somewhat treacherous when using a formula to compute a growth rate.
Comment by Jan Skoch (The_Dude) - Tuesday, 29 October 2013, 14:18 GMT
Looks very promising. I think such solution really solves the growth zero and fund buildings override. Good work.

Just three remarks I would have

1) GS could have a function GSTown::GetFundedMonths of simply IsFunded(), so script could extend growth when players spent money for funding (like normal openttd does)

2) I still miss the grow_counter adjustment when script sets new town growh
Something like
if(t->grow_counter >= t->growth) t->grow_counter = t->growth;
so when you set high growth, and after some time you set lower or much lower, because player did very good, counter could behave the same as in game, where it is reseted like this

3) Also some script function to set growth back to non-custom might be useful, although I admit I am not sure about that
Comment by frosch (frosch) - Wednesday, 30 October 2013, 10:03 GMT
2) Fixed in r25931.
3) Already included in above diff via TOWN_GROWTH_NORMAL.
Comment by Jan Skoch (The_Dude) - Wednesday, 30 October 2013, 11:09 GMT
Excellent. That would be really very great to see in next branch.

Also if 1) would be included