OpenTTD Source: src/cargoaction.cpp Source File

OpenTTD Source 12.0-beta1

cargoaction.cpp
Go to the documentation of this file.
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
10 #include "stdafx.h"
11 #include "economy_base.h"
12 #include "cargoaction.h"
13 #include "station_base.h"
14
15 #include "safeguards.h"
16
23 template<class Tsource, class Tdest>
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
41 template<class Tsource>
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
60 template<class Tsource>
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
78 template<>
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
92 template<>
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
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
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
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
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
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
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
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
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);
CargoShift::operator()
bool operator()(CargoPacket *cp)
Shifts some cargo from a vehicle to another one.
Definition: cargoaction.cpp:181
economy_base.h
CargoReservation::operator()
bool operator()(CargoPacket *cp)
Reserves some cargo for loading.
Definition: cargoaction.cpp:134
StationCargoList::reserved_count
uint reserved_count
Amount of cargo being reserved for loading.
Definition: cargopacket.h:454
CargoDelivery::operator()
bool operator()(CargoPacket *cp)
Delivers some cargo.
Definition: cargoaction.cpp:106
VehicleCargoList::RemoveFromMeta
void RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
Removes a packet or part of it from the metadata.
Definition: cargopacket.cpp:356
CargoMovement< StationCargoList, VehicleCargoList >::destination
VehicleCargoList * destination
Destination for the cargo.
Definition: cargoaction.h:57
CargoMovement::Preprocess
CargoPacket * Preprocess(CargoPacket *cp)
Decides if a packet needs to be split.
Definition: cargoaction.cpp:24
CargoRemoval::Preprocess
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:42
VehicleCargoReroute::operator()
bool operator()(CargoPacket *cp)
Reroutes some cargo in a VehicleCargoList.
Definition: cargoaction.cpp:218
cargoaction.h
CargoPacket::SourceStation
StationID SourceStation() const
Gets the ID of the station where the cargo was loaded for the first time.
Definition: cargopacket.h:159
VehicleCargoList::AddToMeta
void AddToMeta(const CargoPacket *cp, MoveToAction action)
Adds a packet to the metadata.
Definition: cargopacket.cpp:370
GoodsEntry::GetVia
StationID GetVia(StationID source) const
Get the best next hop for a cargo packet from station source.
Definition: station_base.h:280
VehicleCargoList::Append
void Append(CargoPacket *cp, MoveToAction action=MTA_KEEP)
Appends the given cargo packet.
Definition: cargopacket.cpp:250
CargoPacket::Split
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
Definition: cargopacket.cpp:89
CargoPayment::PayFinalDelivery
void PayFinalDelivery(const CargoPacket *cp, uint count)
Handle payment for final delivery of the given cargo packet.
Definition: economy.cpp:1210
safeguards.h
CargoLoad::operator()
bool operator()(CargoPacket *cp)
Loads some cargo onto a vehicle.
Definition: cargoaction.cpp:119
CargoPacket::Count
uint16 Count() const
Gets the number of 'items' in this packet.
Definition: cargopacket.h:100
CargoList< VehicleCargoList, CargoPacketList >::MTA_TRANSFER
@ MTA_TRANSFER
Transfer the cargo to the station.
Definition: cargopacket.h:215
stdafx.h
CargoPacket::Reduce
void Reduce(uint count)
Reduce the packet by the given amount and remove the feeder share.
Definition: cargopacket.cpp:115
CargoList::RemoveFromCache
void RemoveFromCache(const CargoPacket *cp, uint count)
Update the cached values to reflect the removal of this packet or part of it.
Definition: cargopacket.cpp:179
CargoList::packets
Tcont packets
The cargo packets in this list.
Definition: cargopacket.h:227
station_base.h
CargoRemoval
Abstract action of removing cargo from a vehicle or a station.
Definition: cargoaction.h:20
CargoList< VehicleCargoList, CargoPacketList >::MTA_LOAD
@ MTA_LOAD
Load the cargo from the station.
Definition: cargopacket.h:218
CargoPacket::SetNextStation
void SetNextStation(StationID next_station)
Sets the station where the packet is supposed to go next.
Definition: cargopacket.h:88
CargoRemoval::source
Tsource * source
Source of the cargo.
Definition: cargoaction.h:22
CargoReturn::operator()
bool operator()(CargoPacket *cp)
Returns some reserved cargo.
Definition: cargoaction.cpp:150
CargoPacket::SetLoadPlace
void SetLoadPlace(TileIndex load_place)
Sets the tile where the packet was loaded last.
Definition: cargopacket.h:82
CargoLoad::load_place
TileIndex load_place
TileIndex to be saved in the packets' loaded_at_xy.
Definition: cargoaction.h:81
CargoTransfer::operator()
bool operator()(CargoPacket *cp)
Transfers some cargo from a vehicle to a station.
Definition: cargoaction.cpp:166
CargoPacket::NextStation
StationID NextStation() const
Gets the ID of station the cargo wants to go next.
Definition: cargopacket.h:186
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:43
CargoList< VehicleCargoList, CargoPacketList >::MTA_KEEP
@ MTA_KEEP
Keep the cargo in the vehicle.
Definition: cargopacket.h:217
CargoRemoval::Postprocess
bool Postprocess(CargoPacket *cp, uint remove)
Finalize cargo removal.
Definition: cargoaction.cpp:61
CargoList< VehicleCargoList, CargoPacketList >::MTA_DELIVER
@ MTA_DELIVER
Deliver the cargo to some town or industry.
Definition: cargopacket.h:216
StationCargoList::Append
void Append(CargoPacket *cp, StationID next)
Appends the given cargo packet to the range of packets with the same next station.
Definition: cargopacket.cpp:690
CargoMovement< StationCargoList, VehicleCargoList >::source
StationCargoList * source
Source of the cargo.
Definition: cargoaction.h:56
CargoDelivery::payment
CargoPayment * payment
Payment object where payments will be registered.
Definition: cargoaction.h:41
MultiMap::Insert
void Insert(const Tkey &key, const Tvalue &val)
Insert a value at the end of the range with the specified key.
Definition: multimap.hpp:326
StationCargoReroute::operator()
bool operator()(CargoPacket *cp)
Reroutes some cargo from one Station sublist to another.
Definition: cargoaction.cpp:195
CargoList::AddToCache
void AddToCache(const CargoPacket *cp)
Update the cache to reflect adding of this packet.
Definition: cargopacket.cpp:192