OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

cargoaction.cpp
Go to the documentation of this file.
1 /* $Id: cargoaction.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
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 */
9
12 #include "stdafx.h"
13 #include "economy_base.h"
14 #include "cargoaction.h"
15 #include "station_base.h"
16
17 #include "safeguards.h"
18
25 template<class Tsource, class Tdest>
26 CargoPacket *CargoMovement<Tsource, Tdest>::Preprocess(CargoPacket *cp)
27 {
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();
33 }
34 return cp;
35 }
36
43 template<class Tsource>
44 uint CargoRemoval<Tsource>::Preprocess(CargoPacket *cp)
45 {
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;
53 }
54 }
55
62 template<class Tsource>
63 bool CargoRemoval<Tsource>::Postprocess(CargoPacket *cp, uint remove)
64 {
65 if (remove == cp->Count()) {
66 delete cp;
67 return true;
68 } else {
69 cp->Reduce(remove);
70 return false;
71 }
72 }
73
80 template<>
81 bool CargoRemoval<StationCargoList>::operator()(CargoPacket *cp)
82 {
83 uint remove = this->Preprocess(cp);
84 this->source->RemoveFromCache(cp, remove);
85 return this->Postprocess(cp, remove);
86 }
87
94 template<>
95 bool CargoRemoval<VehicleCargoList>::operator()(CargoPacket *cp)
96 {
97 uint remove = this->Preprocess(cp);
98 this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_KEEP, remove);
99 return this->Postprocess(cp, remove);
100 }
101
108 bool CargoDelivery::operator()(CargoPacket *cp)
109 {
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);
114 }
115
121 bool CargoLoad::operator()(CargoPacket *cp)
122 {
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;
129 }
130
136 bool CargoReservation::operator()(CargoPacket *cp)
137 {
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;
145 }
146
152 bool CargoReturn::operator()(CargoPacket *cp)
153 {
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;
161 }
162
168 bool CargoTransfer::operator()(CargoPacket *cp)
169 {
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;
176 }
177
183 bool CargoShift::operator()(CargoPacket *cp)
184 {
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;
190 }
191
197 bool StationCargoReroute::operator()(CargoPacket *cp)
198 {
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);
206 }
207
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;
213 }
214
220 bool VehicleCargoReroute::operator()(CargoPacket *cp)
221 {
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));
226 }
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);
230 }
231
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;
235 }
236
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);