4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file cargopacket.cpp Implementation of the cargo packets. */
13 #include "station_base.h"
14 #include "core/pool_func.hpp"
15 #include "core/random_func.hpp"
16 #include "economy_base.h"
17 #include "cargoaction.h"
18 #include "order_type.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 NULL if no packet could be allocated!
89 CargoPacket
*CargoPacket::Split(uint new_size
)
91 if (!CargoPacket::CanAllocateItem()) return NULL
;
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
)
130 FOR_ALL_CARGOPACKETS(cp
) {
131 if (cp
->source_type
== src_type
&& cp
->source_id
== src
) cp
->source_id
= INVALID_SOURCE
;
136 * Invalidates (sets source to INVALID_STATION) all cargo packets from given station.
137 * @param sid Station that gets removed.
139 /* static */ void CargoPacket::InvalidateAllFrom(StationID sid
)
142 FOR_ALL_CARGOPACKETS(cp
) {
143 if (cp
->source
== sid
) cp
->source
= INVALID_STATION
;
149 * Cargo list implementation
154 * Destroy the cargolist ("frees" all cargo packets).
156 template <class Tinst
, class Tcont
>
157 CargoList
<Tinst
, Tcont
>::~CargoList()
159 for (Iterator
it(this->packets
.begin()); it
!= this->packets
.end(); ++it
) {
165 * Empty the cargo list, but don't free the cargo packets;
166 * the cargo packets are cleaned by CargoPacket's CleanPool.
168 template <class Tinst
, class Tcont
>
169 void CargoList
<Tinst
, Tcont
>::OnCleanPool()
171 this->packets
.clear();
175 * Update the cached values to reflect the removal of this packet or part of it.
176 * Decreases count and days_in_transit.
177 * @param cp Packet to be removed from cache.
178 * @param count Amount of cargo from the given packet to be removed.
180 template <class Tinst
, class Tcont
>
181 void CargoList
<Tinst
, Tcont
>::RemoveFromCache(const CargoPacket
*cp
, uint count
)
183 assert(count
<= cp
->count
);
184 this->count
-= count
;
185 this->cargo_days_in_transit
-= cp
->days_in_transit
* count
;
189 * Update the cache to reflect adding of this packet.
190 * Increases count and days_in_transit.
191 * @param cp New packet to be inserted.
193 template <class Tinst
, class Tcont
>
194 void CargoList
<Tinst
, Tcont
>::AddToCache(const CargoPacket
*cp
)
196 this->count
+= cp
->count
;
197 this->cargo_days_in_transit
+= cp
->days_in_transit
* cp
->count
;
200 /** Invalidates the cached data and rebuilds it. */
201 template <class Tinst
, class Tcont
>
202 void CargoList
<Tinst
, Tcont
>::InvalidateCache()
205 this->cargo_days_in_transit
= 0;
207 for (ConstIterator
it(this->packets
.begin()); it
!= this->packets
.end(); it
++) {
208 static_cast<Tinst
*>(this)->AddToCache(*it
);
213 * Tries to merge the second packet into the first and return if that was
215 * @param icp Packet to be merged into.
216 * @param cp Packet to be eliminated.
217 * @return If the packets could be merged.
219 template <class Tinst
, class Tcont
>
220 /* static */ bool CargoList
<Tinst
, Tcont
>::TryMerge(CargoPacket
*icp
, CargoPacket
*cp
)
222 if (Tinst::AreMergable(icp
, cp
) &&
223 icp
->count
+ cp
->count
<= CargoPacket::MAX_COUNT
) {
233 * Vehicle cargo list implementation.
238 * Appends the given cargo packet. Tries to merge it with another one in the
239 * packets list. If no fitting packet is found, appends it. You can only append
240 * packets to the ranges of packets designated for keeping or loading.
241 * Furthermore if there are already packets reserved for loading you cannot
242 * directly add packets to the "keep" list. You first have to load the reserved
244 * @warning After appending this packet may not exist anymore!
245 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
246 * @param cp Cargo packet to add.
247 * @param action Either MTA_KEEP if you want to add the packet directly or MTA_LOAD
248 * if you want to reserve it first.
250 * @pre action == MTA_LOAD || (action == MTA_KEEP && this->designation_counts[MTA_LOAD] == 0)
252 void VehicleCargoList::Append(CargoPacket
*cp
, MoveToAction action
)
255 assert(action
== MTA_LOAD
||
256 (action
== MTA_KEEP
&& this->action_counts
[MTA_LOAD
] == 0));
257 this->AddToMeta(cp
, action
);
259 if (this->count
== cp
->count
) {
260 this->packets
.push_back(cp
);
264 uint sum
= cp
->count
;
265 for (ReverseIterator
it(this->packets
.rbegin()); it
!= this->packets
.rend(); it
++) {
266 CargoPacket
*icp
= *it
;
267 if (VehicleCargoList::TryMerge(icp
, cp
)) return;
269 if (sum
>= this->action_counts
[action
]) {
270 this->packets
.push_back(cp
);
279 * Shifts cargo from the front of the packet list and applies some action to it.
280 * @tparam Taction Action class or function to be used. It should define
281 * "bool operator()(CargoPacket *)". If true is returned the
282 * cargo packet will be removed from the list. Otherwise it
283 * will be kept and the loop will be aborted.
284 * @param action Action instance to be applied.
286 template<class Taction
>
287 void VehicleCargoList::ShiftCargo(Taction action
)
289 Iterator
it(this->packets
.begin());
290 while (it
!= this->packets
.end() && action
.MaxMove() > 0) {
291 CargoPacket
*cp
= *it
;
293 it
= this->packets
.erase(it
);
301 * Pops cargo from the back of the packet list and applies some action to it.
302 * @tparam Taction Action class or function to be used. It should define
303 * "bool operator()(CargoPacket *)". If true is returned the
304 * cargo packet will be removed from the list. Otherwise it
305 * will be kept and the loop will be aborted.
306 * @param action Action instance to be applied.
308 template<class Taction
>
309 void VehicleCargoList::PopCargo(Taction action
)
311 if (this->packets
.empty()) return;
312 Iterator
it(--(this->packets
.end()));
313 Iterator
begin(this->packets
.begin());
314 while (action
.MaxMove() > 0) {
315 CargoPacket
*cp
= *it
;
318 this->packets
.erase(it
--);
320 this->packets
.erase(it
);
330 * Update the cached values to reflect the removal of this packet or part of it.
331 * Decreases count, feeder share and days_in_transit.
332 * @param cp Packet to be removed from cache.
333 * @param count Amount of cargo from the given packet to be removed.
335 void VehicleCargoList::RemoveFromCache(const CargoPacket
*cp
, uint count
)
337 this->feeder_share
-= cp
->FeederShare(count
);
338 this->Parent::RemoveFromCache(cp
, count
);
342 * Update the cache to reflect adding of this packet.
343 * Increases count, feeder share and days_in_transit.
344 * @param cp New packet to be inserted.
346 void VehicleCargoList::AddToCache(const CargoPacket
*cp
)
348 this->feeder_share
+= cp
->feeder_share
;
349 this->Parent::AddToCache(cp
);
353 * Removes a packet or part of it from the metadata.
354 * @param cp Packet to be removed.
355 * @param action MoveToAction of the packet (for updating the counts).
356 * @param count Amount of cargo to be removed.
358 void VehicleCargoList::RemoveFromMeta(const CargoPacket
*cp
, MoveToAction action
, uint count
)
360 this->AssertCountConsistency();
361 this->RemoveFromCache(cp
, count
);
362 this->action_counts
[action
] -= count
;
363 this->AssertCountConsistency();
367 * Adds a packet to the metadata.
368 * @param cp Packet to be added.
369 * @param action MoveToAction of the packet.
371 void VehicleCargoList::AddToMeta(const CargoPacket
*cp
, MoveToAction action
)
373 this->AssertCountConsistency();
374 this->AddToCache(cp
);
375 this->action_counts
[action
] += cp
->count
;
376 this->AssertCountConsistency();
380 * Ages the all cargo in this list.
382 void VehicleCargoList::AgeCargo()
384 for (ConstIterator
it(this->packets
.begin()); it
!= this->packets
.end(); it
++) {
385 CargoPacket
*cp
= *it
;
386 /* If we're at the maximum, then we can't increase no more. */
387 if (cp
->days_in_transit
== 0xFF) continue;
389 cp
->days_in_transit
++;
390 this->cargo_days_in_transit
+= cp
->count
;
395 * Sets loaded_at_xy to the current station for all cargo to be transfered.
396 * This is done when stopping or skipping while the vehicle is unloading. In
397 * that case the vehicle will get part of its transfer credits early and it may
398 * get more transfer credits than it's entitled to.
399 * @param xy New loaded_at_xy for the cargo.
401 void VehicleCargoList::SetTransferLoadPlace(TileIndex xy
)
404 for (Iterator it
= this->packets
.begin(); sum
< this->action_counts
[MTA_TRANSFER
]; ++it
) {
405 CargoPacket
*cp
= *it
;
406 cp
->loaded_at_xy
= xy
;
412 * Choose action to be performed with the given cargo packet.
413 * @param cp The packet.
414 * @param cargo_next Next hop the cargo wants to pass.
415 * @param current_station Current station of the vehicle carrying the cargo.
416 * @param accepted If the cargo is accepted at the current station.
417 * @param next_station Next station(s) the vehicle may stop at.
418 * @return MoveToAction to be performed.
420 /* static */ VehicleCargoList::MoveToAction
VehicleCargoList::ChooseAction(const CargoPacket
*cp
, StationID cargo_next
,
421 StationID current_station
, bool accepted
, StationIDStack next_station
)
423 if (cargo_next
== INVALID_STATION
) {
424 return (accepted
&& cp
->source
!= current_station
) ? MTA_DELIVER
: MTA_KEEP
;
425 } else if (cargo_next
== current_station
) {
427 } else if (next_station
.Contains(cargo_next
)) {
435 * Stages cargo for unloading. The cargo is sorted so that packets to be
436 * transferred, delivered or kept are in consecutive chunks in the list. At the
437 * same time the designation_counts are updated to reflect the size of those
439 * @param accepted If the cargo will be accepted at the station.
440 * @param current_station ID of the station.
441 * @param next_station ID of the station the vehicle will go to next.
442 * @param order_flags OrderUnloadFlags that will apply to the unload operation.
443 * @param ge GoodsEntry for getting the flows.
444 * @param payment Payment object for registering transfers.
445 * return If any cargo will be unloaded.
447 bool VehicleCargoList::Stage(bool accepted
, StationID current_station
, StationIDStack next_station
, uint8 order_flags
, const GoodsEntry
*ge
, CargoPayment
*payment
)
449 this->AssertCountConsistency();
450 assert(this->action_counts
[MTA_LOAD
] == 0);
451 this->action_counts
[MTA_TRANSFER
] = this->action_counts
[MTA_DELIVER
] = this->action_counts
[MTA_KEEP
] = 0;
452 Iterator deliver
= this->packets
.end();
453 Iterator it
= this->packets
.begin();
456 bool force_keep
= (order_flags
& OUFB_NO_UNLOAD
) != 0;
457 bool force_unload
= (order_flags
& OUFB_UNLOAD
) != 0;
458 bool force_transfer
= (order_flags
& (OUFB_TRANSFER
| OUFB_UNLOAD
)) != 0;
459 assert(this->count
> 0 || it
== this->packets
.end());
460 while (sum
< this->count
) {
461 CargoPacket
*cp
= *it
;
463 this->packets
.erase(it
++);
464 StationID cargo_next
= INVALID_STATION
;
465 MoveToAction action
= MTA_LOAD
;
468 } else if (force_unload
&& accepted
&& cp
->source
!= current_station
) {
469 action
= MTA_DELIVER
;
470 } else if (force_transfer
) {
471 action
= MTA_TRANSFER
;
472 /* We cannot send the cargo to any of the possible next hops and
473 * also not to the current station. */
474 FlowStatMap::const_iterator
flow_it(ge
->flows
.find(cp
->source
));
475 if (flow_it
== ge
->flows
.end()) {
476 cargo_next
= INVALID_STATION
;
478 FlowStat new_shares
= flow_it
->second
;
479 new_shares
.ChangeShare(current_station
, INT_MIN
);
480 StationIDStack excluded
= next_station
;
481 while (!excluded
.IsEmpty() && !new_shares
.GetShares()->empty()) {
482 new_shares
.ChangeShare(excluded
.Pop(), INT_MIN
);
484 if (new_shares
.GetShares()->empty()) {
485 cargo_next
= INVALID_STATION
;
487 cargo_next
= new_shares
.GetVia();
491 /* Rewrite an invalid source station to some random other one to
492 * avoid keeping the cargo in the vehicle forever. */
493 if (cp
->source
== INVALID_STATION
&& !ge
->flows
.empty()) {
494 cp
->source
= ge
->flows
.begin()->first
;
496 bool restricted
= false;
497 FlowStatMap::const_iterator
flow_it(ge
->flows
.find(cp
->source
));
498 if (flow_it
== ge
->flows
.end()) {
499 cargo_next
= INVALID_STATION
;
501 cargo_next
= flow_it
->second
.GetViaWithRestricted(restricted
);
503 action
= VehicleCargoList::ChooseAction(cp
, cargo_next
, current_station
, accepted
, next_station
);
504 if (restricted
&& action
== MTA_TRANSFER
) {
505 /* If the flow is restricted we can't transfer to it. Choose an
506 * unrestricted one instead. */
507 cargo_next
= flow_it
->second
.GetVia();
508 action
= VehicleCargoList::ChooseAction(cp
, cargo_next
, current_station
, accepted
, next_station
);
514 this->packets
.push_back(cp
);
515 if (deliver
== this->packets
.end()) --deliver
;
518 this->packets
.insert(deliver
, cp
);
521 this->packets
.push_front(cp
);
522 /* Add feeder share here to allow reusing field for next station. */
523 share
= payment
->PayTransfer(cp
, cp
->count
);
524 cp
->AddFeederShare(share
);
525 this->feeder_share
+= share
;
526 cp
->next_station
= cargo_next
;
531 this->action_counts
[action
] += cp
->count
;
534 this->AssertCountConsistency();
535 return this->action_counts
[MTA_DELIVER
] > 0 || this->action_counts
[MTA_TRANSFER
] > 0;
538 /** Invalidates the cached data and rebuild it. */
539 void VehicleCargoList::InvalidateCache()
541 this->feeder_share
= 0;
542 this->Parent::InvalidateCache();
546 * Moves some cargo from one designation to another. You can only move
547 * between adjacent designations. E.g. you can keep cargo that was
548 * previously reserved (MTA_LOAD) or you can mark cargo to be transferred
549 * that was previously marked as to be delivered, but you can't reserve
550 * cargo that's marked as to be delivered.
552 uint
VehicleCargoList::Reassign(uint max_move
, MoveToAction from
, MoveToAction to
)
554 max_move
= min(this->action_counts
[from
], max_move
);
555 assert(Delta((int)from
, (int)to
) == 1);
556 this->action_counts
[from
] -= max_move
;
557 this->action_counts
[to
] += max_move
;
562 * Returns reserved cargo to the station and removes it from the cache.
563 * @param max_move Maximum amount of cargo to move.
564 * @param dest Station the cargo is returned to.
565 * @param ID of next the station the cargo wants to go next.
566 * @return Amount of cargo actually returned.
568 uint
VehicleCargoList::Return(uint max_move
, StationCargoList
*dest
, StationID next
)
570 max_move
= min(this->action_counts
[MTA_LOAD
], max_move
);
571 this->PopCargo(CargoReturn(this, dest
, max_move
, next
));
576 * Shifts cargo between two vehicles.
577 * @param dest Other vehicle's cargo list.
578 * @param max_move Maximum amount of cargo to be moved.
579 * @return Amount of cargo actually moved.
581 uint
VehicleCargoList::Shift(uint max_move
, VehicleCargoList
*dest
)
583 max_move
= min(this->count
, max_move
);
584 this->PopCargo(CargoShift(this, dest
, max_move
));
589 * Unloads cargo at the given station. Deliver or transfer, depending on the
590 * ranges defined by designation_counts.
591 * @param dest StationCargoList to add transferred cargo to.
592 * @param max_move Maximum amount of cargo to move.
593 * @param payment Payment object to register payments in.
594 * @return Amount of cargo actually unloaded.
596 uint
VehicleCargoList::Unload(uint max_move
, StationCargoList
*dest
, CargoPayment
*payment
)
599 if (this->action_counts
[MTA_TRANSFER
] > 0) {
600 uint move
= min(this->action_counts
[MTA_TRANSFER
], max_move
);
601 this->ShiftCargo(CargoTransfer(this, dest
, move
));
604 if (this->action_counts
[MTA_TRANSFER
] == 0 && this->action_counts
[MTA_DELIVER
] > 0 && moved
< max_move
) {
605 uint move
= min(this->action_counts
[MTA_DELIVER
], max_move
- moved
);
606 this->ShiftCargo(CargoDelivery(this, move
, payment
));
613 * Truncates the cargo in this list to the given amount. It leaves the
614 * first cargo entities and removes max_move from the back of the list.
615 * @param max_move Maximum amount of entities to be removed from the list.
616 * @return Amount of entities actually moved.
618 uint
VehicleCargoList::Truncate(uint max_move
)
620 max_move
= min(this->count
, max_move
);
621 this->PopCargo(CargoRemoval
<VehicleCargoList
>(this, max_move
));
626 * Routes packets with station "avoid" as next hop to a different place.
627 * @param max_move Maximum amount of cargo to move.
628 * @param dest List to prepend the cargo to.
629 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
630 * @param avoid2 Additional station to exclude from routing.
631 * @oaram ge GoodsEntry to get the routing info from.
633 uint
VehicleCargoList::Reroute(uint max_move
, VehicleCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
)
635 max_move
= min(this->action_counts
[MTA_TRANSFER
], max_move
);
636 this->ShiftCargo(VehicleCargoReroute(this, dest
, max_move
, avoid
, avoid2
, ge
));
642 * Station cargo list implementation.
647 * Appends the given cargo packet to the range of packets with the same next station
648 * @warning After appending this packet may not exist anymore!
649 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
650 * @param next the next hop
651 * @param cp the cargo packet to add
654 void StationCargoList::Append(CargoPacket
*cp
, StationID next
)
657 this->AddToCache(cp
);
659 StationCargoPacketMap::List
&list
= this->packets
[next
];
660 for (StationCargoPacketMap::List::reverse_iterator
it(list
.rbegin());
661 it
!= list
.rend(); it
++) {
662 if (StationCargoList::TryMerge(*it
, cp
)) return;
665 /* The packet could not be merged with another one */
670 * Shifts cargo from the front of the packet list for a specific station and
671 * applies some action to it.
672 * @tparam Taction Action class or function to be used. It should define
673 * "bool operator()(CargoPacket *)". If true is returned the
674 * cargo packet will be removed from the list. Otherwise it
675 * will be kept and the loop will be aborted.
676 * @param action Action instance to be applied.
677 * @param next Next hop the cargo wants to visit.
678 * @return True if all packets with the given next hop have been removed,
681 template <class Taction
>
682 bool StationCargoList::ShiftCargo(Taction
&action
, StationID next
)
684 std::pair
<Iterator
, Iterator
> range(this->packets
.equal_range(next
));
685 for (Iterator
it(range
.first
); it
!= range
.second
&& it
.GetKey() == next
;) {
686 if (action
.MaxMove() == 0) return false;
687 CargoPacket
*cp
= *it
;
689 it
= this->packets
.erase(it
);
698 * Shifts cargo from the front of the packet list for a specific station and
699 * and optional also from the list for "any station", then applies some action
701 * @tparam Taction Action class or function to be used. It should define
702 * "bool operator()(CargoPacket *)". If true is returned the
703 * cargo packet will be removed from the list. Otherwise it
704 * will be kept and the loop will be aborted.
705 * @param action Action instance to be applied.
706 * @param next Next hop the cargo wants to visit.
707 * @param include_invalid If cargo from the INVALID_STATION list should be
709 * @return Amount of cargo actually moved.
711 template <class Taction
>
712 uint
StationCargoList::ShiftCargo(Taction action
, StationIDStack next
, bool include_invalid
)
714 uint max_move
= action
.MaxMove();
715 while (!next
.IsEmpty()) {
716 this->ShiftCargo(action
, next
.Pop());
717 if (action
.MaxMove() == 0) break;
719 if (include_invalid
&& action
.MaxMove() > 0) {
720 this->ShiftCargo(action
, INVALID_STATION
);
722 return max_move
- action
.MaxMove();
726 * Truncates where each destination loses roughly the same percentage of its
727 * cargo. This is done by randomizing the selection of packets to be removed.
728 * Optionally count the cargo by origin station.
729 * @param max_move Maximum amount of cargo to remove.
730 * @param cargo_per_source Container for counting the cargo by origin.
731 * @return Amount of cargo actually moved.
733 uint
StationCargoList::Truncate(uint max_move
, StationCargoAmountMap
*cargo_per_source
)
735 max_move
= min(max_move
, this->count
);
736 uint prev_count
= this->count
;
739 bool do_count
= cargo_per_source
!= NULL
;
740 while (max_move
> moved
) {
741 for (Iterator
it(this->packets
.begin()); it
!= this->packets
.end();) {
742 CargoPacket
*cp
= *it
;
743 if (prev_count
> max_move
&& RandomRange(prev_count
) < prev_count
- max_move
) {
744 if (do_count
&& loop
== 0) {
745 (*cargo_per_source
)[cp
->source
] += cp
->count
;
750 uint diff
= max_move
- moved
;
751 if (cp
->count
> diff
) {
753 this->RemoveFromCache(cp
, diff
);
758 if (do_count
) (*cargo_per_source
)[cp
->source
] -= diff
;
761 if (do_count
) (*cargo_per_source
)[cp
->source
] += cp
->count
;
765 it
= this->packets
.erase(it
);
766 if (do_count
&& loop
> 0) {
767 (*cargo_per_source
)[cp
->source
] -= cp
->count
;
770 this->RemoveFromCache(cp
, cp
->count
);
780 * Reserves cargo for loading onto the vehicle.
781 * @param max_move Maximum amount of cargo to reserve.
782 * @param dest VehicleCargoList to reserve for.
783 * @param load_place Tile index of the current station.
784 * @param next_station Next station(s) the loading vehicle will visit.
785 * @return Amount of cargo actually reserved.
787 uint
StationCargoList::Reserve(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next_station
)
789 return this->ShiftCargo(CargoReservation(this, dest
, max_move
, load_place
), next_station
, true);
793 * Loads cargo onto a vehicle. If the vehicle has reserved cargo load that.
794 * Otherwise load cargo from the station.
795 * @param max_move Amount of cargo to load.
796 * @param dest Vehicle cargo list where the cargo resides.
797 * @param load_place The new loaded_at_xy to be assigned to packets being moved.
798 * @param next_station Next station(s) the loading vehicle will visit.
799 * @return Amount of cargo actually loaded.
800 * @note Vehicles may or may not reserve, depending on their orders. The two
801 * modes of loading are exclusive, though. If cargo is reserved we don't
802 * need to load unreserved cargo.
804 uint
StationCargoList::Load(uint max_move
, VehicleCargoList
*dest
, TileIndex load_place
, StationIDStack next_station
)
806 uint move
= min(dest
->ActionCount(VehicleCargoList::MTA_LOAD
), max_move
);
808 this->reserved_count
-= move
;
809 dest
->Reassign(move
, VehicleCargoList::MTA_LOAD
, VehicleCargoList::MTA_KEEP
);
812 return this->ShiftCargo(CargoLoad(this, dest
, max_move
, load_place
), next_station
, true);
817 * Routes packets with station "avoid" as next hop to a different place.
818 * @param max_move Maximum amount of cargo to move.
819 * @param dest List to append the cargo to.
820 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
821 * @param avoid2 Additional station to exclude from routing.
822 * @oaram ge GoodsEntry to get the routing info from.
824 uint
StationCargoList::Reroute(uint max_move
, StationCargoList
*dest
, StationID avoid
, StationID avoid2
, const GoodsEntry
*ge
)
826 return this->ShiftCargo(StationCargoReroute(this, dest
, max_move
, avoid
, avoid2
, ge
), avoid
, false);
830 * We have to instantiate everything we want to be usable.
832 template class CargoList
<VehicleCargoList
, CargoPacketList
>;
833 template class CargoList
<StationCargoList
, StationCargoPacketMap
>;