Remove SIGTYPE_LAST_NOPBS
[openttd/fttd.git] / src / cargopacket.cpp
blobffde7791135631f3c9414698c5a90c1ae3662867
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file cargopacket.cpp Implementation of the cargo packets. */
12 #include "stdafx.h"
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 template<> CargoPacket::Pool CargoPacket::PoolItem::pool ("CargoPacket");
22 INSTANTIATE_POOL_METHODS(CargoPacket)
24 /**
25 * Create a new packet for savegame loading.
27 CargoPacket::CargoPacket()
29 this->source_type = ST_INDUSTRY;
30 this->source_id = INVALID_SOURCE;
33 /**
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).
40 * @pre count != 0
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) :
45 feeder_share(0),
46 count(count),
47 days_in_transit(0),
48 source_id(source_id),
49 source(source),
50 source_xy(source_xy),
51 loaded_at_xy(0)
53 assert(count != 0);
54 this->source_type = source_type;
57 /**
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),
73 count(count),
74 days_in_transit(days_in_transit),
75 source_id(source_id),
76 source(source),
77 source_xy(source_xy),
78 loaded_at_xy(loaded_at_xy)
80 assert(count != 0);
81 this->source_type = source_type;
84 /**
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;
97 return cp_new;
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;
108 delete cp;
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 CargoPacket *cp;
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)
141 CargoPacket *cp;
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) {
160 delete *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()
204 this->count = 0;
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
214 * successful.
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) {
224 icp->Merge(cp);
225 return true;
226 } else {
227 return false;
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
243 * ones.
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.
249 * @pre cp != NULL
250 * @pre action == MTA_LOAD || (action == MTA_KEEP && this->designation_counts[MTA_LOAD] == 0)
252 void VehicleCargoList::Append(CargoPacket *cp, MoveToAction action)
254 assert(cp != NULL);
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);
261 return;
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;
268 sum += icp->count;
269 if (sum >= this->action_counts[action]) {
270 this->packets.push_back(cp);
271 return;
275 NOT_REACHED();
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;
292 if (action(cp)) {
293 it = this->packets.erase(it);
294 } else {
295 break;
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;
316 if (action(cp)) {
317 if (it != begin) {
318 this->packets.erase(it--);
319 } else {
320 this->packets.erase(it);
321 break;
323 } else {
324 break;
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)
403 uint sum = 0;
404 for (Iterator it = this->packets.begin(); sum < this->action_counts[MTA_TRANSFER]; ++it) {
405 CargoPacket *cp = *it;
406 cp->loaded_at_xy = xy;
407 sum += cp->count;
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) {
426 return MTA_DELIVER;
427 } else if (next_station.Contains(cargo_next)) {
428 return MTA_KEEP;
429 } else {
430 return MTA_TRANSFER;
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
438 * chunks.
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();
454 uint sum = 0;
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;
466 if (force_keep) {
467 action = MTA_KEEP;
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;
477 } else {
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;
486 } else {
487 cargo_next = new_shares.GetVia();
490 } else {
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;
500 } else {
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);
511 Money share;
512 switch (action) {
513 case MTA_KEEP:
514 this->packets.push_back(cp);
515 if (deliver == this->packets.end()) --deliver;
516 break;
517 case MTA_DELIVER:
518 this->packets.insert(deliver, cp);
519 break;
520 case MTA_TRANSFER:
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;
527 break;
528 default:
529 NOT_REACHED();
531 this->action_counts[action] += cp->count;
532 sum += 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 previously
548 * reserved (MTA_LOAD), but you can't reserve cargo that's marked as to be
549 * delivered. Furthermore, as this method doesn't change the actual packets,
550 * you cannot move cargo from or to MTA_TRANSFER. You need a specialized
551 * template method for that.
552 * @tparam from Previous designation of cargo.
553 * @tparam to New designation of cargo.
554 * @param max_move Maximum amount of cargo to reassign.
555 * @return Amount of cargo actually reassigned.
557 template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
558 uint VehicleCargoList::Reassign(uint max_move, TileOrStationID)
560 assert_tcompile(Tfrom != MTA_TRANSFER && Tto != MTA_TRANSFER);
561 assert_tcompile(Tfrom - Tto == 1 || Tto - Tfrom == 1);
562 max_move = min(this->action_counts[Tfrom], max_move);
563 this->action_counts[Tfrom] -= max_move;
564 this->action_counts[Tto] += max_move;
565 return max_move;
569 * Reassign cargo from MTA_DELIVER to MTA_TRANSFER and take care of the next
570 * station the cargo wants to visit.
571 * @param max_move Maximum amount of cargo to reassign.
572 * @param next_station Station to record as next hop in the reassigned packets.
573 * @return Amount of cargo actually reassigned.
575 template<>
576 uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move, TileOrStationID next_station)
578 max_move = min(this->action_counts[MTA_DELIVER], max_move);
580 uint sum = 0;
581 for (Iterator it(this->packets.begin()); sum < this->action_counts[MTA_TRANSFER] + max_move;) {
582 CargoPacket *cp = *it++;
583 sum += cp->Count();
584 if (sum <= this->action_counts[MTA_TRANSFER]) continue;
585 if (sum > this->action_counts[MTA_TRANSFER] + max_move) {
586 CargoPacket *cp_split = cp->Split(sum - this->action_counts[MTA_TRANSFER] + max_move);
587 sum -= cp_split->Count();
588 this->packets.insert(it, cp_split);
590 cp->next_station = next_station;
593 this->action_counts[MTA_DELIVER] -= max_move;
594 this->action_counts[MTA_TRANSFER] += max_move;
595 return max_move;
599 * Returns reserved cargo to the station and removes it from the cache.
600 * @param max_move Maximum amount of cargo to move.
601 * @param dest Station the cargo is returned to.
602 * @param ID of next the station the cargo wants to go next.
603 * @return Amount of cargo actually returned.
605 uint VehicleCargoList::Return(uint max_move, StationCargoList *dest, StationID next)
607 max_move = min(this->action_counts[MTA_LOAD], max_move);
608 this->PopCargo(CargoReturn(this, dest, max_move, next));
609 return max_move;
613 * Shifts cargo between two vehicles.
614 * @param dest Other vehicle's cargo list.
615 * @param max_move Maximum amount of cargo to be moved.
616 * @return Amount of cargo actually moved.
618 uint VehicleCargoList::Shift(uint max_move, VehicleCargoList *dest)
620 max_move = min(this->count, max_move);
621 this->PopCargo(CargoShift(this, dest, max_move));
622 return max_move;
626 * Unloads cargo at the given station. Deliver or transfer, depending on the
627 * ranges defined by designation_counts.
628 * @param dest StationCargoList to add transferred cargo to.
629 * @param max_move Maximum amount of cargo to move.
630 * @param payment Payment object to register payments in.
631 * @return Amount of cargo actually unloaded.
633 uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment)
635 uint moved = 0;
636 if (this->action_counts[MTA_TRANSFER] > 0) {
637 uint move = min(this->action_counts[MTA_TRANSFER], max_move);
638 this->ShiftCargo(CargoTransfer(this, dest, move));
639 moved += move;
641 if (this->action_counts[MTA_TRANSFER] == 0 && this->action_counts[MTA_DELIVER] > 0 && moved < max_move) {
642 uint move = min(this->action_counts[MTA_DELIVER], max_move - moved);
643 this->ShiftCargo(CargoDelivery(this, move, payment));
644 moved += move;
646 return moved;
650 * Truncates the cargo in this list to the given amount. It leaves the
651 * first cargo entities and removes max_move from the back of the list.
652 * @param max_move Maximum amount of entities to be removed from the list.
653 * @return Amount of entities actually moved.
655 uint VehicleCargoList::Truncate(uint max_move)
657 max_move = min(this->count, max_move);
658 this->PopCargo(CargoRemoval<VehicleCargoList>(this, max_move));
659 return max_move;
663 * Routes packets with station "avoid" as next hop to a different place.
664 * @param max_move Maximum amount of cargo to move.
665 * @param dest List to prepend the cargo to.
666 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
667 * @param avoid2 Additional station to exclude from routing.
668 * @oaram ge GoodsEntry to get the routing info from.
670 uint VehicleCargoList::Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
672 max_move = min(this->action_counts[MTA_TRANSFER], max_move);
673 this->ShiftCargo(VehicleCargoReroute(this, dest, max_move, avoid, avoid2, ge));
674 return max_move;
679 * Station cargo list implementation.
684 * Appends the given cargo packet to the range of packets with the same next station
685 * @warning After appending this packet may not exist anymore!
686 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
687 * @param next the next hop
688 * @param cp the cargo packet to add
689 * @pre cp != NULL
691 void StationCargoList::Append(CargoPacket *cp, StationID next)
693 assert(cp != NULL);
694 this->AddToCache(cp);
696 StationCargoPacketMap::List &list = this->packets[next];
697 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
698 it != list.rend(); it++) {
699 if (StationCargoList::TryMerge(*it, cp)) return;
702 /* The packet could not be merged with another one */
703 list.push_back(cp);
707 * Shifts cargo from the front of the packet list for a specific station and
708 * applies some action to it.
709 * @tparam Taction Action class or function to be used. It should define
710 * "bool operator()(CargoPacket *)". If true is returned the
711 * cargo packet will be removed from the list. Otherwise it
712 * will be kept and the loop will be aborted.
713 * @param action Action instance to be applied.
714 * @param next Next hop the cargo wants to visit.
715 * @return True if all packets with the given next hop have been removed,
716 * False otherwise.
718 template <class Taction>
719 bool StationCargoList::ShiftCargo(Taction &action, StationID next)
721 std::pair<Iterator, Iterator> range(this->packets.equal_range(next));
722 for (Iterator it(range.first); it != range.second && it.GetKey() == next;) {
723 if (action.MaxMove() == 0) return false;
724 CargoPacket *cp = *it;
725 if (action(cp)) {
726 it = this->packets.erase(it);
727 } else {
728 return false;
731 return true;
735 * Shifts cargo from the front of the packet list for a specific station and
736 * and optional also from the list for "any station", then applies some action
737 * to it.
738 * @tparam Taction Action class or function to be used. It should define
739 * "bool operator()(CargoPacket *)". If true is returned the
740 * cargo packet will be removed from the list. Otherwise it
741 * will be kept and the loop will be aborted.
742 * @param action Action instance to be applied.
743 * @param next Next hop the cargo wants to visit.
744 * @param include_invalid If cargo from the INVALID_STATION list should be
745 * used if necessary.
746 * @return Amount of cargo actually moved.
748 template <class Taction>
749 uint StationCargoList::ShiftCargo(Taction action, StationIDStack next, bool include_invalid)
751 uint max_move = action.MaxMove();
752 while (!next.IsEmpty()) {
753 this->ShiftCargo(action, next.Pop());
754 if (action.MaxMove() == 0) break;
756 if (include_invalid && action.MaxMove() > 0) {
757 this->ShiftCargo(action, INVALID_STATION);
759 return max_move - action.MaxMove();
763 * Truncates where each destination loses roughly the same percentage of its
764 * cargo. This is done by randomizing the selection of packets to be removed.
765 * Optionally count the cargo by origin station.
766 * @param max_move Maximum amount of cargo to remove.
767 * @param cargo_per_source Container for counting the cargo by origin.
768 * @return Amount of cargo actually moved.
770 uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source)
772 max_move = min(max_move, this->count);
773 uint prev_count = this->count;
774 uint moved = 0;
775 uint loop = 0;
776 bool do_count = cargo_per_source != NULL;
777 while (max_move > moved) {
778 for (Iterator it(this->packets.begin()); it != this->packets.end();) {
779 CargoPacket *cp = *it;
780 if (prev_count > max_move && RandomRange(prev_count) < prev_count - max_move) {
781 if (do_count && loop == 0) {
782 (*cargo_per_source)[cp->source] += cp->count;
784 ++it;
785 continue;
787 uint diff = max_move - moved;
788 if (cp->count > diff) {
789 if (diff > 0) {
790 this->RemoveFromCache(cp, diff);
791 cp->Reduce(diff);
792 moved += diff;
794 if (loop > 0) {
795 if (do_count) (*cargo_per_source)[cp->source] -= diff;
796 return moved;
797 } else {
798 if (do_count) (*cargo_per_source)[cp->source] += cp->count;
799 ++it;
801 } else {
802 it = this->packets.erase(it);
803 if (do_count && loop > 0) {
804 (*cargo_per_source)[cp->source] -= cp->count;
806 moved += cp->count;
807 this->RemoveFromCache(cp, cp->count);
808 delete cp;
811 loop++;
813 return moved;
817 * Reserves cargo for loading onto the vehicle.
818 * @param max_move Maximum amount of cargo to reserve.
819 * @param dest VehicleCargoList to reserve for.
820 * @param load_place Tile index of the current station.
821 * @param next_station Next station(s) the loading vehicle will visit.
822 * @return Amount of cargo actually reserved.
824 uint StationCargoList::Reserve(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next_station)
826 return this->ShiftCargo(CargoReservation(this, dest, max_move, load_place), next_station, true);
830 * Loads cargo onto a vehicle. If the vehicle has reserved cargo load that.
831 * Otherwise load cargo from the station.
832 * @param max_move Amount of cargo to load.
833 * @param dest Vehicle cargo list where the cargo resides.
834 * @param load_place The new loaded_at_xy to be assigned to packets being moved.
835 * @param next_station Next station(s) the loading vehicle will visit.
836 * @return Amount of cargo actually loaded.
837 * @note Vehicles may or may not reserve, depending on their orders. The two
838 * modes of loading are exclusive, though. If cargo is reserved we don't
839 * need to load unreserved cargo.
841 uint StationCargoList::Load(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next_station)
843 uint move = min(dest->ActionCount(VehicleCargoList::MTA_LOAD), max_move);
844 if (move > 0) {
845 this->reserved_count -= move;
846 dest->Reassign<VehicleCargoList::MTA_LOAD, VehicleCargoList::MTA_KEEP>(move);
847 return move;
848 } else {
849 return this->ShiftCargo(CargoLoad(this, dest, max_move, load_place), next_station, true);
854 * Routes packets with station "avoid" as next hop to a different place.
855 * @param max_move Maximum amount of cargo to move.
856 * @param dest List to append the cargo to.
857 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
858 * @param avoid2 Additional station to exclude from routing.
859 * @oaram ge GoodsEntry to get the routing info from.
861 uint StationCargoList::Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
863 return this->ShiftCargo(StationCargoReroute(this, dest, max_move, avoid, avoid2, ge), avoid, false);
867 * We have to instantiate everything we want to be usable.
869 template class CargoList<VehicleCargoList, CargoPacketList>;
870 template class CargoList<StationCargoList, StationCargoPacketMap>;
871 template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint, TileOrStationID);