From a4a92741778047893eb05dd9e98d0f153d41e2eb Mon Sep 17 00:00:00 2001 From: terkhen Date: Tue, 14 Dec 2010 21:31:00 +0000 Subject: [PATCH] (svn r21519) -Codechange: Allow direct access to the GroundVehicleCache from a Vehicle. --- src/vehicle.cpp | 31 +++++++++++++++++++++++++++++++ src/vehicle_base.h | 4 ++++ src/vehicle_gui.cpp | 45 ++++++++++++++++++--------------------------- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/vehicle.cpp b/src/vehicle.cpp index e9630566ef..d3b47c7e32 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -52,6 +52,7 @@ #include "vehiclelist.h" #include "tunnel_map.h" #include "depot_map.h" +#include "ground_vehicle.hpp" #include "table/strings.h" @@ -2228,3 +2229,33 @@ bool CanVehicleUseStation(const Vehicle *v, const Station *st) return CanVehicleUseStation(v->engine_type, st); } + +/** + * Access the ground vehicle cache of the vehicle. + * @pre The vehicle is a #GroundVehicle. + * @return #GroundVehicleCache of the vehicle. + */ +GroundVehicleCache *Vehicle::GetGroundVehicleCache() +{ + assert(this->IsGroundVehicle()); + if (this->type == VEH_TRAIN) { + return &Train::From(this)->gcache; + } else { + return &RoadVehicle::From(this)->gcache; + } +} + +/** + * Access the ground vehicle cache of the vehicle. + * @pre The vehicle is a #GroundVehicle. + * @return #GroundVehicleCache of the vehicle. + */ +const GroundVehicleCache *Vehicle::GetGroundVehicleCache() const +{ + assert(this->IsGroundVehicle()); + if (this->type == VEH_TRAIN) { + return &Train::From(this)->gcache; + } else { + return &RoadVehicle::From(this)->gcache; + } +} diff --git a/src/vehicle_base.h b/src/vehicle_base.h index d81713d74f..381a5e3f6c 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -98,6 +98,7 @@ extern VehiclePool _vehicle_pool; /* Some declarations of functions, so we can make them friendly */ struct SaveLoad; +struct GroundVehicleCache; extern const SaveLoad *GetVehicleDescription(VehicleType vt); struct LoadgameState; extern bool LoadOldVehicle(LoadgameState *ls, int num); @@ -239,6 +240,9 @@ public: void BeginLoading(); void LeaveStation(); + GroundVehicleCache *GetGroundVehicleCache(); + const GroundVehicleCache *GetGroundVehicleCache() const; + /** * Handle the loading of the vehicle; when not it skips through dummy * orders and does nothing in all other cases. diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 3fb6f403ad..dfd534da0f 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1701,34 +1701,25 @@ struct VehicleDetailsWindow : Window { y += FONT_HEIGHT_NORMAL; /* Draw max speed */ - switch (v->type) { - case VEH_TRAIN: - SetDParam(2, v->GetDisplayMaxSpeed()); - SetDParam(1, Train::From(v)->gcache.cached_power); - SetDParam(0, Train::From(v)->gcache.cached_weight); - SetDParam(3, Train::From(v)->gcache.cached_max_te / 1000); - DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(Train::From(v)->railtype)->acceleration_type != 2) ? - STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE : STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED); - break; - - case VEH_ROAD: - if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) { - SetDParam(2, v->GetDisplayMaxSpeed()); - SetDParam(1, RoadVehicle::From(v)->gcache.cached_power); - SetDParam(0, RoadVehicle::From(v)->gcache.cached_weight); - SetDParam(3, RoadVehicle::From(v)->gcache.cached_max_te / 1000); - DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE); - break; - } - /* FALL THROUGH */ - case VEH_SHIP: - case VEH_AIRCRAFT: - SetDParam(0, v->GetDisplayMaxSpeed()); - DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_VEHICLE_INFO_MAX_SPEED); - break; - - default: NOT_REACHED(); + StringID string; + if (v->type == VEH_TRAIN || + (v->type == VEH_ROAD && _settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL)) { + const GroundVehicleCache *gcache = v->GetGroundVehicleCache(); + SetDParam(2, v->GetDisplayMaxSpeed()); + SetDParam(1, gcache->cached_power); + SetDParam(0, gcache->cached_weight); + SetDParam(3, gcache->cached_max_te / 1000); + if (v->type == VEH_TRAIN && (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL || + GetRailTypeInfo(Train::From(v)->railtype)->acceleration_type == 2)) { + string = STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED; + } else { + string = STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE; + } + } else { + SetDParam(0, v->GetDisplayMaxSpeed()); + string = STR_VEHICLE_INFO_MAX_SPEED; } + DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, string); y += FONT_HEIGHT_NORMAL; /* Draw profit */