Skip to content

Commit

Permalink
Codechange: Update each town label dimensions once during map gen. (O…
Browse files Browse the repository at this point in the history
…penTTD#12742)

This avoids re-layouting the label for each change in population as the town is grown.
  • Loading branch information
PeterN authored Jun 1, 2024
1 parent 96fc367 commit 6d2424c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/town_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void RebuildTownKdtree()
_town_kdtree.Build(townids.begin(), townids.end());
}

/** Set if a town is being generated. */
static bool _generating_town = false;

/**
* Check if a town 'owns' a bridge.
Expand Down Expand Up @@ -442,6 +444,8 @@ void ClearAllTownCachedNames()
static void ChangePopulation(Town *t, int mod)
{
t->cache.population += mod;
if (_generating_town) [[unlikely]] return;

InvalidateWindowData(WC_TOWN_VIEW, t->index); // Cargo requirements may appear/vanish for small populations
if (_settings_client.gui.population_in_label) t->UpdateVirtCoord();

Expand Down Expand Up @@ -1973,6 +1977,8 @@ static void UpdateTownGrowth(Town *t);
*/
static void DoCreateTown(Town *t, TileIndex tile, uint32_t townnameparts, TownSize size, bool city, TownLayout layout, bool manual)
{
AutoRestoreBackup backup(_generating_town, true);

t->xy = tile;
t->cache.num_houses = 0;
t->time_until_rebuild = 10;
Expand Down Expand Up @@ -2016,9 +2022,6 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32_t townnameparts, TownSi
}
t->townnameparts = townnameparts;

t->UpdateVirtCoord();
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_REBUILD);

t->InitializeLayout(layout);

t->larger_town = city;
Expand All @@ -2036,6 +2039,9 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32_t townnameparts, TownSi
GrowTown(t);
} while (--i);

t->UpdateVirtCoord();
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_REBUILD);

t->cache.num_houses -= x;
UpdateTownRadius(t);
UpdateTownGrowthRate(t);
Expand Down

0 comments on commit 6d2424c

Please sign in to comment.