2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file cargopacket.cpp Implementation of the cargo packets. */
11 #include "station_base.h"
12 #include "core/pool_func.hpp"
13 #include "core/random_func.hpp"
14 #include "economy_base.h"
15 #include "cargoaction.h"
16 #include "order_type.h"
18 #include "safeguards.h"
20 /* Initialize the cargopacket-pool */
21 CargoPacketPool
_cargopacket_pool("CargoPacket");
22 INSTANTIATE_POOL_METHODS(CargoPacket
)
25 * Create a new packet for savegame loading.
27 CargoPacket::CargoPacket()
29 this->source_type
= SourceType::Industry
;
30 this->source_id
= INVALID_SOURCE
;
34 * Creates a new cargo packet.
36 * @param first_station Source station of the packet.
37 * @param count Number of cargo entities to put in this packet.
38 * @param source_type 'Type' of source the packet comes from (for subsidies).
39 * @param source_id Actual source of the packet (for subsidies).
42 CargoPacket::CargoPacket(StationID first_station
,uint16_t count
, SourceType source_type
, SourceID source_id
) :
45 source_type(source_type
),
46 first_station(first_station
)
52 * Create a new cargo packet. Used for older savegames to load in their partial data.
54 * @param count Number of cargo entities to put in this packet.
55 * @param periods_in_transit Number of cargo aging periods the cargo has been in transit.
56 * @param first_station Station the cargo was initially loaded.
57 * @param source_xy Station location the cargo was initially loaded.
58 * @param feeder_share Feeder share the packet has already accumulated.
60 CargoPacket::CargoPacket(uint16_t count
, uint16_t periods_in_transit
, StationID first_station
, TileIndex source_xy
, Money feeder_share
) :
62 periods_in_transit(periods_in_transit
),
63 feeder_share(feeder_share
),
65 first_station(first_station
)
71 * Creates a new cargo packet. Used when loading or splitting packets.
73 * @param count Number of cargo entities to put in this packet.
74 * @param feeder_share Feeder share the packet has already accumulated.
75 * @param original The original packet we are splitting.
77 CargoPacket::CargoPacket(uint16_t count
, Money feeder_share
, CargoPacket
&original
) :
79 periods_in_transit(original
.periods_in_transit
),
80 feeder_share(feeder_share
),
81 source_xy(original
.source_xy
),
82 travelled(original
.travelled
),
83 source_id(original
.source_id
),
84 source_type(original
.source_type
),
86 in_vehicle(original
.in_vehicle
),
87 #endif /* WITH_ASSERT */
88 first_station(original
.first_station
),
89 next_hop(original
.next_hop
)
95 * Split this packet in two and return the split off part.
96 * @param new_size Size of the split part.
97 * @return Split off part, or nullptr if no packet could be allocated!
99 CargoPacket
*CargoPacket::Split(uint new_size
)
101 if (!CargoPacket::CanAllocateItem()) return nullptr;
103 Money fs
= this->GetFeederShare(new_size
);
104 CargoPacket
*cp_new
= new CargoPacket(new_size
, fs
, *this);
105 this->feeder_share
-= fs
;
106 this->count
-= new_size
;
111 * Merge another packet into this one.
112 * @param cp Packet to be merged in.
114 void CargoPacket::Merge(CargoPacket
*cp
)
116 this->count
+= cp
->count
;
117 this->feeder_share
+= cp
->feeder_share
;
122 * Reduce the packet by the given amount and remove the feeder share.
123 * @param count Amount to be removed.
125 void CargoPacket::Reduce(uint count
)
127 assert(count
< this->count
);
128 this->feeder_share
-= this->GetFeederShare(count
);
129 this->count
-= count
;
133 * Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
134 * @param src_type Type of source.
135 * @param src Index of source.
137 /* static */ void CargoPacket::InvalidateAllFrom(SourceType src_type
, SourceID src
)
139 for (CargoPacket
*cp
: CargoPacket::Iterate()) {
140 if (cp
->source_type
== src_type
&& cp
->source_id
== src
) cp
->source_id
= INVALID_SOURCE
;
145 * Invalidates (sets source to INVALID_STATION) all cargo packets from given station.
146 * @param sid Station that gets removed.
148 /* static */ void CargoPacket::InvalidateAllFrom(StationID sid
)
150 for (CargoPacket
*cp
: CargoPacket::Iterate()) {
151 if (cp
->first_station
== sid
) cp
->first_station
= INVALID_STATION
;
157 * Cargo list implementation
162 * Destroy the cargolist ("frees" all cargo packets).
164 template <class Tinst
, class Tcont
>
165 CargoList
<Tinst
, Tcont
>::~CargoList()
167 for (Iterator
it(this->packets
.begin()); it
!= this->packets
.end(); ++it
) {
173 * Empty the cargo list, but don't free the cargo packets;
174 * the cargo packets are cleaned by CargoPacket's CleanPool.
176 template <class Tinst
, class Tcont
>
177 void CargoList
<Tinst
, Tcont
>::OnCleanPool()
179 this->packets
.clear();
183 * Update the cached values to reflect the removal of this packet or part of it.
184 * Decreases count and periods_in_transit.
185 * @param cp Packet to be removed from cache.
186 * @param count Amount of cargo from the given packet to be removed.
188 template <class Tinst
, class Tcont
>
189 void CargoList
<Tinst
, Tcont
>::RemoveFromCache(const CargoPacket
*cp
, uint count
)
191 assert(count
<= cp
->count
);
192 this->count
-= count
;
193 this->cargo_periods_in_transit
-= static_cast<uint64_t>(cp
->periods_in_transit
) * count
;
197 * Update the cache to reflect adding of this packet.
198 * Increases count and periods_in_transit.
199 * @param cp New packet to be inserted.
201 template <class Tinst
, class Tcont
>
202 void CargoList
<Tinst
, Tcont
>::AddToCache(const CargoPacket
*cp
)
204 this->count
+= cp
->count
;
205 this->cargo_periods_in_transit
+= static_cast<uint64_t>(cp
->periods_in_transit
) * cp
->count
;
208 /** Invalidates the cached data and rebuilds it. */
209 template <class Tinst
, class Tcont
>
210 void CargoList
<Tinst
, Tcont
>::InvalidateCache()
213 this->cargo_periods_in_transit
= 0;
215 for (ConstIterator
it(this->packets
.begin()); it
!= this->packets
.end(); it
++) {
216 static_cast<Tinst
*>(this)->AddToCache(*it
);
221 * Tries to merge the second packet into the first and return if that was
223 * @param icp Packet to be merged into.
224 * @param cp Packet to be eliminated.
225 * @return If the packets could be merged.
227 template <class Tinst
, class Tcont
>
228 /* static */ bool CargoList
<Tinst
, Tcont
>::TryMerge(CargoPacket
*icp
, CargoPacket
*cp
)
230 if (Tinst::AreMergable(icp
, cp
) &&
231 icp
->count
+ cp
->count
<= CargoPacket::MAX_COUNT
) {
241 * Vehicle cargo list implementation.
246 * Appends the given cargo packet. Tries to merge it with another one in the
247 * packets list. If no fitting packet is found, appends it. You can only append
248 * packets to the ranges of packets designated for keeping or loading.
249 * Furthermore if there are already packets reserved for loading you cannot
250 * directly add packets to the "keep" list. You first have to load the reserved
252 * @warning After appending this packet may not exist anymore!
253 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
254 * @param cp Cargo packet to add.
255 * @param action Either MTA_KEEP if you want to add the packet directly or MTA_LOAD
256 * if you want to reserve it first.
258 * @pre action == MTA_LOAD || (action == MTA_KEEP && this->designation_counts[MTA_LOAD] == 0)
260 void VehicleCargoList::Append(CargoPacket
*cp
, MoveToAction action
)
262 assert(cp
!= nullptr);
263 assert(action
== MTA_LOAD
||
264 (action
== MTA_KEEP
&& this->action_counts
[MTA_LOAD
] == 0));
265 this->AddToMeta(cp
, action
);
267 if (this->count
== cp
->count
) {
268 this->packets
.push_back(cp
);
272 uint sum
= cp
->count
;
273 for (ReverseIterator
it(this->packets
.rbegin()); it
!= this->packets
.rend(); it
++) {
274 CargoPacket
*icp
= *it
;
275 if (VehicleCargoList::TryMerge(icp
, cp
)) return;
277 if (sum
>= this->action_counts
[action
]) {
278 this->packets
.push_back(cp
);
287 * Shifts cargo from the front of the packet list and applies some action to it.
288 * @tparam Taction Action class or function to be used. It should define
289 * "bool operator()(CargoPacket *)". If true is returned the
290 * cargo packet will be removed from the list. Otherwise it
291 * will be kept and the loop will be aborted.
292 * @param action Action instance to be applied.
294 template<class Taction
>
295 void VehicleCargoList::ShiftCargo(Taction action
)
297 Iterator
it(this->packets
.begin());
298 while (it
!= this->packets
.end() && action
.MaxMove() > 0) {
299 CargoPacket
*cp
= *it
;
301 it
= this->packets
.erase(it
);
309 * Pops cargo from the back of the packet list and applies some action to it.
310 * @tparam Taction Action class or function to be used. It should define
311 * "bool operator()(CargoPacket *)". If true is returned the
312 * cargo packet will be removed from the list. Otherwise it
313 * will be kept and the loop will be aborted.
314 * @param action Action instance to be applied.
316 template<class Taction
>
317 void VehicleCargoList::PopCargo(Taction action
)
319 if (this->packets
.empty()) return;
320 Iterator
it(--(this->packets
.end()));
321 Iterator
begin(this->packets
.begin());
322 while (action
.MaxMove() > 0) {
323 CargoPacket
*cp
= *it
;
326 this->packets
.erase(it
--);
328 this->packets
.erase(it
);
338 * Update the cached values to reflect the removal of this packet or part of it.
339 * Decreases count, feeder share and periods_in_transit.
340 * @param cp Packet to be removed from cache.
341 * @param count Amount of cargo from the given packet to be removed.
343 void VehicleCargoList::RemoveFromCache(const CargoPacket
*cp
, uint count
)
345 this->feeder_share
-= cp
->GetFeederShare(count
);
346 this->Parent::RemoveFromCache(cp
, count
);
350 * Update the cache to reflect adding of this packet.
351 * Increases count, feeder share and periods_in_transit.
352 * @param cp New packet to be inserted.
354 void VehicleCargoList::AddToCache(const CargoPacket
*cp
)
356 this->feeder_share
+= cp
->feeder_share
;
357 this->Parent::AddToCache(cp
);
361 * Removes a packet or part of it from the metadata.
362 * @param cp Packet to be removed.
363 * @param action MoveToAction of the packet (for updating the counts).
364 * @param count Amount of cargo to be removed.
366 void VehicleCargoList::RemoveFromMeta(const CargoPacket
*cp
, MoveToAction action
, uint count
)
368 assert(count
<= this->action_counts
[action
]);
369 this->AssertCountConsistency();
370 this->RemoveFromCache(cp
, count
);
371 this->action_counts
[action
] -= count
;
372 this->AssertCountConsistency();
376 * Adds a packet to the metadata.
377 * @param cp Packet to be added.
378 * @param action MoveToAction of the packet.
380 void VehicleCargoList::AddToMeta(const CargoPacket
*cp
, MoveToAction action
)
382 this->AssertCountConsistency();
383 this->AddToCache(cp
);
384 this->action_counts
[action
] += cp
->count
;
385 this->AssertCountConsistency();
389 * Ages the all cargo in this list.
391 void VehicleCargoList::AgeCargo()
393 for (const auto &cp
: this->packets
) {
394 /* If we're at the maximum, then we can't increase no more. */
395 if (cp
->periods_in_transit
== UINT16_MAX
) continue;
397 cp
->periods_in_transit
++;
398 this->cargo_periods_in_transit
+= cp
->count
;
403 * Choose action to be performed with the given cargo packet.
404 * @param cp The packet.
405 * @param cargo_next Next hop the cargo wants to pass.
406 * @param current_station Current station of the vehicle carrying the cargo.
407 * @param accepted If the cargo is accepted at the current station.
408 * @param next_station Next station(s) the vehicle may stop at.
409 * @return MoveToAction to be performed.
411 /* static */ VehicleCargoList::MoveToAction
VehicleCargoList::ChooseAction(const CargoPacket
*cp
, StationID cargo_next
,
412 StationID current_station
, bool accepted
, StationIDStack next_station
)
414 if (cargo_next
== INVALID_STATION
) {
415 return (accepted
&& cp
->first_station
!= current_station
) ? MTA_DELIVER
: MTA_KEEP
;
416 } else if (cargo_next
== current_station
) {
418 } else if (next_station
.Contains(cargo_next
)) {
426 * Stages cargo for unloading. The cargo is sorted so that packets to be
427 * transferred, delivered or kept are in consecutive chunks in the list. At the
428 * same time the designation_counts are updated to reflect the size of those
430 * @param accepted If the cargo will be accepted at the station.
431 * @param current_station ID of the station.
432 * @param next_station ID of the station the vehicle will go to next.
433 * @param order_flags OrderUnloadFlags that will apply to the unload operation.
434 * @param ge GoodsEntry for getting the flows.
435 * @param cargo The cargo type of the cargo.
436 * @param payment Payment object for registering transfers.
437 * @param current_tile Current tile the cargo handling is happening on.
438 * return If any cargo will be unloaded.
440 bool VehicleCargoList::Stage(bool accepted
, StationID current_station
, StationIDStack next_station
, uint8_t order_flags
, const GoodsEntry
*ge
, CargoID cargo
, CargoPayment
*payment
, TileIndex current_tile
)
442 this->AssertCountConsistency();
443 assert(this->action_counts
[MTA_LOAD
] == 0);
444 this->action_counts
[MTA_TRANSFER
] = this->action_counts
[MTA_DELIVER
] = this->action_counts
[MTA_KEEP
] = 0;
445 Iterator deliver
= this->packets
.end();
446 Iterator it
= this->packets
.begin();
449 bool force_keep
= (order_flags
& OUFB_NO_UNLOAD
) != 0;
450 bool force_unload
= (order_flags
& OUFB_UNLOAD
) != 0;
451 bool force_transfer
= (order_flags
& (OUFB_TRANSFER
| OUFB_UNLOAD
)) != 0;
452 assert(this->count
> 0 || it
== this->packets
.end());
453 while (sum
< this->count
) {
454 CargoPacket
*cp
= *it
;
456 this->packets
.erase(it
++);
457 StationID cargo_next
= INVALID_STATION
;
458 MoveToAction action
= MTA_LOAD
;
461 } else if (force_unload
&& accepted
&& cp
->first_station
!= current_station
) {
462 action
= MTA_DELIVER
;
463 } else if (force_transfer
) {
464 action
= MTA_TRANSFER
;
465 /* We cannot send the cargo to any of the possible next hops and
466 * also not to the current station. */
467 FlowStatMap::const_iterator
flow_it(ge
->flows
.find(cp
->first_station
));
468 if (flow_it
== ge
->flows
.end()) {
469 cargo_next
= INVALID_STATION
;
471 FlowStat new_shares
= flow_it
->second
;
472 new_shares
.ChangeShare(current_station
, INT_MIN
);
473 StationIDStack excluded
= next_station
;
474 while (!excluded
.IsEmpty() && !new_shares
.GetShares()->empty()) {
475 new_shares
.ChangeShare(excluded
.Pop(), INT_MIN
);
477 if (new_shares
.GetShares()->empty()) {
478 cargo_next
= INVALID_STATION
;
480 cargo_next
= new_shares
.GetVia();
484 /* Rewrite an invalid source station to some random other one to
485 * avoid keeping the cargo in the vehicle forever. */
486 if (cp
->first_station
== INVALID_STATION
&& !ge
->flows
.empty()) {
487 cp
->first_station
= ge
->flows
.begin()->first
;
489 bool restricted
= false;
490 FlowStatMap::const_iterator
flow_it(ge
->flows
.find(cp
->first_station
));
491 if (flow_it
== ge
->flows
.end()) {
492 cargo_next
= INVALID_STATION
;
494 cargo_next
= flow_it
->second
.GetViaWithRestricted(restricted
);
496 action
= VehicleCargoList::ChooseAction(cp
, cargo_next
, current_station
, accepted
, next_station
);
497 if (restricted
&& action
== MTA_TRANSFER
) {
498 /* If the flow is restricted we can't transfer to it. Choose an
499 * unrestricted one instead. */
500 cargo_next
= flow_it
->second
.GetVia();
501 action
= VehicleCargoList::ChooseAction(cp
, cargo_next
, current_station
, accepted
, next_station
);
507 this->packets
.push_back(cp
);
508 if (deliver
== this->packets
.end()) --deliver
;
511 this->packets
.insert(deliver
, cp
);
514 this->packets
.push_front(cp
);
515 /* Add feeder share here to allow reusing field for next station. */
516 share
= payment
->PayTransfer(cargo
, cp
, cp
->count
, current_tile
);
517 cp
->AddFeederShare(share
);
518 this->feeder_share
+= share
;
519 cp
->next_hop
= cargo_next
;
524 this->action_counts
[action
] += cp
->count
;
527 this->AssertCountConsistency();
528 return this->action_counts
[MTA_DELIVER
] > 0 || this->action_counts
[MTA_TRANSFER
] > 0;
531 /** Invalidates the cached data and rebuild it. */
532 void VehicleCargoList::InvalidateCache()
534 this->feeder_share
= 0;
535 this->Parent::InvalidateCache();
539 * Moves some cargo from one designation to another. You can only move
540 * between adjacent designations. E.g. you can keep cargo that was previously
541 * reserved (MTA_LOAD), but you can't reserve cargo that's marked as to be
542 * delivered. Furthermore, as this method doesn't change the actual packets,
543 * you cannot move cargo from or to MTA_TRANSFER. You need a specialized
544 * template method for that.
545 * @tparam from Previous designation of cargo.
546 * @tparam to New designation of cargo.
547 * @param max_move Maximum amount of cargo to reassign.
548 * @return Amount of cargo actually reassigned.
550 template<VehicleCargoList::MoveToAction Tfrom
, VehicleCargoList::MoveToAction Tto
>
551 uint
VehicleCargoList::Reassign(uint max_move
)
553 static_assert(Tfrom
!= MTA_TRANSFER
&& Tto
!= MTA_TRANSFER
);
554 static_assert(Tfrom
- Tto
== 1 || Tto
- Tfrom
== 1);
555 max_move
= std::min(this->action_counts
[Tfrom
], max_move
);
556 this->action_counts
[Tfrom
] -= max_move
;
557 this->action_counts
[Tto
] += max_move
;
562 * Reassign cargo from MTA_DELIVER to MTA_TRANSFER and take care of the next
563 * station the cargo wants to visit.
564 * @param max_move Maximum amount of cargo to reassign.
565 * @return Amount of cargo actually reassigned.
568 uint
VehicleCargoList::Reassign
<VehicleCargoList::MTA_DELIVER
, VehicleCargoList::MTA_TRANSFER
>(uint max_move
)
570 max_move
= std::min(this->action_counts
[MTA_DELIVER
], max_move
);
573 for (Iterator
it(this->packets
.begin()); sum
< this->action_counts
[MTA_TRANSFER
] + max_move
;) {
574 CargoPacket
*cp
= *it
++;
576 if (sum
<= this->action_counts
[MTA_TRANSFER
]) continue;
577 if (sum
> this->action_counts
[MTA_TRANSFER
] + max_move
) {
578 CargoPacket
*cp_split
= cp
->Split(sum
- this->action_counts
[MTA_TRANSFER
] + max_move
);
579 sum
-= cp_split
->Count();
580 this->packets
.insert(it
, cp_split
);
582 cp
->next_hop
= INVALID_STATION
;
585 this->action_counts
[MTA_DELIVER
] -= max_move
;
586 this->action_counts
[MTA_TRANSFER
] += max_move
;
591 * Returns reserved cargo to the station and removes it from the cache.
592 * @param max_move Maximum amount of cargo to move.
593 * @param dest Station the cargo is returned to.
594 * @param next ID of the next station the cargo wants to go to.
595 * @param current_tile Current tile the cargo handling is happening on.
596 * @return Amount of cargo actually returned.
598 uint
VehicleCargoList::Return(uint max_move
, StationCargoList
*dest
, StationID next
, TileIndex current_tile
)
600 max_move
= std::min(this->action_counts
[MTA_LOAD
], max_move
);
601 this->PopCargo(CargoReturn(this, dest
, max_move
, next
, current_tile
));
606 * Shifts cargo between two vehicles.
607 * @param dest Other vehicle's cargo list.
608 * @param max_move Maximum amount of cargo to be moved.
609 * @return Amount of cargo actually moved.
611 uint
VehicleCargoList::Shift(uint max_move
, VehicleCargoList
*dest
)
613 max_move
= std::min(this->count
, max_move
);
614 this->PopCargo(CargoShift(this, dest
, max_move
));
619 * Unloads cargo at the given station. Deliver or transfer, depending on the
620 * ranges defined by designation_counts.
621 * @param dest StationCargoList to add transferred cargo to.
622 * @param max_move Maximum amount of cargo to move.
623 * @param cargo The cargo type of the cargo.
624 * @param payment Payment object to register payments in.
625 * @param current_tile Current tile the cargo handling is happening on.
626 * @return Amount of cargo actually unloaded.
628 uint
VehicleCargoList::Unload(uint max_move
, StationCargoList
*dest
, CargoID cargo
, CargoPayment
*payment
, TileIndex current_tile
)
631 if (this->action_counts
[MTA_TRANSFER
] > 0) {
632 uint move
= std::min(this->action_counts
[MTA_TRANSFER
], max_move
);
633 this->ShiftCargo(CargoTransfer(this, dest
, move
, current_tile
));
636 if (this->action_counts
[MTA_TRANSFER
] == 0 && this->action_counts
[MTA_DELIVER
] > 0 && moved
< max_move
) {
637 uint move
= std::min(this->action_counts
[MTA_DELIVER
], max_move
- moved
);
638 this->ShiftCargo(CargoDelivery(this, move
, cargo
, payment
, current_tile
));
645 * Truncates the cargo in this list to the given amount. It leaves the
646 * first cargo entities and removes max_move from the back of the list.
647 * @param max_move Maximum amount of entities to be removed from the list.
648 * @return Amount of entities actually moved.
650 uint
VehicleCargoList::Truncate(uint max_move
)
652 max_move
= std::min(this->count
, max_move
);
653 this->PopCargo(CargoRemoval
<VehicleCargoList
>(this, max_move
));
658 * Routes packets with station "avoid" as next hop to a different place.
659 * @param max_move Maximum amount of cargo to move.
660 * @param dest List to prepend the cargo to.
661 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
662 * @param avoid2 Additional station to exclude from routing.
663 * @param ge GoodsEntry to get the routing info from.
665 uint
VehicleCargoList::Reroute(uint max_move
, VehicleCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
)
667 max_move
= std::min(this->action_counts
[MTA_TRANSFER
], max_move
);
668 this->ShiftCargo(VehicleCargoReroute(this, dest
, max_move
, avoid
, avoid2
, ge
));
674 * Station cargo list implementation.
679 * Appends the given cargo packet to the range of packets with the same next station
680 * @warning After appending this packet may not exist anymore!
681 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
682 * @param next the next hop
683 * @param cp the cargo packet to add
686 void StationCargoList::Append(CargoPacket
*cp
, StationID next
)
688 assert(cp
!= nullptr);
689 this->AddToCache(cp
);
691 StationCargoPacketMap::List
&list
= this->packets
[next
];
692 for (StationCargoPacketMap::List::reverse_iterator
it(list
.rbegin());
693 it
!= list
.rend(); it
++) {
694 if (StationCargoList::TryMerge(*it
, cp
)) return;
697 /* The packet could not be merged with another one */
702 * Shifts cargo from the front of the packet list for a specific station and
703 * applies some action to it.
704 * @tparam Taction Action class or function to be used. It should define
705 * "bool operator()(CargoPacket *)". If true is returned the
706 * cargo packet will be removed from the list. Otherwise it
707 * will be kept and the loop will be aborted.
708 * @param action Action instance to be applied.
709 * @param next Next hop the cargo wants to visit.
710 * @return True if all packets with the given next hop have been removed,
713 template <class Taction
>
714 bool StationCargoList::ShiftCargo(Taction
&action
, StationID next
)
716 std::pair
<Iterator
, Iterator
> range(this->packets
.equal_range(next
));
717 for (Iterator
it(range
.first
); it
!= range
.second
&& it
.GetKey() == next
;) {
718 if (action
.MaxMove() == 0) return false;
719 CargoPacket
*cp
= *it
;
721 it
= this->packets
.erase(it
);
730 * Shifts cargo from the front of the packet list for a specific station and
731 * and optional also from the list for "any station", then applies some action
733 * @tparam Taction Action class or function to be used. It should define
734 * "bool operator()(CargoPacket *)". If true is returned the
735 * cargo packet will be removed from the list. Otherwise it
736 * will be kept and the loop will be aborted.
737 * @param action Action instance to be applied.
738 * @param next Next hop the cargo wants to visit.
739 * @param include_invalid If cargo from the INVALID_STATION list should be
741 * @return Amount of cargo actually moved.
743 template <class Taction
>
744 uint
StationCargoList::ShiftCargo(Taction action
, StationIDStack next
, bool include_invalid
)
746 uint max_move
= action
.MaxMove();
747 while (!next
.IsEmpty()) {
748 this->ShiftCargo(action
, next
.Pop());
749 if (action
.MaxMove() == 0) break;
751 if (include_invalid
&& action
.MaxMove() > 0) {
752 this->ShiftCargo(action
, INVALID_STATION
);
754 return max_move
- action
.MaxMove();
758 * Truncates where each destination loses roughly the same percentage of its
759 * cargo. This is done by randomizing the selection of packets to be removed.
760 * Optionally count the cargo by origin station.
761 * @param max_move Maximum amount of cargo to remove.
762 * @param cargo_per_source Container for counting the cargo by origin.
763 * @return Amount of cargo actually moved.
765 uint
StationCargoList::Truncate(uint max_move
, StationCargoAmountMap
*cargo_per_source
)
767 max_move
= std::min(max_move
, this->count
);
768 uint prev_count
= this->count
;
771 bool do_count
= cargo_per_source
!= nullptr;
772 while (max_move
> moved
) {
773 for (Iterator
it(this->packets
.begin()); it
!= this->packets
.end();) {
774 CargoPacket
*cp
= *it
;
775 if (prev_count
> max_move
&& RandomRange(prev_count
) < prev_count
- max_move
) {
776 if (do_count
&& loop
== 0) {
777 (*cargo_per_source
)[cp
->first_station
] += cp
->count
;
782 uint diff
= max_move
- moved
;
783 if (cp
->count
> diff
) {
785 this->RemoveFromCache(cp
, diff
);
790 if (do_count
) (*cargo_per_source
)[cp
->first_station
] -= diff
;
793 if (do_count
) (*cargo_per_source
)[cp
->first_station
] += cp
->count
;
797 it
= this->packets
.erase(it
);
798 if (do_count
&& loop
> 0) {
799 (*cargo_per_source
)[cp
->first_station
] -= cp
->count
;
802 this->RemoveFromCache(cp
, cp
->count
);
812 * Reserves cargo for loading onto the vehicle.
813 * @param max_move Maximum amount of cargo to reserve.
814 * @param dest VehicleCargoList to reserve for.
815 * @param next_station Next station(s) the loading vehicle will visit.
816 * @param current_tile Current tile the cargo handling is happening on.
817 * @return Amount of cargo actually reserved.
819 uint
StationCargoList::Reserve(uint max_move
, VehicleCargoList
*dest
, StationIDStack next_station
, TileIndex current_tile
)
821 return this->ShiftCargo(CargoReservation(this, dest
, max_move
, current_tile
), next_station
, true);
825 * Loads cargo onto a vehicle. If the vehicle has reserved cargo load that.
826 * Otherwise load cargo from the station.
827 * @param max_move Amount of cargo to load.
828 * @param dest Vehicle cargo list where the cargo resides.
829 * @param next_station Next station(s) the loading vehicle will visit.
830 * @param current_tile Current tile the cargo handling is happening on.
831 * @return Amount of cargo actually loaded.
832 * @note Vehicles may or may not reserve, depending on their orders. The two
833 * modes of loading are exclusive, though. If cargo is reserved we don't
834 * need to load unreserved cargo.
836 uint
StationCargoList::Load(uint max_move
, VehicleCargoList
*dest
, StationIDStack next_station
, TileIndex current_tile
)
838 uint move
= std::min(dest
->ActionCount(VehicleCargoList::MTA_LOAD
), max_move
);
840 this->reserved_count
-= move
;
841 dest
->Reassign
<VehicleCargoList::MTA_LOAD
, VehicleCargoList::MTA_KEEP
>(move
);
844 return this->ShiftCargo(CargoLoad(this, dest
, max_move
, current_tile
), next_station
, true);
849 * Routes packets with station "avoid" as next hop to a different place.
850 * @param max_move Maximum amount of cargo to move.
851 * @param dest List to append the cargo to.
852 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
853 * @param avoid2 Additional station to exclude from routing.
854 * @param ge GoodsEntry to get the routing info from.
856 uint
StationCargoList::Reroute(uint max_move
, StationCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
)
858 return this->ShiftCargo(StationCargoReroute(this, dest
, max_move
, avoid
, avoid2
, ge
), avoid
, false);
862 * We have to instantiate everything we want to be usable.
864 template class CargoList
<VehicleCargoList
, CargoPacketList
>;
865 template class CargoList
<StationCargoList
, StationCargoPacketMap
>;
866 template uint
VehicleCargoList::Reassign
<VehicleCargoList::MTA_DELIVER
, VehicleCargoList::MTA_KEEP
>(uint
);