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
= ST_INDUSTRY
;
30 this->source_id
= INVALID_SOURCE
;
34 * Creates a new cargo packet.
35 * @param source Source station of the packet.
36 * @param source_xy Source location 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).
41 * @note We have to zero memory ourselves here because we are using a 'new'
42 * that, in contrary to all other pools, does not memset to 0.
44 CargoPacket::CargoPacket(StationID source
, TileIndex source_xy
, uint16 count
, SourceType source_type
, SourceID source_id
) :
54 this->source_type
= source_type
;
58 * Creates a new cargo packet. Initializes the fields that cannot be changed later.
59 * Used when loading or splitting packets.
60 * @param count Number of cargo entities to put in this packet.
61 * @param days_in_transit Number of days the cargo has been in transit.
62 * @param source Station the cargo was initially loaded.
63 * @param source_xy Station location the cargo was initially loaded.
64 * @param loaded_at_xy Location the cargo was loaded last.
65 * @param feeder_share Feeder share the packet has already accumulated.
66 * @param source_type 'Type' of source the packet comes from (for subsidies).
67 * @param source_id Actual source of the packet (for subsidies).
68 * @note We have to zero memory ourselves here because we are using a 'new'
69 * that, in contrary to all other pools, does not memset to 0.
71 CargoPacket::CargoPacket(uint16 count
, byte days_in_transit
, StationID source
, TileIndex source_xy
, TileIndex loaded_at_xy
, Money feeder_share
, SourceType source_type
, SourceID source_id
) :
72 feeder_share(feeder_share
),
74 days_in_transit(days_in_transit
),
78 loaded_at_xy(loaded_at_xy
)
81 this->source_type
= source_type
;
85 * Split this packet in two and return the split off part.
86 * @param new_size Size of the split part.
87 * @return Split off part, or nullptr if no packet could be allocated!
89 CargoPacket
*CargoPacket::Split(uint new_size
)
91 if (!CargoPacket::CanAllocateItem()) return nullptr;
93 Money fs
= this->FeederShare(new_size
);
94 CargoPacket
*cp_new
= new CargoPacket(new_size
, this->days_in_transit
, this->source
, this->source_xy
, this->loaded_at_xy
, fs
, this->source_type
, this->source_id
);
95 this->feeder_share
-= fs
;
96 this->count
-= new_size
;
101 * Merge another packet into this one.
102 * @param cp Packet to be merged in.
104 void CargoPacket::Merge(CargoPacket
*cp
)
106 this->count
+= cp
->count
;
107 this->feeder_share
+= cp
->feeder_share
;
112 * Reduce the packet by the given amount and remove the feeder share.
113 * @param count Amount to be removed.
115 void CargoPacket::Reduce(uint count
)
117 assert(count
< this->count
);
118 this->feeder_share
-= this->FeederShare(count
);
119 this->count
-= count
;
123 * Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
124 * @param src_type Type of source.
125 * @param src Index of source.
127 /* static */ void CargoPacket::InvalidateAllFrom(SourceType src_type
, SourceID src
)
129 for (CargoPacket
*cp
: CargoPacket::Iterate()) {
130 if (cp
->source_type
== src_type
&& cp
->source_id
== src
) cp
->source_id
= INVALID_SOURCE
;
135 * Invalidates (sets source to INVALID_STATION) all cargo packets from given station.
136 * @param sid Station that gets removed.
138 /* static */ void CargoPacket::InvalidateAllFrom(StationID sid
)
140 for (CargoPacket
*cp
: CargoPacket::Iterate()) {
141 if (cp
->source
== sid
) cp
->source
= INVALID_STATION
;
147 * Cargo list implementation
152 * Destroy the cargolist ("frees" all cargo packets).
154 template <class Tinst
, class Tcont
>
155 CargoList
<Tinst
, Tcont
>::~CargoList()
157 for (Iterator
it(this->packets
.begin()); it
!= this->packets
.end(); ++it
) {
163 * Empty the cargo list, but don't free the cargo packets;
164 * the cargo packets are cleaned by CargoPacket's CleanPool.
166 template <class Tinst
, class Tcont
>
167 void CargoList
<Tinst
, Tcont
>::OnCleanPool()
169 this->packets
.clear();
173 * Update the cached values to reflect the removal of this packet or part of it.
174 * Decreases count and days_in_transit.
175 * @param cp Packet to be removed from cache.
176 * @param count Amount of cargo from the given packet to be removed.
178 template <class Tinst
, class Tcont
>
179 void CargoList
<Tinst
, Tcont
>::RemoveFromCache(const CargoPacket
*cp
, uint count
)
181 assert(count
<= cp
->count
);
182 this->count
-= count
;
183 this->cargo_days_in_transit
-= cp
->days_in_transit
* count
;
187 * Update the cache to reflect adding of this packet.
188 * Increases count and days_in_transit.
189 * @param cp New packet to be inserted.
191 template <class Tinst
, class Tcont
>
192 void CargoList
<Tinst
, Tcont
>::AddToCache(const CargoPacket
*cp
)
194 this->count
+= cp
->count
;
195 this->cargo_days_in_transit
+= cp
->days_in_transit
* cp
->count
;
198 /** Invalidates the cached data and rebuilds it. */
199 template <class Tinst
, class Tcont
>
200 void CargoList
<Tinst
, Tcont
>::InvalidateCache()
203 this->cargo_days_in_transit
= 0;
205 for (ConstIterator
it(this->packets
.begin()); it
!= this->packets
.end(); it
++) {
206 static_cast<Tinst
*>(this)->AddToCache(*it
);
211 * Tries to merge the second packet into the first and return if that was
213 * @param icp Packet to be merged into.
214 * @param cp Packet to be eliminated.
215 * @return If the packets could be merged.
217 template <class Tinst
, class Tcont
>
218 /* static */ bool CargoList
<Tinst
, Tcont
>::TryMerge(CargoPacket
*icp
, CargoPacket
*cp
)
220 if (Tinst::AreMergable(icp
, cp
) &&
221 icp
->count
+ cp
->count
<= CargoPacket::MAX_COUNT
) {
231 * Vehicle cargo list implementation.
236 * Appends the given cargo packet. Tries to merge it with another one in the
237 * packets list. If no fitting packet is found, appends it. You can only append
238 * packets to the ranges of packets designated for keeping or loading.
239 * Furthermore if there are already packets reserved for loading you cannot
240 * directly add packets to the "keep" list. You first have to load the reserved
242 * @warning After appending this packet may not exist anymore!
243 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
244 * @param cp Cargo packet to add.
245 * @param action Either MTA_KEEP if you want to add the packet directly or MTA_LOAD
246 * if you want to reserve it first.
248 * @pre action == MTA_LOAD || (action == MTA_KEEP && this->designation_counts[MTA_LOAD] == 0)
250 void VehicleCargoList::Append(CargoPacket
*cp
, MoveToAction action
)
252 assert(cp
!= nullptr);
253 assert(action
== MTA_LOAD
||
254 (action
== MTA_KEEP
&& this->action_counts
[MTA_LOAD
] == 0));
255 this->AddToMeta(cp
, action
);
257 if (this->count
== cp
->count
) {
258 this->packets
.push_back(cp
);
262 uint sum
= cp
->count
;
263 for (ReverseIterator
it(this->packets
.rbegin()); it
!= this->packets
.rend(); it
++) {
264 CargoPacket
*icp
= *it
;
265 if (VehicleCargoList::TryMerge(icp
, cp
)) return;
267 if (sum
>= this->action_counts
[action
]) {
268 this->packets
.push_back(cp
);
277 * Shifts cargo from the front of the packet list and applies some action to it.
278 * @tparam Taction Action class or function to be used. It should define
279 * "bool operator()(CargoPacket *)". If true is returned the
280 * cargo packet will be removed from the list. Otherwise it
281 * will be kept and the loop will be aborted.
282 * @param action Action instance to be applied.
284 template<class Taction
>
285 void VehicleCargoList::ShiftCargo(Taction action
)
287 Iterator
it(this->packets
.begin());
288 while (it
!= this->packets
.end() && action
.MaxMove() > 0) {
289 CargoPacket
*cp
= *it
;
291 it
= this->packets
.erase(it
);
299 * Pops cargo from the back of the packet list and applies some action to it.
300 * @tparam Taction Action class or function to be used. It should define
301 * "bool operator()(CargoPacket *)". If true is returned the
302 * cargo packet will be removed from the list. Otherwise it
303 * will be kept and the loop will be aborted.
304 * @param action Action instance to be applied.
306 template<class Taction
>
307 void VehicleCargoList::PopCargo(Taction action
)
309 if (this->packets
.empty()) return;
310 Iterator
it(--(this->packets
.end()));
311 Iterator
begin(this->packets
.begin());
312 while (action
.MaxMove() > 0) {
313 CargoPacket
*cp
= *it
;
316 this->packets
.erase(it
--);
318 this->packets
.erase(it
);
328 * Update the cached values to reflect the removal of this packet or part of it.
329 * Decreases count, feeder share and days_in_transit.
330 * @param cp Packet to be removed from cache.
331 * @param count Amount of cargo from the given packet to be removed.
333 void VehicleCargoList::RemoveFromCache(const CargoPacket
*cp
, uint count
)
335 this->feeder_share
-= cp
->FeederShare(count
);
336 this->Parent::RemoveFromCache(cp
, count
);
340 * Update the cache to reflect adding of this packet.
341 * Increases count, feeder share and days_in_transit.
342 * @param cp New packet to be inserted.
344 void VehicleCargoList::AddToCache(const CargoPacket
*cp
)
346 this->feeder_share
+= cp
->feeder_share
;
347 this->Parent::AddToCache(cp
);
351 * Removes a packet or part of it from the metadata.
352 * @param cp Packet to be removed.
353 * @param action MoveToAction of the packet (for updating the counts).
354 * @param count Amount of cargo to be removed.
356 void VehicleCargoList::RemoveFromMeta(const CargoPacket
*cp
, MoveToAction action
, uint count
)
358 assert(count
<= this->action_counts
[action
]);
359 this->AssertCountConsistency();
360 this->RemoveFromCache(cp
, count
);
361 this->action_counts
[action
] -= count
;
362 this->AssertCountConsistency();
366 * Adds a packet to the metadata.
367 * @param cp Packet to be added.
368 * @param action MoveToAction of the packet.
370 void VehicleCargoList::AddToMeta(const CargoPacket
*cp
, MoveToAction action
)
372 this->AssertCountConsistency();
373 this->AddToCache(cp
);
374 this->action_counts
[action
] += cp
->count
;
375 this->AssertCountConsistency();
379 * Ages the all cargo in this list.
381 void VehicleCargoList::AgeCargo()
383 for (ConstIterator
it(this->packets
.begin()); it
!= this->packets
.end(); it
++) {
384 CargoPacket
*cp
= *it
;
385 /* If we're at the maximum, then we can't increase no more. */
386 if (cp
->days_in_transit
== 0xFF) continue;
388 cp
->days_in_transit
++;
389 this->cargo_days_in_transit
+= cp
->count
;
394 * Sets loaded_at_xy to the current station for all cargo to be transferred.
395 * This is done when stopping or skipping while the vehicle is unloading. In
396 * that case the vehicle will get part of its transfer credits early and it may
397 * get more transfer credits than it's entitled to.
398 * @param xy New loaded_at_xy for the cargo.
400 void VehicleCargoList::SetTransferLoadPlace(TileIndex xy
)
403 for (Iterator it
= this->packets
.begin(); sum
< this->action_counts
[MTA_TRANSFER
]; ++it
) {
404 CargoPacket
*cp
= *it
;
405 cp
->loaded_at_xy
= xy
;
411 * Choose action to be performed with the given cargo packet.
412 * @param cp The packet.
413 * @param cargo_next Next hop the cargo wants to pass.
414 * @param current_station Current station of the vehicle carrying the cargo.
415 * @param accepted If the cargo is accepted at the current station.
416 * @param next_station Next station(s) the vehicle may stop at.
417 * @return MoveToAction to be performed.
419 /* static */ VehicleCargoList::MoveToAction
VehicleCargoList::ChooseAction(const CargoPacket
*cp
, StationID cargo_next
,
420 StationID current_station
, bool accepted
, StationIDStack next_station
)
422 if (cargo_next
== INVALID_STATION
) {
423 return (accepted
&& cp
->source
!= current_station
) ? MTA_DELIVER
: MTA_KEEP
;
424 } else if (cargo_next
== current_station
) {
426 } else if (next_station
.Contains(cargo_next
)) {
434 * Stages cargo for unloading. The cargo is sorted so that packets to be
435 * transferred, delivered or kept are in consecutive chunks in the list. At the
436 * same time the designation_counts are updated to reflect the size of those
438 * @param accepted If the cargo will be accepted at the station.
439 * @param current_station ID of the station.
440 * @param next_station ID of the station the vehicle will go to next.
441 * @param order_flags OrderUnloadFlags that will apply to the unload operation.
442 * @param ge GoodsEntry for getting the flows.
443 * @param payment Payment object for registering transfers.
444 * return If any cargo will be unloaded.
446 bool VehicleCargoList::Stage(bool accepted
, StationID current_station
, StationIDStack next_station
, uint8 order_flags
, const GoodsEntry
*ge
, CargoPayment
*payment
)
448 this->AssertCountConsistency();
449 assert(this->action_counts
[MTA_LOAD
] == 0);
450 this->action_counts
[MTA_TRANSFER
] = this->action_counts
[MTA_DELIVER
] = this->action_counts
[MTA_KEEP
] = 0;
451 Iterator deliver
= this->packets
.end();
452 Iterator it
= this->packets
.begin();
455 bool force_keep
= (order_flags
& OUFB_NO_UNLOAD
) != 0;
456 bool force_unload
= (order_flags
& OUFB_UNLOAD
) != 0;
457 bool force_transfer
= (order_flags
& (OUFB_TRANSFER
| OUFB_UNLOAD
)) != 0;
458 assert(this->count
> 0 || it
== this->packets
.end());
459 while (sum
< this->count
) {
460 CargoPacket
*cp
= *it
;
462 this->packets
.erase(it
++);
463 StationID cargo_next
= INVALID_STATION
;
464 MoveToAction action
= MTA_LOAD
;
467 } else if (force_unload
&& accepted
&& cp
->source
!= current_station
) {
468 action
= MTA_DELIVER
;
469 } else if (force_transfer
) {
470 action
= MTA_TRANSFER
;
471 /* We cannot send the cargo to any of the possible next hops and
472 * also not to the current station. */
473 FlowStatMap::const_iterator
flow_it(ge
->flows
.find(cp
->source
));
474 if (flow_it
== ge
->flows
.end()) {
475 cargo_next
= INVALID_STATION
;
477 FlowStat new_shares
= flow_it
->second
;
478 new_shares
.ChangeShare(current_station
, INT_MIN
);
479 StationIDStack excluded
= next_station
;
480 while (!excluded
.IsEmpty() && !new_shares
.GetShares()->empty()) {
481 new_shares
.ChangeShare(excluded
.Pop(), INT_MIN
);
483 if (new_shares
.GetShares()->empty()) {
484 cargo_next
= INVALID_STATION
;
486 cargo_next
= new_shares
.GetVia();
490 /* Rewrite an invalid source station to some random other one to
491 * avoid keeping the cargo in the vehicle forever. */
492 if (cp
->source
== INVALID_STATION
&& !ge
->flows
.empty()) {
493 cp
->source
= ge
->flows
.begin()->first
;
495 bool restricted
= false;
496 FlowStatMap::const_iterator
flow_it(ge
->flows
.find(cp
->source
));
497 if (flow_it
== ge
->flows
.end()) {
498 cargo_next
= INVALID_STATION
;
500 cargo_next
= flow_it
->second
.GetViaWithRestricted(restricted
);
502 action
= VehicleCargoList::ChooseAction(cp
, cargo_next
, current_station
, accepted
, next_station
);
503 if (restricted
&& action
== MTA_TRANSFER
) {
504 /* If the flow is restricted we can't transfer to it. Choose an
505 * unrestricted one instead. */
506 cargo_next
= flow_it
->second
.GetVia();
507 action
= VehicleCargoList::ChooseAction(cp
, cargo_next
, current_station
, accepted
, next_station
);
513 this->packets
.push_back(cp
);
514 if (deliver
== this->packets
.end()) --deliver
;
517 this->packets
.insert(deliver
, cp
);
520 this->packets
.push_front(cp
);
521 /* Add feeder share here to allow reusing field for next station. */
522 share
= payment
->PayTransfer(cp
, cp
->count
);
523 cp
->AddFeederShare(share
);
524 this->feeder_share
+= share
;
525 cp
->next_station
= cargo_next
;
530 this->action_counts
[action
] += cp
->count
;
533 this->AssertCountConsistency();
534 return this->action_counts
[MTA_DELIVER
] > 0 || this->action_counts
[MTA_TRANSFER
] > 0;
537 /** Invalidates the cached data and rebuild it. */
538 void VehicleCargoList::InvalidateCache()
540 this->feeder_share
= 0;
541 this->Parent::InvalidateCache();
545 * Moves some cargo from one designation to another. You can only move
546 * between adjacent designations. E.g. you can keep cargo that was previously
547 * reserved (MTA_LOAD), but you can't reserve cargo that's marked as to be
548 * delivered. Furthermore, as this method doesn't change the actual packets,
549 * you cannot move cargo from or to MTA_TRANSFER. You need a specialized
550 * template method for that.
551 * @tparam from Previous designation of cargo.
552 * @tparam to New designation of cargo.
553 * @param max_move Maximum amount of cargo to reassign.
554 * @return Amount of cargo actually reassigned.
556 template<VehicleCargoList::MoveToAction Tfrom
, VehicleCargoList::MoveToAction Tto
>
557 uint
VehicleCargoList::Reassign(uint max_move
, TileOrStationID
)
559 assert_tcompile(Tfrom
!= MTA_TRANSFER
&& Tto
!= MTA_TRANSFER
);
560 assert_tcompile(Tfrom
- Tto
== 1 || Tto
- Tfrom
== 1);
561 max_move
= min(this->action_counts
[Tfrom
], max_move
);
562 this->action_counts
[Tfrom
] -= max_move
;
563 this->action_counts
[Tto
] += max_move
;
568 * Reassign cargo from MTA_DELIVER to MTA_TRANSFER and take care of the next
569 * station the cargo wants to visit.
570 * @param max_move Maximum amount of cargo to reassign.
571 * @param next_station Station to record as next hop in the reassigned packets.
572 * @return Amount of cargo actually reassigned.
575 uint
VehicleCargoList::Reassign
<VehicleCargoList::MTA_DELIVER
, VehicleCargoList::MTA_TRANSFER
>(uint max_move
, TileOrStationID next_station
)
577 max_move
= min(this->action_counts
[MTA_DELIVER
], max_move
);
580 for (Iterator
it(this->packets
.begin()); sum
< this->action_counts
[MTA_TRANSFER
] + max_move
;) {
581 CargoPacket
*cp
= *it
++;
583 if (sum
<= this->action_counts
[MTA_TRANSFER
]) continue;
584 if (sum
> this->action_counts
[MTA_TRANSFER
] + max_move
) {
585 CargoPacket
*cp_split
= cp
->Split(sum
- this->action_counts
[MTA_TRANSFER
] + max_move
);
586 sum
-= cp_split
->Count();
587 this->packets
.insert(it
, cp_split
);
589 cp
->next_station
= next_station
;
592 this->action_counts
[MTA_DELIVER
] -= max_move
;
593 this->action_counts
[MTA_TRANSFER
] += max_move
;
598 * Returns reserved cargo to the station and removes it from the cache.
599 * @param max_move Maximum amount of cargo to move.
600 * @param dest Station the cargo is returned to.
601 * @param next ID of the next station the cargo wants to go to.
602 * @return Amount of cargo actually returned.
604 uint
VehicleCargoList::Return(uint max_move
, StationCargoList
*dest
, StationID next
)
606 max_move
= min(this->action_counts
[MTA_LOAD
], max_move
);
607 this->PopCargo(CargoReturn(this, dest
, max_move
, next
));
612 * Shifts cargo between two vehicles.
613 * @param dest Other vehicle's cargo list.
614 * @param max_move Maximum amount of cargo to be moved.
615 * @return Amount of cargo actually moved.
617 uint
VehicleCargoList::Shift(uint max_move
, VehicleCargoList
*dest
)
619 max_move
= min(this->count
, max_move
);
620 this->PopCargo(CargoShift(this, dest
, max_move
));
625 * Unloads cargo at the given station. Deliver or transfer, depending on the
626 * ranges defined by designation_counts.
627 * @param dest StationCargoList to add transferred cargo to.
628 * @param max_move Maximum amount of cargo to move.
629 * @param payment Payment object to register payments in.
630 * @return Amount of cargo actually unloaded.
632 uint
VehicleCargoList::Unload(uint max_move
, StationCargoList
*dest
, CargoPayment
*payment
)
635 if (this->action_counts
[MTA_TRANSFER
] > 0) {
636 uint move
= min(this->action_counts
[MTA_TRANSFER
], max_move
);
637 this->ShiftCargo(CargoTransfer(this, dest
, move
));
640 if (this->action_counts
[MTA_TRANSFER
] == 0 && this->action_counts
[MTA_DELIVER
] > 0 && moved
< max_move
) {
641 uint move
= min(this->action_counts
[MTA_DELIVER
], max_move
- moved
);
642 this->ShiftCargo(CargoDelivery(this, move
, payment
));
649 * Truncates the cargo in this list to the given amount. It leaves the
650 * first cargo entities and removes max_move from the back of the list.
651 * @param max_move Maximum amount of entities to be removed from the list.
652 * @return Amount of entities actually moved.
654 uint
VehicleCargoList::Truncate(uint max_move
)
656 max_move
= min(this->count
, max_move
);
657 this->PopCargo(CargoRemoval
<VehicleCargoList
>(this, max_move
));
662 * Routes packets with station "avoid" as next hop to a different place.
663 * @param max_move Maximum amount of cargo to move.
664 * @param dest List to prepend the cargo to.
665 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
666 * @param avoid2 Additional station to exclude from routing.
667 * @param ge GoodsEntry to get the routing info from.
669 uint
VehicleCargoList::Reroute(uint max_move
, VehicleCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
)
671 max_move
= min(this->action_counts
[MTA_TRANSFER
], max_move
);
672 this->ShiftCargo(VehicleCargoReroute(this, dest
, max_move
, avoid
, avoid2
, ge
));
678 * Station cargo list implementation.
683 * Appends the given cargo packet to the range of packets with the same next station
684 * @warning After appending this packet may not exist anymore!
685 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
686 * @param next the next hop
687 * @param cp the cargo packet to add
690 void StationCargoList::Append(CargoPacket
*cp
, StationID next
)
692 assert(cp
!= nullptr);
693 this->AddToCache(cp
);
695 StationCargoPacketMap::List
&list
= this->packets
[next
];
696 for (StationCargoPacketMap::List::reverse_iterator
it(list
.rbegin());
697 it
!= list
.rend(); it
++) {
698 if (StationCargoList::TryMerge(*it
, cp
)) return;
701 /* The packet could not be merged with another one */
706 * Shifts cargo from the front of the packet list for a specific station and
707 * applies some action to it.
708 * @tparam Taction Action class or function to be used. It should define
709 * "bool operator()(CargoPacket *)". If true is returned the
710 * cargo packet will be removed from the list. Otherwise it
711 * will be kept and the loop will be aborted.
712 * @param action Action instance to be applied.
713 * @param next Next hop the cargo wants to visit.
714 * @return True if all packets with the given next hop have been removed,
717 template <class Taction
>
718 bool StationCargoList::ShiftCargo(Taction
&action
, StationID next
)
720 std::pair
<Iterator
, Iterator
> range(this->packets
.equal_range(next
));
721 for (Iterator
it(range
.first
); it
!= range
.second
&& it
.GetKey() == next
;) {
722 if (action
.MaxMove() == 0) return false;
723 CargoPacket
*cp
= *it
;
725 it
= this->packets
.erase(it
);
734 * Shifts cargo from the front of the packet list for a specific station and
735 * and optional also from the list for "any station", then applies some action
737 * @tparam Taction Action class or function to be used. It should define
738 * "bool operator()(CargoPacket *)". If true is returned the
739 * cargo packet will be removed from the list. Otherwise it
740 * will be kept and the loop will be aborted.
741 * @param action Action instance to be applied.
742 * @param next Next hop the cargo wants to visit.
743 * @param include_invalid If cargo from the INVALID_STATION list should be
745 * @return Amount of cargo actually moved.
747 template <class Taction
>
748 uint
StationCargoList::ShiftCargo(Taction action
, StationIDStack next
, bool include_invalid
)
750 uint max_move
= action
.MaxMove();
751 while (!next
.IsEmpty()) {
752 this->ShiftCargo(action
, next
.Pop());
753 if (action
.MaxMove() == 0) break;
755 if (include_invalid
&& action
.MaxMove() > 0) {
756 this->ShiftCargo(action
, INVALID_STATION
);
758 return max_move
- action
.MaxMove();
762 * Truncates where each destination loses roughly the same percentage of its
763 * cargo. This is done by randomizing the selection of packets to be removed.
764 * Optionally count the cargo by origin station.
765 * @param max_move Maximum amount of cargo to remove.
766 * @param cargo_per_source Container for counting the cargo by origin.
767 * @return Amount of cargo actually moved.
769 uint
StationCargoList::Truncate(uint max_move
, StationCargoAmountMap
*cargo_per_source
)
771 max_move
= min(max_move
, this->count
);
772 uint prev_count
= this->count
;
775 bool do_count
= cargo_per_source
!= nullptr;
776 while (max_move
> moved
) {
777 for (Iterator
it(this->packets
.begin()); it
!= this->packets
.end();) {
778 CargoPacket
*cp
= *it
;
779 if (prev_count
> max_move
&& RandomRange(prev_count
) < prev_count
- max_move
) {
780 if (do_count
&& loop
== 0) {
781 (*cargo_per_source
)[cp
->source
] += cp
->count
;
786 uint diff
= max_move
- moved
;
787 if (cp
->count
> diff
) {
789 this->RemoveFromCache(cp
, diff
);
794 if (do_count
) (*cargo_per_source
)[cp
->source
] -= diff
;
797 if (do_count
) (*cargo_per_source
)[cp
->source
] += cp
->count
;
801 it
= this->packets
.erase(it
);
802 if (do_count
&& loop
> 0) {
803 (*cargo_per_source
)[cp
->source
] -= cp
->count
;
806 this->RemoveFromCache(cp
, cp
->count
);
816 * Reserves cargo for loading onto the vehicle.
817 * @param max_move Maximum amount of cargo to reserve.
818 * @param dest VehicleCargoList to reserve for.
819 * @param load_place Tile index of the current station.
820 * @param next_station Next station(s) the loading vehicle will visit.
821 * @return Amount of cargo actually reserved.
823 uint
StationCargoList::Reserve(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next_station
)
825 return this->ShiftCargo(CargoReservation(this, dest
, max_move
, load_place
), next_station
, true);
829 * Loads cargo onto a vehicle. If the vehicle has reserved cargo load that.
830 * Otherwise load cargo from the station.
831 * @param max_move Amount of cargo to load.
832 * @param dest Vehicle cargo list where the cargo resides.
833 * @param load_place The new loaded_at_xy to be assigned to packets being moved.
834 * @param next_station Next station(s) the loading vehicle will visit.
835 * @return Amount of cargo actually loaded.
836 * @note Vehicles may or may not reserve, depending on their orders. The two
837 * modes of loading are exclusive, though. If cargo is reserved we don't
838 * need to load unreserved cargo.
840 uint
StationCargoList::Load(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next_station
)
842 uint move
= min(dest
->ActionCount(VehicleCargoList::MTA_LOAD
), max_move
);
844 this->reserved_count
-= move
;
845 dest
->Reassign
<VehicleCargoList::MTA_LOAD
, VehicleCargoList::MTA_KEEP
>(move
);
848 return this->ShiftCargo(CargoLoad(this, dest
, max_move
, load_place
), next_station
, true);
853 * Routes packets with station "avoid" as next hop to a different place.
854 * @param max_move Maximum amount of cargo to move.
855 * @param dest List to append the cargo to.
856 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
857 * @param avoid2 Additional station to exclude from routing.
858 * @param ge GoodsEntry to get the routing info from.
860 uint
StationCargoList::Reroute(uint max_move
, StationCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
)
862 return this->ShiftCargo(StationCargoReroute(this, dest
, max_move
, avoid
, avoid2
, ge
), avoid
, false);
866 * We have to instantiate everything we want to be usable.
868 template class CargoList
<VehicleCargoList
, CargoPacketList
>;
869 template class CargoList
<StationCargoList
, StationCargoPacketMap
>;
870 template uint
VehicleCargoList::Reassign
<VehicleCargoList::MTA_DELIVER
, VehicleCargoList::MTA_KEEP
>(uint
, TileOrStationID
);