OpenTTD Source: src/cargopacket.cpp Source File

OpenTTD Source 14.0-RC3

cargopacket.cpp
Go to the documentation of this file.
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 */
7
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"
17
18 #include "safeguards.h"
19
20 /* Initialize the cargopacket-pool */
21 CargoPacketPool _cargopacket_pool("CargoPacket");
22 INSTANTIATE_POOL_METHODS(CargoPacket)
23
24
27 CargoPacket::CargoPacket()
28 {
29 this->source_type = SourceType::Industry;
30 this->source_id = INVALID_SOURCE;
31 }
32
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)
47 {
48 assert(count != 0);
49 }
50
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)
66 {
67 assert(count != 0);
68 }
69
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)
90 {
91 assert(count != 0);
92 }
93
99 CargoPacket *CargoPacket::Split(uint new_size)
100 {
101 if (!CargoPacket::CanAllocateItem()) return nullptr;
102
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;
108 }
109
114 void CargoPacket::Merge(CargoPacket *cp)
115 {
116 this->count += cp->count;
117 this->feeder_share += cp->feeder_share;
118 delete cp;
119 }
120
125 void CargoPacket::Reduce(uint count)
126 {
127 assert(count < this->count);
128 this->feeder_share -= this->GetFeederShare(count);
129 this->count -= count;
130 }
131
137 /* static */ void CargoPacket::InvalidateAllFrom(SourceType src_type, SourceID src)
138 {
139 for (CargoPacket *cp : CargoPacket::Iterate()) {
140 if (cp->source_type == src_type && cp->source_id == src) cp->source_id = INVALID_SOURCE;
141 }
142 }
143
148 /* static */ void CargoPacket::InvalidateAllFrom(StationID sid)
149 {
150 for (CargoPacket *cp : CargoPacket::Iterate()) {
151 if (cp->first_station == sid) cp->first_station = INVALID_STATION;
152 }
153 }
154
155 /*
156 *
157 * Cargo list implementation
158 *
159 */
160
164 template <class Tinst, class Tcont>
165 CargoList<Tinst, Tcont>::~CargoList()
166 {
167 for (Iterator it(this->packets.begin()); it != this->packets.end(); ++it) {
168 delete *it;
169 }
170 }
171
176 template <class Tinst, class Tcont>
177 void CargoList<Tinst, Tcont>::OnCleanPool()
178 {
179 this->packets.clear();
180 }
181
188 template <class Tinst, class Tcont>
189 void CargoList<Tinst, Tcont>::RemoveFromCache(const CargoPacket *cp, uint count)
190 {
191 assert(count <= cp->count);
192 this->count -= count;
193 this->cargo_periods_in_transit -= static_cast<uint64_t>(cp->periods_in_transit) * count;
194 }
195
201 template <class Tinst, class Tcont>
202 void CargoList<Tinst, Tcont>::AddToCache(const CargoPacket *cp)
203 {
204 this->count += cp->count;
205 this->cargo_periods_in_transit += static_cast<uint64_t>(cp->periods_in_transit) * cp->count;
206 }
207
209 template <class Tinst, class Tcont>
210 void CargoList<Tinst, Tcont>::InvalidateCache()
211 {
212 this->count = 0;
213 this->cargo_periods_in_transit = 0;
214
215 for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
216 static_cast<Tinst *>(this)->AddToCache(*it);
217 }
218 }
219
227 template <class Tinst, class Tcont>
228 /* static */ bool CargoList<Tinst, Tcont>::TryMerge(CargoPacket *icp, CargoPacket *cp)
229 {
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;
236 }
237 }
238
239 /*
240 *
241 * Vehicle cargo list implementation.
242 *
243 */
244
260 void VehicleCargoList::Append(CargoPacket *cp, MoveToAction action)
261 {
262 assert(cp != nullptr);
263 assert(action == MTA_LOAD ||
264 (action == MTA_KEEP && this->action_counts[MTA_LOAD] == 0));
265 this->AddToMeta(cp, action);
266
267 if (this->count == cp->count) {
268 this->packets.push_back(cp);
269 return;
270 }
271
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;
280 }
281 }
282
283 NOT_REACHED();
284 }
285
294 template<class Taction>
295 void VehicleCargoList::ShiftCargo(Taction action)
296 {
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;
304 }
305 }
306 }
307
316 template<class Taction>
317 void VehicleCargoList::PopCargo(Taction action)
318 {
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;
330 }
331 } else {
332 break;
333 }
334 }
335 }
336
343 void VehicleCargoList::RemoveFromCache(const CargoPacket *cp, uint count)
344 {
345 this->feeder_share -= cp->GetFeederShare(count);
346 this->Parent::RemoveFromCache(cp, count);
347 }
348
354 void VehicleCargoList::AddToCache(const CargoPacket *cp)
355 {
356 this->feeder_share += cp->feeder_share;
357 this->Parent::AddToCache(cp);
358 }
359
366 void VehicleCargoList::RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
367 {
368 assert(count <= this->action_counts[action]);
369 this->AssertCountConsistency();
370 this->RemoveFromCache(cp, count);
371 this->action_counts[action] -= count;
372 this->AssertCountConsistency();
373 }
374
380 void VehicleCargoList::AddToMeta(const CargoPacket *cp, MoveToAction action)
381 {
382 this->AssertCountConsistency();
383 this->AddToCache(cp);
384 this->action_counts[action] += cp->count;
385 this->AssertCountConsistency();
386 }
387
391 void VehicleCargoList::AgeCargo()
392 {
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;
396
397 cp->periods_in_transit++;
398 this->cargo_periods_in_transit += cp->count;
399 }
400 }
401
411 /* static */ VehicleCargoList::MoveToAction VehicleCargoList::ChooseAction(const CargoPacket *cp, StationID cargo_next,
412 StationID current_station, bool accepted, StationIDStack next_station)
413 {
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;
422 }
423 }
424
439 bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment, TileIndex current_tile)
440 {
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;
447
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;
454
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);
475 }
476 if (new_shares.GetShares()->empty()) {
477 cargo_next = INVALID_STATION;
478 } else {
479 cargo_next = new_shares.GetVia();
480 }
481 }
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;
487 }
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);
494 }
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);
501 }
502 }
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();
522 }
523 this->action_counts[action] += cp->count;
524 sum += cp->count;
525 }
526 this->AssertCountConsistency();
527 return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0;
528 }
529
531 void VehicleCargoList::InvalidateCache()
532 {
533 this->feeder_share = 0;
534 this->Parent::InvalidateCache();
535 }
536
549 template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
550 uint VehicleCargoList::Reassign(uint max_move)
551 {
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;
558 }
559
566 template<>
567 uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move)
568 {
569 max_move = std::min(this->action_counts[MTA_DELIVER], max_move);
570
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);
580 }
581 cp->next_hop = INVALID_STATION;
582 }
583
584 this->action_counts[MTA_DELIVER] -= max_move;
585 this->action_counts[MTA_TRANSFER] += max_move;
586 return max_move;
587 }
588
597 uint VehicleCargoList::Return(uint max_move, StationCargoList *dest, StationID next, TileIndex current_tile)
598 {
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;
602 }
603
610 uint VehicleCargoList::Shift(uint max_move, VehicleCargoList *dest)
611 {
612 max_move = std::min(this->count, max_move);
613 this->PopCargo(CargoShift(this, dest, max_move));
614 return max_move;
615 }
616
626 uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment, TileIndex current_tile)
627 {
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;
633 }
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;
638 }
639 return moved;
640 }
641
648 uint VehicleCargoList::Truncate(uint max_move)
649 {
650 max_move = std::min(this->count, max_move);
651 this->PopCargo(CargoRemoval<VehicleCargoList>(this, max_move));
652 return max_move;
653 }
654
663 uint VehicleCargoList::Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
664 {
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;
668 }
669
670 /*
671 *
672 * Station cargo list implementation.
673 *
674 */
675
684 void StationCargoList::Append(CargoPacket *cp, StationID next)
685 {
686 assert(cp != nullptr);
687 this->AddToCache(cp);
688
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;
693 }
694
695 /* The packet could not be merged with another one */
696 list.push_back(cp);
697 }
698
711 template <class Taction>
712 bool StationCargoList::ShiftCargo(Taction &action, StationID next)
713 {
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;
722 }
723 }
724 return true;
725 }
726
741 template <class Taction>
742 uint StationCargoList::ShiftCargo(Taction action, StationIDStack next, bool include_invalid)
743 {
744 uint max_move = action.MaxMove();
745 while (!next.IsEmpty()) {
746 this->ShiftCargo(action, next.Pop());
747 if (action.MaxMove() == 0) break;
748 }
749 if (include_invalid && action.MaxMove() > 0) {
750 this->ShiftCargo(action, INVALID_STATION);
751 }
752 return max_move - action.MaxMove();
753 }
754
763 uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source)
764 {
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;
776 }
777 ++it;
778 continue;
779 }
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;
786 }
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;
793 }
794 } else {
795 it = this->packets.erase(it);
796 if (do_count && loop > 0) {
797 (*cargo_per_source)[cp->first_station] -= cp->count;
798 }
799 moved += cp->count;
800 this->RemoveFromCache(cp, cp->count);
801 delete cp;
802 }
803 }
804 loop++;
805 }
806 return moved;
807 }
808
817 uint StationCargoList::Reserve(uint max_move, VehicleCargoList *dest, StationIDStack next_station, TileIndex current_tile)
818 {
819 return this->ShiftCargo(CargoReservation(this, dest, max_move, current_tile), next_station, true);
820 }
821
834 uint StationCargoList::Load(uint max_move, VehicleCargoList *dest, StationIDStack next_station, TileIndex current_tile)
835 {
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);
843 }
844 }
845
854 uint StationCargoList::Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
855 {
856 return this->ShiftCargo(StationCargoReroute(this, dest, max_move, avoid, avoid2, ge), avoid, false);
857 }
858
859 /*
860 * We have to instantiate everything we want to be usable.
861 */
862 template class CargoList<VehicleCargoList, CargoPacketList>;
863 template class CargoList<StationCargoList, StationCargoPacketMap>;
864 template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint);
CargoPacket::feeder_share
Money feeder_share
Value of feeder pickup to be paid for on delivery of cargo.
Definition: cargopacket.h:51
CargoList< VehicleCargoList, CargoPacketList >::ConstIterator
CargoPacketList ::const_iterator ConstIterator
The const iterator for our container.
Definition: cargopacket.h:290
SmallStack
Minimal stack that uses a pool to avoid pointers.
Definition: smallstack_type.hpp:135
VehicleCargoList::ShiftCargo
void ShiftCargo(Taction action)
Shifts cargo from the front of the packet list and applies some action to it.
Definition: cargopacket.cpp:295
SmallStack::Pop
Titem Pop()
Pop an item from the stack.
Definition: smallstack_type.hpp:212
OUFB_UNLOAD
@ OUFB_UNLOAD
Force unloading all cargo onto the platform, possibly not getting paid.
Definition: order_type.h:54
CargoList< VehicleCargoList, CargoPacketList >::MoveToAction
MoveToAction
Kind of actions that could be done with packets on move.
Definition: cargopacket.h:295
CargoReservation
Action of reserving cargo from a station to be loaded onto a vehicle.
Definition: cargoaction.h:92
VehicleCargoList::Shift
uint Shift(uint max_move, VehicleCargoList *dest)
Shifts cargo between two vehicles.
Definition: cargopacket.cpp:610
CargoList::OnCleanPool
void OnCleanPool()
Empty the cargo list, but don't free the cargo packets; the cargo packets are cleaned by CargoPacket'...
Definition: cargopacket.cpp:177
VehicleCargoList::Reroute
uint Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
Routes packets with station "avoid" as next hop to a different place.
Definition: cargopacket.cpp:663
VehicleCargoList::Stage
bool Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8_t order_flags, const GoodsEntry *ge, CargoPayment *payment, TileIndex current_tile)
Stages cargo for unloading.
Definition: cargopacket.cpp:439
CargoList::TryMerge
static bool TryMerge(CargoPacket *cp, CargoPacket *icp)
Tries to merge the second packet into the first and return if that was successful.
Definition: cargopacket.cpp:228
StationCargoList::Load
uint Load(uint max_move, VehicleCargoList *dest, StationIDStack next, TileIndex current_tile)
Loads cargo onto a vehicle.
Definition: cargopacket.cpp:834
VehicleCargoList::Unload
uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment, TileIndex current_tile)
Unloads cargo at the given station.
Definition: cargopacket.cpp:626
OUFB_TRANSFER
@ OUFB_TRANSFER
Transfer all cargo onto the platform.
Definition: order_type.h:55
economy_base.h
CargoPacket::InvalidateAllFrom
static void InvalidateAllFrom(SourceType src_type, SourceID src)
Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source.
Definition: cargopacket.cpp:137
StationCargoList::reserved_count
uint reserved_count
Amount of cargo being reserved for loading.
Definition: cargopacket.h:534
VehicleCargoList::InvalidateCache
void InvalidateCache()
Invalidates the cached data and rebuild it.
Definition: cargopacket.cpp:531
VehicleCargoList::RemoveFromMeta
void RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
Removes a packet or part of it from the metadata.
Definition: cargopacket.cpp:366
VehicleCargoList::AssertCountConsistency
void AssertCountConsistency() const
Assert that the designation counts add up.
Definition: cargopacket.h:368
StationCargoList
CargoList that is used for stations.
Definition: cargopacket.h:529
CargoList::~CargoList
~CargoList()
Destroy the cargolist ("frees" all cargo packets).
Definition: cargopacket.cpp:165
CargoReturn
Action of returning previously reserved cargo from the vehicle to the station.
Definition: cargoaction.h:100
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
VehicleCargoList::Return
uint Return(uint max_move, StationCargoList *dest, StationID next_station, TileIndex current_tile)
Returns reserved cargo to the station and removes it from the cache.
Definition: cargopacket.cpp:597
VehicleCargoList::AddToCache
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.
Definition: cargopacket.cpp:354
RandomRange
static uint32_t RandomRange(uint32_t limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
VehicleCargoList
CargoList that is used for vehicles.
Definition: cargopacket.h:351
CargoPacket::first_station
StationID first_station
The station where the cargo came from first.
Definition: cargopacket.h:63
CargoPacket::Merge
void Merge(CargoPacket *cp)
Merge another packet into this one.
Definition: cargopacket.cpp:114
CargoPacket::count
uint16_t count
The amount of cargo in this packet.
Definition: cargopacket.h:48
FlowStat
Flow statistics telling how much flow should be sent along a link.
Definition: station_base.h:32
cargoaction.h
StationCargoList::Reroute
uint Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
Routes packets with station "avoid" as next hop to a different place.
Definition: cargopacket.cpp:854
VehicleCargoReroute
Action of rerouting cargo staged for transfer in a vehicle.
Definition: cargoaction.h:139
VehicleCargoList::Truncate
uint Truncate(uint max_move=UINT_MAX)
Truncates the cargo in this list to the given amount.
Definition: cargopacket.cpp:648
CargoPacket::GetFeederShare
Money GetFeederShare() const
Gets the amount of money already paid to earlier vehicles in the feeder chain.
Definition: cargopacket.h:168
VehicleCargoList::Reassign
uint Reassign(uint max_move)
Moves some cargo from one designation to another.
Definition: cargopacket.cpp:550
VehicleCargoList::AgeCargo
void AgeCargo()
Ages the all cargo in this list.
Definition: cargopacket.cpp:391
VehicleCargoList::feeder_share
Money feeder_share
Cache for the feeder share.
Definition: cargopacket.h:356
CargoPayment::PayTransfer
Money PayTransfer(const CargoPacket *cp, uint count, TileIndex current_tile)
Handle payment for transfer of the given cargo packet.
Definition: economy.cpp:1255
VehicleCargoList::AddToMeta
void AddToMeta(const CargoPacket *cp, MoveToAction action)
Adds a packet to the metadata.
Definition: cargopacket.cpp:380
FlowStat::GetShares
const SharesMap * GetShares() const
Get the actual shares as a const pointer so that they can be iterated over.
Definition: station_base.h:88
MultiMap::erase
iterator erase(iterator it)
Erase the value pointed to by an iterator.
Definition: multimap.hpp:299
CargoPayment
Helper class to perform the cargo payment.
Definition: economy_base.h:24
VehicleCargoList::Append
void Append(CargoPacket *cp, MoveToAction action=MTA_KEEP)
Appends the given cargo packet.
Definition: cargopacket.cpp:260
CargoDelivery
Action of final delivery of cargo.
Definition: cargoaction.h:39
CargoPacket::Split
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
Definition: cargopacket.cpp:99
CargoList::InvalidateCache
void InvalidateCache()
Invalidates the cached data and rebuilds it.
Definition: cargopacket.cpp:210
SourceID
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:143
VehicleCargoList::ChooseAction
static MoveToAction ChooseAction(const CargoPacket *cp, StationID cargo_next, StationID current_station, bool accepted, StationIDStack next_station)
Choose action to be performed with the given cargo packet.
Definition: cargopacket.cpp:411
CargoList< VehicleCargoList, CargoPacketList >::count
uint count
Cache for the number of cargo entities.
Definition: cargopacket.h:306
safeguards.h
CargoPacket::MAX_COUNT
static const uint16_t MAX_COUNT
Maximum number of items in a single cargo packet.
Definition: cargopacket.h:74
CargoPacket::AddFeederShare
void AddFeederShare(Money new_share)
Adds some feeder share to the packet.
Definition: cargopacket.h:149
CargoList< VehicleCargoList, CargoPacketList >::MTA_TRANSFER
@ MTA_TRANSFER
Transfer the cargo to the station.
Definition: cargopacket.h:297
VehicleCargoList::action_counts
uint action_counts[NUM_MOVE_TO_ACTION]
Counts of cargo to be transferred, delivered, kept and loaded.
Definition: cargopacket.h:357
SourceType::Industry
@ Industry
Source/destination is an industry.
stdafx.h
CargoList< VehicleCargoList, CargoPacketList >::cargo_periods_in_transit
uint64_t cargo_periods_in_transit
Cache for the sum of number of cargo aging periods in transit of each entity; comparable to man-hours...
Definition: cargopacket.h:307
VehicleCargoList::RemoveFromCache
void RemoveFromCache(const CargoPacket *cp, uint count)
Update the cached values to reflect the removal of this packet or part of it.
Definition: cargopacket.cpp:343
CargoPacket::Reduce
void Reduce(uint count)
Reduce the packet by the given amount and remove the feeder share.
Definition: cargopacket.cpp:125
MultiMap::equal_range
std::pair< iterator, iterator > equal_range(const Tkey &key)
Get a pair of iterators specifying a range of items with equal keys.
Definition: multimap.hpp:357
CargoList::RemoveFromCache
void RemoveFromCache(const CargoPacket *cp, uint count)
Update the cached values to reflect the removal of this packet or part of it.
Definition: cargopacket.cpp:189
CargoShift
Action of shifting cargo from one vehicle to another.
Definition: cargoaction.h:111
CargoList< VehicleCargoList, CargoPacketList >::ReverseIterator
CargoPacketList ::reverse_iterator ReverseIterator
The reverse iterator for our container.
Definition: cargopacket.h:288
CargoPacket::periods_in_transit
uint16_t periods_in_transit
Amount of cargo aging periods this packet has been in transit.
Definition: cargopacket.h:49
CargoList< VehicleCargoList, CargoPacketList >::packets
CargoPacketList packets
The cargo packets in this list.
Definition: cargopacket.h:309
CargoList< VehicleCargoList, CargoPacketList >
GoodsEntry
Stores station stats for a single cargo.
Definition: station_base.h:166
station_base.h
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:137
Pool::PoolItem<&_cargopacket_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
CargoPacket::next_hop
StationID next_hop
Station where the cargo wants to go next.
Definition: cargopacket.h:64
Pool
Base class for all pools.
Definition: pool_type.hpp:80
CargoTransfer
Action of transferring cargo from a vehicle to a station.
Definition: cargoaction.h:72
GoodsEntry::flows
FlowStatMap flows
Planned flows through this station.
Definition: station_base.h:211
OUFB_NO_UNLOAD
@ OUFB_NO_UNLOAD
Totally no unloading will be done.
Definition: order_type.h:56
CargoRemoval< VehicleCargoList >
CargoList< VehicleCargoList, CargoPacketList >::MTA_LOAD
@ MTA_LOAD
Load the cargo from the station.
Definition: cargopacket.h:300
Pool::PoolItem<&_cargopacket_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:309
INVALID_SOURCE
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:144
FlowStat::GetVia
StationID GetVia() const
Get a station a package can be routed to.
Definition: station_base.h:130
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:237
StationCargoList::ShiftCargo
bool ShiftCargo(Taction &action, StationID next)
Shifts cargo from the front of the packet list for a specific station and applies some action to it.
Definition: cargopacket.cpp:712
CargoList< VehicleCargoList, CargoPacketList >::Iterator
CargoPacketList ::iterator Iterator
The iterator for our container.
Definition: cargopacket.h:286
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:40
random_func.hpp
SmallStack::IsEmpty
bool IsEmpty() const
Check if the stack is empty.
Definition: smallstack_type.hpp:243
CargoList< VehicleCargoList, CargoPacketList >::MTA_KEEP
@ MTA_KEEP
Keep the cargo in the vehicle.
Definition: cargopacket.h:299
OverflowSafeInt< int64_t >
VehicleCargoList::ActionCount
uint ActionCount(MoveToAction action) const
Returns the amount of cargo designated for a given purpose.
Definition: cargopacket.h:424
CargoList< VehicleCargoList, CargoPacketList >::MTA_DELIVER
@ MTA_DELIVER
Deliver the cargo to some town or industry.
Definition: cargopacket.h:298
StationCargoList::Append
void Append(CargoPacket *cp, StationID next)
Appends the given cargo packet to the range of packets with the same next station.
Definition: cargopacket.cpp:684
CargoPacket::Count
uint16_t Count() const
Gets the number of 'items' in this packet.
Definition: cargopacket.h:158
StationCargoList::Reserve
uint Reserve(uint max_move, VehicleCargoList *dest, StationIDStack next, TileIndex current_tile)
Reserves cargo for loading onto the vehicle.
Definition: cargopacket.cpp:817
VehicleCargoList::PopCargo
void PopCargo(Taction action)
Pops cargo from the back of the packet list and applies some action to it.
Definition: cargopacket.cpp:317
pool_func.hpp
StationCargoReroute
Action of rerouting cargo in a station.
Definition: cargoaction.h:131
_cargopacket_pool
CargoPacketPool _cargopacket_pool("CargoPacket")
The actual pool with cargo packets.
order_type.h
StationCargoList::Truncate
uint Truncate(uint max_move=UINT_MAX, StationCargoAmountMap *cargo_per_source=nullptr)
Truncates where each destination loses roughly the same percentage of its cargo.
Definition: cargopacket.cpp:763
SmallStack::Contains
bool Contains(const Titem &item) const
Check if the given item is contained in the stack.
Definition: smallstack_type.hpp:253
FlowStat::ChangeShare
void ChangeShare(StationID st, int flow)
Change share for specified station.
Definition: station_cmd.cpp:4644
CargoPacket::CargoPacket
CargoPacket()
Create a new packet for savegame loading.
Definition: cargopacket.cpp:27
CargoLoad
Action of loading cargo from a station onto a vehicle.
Definition: cargoaction.h:82
CargoList::AddToCache
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.
Definition: cargopacket.cpp:202