diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -512,21 +512,21 @@ static uint32 VehicleGetVariable(const R return GetEngineGRFID(v->engine_type); case 0x40: // Get length of consist - if (!HasBit(v->vcache.cache_valid, 0)) { - v->vcache.cached_var40 = PositionHelper(v, false); - SetBit(v->vcache.cache_valid, 0); + if (!HasBit(v->grf_cache.cache_valid, 0)) { + v->grf_cache.position_consist_length = PositionHelper(v, false); + SetBit(v->grf_cache.cache_valid, 0); } - return v->vcache.cached_var40; + return v->grf_cache.position_consist_length; case 0x41: // Get length of same consecutive wagons - if (!HasBit(v->vcache.cache_valid, 1)) { - v->vcache.cached_var41 = PositionHelper(v, true); - SetBit(v->vcache.cache_valid, 1); + if (!HasBit(v->grf_cache.cache_valid, 1)) { + v->grf_cache.position_same_id_length = PositionHelper(v, true); + SetBit(v->grf_cache.cache_valid, 1); } - return v->vcache.cached_var41; + return v->grf_cache.position_same_id_length; case 0x42: // Consist cargo information - if (!HasBit(v->vcache.cache_valid, 2)) { + if (!HasBit(v->grf_cache.cache_valid, 2)) { const Vehicle *u; byte cargo_classes = 0; uint8 common_cargos[NUM_CARGO]; @@ -576,17 +576,17 @@ static uint32 VehicleGetVariable(const R } uint8 common_bitnum = (common_cargo_type == CT_INVALID ? 0xFF : CargoSpec::Get(common_cargo_type)->bitnum); - v->vcache.cached_var42 = cargo_classes | (common_bitnum << 8) | (common_subtype << 16) | (user_def_data << 24); - SetBit(v->vcache.cache_valid, 2); + v->grf_cache.consist_cargo_information = cargo_classes | (common_bitnum << 8) | (common_subtype << 16) | (user_def_data << 24); + SetBit(v->grf_cache.cache_valid, 2); } - return v->vcache.cached_var42; + return v->grf_cache.consist_cargo_information; case 0x43: // Company information - if (!HasBit(v->vcache.cache_valid, 3)) { - v->vcache.cached_var43 = v->owner | (Company::IsHumanID(v->owner) ? 0 : 0x10000) | (LiveryHelper(v->engine_type, v) << 24); - SetBit(v->vcache.cache_valid, 3); + if (!HasBit(v->grf_cache.cache_valid, 3)) { + v->grf_cache.company_information = v->owner | (Company::IsHumanID(v->owner) ? 0 : 0x10000) | (LiveryHelper(v->engine_type, v) << 24); + SetBit(v->grf_cache.cache_valid, 3); } - return v->vcache.cached_var43; + return v->grf_cache.company_information; case 0x44: // Aircraft information if (v->type != VEH_AIRCRAFT) return UINT_MAX; diff --git a/src/openttd.cpp b/src/openttd.cpp --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1133,7 +1133,7 @@ static void CheckCaches() uint length = 0; for (const Vehicle *u = v; u != NULL; u = u->Next()) length++; - VehicleCache *veh_cache = CallocT(length); + NewGRFCache *grf_cache = CallocT(length); AccelerationCache *acc_cache = CallocT(length); TrainCache *tra_cache = CallocT(length); RoadVehicleCache *roa_cache = CallocT(length); @@ -1141,7 +1141,7 @@ static void CheckCaches() length = 0; for (const Vehicle *u = v; u != NULL; u = u->Next()) { - veh_cache[length] = u->vcache; + grf_cache[length] = u->grf_cache; switch (u->type) { case VEH_TRAIN: acc_cache[length] = Train::From(u)->acc_cache; @@ -1168,8 +1168,8 @@ static void CheckCaches() length = 0; for (const Vehicle *u = v; u != NULL; u = u->Next()) { - if (memcmp(&veh_cache[length], &u->vcache, sizeof(VehicleCache)) != 0) { - DEBUG(desync, 2, "vehicle cache mismatch: type %i, vehicle %i, company %i, unit number %i, wagon %i", (int)v->type, v->index, (int)v->owner, v->unitnumber, length); + if (memcmp(&grf_cache[length], &u->grf_cache, sizeof(NewGRFCache)) != 0) { + DEBUG(desync, 2, "newgrf cache mismatch: type %i, vehicle %i, company %i, unit number %i, wagon %i", (int)v->type, v->index, (int)v->owner, v->unitnumber, length); } switch (u->type) { case VEH_TRAIN: @@ -1199,7 +1199,7 @@ static void CheckCaches() length++; } - free(veh_cache); + free(grf_cache); free(acc_cache); free(tra_cache); free(roa_cache); diff --git a/src/vehicle_base.h b/src/vehicle_base.h --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -47,12 +47,13 @@ enum VehicleFlags { }; /** Cached often queried (NewGRF) values */ -struct VehicleCache { - uint8 cache_valid; ///< Whether the caches are valid - uint32 cached_var40; ///< Cache for NewGRF var 40 - uint32 cached_var41; ///< Cache for NewGRF var 41 - uint32 cached_var42; ///< Cache for NewGRF var 42 - uint32 cached_var43; ///< Cache for NewGRF var 43 +struct NewGRFCache { + /* Values calculated when they are requested for the first time after invalidating the NewGRF cache. */ + uint32 position_consist_length; ///< Cache for NewGRF var 40. + uint32 position_same_id_length; ///< Cache for NewGRF var 41. + uint32 consist_cargo_information; ///< Cache for NewGRF var 42. + uint32 company_information; ///< Cache for NewGRF var 43. + uint8 cache_valid; ///< Bitset that indicates which cache values are valid. }; /** A vehicle pool for a little over 1 million vehicles. */ @@ -189,7 +190,7 @@ public: byte subtype; ///< subtype (Filled with values from #EffectVehicles/#TrainSubTypes/#AircraftSubTypes) - VehicleCache vcache; ///< Cache of often used calculated values + NewGRFCache grf_cache; ///< Cache of often used calculated NewGRF values /** Create a new vehicle */ Vehicle(VehicleType type = VEH_INVALID); @@ -308,7 +309,7 @@ public: */ FORCEINLINE void InvalidateNewGRFCache() { - this->vcache.cache_valid = 0; + this->grf_cache.cache_valid = 0; } /**