Change: Only resort town directory window on population change if necessary
[openttd-github.git] / src / cargoaction.cpp
blob5853b87da990200a84ce40c50d969febaaec4f06
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file cargoaction.cpp Implementation of cargo actions. */
10 #include "stdafx.h"
11 #include "economy_base.h"
12 #include "cargoaction.h"
13 #include "station_base.h"
15 #include "safeguards.h"
17 /**
18 * Decides if a packet needs to be split.
19 * @param cp Packet to be either split or moved in one piece.
20 * @return Either new packet if splitting was necessary or the given one
21 * otherwise.
23 template<class Tsource, class Tdest>
24 CargoPacket *CargoMovement<Tsource, Tdest>::Preprocess(CargoPacket *cp)
26 if (this->max_move < cp->Count()) {
27 cp = cp->Split(this->max_move);
28 this->max_move = 0;
29 } else {
30 this->max_move -= cp->Count();
32 return cp;
35 /**
36 * Determines the amount of cargo to be removed from a packet and removes that
37 * from the metadata of the list.
38 * @param cp Packet to be removed completely or partially.
39 * @return Amount of cargo to be removed.
41 template<class Tsource>
42 uint CargoRemoval<Tsource>::Preprocess(CargoPacket *cp)
44 if (this->max_move >= cp->Count()) {
45 this->max_move -= cp->Count();
46 return cp->Count();
47 } else {
48 uint ret = this->max_move;
49 this->max_move = 0;
50 return ret;
54 /**
55 * Finalize cargo removal. Either delete the packet or reduce it.
56 * @param cp Packet to be removed or reduced.
57 * @param remove Amount of cargo to be removed.
58 * @return True if the packet was deleted, False if it was reduced.
60 template<class Tsource>
61 bool CargoRemoval<Tsource>::Postprocess(CargoPacket *cp, uint remove)
63 if (remove == cp->Count()) {
64 delete cp;
65 return true;
66 } else {
67 cp->Reduce(remove);
68 return false;
72 /**
73 * Removes some cargo from a StationCargoList.
74 * @param cp Packet to be removed.
75 * @return True if the packet was completely delivered, false if only part of
76 * it was.
78 template<>
79 bool CargoRemoval<StationCargoList>::operator()(CargoPacket *cp)
81 uint remove = this->Preprocess(cp);
82 this->source->RemoveFromCache(cp, remove);
83 return this->Postprocess(cp, remove);
86 /**
87 * Removes some cargo from a VehicleCargoList.
88 * @param cp Packet to be removed.
89 * @return True if the packet was completely delivered, false if only part of
90 * it was.
92 template<>
93 bool CargoRemoval<VehicleCargoList>::operator()(CargoPacket *cp)
95 uint remove = this->Preprocess(cp);
96 this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_KEEP, remove);
97 return this->Postprocess(cp, remove);
101 * Delivers some cargo.
102 * @param cp Packet to be delivered.
103 * @return True if the packet was completely delivered, false if only part of
104 * it was.
106 bool CargoDelivery::operator()(CargoPacket *cp)
108 uint remove = this->Preprocess(cp);
109 this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_DELIVER, remove);
110 this->payment->PayFinalDelivery(cp, remove);
111 return this->Postprocess(cp, remove);
115 * Loads some cargo onto a vehicle.
116 * @param cp Packet to be loaded.
117 * @return True if the packet was completely loaded, false if part of it was.
119 bool CargoLoad::operator()(CargoPacket *cp)
121 CargoPacket *cp_new = this->Preprocess(cp);
122 if (cp_new == nullptr) return false;
123 cp_new->SetLoadPlace(this->load_place);
124 this->source->RemoveFromCache(cp_new, cp_new->Count());
125 this->destination->Append(cp_new, VehicleCargoList::MTA_KEEP);
126 return cp_new == cp;
130 * Reserves some cargo for loading.
131 * @param cp Packet to be reserved.
132 * @return True if the packet was completely reserved, false if part of it was.
134 bool CargoReservation::operator()(CargoPacket *cp)
136 CargoPacket *cp_new = this->Preprocess(cp);
137 if (cp_new == nullptr) return false;
138 cp_new->SetLoadPlace(this->load_place);
139 this->source->reserved_count += cp_new->Count();
140 this->source->RemoveFromCache(cp_new, cp_new->Count());
141 this->destination->Append(cp_new, VehicleCargoList::MTA_LOAD);
142 return cp_new == cp;
146 * Returns some reserved cargo.
147 * @param cp Packet to be returned.
148 * @return True if the packet was completely returned, false if part of it was.
150 bool CargoReturn::operator()(CargoPacket *cp)
152 CargoPacket *cp_new = this->Preprocess(cp);
153 if (cp_new == nullptr) cp_new = cp;
154 assert(cp_new->Count() <= this->destination->reserved_count);
155 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_LOAD, cp_new->Count());
156 this->destination->reserved_count -= cp_new->Count();
157 this->destination->Append(cp_new, this->next);
158 return cp_new == cp;
162 * Transfers some cargo from a vehicle to a station.
163 * @param cp Packet to be transferred.
164 * @return True if the packet was completely reserved, false if part of it was.
166 bool CargoTransfer::operator()(CargoPacket *cp)
168 CargoPacket *cp_new = this->Preprocess(cp);
169 if (cp_new == nullptr) return false;
170 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_TRANSFER, cp_new->Count());
171 /* No transfer credits here as they were already granted during Stage(). */
172 this->destination->Append(cp_new, cp_new->NextStation());
173 return cp_new == cp;
177 * Shifts some cargo from a vehicle to another one.
178 * @param cp Packet to be shifted.
179 * @return True if the packet was completely shifted, false if part of it was.
181 bool CargoShift::operator()(CargoPacket *cp)
183 CargoPacket *cp_new = this->Preprocess(cp);
184 if (cp_new == nullptr) cp_new = cp;
185 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_KEEP, cp_new->Count());
186 this->destination->Append(cp_new, VehicleCargoList::MTA_KEEP);
187 return cp_new == cp;
191 * Reroutes some cargo from one Station sublist to another.
192 * @param cp Packet to be rerouted.
193 * @return True if the packet was completely rerouted, false if part of it was.
195 bool StationCargoReroute::operator()(CargoPacket *cp)
197 CargoPacket *cp_new = this->Preprocess(cp);
198 if (cp_new == nullptr) cp_new = cp;
199 StationID next = this->ge->GetVia(cp_new->SourceStation(), this->avoid, this->avoid2);
200 assert(next != this->avoid && next != this->avoid2);
201 if (this->source != this->destination) {
202 this->source->RemoveFromCache(cp_new, cp_new->Count());
203 this->destination->AddToCache(cp_new);
206 /* Legal, as insert doesn't invalidate iterators in the MultiMap, however
207 * this might insert the packet between range.first and range.second (which might be end())
208 * This is why we check for GetKey above to avoid infinite loops. */
209 this->destination->packets.Insert(next, cp_new);
210 return cp_new == cp;
214 * Reroutes some cargo in a VehicleCargoList.
215 * @param cp Packet to be rerouted.
216 * @return True if the packet was completely rerouted, false if part of it was.
218 bool VehicleCargoReroute::operator()(CargoPacket *cp)
220 CargoPacket *cp_new = this->Preprocess(cp);
221 if (cp_new == nullptr) cp_new = cp;
222 if (cp_new->NextStation() == this->avoid || cp_new->NextStation() == this->avoid2) {
223 cp->SetNextStation(this->ge->GetVia(cp_new->SourceStation(), this->avoid, this->avoid2));
225 if (this->source != this->destination) {
226 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_TRANSFER, cp_new->Count());
227 this->destination->AddToMeta(cp_new, VehicleCargoList::MTA_TRANSFER);
230 /* Legal, as front pushing doesn't invalidate iterators in std::list. */
231 this->destination->packets.push_front(cp_new);
232 return cp_new == cp;
235 template uint CargoRemoval<VehicleCargoList>::Preprocess(CargoPacket *cp);
236 template uint CargoRemoval<StationCargoList>::Preprocess(CargoPacket *cp);
237 template bool CargoRemoval<VehicleCargoList>::Postprocess(CargoPacket *cp, uint remove);
238 template bool CargoRemoval<StationCargoList>::Postprocess(CargoPacket *cp, uint remove);