OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
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
23template <class Tsource, class Tdest>
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
41template <class Tsource>
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
60template <class Tsource>
62{
63 if (remove == cp->Count()) {
64 delete cp;
65 return true;
66 } else {
67 cp->Reduce(remove);
68 return false;
69 }
70}
71
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 */
78template <>
79bool 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
92template <>
93bool CargoRemoval<VehicleCargoList>::operator()(CargoPacket *cp)
94{
95 uint remove = this->Preprocess(cp);
96
97 uint remaining = remove;
98 for (VehicleCargoList::MoveToAction action : {VehicleCargoList::MoveToAction::Keep, VehicleCargoList::MoveToAction::Transfer, VehicleCargoList::MoveToAction::Deliver, VehicleCargoList::MoveToAction::Load}) {
99 remaining -= this->source->TryRemoveFromMeta(cp, action, remaining);
100 if (remaining == 0) break;
101 }
102 assert(remaining == 0);
103
104 return this->Postprocess(cp, remove);
105}
106
114{
115 uint remove = this->Preprocess(cp);
116 this->source->RemoveFromMeta(cp, VehicleCargoList::MoveToAction::Deliver, remove);
117 this->payment->PayFinalDelivery(this->cargo, cp, remove, this->current_tile);
118 return this->Postprocess(cp, remove);
119}
120
127{
128 CargoPacket *cp_new = this->Preprocess(cp);
129 if (cp_new == nullptr) return false;
130 cp_new->UpdateLoadingTile(this->current_tile);
131 this->source->RemoveFromCache(cp_new, cp_new->Count());
132 this->destination->Append(cp_new, VehicleCargoList::MoveToAction::Keep);
133 return cp_new == cp;
134}
135
142{
143 CargoPacket *cp_new = this->Preprocess(cp);
144 if (cp_new == nullptr) return false;
145 cp_new->UpdateLoadingTile(this->current_tile);
146 this->source->reserved_count += cp_new->Count();
147 this->source->RemoveFromCache(cp_new, cp_new->Count());
148 this->destination->Append(cp_new, VehicleCargoList::MoveToAction::Load);
149 return cp_new == cp;
150}
151
158{
159 CargoPacket *cp_new = this->Preprocess(cp);
160 if (cp_new == nullptr) cp_new = cp;
161 assert(cp_new->Count() <= this->destination->reserved_count);
162 cp_new->UpdateUnloadingTile(this->current_tile);
163 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MoveToAction::Load, cp_new->Count());
164 this->destination->reserved_count -= cp_new->Count();
165 this->destination->Append(cp_new, this->next);
166 return cp_new == cp;
167}
168
175{
176 CargoPacket *cp_new = this->Preprocess(cp);
177 if (cp_new == nullptr) return false;
178 cp_new->UpdateUnloadingTile(this->current_tile);
179 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MoveToAction::Transfer, cp_new->Count());
180 /* No transfer credits here as they were already granted during Stage(). */
181 this->destination->Append(cp_new, cp_new->GetNextHop());
182 return cp_new == cp;
183}
184
191{
192 CargoPacket *cp_new = this->Preprocess(cp);
193 if (cp_new == nullptr) cp_new = cp;
194 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MoveToAction::Keep, cp_new->Count());
195 this->destination->Append(cp_new, VehicleCargoList::MoveToAction::Keep);
196 return cp_new == cp;
197}
198
205{
206 CargoPacket *cp_new = this->Preprocess(cp);
207 if (cp_new == nullptr) cp_new = cp;
208 StationID next = this->ge->GetVia(cp_new->GetFirstStation(), this->avoid, this->avoid2);
209 assert(next != this->avoid && next != this->avoid2);
210 if (this->source != this->destination) {
211 this->source->RemoveFromCache(cp_new, cp_new->Count());
212 this->destination->AddToCache(cp_new);
213 }
214
215 /* Legal, as insert doesn't invalidate iterators in the MultiMap, however
216 * this might insert the packet between range.first and range.second (which might be end())
217 * This is why we check for GetKey above to avoid infinite loops. */
218 this->destination->packets.Insert(next, cp_new);
219 return cp_new == cp;
220}
221
228{
229 CargoPacket *cp_new = this->Preprocess(cp);
230 if (cp_new == nullptr) cp_new = cp;
231 if (cp_new->GetNextHop() == this->avoid || cp_new->GetNextHop() == this->avoid2) {
232 cp->SetNextHop(this->ge->GetVia(cp_new->GetFirstStation(), this->avoid, this->avoid2));
233 }
234 if (this->source != this->destination) {
235 this->source->RemoveFromMeta(cp_new, VehicleCargoList::MoveToAction::Transfer, cp_new->Count());
236 this->destination->AddToMeta(cp_new, VehicleCargoList::MoveToAction::Transfer);
237 }
238
239 /* Legal, as front pushing doesn't invalidate iterators in std::list. */
240 this->destination->packets.push_front(cp_new);
241 return cp_new == cp;
242}
243
246template bool CargoRemoval<VehicleCargoList>::Postprocess(CargoPacket *cp, uint remove);
247template bool CargoRemoval<StationCargoList>::Postprocess(CargoPacket *cp, uint remove);
Actions to be applied to cargo packets.
CargoPayment * payment
Payment object where payments will be registered.
Definition cargoaction.h:47
CargoType cargo
The cargo type of the cargo.
Definition cargoaction.h:48
TileIndex current_tile
Current tile cargo delivery is happening.
Definition cargoaction.h:46
bool operator()(CargoPacket *cp)
Delivers some cargo.
bool operator()(CargoPacket *cp)
Loads some cargo onto a vehicle.
TileIndex current_tile
Current tile cargo loading is happening.
CargoPacket * Preprocess(CargoPacket *cp)
Decides if a packet needs to be split.
uint Preprocess(CargoPacket *cp)
Determines the amount of cargo to be removed from a packet and removes that from the metadata of the ...
bool Postprocess(CargoPacket *cp, uint remove)
Finalize cargo removal.
bool operator()(CargoPacket *cp)
Reserves some cargo for loading.
TileIndex current_tile
Current tile cargo unloading is happening.
bool operator()(CargoPacket *cp)
Returns some reserved cargo.
bool operator()(CargoPacket *cp)
Shifts some cargo from a vehicle to another one.
bool operator()(CargoPacket *cp)
Transfers some cargo from a vehicle to a station.
TileIndex current_tile
Current tile cargo unloading is happening.
Definition cargoaction.h:94
bool operator()(CargoPacket *cp)
Reroutes some cargo from one Station sublist to another.
bool operator()(CargoPacket *cp)
Reroutes some cargo in a VehicleCargoList.
Base classes related to the economy.
A number of safeguards to prevent using unsafe methods.
Base classes/functions for stations.
@ Count
by amount of cargo
Definition of base types and functions in a cross-platform compatible way.
Container for cargo from the same location and time.
Definition cargopacket.h:40
void Reduce(uint count)
Reduce the packet by the given amount and remove the feeder share.
uint16_t Count() const
Gets the number of 'items' in this packet.
void SetNextHop(StationID next_hop)
Sets the station where the packet is supposed to go next.
Definition cargopacket.h:82
CargoPacket * Split(uint new_size)
Split this packet in two and return the split off part.
void UpdateLoadingTile(TileIndex tile)
Update for the cargo being loaded on this tile.
StationID GetNextHop() const
Gets the ID of station the cargo wants to go next.
StationID GetFirstStation() const
Gets the ID of the station where the cargo was loaded for the first time.
void UpdateUnloadingTile(TileIndex tile)
Update for the cargo being unloaded on this tile.