changeset 19290:68bfe04ccd9a draft

(svn r24179) -Codechange: move some variables of Town to TownCache
author rubidium <rubidium@openttd.org>
date Wed, 25 Apr 2012 20:50:13 +0000
parents 051134f6d6bd
children 7d117b7796e1
files src/industry_cmd.cpp src/newgrf_house.cpp src/newgrf_town.cpp src/road_cmd.cpp src/saveload/town_sl.cpp src/script/api/script_town.cpp src/smallmap_gui.cpp src/subsidy.cpp src/town.h src/town_cmd.cpp src/town_gui.cpp src/tunnelbridge_cmd.cpp src/viewport.cpp
diffstat 13 files changed, 108 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -1449,7 +1449,7 @@
  */
 static CommandCost CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
 {
-	if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
+	if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->cache.population < 1200) {
 		return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200);
 	}
 
--- a/src/newgrf_house.cpp
+++ b/src/newgrf_house.cpp
@@ -49,6 +49,11 @@
 void InitializeBuildingCounts()
 {
 	memset(&_building_counts, 0, sizeof(_building_counts));
+
+	Town *t;
+	FOR_ALL_TOWNS(t) {
+		memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts));
+	}
 }
 
 /**
@@ -63,12 +68,12 @@
 
 	if (!_loaded_newgrf_features.has_newhouses) return;
 
-	t->building_counts.id_count[house_id]++;
+	t->cache.building_counts.id_count[house_id]++;
 	_building_counts.id_count[house_id]++;
 
 	if (class_id == HOUSE_NO_CLASS) return;
 
-	t->building_counts.class_count[class_id]++;
+	t->cache.building_counts.class_count[class_id]++;
 	_building_counts.class_count[class_id]++;
 }
 
@@ -84,13 +89,13 @@
 
 	if (!_loaded_newgrf_features.has_newhouses) return;
 
-	if (t->building_counts.id_count[house_id] > 0) t->building_counts.id_count[house_id]--;
-	if (_building_counts.id_count[house_id] > 0)   _building_counts.id_count[house_id]--;
+	if (t->cache.building_counts.id_count[house_id] > 0) t->cache.building_counts.id_count[house_id]--;
+	if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--;
 
 	if (class_id == HOUSE_NO_CLASS) return;
 
-	if (t->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--;
-	if (_building_counts.class_count[class_id] > 0)   _building_counts.class_count[class_id]--;
+	if (t->cache.building_counts.class_count[class_id] > 0) t->cache.building_counts.class_count[class_id]--;
+	if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
 }
 
 static uint32 HouseGetRandomBits(const ResolverObject *object)
@@ -123,8 +128,8 @@
 
 	map_id_count     = ClampU(_building_counts.id_count[house_id], 0, 255);
 	map_class_count  = ClampU(_building_counts.class_count[class_id], 0, 255);
-	town_id_count    = ClampU(town->building_counts.id_count[house_id], 0, 255);
-	town_class_count = ClampU(town->building_counts.class_count[class_id], 0, 255);
+	town_id_count    = ClampU(town->cache.building_counts.id_count[house_id], 0, 255);
+	town_class_count = ClampU(town->cache.building_counts.class_count[class_id], 0, 255);
 
 	return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
 }
--- a/src/newgrf_town.cpp
+++ b/src/newgrf_town.cpp
@@ -55,21 +55,21 @@
 		/* Town properties */
 		case 0x80: return t->xy;
 		case 0x81: return GB(t->xy, 8, 8);
-		case 0x82: return ClampToU16(t->population);
-		case 0x83: return GB(ClampToU16(t->population), 8, 8);
+		case 0x82: return ClampToU16(t->cache.population);
+		case 0x83: return GB(ClampToU16(t->cache.population), 8, 8);
 		case 0x8A: return t->grow_counter;
 		case 0x92: return t->flags;  // In original game, 0x92 and 0x93 are really one word. Since flags is a byte, this is to adjust
 		case 0x93: return 0;
-		case 0x94: return ClampToU16(t->squared_town_zone_radius[0]);
-		case 0x95: return GB(ClampToU16(t->squared_town_zone_radius[0]), 8, 8);
-		case 0x96: return ClampToU16(t->squared_town_zone_radius[1]);
-		case 0x97: return GB(ClampToU16(t->squared_town_zone_radius[1]), 8, 8);
-		case 0x98: return ClampToU16(t->squared_town_zone_radius[2]);
-		case 0x99: return GB(ClampToU16(t->squared_town_zone_radius[2]), 8, 8);
-		case 0x9A: return ClampToU16(t->squared_town_zone_radius[3]);
-		case 0x9B: return GB(ClampToU16(t->squared_town_zone_radius[3]), 8, 8);
-		case 0x9C: return ClampToU16(t->squared_town_zone_radius[4]);
-		case 0x9D: return GB(ClampToU16(t->squared_town_zone_radius[4]), 8, 8);
+		case 0x94: return ClampToU16(t->cache.squared_town_zone_radius[0]);
+		case 0x95: return GB(ClampToU16(t->cache.squared_town_zone_radius[0]), 8, 8);
+		case 0x96: return ClampToU16(t->cache.squared_town_zone_radius[1]);
+		case 0x97: return GB(ClampToU16(t->cache.squared_town_zone_radius[1]), 8, 8);
+		case 0x98: return ClampToU16(t->cache.squared_town_zone_radius[2]);
+		case 0x99: return GB(ClampToU16(t->cache.squared_town_zone_radius[2]), 8, 8);
+		case 0x9A: return ClampToU16(t->cache.squared_town_zone_radius[3]);
+		case 0x9B: return GB(ClampToU16(t->cache.squared_town_zone_radius[3]), 8, 8);
+		case 0x9C: return ClampToU16(t->cache.squared_town_zone_radius[4]);
+		case 0x9D: return GB(ClampToU16(t->cache.squared_town_zone_radius[4]), 8, 8);
 		case 0x9E: return t->ratings[0];
 		case 0x9F: return GB(t->ratings[0], 8, 8);
 		case 0xA0: return t->ratings[1];
@@ -88,7 +88,7 @@
 		case 0xAD: return GB(t->ratings[7], 8, 8);
 		case 0xAE: return t->have_ratings;
 		case 0xB2: return t->statues;
-		case 0xB6: return ClampToU16(t->num_houses);
+		case 0xB6: return ClampToU16(t->cache.num_houses);
 		case 0xB9: return t->growth_rate & (~TOWN_GROW_RATE_CUSTOM);
 		case 0xBA: return ClampToU16(t->supplied[CT_PASSENGERS].new_max);
 		case 0xBB: return GB(ClampToU16(t->supplied[CT_PASSENGERS].new_max), 8, 8);
--- a/src/road_cmd.cpp
+++ b/src/road_cmd.cpp
@@ -493,7 +493,7 @@
 			company = OWNER_TOWN;
 
 			/* If we are not within a town, we are not owned by the town */
-			if (town == NULL || DistanceSquare(tile, town->xy) > town->squared_town_zone_radius[HZB_TOWN_EDGE]) {
+			if (town == NULL || DistanceSquare(tile, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
 				company = OWNER_NONE;
 			}
 		}
--- a/src/saveload/town_sl.cpp
+++ b/src/saveload/town_sl.cpp
@@ -32,8 +32,8 @@
 
 	/* Reset town population and num_houses */
 	FOR_ALL_TOWNS(town) {
-		town->population = 0;
-		town->num_houses = 0;
+		town->cache.population = 0;
+		town->cache.num_houses = 0;
 	}
 
 	for (TileIndex t = 0; t < MapSize(); t++) {
@@ -88,10 +88,10 @@
 		HouseID house_id = GetCleanHouseType(t);
 		town = Town::GetByTile(t);
 		IncreaseBuildingCount(town, house_id);
-		if (IsHouseCompleted(t)) town->population += HouseSpec::Get(house_id)->population;
+		if (IsHouseCompleted(t)) town->cache.population += HouseSpec::Get(house_id)->population;
 
 		/* Increase the number of houses for every house, but only once. */
-		if (GetHouseNorthPart(house_id) == 0) town->num_houses++;
+		if (GetHouseNorthPart(house_id) == 0) town->cache.num_houses++;
 	}
 
 	/* Update the population and num_house dependant values */
--- a/src/script/api/script_town.cpp
+++ b/src/script/api/script_town.cpp
@@ -52,14 +52,14 @@
 {
 	if (!IsValidTown(town_id)) return -1;
 	const Town *t = ::Town::Get(town_id);
-	return t->population;
+	return t->cache.population;
 }
 
 /* static */ int32 ScriptTown::GetHouseCount(TownID town_id)
 {
 	if (!IsValidTown(town_id)) return -1;
 	const Town *t = ::Town::Get(town_id);
-	return t->num_houses;
+	return t->cache.num_houses;
 }
 
 /* static */ TileIndex ScriptTown::GetLocation(TownID town_id)
@@ -125,11 +125,11 @@
 
 	switch (t->goal[towneffect_id]) {
 		case TOWN_GROWTH_WINTER:
-			if (TileHeight(t->xy) >= GetSnowLine() && t->population > 90) return 1;
+			if (TileHeight(t->xy) >= GetSnowLine() && t->cache.population > 90) return 1;
 			return 0;
 
 		case TOWN_GROWTH_DESERT:
-			if (GetTropicZone(t->xy) == TROPICZONE_DESERT && t->population > 60) return 1;
+			if (GetTropicZone(t->xy) == TROPICZONE_DESERT && t->cache.population > 60) return 1;
 			return 0;
 
 		default: return t->goal[towneffect_id];
@@ -170,7 +170,7 @@
 	if (!IsValidTown(town_id)) return false;
 
 	const Town *t = ::Town::Get(town_id);
-	return ((uint32)GetDistanceSquareToTile(town_id, tile) <= t->squared_town_zone_radius[0]);
+	return ((uint32)GetDistanceSquareToTile(town_id, tile) <= t->cache.squared_town_zone_radius[0]);
 }
 
 /* static */ bool ScriptTown::HasStatue(TownID town_id)
--- a/src/smallmap_gui.cpp
+++ b/src/smallmap_gui.cpp
@@ -879,17 +879,17 @@
 		FOR_ALL_TOWNS(t) {
 			/* Remap the town coordinate */
 			Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
-			int x = pt.x - this->subscroll - (t->sign.width_small >> 1);
+			int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
 			int y = pt.y;
 
 			/* Check if the town sign is within bounds */
-			if (x + t->sign.width_small > dpi->left &&
+			if (x + t->cache.sign.width_small > dpi->left &&
 					x < dpi->left + dpi->width &&
 					y + FONT_HEIGHT_SMALL > dpi->top &&
 					y < dpi->top + dpi->height) {
 				/* And draw it. */
 				SetDParam(0, t->index);
-				DrawString(x, x + t->sign.width_small, y, STR_SMALLMAP_TOWN);
+				DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
 			}
 		}
 	}
--- a/src/subsidy.cpp
+++ b/src/subsidy.cpp
@@ -121,7 +121,7 @@
 {
 	switch (type) {
 		case ST_INDUSTRY: Industry::Get(index)->part_of_subsidy |= flag; return;
-		case ST_TOWN:         Town::Get(index)->part_of_subsidy |= flag; return;
+		case ST_TOWN:   Town::Get(index)->cache.part_of_subsidy |= flag; return;
 		default: NOT_REACHED();
 	}
 }
@@ -130,7 +130,7 @@
 void RebuildSubsidisedSourceAndDestinationCache()
 {
 	Town *t;
-	FOR_ALL_TOWNS(t) t->part_of_subsidy = POS_NONE;
+	FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE;
 
 	Industry *i;
 	FOR_ALL_INDUSTRIES(i) i->part_of_subsidy = POS_NONE;
@@ -297,13 +297,13 @@
 	if (!Subsidy::CanAllocateItem()) return false;
 
 	const Town *src = Town::GetRandom();
-	if (src->population < SUBSIDY_PAX_MIN_POPULATION ||
+	if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
 			src->GetPercentTransported(CT_PASSENGERS) > SUBSIDY_MAX_PCT_TRANSPORTED) {
 		return false;
 	}
 
 	const Town *dst = Town::GetRandom();
-	if (dst->population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
+	if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
 		return false;
 	}
 
@@ -534,7 +534,7 @@
 			if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
 			break;
 		case ST_TOWN:
-			if (!(    Town::Get(src)->part_of_subsidy & POS_SRC)) return false;
+			if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
 			break;
 		default: return false;
 	}
@@ -557,7 +557,7 @@
 					TileIndex tile = TileXY(x, y);
 					if (!IsTileType(tile, MP_HOUSE)) continue;
 					const Town *t = Town::GetByTile(tile);
-					if (t->part_of_subsidy & POS_DST) towns_near.Include(t);
+					if (t->cache.part_of_subsidy & POS_DST) towns_near.Include(t);
 				}
 			}
 			break;
@@ -584,7 +584,7 @@
 				case ST_TOWN:
 					for (const Town * const *tp = towns_near.Begin(); tp != towns_near.End(); tp++) {
 						if (s->dst == (*tp)->index) {
-							assert((*tp)->part_of_subsidy & POS_DST);
+							assert((*tp)->cache.part_of_subsidy & POS_DST);
 							subsidised = true;
 							if (!s->IsAwarded()) s->AwardTo(company);
 						}
--- a/src/town.h
+++ b/src/town.h
@@ -40,12 +40,21 @@
 typedef Pool<Town, TownID, 64, 64000> TownPool;
 extern TownPool _town_pool;
 
+/** Data structure with cached data of towns. */
+struct TownCache {
+	uint32 num_houses;                        ///< Amount of houses
+	uint32 population;                        ///< Current population of people
+	ViewportSign sign;                        ///< Location of name sign, UpdateVirtCoord updates this
+	PartOfSubsidyByte part_of_subsidy;        ///< Is this town a source/destination of a subsidy?
+	uint32 squared_town_zone_radius[HZB_END]; ///< UpdateTownRadius updates this given the house count
+	BuildingCounts<uint16> building_counts;   ///< The number of each type of building in the town
+};
+
 /** Town data structure. */
 struct Town : TownPool::PoolItem<&_town_pool> {
 	TileIndex xy;                  ///< town center tile
 
-	uint32 num_houses;             ///< amount of houses
-	uint32 population;             ///< current population of people
+	TownCache cache; ///< Container for all cacheable data.
 
 	/* Town name */
 	uint32 townnamegrfid;
@@ -53,8 +62,6 @@
 	uint32 townnameparts;
 	char *name;
 
-	ViewportSign sign;             ///< NOSAVE: Location of name sign, UpdateVirtCoord updates this
-
 	/* Makes sure we don't build certain house types twice.
 	 * bit 0 = Building funds received
 	 * bit 1 = CHURCH
@@ -98,12 +105,6 @@
 
 	std::list<PersistentStorage *> psa_list;
 
-	PartOfSubsidyByte part_of_subsidy; ///< NOSAVE: is this town a source/destination of a subsidy?
-
-	uint32 squared_town_zone_radius[HZB_END]; ///< NOSAVE: UpdateTownRadius updates this given the house count
-
-	BuildingCounts<uint16> building_counts; ///< NOSAVE: the number of each type of building in the town
-
 	/**
 	 * Creates a new town.
 	 * @param tile center tile of the town
@@ -123,10 +124,10 @@
 	 */
 	inline uint16 MaxTownNoise() const
 	{
-		if (this->population == 0) return 0; // no population? no noise
+		if (this->cache.population == 0) return 0; // no population? no noise
 
 		/* 3 is added (the noise of the lowest airport), so the  user can at least build a small airfield. */
-		return (this->population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
+		return (this->cache.population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
 	}
 
 	void UpdateVirtCoord();
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -355,8 +355,8 @@
 {
 	Point pt = RemapCoords2(TileX(this->xy) * TILE_SIZE, TileY(this->xy) * TILE_SIZE);
 	SetDParam(0, this->index);
-	SetDParam(1, this->population);
-	this->sign.UpdatePosition(pt.x, pt.y - 24 * ZOOM_LVL_BASE,
+	SetDParam(1, this->cache.population);
+	this->cache.sign.UpdatePosition(pt.x, pt.y - 24 * ZOOM_LVL_BASE,
 		_settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN);
 
 	SetWindowDirty(WC_TOWN_VIEW, this->index);
@@ -379,7 +379,7 @@
  */
 static void ChangePopulation(Town *t, int mod)
 {
-	t->population += mod;
+	t->cache.population += mod;
 	InvalidateWindowData(WC_TOWN_VIEW, t->index); // Cargo requirements may appear/vanish for small populations
 	t->UpdateVirtCoord();
 
@@ -396,7 +396,7 @@
 	uint32 pop = 0;
 	const Town *t;
 
-	FOR_ALL_TOWNS(t) pop += t->population;
+	FOR_ALL_TOWNS(t) pop += t->cache.population;
 	return pop;
 }
 
@@ -1295,16 +1295,16 @@
 	 * them a little handicap. */
 	switch (t->layout) {
 		case TL_BETTER_ROADS:
-			_grow_town_result = 10 + t->num_houses * 2 / 9;
+			_grow_town_result = 10 + t->cache.num_houses * 2 / 9;
 			break;
 
 		case TL_3X3_GRID:
 		case TL_2X2_GRID:
-			_grow_town_result = 10 + t->num_houses * 1 / 9;
+			_grow_town_result = 10 + t->cache.num_houses * 1 / 9;
 			break;
 
 		default:
-			_grow_town_result = 10 + t->num_houses * 4 / 9;
+			_grow_town_result = 10 + t->cache.num_houses * 4 / 9;
 			break;
 	}
 
@@ -1453,25 +1453,25 @@
 		{121, 81,  0, 49, 36}, // 88
 	};
 
-	if (t->num_houses < 92) {
-		memcpy(t->squared_town_zone_radius, _town_squared_town_zone_radius_data[t->num_houses / 4], sizeof(t->squared_town_zone_radius));
+	if (t->cache.num_houses < 92) {
+		memcpy(t->cache.squared_town_zone_radius, _town_squared_town_zone_radius_data[t->cache.num_houses / 4], sizeof(t->cache.squared_town_zone_radius));
 	} else {
-		int mass = t->num_houses / 8;
+		int mass = t->cache.num_houses / 8;
 		/* Actually we are proportional to sqrt() but that's right because we are covering an area.
 		 * The offsets are to make sure the radii do not decrease in size when going from the table
 		 * to the calculated value.*/
-		t->squared_town_zone_radius[0] = mass * 15 - 40;
-		t->squared_town_zone_radius[1] = mass * 9 - 15;
-		t->squared_town_zone_radius[2] = 0;
-		t->squared_town_zone_radius[3] = mass * 5 - 5;
-		t->squared_town_zone_radius[4] = mass * 3 + 5;
+		t->cache.squared_town_zone_radius[0] = mass * 15 - 40;
+		t->cache.squared_town_zone_radius[1] = mass * 9 - 15;
+		t->cache.squared_town_zone_radius[2] = 0;
+		t->cache.squared_town_zone_radius[3] = mass * 5 - 5;
+		t->cache.squared_town_zone_radius[4] = mass * 3 + 5;
 	}
 }
 
 void UpdateTownMaxPass(Town *t)
 {
-	t->supplied[CT_PASSENGERS].old_max = t->population >> 3;
-	t->supplied[CT_MAIL].old_max = t->population >> 4;
+	t->supplied[CT_PASSENGERS].old_max = t->cache.population >> 3;
+	t->supplied[CT_MAIL].old_max = t->cache.population >> 4;
 }
 
 /**
@@ -1488,11 +1488,11 @@
 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize size, bool city, TownLayout layout, bool manual)
 {
 	t->xy = tile;
-	t->num_houses = 0;
+	t->cache.num_houses = 0;
 	t->time_until_rebuild = 10;
 	UpdateTownRadius(t);
 	t->flags = 0;
-	t->population = 0;
+	t->cache.population = 0;
 	t->grow_counter = 0;
 	t->growth_rate = 250;
 
@@ -1541,7 +1541,7 @@
 	/* Don't create huge cities when founding town in-game */
 	if (city && (!manual || _game_mode == GM_EDITOR)) x *= _settings_game.economy.initial_city_size;
 
-	t->num_houses += x;
+	t->cache.num_houses += x;
 	UpdateTownRadius(t);
 
 	int i = x * 4;
@@ -1549,7 +1549,7 @@
 		GrowTown(t);
 	} while (--i);
 
-	t->num_houses -= x;
+	t->cache.num_houses -= x;
 	UpdateTownRadius(t);
 	UpdateTownMaxPass(t);
 	UpdateAirportsNoise();
@@ -1845,7 +1845,7 @@
 
 		/* if the population is still 0 at the point, then the
 		 * placement is so bad it couldn't grow at all */
-		if (t->population > 0) return t;
+		if (t->cache.population > 0) return t;
 		CommandCost rc = DoCommand(t->xy, t->index, 0, DC_EXEC, CMD_DELETE_TOWN);
 		assert(rc.Succeeded());
 
@@ -1921,7 +1921,7 @@
 
 	HouseZonesBits smallest = HZB_TOWN_EDGE;
 	for (HouseZonesBits i = HZB_BEGIN; i < HZB_END; i++) {
-		if (dist < t->squared_town_zone_radius[i]) smallest = i;
+		if (dist < t->cache.squared_town_zone_radius[i]) smallest = i;
 	}
 
 	return smallest;
@@ -2202,10 +2202,10 @@
 		/* Don't let these counters overflow. Global counters are 32bit, there will never be that many houses. */
 		if (hs->class_id != HOUSE_NO_CLASS) {
 			/* id_count is always <= class_count, so it doesn't need to be checked */
-			if (t->building_counts.class_count[hs->class_id] == UINT16_MAX) continue;
+			if (t->cache.building_counts.class_count[hs->class_id] == UINT16_MAX) continue;
 		} else {
 			/* If the house has no class, check id_count instead */
-			if (t->building_counts.id_count[i] == UINT16_MAX) continue;
+			if (t->cache.building_counts.id_count[i] == UINT16_MAX) continue;
 		}
 
 		/* Without NewHouses, all houses have probability '1' */
@@ -2282,7 +2282,7 @@
 		}
 
 		/* build the house */
-		t->num_houses++;
+		t->cache.num_houses++;
 
 		/* Special houses that there can be only one of. */
 		t->flags |= oneof;
@@ -2371,7 +2371,7 @@
 		ChangePopulation(t, -hs->population);
 	}
 
-	t->num_houses--;
+	t->cache.num_houses--;
 
 	/* Clear flags for houses that only may exist once/town. */
 	if (hs->building_flags & BUILDING_IS_CHURCH) {
@@ -2543,14 +2543,14 @@
 	if (flags & DC_EXEC) {
 		/* The more houses, the faster we grow */
 		if (p2 == 0) {
-			uint amount = RandomRange(ClampToU16(t->num_houses / 10)) + 3;
-			t->num_houses += amount;
+			uint amount = RandomRange(ClampToU16(t->cache.num_houses / 10)) + 3;
+			t->cache.num_houses += amount;
 			UpdateTownRadius(t);
 
 			uint n = amount * 10;
 			do GrowTown(t); while (--n);
 
-			t->num_houses -= amount;
+			t->cache.num_houses -= amount;
 		} else {
 			for (; p2 > 0; p2--) {
 				/* Try several times to grow, as we are really suppose to grow */
@@ -2962,7 +2962,7 @@
 
 	const Station *st;
 	FOR_ALL_STATIONS(st) {
-		if (DistanceSquare(st->xy, t->xy) <= t->squared_town_zone_radius[0]) {
+		if (DistanceSquare(st->xy, t->xy) <= t->cache.squared_town_zone_radius[0]) {
 			if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
 				if (Company::IsValidID(st->owner)) {
 					int new_rating = t->ratings[st->owner] + RATING_STATION_UP_STEP;
@@ -2997,10 +2997,10 @@
 		for (int i = TE_BEGIN; i < TE_END; i++) {
 			switch (t->goal[i]) {
 				case TOWN_GROWTH_WINTER:
-					if (TileHeight(t->xy) >= GetSnowLine() && t->received[i].old_act == 0 && t->population > 90) return;
+					if (TileHeight(t->xy) >= GetSnowLine() && t->received[i].old_act == 0 && t->cache.population > 90) return;
 					break;
 				case TOWN_GROWTH_DESERT:
-					if (GetTropicZone(t->xy) == TROPICZONE_DESERT && t->received[i].old_act == 0 && t->population > 60) return;
+					if (GetTropicZone(t->xy) == TROPICZONE_DESERT && t->received[i].old_act == 0 && t->cache.population > 60) return;
 					break;
 				default:
 					if (t->goal[i] > t->received[i].old_act) return;
@@ -3028,7 +3028,7 @@
 
 	const Station *st;
 	FOR_ALL_STATIONS(st) {
-		if (DistanceSquare(st->xy, t->xy) <= t->squared_town_zone_radius[0]) {
+		if (DistanceSquare(st->xy, t->xy) <= t->cache.squared_town_zone_radius[0]) {
 			if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
 				n++;
 			}
@@ -3051,7 +3051,7 @@
 	m >>= growth_multiplier;
 	if (t->larger_town) m /= 2;
 
-	t->growth_rate = m / (t->num_houses / 50 + 1);
+	t->growth_rate = m / (t->cache.num_houses / 50 + 1);
 	if (m <= t->grow_counter) {
 		t->grow_counter = m;
 	}
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -328,8 +328,8 @@
 
 		uint y = r.top + WD_FRAMERECT_TOP;
 
-		SetDParam(0, this->town->population);
-		SetDParam(1, this->town->num_houses);
+		SetDParam(0, this->town->cache.population);
+		SetDParam(1, this->town->cache.num_houses);
 		DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
 
 		SetDParam(0, this->town->supplied[CT_PASSENGERS].old_act);
@@ -343,8 +343,8 @@
 		bool first = true;
 		for (int i = TE_BEGIN; i < TE_END; i++) {
 			if (this->town->goal[i] == 0) continue;
-			if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->population <= 90)) continue;
-			if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->population <= 60)) continue;
+			if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
+			if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
 
 			if (first) {
 				DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
@@ -465,8 +465,8 @@
 		bool first = true;
 		for (int i = TE_BEGIN; i < TE_END; i++) {
 			if (this->town->goal[i] == 0) continue;
-			if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->population <= 90)) continue;
-			if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->population <= 60)) continue;
+			if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
+			if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
 
 			if (first) {
 				aimed_height += FONT_HEIGHT_NORMAL;
@@ -681,7 +681,7 @@
 	/** Sort by population */
 	static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
 	{
-		return (*a)->population - (*b)->population;
+		return (*a)->cache.population - (*b)->cache.population;
 	}
 
 public:
@@ -750,7 +750,7 @@
 					}
 
 					SetDParam(0, t->index);
-					SetDParam(1, t->population);
+					SetDParam(1, t->cache.population);
 					DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, STR_TOWN_DIRECTORY_TOWN);
 
 					y += this->resize.step_height;
--- a/src/tunnelbridge_cmd.cpp
+++ b/src/tunnelbridge_cmd.cpp
@@ -251,7 +251,7 @@
 		company = OWNER_TOWN;
 
 		/* If we are not within a town, we are not owned by the town */
-		if (town == NULL || DistanceSquare(tile_start, town->xy) > town->squared_town_zone_radius[HZB_TOWN_EDGE]) {
+		if (town == NULL || DistanceSquare(tile_start, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
 			company = OWNER_NONE;
 		}
 	}
@@ -560,7 +560,7 @@
 		company = OWNER_TOWN;
 
 		/* If we are not within a town, we are not owned by the town */
-		if (town == NULL || DistanceSquare(start_tile, town->xy) > town->squared_town_zone_radius[HZB_TOWN_EDGE]) {
+		if (town == NULL || DistanceSquare(start_tile, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
 			company = OWNER_NONE;
 		}
 	}
--- a/src/viewport.cpp
+++ b/src/viewport.cpp
@@ -1171,10 +1171,10 @@
 
 	const Town *t;
 	FOR_ALL_TOWNS(t) {
-		ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &t->sign,
+		ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &t->cache.sign,
 				_settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
 				STR_VIEWPORT_TOWN_TINY_WHITE, STR_VIEWPORT_TOWN_TINY_BLACK,
-				t->index, t->population);
+				t->index, t->cache.population);
 	}
 }
 
@@ -1865,7 +1865,7 @@
 
 	const Town *t;
 	FOR_ALL_TOWNS(t) {
-		if (CheckClickOnViewportSign(vp, x, y, &t->sign)) {
+		if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) {
 			ShowTownViewWindow(t->index);
 			return true;
 		}