OpenTTD
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>
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>
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>
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<>
82 {
83  uint remove = this->Preprocess(cp);
84  this->source->RemoveFromCache(cp, remove);
85  return this->Postprocess(cp, remove);
86 }
87 
94 template<>
96 {
97  uint remove = this->Preprocess(cp);
98  this->source->RemoveFromMeta(cp, VehicleCargoList::MTA_KEEP, remove);
99  return this->Postprocess(cp, remove);
100 }
101 
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 
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 
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 
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 
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 
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 
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 
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 
239 template bool CargoRemoval<VehicleCargoList>::Postprocess(CargoPacket *cp, uint remove);
240 template bool CargoRemoval<StationCargoList>::Postprocess(CargoPacket *cp, uint remove);
bool operator()(CargoPacket *cp)
Reroutes some cargo from one Station sublist to another.
StationID NextStation() const
Gets the ID of station the cargo wants to go next.
Definition: cargopacket.h:187
void SetLoadPlace(TileIndex load_place)
Sets the tile where the packet was loaded last.
Definition: cargopacket.h:83
CargoPacket * Preprocess(CargoPacket *cp)
Decides if a packet needs to be split.
Definition: cargoaction.cpp:26
bool Postprocess(CargoPacket *cp, uint remove)
Finalize cargo removal.
Definition: cargoaction.cpp:63
Tsource * source
Source of the cargo.
Definition: cargoaction.h:24
bool operator()(CargoPacket *cp)
Shifts some cargo from a vehicle to another one.
Actions to be applied to cargo packets.
Container for cargo from the same location and time.
Definition: cargopacket.h:44
Definition of base types and functions in a cross-platform compatible way.
StationID SourceStation() const
Gets the ID of the station where the cargo was loaded for the first time.
Definition: cargopacket.h:160
A number of safeguards to prevent using unsafe methods.
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
Definition: cargopacket.cpp:91
Deliver the cargo to some town or industry.
Definition: cargopacket.h:230
bool operator()(CargoPacket *cp)
Reroutes some cargo in a VehicleCargoList.
bool operator()(CargoPacket *cp)
Transfers some cargo from a vehicle to a station.
bool operator()(CargoPacket *cp)
Loads some cargo onto a vehicle.
Abstract action of removing cargo from a vehicle or a station.
Definition: cargoaction.h:22
bool operator()(CargoPacket *cp)
Reserves some cargo for loading.
void Reduce(uint count)
Reduce the packet by the given amount and remove the feeder share.
Base classes related to the economy.
bool operator()(CargoPacket *cp)
Delivers some cargo.
uint Preprocess(CargoPacket *cp)
Determines the amount of cargo to be removed from a packet and removes that from the metadata of the ...
Definition: cargoaction.cpp:44
uint16 Count() const
Gets the number of &#39;items&#39; in this packet.
Definition: cargopacket.h:101
void SetNextStation(StationID next_station)
Sets the station where the packet is supposed to go next.
Definition: cargopacket.h:89
bool operator()(CargoPacket *cp)
Returns some reserved cargo.
Base classes/functions for stations.