diff options
author | terkhen <terkhen@openttd.org> | 2010-12-14 21:33:53 +0000 |
---|---|---|
committer | terkhen <terkhen@openttd.org> | 2010-12-14 21:33:53 +0000 |
commit | 48b7916458de4b2cdbc226a4a6ab078db4c77e73 (patch) | |
tree | c3a49ca7a91890042ca56765574bc40a54c15f96 /src | |
parent | a93944e765776c6e9a2bd556457cd93748068da6 (diff) | |
download | openttd-48b7916458de4b2cdbc226a4a6ab078db4c77e73.tar.xz |
(svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/api/ai_vehicle.cpp | 6 | ||||
-rw-r--r-- | src/articulated_vehicles.cpp | 7 | ||||
-rw-r--r-- | src/autoreplace_cmd.cpp | 6 | ||||
-rw-r--r-- | src/depot_gui.cpp | 16 | ||||
-rw-r--r-- | src/economy.cpp | 2 | ||||
-rw-r--r-- | src/ground_vehicle.hpp | 5 | ||||
-rw-r--r-- | src/newgrf_engine.cpp | 21 | ||||
-rw-r--r-- | src/openttd.cpp | 6 | ||||
-rw-r--r-- | src/pathfinder/yapf/yapf_costrail.hpp | 4 | ||||
-rw-r--r-- | src/rail_cmd.cpp | 4 | ||||
-rw-r--r-- | src/roadstop.cpp | 6 | ||||
-rw-r--r-- | src/roadveh.h | 8 | ||||
-rw-r--r-- | src/roadveh_cmd.cpp | 16 | ||||
-rw-r--r-- | src/saveload/vehicle_sl.cpp | 3 | ||||
-rw-r--r-- | src/train.h | 12 | ||||
-rw-r--r-- | src/train_cmd.cpp | 40 | ||||
-rw-r--r-- | src/vehicle.cpp | 6 | ||||
-rw-r--r-- | src/vehicle_gui.cpp | 14 |
18 files changed, 69 insertions, 113 deletions
diff --git a/src/ai/api/ai_vehicle.cpp b/src/ai/api/ai_vehicle.cpp index 2aad40f60..7ece9cdef 100644 --- 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) diff --git a/src/articulated_vehicles.cpp b/src/articulated_vehicles.cpp index 7556d5497..8a67b2a9a 100644 --- a/src/articulated_vehicles.cpp +++ b/src/articulated_vehicles.cpp @@ -313,6 +313,9 @@ void AddArticulatedParts(Vehicle *first) * 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 @@ void AddArticulatedParts(Vehicle *first) 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 @@ void AddArticulatedParts(Vehicle *first) 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; diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index 1ba2d8f0a..1ff9bcffc 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -421,7 +421,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon 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 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon } /* 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 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon /* 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; } diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 8caeea967..8b6188efd 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -287,7 +287,7 @@ struct DepotWindow : Window { 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 @@ struct DepotWindow : Window { 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; diff --git a/src/economy.cpp b/src/economy.cpp index 1e94d7387..9d7d78143 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1370,7 +1370,7 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) 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; diff --git a/src/ground_vehicle.hpp b/src/ground_vehicle.hpp index 5033bef75..2922fff1d 100644 --- a/src/ground_vehicle.hpp +++ b/src/ground_vehicle.hpp @@ -36,6 +36,11 @@ struct GroundVehicleCache { 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. */ diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 22eca8844..76c5572f0 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -439,10 +439,8 @@ static uint8 LiveryHelper(EngineID engine, const Vehicle *v) 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 @@ static uint32 VehicleGetVariable(const ResolverObject *object, byte variable, by 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 @@ static const SpriteGroup *GetVehicleSpriteGroup(EngineID engine, const Vehicle * } 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; } } diff --git a/src/openttd.cpp b/src/openttd.cpp index 54d0ac3c1..41862c7b2 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1145,7 +1145,6 @@ static void CheckCaches() 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 @@ static void CheckCaches() 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 @@ static void CheckCaches() 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 @@ static void CheckCaches() free(veh_cache); free(gro_cache); free(tra_cache); - free(roa_cache); } /* Check whether the caches are still valid */ diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp index 53f7f60af..5c445843a 100644 --- a/src/pathfinder/yapf/yapf_costrail.hpp +++ b/src/pathfinder/yapf/yapf_costrail.hpp @@ -258,8 +258,8 @@ public: 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; diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index 2e88b1eb7..eed8c0bb8 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -2698,7 +2698,7 @@ static const int8 _deltacoord_leaveoffset[8] = { 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 @@ static VehicleEnterTileStatus VehicleEnter_Track(Vehicle *u, TileIndex tile, int /* 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 diff --git a/src/roadstop.cpp b/src/roadstop.cpp index 3e9e6ac93..921125f08 100644 --- a/src/roadstop.cpp +++ b/src/roadstop.cpp @@ -278,7 +278,7 @@ bool RoadStop::Enter(RoadVehicle *rv) */ 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 @@ void RoadStop::Entry::Enter(const RoadVehicle *rv) /* 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 @@ void RoadStop::Entry::Rebuild(const RoadStop *rs, int side) 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; } } diff --git a/src/roadveh.h b/src/roadveh.h index a76d19f5f..72c789fd1 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -83,18 +83,10 @@ enum RoadVehicleSubType { 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; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index aea5a9e99..422086e83 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -102,7 +102,7 @@ int RoadVehicle::GetDisplayImageWidth(Point *offset) const 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 @@ void RoadVehUpdateCache(RoadVehicle *v) 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 @@ CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engin 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 @@ CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engin 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 @@ again: * 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); } } diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index c081ba2bc..f6e40e792 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -251,8 +251,7 @@ void AfterLoadVehicles(bool part_of_load) 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 diff --git a/src/train.h b/src/train.h index 4e0bc10de..4cc9ea7a2 100644 --- a/src/train.h +++ b/src/train.h @@ -68,16 +68,12 @@ struct TrainCache { 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 @@ protected: // These functions should not be called outside acceleration code. { /* 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 @@ protected: // These functions should not be called outside acceleration code. /* 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; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 6e995b0a7..958ca090e 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -110,7 +110,7 @@ void CheckTrainsLengths() 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 @@ void Train::ConsistChanged(bool same_length) 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 @@ void Train::ConsistChanged(bool same_length) 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 @@ void Train::ConsistChanged(bool same_length) 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 @@ void Train::ConsistChanged(bool same_length) 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; + if (!same_length) u->gcache.cached_veh_length = veh_len; - this->tcache.cached_total_length += u->tcache.cached_veh_length; + this->gcache.cached_total_length += u->gcache.cached_veh_length; this->InvalidateNewGRFCache(); u->InvalidateNewGRFCache(); } @@ -281,7 +281,7 @@ int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, i /* 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 @@ int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, i 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 @@ int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, i /* 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 @@ int Train::GetDisplayImageWidth(Point *offset) const 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 @@ static CommandCost CmdBuildRailWagon(TileIndex tile, DoCommandFlag flags, const 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 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, const Engin 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 @@ static CommandCost CheckTrainAttachment(Train *t) 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 @@ static CommandCost CheckTrainAttachment(Train *t) 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 @@ static void AdvanceWagonsBeforeSwap(Train *v) 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 @@ static void AdvanceWagonsAfterSwap(Train *v) 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 @@ static bool TrainApproachingLineEnd(Train *v, bool signal) } /* 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); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index d3b47c7e3..cb915a6f7 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1665,10 +1665,8 @@ PaletteID GetEnginePalette(EngineID engine_type, CompanyID company) 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); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index dfd534da0..5625498ff 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -847,19 +847,7 @@ static int CDECL VehicleValueSorter(const Vehicle * const *a, const Vehicle * co /** 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); } |