changeset 16788:efb0333e5aad draft

(svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
author terkhen <terkhen@openttd.org>
date Tue, 14 Dec 2010 21:33:53 +0000 (2010-12-14)
parents 530915d6b2c5
children 4c560d64c989
files src/ai/api/ai_vehicle.cpp src/articulated_vehicles.cpp src/autoreplace_cmd.cpp src/depot_gui.cpp src/economy.cpp src/ground_vehicle.hpp src/newgrf_engine.cpp src/openttd.cpp src/pathfinder/yapf/yapf_costrail.hpp src/rail_cmd.cpp src/roadstop.cpp src/roadveh.h src/roadveh_cmd.cpp src/saveload/vehicle_sl.cpp src/train.h src/train_cmd.cpp src/vehicle.cpp src/vehicle_gui.cpp
diffstat 18 files changed, 70 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/api/ai_vehicle.cpp
+++ b/src/ai/api/ai_vehicle.cpp
@@ -48,11 +48,7 @@
 	if (!IsValidVehicle(vehicle_id)) return -1;
 
 	const Vehicle *v = ::Vehicle::Get(vehicle_id);
-	switch (v->type) {
-		case VEH_ROAD:  return ::RoadVehicle::From(v)->rcache.cached_total_length;
-		case VEH_TRAIN: return ::Train::From(v)->tcache.cached_total_length;
-		default: return -1;
-	}
+	return v->IsGroundVehicle() ? v->GetGroundVehicleCache()->cached_total_length : -1;
 }
 
 /* static */ VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id)
--- a/src/articulated_vehicles.cpp
+++ b/src/articulated_vehicles.cpp
@@ -313,6 +313,9 @@
 		 * and we run out of available vehicles, bail out. */
 		if (!Vehicle::CanAllocateItem()) return;
 
+		GroundVehicleCache *gcache = v->GetGroundVehicleCache();
+		gcache->first_engine = v->engine_type; // Needs to be set before first callback
+
 		const Engine *e_artic = Engine::Get(engine_type);
 		switch (type) {
 			default: NOT_REACHED();
@@ -326,7 +329,6 @@
 				t->subtype = 0;
 				t->track = front->track;
 				t->railtype = front->railtype;
-				t->tcache.first_engine = front->engine_type; // needs to be set before first callback
 
 				t->spritenum = e_artic->u.rail.image_index;
 				if (e_artic->CanCarryCargo()) {
@@ -348,8 +350,7 @@
 				v = rv;
 
 				rv->subtype = 0;
-				rv->rcache.first_engine = front->engine_type; // needs to be set before first callback
-				rv->rcache.cached_veh_length = 8; // Callback is called when the consist is finished
+				gcache->cached_veh_length = 8; // Callback is called when the consist is finished
 				rv->state = RVSB_IN_DEPOT;
 
 				rv->roadtype = front->roadtype;
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -421,7 +421,7 @@
 
 	if (old_head->type == VEH_TRAIN) {
 		/* Store the length of the old vehicle chain, rounded up to whole tiles */
-		uint16 old_total_length = CeilDiv(Train::From(old_head)->tcache.cached_total_length, TILE_SIZE) * TILE_SIZE;
+		uint16 old_total_length = CeilDiv(Train::From(old_head)->gcache.cached_total_length, TILE_SIZE) * TILE_SIZE;
 
 		int num_units = 0; ///< Number of units in the chain
 		for (Train *w = Train::From(old_head); w != NULL; w = w->GetNextUnit()) num_units++;
@@ -481,7 +481,7 @@
 			}
 
 			/* When wagon removal is enabled and the new engines without any wagons are already longer than the old, we have to fail */
-			if (cost.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
+			if (cost.Succeeded() && wagon_removal && new_head->gcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
 
 			/* Append/insert wagons into the new vehicle chain
 			 * We do this from back to front, so we can stop when wagon removal or maximum train length (i.e. from mammoth-train setting) is triggered.
@@ -495,7 +495,7 @@
 						/* Insert wagon after 'last_engine' */
 						CommandCost res = MoveVehicle(append, last_engine, DC_EXEC, false);
 
-						if (res.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) {
+						if (res.Succeeded() && wagon_removal && new_head->gcache.cached_total_length > old_total_length) {
 							MoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false);
 							break;
 						}
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -287,7 +287,7 @@
 						this->sel, free_wagon ? 0 : this->hscroll->GetPosition(), this->vehicle_over);
 
 				/* Number of wagons relative to a standard length wagon (rounded up) */
-				SetDParam(0, CeilDiv(u->tcache.cached_total_length, 8));
+				SetDParam(0, CeilDiv(u->gcache.cached_total_length, 8));
 				DrawString(rtl ? left + WD_FRAMERECT_LEFT : right - this->count_width, rtl ? left + this->count_width : right - WD_FRAMERECT_RIGHT, y + (this->resize.step_height - FONT_HEIGHT_SMALL) / 2, STR_TINY_BLACK_COMA, TC_FROMSTRING, SA_RIGHT); // Draw the counter
 				break;
 			}
@@ -507,19 +507,7 @@
 					this->sel = v->index;
 					this->SetDirty();
 
-					switch (v->type) {
-						case VEH_TRAIN:
-							_cursor.short_vehicle_offset = 16 - Train::From(v)->tcache.cached_veh_length * 2;
-							break;
-
-						case VEH_ROAD:
-							_cursor.short_vehicle_offset = 16 - RoadVehicle::From(v)->rcache.cached_veh_length * 2;
-							break;
-
-						default:
-							_cursor.short_vehicle_offset = 0;
-							break;
-					}
+					_cursor.short_vehicle_offset = v->IsGroundVehicle() ? 16 - v->GetGroundVehicleCache()->cached_veh_length * 2 : 0;
 					_cursor.vehchain = _ctrl_pressed;
 				}
 				break;
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1370,7 +1370,7 @@
 
 	if (v->type == VEH_TRAIN) {
 		/* Each platform tile is worth 2 rail vehicles. */
-		int overhang = Train::From(v)->tcache.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
+		int overhang = v->GetGroundVehicleCache()->cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
 		if (overhang > 0) {
 			unloading_time <<= 1;
 			unloading_time += (overhang * unloading_time) / 8;
--- a/src/ground_vehicle.hpp
+++ b/src/ground_vehicle.hpp
@@ -36,6 +36,11 @@
 	uint16 cached_max_track_speed;  ///< Maximum consist speed limited by track type (valid only for the first engine).
 	uint32 cached_power;            ///< Total power of the consist (valid only for the first engine).
 	uint32 cached_air_drag;         ///< Air drag coefficient of the vehicle (valid only for the first engine).
+
+	/* Cached NewGRF values, recalculated on load and each time a vehicle is added to/removed from the consist. */
+	uint16 cached_total_length;     ///< Length of the whole vehicle (valid only for the first engine).
+	EngineID first_engine;          ///< Cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
+	uint8 cached_veh_length;        ///< Length of this vehicle in units of 1/8 of normal length. It is cached because this can be set by a callback.
 };
 
 /** Ground vehicle flags. */
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -439,10 +439,8 @@
 	if (v == NULL) {
 		if (!Company::IsValidID(_current_company)) return 0;
 		l = GetEngineLivery(engine, _current_company, INVALID_ENGINE, NULL, LIT_ALL);
-	} else if (v->type == VEH_TRAIN) {
-		l = GetEngineLivery(v->engine_type, v->owner, Train::From(v)->tcache.first_engine, v, LIT_ALL);
-	} else if (v->type == VEH_ROAD) {
-		l = GetEngineLivery(v->engine_type, v->owner, RoadVehicle::From(v)->rcache.first_engine, v, LIT_ALL);
+	} else if (v->IsGroundVehicle()) {
+		l = GetEngineLivery(v->engine_type, v->owner, v->GetGroundVehicleCache()->first_engine, v, LIT_ALL);
 	} else {
 		l = GetEngineLivery(v->engine_type, v->owner, INVALID_ENGINE, v, LIT_ALL);
 	}
@@ -794,7 +792,7 @@
 			switch (variable - 0x80) {
 				case 0x62: return t->track;
 				case 0x66: return t->railtype;
-				case 0x73: return t->tcache.cached_veh_length;
+				case 0x73: return t->gcache.cached_veh_length;
 				case 0x74: return t->gcache.cached_power;
 				case 0x75: return GB(t->gcache.cached_power,  8, 24);
 				case 0x76: return GB(t->gcache.cached_power, 16, 16);
@@ -909,14 +907,15 @@
 	} else {
 		cargo = v->cargo_type;
 
-		if (v->type == VEH_TRAIN) {
-			/* We always use cached value, except for callbacks because the override spriteset
+		if (v->IsGroundVehicle()) {
+			/* For trains we always use cached value, except for callbacks because the override spriteset
 			 * to use may be different than the one cached. It happens for callback 0x15 (refit engine),
 			 * as v->cargo_type is temporary changed to the new type */
-			group = use_cache ? Train::From(v)->tcache.cached_override : GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, Train::From(v)->tcache.first_engine);
-			if (group != NULL) return group;
-		} else if (v->type == VEH_ROAD) {
-			group = GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, RoadVehicle::From(v)->rcache.first_engine);
+			if (use_cache && v->type == VEH_TRAIN) {
+				group = Train::From(v)->tcache.cached_override;
+			} else {
+				group = GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, v->GetGroundVehicleCache()->first_engine);
+			}
 			if (group != NULL) return group;
 		}
 	}
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -1145,7 +1145,6 @@
 		VehicleCache       *veh_cache = CallocT<VehicleCache>(length);
 		GroundVehicleCache *gro_cache = CallocT<GroundVehicleCache>(length);
 		TrainCache         *tra_cache = CallocT<TrainCache>(length);
-		RoadVehicleCache   *roa_cache = CallocT<RoadVehicleCache>(length);
 
 		length = 0;
 		for (const Vehicle *u = v; u != NULL; u = u->Next()) {
@@ -1159,7 +1158,6 @@
 					break;
 				case VEH_ROAD:
 					gro_cache[length] = RoadVehicle::From(u)->gcache;
-					roa_cache[length] = RoadVehicle::From(u)->rcache;
 					break;
 				default:
 					break;
@@ -1197,9 +1195,6 @@
 					if (memcmp(&gro_cache[length], &RoadVehicle::From(u)->gcache, sizeof(GroundVehicleCache)) != 0) {
 						DEBUG(desync, 2, "road vehicle ground vehicle cache mismatch: vehicle %i, company %i, unit number %i, wagon %i", v->index, (int)v->owner, v->unitnumber, length);
 					}
-					if (memcmp(&roa_cache[length], &RoadVehicle::From(u)->rcache, sizeof(RoadVehicleCache)) != 0) {
-						DEBUG(desync, 2, "road vehicle cache mismatch: vehicle %i, company %i, unit number %i, wagon %i", v->index, (int)v->owner, v->unitnumber, length);
-					}
 					break;
 				default:
 					break;
@@ -1211,7 +1206,6 @@
 		free(veh_cache);
 		free(gro_cache);
 		free(tra_cache);
-		free(roa_cache);
 	}
 
 	/* Check whether the caches are still valid */
--- a/src/pathfinder/yapf/yapf_costrail.hpp
+++ b/src/pathfinder/yapf/yapf_costrail.hpp
@@ -258,8 +258,8 @@
 		const Train *v = Yapf().GetVehicle();
 		assert(v != NULL);
 		assert(v->type == VEH_TRAIN);
-		assert(v->tcache.cached_total_length != 0);
-		int missing_platform_length = CeilDiv(v->tcache.cached_total_length, TILE_SIZE) - platform_length;
+		assert(v->gcache.cached_total_length != 0);
+		int missing_platform_length = CeilDiv(v->gcache.cached_total_length, TILE_SIZE) - platform_length;
 		if (missing_platform_length < 0) {
 			/* apply penalty for longer platform than needed */
 			cost += Yapf().PfGetSettings().rail_longer_platform_penalty + Yapf().PfGetSettings().rail_longer_platform_per_tile_penalty * -missing_platform_length;
--- a/src/rail_cmd.cpp
+++ b/src/rail_cmd.cpp
@@ -2698,7 +2698,7 @@
 int TicksToLeaveDepot(const Train *v)
 {
 	DiagDirection dir = GetRailDepotDirection(v->tile);
-	int length = v->tcache.cached_veh_length;
+	int length = v->gcache.cached_veh_length;
 
 	switch (dir) {
 		case DIAGDIR_NE: return  ((int)(v->x_pos & 0x0F) - ((_fractcoords_enter[dir] & 0x0F) - (length + 1)));
@@ -2727,7 +2727,7 @@
 
 	/* calculate the point where the following wagon should be activated
 	 * this depends on the length of the current vehicle */
-	int length = v->tcache.cached_veh_length;
+	int length = v->gcache.cached_veh_length;
 
 	byte fract_coord_leave =
 		((_fractcoords_enter[dir] & 0x0F) + // x
--- a/src/roadstop.cpp
+++ b/src/roadstop.cpp
@@ -278,7 +278,7 @@
  */
 void RoadStop::Entry::Leave(const RoadVehicle *rv)
 {
-	this->occupied -= rv->rcache.cached_total_length;
+	this->occupied -= rv->gcache.cached_total_length;
 	assert(this->occupied >= 0);
 }
 
@@ -291,7 +291,7 @@
 	/* we cannot assert on this->occupied < this->length because of the
 	 * remote possibility that RVs are running through eachother when
 	 * trying to prevention an infinite jam. */
-	this->occupied += rv->rcache.cached_total_length;
+	this->occupied += rv->gcache.cached_total_length;
 }
 
 /**
@@ -367,7 +367,7 @@
 
 	this->occupied = 0;
 	for (RVList::iterator it = rserh.vehicles.begin(); it != rserh.vehicles.end(); it++) {
-		this->occupied += (*it)->rcache.cached_total_length;
+		this->occupied += (*it)->gcache.cached_total_length;
 	}
 }
 
--- a/src/roadveh.h
+++ b/src/roadveh.h
@@ -83,18 +83,10 @@
 
 void RoadVehUpdateCache(RoadVehicle *v);
 
-/** Cached oftenly queried (NewGRF) values */
-struct RoadVehicleCache {
-	uint16 cached_total_length; ///< Length of the whole train, valid only for first engine.
-	byte cached_veh_length;     ///< length of this vehicle in units of 1/8 of normal length, cached because this can be set by a callback
-	EngineID first_engine;      ///< Cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
-};
-
 /**
  * Buses, trucks and trams belong to this class.
  */
 struct RoadVehicle : public GroundVehicle<RoadVehicle, VEH_ROAD> {
-	RoadVehicleCache rcache; ///< Cache of often used calculated values
 	byte state;             ///< @see RoadVehicleStates
 	byte frame;
 	uint16 blocked_ctr;
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -102,7 +102,7 @@
 		offset->x = reference_width / 2;
 		offset->y = 0;
 	}
-	return this->rcache.cached_veh_length * reference_width / 8;
+	return this->gcache.cached_veh_length * reference_width / 8;
 }
 
 static SpriteID GetRoadVehIcon(EngineID engine)
@@ -175,18 +175,18 @@
 
 	v->InvalidateNewGRFCacheOfChain();
 
-	v->rcache.cached_total_length = 0;
+	v->gcache.cached_total_length = 0;
 
 	for (RoadVehicle *u = v; u != NULL; u = u->Next()) {
 		/* Check the v->first cache. */
 		assert(u->First() == v);
 
 		/* Update the 'first engine' */
-		u->rcache.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
+		u->gcache.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
 
 		/* Update the length of the vehicle. */
-		u->rcache.cached_veh_length = GetRoadVehLength(u);
-		v->rcache.cached_total_length += u->rcache.cached_veh_length;
+		u->gcache.cached_veh_length = GetRoadVehLength(u);
+		v->gcache.cached_total_length += u->gcache.cached_veh_length;
 
 		/* Update visual effect */
 		v->UpdateVisualEffect();
@@ -236,7 +236,7 @@
 
 		v->last_station_visited = INVALID_STATION;
 		v->engine_type = e->index;
-		v->rcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
+		v->gcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
 
 		v->reliability = e->reliability;
 		v->reliability_spd_dec = e->reliability_spd_dec;
@@ -254,7 +254,7 @@
 
 		v->roadtype = HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
 		v->compatible_roadtypes = RoadTypeToRoadTypes(v->roadtype);
-		v->rcache.cached_veh_length = 8;
+		v->gcache.cached_veh_length = 8;
 
 		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
 
@@ -1317,7 +1317,7 @@
 	 * it's on a depot tile, check if it's time to activate the next vehicle in
 	 * the chain yet. */
 	if (v->Next() != NULL && IsRoadDepotTile(v->tile)) {
-		if (v->frame == v->rcache.cached_veh_length + RVC_DEPOT_START_FRAME) {
+		if (v->frame == v->gcache.cached_veh_length + RVC_DEPOT_START_FRAME) {
 			RoadVehLeaveDepot(v->Next(), false);
 		}
 	}
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -251,8 +251,7 @@
 
 		if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
 		v->first = NULL;
-		if (v->type == VEH_TRAIN) Train::From(v)->tcache.first_engine = INVALID_ENGINE;
-		if (v->type == VEH_ROAD)  RoadVehicle::From(v)->rcache.first_engine = INVALID_ENGINE;
+		if (v->IsGroundVehicle()) v->GetGroundVehicleCache()->first_engine = INVALID_ENGINE;
 	}
 
 	/* AfterLoadVehicles may also be called in case of NewGRF reload, in this
--- a/src/train.h
+++ b/src/train.h
@@ -68,16 +68,12 @@
 	uint16 last_speed; // NOSAVE: only used in UI
 
 	/* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
-	uint16 cached_total_length; ///< Length of the whole train, valid only for first engine.
-	uint8 cached_veh_length;    ///< length of this vehicle in units of 1/8 of normal length, cached because this can be set by a callback
 	bool cached_tilt;           ///< train can tilt; feature provides a bonus in curves
 
+	byte user_def_data;         ///< Cached property 0x25. Can be set by Callback 0x36.
+
 	/* cached max. speed / acceleration data */
 	int cached_max_curve_speed; ///< max consist speed limited by curves
-
-	byte user_def_data;
-
-	EngineID first_engine;  ///< cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
 };
 
 /**
@@ -387,7 +383,7 @@
 	{
 		/* For powered wagons the engine defines the type of engine (i.e. railtype) */
 		if (HasBit(this->flags, VRF_POWEREDWAGON) && HasPowerOnRail(head->railtype, GetRailType(this->tile))) {
-			return RailVehInfo(this->tcache.first_engine)->pow_wag_power;
+			return RailVehInfo(this->gcache.first_engine)->pow_wag_power;
 		}
 
 		return 0;
@@ -408,7 +404,7 @@
 
 		/* Powered wagons have extra weight added. */
 		if (HasBit(this->flags, VRF_POWEREDWAGON)) {
-			weight += RailVehInfo(this->tcache.first_engine)->pow_wag_weight;
+			weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
 		}
 
 		return weight;
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -110,7 +110,7 @@
 			for (const Train *u = v, *w = v->Next(); w != NULL; u = w, w = w->Next()) {
 				if (u->track != TRACK_BIT_DEPOT) {
 					if ((w->track != TRACK_BIT_DEPOT &&
-							max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->tcache.cached_veh_length) ||
+							max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->gcache.cached_veh_length) ||
 							(w->track == TRACK_BIT_DEPOT && TicksToLeaveDepot(u) <= 0)) {
 						SetDParam(0, v->index);
 						SetDParam(1, v->owner);
@@ -152,7 +152,7 @@
 
 	const RailVehicleInfo *rvi_v = RailVehInfo(this->engine_type);
 	EngineID first_engine = this->IsFrontEngine() ? this->engine_type : INVALID_ENGINE;
-	this->tcache.cached_total_length = 0;
+	this->gcache.cached_total_length = 0;
 	this->compatible_railtypes = RAILTYPES_NONE;
 
 	bool train_can_tilt = true;
@@ -164,7 +164,7 @@
 		assert(u->First() == this);
 
 		/* update the 'first engine' */
-		u->tcache.first_engine = this == u ? INVALID_ENGINE : first_engine;
+		u->gcache.first_engine = this == u ? INVALID_ENGINE : first_engine;
 		u->railtype = rvi_u->railtype;
 
 		if (u->IsEngine()) first_engine = u->engine_type;
@@ -189,7 +189,7 @@
 		if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
 
 		/* Cache wagon override sprite group. NULL is returned if there is none */
-		u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->tcache.first_engine);
+		u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->gcache.first_engine);
 
 		/* Reset colour map */
 		u->colourmap = PAL_NONE;
@@ -237,12 +237,12 @@
 		veh_len = 8 - Clamp(veh_len, 0, 7);
 
 		/* verify length hasn't changed */
-		if (same_length && veh_len != u->tcache.cached_veh_length) RailVehicleLengthChanged(u);
+		if (same_length && veh_len != u->gcache.cached_veh_length) RailVehicleLengthChanged(u);
 
 		/* update vehicle length? */
-		if (!same_length) u->tcache.cached_veh_length = veh_len;
-
-		this->tcache.cached_total_length += u->tcache.cached_veh_length;
+		if (!same_length) u->gcache.cached_veh_length = veh_len;
+
+		this->gcache.cached_total_length += u->gcache.cached_veh_length;
 		this->InvalidateNewGRFCache();
 		u->InvalidateNewGRFCache();
 	}
@@ -281,7 +281,7 @@
 	/* Default to the middle of the station for stations stops that are not in
 	 * the order list like intermediate stations when non-stop is disabled */
 	OrderStopLocation osl = OSL_PLATFORM_MIDDLE;
-	if (v->tcache.cached_total_length >= *station_length) {
+	if (v->gcache.cached_total_length >= *station_length) {
 		/* The train is longer than the station, make it stop at the far end of the platform */
 		osl = OSL_PLATFORM_FAR_END;
 	} else if (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == station_id) {
@@ -294,11 +294,11 @@
 		default: NOT_REACHED();
 
 		case OSL_PLATFORM_NEAR_END:
-			stop = v->tcache.cached_total_length;
+			stop = v->gcache.cached_total_length;
 			break;
 
 		case OSL_PLATFORM_MIDDLE:
-			stop = *station_length - (*station_length - v->tcache.cached_total_length) / 2;
+			stop = *station_length - (*station_length - v->gcache.cached_total_length) / 2;
 			break;
 
 		case OSL_PLATFORM_FAR_END:
@@ -308,7 +308,7 @@
 
 	/* Subtract half the front vehicle length of the train so we get the real
 	 * stop location of the train. */
-	return stop - (v->tcache.cached_veh_length + 1) / 2;
+	return stop - (v->gcache.cached_veh_length + 1) / 2;
 }
 
 
@@ -455,7 +455,7 @@
 		offset->x = reference_width / 2;
 		offset->y = vehicle_pitch;
 	}
-	return this->tcache.cached_veh_length * reference_width / 8;
+	return this->gcache.cached_veh_length * reference_width / 8;
 }
 
 static SpriteID GetDefaultTrainSprite(uint8 spritenum, Direction direction)
@@ -551,7 +551,7 @@
 		v->spritenum = rvi->image_index;
 
 		v->engine_type = e->index;
-		v->tcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
+		v->gcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
 
 		DiagDirection dir = GetRailDepotDirection(tile);
 
@@ -697,7 +697,7 @@
 		v->last_station_visited = INVALID_STATION;
 
 		v->engine_type = e->index;
-		v->tcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
+		v->gcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
 
 		v->reliability = e->reliability;
 		v->reliability_spd_dec = e->reliability_spd_dec;
@@ -963,8 +963,8 @@
 			allowed_len--; // We do not count articulated parts and rear heads either.
 
 			/* Back up and clear the first_engine data to avoid using wagon override group */
-			EngineID first_engine = t->tcache.first_engine;
-			t->tcache.first_engine = INVALID_ENGINE;
+			EngineID first_engine = t->gcache.first_engine;
+			t->gcache.first_engine = INVALID_ENGINE;
 
 			/* We don't want the cache to interfere. head's cache is cleared before
 			 * the loop and after each callback does not need to be cleared here. */
@@ -973,7 +973,7 @@
 			uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, head->engine_type, t, head);
 
 			/* Restore original first_engine data */
-			t->tcache.first_engine = first_engine;
+			t->gcache.first_engine = first_engine;
 
 			/* We do not want to remember any cached variables from the test run */
 			t->InvalidateNewGRFCache();
@@ -1599,7 +1599,7 @@
 		last = last->Previous();
 		first = first->Next();
 
-		int differential = base->tcache.cached_veh_length - last->tcache.cached_veh_length;
+		int differential = base->gcache.cached_veh_length - last->gcache.cached_veh_length;
 
 		/* do not update images now
 		 * negative differential will be handled in AdvanceWagonsAfterSwap() */
@@ -1659,7 +1659,7 @@
 		last = last->Previous();
 		first = first->Next();
 
-		int differential = last->tcache.cached_veh_length - base->tcache.cached_veh_length;
+		int differential = last->gcache.cached_veh_length - base->gcache.cached_veh_length;
 
 		/* do not update images now */
 		for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->Next() : NULL));
@@ -3387,7 +3387,7 @@
 	}
 
 	/* do not reverse when approaching red signal */
-	if (!signal && x + (v->tcache.cached_veh_length + 1) / 2 >= TILE_SIZE) {
+	if (!signal && x + (v->gcache.cached_veh_length + 1) / 2 >= TILE_SIZE) {
 		/* we are too near the tile end, reverse now */
 		v->cur_speed = 0;
 		ReverseTrainDirection(v);
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -1665,10 +1665,8 @@
 
 PaletteID GetVehiclePalette(const Vehicle *v)
 {
-	if (v->type == VEH_TRAIN) {
-		return GetEngineColourMap(v->engine_type, v->owner, Train::From(v)->tcache.first_engine, v);
-	} else if (v->type == VEH_ROAD) {
-		return GetEngineColourMap(v->engine_type, v->owner, RoadVehicle::From(v)->rcache.first_engine, v);
+	if (v->IsGroundVehicle()) {
+		return GetEngineColourMap(v->engine_type, v->owner, v->GetGroundVehicleCache()->first_engine, v);
 	}
 
 	return GetEngineColourMap(v->engine_type, v->owner, INVALID_ENGINE, v);
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -847,19 +847,7 @@
 /** Sort vehicles by their length */
 static int CDECL VehicleLengthSorter(const Vehicle * const *a, const Vehicle * const *b)
 {
-	int r = 0;
-	switch ((*a)->type) {
-		case VEH_TRAIN:
-			r = Train::From(*a)->tcache.cached_total_length - Train::From(*b)->tcache.cached_total_length;
-			break;
-
-		case VEH_ROAD: {
-			r = RoadVehicle::From(*a)->rcache.cached_total_length - RoadVehicle::From(*b)->rcache.cached_total_length;
-			break;
-		}
-
-		default: NOT_REACHED();
-	}
+	int r = (*a)->GetGroundVehicleCache()->cached_total_length - (*b)->GetGroundVehicleCache()->cached_total_length;
 	return (r != 0) ? r : VehicleNumberSorter(a, b);
 }