Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / cargopacket.cpp
blobfc5031d7eb52b83d484a87cdb4d10c4c7711353c
1 /*
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/>.
6 */
8 /** @file cargopacket.cpp Implementation of the cargo packets. */
10 #include "stdafx.h"
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)
24 /**
25 * Create a new packet for savegame loading.
27 CargoPacket::CargoPacket()
29 this->source_type = SourceType::Industry;
30 this->source_id = INVALID_SOURCE;
33 /**
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).
40 * @pre count != 0
42 CargoPacket::CargoPacket(StationID first_station,uint16_t count, SourceType source_type, SourceID source_id) :
43 count(count),
44 source_id(source_id),
45 source_type(source_type),
46 first_station(first_station)
48 assert(count != 0);
51 /**
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) :
61 count(count),
62 periods_in_transit(periods_in_transit),
63 feeder_share(feeder_share),
64 source_xy(source_xy),
65 first_station(first_station)
67 assert(count != 0);
70 /**
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) :
78 count(count),
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),
85 #ifdef WITH_ASSERT
86 in_vehicle(original.in_vehicle),
87 #endif /* WITH_ASSERT */
88 first_station(original.first_station),
89 next_hop(original.next_hop)
91 assert(count != 0);
94 /**
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;
107 return cp_new;
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;
118 delete cp;
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) {
168 delete *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()
212 this->count = 0;
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
222 * successful.
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) {
232 icp->Merge(cp);
233 return true;
234 } else {
235 return false;
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
251 * ones.
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.
257 * @pre cp != nullptr
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);
269 return;
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;
276 sum += icp->count;
277 if (sum >= this->action_counts[action]) {
278 this->packets.push_back(cp);
279 return;
283 NOT_REACHED();
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;
300 if (action(cp)) {
301 it = this->packets.erase(it);
302 } else {
303 break;
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;
324 if (action(cp)) {
325 if (it != begin) {
326 this->packets.erase(it--);
327 } else {
328 this->packets.erase(it);
329 break;
331 } else {
332 break;
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) {
417 return MTA_DELIVER;
418 } else if (next_station.Contains(cargo_next)) {
419 return MTA_KEEP;
420 } else {
421 return MTA_TRANSFER;
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
429 * chunks.
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 payment Payment object for registering transfers.
436 * @param current_tile Current tile the cargo handling is happening on.
437 * return If any cargo will be unloaded.
439 bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment, TileIndex current_tile)
441 this->AssertCountConsistency();
442 assert(this->action_counts[MTA_LOAD] == 0);
443 this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_DELIVER] = this->action_counts[MTA_KEEP] = 0;
444 Iterator deliver = this->packets.end();
445 Iterator it = this->packets.begin();
446 uint sum = 0;
448 bool force_keep = (order_flags & OUFB_NO_UNLOAD) != 0;
449 bool force_unload = (order_flags & OUFB_UNLOAD) != 0;
450 bool force_transfer = (order_flags & (OUFB_TRANSFER | OUFB_UNLOAD)) != 0;
451 assert(this->count > 0 || it == this->packets.end());
452 while (sum < this->count) {
453 CargoPacket *cp = *it;
455 this->packets.erase(it++);
456 StationID cargo_next = INVALID_STATION;
457 MoveToAction action = MTA_LOAD;
458 if (force_keep) {
459 action = MTA_KEEP;
460 } else if (force_unload && accepted && cp->first_station != current_station) {
461 action = MTA_DELIVER;
462 } else if (force_transfer) {
463 action = MTA_TRANSFER;
464 /* We cannot send the cargo to any of the possible next hops and
465 * also not to the current station. */
466 FlowStatMap::const_iterator flow_it(ge->flows.find(cp->first_station));
467 if (flow_it == ge->flows.end()) {
468 cargo_next = INVALID_STATION;
469 } else {
470 FlowStat new_shares = flow_it->second;
471 new_shares.ChangeShare(current_station, INT_MIN);
472 StationIDStack excluded = next_station;
473 while (!excluded.IsEmpty() && !new_shares.GetShares()->empty()) {
474 new_shares.ChangeShare(excluded.Pop(), INT_MIN);
476 if (new_shares.GetShares()->empty()) {
477 cargo_next = INVALID_STATION;
478 } else {
479 cargo_next = new_shares.GetVia();
482 } else {
483 /* Rewrite an invalid source station to some random other one to
484 * avoid keeping the cargo in the vehicle forever. */
485 if (cp->first_station == INVALID_STATION && !ge->flows.empty()) {
486 cp->first_station = ge->flows.begin()->first;
488 bool restricted = false;
489 FlowStatMap::const_iterator flow_it(ge->flows.find(cp->first_station));
490 if (flow_it == ge->flows.end()) {
491 cargo_next = INVALID_STATION;
492 } else {
493 cargo_next = flow_it->second.GetViaWithRestricted(restricted);
495 action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
496 if (restricted && action == MTA_TRANSFER) {
497 /* If the flow is restricted we can't transfer to it. Choose an
498 * unrestricted one instead. */
499 cargo_next = flow_it->second.GetVia();
500 action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
503 Money share;
504 switch (action) {
505 case MTA_KEEP:
506 this->packets.push_back(cp);
507 if (deliver == this->packets.end()) --deliver;
508 break;
509 case MTA_DELIVER:
510 this->packets.insert(deliver, cp);
511 break;
512 case MTA_TRANSFER:
513 this->packets.push_front(cp);
514 /* Add feeder share here to allow reusing field for next station. */
515 share = payment->PayTransfer(cp, cp->count, current_tile);
516 cp->AddFeederShare(share);
517 this->feeder_share += share;
518 cp->next_hop = cargo_next;
519 break;
520 default:
521 NOT_REACHED();
523 this->action_counts[action] += cp->count;
524 sum += cp->count;
526 this->AssertCountConsistency();
527 return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0;
530 /** Invalidates the cached data and rebuild it. */
531 void VehicleCargoList::InvalidateCache()
533 this->feeder_share = 0;
534 this->Parent::InvalidateCache();
538 * Moves some cargo from one designation to another. You can only move
539 * between adjacent designations. E.g. you can keep cargo that was previously
540 * reserved (MTA_LOAD), but you can't reserve cargo that's marked as to be
541 * delivered. Furthermore, as this method doesn't change the actual packets,
542 * you cannot move cargo from or to MTA_TRANSFER. You need a specialized
543 * template method for that.
544 * @tparam from Previous designation of cargo.
545 * @tparam to New designation of cargo.
546 * @param max_move Maximum amount of cargo to reassign.
547 * @return Amount of cargo actually reassigned.
549 template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
550 uint VehicleCargoList::Reassign(uint max_move)
552 static_assert(Tfrom != MTA_TRANSFER && Tto != MTA_TRANSFER);
553 static_assert(Tfrom - Tto == 1 || Tto - Tfrom == 1);
554 max_move = std::min(this->action_counts[Tfrom], max_move);
555 this->action_counts[Tfrom] -= max_move;
556 this->action_counts[Tto] += max_move;
557 return max_move;
561 * Reassign cargo from MTA_DELIVER to MTA_TRANSFER and take care of the next
562 * station the cargo wants to visit.
563 * @param max_move Maximum amount of cargo to reassign.
564 * @return Amount of cargo actually reassigned.
566 template<>
567 uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move)
569 max_move = std::min(this->action_counts[MTA_DELIVER], max_move);
571 uint sum = 0;
572 for (Iterator it(this->packets.begin()); sum < this->action_counts[MTA_TRANSFER] + max_move;) {
573 CargoPacket *cp = *it++;
574 sum += cp->Count();
575 if (sum <= this->action_counts[MTA_TRANSFER]) continue;
576 if (sum > this->action_counts[MTA_TRANSFER] + max_move) {
577 CargoPacket *cp_split = cp->Split(sum - this->action_counts[MTA_TRANSFER] + max_move);
578 sum -= cp_split->Count();
579 this->packets.insert(it, cp_split);
581 cp->next_hop = INVALID_STATION;
584 this->action_counts[MTA_DELIVER] -= max_move;
585 this->action_counts[MTA_TRANSFER] += max_move;
586 return max_move;
590 * Returns reserved cargo to the station and removes it from the cache.
591 * @param max_move Maximum amount of cargo to move.
592 * @param dest Station the cargo is returned to.
593 * @param next ID of the next station the cargo wants to go to.
594 * @param current_tile Current tile the cargo handling is happening on.
595 * @return Amount of cargo actually returned.
597 uint VehicleCargoList::Return(uint max_move, StationCargoList *dest, StationID next, TileIndex current_tile)
599 max_move = std::min(this->action_counts[MTA_LOAD], max_move);
600 this->PopCargo(CargoReturn(this, dest, max_move, next, current_tile));
601 return max_move;
605 * Shifts cargo between two vehicles.
606 * @param dest Other vehicle's cargo list.
607 * @param max_move Maximum amount of cargo to be moved.
608 * @return Amount of cargo actually moved.
610 uint VehicleCargoList::Shift(uint max_move, VehicleCargoList *dest)
612 max_move = std::min(this->count, max_move);
613 this->PopCargo(CargoShift(this, dest, max_move));
614 return max_move;
618 * Unloads cargo at the given station. Deliver or transfer, depending on the
619 * ranges defined by designation_counts.
620 * @param dest StationCargoList to add transferred cargo to.
621 * @param max_move Maximum amount of cargo to move.
622 * @param payment Payment object to register payments in.
623 * @param current_tile Current tile the cargo handling is happening on.
624 * @return Amount of cargo actually unloaded.
626 uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment, TileIndex current_tile)
628 uint moved = 0;
629 if (this->action_counts[MTA_TRANSFER] > 0) {
630 uint move = std::min(this->action_counts[MTA_TRANSFER], max_move);
631 this->ShiftCargo(CargoTransfer(this, dest, move, current_tile));
632 moved += move;
634 if (this->action_counts[MTA_TRANSFER] == 0 && this->action_counts[MTA_DELIVER] > 0 && moved < max_move) {
635 uint move = std::min(this->action_counts[MTA_DELIVER], max_move - moved);
636 this->ShiftCargo(CargoDelivery(this, move, payment, current_tile));
637 moved += move;
639 return moved;
643 * Truncates the cargo in this list to the given amount. It leaves the
644 * first cargo entities and removes max_move from the back of the list.
645 * @param max_move Maximum amount of entities to be removed from the list.
646 * @return Amount of entities actually moved.
648 uint VehicleCargoList::Truncate(uint max_move)
650 max_move = std::min(this->count, max_move);
651 this->PopCargo(CargoRemoval<VehicleCargoList>(this, max_move));
652 return max_move;
656 * Routes packets with station "avoid" as next hop to a different place.
657 * @param max_move Maximum amount of cargo to move.
658 * @param dest List to prepend the cargo to.
659 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
660 * @param avoid2 Additional station to exclude from routing.
661 * @param ge GoodsEntry to get the routing info from.
663 uint VehicleCargoList::Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
665 max_move = std::min(this->action_counts[MTA_TRANSFER], max_move);
666 this->ShiftCargo(VehicleCargoReroute(this, dest, max_move, avoid, avoid2, ge));
667 return max_move;
672 * Station cargo list implementation.
677 * Appends the given cargo packet to the range of packets with the same next station
678 * @warning After appending this packet may not exist anymore!
679 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
680 * @param next the next hop
681 * @param cp the cargo packet to add
682 * @pre cp != nullptr
684 void StationCargoList::Append(CargoPacket *cp, StationID next)
686 assert(cp != nullptr);
687 this->AddToCache(cp);
689 StationCargoPacketMap::List &list = this->packets[next];
690 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
691 it != list.rend(); it++) {
692 if (StationCargoList::TryMerge(*it, cp)) return;
695 /* The packet could not be merged with another one */
696 list.push_back(cp);
700 * Shifts cargo from the front of the packet list for a specific station and
701 * applies some action to it.
702 * @tparam Taction Action class or function to be used. It should define
703 * "bool operator()(CargoPacket *)". If true is returned the
704 * cargo packet will be removed from the list. Otherwise it
705 * will be kept and the loop will be aborted.
706 * @param action Action instance to be applied.
707 * @param next Next hop the cargo wants to visit.
708 * @return True if all packets with the given next hop have been removed,
709 * False otherwise.
711 template <class Taction>
712 bool StationCargoList::ShiftCargo(Taction &action, StationID next)
714 std::pair<Iterator, Iterator> range(this->packets.equal_range(next));
715 for (Iterator it(range.first); it != range.second && it.GetKey() == next;) {
716 if (action.MaxMove() == 0) return false;
717 CargoPacket *cp = *it;
718 if (action(cp)) {
719 it = this->packets.erase(it);
720 } else {
721 return false;
724 return true;
728 * Shifts cargo from the front of the packet list for a specific station and
729 * and optional also from the list for "any station", then applies some action
730 * to it.
731 * @tparam Taction Action class or function to be used. It should define
732 * "bool operator()(CargoPacket *)". If true is returned the
733 * cargo packet will be removed from the list. Otherwise it
734 * will be kept and the loop will be aborted.
735 * @param action Action instance to be applied.
736 * @param next Next hop the cargo wants to visit.
737 * @param include_invalid If cargo from the INVALID_STATION list should be
738 * used if necessary.
739 * @return Amount of cargo actually moved.
741 template <class Taction>
742 uint StationCargoList::ShiftCargo(Taction action, StationIDStack next, bool include_invalid)
744 uint max_move = action.MaxMove();
745 while (!next.IsEmpty()) {
746 this->ShiftCargo(action, next.Pop());
747 if (action.MaxMove() == 0) break;
749 if (include_invalid && action.MaxMove() > 0) {
750 this->ShiftCargo(action, INVALID_STATION);
752 return max_move - action.MaxMove();
756 * Truncates where each destination loses roughly the same percentage of its
757 * cargo. This is done by randomizing the selection of packets to be removed.
758 * Optionally count the cargo by origin station.
759 * @param max_move Maximum amount of cargo to remove.
760 * @param cargo_per_source Container for counting the cargo by origin.
761 * @return Amount of cargo actually moved.
763 uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source)
765 max_move = std::min(max_move, this->count);
766 uint prev_count = this->count;
767 uint moved = 0;
768 uint loop = 0;
769 bool do_count = cargo_per_source != nullptr;
770 while (max_move > moved) {
771 for (Iterator it(this->packets.begin()); it != this->packets.end();) {
772 CargoPacket *cp = *it;
773 if (prev_count > max_move && RandomRange(prev_count) < prev_count - max_move) {
774 if (do_count && loop == 0) {
775 (*cargo_per_source)[cp->first_station] += cp->count;
777 ++it;
778 continue;
780 uint diff = max_move - moved;
781 if (cp->count > diff) {
782 if (diff > 0) {
783 this->RemoveFromCache(cp, diff);
784 cp->Reduce(diff);
785 moved += diff;
787 if (loop > 0) {
788 if (do_count) (*cargo_per_source)[cp->first_station] -= diff;
789 return moved;
790 } else {
791 if (do_count) (*cargo_per_source)[cp->first_station] += cp->count;
792 ++it;
794 } else {
795 it = this->packets.erase(it);
796 if (do_count && loop > 0) {
797 (*cargo_per_source)[cp->first_station] -= cp->count;
799 moved += cp->count;
800 this->RemoveFromCache(cp, cp->count);
801 delete cp;
804 loop++;
806 return moved;
810 * Reserves cargo for loading onto the vehicle.
811 * @param max_move Maximum amount of cargo to reserve.
812 * @param dest VehicleCargoList to reserve for.
813 * @param next_station Next station(s) the loading vehicle will visit.
814 * @param current_tile Current tile the cargo handling is happening on.
815 * @return Amount of cargo actually reserved.
817 uint StationCargoList::Reserve(uint max_move, VehicleCargoList *dest, StationIDStack next_station, TileIndex current_tile)
819 return this->ShiftCargo(CargoReservation(this, dest, max_move, current_tile), next_station, true);
823 * Loads cargo onto a vehicle. If the vehicle has reserved cargo load that.
824 * Otherwise load cargo from the station.
825 * @param max_move Amount of cargo to load.
826 * @param dest Vehicle cargo list where the cargo resides.
827 * @param next_station Next station(s) the loading vehicle will visit.
828 * @param current_tile Current tile the cargo handling is happening on.
829 * @return Amount of cargo actually loaded.
830 * @note Vehicles may or may not reserve, depending on their orders. The two
831 * modes of loading are exclusive, though. If cargo is reserved we don't
832 * need to load unreserved cargo.
834 uint StationCargoList::Load(uint max_move, VehicleCargoList *dest, StationIDStack next_station, TileIndex current_tile)
836 uint move = std::min(dest->ActionCount(VehicleCargoList::MTA_LOAD), max_move);
837 if (move > 0) {
838 this->reserved_count -= move;
839 dest->Reassign<VehicleCargoList::MTA_LOAD, VehicleCargoList::MTA_KEEP>(move);
840 return move;
841 } else {
842 return this->ShiftCargo(CargoLoad(this, dest, max_move, current_tile), next_station, true);
847 * Routes packets with station "avoid" as next hop to a different place.
848 * @param max_move Maximum amount of cargo to move.
849 * @param dest List to append the cargo to.
850 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
851 * @param avoid2 Additional station to exclude from routing.
852 * @param ge GoodsEntry to get the routing info from.
854 uint StationCargoList::Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
856 return this->ShiftCargo(StationCargoReroute(this, dest, max_move, avoid, avoid2, ge), avoid, false);
860 * We have to instantiate everything we want to be usable.
862 template class CargoList<VehicleCargoList, CargoPacketList>;
863 template class CargoList<StationCargoList, StationCargoPacketMap>;
864 template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint);