(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / cargopacket.cpp
blob9b96be62eb671a15e480775eb90dd85bffcea0dc
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 #include "safeguards.h"
22 /* Initialize the cargopacket-pool */
23 CargoPacketPool _cargopacket_pool("CargoPacket");
24 INSTANTIATE_POOL_METHODS(CargoPacket)
26 /**
27 * Create a new packet for savegame loading.
29 CargoPacket::CargoPacket()
31 this->source_type = ST_INDUSTRY;
32 this->source_id = INVALID_SOURCE;
35 /**
36 * Creates a new cargo packet.
37 * @param source Source station of the packet.
38 * @param source_xy Source location of the packet.
39 * @param count Number of cargo entities to put in this packet.
40 * @param source_type 'Type' of source the packet comes from (for subsidies).
41 * @param source_id Actual source of the packet (for subsidies).
42 * @pre count != 0
43 * @note We have to zero memory ourselves here because we are using a 'new'
44 * that, in contrary to all other pools, does not memset to 0.
46 CargoPacket::CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id) :
47 feeder_share(0),
48 count(count),
49 days_in_transit(0),
50 source_id(source_id),
51 source(source),
52 source_xy(source_xy),
53 loaded_at_xy(0)
55 assert(count != 0);
56 this->source_type = source_type;
59 /**
60 * Creates a new cargo packet. Initializes the fields that cannot be changed later.
61 * Used when loading or splitting packets.
62 * @param count Number of cargo entities to put in this packet.
63 * @param days_in_transit Number of days the cargo has been in transit.
64 * @param source Station the cargo was initially loaded.
65 * @param source_xy Station location the cargo was initially loaded.
66 * @param loaded_at_xy Location the cargo was loaded last.
67 * @param feeder_share Feeder share the packet has already accumulated.
68 * @param source_type 'Type' of source the packet comes from (for subsidies).
69 * @param source_id Actual source of the packet (for subsidies).
70 * @note We have to zero memory ourselves here because we are using a 'new'
71 * that, in contrary to all other pools, does not memset to 0.
73 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) :
74 feeder_share(feeder_share),
75 count(count),
76 days_in_transit(days_in_transit),
77 source_id(source_id),
78 source(source),
79 source_xy(source_xy),
80 loaded_at_xy(loaded_at_xy)
82 assert(count != 0);
83 this->source_type = source_type;
86 /**
87 * Split this packet in two and return the split off part.
88 * @param new_size Size of the split part.
89 * @return Split off part, or NULL if no packet could be allocated!
91 CargoPacket *CargoPacket::Split(uint new_size)
93 if (!CargoPacket::CanAllocateItem()) return NULL;
95 Money fs = this->FeederShare(new_size);
96 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);
97 this->feeder_share -= fs;
98 this->count -= new_size;
99 return cp_new;
103 * Merge another packet into this one.
104 * @param cp Packet to be merged in.
106 void CargoPacket::Merge(CargoPacket *cp)
108 this->count += cp->count;
109 this->feeder_share += cp->feeder_share;
110 delete cp;
114 * Reduce the packet by the given amount and remove the feeder share.
115 * @param count Amount to be removed.
117 void CargoPacket::Reduce(uint count)
119 assert(count < this->count);
120 this->feeder_share -= this->FeederShare(count);
121 this->count -= count;
125 * Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
126 * @param src_type Type of source.
127 * @param src Index of source.
129 /* static */ void CargoPacket::InvalidateAllFrom(SourceType src_type, SourceID src)
131 CargoPacket *cp;
132 FOR_ALL_CARGOPACKETS(cp) {
133 if (cp->source_type == src_type && cp->source_id == src) cp->source_id = INVALID_SOURCE;
138 * Invalidates (sets source to INVALID_STATION) all cargo packets from given station.
139 * @param sid Station that gets removed.
141 /* static */ void CargoPacket::InvalidateAllFrom(StationID sid)
143 CargoPacket *cp;
144 FOR_ALL_CARGOPACKETS(cp) {
145 if (cp->source == sid) cp->source = INVALID_STATION;
151 * Cargo list implementation
156 * Destroy the cargolist ("frees" all cargo packets).
158 template <class Tinst, class Tcont>
159 CargoList<Tinst, Tcont>::~CargoList()
161 for (Iterator it(this->packets.begin()); it != this->packets.end(); ++it) {
162 delete *it;
167 * Empty the cargo list, but don't free the cargo packets;
168 * the cargo packets are cleaned by CargoPacket's CleanPool.
170 template <class Tinst, class Tcont>
171 void CargoList<Tinst, Tcont>::OnCleanPool()
173 this->packets.clear();
177 * Update the cached values to reflect the removal of this packet or part of it.
178 * Decreases count and days_in_transit.
179 * @param cp Packet to be removed from cache.
180 * @param count Amount of cargo from the given packet to be removed.
182 template <class Tinst, class Tcont>
183 void CargoList<Tinst, Tcont>::RemoveFromCache(const CargoPacket *cp, uint count)
185 assert(count <= cp->count);
186 this->count -= count;
187 this->cargo_days_in_transit -= cp->days_in_transit * count;
191 * Update the cache to reflect adding of this packet.
192 * Increases count and days_in_transit.
193 * @param cp New packet to be inserted.
195 template <class Tinst, class Tcont>
196 void CargoList<Tinst, Tcont>::AddToCache(const CargoPacket *cp)
198 this->count += cp->count;
199 this->cargo_days_in_transit += cp->days_in_transit * cp->count;
202 /** Invalidates the cached data and rebuilds it. */
203 template <class Tinst, class Tcont>
204 void CargoList<Tinst, Tcont>::InvalidateCache()
206 this->count = 0;
207 this->cargo_days_in_transit = 0;
209 for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
210 static_cast<Tinst *>(this)->AddToCache(*it);
215 * Tries to merge the second packet into the first and return if that was
216 * successful.
217 * @param icp Packet to be merged into.
218 * @param cp Packet to be eliminated.
219 * @return If the packets could be merged.
221 template <class Tinst, class Tcont>
222 /* static */ bool CargoList<Tinst, Tcont>::TryMerge(CargoPacket *icp, CargoPacket *cp)
224 if (Tinst::AreMergable(icp, cp) &&
225 icp->count + cp->count <= CargoPacket::MAX_COUNT) {
226 icp->Merge(cp);
227 return true;
228 } else {
229 return false;
235 * Vehicle cargo list implementation.
240 * Appends the given cargo packet. Tries to merge it with another one in the
241 * packets list. If no fitting packet is found, appends it. You can only append
242 * packets to the ranges of packets designated for keeping or loading.
243 * Furthermore if there are already packets reserved for loading you cannot
244 * directly add packets to the "keep" list. You first have to load the reserved
245 * ones.
246 * @warning After appending this packet may not exist anymore!
247 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
248 * @param cp Cargo packet to add.
249 * @param action Either MTA_KEEP if you want to add the packet directly or MTA_LOAD
250 * if you want to reserve it first.
251 * @pre cp != NULL
252 * @pre action == MTA_LOAD || (action == MTA_KEEP && this->designation_counts[MTA_LOAD] == 0)
254 void VehicleCargoList::Append(CargoPacket *cp, MoveToAction action)
256 assert(cp != NULL);
257 assert(action == MTA_LOAD ||
258 (action == MTA_KEEP && this->action_counts[MTA_LOAD] == 0));
259 this->AddToMeta(cp, action);
261 if (this->count == cp->count) {
262 this->packets.push_back(cp);
263 return;
266 uint sum = cp->count;
267 for (ReverseIterator it(this->packets.rbegin()); it != this->packets.rend(); it++) {
268 CargoPacket *icp = *it;
269 if (VehicleCargoList::TryMerge(icp, cp)) return;
270 sum += icp->count;
271 if (sum >= this->action_counts[action]) {
272 this->packets.push_back(cp);
273 return;
277 NOT_REACHED();
281 * Shifts cargo from the front of the packet list and applies some action to it.
282 * @tparam Taction Action class or function to be used. It should define
283 * "bool operator()(CargoPacket *)". If true is returned the
284 * cargo packet will be removed from the list. Otherwise it
285 * will be kept and the loop will be aborted.
286 * @param action Action instance to be applied.
288 template<class Taction>
289 void VehicleCargoList::ShiftCargo(Taction action)
291 Iterator it(this->packets.begin());
292 while (it != this->packets.end() && action.MaxMove() > 0) {
293 CargoPacket *cp = *it;
294 if (action(cp)) {
295 it = this->packets.erase(it);
296 } else {
297 break;
303 * Pops cargo from the back of the packet list and applies some action to it.
304 * @tparam Taction Action class or function to be used. It should define
305 * "bool operator()(CargoPacket *)". If true is returned the
306 * cargo packet will be removed from the list. Otherwise it
307 * will be kept and the loop will be aborted.
308 * @param action Action instance to be applied.
310 template<class Taction>
311 void VehicleCargoList::PopCargo(Taction action)
313 if (this->packets.empty()) return;
314 Iterator it(--(this->packets.end()));
315 Iterator begin(this->packets.begin());
316 while (action.MaxMove() > 0) {
317 CargoPacket *cp = *it;
318 if (action(cp)) {
319 if (it != begin) {
320 this->packets.erase(it--);
321 } else {
322 this->packets.erase(it);
323 break;
325 } else {
326 break;
332 * Update the cached values to reflect the removal of this packet or part of it.
333 * Decreases count, feeder share and days_in_transit.
334 * @param cp Packet to be removed from cache.
335 * @param count Amount of cargo from the given packet to be removed.
337 void VehicleCargoList::RemoveFromCache(const CargoPacket *cp, uint count)
339 this->feeder_share -= cp->FeederShare(count);
340 this->Parent::RemoveFromCache(cp, count);
344 * Update the cache to reflect adding of this packet.
345 * Increases count, feeder share and days_in_transit.
346 * @param cp New packet to be inserted.
348 void VehicleCargoList::AddToCache(const CargoPacket *cp)
350 this->feeder_share += cp->feeder_share;
351 this->Parent::AddToCache(cp);
355 * Removes a packet or part of it from the metadata.
356 * @param cp Packet to be removed.
357 * @param action MoveToAction of the packet (for updating the counts).
358 * @param count Amount of cargo to be removed.
360 void VehicleCargoList::RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
362 assert(count <= this->action_counts[action]);
363 this->AssertCountConsistency();
364 this->RemoveFromCache(cp, count);
365 this->action_counts[action] -= count;
366 this->AssertCountConsistency();
370 * Adds a packet to the metadata.
371 * @param cp Packet to be added.
372 * @param action MoveToAction of the packet.
374 void VehicleCargoList::AddToMeta(const CargoPacket *cp, MoveToAction action)
376 this->AssertCountConsistency();
377 this->AddToCache(cp);
378 this->action_counts[action] += cp->count;
379 this->AssertCountConsistency();
383 * Ages the all cargo in this list.
385 void VehicleCargoList::AgeCargo()
387 for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
388 CargoPacket *cp = *it;
389 /* If we're at the maximum, then we can't increase no more. */
390 if (cp->days_in_transit == 0xFF) continue;
392 cp->days_in_transit++;
393 this->cargo_days_in_transit += cp->count;
398 * Sets loaded_at_xy to the current station for all cargo to be transfered.
399 * This is done when stopping or skipping while the vehicle is unloading. In
400 * that case the vehicle will get part of its transfer credits early and it may
401 * get more transfer credits than it's entitled to.
402 * @param xy New loaded_at_xy for the cargo.
404 void VehicleCargoList::SetTransferLoadPlace(TileIndex xy)
406 uint sum = 0;
407 for (Iterator it = this->packets.begin(); sum < this->action_counts[MTA_TRANSFER]; ++it) {
408 CargoPacket *cp = *it;
409 cp->loaded_at_xy = xy;
410 sum += cp->count;
415 * Choose action to be performed with the given cargo packet.
416 * @param cp The packet.
417 * @param cargo_next Next hop the cargo wants to pass.
418 * @param current_station Current station of the vehicle carrying the cargo.
419 * @param accepted If the cargo is accepted at the current station.
420 * @param next_station Next station(s) the vehicle may stop at.
421 * @return MoveToAction to be performed.
423 /* static */ VehicleCargoList::MoveToAction VehicleCargoList::ChooseAction(const CargoPacket *cp, StationID cargo_next,
424 StationID current_station, bool accepted, StationIDStack next_station)
426 if (cargo_next == INVALID_STATION) {
427 return (accepted && cp->source != current_station) ? MTA_DELIVER : MTA_KEEP;
428 } else if (cargo_next == current_station) {
429 return MTA_DELIVER;
430 } else if (next_station.Contains(cargo_next)) {
431 return MTA_KEEP;
432 } else {
433 return MTA_TRANSFER;
438 * Stages cargo for unloading. The cargo is sorted so that packets to be
439 * transferred, delivered or kept are in consecutive chunks in the list. At the
440 * same time the designation_counts are updated to reflect the size of those
441 * chunks.
442 * @param accepted If the cargo will be accepted at the station.
443 * @param current_station ID of the station.
444 * @param next_station ID of the station the vehicle will go to next.
445 * @param order_flags OrderUnloadFlags that will apply to the unload operation.
446 * @param ge GoodsEntry for getting the flows.
447 * @param payment Payment object for registering transfers.
448 * return If any cargo will be unloaded.
450 bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8 order_flags, const GoodsEntry *ge, CargoPayment *payment)
452 this->AssertCountConsistency();
453 assert(this->action_counts[MTA_LOAD] == 0);
454 this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_DELIVER] = this->action_counts[MTA_KEEP] = 0;
455 Iterator deliver = this->packets.end();
456 Iterator it = this->packets.begin();
457 uint sum = 0;
459 bool force_keep = (order_flags & OUFB_NO_UNLOAD) != 0;
460 bool force_unload = (order_flags & OUFB_UNLOAD) != 0;
461 bool force_transfer = (order_flags & (OUFB_TRANSFER | OUFB_UNLOAD)) != 0;
462 assert(this->count > 0 || it == this->packets.end());
463 while (sum < this->count) {
464 CargoPacket *cp = *it;
466 this->packets.erase(it++);
467 StationID cargo_next = INVALID_STATION;
468 MoveToAction action = MTA_LOAD;
469 if (force_keep) {
470 action = MTA_KEEP;
471 } else if (force_unload && accepted && cp->source != current_station) {
472 action = MTA_DELIVER;
473 } else if (force_transfer) {
474 action = MTA_TRANSFER;
475 /* We cannot send the cargo to any of the possible next hops and
476 * also not to the current station. */
477 FlowStatMap::const_iterator flow_it(ge->flows.find(cp->source));
478 if (flow_it == ge->flows.end()) {
479 cargo_next = INVALID_STATION;
480 } else {
481 FlowStat new_shares = flow_it->second;
482 new_shares.ChangeShare(current_station, INT_MIN);
483 StationIDStack excluded = next_station;
484 while (!excluded.IsEmpty() && !new_shares.GetShares()->empty()) {
485 new_shares.ChangeShare(excluded.Pop(), INT_MIN);
487 if (new_shares.GetShares()->empty()) {
488 cargo_next = INVALID_STATION;
489 } else {
490 cargo_next = new_shares.GetVia();
493 } else {
494 /* Rewrite an invalid source station to some random other one to
495 * avoid keeping the cargo in the vehicle forever. */
496 if (cp->source == INVALID_STATION && !ge->flows.empty()) {
497 cp->source = ge->flows.begin()->first;
499 bool restricted = false;
500 FlowStatMap::const_iterator flow_it(ge->flows.find(cp->source));
501 if (flow_it == ge->flows.end()) {
502 cargo_next = INVALID_STATION;
503 } else {
504 cargo_next = flow_it->second.GetViaWithRestricted(restricted);
506 action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
507 if (restricted && action == MTA_TRANSFER) {
508 /* If the flow is restricted we can't transfer to it. Choose an
509 * unrestricted one instead. */
510 cargo_next = flow_it->second.GetVia();
511 action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
514 Money share;
515 switch (action) {
516 case MTA_KEEP:
517 this->packets.push_back(cp);
518 if (deliver == this->packets.end()) --deliver;
519 break;
520 case MTA_DELIVER:
521 this->packets.insert(deliver, cp);
522 break;
523 case MTA_TRANSFER:
524 this->packets.push_front(cp);
525 /* Add feeder share here to allow reusing field for next station. */
526 share = payment->PayTransfer(cp, cp->count);
527 cp->AddFeederShare(share);
528 this->feeder_share += share;
529 cp->next_station = cargo_next;
530 break;
531 default:
532 NOT_REACHED();
534 this->action_counts[action] += cp->count;
535 sum += cp->count;
537 this->AssertCountConsistency();
538 return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0;
541 /** Invalidates the cached data and rebuild it. */
542 void VehicleCargoList::InvalidateCache()
544 this->feeder_share = 0;
545 this->Parent::InvalidateCache();
549 * Moves some cargo from one designation to another. You can only move
550 * between adjacent designations. E.g. you can keep cargo that was previously
551 * reserved (MTA_LOAD), but you can't reserve cargo that's marked as to be
552 * delivered. Furthermore, as this method doesn't change the actual packets,
553 * you cannot move cargo from or to MTA_TRANSFER. You need a specialized
554 * template method for that.
555 * @tparam from Previous designation of cargo.
556 * @tparam to New designation of cargo.
557 * @param max_move Maximum amount of cargo to reassign.
558 * @return Amount of cargo actually reassigned.
560 template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
561 uint VehicleCargoList::Reassign(uint max_move, TileOrStationID)
563 assert_tcompile(Tfrom != MTA_TRANSFER && Tto != MTA_TRANSFER);
564 assert_tcompile(Tfrom - Tto == 1 || Tto - Tfrom == 1);
565 max_move = min(this->action_counts[Tfrom], max_move);
566 this->action_counts[Tfrom] -= max_move;
567 this->action_counts[Tto] += max_move;
568 return max_move;
572 * Reassign cargo from MTA_DELIVER to MTA_TRANSFER and take care of the next
573 * station the cargo wants to visit.
574 * @param max_move Maximum amount of cargo to reassign.
575 * @param next_station Station to record as next hop in the reassigned packets.
576 * @return Amount of cargo actually reassigned.
578 template<>
579 uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move, TileOrStationID next_station)
581 max_move = min(this->action_counts[MTA_DELIVER], max_move);
583 uint sum = 0;
584 for (Iterator it(this->packets.begin()); sum < this->action_counts[MTA_TRANSFER] + max_move;) {
585 CargoPacket *cp = *it++;
586 sum += cp->Count();
587 if (sum <= this->action_counts[MTA_TRANSFER]) continue;
588 if (sum > this->action_counts[MTA_TRANSFER] + max_move) {
589 CargoPacket *cp_split = cp->Split(sum - this->action_counts[MTA_TRANSFER] + max_move);
590 sum -= cp_split->Count();
591 this->packets.insert(it, cp_split);
593 cp->next_station = next_station;
596 this->action_counts[MTA_DELIVER] -= max_move;
597 this->action_counts[MTA_TRANSFER] += max_move;
598 return max_move;
602 * Returns reserved cargo to the station and removes it from the cache.
603 * @param max_move Maximum amount of cargo to move.
604 * @param dest Station the cargo is returned to.
605 * @param ID of next the station the cargo wants to go next.
606 * @return Amount of cargo actually returned.
608 uint VehicleCargoList::Return(uint max_move, StationCargoList *dest, StationID next)
610 max_move = min(this->action_counts[MTA_LOAD], max_move);
611 this->PopCargo(CargoReturn(this, dest, max_move, next));
612 return max_move;
616 * Shifts cargo between two vehicles.
617 * @param dest Other vehicle's cargo list.
618 * @param max_move Maximum amount of cargo to be moved.
619 * @return Amount of cargo actually moved.
621 uint VehicleCargoList::Shift(uint max_move, VehicleCargoList *dest)
623 max_move = min(this->count, max_move);
624 this->PopCargo(CargoShift(this, dest, max_move));
625 return max_move;
629 * Unloads cargo at the given station. Deliver or transfer, depending on the
630 * ranges defined by designation_counts.
631 * @param dest StationCargoList to add transferred cargo to.
632 * @param max_move Maximum amount of cargo to move.
633 * @param payment Payment object to register payments in.
634 * @return Amount of cargo actually unloaded.
636 uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment)
638 uint moved = 0;
639 if (this->action_counts[MTA_TRANSFER] > 0) {
640 uint move = min(this->action_counts[MTA_TRANSFER], max_move);
641 this->ShiftCargo(CargoTransfer(this, dest, move));
642 moved += move;
644 if (this->action_counts[MTA_TRANSFER] == 0 && this->action_counts[MTA_DELIVER] > 0 && moved < max_move) {
645 uint move = min(this->action_counts[MTA_DELIVER], max_move - moved);
646 this->ShiftCargo(CargoDelivery(this, move, payment));
647 moved += move;
649 return moved;
653 * Truncates the cargo in this list to the given amount. It leaves the
654 * first cargo entities and removes max_move from the back of the list.
655 * @param max_move Maximum amount of entities to be removed from the list.
656 * @return Amount of entities actually moved.
658 uint VehicleCargoList::Truncate(uint max_move)
660 max_move = min(this->count, max_move);
661 this->PopCargo(CargoRemoval<VehicleCargoList>(this, max_move));
662 return max_move;
666 * Routes packets with station "avoid" as next hop to a different place.
667 * @param max_move Maximum amount of cargo to move.
668 * @param dest List to prepend the cargo to.
669 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
670 * @param avoid2 Additional station to exclude from routing.
671 * @oaram ge GoodsEntry to get the routing info from.
673 uint VehicleCargoList::Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
675 max_move = min(this->action_counts[MTA_TRANSFER], max_move);
676 this->ShiftCargo(VehicleCargoReroute(this, dest, max_move, avoid, avoid2, ge));
677 return max_move;
682 * Station cargo list implementation.
687 * Appends the given cargo packet to the range of packets with the same next station
688 * @warning After appending this packet may not exist anymore!
689 * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
690 * @param next the next hop
691 * @param cp the cargo packet to add
692 * @pre cp != NULL
694 void StationCargoList::Append(CargoPacket *cp, StationID next)
696 assert(cp != NULL);
697 this->AddToCache(cp);
699 StationCargoPacketMap::List &list = this->packets[next];
700 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
701 it != list.rend(); it++) {
702 if (StationCargoList::TryMerge(*it, cp)) return;
705 /* The packet could not be merged with another one */
706 list.push_back(cp);
710 * Shifts cargo from the front of the packet list for a specific station and
711 * applies some action to it.
712 * @tparam Taction Action class or function to be used. It should define
713 * "bool operator()(CargoPacket *)". If true is returned the
714 * cargo packet will be removed from the list. Otherwise it
715 * will be kept and the loop will be aborted.
716 * @param action Action instance to be applied.
717 * @param next Next hop the cargo wants to visit.
718 * @return True if all packets with the given next hop have been removed,
719 * False otherwise.
721 template <class Taction>
722 bool StationCargoList::ShiftCargo(Taction &action, StationID next)
724 std::pair<Iterator, Iterator> range(this->packets.equal_range(next));
725 for (Iterator it(range.first); it != range.second && it.GetKey() == next;) {
726 if (action.MaxMove() == 0) return false;
727 CargoPacket *cp = *it;
728 if (action(cp)) {
729 it = this->packets.erase(it);
730 } else {
731 return false;
734 return true;
738 * Shifts cargo from the front of the packet list for a specific station and
739 * and optional also from the list for "any station", then applies some action
740 * to it.
741 * @tparam Taction Action class or function to be used. It should define
742 * "bool operator()(CargoPacket *)". If true is returned the
743 * cargo packet will be removed from the list. Otherwise it
744 * will be kept and the loop will be aborted.
745 * @param action Action instance to be applied.
746 * @param next Next hop the cargo wants to visit.
747 * @param include_invalid If cargo from the INVALID_STATION list should be
748 * used if necessary.
749 * @return Amount of cargo actually moved.
751 template <class Taction>
752 uint StationCargoList::ShiftCargo(Taction action, StationIDStack next, bool include_invalid)
754 uint max_move = action.MaxMove();
755 while (!next.IsEmpty()) {
756 this->ShiftCargo(action, next.Pop());
757 if (action.MaxMove() == 0) break;
759 if (include_invalid && action.MaxMove() > 0) {
760 this->ShiftCargo(action, INVALID_STATION);
762 return max_move - action.MaxMove();
766 * Truncates where each destination loses roughly the same percentage of its
767 * cargo. This is done by randomizing the selection of packets to be removed.
768 * Optionally count the cargo by origin station.
769 * @param max_move Maximum amount of cargo to remove.
770 * @param cargo_per_source Container for counting the cargo by origin.
771 * @return Amount of cargo actually moved.
773 uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source)
775 max_move = min(max_move, this->count);
776 uint prev_count = this->count;
777 uint moved = 0;
778 uint loop = 0;
779 bool do_count = cargo_per_source != NULL;
780 while (max_move > moved) {
781 for (Iterator it(this->packets.begin()); it != this->packets.end();) {
782 CargoPacket *cp = *it;
783 if (prev_count > max_move && RandomRange(prev_count) < prev_count - max_move) {
784 if (do_count && loop == 0) {
785 (*cargo_per_source)[cp->source] += cp->count;
787 ++it;
788 continue;
790 uint diff = max_move - moved;
791 if (cp->count > diff) {
792 if (diff > 0) {
793 this->RemoveFromCache(cp, diff);
794 cp->Reduce(diff);
795 moved += diff;
797 if (loop > 0) {
798 if (do_count) (*cargo_per_source)[cp->source] -= diff;
799 return moved;
800 } else {
801 if (do_count) (*cargo_per_source)[cp->source] += cp->count;
802 ++it;
804 } else {
805 it = this->packets.erase(it);
806 if (do_count && loop > 0) {
807 (*cargo_per_source)[cp->source] -= cp->count;
809 moved += cp->count;
810 this->RemoveFromCache(cp, cp->count);
811 delete cp;
814 loop++;
816 return moved;
820 * Reserves cargo for loading onto the vehicle.
821 * @param max_move Maximum amount of cargo to reserve.
822 * @param dest VehicleCargoList to reserve for.
823 * @param load_place Tile index of the current station.
824 * @param next_station Next station(s) the loading vehicle will visit.
825 * @return Amount of cargo actually reserved.
827 uint StationCargoList::Reserve(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next_station)
829 return this->ShiftCargo(CargoReservation(this, dest, max_move, load_place), next_station, true);
833 * Loads cargo onto a vehicle. If the vehicle has reserved cargo load that.
834 * Otherwise load cargo from the station.
835 * @param max_move Amount of cargo to load.
836 * @param dest Vehicle cargo list where the cargo resides.
837 * @param load_place The new loaded_at_xy to be assigned to packets being moved.
838 * @param next_station Next station(s) the loading vehicle will visit.
839 * @return Amount of cargo actually loaded.
840 * @note Vehicles may or may not reserve, depending on their orders. The two
841 * modes of loading are exclusive, though. If cargo is reserved we don't
842 * need to load unreserved cargo.
844 uint StationCargoList::Load(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next_station)
846 uint move = min(dest->ActionCount(VehicleCargoList::MTA_LOAD), max_move);
847 if (move > 0) {
848 this->reserved_count -= move;
849 dest->Reassign<VehicleCargoList::MTA_LOAD, VehicleCargoList::MTA_KEEP>(move);
850 return move;
851 } else {
852 return this->ShiftCargo(CargoLoad(this, dest, max_move, load_place), next_station, true);
857 * Routes packets with station "avoid" as next hop to a different place.
858 * @param max_move Maximum amount of cargo to move.
859 * @param dest List to append the cargo to.
860 * @param avoid Station to exclude from routing and current next hop of packets to reroute.
861 * @param avoid2 Additional station to exclude from routing.
862 * @oaram ge GoodsEntry to get the routing info from.
864 uint StationCargoList::Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
866 return this->ShiftCargo(StationCargoReroute(this, dest, max_move, avoid, avoid2, ge), avoid, false);
870 * We have to instantiate everything we want to be usable.
872 template class CargoList<VehicleCargoList, CargoPacketList>;
873 template class CargoList<StationCargoList, StationCargoPacketMap>;
874 template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint, TileOrStationID);