From 25d1b2f54b9cac01e2737848c208f6d8096a4a92 Mon Sep 17 00:00:00 2001 From: terkhen Date: Sat, 6 Nov 2010 13:03:17 +0000 Subject: (svn r21098) -Codechange: Ships now store their max speed in the cache instead of recalculating it every time. --- src/economy.cpp | 6 ++++-- src/newgrf_engine.cpp | 3 ++- src/saveload/vehicle_sl.cpp | 34 +++++++++++++++++++++++----------- src/ship.h | 3 ++- src/ship_cmd.cpp | 9 ++++++++- src/vehicle.cpp | 1 + src/vehicle_gui.cpp | 2 +- 7 files changed, 41 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/economy.cpp b/src/economy.cpp index f7326f4e2..ecccc99a1 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1228,9 +1228,11 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) /* update stats */ int t; switch (u->type) { - case VEH_TRAIN: t = u->vcache.cached_max_speed; break; + case VEH_TRAIN: /* FALL THROUGH */ + case VEH_SHIP: + t = u->vcache.cached_max_speed; + break; case VEH_ROAD: t = u->max_speed / 2; break; - case VEH_SHIP: t = u->max_speed; break; case VEH_AIRCRAFT: t = Aircraft::From(u)->GetSpeedOldUnits(); break; // Convert to old units. default: NOT_REACHED(); } diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index d93159fe1..04e71c8fb 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -717,7 +717,8 @@ static uint32 VehicleGetVariable(const ResolverObject *object, byte variable, by case 0x19: { uint max_speed; switch (v->type) { - case VEH_TRAIN: + case VEH_TRAIN: /* FALL THROUGH */ + case VEH_SHIP: max_speed = v->vcache.cached_max_speed; break; diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 6da47fee4..7817e81ba 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -313,20 +313,32 @@ void AfterLoadVehicles(bool part_of_load) FOR_ALL_VEHICLES(v) { assert(v->first != NULL); - if (v->type == VEH_TRAIN) { - Train *t = Train::From(v); - if (t->IsFrontEngine() || t->IsFreeWagon()) { - t->tcache.last_speed = t->cur_speed; // update displayed train speed - t->ConsistChanged(false); + switch (v->type) { + case VEH_TRAIN: { + Train *t = Train::From(v); + if (t->IsFrontEngine() || t->IsFreeWagon()) { + t->tcache.last_speed = t->cur_speed; // update displayed train speed + t->ConsistChanged(false); + } + break; } - } else if (v->type == VEH_ROAD) { - RoadVehicle *rv = RoadVehicle::From(v); - if (rv->IsRoadVehFront()) { - RoadVehUpdateCache(rv); - if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) { - rv->CargoChanged(); + + case VEH_ROAD: { + RoadVehicle *rv = RoadVehicle::From(v); + if (rv->IsRoadVehFront()) { + RoadVehUpdateCache(rv); + if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) { + rv->CargoChanged(); + } } + break; } + + case VEH_SHIP: + Ship::From(v)->UpdateCache(); + break; + + default: break; } } diff --git a/src/ship.h b/src/ship.h index 1c6749576..ca4cefa6d 100644 --- a/src/ship.h +++ b/src/ship.h @@ -35,7 +35,7 @@ struct Ship: public SpecializedVehicle { bool IsPrimaryVehicle() const { return true; } SpriteID GetImage(Direction direction) const; int GetDisplaySpeed() const { return this->cur_speed / 2; } - int GetDisplayMaxSpeed() const { return this->max_speed / 2; } + int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed / 2; } Money GetRunningCost() const; bool IsInDepot() const { return this->state == TRACK_BIT_DEPOT; } bool Tick(); @@ -43,6 +43,7 @@ struct Ship: public SpecializedVehicle { Trackdir GetVehicleTrackdir() const; TileIndex GetOrderStationLocation(StationID station); bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse); + void UpdateCache(); }; #define FOR_ALL_SHIPS(var) FOR_ALL_VEHICLES_OF_TYPE(Ship, var) diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 24e9eb654..f220e13a0 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -153,6 +153,11 @@ static void CheckIfShipNeedsService(Vehicle *v) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); } +void Ship::UpdateCache() +{ + this->vcache.cached_max_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, this->max_speed); +} + Money Ship::GetRunningCost() const { const Engine *e = Engine::Get(this->engine_type); @@ -296,7 +301,7 @@ static bool ShipAccelerate(Vehicle *v) uint spd; byte t; - spd = min(v->cur_speed + 1, GetVehicleProperty(v, PROP_SHIP_SPEED, v->max_speed)); + spd = min(v->cur_speed + 1, v->vcache.cached_max_speed); /* updates statusbar only if speed have changed to save CPU time */ if (spd != v->cur_speed) { @@ -615,6 +620,8 @@ CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, u v->cur_image = SPR_IMG_QUERY; v->random_bits = VehicleRandomBits(); + v->UpdateCache(); + if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE); v->InvalidateNewGRFCacheOfChain(); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 8052a29ac..a10f0481d 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1163,6 +1163,7 @@ void VehicleEnterDepot(Vehicle *v) SetWindowClassesDirty(WC_SHIPS_LIST); Ship *ship = Ship::From(v); ship->state = TRACK_BIT_DEPOT; + ship->UpdateCache(); ship->UpdateViewport(true, true); SetWindowDirty(WC_VEHICLE_DEPOT, v->tile); break; diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 7c788b3eb..7adacc88c 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -807,7 +807,7 @@ static int CDECL VehicleReliabilitySorter(const Vehicle * const *a, const Vehicl static int CDECL VehicleMaxSpeedSorter(const Vehicle * const *a, const Vehicle * const *b) { int r = 0; - if (((*a)->type == VEH_TRAIN && (*b)->type == VEH_TRAIN) || ((*a)->type == VEH_AIRCRAFT && (*b)->type == VEH_AIRCRAFT)) { + if ((*a)->type != VEH_ROAD && (*b)->type != VEH_ROAD) { r = (*a)->vcache.cached_max_speed - (*b)->vcache.cached_max_speed; } else { r = (*a)->max_speed - (*b)->max_speed; -- cgit v1.2.3-70-g09d2