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 
8 /** @file cargopacket.cpp Implementation of the cargo packets. */
9 
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");
INSTANTIATE_POOL_METHODS(CargoPacket)22 INSTANTIATE_POOL_METHODS(CargoPacket)
23 
24 /**
25  * Create a new packet for savegame loading.
26  */
27 CargoPacket::CargoPacket()
28 {
29 	this->source_type = ST_INDUSTRY;
30 	this->source_id   = INVALID_SOURCE;
31 }
32 
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.
43  */
CargoPacket(StationID source,TileIndex source_xy,uint16 count,SourceType source_type,SourceID source_id)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)
52 {
53 	assert(count != 0);
54 	this->source_type  = source_type;
55 }
56 
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.
70  */
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)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)
79 {
80 	assert(count != 0);
81 	this->source_type = source_type;
82 }
83 
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 nullptr if no packet could be allocated!
88  */
Split(uint new_size)89 CargoPacket *CargoPacket::Split(uint new_size)
90 {
91 	if (!CargoPacket::CanAllocateItem()) return nullptr;
92 
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;
98 }
99 
100 /**
101  * Merge another packet into this one.
102  * @param cp Packet to be merged in.
103  */
Merge(CargoPacket * cp)104 void CargoPacket::Merge(CargoPacket *cp)
105 {
106 	this->count += cp->count;
107 	this->feeder_share += cp->feeder_share;
108 	delete cp;
109 }
110 
111 /**
112  * Reduce the packet by the given amount and remove the feeder share.
113  * @param count Amount to be removed.
114  */
Reduce(uint count)115 void CargoPacket::Reduce(uint count)
116 {
117 	assert(count < this->count);
118 	this->feeder_share -= this->FeederShare(count);
119 	this->count -= count;
120 }
121 
122 /**
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.
126  */
InvalidateAllFrom(SourceType src_type,SourceID src)127 /* static */ void CargoPacket::InvalidateAllFrom(SourceType src_type, SourceID src)
128 {
129 	for (CargoPacket *cp : CargoPacket::Iterate()) {
130 		if (cp->source_type == src_type && cp->source_id == src) cp->source_id = INVALID_SOURCE;
131 	}
132 }
133 
134 /**
135  * Invalidates (sets source to INVALID_STATION) all cargo packets from given station.
136  * @param sid Station that gets removed.
137  */
InvalidateAllFrom(StationID sid)138 /* static */ void CargoPacket::InvalidateAllFrom(StationID sid)
139 {
140 	for (CargoPacket *cp : CargoPacket::Iterate()) {
141 		if (cp->source == sid) cp->source = INVALID_STATION;
142 	}
143 }
144 
145 /*
146  *
147  * Cargo list implementation
148  *
149  */
150 
151 /**
152  * Destroy the cargolist ("frees" all cargo packets).
153  */
154 template <class Tinst, class Tcont>
~CargoList()155 CargoList<Tinst, Tcont>::~CargoList()
156 {
157 	for (Iterator it(this->packets.begin()); it != this->packets.end(); ++it) {
158 		delete *it;
159 	}
160 }
161 
162 /**
163  * Empty the cargo list, but don't free the cargo packets;
164  * the cargo packets are cleaned by CargoPacket's CleanPool.
165  */
166 template <class Tinst, class Tcont>
OnCleanPool()167 void CargoList<Tinst, Tcont>::OnCleanPool()
168 {
169 	this->packets.clear();
170 }
171 
172 /**
173  * Update the cached values to reflect the removal of this packet or part of it.
174  * Decreases count and days_in_transit.
175  * @param cp Packet to be removed from cache.
176  * @param count Amount of cargo from the given packet to be removed.
177  */
178 template <class Tinst, class Tcont>
RemoveFromCache(const CargoPacket * cp,uint count)179 void CargoList<Tinst, Tcont>::RemoveFromCache(const CargoPacket *cp, uint count)
180 {
181 	assert(count <= cp->count);
182 	this->count                 -= count;
183 	this->cargo_days_in_transit -= cp->days_in_transit * count;
184 }
185 
186 /**
187  * Update the cache to reflect adding of this packet.
188  * Increases count and days_in_transit.
189  * @param cp New packet to be inserted.
190  */
191 template <class Tinst, class Tcont>
AddToCache(const CargoPacket * cp)192 void CargoList<Tinst, Tcont>::AddToCache(const CargoPacket *cp)
193 {
194 	this->count                 += cp->count;
195 	this->cargo_days_in_transit += cp->days_in_transit * cp->count;
196 }
197 
198 /** Invalidates the cached data and rebuilds it. */
199 template <class Tinst, class Tcont>
InvalidateCache()200 void CargoList<Tinst, Tcont>::InvalidateCache()
201 {
202 	this->count = 0;
203 	this->cargo_days_in_transit = 0;
204 
205 	for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
206 		static_cast<Tinst *>(this)->AddToCache(*it);
207 	}
208 }
209 
210 /**
211  * Tries to merge the second packet into the first and return if that was
212  * successful.
213  * @param icp Packet to be merged into.
214  * @param cp Packet to be eliminated.
215  * @return If the packets could be merged.
216  */
217 template <class Tinst, class Tcont>
TryMerge(CargoPacket * icp,CargoPacket * cp)218 /* static */ bool CargoList<Tinst, Tcont>::TryMerge(CargoPacket *icp, CargoPacket *cp)
219 {
220 	if (Tinst::AreMergable(icp, cp) &&
221 			icp->count + cp->count <= CargoPacket::MAX_COUNT) {
222 		icp->Merge(cp);
223 		return true;
224 	} else {
225 		return false;
226 	}
227 }
228 
229 /*
230  *
231  * Vehicle cargo list implementation.
232  *
233  */
234 
235 /**
236  * Appends the given cargo packet. Tries to merge it with another one in the
237  * packets list. If no fitting packet is found, appends it. You can only append
238  * packets to the ranges of packets designated for keeping or loading.
239  * Furthermore if there are already packets reserved for loading you cannot
240  * directly add packets to the "keep" list. You first have to load the reserved
241  * ones.
242  * @warning After appending this packet may not exist anymore!
243  * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
244  * @param cp Cargo packet to add.
245  * @param action Either MTA_KEEP if you want to add the packet directly or MTA_LOAD
246  * if you want to reserve it first.
247  * @pre cp != nullptr
248  * @pre action == MTA_LOAD || (action == MTA_KEEP && this->designation_counts[MTA_LOAD] == 0)
249  */
Append(CargoPacket * cp,MoveToAction action)250 void VehicleCargoList::Append(CargoPacket *cp, MoveToAction action)
251 {
252 	assert(cp != nullptr);
253 	assert(action == MTA_LOAD ||
254 			(action == MTA_KEEP && this->action_counts[MTA_LOAD] == 0));
255 	this->AddToMeta(cp, action);
256 
257 	if (this->count == cp->count) {
258 		this->packets.push_back(cp);
259 		return;
260 	}
261 
262 	uint sum = cp->count;
263 	for (ReverseIterator it(this->packets.rbegin()); it != this->packets.rend(); it++) {
264 		CargoPacket *icp = *it;
265 		if (VehicleCargoList::TryMerge(icp, cp)) return;
266 		sum += icp->count;
267 		if (sum >= this->action_counts[action]) {
268 			this->packets.push_back(cp);
269 			return;
270 		}
271 	}
272 
273 	NOT_REACHED();
274 }
275 
276 /**
277  * Shifts cargo from the front of the packet list and applies some action to it.
278  * @tparam Taction Action class or function to be used. It should define
279  *                 "bool operator()(CargoPacket *)". If true is returned the
280  *                 cargo packet will be removed from the list. Otherwise it
281  *                 will be kept and the loop will be aborted.
282  * @param action Action instance to be applied.
283  */
284 template<class Taction>
ShiftCargo(Taction action)285 void VehicleCargoList::ShiftCargo(Taction action)
286 {
287 	Iterator it(this->packets.begin());
288 	while (it != this->packets.end() && action.MaxMove() > 0) {
289 		CargoPacket *cp = *it;
290 		if (action(cp)) {
291 			it = this->packets.erase(it);
292 		} else {
293 			break;
294 		}
295 	}
296 }
297 
298 /**
299  * Pops cargo from the back of the packet list and applies some action to it.
300  * @tparam Taction Action class or function to be used. It should define
301  *                 "bool operator()(CargoPacket *)". If true is returned the
302  *                 cargo packet will be removed from the list. Otherwise it
303  *                 will be kept and the loop will be aborted.
304  * @param action Action instance to be applied.
305  */
306 template<class Taction>
PopCargo(Taction action)307 void VehicleCargoList::PopCargo(Taction action)
308 {
309 	if (this->packets.empty()) return;
310 	Iterator it(--(this->packets.end()));
311 	Iterator begin(this->packets.begin());
312 	while (action.MaxMove() > 0) {
313 		CargoPacket *cp = *it;
314 		if (action(cp)) {
315 			if (it != begin) {
316 				this->packets.erase(it--);
317 			} else {
318 				this->packets.erase(it);
319 				break;
320 			}
321 		} else {
322 			break;
323 		}
324 	}
325 }
326 
327 /**
328  * Update the cached values to reflect the removal of this packet or part of it.
329  * Decreases count, feeder share and days_in_transit.
330  * @param cp Packet to be removed from cache.
331  * @param count Amount of cargo from the given packet to be removed.
332  */
RemoveFromCache(const CargoPacket * cp,uint count)333 void VehicleCargoList::RemoveFromCache(const CargoPacket *cp, uint count)
334 {
335 	this->feeder_share -= cp->FeederShare(count);
336 	this->Parent::RemoveFromCache(cp, count);
337 }
338 
339 /**
340  * Update the cache to reflect adding of this packet.
341  * Increases count, feeder share and days_in_transit.
342  * @param cp New packet to be inserted.
343  */
AddToCache(const CargoPacket * cp)344 void VehicleCargoList::AddToCache(const CargoPacket *cp)
345 {
346 	this->feeder_share += cp->feeder_share;
347 	this->Parent::AddToCache(cp);
348 }
349 
350 /**
351  * Removes a packet or part of it from the metadata.
352  * @param cp Packet to be removed.
353  * @param action MoveToAction of the packet (for updating the counts).
354  * @param count Amount of cargo to be removed.
355  */
RemoveFromMeta(const CargoPacket * cp,MoveToAction action,uint count)356 void VehicleCargoList::RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
357 {
358 	assert(count <= this->action_counts[action]);
359 	this->AssertCountConsistency();
360 	this->RemoveFromCache(cp, count);
361 	this->action_counts[action] -= count;
362 	this->AssertCountConsistency();
363 }
364 
365 /**
366  * Adds a packet to the metadata.
367  * @param cp Packet to be added.
368  * @param action MoveToAction of the packet.
369  */
AddToMeta(const CargoPacket * cp,MoveToAction action)370 void VehicleCargoList::AddToMeta(const CargoPacket *cp, MoveToAction action)
371 {
372 	this->AssertCountConsistency();
373 	this->AddToCache(cp);
374 	this->action_counts[action] += cp->count;
375 	this->AssertCountConsistency();
376 }
377 
378 /**
379  * Ages the all cargo in this list.
380  */
AgeCargo()381 void VehicleCargoList::AgeCargo()
382 {
383 	for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
384 		CargoPacket *cp = *it;
385 		/* If we're at the maximum, then we can't increase no more. */
386 		if (cp->days_in_transit == 0xFF) continue;
387 
388 		cp->days_in_transit++;
389 		this->cargo_days_in_transit += cp->count;
390 	}
391 }
392 
393 /**
394  * Sets loaded_at_xy to the current station for all cargo to be transferred.
395  * This is done when stopping or skipping while the vehicle is unloading. In
396  * that case the vehicle will get part of its transfer credits early and it may
397  * get more transfer credits than it's entitled to.
398  * @param xy New loaded_at_xy for the cargo.
399  */
SetTransferLoadPlace(TileIndex xy)400 void VehicleCargoList::SetTransferLoadPlace(TileIndex xy)
401 {
402 	uint sum = 0;
403 	for (Iterator it = this->packets.begin(); sum < this->action_counts[MTA_TRANSFER]; ++it) {
404 		CargoPacket *cp = *it;
405 		cp->loaded_at_xy = xy;
406 		sum += cp->count;
407 	}
408 }
409 
410 /**
411  * Choose action to be performed with the given cargo packet.
412  * @param cp The packet.
413  * @param cargo_next Next hop the cargo wants to pass.
414  * @param current_station Current station of the vehicle carrying the cargo.
415  * @param accepted If the cargo is accepted at the current station.
416  * @param next_station Next station(s) the vehicle may stop at.
417  * @return MoveToAction to be performed.
418  */
ChooseAction(const CargoPacket * cp,StationID cargo_next,StationID current_station,bool accepted,StationIDStack next_station)419 /* static */ VehicleCargoList::MoveToAction VehicleCargoList::ChooseAction(const CargoPacket *cp, StationID cargo_next,
420 		StationID current_station, bool accepted, StationIDStack next_station)
421 {
422 	if (cargo_next == INVALID_STATION) {
423 		return (accepted && cp->source != current_station) ? MTA_DELIVER : MTA_KEEP;
424 	} else if (cargo_next == current_station) {
425 		return MTA_DELIVER;
426 	} else if (next_station.Contains(cargo_next)) {
427 		return MTA_KEEP;
428 	} else {
429 		return MTA_TRANSFER;
430 	}
431 }
432 
433 /**
434  * Stages cargo for unloading. The cargo is sorted so that packets to be
435  * transferred, delivered or kept are in consecutive chunks in the list. At the
436  * same time the designation_counts are updated to reflect the size of those
437  * chunks.
438  * @param accepted If the cargo will be accepted at the station.
439  * @param current_station ID of the station.
440  * @param next_station ID of the station the vehicle will go to next.
441  * @param order_flags OrderUnloadFlags that will apply to the unload operation.
442  * @param ge GoodsEntry for getting the flows.
443  * @param payment Payment object for registering transfers.
444  * return If any cargo will be unloaded.
445  */
Stage(bool accepted,StationID current_station,StationIDStack next_station,uint8 order_flags,const GoodsEntry * ge,CargoPayment * payment)446 bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8 order_flags, const GoodsEntry *ge, CargoPayment *payment)
447 {
448 	this->AssertCountConsistency();
449 	assert(this->action_counts[MTA_LOAD] == 0);
450 	this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_DELIVER] = this->action_counts[MTA_KEEP] = 0;
451 	Iterator deliver = this->packets.end();
452 	Iterator it = this->packets.begin();
453 	uint sum = 0;
454 
455 	bool force_keep = (order_flags & OUFB_NO_UNLOAD) != 0;
456 	bool force_unload = (order_flags & OUFB_UNLOAD) != 0;
457 	bool force_transfer = (order_flags & (OUFB_TRANSFER | OUFB_UNLOAD)) != 0;
458 	assert(this->count > 0 || it == this->packets.end());
459 	while (sum < this->count) {
460 		CargoPacket *cp = *it;
461 
462 		this->packets.erase(it++);
463 		StationID cargo_next = INVALID_STATION;
464 		MoveToAction action = MTA_LOAD;
465 		if (force_keep) {
466 			action = MTA_KEEP;
467 		} else if (force_unload && accepted && cp->source != current_station) {
468 			action = MTA_DELIVER;
469 		} else if (force_transfer) {
470 			action = MTA_TRANSFER;
471 			/* We cannot send the cargo to any of the possible next hops and
472 			 * also not to the current station. */
473 			FlowStatMap::const_iterator flow_it(ge->flows.find(cp->source));
474 			if (flow_it == ge->flows.end()) {
475 				cargo_next = INVALID_STATION;
476 			} else {
477 				FlowStat new_shares = flow_it->second;
478 				new_shares.ChangeShare(current_station, INT_MIN);
479 				StationIDStack excluded = next_station;
480 				while (!excluded.IsEmpty() && !new_shares.GetShares()->empty()) {
481 					new_shares.ChangeShare(excluded.Pop(), INT_MIN);
482 				}
483 				if (new_shares.GetShares()->empty()) {
484 					cargo_next = INVALID_STATION;
485 				} else {
486 					cargo_next = new_shares.GetVia();
487 				}
488 			}
489 		} else {
490 			/* Rewrite an invalid source station to some random other one to
491 			 * avoid keeping the cargo in the vehicle forever. */
492 			if (cp->source == INVALID_STATION && !ge->flows.empty()) {
493 				cp->source = ge->flows.begin()->first;
494 			}
495 			bool restricted = false;
496 			FlowStatMap::const_iterator flow_it(ge->flows.find(cp->source));
497 			if (flow_it == ge->flows.end()) {
498 				cargo_next = INVALID_STATION;
499 			} else {
500 				cargo_next = flow_it->second.GetViaWithRestricted(restricted);
501 			}
502 			action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
503 			if (restricted && action == MTA_TRANSFER) {
504 				/* If the flow is restricted we can't transfer to it. Choose an
505 				 * unrestricted one instead. */
506 				cargo_next = flow_it->second.GetVia();
507 				action = VehicleCargoList::ChooseAction(cp, cargo_next, current_station, accepted, next_station);
508 			}
509 		}
510 		Money share;
511 		switch (action) {
512 			case MTA_KEEP:
513 				this->packets.push_back(cp);
514 				if (deliver == this->packets.end()) --deliver;
515 				break;
516 			case MTA_DELIVER:
517 				this->packets.insert(deliver, cp);
518 				break;
519 			case MTA_TRANSFER:
520 				this->packets.push_front(cp);
521 				/* Add feeder share here to allow reusing field for next station. */
522 				share = payment->PayTransfer(cp, cp->count);
523 				cp->AddFeederShare(share);
524 				this->feeder_share += share;
525 				cp->next_station = cargo_next;
526 				break;
527 			default:
528 				NOT_REACHED();
529 		}
530 		this->action_counts[action] += cp->count;
531 		sum += cp->count;
532 	}
533 	this->AssertCountConsistency();
534 	return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0;
535 }
536 
537 /** Invalidates the cached data and rebuild it. */
InvalidateCache()538 void VehicleCargoList::InvalidateCache()
539 {
540 	this->feeder_share = 0;
541 	this->Parent::InvalidateCache();
542 }
543 
544 /**
545  * Moves some cargo from one designation to another. You can only move
546  * between adjacent designations. E.g. you can keep cargo that was previously
547  * reserved (MTA_LOAD), but you can't reserve cargo that's marked as to be
548  * delivered. Furthermore, as this method doesn't change the actual packets,
549  * you cannot move cargo from or to MTA_TRANSFER. You need a specialized
550  * template method for that.
551  * @tparam from Previous designation of cargo.
552  * @tparam to New designation of cargo.
553  * @param max_move Maximum amount of cargo to reassign.
554  * @return Amount of cargo actually reassigned.
555  */
556 template<VehicleCargoList::MoveToAction Tfrom, VehicleCargoList::MoveToAction Tto>
Reassign(uint max_move,TileOrStationID)557 uint VehicleCargoList::Reassign(uint max_move, TileOrStationID)
558 {
559 	static_assert(Tfrom != MTA_TRANSFER && Tto != MTA_TRANSFER);
560 	static_assert(Tfrom - Tto == 1 || Tto - Tfrom == 1);
561 	max_move = std::min(this->action_counts[Tfrom], max_move);
562 	this->action_counts[Tfrom] -= max_move;
563 	this->action_counts[Tto] += max_move;
564 	return max_move;
565 }
566 
567 /**
568  * Reassign cargo from MTA_DELIVER to MTA_TRANSFER and take care of the next
569  * station the cargo wants to visit.
570  * @param max_move Maximum amount of cargo to reassign.
571  * @param next_station Station to record as next hop in the reassigned packets.
572  * @return Amount of cargo actually reassigned.
573  */
574 template<>
Reassign(uint max_move,TileOrStationID next_station)575 uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(uint max_move, TileOrStationID next_station)
576 {
577 	max_move = std::min(this->action_counts[MTA_DELIVER], max_move);
578 
579 	uint sum = 0;
580 	for (Iterator it(this->packets.begin()); sum < this->action_counts[MTA_TRANSFER] + max_move;) {
581 		CargoPacket *cp = *it++;
582 		sum += cp->Count();
583 		if (sum <= this->action_counts[MTA_TRANSFER]) continue;
584 		if (sum > this->action_counts[MTA_TRANSFER] + max_move) {
585 			CargoPacket *cp_split = cp->Split(sum - this->action_counts[MTA_TRANSFER] + max_move);
586 			sum -= cp_split->Count();
587 			this->packets.insert(it, cp_split);
588 		}
589 		cp->next_station = next_station;
590 	}
591 
592 	this->action_counts[MTA_DELIVER] -= max_move;
593 	this->action_counts[MTA_TRANSFER] += max_move;
594 	return max_move;
595 }
596 
597 /**
598  * Returns reserved cargo to the station and removes it from the cache.
599  * @param max_move Maximum amount of cargo to move.
600  * @param dest Station the cargo is returned to.
601  * @param next ID of the next station the cargo wants to go to.
602  * @return Amount of cargo actually returned.
603  */
Return(uint max_move,StationCargoList * dest,StationID next)604 uint VehicleCargoList::Return(uint max_move, StationCargoList *dest, StationID next)
605 {
606 	max_move = std::min(this->action_counts[MTA_LOAD], max_move);
607 	this->PopCargo(CargoReturn(this, dest, max_move, next));
608 	return max_move;
609 }
610 
611 /**
612  * Shifts cargo between two vehicles.
613  * @param dest Other vehicle's cargo list.
614  * @param max_move Maximum amount of cargo to be moved.
615  * @return Amount of cargo actually moved.
616  */
Shift(uint max_move,VehicleCargoList * dest)617 uint VehicleCargoList::Shift(uint max_move, VehicleCargoList *dest)
618 {
619 	max_move = std::min(this->count, max_move);
620 	this->PopCargo(CargoShift(this, dest, max_move));
621 	return max_move;
622 }
623 
624 /**
625  * Unloads cargo at the given station. Deliver or transfer, depending on the
626  * ranges defined by designation_counts.
627  * @param dest StationCargoList to add transferred cargo to.
628  * @param max_move Maximum amount of cargo to move.
629  * @param payment Payment object to register payments in.
630  * @return Amount of cargo actually unloaded.
631  */
Unload(uint max_move,StationCargoList * dest,CargoPayment * payment)632 uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment)
633 {
634 	uint moved = 0;
635 	if (this->action_counts[MTA_TRANSFER] > 0) {
636 		uint move = std::min(this->action_counts[MTA_TRANSFER], max_move);
637 		this->ShiftCargo(CargoTransfer(this, dest, move));
638 		moved += move;
639 	}
640 	if (this->action_counts[MTA_TRANSFER] == 0 && this->action_counts[MTA_DELIVER] > 0 && moved < max_move) {
641 		uint move = std::min(this->action_counts[MTA_DELIVER], max_move - moved);
642 		this->ShiftCargo(CargoDelivery(this, move, payment));
643 		moved += move;
644 	}
645 	return moved;
646 }
647 
648 /**
649  * Truncates the cargo in this list to the given amount. It leaves the
650  * first cargo entities and removes max_move from the back of the list.
651  * @param max_move Maximum amount of entities to be removed from the list.
652  * @return Amount of entities actually moved.
653  */
Truncate(uint max_move)654 uint VehicleCargoList::Truncate(uint max_move)
655 {
656 	max_move = std::min(this->count, max_move);
657 	this->PopCargo(CargoRemoval<VehicleCargoList>(this, max_move));
658 	return max_move;
659 }
660 
661 /**
662  * Routes packets with station "avoid" as next hop to a different place.
663  * @param max_move Maximum amount of cargo to move.
664  * @param dest List to prepend the cargo to.
665  * @param avoid Station to exclude from routing and current next hop of packets to reroute.
666  * @param avoid2 Additional station to exclude from routing.
667  * @param ge GoodsEntry to get the routing info from.
668  */
Reroute(uint max_move,VehicleCargoList * dest,StationID avoid,StationID avoid2,const GoodsEntry * ge)669 uint VehicleCargoList::Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
670 {
671 	max_move = std::min(this->action_counts[MTA_TRANSFER], max_move);
672 	this->ShiftCargo(VehicleCargoReroute(this, dest, max_move, avoid, avoid2, ge));
673 	return max_move;
674 }
675 
676 /*
677  *
678  * Station cargo list implementation.
679  *
680  */
681 
682 /**
683  * Appends the given cargo packet to the range of packets with the same next station
684  * @warning After appending this packet may not exist anymore!
685  * @note Do not use the cargo packet anymore after it has been appended to this CargoList!
686  * @param next the next hop
687  * @param cp the cargo packet to add
688  * @pre cp != nullptr
689  */
Append(CargoPacket * cp,StationID next)690 void StationCargoList::Append(CargoPacket *cp, StationID next)
691 {
692 	assert(cp != nullptr);
693 	this->AddToCache(cp);
694 
695 	StationCargoPacketMap::List &list = this->packets[next];
696 	for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
697 			it != list.rend(); it++) {
698 		if (StationCargoList::TryMerge(*it, cp)) return;
699 	}
700 
701 	/* The packet could not be merged with another one */
702 	list.push_back(cp);
703 }
704 
705 /**
706  * Shifts cargo from the front of the packet list for a specific station and
707  * applies some action to it.
708  * @tparam Taction Action class or function to be used. It should define
709  *                 "bool operator()(CargoPacket *)". If true is returned the
710  *                 cargo packet will be removed from the list. Otherwise it
711  *                 will be kept and the loop will be aborted.
712  * @param action Action instance to be applied.
713  * @param next Next hop the cargo wants to visit.
714  * @return True if all packets with the given next hop have been removed,
715  *         False otherwise.
716  */
717 template <class Taction>
ShiftCargo(Taction & action,StationID next)718 bool StationCargoList::ShiftCargo(Taction &action, StationID next)
719 {
720 	std::pair<Iterator, Iterator> range(this->packets.equal_range(next));
721 	for (Iterator it(range.first); it != range.second && it.GetKey() == next;) {
722 		if (action.MaxMove() == 0) return false;
723 		CargoPacket *cp = *it;
724 		if (action(cp)) {
725 			it = this->packets.erase(it);
726 		} else {
727 			return false;
728 		}
729 	}
730 	return true;
731 }
732 
733 /**
734  * Shifts cargo from the front of the packet list for a specific station and
735  * and optional also from the list for "any station", then applies some action
736  * to it.
737  * @tparam Taction Action class or function to be used. It should define
738  *                 "bool operator()(CargoPacket *)". If true is returned the
739  *                 cargo packet will be removed from the list. Otherwise it
740  *                 will be kept and the loop will be aborted.
741  * @param action Action instance to be applied.
742  * @param next Next hop the cargo wants to visit.
743  * @param include_invalid If cargo from the INVALID_STATION list should be
744  *                        used if necessary.
745  * @return Amount of cargo actually moved.
746  */
747 template <class Taction>
ShiftCargo(Taction action,StationIDStack next,bool include_invalid)748 uint StationCargoList::ShiftCargo(Taction action, StationIDStack next, bool include_invalid)
749 {
750 	uint max_move = action.MaxMove();
751 	while (!next.IsEmpty()) {
752 		this->ShiftCargo(action, next.Pop());
753 		if (action.MaxMove() == 0) break;
754 	}
755 	if (include_invalid && action.MaxMove() > 0) {
756 		this->ShiftCargo(action, INVALID_STATION);
757 	}
758 	return max_move - action.MaxMove();
759 }
760 
761 /**
762  * Truncates where each destination loses roughly the same percentage of its
763  * cargo. This is done by randomizing the selection of packets to be removed.
764  * Optionally count the cargo by origin station.
765  * @param max_move Maximum amount of cargo to remove.
766  * @param cargo_per_source Container for counting the cargo by origin.
767  * @return Amount of cargo actually moved.
768  */
Truncate(uint max_move,StationCargoAmountMap * cargo_per_source)769 uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source)
770 {
771 	max_move = std::min(max_move, this->count);
772 	uint prev_count = this->count;
773 	uint moved = 0;
774 	uint loop = 0;
775 	bool do_count = cargo_per_source != nullptr;
776 	while (max_move > moved) {
777 		for (Iterator it(this->packets.begin()); it != this->packets.end();) {
778 			CargoPacket *cp = *it;
779 			if (prev_count > max_move && RandomRange(prev_count) < prev_count - max_move) {
780 				if (do_count && loop == 0) {
781 					(*cargo_per_source)[cp->source] += cp->count;
782 				}
783 				++it;
784 				continue;
785 			}
786 			uint diff = max_move - moved;
787 			if (cp->count > diff) {
788 				if (diff > 0) {
789 					this->RemoveFromCache(cp, diff);
790 					cp->Reduce(diff);
791 					moved += diff;
792 				}
793 				if (loop > 0) {
794 					if (do_count) (*cargo_per_source)[cp->source] -= diff;
795 					return moved;
796 				} else {
797 					if (do_count) (*cargo_per_source)[cp->source] += cp->count;
798 					++it;
799 				}
800 			} else {
801 				it = this->packets.erase(it);
802 				if (do_count && loop > 0) {
803 					(*cargo_per_source)[cp->source] -= cp->count;
804 				}
805 				moved += cp->count;
806 				this->RemoveFromCache(cp, cp->count);
807 				delete cp;
808 			}
809 		}
810 		loop++;
811 	}
812 	return moved;
813 }
814 
815 /**
816  * Reserves cargo for loading onto the vehicle.
817  * @param max_move Maximum amount of cargo to reserve.
818  * @param dest VehicleCargoList to reserve for.
819  * @param load_place Tile index of the current station.
820  * @param next_station Next station(s) the loading vehicle will visit.
821  * @return Amount of cargo actually reserved.
822  */
Reserve(uint max_move,VehicleCargoList * dest,TileIndex load_place,StationIDStack next_station)823 uint StationCargoList::Reserve(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next_station)
824 {
825 	return this->ShiftCargo(CargoReservation(this, dest, max_move, load_place), next_station, true);
826 }
827 
828 /**
829  * Loads cargo onto a vehicle. If the vehicle has reserved cargo load that.
830  * Otherwise load cargo from the station.
831  * @param max_move Amount of cargo to load.
832  * @param dest Vehicle cargo list where the cargo resides.
833  * @param load_place The new loaded_at_xy to be assigned to packets being moved.
834  * @param next_station Next station(s) the loading vehicle will visit.
835  * @return Amount of cargo actually loaded.
836  * @note Vehicles may or may not reserve, depending on their orders. The two
837  *       modes of loading are exclusive, though. If cargo is reserved we don't
838  *       need to load unreserved cargo.
839  */
Load(uint max_move,VehicleCargoList * dest,TileIndex load_place,StationIDStack next_station)840 uint StationCargoList::Load(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next_station)
841 {
842 	uint move = std::min(dest->ActionCount(VehicleCargoList::MTA_LOAD), max_move);
843 	if (move > 0) {
844 		this->reserved_count -= move;
845 		dest->Reassign<VehicleCargoList::MTA_LOAD, VehicleCargoList::MTA_KEEP>(move);
846 		return move;
847 	} else {
848 		return this->ShiftCargo(CargoLoad(this, dest, max_move, load_place), next_station, true);
849 	}
850 }
851 
852 /**
853  * Routes packets with station "avoid" as next hop to a different place.
854  * @param max_move Maximum amount of cargo to move.
855  * @param dest List to append the cargo to.
856  * @param avoid Station to exclude from routing and current next hop of packets to reroute.
857  * @param avoid2 Additional station to exclude from routing.
858  * @param ge GoodsEntry to get the routing info from.
859  */
Reroute(uint max_move,StationCargoList * dest,StationID avoid,StationID avoid2,const GoodsEntry * ge)860 uint StationCargoList::Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
861 {
862 	return this->ShiftCargo(StationCargoReroute(this, dest, max_move, avoid, avoid2, ge), avoid, false);
863 }
864 
865 /*
866  * We have to instantiate everything we want to be usable.
867  */
868 template class CargoList<VehicleCargoList, CargoPacketList>;
869 template class CargoList<StationCargoList, StationCargoPacketMap>;
870 template uint VehicleCargoList::Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_KEEP>(uint, TileOrStationID);
871