(svn r28004) -Update from Eints:
[openttd.git] / src / cargoaction.cpp
blob96ddc3708e3b8e74e8732498c35963f3fcdc5455
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file cargoaction.cpp Implementation of cargo actions. */
12 #include "stdafx.h"
13 #include "economy_base.h"
14 #include "cargoaction.h"
15 #include "station_base.h"
17 #include "safeguards.h"
19 /**
20 * Decides if a packet needs to be split.
21 * @param cp Packet to be either split or moved in one piece.
22 * @return Either new packet if splitting was necessary or the given one
23 * otherwise.
25 template<class Tsource, class Tdest>
26 CargoPacket *CargoMovement<Tsource, Tdest>::Preprocess(CargoPacket *cp)
28 if (this->max_move < cp->Count()) {
29 cp = cp->Split(this->max_move);
30 this->max_move = 0;
31 } else {
32 this->max_move -= cp->Count();
34 return cp;
37 /**
38 * Determines the amount of cargo to be removed from a packet and removes that
39 * from the metadata of the list.
40 * @param cp Packet to be removed completely or partially.
41 * @return Amount of cargo to be removed.
43 template<class Tsource>
44 uint CargoRemoval<Tsource>::Preprocess(CargoPacket *cp)
46 if (this->max_move >= cp->Count()) {
47 this->max_move -= cp->Count();
48 return cp->Count();
49 } else {
50 uint ret = this->max_move;
51 this->max_move = 0;
52 return ret;
56 /**
57 * Finalize cargo removal. Either delete the packet or reduce it.
58 * @param cp Packet to be removed or reduced.
59 * @param remove Amount of cargo to be removed.
60 * @return True if the packet was deleted, False if it was reduced.
62 template<class Tsource>
63 bool CargoRemoval<Tsource>::Postprocess(CargoPacket *cp, uint remove)
65 if (remove == cp->Count()) {
66 delete cp;
67 return true;
68 } else {
69 cp->Reduce(remove);
70 return false;
74 /**
75 * Removes some cargo from a StationCargoList.
76 * @param cp Packet to be removed.
77 * @return True if the packet was completely delivered, false if only part of
78 * it was.
80 template<>
81 bool CargoRemoval<StationCargoList>::operator()(CargoPacket *cp)
83 uint remove = this->Preprocess(cp);
84 this->source->RemoveFromCache(cp, remove);
85 return this->Postprocess(cp, remove);
88 /**
89 * Removes some cargo from a VehicleCargoList.
90 * @param cp Packet to be removed.
91 * @return True if the packet was completely delivered, false if only part of
92 * it was.
94 template<>
95 bool CargoRemoval<VehicleCargoList>::operator()(CargoPacket *cp)
97 uint remove = this->Preprocess(cp);
98 this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_KEEP, remove);
99 return this->Postprocess(cp, remove);
103 * Delivers some cargo.
104 * @param cp Packet to be delivered.
105 * @return True if the packet was completely delivered, false if only part of
106 * it was.
108 bool CargoDelivery::operator()(CargoPacket *cp)
110 uint remove = this->Preprocess(cp);
111 this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_DELIVER, remove);
112 this->payment->PayFinalDelivery(cp, remove);
113 return this->Postprocess(cp, remove);
117 * Loads some cargo onto a vehicle.
118 * @param cp Packet to be loaded.
119 * @return True if the packet was completely loaded, false if part of it was.
121 bool CargoLoad::operator()(CargoPacket *cp)
123 CargoPacket *cp_new = this->Preprocess(cp);
124 if (cp_new == NULL) return false;
125 cp_new->SetLoadPlace(this->load_place);
126 this->source->RemoveFromCache(cp_new, cp_new->Count());
127 this->destination->Append(cp_new, VehicleCargoList::MTA_KEEP);
128 return cp_new == cp;
132 * Reserves some cargo for loading.
133 * @param cp Packet to be reserved.
134 * @return True if the packet was completely reserved, false if part of it was.
136 bool CargoReservation::operator()(CargoPacket *cp)
138 CargoPacket *cp_new = this->Preprocess(cp);
139 if (cp_new == NULL) return false;
140 cp_new->SetLoadPlace(this->load_place);
141 this->source->reserved_count += cp_new->Count();
142 this->source->RemoveFromCache(cp_new, cp_new->Count());
143 this->destination->Append(cp_new, VehicleCargoList::MTA_LOAD);
144 return cp_new == cp;
148 * Returns some reserved cargo.
149 * @param cp Packet to be returned.
150 * @return True if the packet was completely returned, false if part of it was.
152 bool CargoReturn::operator()(CargoPacket *cp)
154 CargoPacket *cp_new = this->Preprocess(cp);
155 if (cp_new == NULL) cp_new = cp;
156 assert(cp_new->Count() <= this->destination->reserved_count);
157 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MTA_LOAD, cp_new->Count());
158 this->destination->reserved_count -= cp_new->Count();
159 this->destination->Append(cp_new, this->next);
160 return cp_new == cp;
164 * Transfers some cargo from a vehicle to a station.
165 * @param cp Packet to be transfered.
166 * @return True if the packet was completely reserved, false if part of it was.
168 bool CargoTransfer::operator()(CargoPacket *cp)
170 CargoPacket *cp_new = this->Preprocess(cp);
171 if (cp_new == NULL) return false;
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->NextStation());
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 == NULL) 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 == NULL) cp_new = cp;
201 StationID next = this->ge->GetVia(cp_new->SourceStation(), 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 == NULL) cp_new = cp;
224 if (cp_new->NextStation() == this->avoid || cp_new->NextStation() == this->avoid2) {
225 cp->SetNextStation(this->ge->GetVia(cp_new->SourceStation(), 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);