Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / cargoaction.cpp
blobe6d32aac898400c7f04b03b302480bc18031c5eb
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(this->cargo, cp, remove, this->current_tile);
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->UpdateLoadingTile(this->current_tile);
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->UpdateLoadingTile(this->current_tile);
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 cp_new->UpdateUnloadingTile(this->current_tile);
156 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_LOAD, cp_new->Count());
157 this->destination->reserved_count -= cp_new->Count();
158 this->destination->Append(cp_new, this->next);
159 return cp_new == cp;
163 * Transfers some cargo from a vehicle to a station.
164 * @param cp Packet to be transferred.
165 * @return True if the packet was completely reserved, false if part of it was.
167 bool CargoTransfer::operator()(CargoPacket *cp)
169 CargoPacket *cp_new = this->Preprocess(cp);
170 if (cp_new == nullptr) return false;
171 cp_new->UpdateUnloadingTile(this->current_tile);
172 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_TRANSFER, cp_new->Count());
173 /* No transfer credits here as they were already granted during Stage(). */
174 this->destination->Append(cp_new, cp_new->GetNextHop());
175 return cp_new == cp;
179 * Shifts some cargo from a vehicle to another one.
180 * @param cp Packet to be shifted.
181 * @return True if the packet was completely shifted, false if part of it was.
183 bool CargoShift::operator()(CargoPacket *cp)
185 CargoPacket *cp_new = this->Preprocess(cp);
186 if (cp_new == nullptr) cp_new = cp;
187 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_KEEP, cp_new->Count());
188 this->destination->Append(cp_new, VehicleCargoList::MTA_KEEP);
189 return cp_new == cp;
193 * Reroutes some cargo from one Station sublist to another.
194 * @param cp Packet to be rerouted.
195 * @return True if the packet was completely rerouted, false if part of it was.
197 bool StationCargoReroute::operator()(CargoPacket *cp)
199 CargoPacket *cp_new = this->Preprocess(cp);
200 if (cp_new == nullptr) cp_new = cp;
201 StationID next = this->ge->GetVia(cp_new->GetFirstStation(), this->avoid, this->avoid2);
202 assert(next != this->avoid && next != this->avoid2);
203 if (this->source != this->destination) {
204 this->source->RemoveFromCache(cp_new, cp_new->Count());
205 this->destination->AddToCache(cp_new);
208 /* Legal, as insert doesn't invalidate iterators in the MultiMap, however
209 * this might insert the packet between range.first and range.second (which might be end())
210 * This is why we check for GetKey above to avoid infinite loops. */
211 this->destination->packets.Insert(next, cp_new);
212 return cp_new == cp;
216 * Reroutes some cargo in a VehicleCargoList.
217 * @param cp Packet to be rerouted.
218 * @return True if the packet was completely rerouted, false if part of it was.
220 bool VehicleCargoReroute::operator()(CargoPacket *cp)
222 CargoPacket *cp_new = this->Preprocess(cp);
223 if (cp_new == nullptr) cp_new = cp;
224 if (cp_new->GetNextHop() == this->avoid || cp_new->GetNextHop() == this->avoid2) {
225 cp->SetNextHop(this->ge->GetVia(cp_new->GetFirstStation(), this->avoid, this->avoid2));
227 if (this->source != this->destination) {
228 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_TRANSFER, cp_new->Count());
229 this->destination->AddToMeta(cp_new, VehicleCargoList::MTA_TRANSFER);
232 /* Legal, as front pushing doesn't invalidate iterators in std::list. */
233 this->destination->packets.push_front(cp_new);
234 return cp_new == cp;
237 template uint CargoRemoval<VehicleCargoList>::Preprocess(CargoPacket *cp);
238 template uint CargoRemoval<StationCargoList>::Preprocess(CargoPacket *cp);
239 template bool CargoRemoval<VehicleCargoList>::Postprocess(CargoPacket *cp, uint remove);
240 template bool CargoRemoval<StationCargoList>::Postprocess(CargoPacket *cp, uint remove);