diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index e9c6e1aa77..5ac76b50d8 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -383,7 +383,7 @@ void VehicleCargoList::AgeCargo() for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) { CargoPacket *cp = *it; /* If we're at the maximum, then we can't increase no more. */ - if (cp->days_in_transit == 0xFF) continue; + if (cp->days_in_transit == UINT16_MAX) continue; cp->days_in_transit++; this->cargo_days_in_transit += cp->count; diff --git a/src/cargopacket.h b/src/cargopacket.h index 0e24bc6233..7cd5a8df93 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -44,7 +44,7 @@ struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> { private: Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo. uint16 count; ///< The amount of cargo in this packet. - byte days_in_transit; ///< Amount of days this packet has been in transit. + uint16 days_in_transit; ///< Amount of days this packet has been in transit. SourceType source_type; ///< Type of \c source_id. SourceID source_id; ///< Index of source, INVALID_SOURCE if unknown/invalid. StationID source; ///< The station where the cargo came from first. @@ -126,10 +126,10 @@ public: /** * Gets the number of days this cargo has been in transit. * This number isn't really in days, but in 2.5 days (CARGO_AGING_TICKS = 185 ticks) and - * it is capped at 255. + * it is capped at UINT16_MAX. * @return Length this cargo has been in transit. */ - inline byte DaysInTransit() const + inline uint16 DaysInTransit() const { return this->days_in_transit; } @@ -221,8 +221,8 @@ public: }; protected: - uint count; ///< Cache for the number of cargo entities. - uint cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours. + uint count; ///< Cache for the number of cargo entities. + uint64 cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours. Tcont packets; ///< The cargo packets in this list. diff --git a/src/economy.cpp b/src/economy.cpp index 6d22e69ff3..bf15a9f232 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -975,7 +975,7 @@ Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift return cost; } -Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type) +Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16 transit_days, CargoID cargo_type) { const CargoSpec *cs = CargoSpec::Get(cargo_type); if (!cs->IsValid()) { @@ -985,7 +985,7 @@ Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, C /* Use callback to calculate cargo profit, if available */ if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) { - uint32 var18 = std::min(dist, 0xFFFFu) | (std::min(num_pieces, 0xFFu) << 16) | (transit_days << 24); + uint32 var18 = std::min(dist, 0xFFFFu) | (std::min(num_pieces, 0xFFu) << 16) | (std::min(transit_days, 0xFFu) << 24); uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs); if (callback != CALLBACK_FAILED) { int result = GB(callback, 0, 14); @@ -1002,25 +1002,35 @@ Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, C static const int MIN_TIME_FACTOR = 31; static const int MAX_TIME_FACTOR = 255; + static const int TIME_FACTOR_FRAC_BITS = 4; + static const int TIME_FACTOR_FRAC = 1 << TIME_FACTOR_FRAC_BITS; const int days1 = cs->transit_days[0]; const int days2 = cs->transit_days[1]; const int days_over_days1 = std::max( transit_days - days1, 0); const int days_over_days2 = std::max(days_over_days1 - days2, 0); + int days_over_max = MIN_TIME_FACTOR - MAX_TIME_FACTOR; + if (days2 > -days_over_max) days_over_max += transit_days - days1; + else days_over_max += 2 * (transit_days - days1) - days2; /* * The time factor is calculated based on the time it took * (transit_days) compared two cargo-depending values. The - * range is divided into three parts: + * range is divided into four parts: * * - constant for fast transits * - linear decreasing with time with a slope of -1 for medium transports * - linear decreasing with time with a slope of -2 for slow transports + * - after hitting MIN_TIME_FACTOR, the time factor will be asymptotically decreased to a limit of 1 with a scaled 1/(x+1) function. * */ - const int time_factor = std::max(MAX_TIME_FACTOR - days_over_days1 - days_over_days2, MIN_TIME_FACTOR); - - return BigMulS(dist * time_factor * num_pieces, cs->current_payment, 21); + if (days_over_max > 0) { + const int time_factor = std::max(2 * MIN_TIME_FACTOR * TIME_FACTOR_FRAC * TIME_FACTOR_FRAC / (days_over_max + 2 * TIME_FACTOR_FRAC), 1); // MIN_TIME_FACTOR / (x/(2 * TIME_FACTOR_FRAC) + 1) + 1, expressed as fixed point with TIME_FACTOR_FRAC_BITS. + return BigMulS(dist * time_factor * num_pieces, cs->current_payment, 21 + TIME_FACTOR_FRAC_BITS); + } else { + const int time_factor = std::max(MAX_TIME_FACTOR - days_over_days1 - days_over_days2, MIN_TIME_FACTOR); + return BigMulS(dist * time_factor * num_pieces, cs->current_payment, 21); + } } /** The industries we've currently brought cargo to. */ @@ -1094,7 +1104,7 @@ static uint DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, uint n * @return Revenue for delivering cargo * @note The cargo is just added to the stockpile of the industry. It is due to the caller to trigger the industry's production machinery */ -static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID dest, TileIndex source_tile, byte days_in_transit, Company *company, SourceType src_type, SourceID src) +static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID dest, TileIndex source_tile, uint16 days_in_transit, Company *company, SourceType src_type, SourceID src) { assert(num_pieces > 0); diff --git a/src/economy_func.h b/src/economy_func.h index 69980b4758..d5bfeae0a2 100644 --- a/src/economy_func.h +++ b/src/economy_func.h @@ -28,7 +28,7 @@ extern Prices _price; int UpdateCompanyRatingAndValue(Company *c, bool update); void StartupIndustryDailyChanges(bool init_counter); -Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type); +Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16 transit_days, CargoID cargo_type); uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations, Owner exclusivity = INVALID_OWNER); void PrepareUnload(Vehicle *front_v); diff --git a/src/saveload/cargopacket_sl.cpp b/src/saveload/cargopacket_sl.cpp index 95b9bec6bf..07ed94287c 100644 --- a/src/saveload/cargopacket_sl.cpp +++ b/src/saveload/cargopacket_sl.cpp @@ -88,14 +88,15 @@ SaveLoadTable GetCargoPacketDesc() { static const SaveLoad _cargopacket_desc[] = { - SLE_VAR(CargoPacket, source, SLE_UINT16), - SLE_VAR(CargoPacket, source_xy, SLE_UINT32), - SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32), - SLE_VAR(CargoPacket, count, SLE_UINT16), - SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8), - SLE_VAR(CargoPacket, feeder_share, SLE_INT64), - SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION), - SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION), + SLE_VAR(CargoPacket, source, SLE_UINT16), + SLE_VAR(CargoPacket, source_xy, SLE_UINT32), + SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32), + SLE_VAR(CargoPacket, count, SLE_UINT16), + SLE_CONDVAR(CargoPacket, days_in_transit, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_MORE_CARGO_AGE), + SLE_CONDVAR(CargoPacket, days_in_transit, SLE_UINT16, SLV_MORE_CARGO_AGE, SL_MAX_VERSION), + SLE_VAR(CargoPacket, feeder_share, SLE_INT64), + SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION), + SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION), }; return _cargopacket_desc; } diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 1614dba06f..539992f5bb 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -349,6 +349,7 @@ enum SaveLoadVersion : uint16 { SLV_VELOCITY_NAUTICAL, ///< 305 PR#10594 Separation of land and nautical velocity (knots!) SLV_CONSISTENT_PARTIAL_Z, ///< 306 PR#10570 Conversion from an inconsistent partial Z calculation for slopes, to one that is (more) consistent. + SLV_MORE_CARGO_AGE, ///< 307 PR#10596 Track cargo age for a longer period. SL_MAX_VERSION, ///< Highest possible saveload version }; diff --git a/src/script/api/script_cargo.cpp b/src/script/api/script_cargo.cpp index 7354c848c0..aad2425461 100644 --- a/src/script/api/script_cargo.cpp +++ b/src/script/api/script_cargo.cpp @@ -76,7 +76,7 @@ distance = Clamp(distance, 0, UINT32_MAX); - return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type); + return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, UINT16_MAX), cargo_type); } /* static */ ScriptCargo::DistributionType ScriptCargo::GetDistributionType(CargoID cargo_type)