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 cargoaction.cpp Implementation of cargo actions. */
9
10 #include "stdafx.h"
11 #include "economy_base.h"
12 #include "cargoaction.h"
13 #include "station_base.h"
14
15 #include "safeguards.h"
16
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.
22 */
23 template<class Tsource, class Tdest>
Preprocess(CargoPacket * cp)24 CargoPacket *CargoMovement<Tsource, Tdest>::Preprocess(CargoPacket *cp)
25 {
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();
31 }
32 return cp;
33 }
34
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.
40 */
41 template<class Tsource>
Preprocess(CargoPacket * cp)42 uint CargoRemoval<Tsource>::Preprocess(CargoPacket *cp)
43 {
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;
51 }
52 }
53
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.
59 */
60 template<class Tsource>
Postprocess(CargoPacket * cp,uint remove)61 bool CargoRemoval<Tsource>::Postprocess(CargoPacket *cp, uint remove)
62 {
63 if (remove == cp->Count()) {
64 delete cp;
65 return true;
66 } else {
67 cp->Reduce(remove);
68 return false;
69 }
70 }
71
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.
77 */
78 template<>
operator ()(CargoPacket * cp)79 bool CargoRemoval<StationCargoList>::operator()(CargoPacket *cp)
80 {
81 uint remove = this->Preprocess(cp);
82 this->source->RemoveFromCache(cp, remove);
83 return this->Postprocess(cp, remove);
84 }
85
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.
91 */
92 template<>
operator ()(CargoPacket * cp)93 bool CargoRemoval<VehicleCargoList>::operator()(CargoPacket *cp)
94 {
95 uint remove = this->Preprocess(cp);
96 this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_KEEP, remove);
97 return this->Postprocess(cp, remove);
98 }
99
100 /**
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.
105 */
operator ()(CargoPacket * cp)106 bool CargoDelivery::operator()(CargoPacket *cp)
107 {
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);
112 }
113
114 /**
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.
118 */
operator ()(CargoPacket * cp)119 bool CargoLoad::operator()(CargoPacket *cp)
120 {
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;
127 }
128
129 /**
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.
133 */
operator ()(CargoPacket * cp)134 bool CargoReservation::operator()(CargoPacket *cp)
135 {
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;
143 }
144
145 /**
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.
149 */
operator ()(CargoPacket * cp)150 bool CargoReturn::operator()(CargoPacket *cp)
151 {
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;
159 }
160
161 /**
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.
165 */
operator ()(CargoPacket * cp)166 bool CargoTransfer::operator()(CargoPacket *cp)
167 {
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;
174 }
175
176 /**
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.
180 */
operator ()(CargoPacket * cp)181 bool CargoShift::operator()(CargoPacket *cp)
182 {
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;
188 }
189
190 /**
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.
194 */
operator ()(CargoPacket * cp)195 bool StationCargoReroute::operator()(CargoPacket *cp)
196 {
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);
204 }
205
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;
211 }
212
213 /**
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.
217 */
operator ()(CargoPacket * cp)218 bool VehicleCargoReroute::operator()(CargoPacket *cp)
219 {
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));
224 }
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);
228 }
229
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;
233 }
234
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);
239