diff options
author | terkhen <terkhen@openttd.org> | 2010-11-06 12:53:31 +0000 |
---|---|---|
committer | terkhen <terkhen@openttd.org> | 2010-11-06 12:53:31 +0000 |
commit | c8a56f17f82be7d4a8e935fc514c86a313dc7d15 (patch) | |
tree | 4a2552f26ee887aa326721881fbbe6f62b8b48d2 | |
parent | 78d0a1cb1af68863e3e7bfed584f4a94ee46f611 (diff) | |
download | openttd-c8a56f17f82be7d4a8e935fc514c86a313dc7d15.tar.xz |
(svn r21097) -Codechange: Unify cached max speed for all vehicle types.
-rw-r--r-- | src/aircraft.h | 11 | ||||
-rw-r--r-- | src/aircraft_cmd.cpp | 10 | ||||
-rw-r--r-- | src/economy.cpp | 2 | ||||
-rw-r--r-- | src/newgrf_engine.cpp | 2 | ||||
-rw-r--r-- | src/openttd.cpp | 15 | ||||
-rw-r--r-- | src/train.h | 3 | ||||
-rw-r--r-- | src/train_cmd.cpp | 10 | ||||
-rw-r--r-- | src/vehicle_base.h | 6 | ||||
-rw-r--r-- | src/vehicle_gui.cpp | 6 |
9 files changed, 29 insertions, 36 deletions
diff --git a/src/aircraft.h b/src/aircraft.h index 34e6e2082..6afe4ef61 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -58,17 +58,10 @@ void AircraftNextAirportPos_and_Order(Aircraft *v); void SetAircraftPosition(Aircraft *v, int x, int y, int z); byte GetAircraftFlyingAltitude(const Aircraft *v); -/** Cached oftenly queried (NewGRF) values */ -struct AircraftCache { - uint16 cached_max_speed; ///< Cached maximum speed of the aircraft. -}; - /** * Aircraft, helicopters, rotors and their shadows belong to this class. */ struct Aircraft : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> { - AircraftCache acache; ///< Cache of often used calculated values - uint16 crashed_counter; ///< Timer for handling crash animations. byte pos; ///< Next desired position of the aircraft. byte previous_pos; ///< Previous desired position of the aircraft. @@ -90,8 +83,8 @@ struct Aircraft : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> { bool IsPrimaryVehicle() const { return this->IsNormalAircraft(); } SpriteID GetImage(Direction direction) const; int GetDisplaySpeed() const { return this->cur_speed; } - int GetDisplayMaxSpeed() const { return this->acache.cached_max_speed; } - int GetSpeedOldUnits() const { return this->acache.cached_max_speed * 10 / 128; } + int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed; } + int GetSpeedOldUnits() const { return this->vcache.cached_max_speed * 10 / 128; } Money GetRunningCost() const; bool IsInDepot() const { return (this->vehstatus & VS_HIDDEN) != 0 && IsHangarTile(this->tile); } bool Tick(); diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 1f9df1853..ed7196261 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -535,10 +535,10 @@ void UpdateAircraftCache(Aircraft *v) /* Convert from original units to km-ish/h */ max_speed = (max_speed * 128) / 10; - v->acache.cached_max_speed = max_speed; + v->vcache.cached_max_speed = max_speed; } else { /* Use the default max speed of the vehicle. */ - v->acache.cached_max_speed = v->max_speed; + v->vcache.cached_max_speed = v->max_speed; } } @@ -570,9 +570,9 @@ static int UpdateAircraftSpeed(Aircraft *v, uint speed_limit = SPEED_LIMIT_NONE, * and take-off speeds being too low. */ speed_limit *= _settings_game.vehicle.plane_speed; - if (v->acache.cached_max_speed < speed_limit) { + if (v->vcache.cached_max_speed < speed_limit) { if (v->cur_speed < speed_limit) hard_limit = false; - speed_limit = v->acache.cached_max_speed; + speed_limit = v->vcache.cached_max_speed; } v->subspeed = (t = v->subspeed) + (byte)spd; @@ -640,7 +640,7 @@ byte GetAircraftFlyingAltitude(const Aircraft *v) } /* Make faster planes fly higher so that they can overtake slower ones */ - base_altitude += min(20 * (v->acache.cached_max_speed / 200), 90); + base_altitude += min(20 * (v->vcache.cached_max_speed / 200), 90); return base_altitude; } diff --git a/src/economy.cpp b/src/economy.cpp index 810e99701..f7326f4e2 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1228,7 +1228,7 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) /* update stats */ int t; switch (u->type) { - case VEH_TRAIN: t = Train::From(u)->tcache.cached_max_speed; break; + case VEH_TRAIN: 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. diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 1d683430c..d93159fe1 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -718,7 +718,7 @@ static uint32 VehicleGetVariable(const ResolverObject *object, byte variable, by uint max_speed; switch (v->type) { case VEH_TRAIN: - max_speed = Train::From(v)->tcache.cached_max_speed; + max_speed = v->vcache.cached_max_speed; break; case VEH_AIRCRAFT: diff --git a/src/openttd.cpp b/src/openttd.cpp index f1d7aebba..e324eeba1 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1134,14 +1134,15 @@ static void CheckCaches() for (const Vehicle *u = v; u != NULL; u = u->Next()) length++; NewGRFCache *grf_cache = CallocT<NewGRFCache>(length); + VehicleCache *veh_cache = CallocT<VehicleCache>(length); AccelerationCache *acc_cache = CallocT<AccelerationCache>(length); TrainCache *tra_cache = CallocT<TrainCache>(length); RoadVehicleCache *roa_cache = CallocT<RoadVehicleCache>(length); - AircraftCache *air_cache = CallocT<AircraftCache>(length); length = 0; for (const Vehicle *u = v; u != NULL; u = u->Next()) { grf_cache[length] = u->grf_cache; + veh_cache[length] = u->vcache; switch (u->type) { case VEH_TRAIN: acc_cache[length] = Train::From(u)->acc_cache; @@ -1151,8 +1152,6 @@ static void CheckCaches() acc_cache[length] = RoadVehicle::From(u)->acc_cache; roa_cache[length] = RoadVehicle::From(u)->rcache; break; - case VEH_AIRCRAFT: - air_cache[length] = Aircraft::From(u)->acache; default: break; } @@ -1171,6 +1170,9 @@ static void CheckCaches() 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); } + 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); + } switch (u->type) { case VEH_TRAIN: if (memcmp(&acc_cache[length], &Train::From(u)->acc_cache, sizeof(AccelerationCache)) != 0) { @@ -1188,11 +1190,6 @@ static void CheckCaches() 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; - case VEH_AIRCRAFT: - if (memcmp(&air_cache[length], &Aircraft::From(u)->acache, sizeof(AircraftCache)) != 0) { - DEBUG(desync, 2, "aircraft cache mismatch: vehicle %i, company %i, unit number %i", v->index, (int)v->owner, v->unitnumber); - } - break; default: break; } @@ -1200,10 +1197,10 @@ static void CheckCaches() } free(grf_cache); + free(veh_cache); free(acc_cache); free(tra_cache); free(roa_cache); - free(air_cache); } /* Check whether the caches are still valid */ diff --git a/src/train.h b/src/train.h index c1dd3af24..b334c0b92 100644 --- a/src/train.h +++ b/src/train.h @@ -76,7 +76,6 @@ struct TrainCache { bool cached_tilt; ///< train can tilt; feature provides a bonus in curves /* cached max. speed / acceleration data */ - uint16 cached_max_speed; ///< max speed of the consist. (minimum of the max speed of all vehicles in the consist) int cached_max_curve_speed; ///< max consist speed limited by curves /** @@ -127,7 +126,7 @@ struct Train : public GroundVehicle<Train, VEH_TRAIN> { bool IsPrimaryVehicle() const { return this->IsFrontEngine(); } SpriteID GetImage(Direction direction) const; int GetDisplaySpeed() const { return this->tcache.last_speed; } - int GetDisplayMaxSpeed() const { return this->tcache.cached_max_speed; } + int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed; } Money GetRunningCost() const; int GetDisplayImageWidth(Point *offset = NULL) const; bool IsInDepot() const; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 2983f76f1..574972694 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -285,7 +285,7 @@ void Train::ConsistChanged(bool same_length) } /* store consist weight/max speed in cache */ - this->tcache.cached_max_speed = max_speed; + this->vcache.cached_max_speed = max_speed; this->tcache.cached_tilt = train_can_tilt; this->tcache.cached_max_curve_speed = this->GetCurveSpeedLimit(); @@ -1984,7 +1984,7 @@ static void HandleLocomotiveSmokeCloud(const Train *v) * third of its maximum speed spectrum. Steam emission finally normalises at very close to train's maximum speed. * REGULATION: * - instead of 1, 4 / 2^smoke_amount (max. 2) is used to provide sufficient regulation to steam puffs' amount. */ - if (GB(v->tick_counter, 0, ((4 >> _settings_game.vehicle.smoke_amount) + ((u->cur_speed * 3) / u->tcache.cached_max_speed))) == 0) { + if (GB(v->tick_counter, 0, ((4 >> _settings_game.vehicle.smoke_amount) + ((u->cur_speed * 3) / u->vcache.cached_max_speed))) == 0) { CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE); sound = true; } @@ -2002,8 +2002,8 @@ static void HandleLocomotiveSmokeCloud(const Train *v) * REGULATION: * - up to which speed a diesel train is emitting smoke (with reduced/small setting only until 1/2 of max_speed), * - in Chance16 - the last value is 512 / 2^smoke_amount (max. smoke when 128 = smoke_amount of 2). */ - if (u->cur_speed < (u->tcache.cached_max_speed >> (2 >> _settings_game.vehicle.smoke_amount)) && - Chance16((64 - ((u->cur_speed << 5) / u->tcache.cached_max_speed) + (32 >> (u->acc_cache.cached_power >> 10)) - (32 >> (u->acc_cache.cached_weight >> 9))), (512 >> _settings_game.vehicle.smoke_amount))) { + if (u->cur_speed < (u->vcache.cached_max_speed >> (2 >> _settings_game.vehicle.smoke_amount)) && + Chance16((64 - ((u->cur_speed << 5) / u->vcache.cached_max_speed) + (32 >> (u->acc_cache.cached_power >> 10)) - (32 >> (u->acc_cache.cached_weight >> 9))), (512 >> _settings_game.vehicle.smoke_amount))) { CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE); sound = true; } @@ -2017,7 +2017,7 @@ static void HandleLocomotiveSmokeCloud(const Train *v) * REGULATION: * - in Chance16 the last value is 360 / 2^smoke_amount (max. sparks when 90 = smoke_amount of 2). */ if (GB(v->tick_counter, 0, 2) == 0 && - Chance16((6 - ((u->cur_speed << 2) / u->tcache.cached_max_speed)), (360 >> _settings_game.vehicle.smoke_amount))) { + Chance16((6 - ((u->cur_speed << 2) / u->vcache.cached_max_speed)), (360 >> _settings_game.vehicle.smoke_amount))) { CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK); sound = true; } diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 13f71c1c7..e757ef581 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -64,6 +64,11 @@ struct NewGRFCache { uint8 cache_valid; ///< Bitset that indicates which cache values are valid. }; +/** Cached often queried values common to all vehicles. */ +struct VehicleCache { + uint16 cached_max_speed; ///< Maximum speed of the consist (minimum of the max speed of all vehicles in the consist). +}; + /** A vehicle pool for a little over 1 million vehicles. */ typedef Pool<Vehicle, VehicleID, 512, 0xFF000> VehiclePool; extern VehiclePool _vehicle_pool; @@ -199,6 +204,7 @@ public: byte subtype; ///< subtype (Filled with values from #EffectVehicles/#TrainSubTypes/#AircraftSubTypes) NewGRFCache grf_cache; ///< Cache of often used calculated NewGRF values + VehicleCache vcache; ///< Cache of often used vehicle values. /** Create a new vehicle */ Vehicle(VehicleType type = VEH_INVALID); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 23b1e1e42..7c788b3eb 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -807,10 +807,8 @@ 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) { - r = Train::From(*a)->tcache.cached_max_speed - Train::From(*b)->tcache.cached_max_speed; - } if ((*a)->type == VEH_AIRCRAFT && (*b)->type == VEH_AIRCRAFT) { - r = Aircraft::From(*a)->acache.cached_max_speed - Aircraft::From(*b)->acache.cached_max_speed; + if (((*a)->type == VEH_TRAIN && (*b)->type == VEH_TRAIN) || ((*a)->type == VEH_AIRCRAFT && (*b)->type == VEH_AIRCRAFT)) { + r = (*a)->vcache.cached_max_speed - (*b)->vcache.cached_max_speed; } else { r = (*a)->max_speed - (*b)->max_speed; } |