1
0
Fork 0

Change: Increase max cargo age and let min cargo payment approach zero. (#10596)

Co-authored-by: Michael Lutz <michi@icosahedron.de>
pull/10625/head
dP 2023-04-10 14:34:28 +04:00 committed by GitHub
parent 770df65141
commit e2f1cd44dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 23 deletions

View File

@ -383,7 +383,7 @@ void VehicleCargoList::AgeCargo()
for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) { for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
CargoPacket *cp = *it; CargoPacket *cp = *it;
/* If we're at the maximum, then we can't increase no more. */ /* 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++; cp->days_in_transit++;
this->cargo_days_in_transit += cp->count; this->cargo_days_in_transit += cp->count;

View File

@ -44,7 +44,7 @@ struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
private: private:
Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo. Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo.
uint16 count; ///< The amount of cargo in this packet. 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. SourceType source_type; ///< Type of \c source_id.
SourceID source_id; ///< Index of source, INVALID_SOURCE if unknown/invalid. SourceID source_id; ///< Index of source, INVALID_SOURCE if unknown/invalid.
StationID source; ///< The station where the cargo came from first. 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. * 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 * 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. * @return Length this cargo has been in transit.
*/ */
inline byte DaysInTransit() const inline uint16 DaysInTransit() const
{ {
return this->days_in_transit; return this->days_in_transit;
} }
@ -221,8 +221,8 @@ public:
}; };
protected: protected:
uint count; ///< Cache for the number of cargo entities. 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. 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. Tcont packets; ///< The cargo packets in this list.

View File

@ -975,7 +975,7 @@ Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift
return cost; 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); const CargoSpec *cs = CargoSpec::Get(cargo_type);
if (!cs->IsValid()) { 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 */ /* Use callback to calculate cargo profit, if available */
if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) { 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<uint16>(transit_days, 0xFFu) << 24);
uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs); uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs);
if (callback != CALLBACK_FAILED) { if (callback != CALLBACK_FAILED) {
int result = GB(callback, 0, 14); 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 MIN_TIME_FACTOR = 31;
static const int MAX_TIME_FACTOR = 255; 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 days1 = cs->transit_days[0];
const int days2 = cs->transit_days[1]; const int days2 = cs->transit_days[1];
const int days_over_days1 = std::max( transit_days - days1, 0); const int days_over_days1 = std::max( transit_days - days1, 0);
const int days_over_days2 = std::max(days_over_days1 - days2, 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 * The time factor is calculated based on the time it took
* (transit_days) compared two cargo-depending values. The * (transit_days) compared two cargo-depending values. The
* range is divided into three parts: * range is divided into four parts:
* *
* - constant for fast transits * - constant for fast transits
* - linear decreasing with time with a slope of -1 for medium transports * - linear decreasing with time with a slope of -1 for medium transports
* - linear decreasing with time with a slope of -2 for slow 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); 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); 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. */ /** 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 * @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 * @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); assert(num_pieces > 0);

View File

@ -28,7 +28,7 @@ extern Prices _price;
int UpdateCompanyRatingAndValue(Company *c, bool update); int UpdateCompanyRatingAndValue(Company *c, bool update);
void StartupIndustryDailyChanges(bool init_counter); 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); 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); void PrepareUnload(Vehicle *front_v);

View File

@ -88,14 +88,15 @@
SaveLoadTable GetCargoPacketDesc() SaveLoadTable GetCargoPacketDesc()
{ {
static const SaveLoad _cargopacket_desc[] = { static const SaveLoad _cargopacket_desc[] = {
SLE_VAR(CargoPacket, source, SLE_UINT16), SLE_VAR(CargoPacket, source, SLE_UINT16),
SLE_VAR(CargoPacket, source_xy, SLE_UINT32), SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32), SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
SLE_VAR(CargoPacket, count, SLE_UINT16), SLE_VAR(CargoPacket, count, SLE_UINT16),
SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8), SLE_CONDVAR(CargoPacket, days_in_transit, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_MORE_CARGO_AGE),
SLE_VAR(CargoPacket, feeder_share, SLE_INT64), SLE_CONDVAR(CargoPacket, days_in_transit, SLE_UINT16, SLV_MORE_CARGO_AGE, SL_MAX_VERSION),
SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION), SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION), 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; return _cargopacket_desc;
} }

View File

@ -349,6 +349,7 @@ enum SaveLoadVersion : uint16 {
SLV_VELOCITY_NAUTICAL, ///< 305 PR#10594 Separation of land and nautical velocity (knots!) 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_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 SL_MAX_VERSION, ///< Highest possible saveload version
}; };

View File

@ -76,7 +76,7 @@
distance = Clamp<SQInteger>(distance, 0, UINT32_MAX); distance = Clamp<SQInteger>(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) /* static */ ScriptCargo::DistributionType ScriptCargo::GetDistributionType(CargoID cargo_type)