diff --git a/src/cargoaction.cpp b/src/cargoaction.cpp index 0b8d203706..771a0d395d 100644 --- a/src/cargoaction.cpp +++ b/src/cargoaction.cpp @@ -107,7 +107,7 @@ bool CargoDelivery::operator()(CargoPacket *cp) { uint remove = this->Preprocess(cp); this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_DELIVER, remove); - this->payment->PayFinalDelivery(cp, remove); + this->payment->PayFinalDelivery(cp, remove, this->current_tile); return this->Postprocess(cp, remove); } diff --git a/src/cargoaction.h b/src/cargoaction.h index 72df5f5789..8c2f4d920c 100644 --- a/src/cargoaction.h +++ b/src/cargoaction.h @@ -38,10 +38,11 @@ public: /** Action of final delivery of cargo. */ class CargoDelivery : public CargoRemoval { protected: + TileIndex current_tile; ///< Current tile cargo delivery is happening. CargoPayment *payment; ///< Payment object where payments will be registered. public: - CargoDelivery(VehicleCargoList *source, uint max_move, CargoPayment *payment) : - CargoRemoval(source, max_move), payment(payment) {} + CargoDelivery(VehicleCargoList *source, uint max_move, CargoPayment *payment, TileIndex current_tile) : + CargoRemoval(source, max_move), current_tile(current_tile), payment(payment) {} bool operator()(CargoPacket *cp); }; diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index cc2929b516..f7472cd5fa 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -431,9 +431,10 @@ void VehicleCargoList::AgeCargo() * @param order_flags OrderUnloadFlags that will apply to the unload operation. * @param ge GoodsEntry for getting the flows. * @param payment Payment object for registering transfers. + * @param current_tile Current tile the cargo handling is happening on. * return If any cargo will be unloaded. */ -bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment) +bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment, TileIndex current_tile) { this->AssertCountConsistency(); assert(this->action_counts[MTA_LOAD] == 0); @@ -509,7 +510,7 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID case MTA_TRANSFER: this->packets.push_front(cp); /* Add feeder share here to allow reusing field for next station. */ - share = payment->PayTransfer(cp, cp->count); + share = payment->PayTransfer(cp, cp->count, current_tile); cp->AddFeederShare(share); this->feeder_share += share; cp->next_hop = cargo_next; @@ -616,9 +617,10 @@ uint VehicleCargoList::Shift(uint max_move, VehicleCargoList *dest) * @param dest StationCargoList to add transferred cargo to. * @param max_move Maximum amount of cargo to move. * @param payment Payment object to register payments in. + * @param current_tile Current tile the cargo handling is happening on. * @return Amount of cargo actually unloaded. */ -uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment) +uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment, TileIndex current_tile) { uint moved = 0; if (this->action_counts[MTA_TRANSFER] > 0) { @@ -628,7 +630,7 @@ uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPaymen } if (this->action_counts[MTA_TRANSFER] == 0 && this->action_counts[MTA_DELIVER] > 0 && moved < max_move) { uint move = std::min(this->action_counts[MTA_DELIVER], max_move - moved); - this->ShiftCargo(CargoDelivery(this, move, payment)); + this->ShiftCargo(CargoDelivery(this, move, payment, current_tile)); moved += move; } return moved; diff --git a/src/cargopacket.h b/src/cargopacket.h index c5dfb0cfef..0cde2cb7fe 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -161,12 +161,14 @@ public: } /** - * Gets the coordinates of the cargo's source. - * @return Source coordinates of cargo. + * Get the current distance the cargo has traveled. + * + * @param current_tile Current tile of the cargo. + * @return uint The distance (in tiles) traveled. */ - inline TileIndex GetSourceXY() const + inline uint GetDistance(TileIndex current_tile) const { - return this->source_xy; + return DistanceManhattan(this->source_xy, current_tile); } /** @@ -386,7 +388,7 @@ public: void InvalidateCache(); - bool Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment); + bool Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment, TileIndex current_tile); /** * Marks all cargo in the vehicle as to be kept. This is mostly useful for @@ -406,7 +408,7 @@ public: template uint Reassign(uint max_move); uint Return(uint max_move, StationCargoList *dest, StationID next_station); - uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment); + uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment, TileIndex current_tile); uint Shift(uint max_move, VehicleCargoList *dest); uint Truncate(uint max_move = UINT_MAX); uint Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge); diff --git a/src/economy.cpp b/src/economy.cpp index fb11504981..1de6b3f8bd 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1096,7 +1096,7 @@ static uint DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, uint n * @param num_pieces amount of cargo delivered * @param cargo_type the type of cargo that is delivered * @param dest Station the cargo has been unloaded - * @param source_tile The origin of the cargo for distance calculation + * @param distance The distance the cargo has traveled. * @param periods_in_transit Travel time in cargo aging periods * @param company The company delivering the cargo * @param src_type Type of source of cargo (industry, town, headquarters) @@ -1104,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, uint16_t periods_in_transit, Company *company, SourceType src_type, SourceID src) +static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID dest, uint distance, uint16_t periods_in_transit, Company *company, SourceType src_type, SourceID src) { assert(num_pieces > 0); @@ -1131,7 +1131,7 @@ static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID dest, Ti st->town->received[cs->town_effect].new_act += accepted_total; /* Determine profit */ - Money profit = GetTransportedGoodsIncome(accepted_total, DistanceManhattan(source_tile, st->xy), periods_in_transit, cargo_type); + Money profit = GetTransportedGoodsIncome(accepted_total, distance, periods_in_transit, cargo_type); /* Update the cargo monitor. */ AddCargoDelivery(cargo_type, company->index, accepted_total - accepted_ind, src_type, src, st); @@ -1225,15 +1225,16 @@ CargoPayment::~CargoPayment() * Handle payment for final delivery of the given cargo packet. * @param cp The cargo packet to pay for. * @param count The number of packets to pay for. + * @param current_tile Current tile the payment is happening on. */ -void CargoPayment::PayFinalDelivery(const CargoPacket *cp, uint count) +void CargoPayment::PayFinalDelivery(const CargoPacket *cp, uint count, TileIndex current_tile) { if (this->owner == nullptr) { this->owner = Company::Get(this->front->owner); } /* Handle end of route payment */ - Money profit = DeliverGoods(count, this->ct, this->current_station, cp->GetSourceXY(), cp->GetPeriodsInTransit(), this->owner, cp->GetSourceType(), cp->GetSourceID()); + Money profit = DeliverGoods(count, this->ct, this->current_station, cp->GetDistance(current_tile), cp->GetPeriodsInTransit(), this->owner, cp->GetSourceType(), cp->GetSourceID()); this->route_profit += profit; /* The vehicle's profit is whatever route profit there is minus feeder shares. */ @@ -1244,15 +1245,16 @@ void CargoPayment::PayFinalDelivery(const CargoPacket *cp, uint count) * Handle payment for transfer of the given cargo packet. * @param cp The cargo packet to pay for; actual payment won't be made!. * @param count The number of packets to pay for. + * @param current_tile Current tile the payment is happening on. * @return The amount of money paid for the transfer. */ -Money CargoPayment::PayTransfer(const CargoPacket *cp, uint count) +Money CargoPayment::PayTransfer(const CargoPacket *cp, uint count, TileIndex current_tile) { + /* Pay transfer vehicle the difference between the payment for the journey from + * the source to the current point, and the sum of the previous transfer payments */ Money profit = -cp->GetFeederShare(count) + GetTransportedGoodsIncome( count, - /* pay transfer vehicle the difference between the payment for the journey from - * the source to the current point, and the sum of the previous transfer payments */ - DistanceManhattan(cp->GetSourceXY(), Station::Get(this->current_station)->xy), + cp->GetDistance(current_tile), cp->GetPeriodsInTransit(), this->ct); @@ -1294,7 +1296,8 @@ void PrepareUnload(Vehicle *front_v) HasBit(ge->status, GoodsEntry::GES_ACCEPTANCE), front_v->last_station_visited, next_station, front_v->current_order.GetUnloadType(), ge, - front_v->cargo_payment); + front_v->cargo_payment, + v->tile); if (v->cargo.UnloadCount() > 0) SetBit(v->vehicle_flags, VF_CARGO_UNLOADING); } } @@ -1721,7 +1724,7 @@ static void LoadUnloadVehicle(Vehicle *front) } } - amount_unloaded = v->cargo.Unload(amount_unloaded, &ge->cargo, payment); + amount_unloaded = v->cargo.Unload(amount_unloaded, &ge->cargo, payment, v->tile); remaining = v->cargo.UnloadCount() > 0; if (amount_unloaded > 0) { dirty_vehicle = true; diff --git a/src/economy_base.h b/src/economy_base.h index 5c9d79b8c8..a0aa97f142 100644 --- a/src/economy_base.h +++ b/src/economy_base.h @@ -37,8 +37,8 @@ struct CargoPayment : CargoPaymentPool::PoolItem<&_cargo_payment_pool> { CargoPayment(Vehicle *front); ~CargoPayment(); - Money PayTransfer(const CargoPacket *cp, uint count); - void PayFinalDelivery(const CargoPacket *cp, uint count); + Money PayTransfer(const CargoPacket *cp, uint count, TileIndex current_tile); + void PayFinalDelivery(const CargoPacket *cp, uint count, TileIndex current_tile); /** * Sets the currently handled cargo type.