2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file cargopacket.h Base class for cargo packets. */
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
#ifndef CARGOPACKET_H
|
|
|
|
#define CARGOPACKET_H
|
|
|
|
|
2009-05-22 15:39:22 +00:00
|
|
|
#include "core/pool_type.hpp"
|
2007-12-21 22:50:51 +00:00
|
|
|
#include "economy_type.h"
|
2008-03-28 16:33:28 +00:00
|
|
|
#include "station_type.h"
|
2013-06-09 13:03:48 +00:00
|
|
|
#include "order_type.h"
|
2009-08-08 16:42:55 +00:00
|
|
|
#include "cargo_type.h"
|
2009-10-06 23:01:35 +00:00
|
|
|
#include "vehicle_type.h"
|
2018-01-26 18:28:57 +00:00
|
|
|
#include "company_type.h"
|
2013-06-09 13:03:48 +00:00
|
|
|
#include "core/multimap.hpp"
|
2016-09-12 19:32:51 +00:00
|
|
|
#include <deque>
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2009-10-06 19:52:27 +00:00
|
|
|
/** Unique identifier for a single cargo packet. */
|
2007-08-02 10:49:24 +00:00
|
|
|
typedef uint32 CargoPacketID;
|
|
|
|
struct CargoPacket;
|
|
|
|
|
2010-08-19 20:58:30 +00:00
|
|
|
/** Type of the pool for cargo packets for a little over 16 million packets. */
|
2011-02-19 23:05:47 +00:00
|
|
|
typedef Pool<CargoPacket, CargoPacketID, 1024, 0xFFF000, PT_NORMAL, true, false> CargoPacketPool;
|
2011-01-19 16:20:26 +00:00
|
|
|
/** The actual pool with cargo packets. */
|
2009-05-22 15:13:50 +00:00
|
|
|
extern CargoPacketPool _cargopacket_pool;
|
2007-08-02 10:49:24 +00:00
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
struct GoodsEntry; // forward-declare for Stage() and RerouteStalePackets()
|
|
|
|
|
|
|
|
template <class Tinst, class Tcont> class CargoList;
|
2013-02-17 14:54:50 +00:00
|
|
|
class StationCargoList; // forward-declare, so we can use it in VehicleCargoList.
|
2009-10-06 17:23:15 +00:00
|
|
|
extern const struct SaveLoad *GetCargoPacketDesc();
|
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
typedef uint32 TileOrStationID;
|
|
|
|
|
2018-01-26 18:28:57 +00:00
|
|
|
void ClearCargoPacketDeferredPayments();
|
|
|
|
void ChangeOwnershipOfCargoPacketDeferredPayments(Owner old_owner, Owner new_owner);
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Container for cargo from the same location and time.
|
2007-06-22 11:58:59 +00:00
|
|
|
*/
|
2009-05-22 15:13:50 +00:00
|
|
|
struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
|
2009-10-06 17:23:15 +00:00
|
|
|
private:
|
2019-04-22 08:28:09 +00:00
|
|
|
Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo.
|
|
|
|
uint16 count; ///< The amount of cargo in this packet.
|
|
|
|
byte days_in_transit; ///< Amount of days this packet has been in transit.
|
|
|
|
SourceType source_type; ///< Type of \c source_id.
|
|
|
|
SourceID source_id; ///< Index of source, INVALID_SOURCE if unknown/invalid.
|
|
|
|
StationID source; ///< The station where the cargo came from first.
|
|
|
|
TileIndex source_xy; ///< The origin of the cargo (first station in feeder chain).
|
2013-06-09 13:03:48 +00:00
|
|
|
union {
|
|
|
|
TileOrStationID loaded_at_xy; ///< Location where this cargo has been loaded into the vehicle.
|
|
|
|
TileOrStationID next_station; ///< Station where the cargo wants to go next.
|
|
|
|
};
|
2018-01-26 18:28:57 +00:00
|
|
|
uint flags = 0; ///< NOSAVE: temporary flags
|
|
|
|
|
2018-01-28 17:24:25 +00:00
|
|
|
/** Cargo packet flag bits in CargoPacket::flags. */
|
2018-01-26 18:28:57 +00:00
|
|
|
enum CargoPacketFlags {
|
|
|
|
CPF_HAS_DEFERRED_PAYMENT = 0x01, ///< Cargo packet has 1 or more deferred payment(s)
|
|
|
|
};
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2009-10-06 17:23:15 +00:00
|
|
|
/** The CargoList caches, thus needs to know about it. */
|
2013-06-09 13:03:48 +00:00
|
|
|
template <class Tinst, class Tcont> friend class CargoList;
|
2009-10-18 13:39:00 +00:00
|
|
|
friend class VehicleCargoList;
|
|
|
|
friend class StationCargoList;
|
2009-10-06 17:23:15 +00:00
|
|
|
/** We want this to be saved, right? */
|
|
|
|
friend const struct SaveLoad *GetCargoPacketDesc();
|
2018-01-26 18:28:57 +00:00
|
|
|
friend void Load_CPDP();
|
2009-10-06 17:23:15 +00:00
|
|
|
public:
|
2009-10-06 17:28:06 +00:00
|
|
|
/** Maximum number of items in a single cargo packet. */
|
|
|
|
static const uint16 MAX_COUNT = UINT16_MAX;
|
2009-10-06 17:23:15 +00:00
|
|
|
|
2009-10-20 22:24:34 +00:00
|
|
|
CargoPacket();
|
|
|
|
CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id);
|
2009-10-18 17:52:44 +00:00
|
|
|
CargoPacket(uint16 count, byte days_in_transit, StationID source, TileIndex source_xy, TileIndex loaded_at_xy, Money feeder_share = 0, SourceType source_type = ST_INDUSTRY, SourceID source_id = INVALID_SOURCE);
|
2018-01-26 18:28:57 +00:00
|
|
|
~CargoPacket();
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2011-01-19 16:25:00 +00:00
|
|
|
CargoPacket *Split(uint new_size);
|
|
|
|
void Merge(CargoPacket *cp);
|
2013-02-17 14:17:06 +00:00
|
|
|
void Reduce(uint count);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the tile where the packet was loaded last.
|
|
|
|
* @param load_place Tile where the packet was loaded last.
|
|
|
|
*/
|
|
|
|
void SetLoadPlace(TileIndex load_place) { this->loaded_at_xy = load_place; }
|
|
|
|
|
2013-06-23 08:28:53 +00:00
|
|
|
/**
|
|
|
|
* Sets the station where the packet is supposed to go next.
|
|
|
|
* @param next_station Next station the packet should go to.
|
|
|
|
*/
|
|
|
|
void SetNextStation(StationID next_station) { this->next_station = next_station; }
|
|
|
|
|
2013-02-17 14:17:06 +00:00
|
|
|
/**
|
|
|
|
* Adds some feeder share to the packet.
|
|
|
|
* @param new_share Feeder share to be added.
|
|
|
|
*/
|
|
|
|
void AddFeederShare(Money new_share) { this->feeder_share += new_share; }
|
2009-10-06 17:23:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of 'items' in this packet.
|
2011-01-19 16:25:00 +00:00
|
|
|
* @return Item count.
|
2009-10-06 17:23:15 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline uint16 Count() const
|
2009-10-06 17:23:15 +00:00
|
|
|
{
|
|
|
|
return this->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the amount of money already paid to earlier vehicles in
|
|
|
|
* the feeder chain.
|
2011-01-19 16:20:26 +00:00
|
|
|
* @return Feeder share.
|
2009-10-06 17:23:15 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline Money FeederShare() const
|
2009-10-06 17:23:15 +00:00
|
|
|
{
|
|
|
|
return this->feeder_share;
|
|
|
|
}
|
|
|
|
|
2013-02-17 14:17:06 +00:00
|
|
|
/**
|
|
|
|
* Gets part of the amount of money already paid to earlier vehicles in
|
|
|
|
* the feeder chain.
|
|
|
|
* @param part Amount of cargo to get the share for.
|
|
|
|
* @return Feeder share for the given amount of cargo.
|
|
|
|
*/
|
|
|
|
inline Money FeederShare(uint part) const
|
|
|
|
{
|
|
|
|
return this->feeder_share * part / static_cast<uint>(this->count);
|
|
|
|
}
|
|
|
|
|
2018-01-26 18:28:57 +00:00
|
|
|
void RegisterDeferredCargoPayment(CompanyID cid, VehicleType type, Money payment);
|
|
|
|
void PayDeferredPayments();
|
|
|
|
|
2009-10-06 17:23:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the number of days this cargo has been in transit.
|
2011-05-21 11:26:37 +00:00
|
|
|
* This number isn't really in days, but in 2.5 days (CARGO_AGING_TICKS = 185 ticks) and
|
2009-10-06 17:23:15 +00:00
|
|
|
* it is capped at 255.
|
2011-01-19 16:20:26 +00:00
|
|
|
* @return Length this cargo has been in transit.
|
2009-10-06 17:23:15 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline byte DaysInTransit() const
|
2009-10-06 17:23:15 +00:00
|
|
|
{
|
|
|
|
return this->days_in_transit;
|
|
|
|
}
|
|
|
|
|
2009-10-18 17:53:34 +00:00
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Gets the type of the cargo's source. industry, town or head quarter.
|
|
|
|
* @return Source type.
|
2009-10-18 17:53:34 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline SourceType SourceSubsidyType() const
|
2009-10-18 17:53:34 +00:00
|
|
|
{
|
|
|
|
return this->source_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Gets the ID of the cargo's source. An IndustryID, TownID or CompanyID.
|
|
|
|
* @return Source ID.
|
2009-10-18 17:53:34 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline SourceID SourceSubsidyID() const
|
2009-10-18 17:53:34 +00:00
|
|
|
{
|
|
|
|
return this->source_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Gets the ID of the station where the cargo was loaded for the first time.
|
|
|
|
* @return StationID.
|
2009-10-18 17:53:34 +00:00
|
|
|
*/
|
2014-06-21 19:52:52 +00:00
|
|
|
inline StationID SourceStation() const
|
2009-10-18 17:53:34 +00:00
|
|
|
{
|
|
|
|
return this->source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Gets the coordinates of the cargo's source station.
|
|
|
|
* @return Source station's coordinates.
|
2009-10-18 17:53:34 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline TileIndex SourceStationXY() const
|
2009-10-18 17:53:34 +00:00
|
|
|
{
|
|
|
|
return this->source_xy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Gets the coordinates of the cargo's last loading station.
|
|
|
|
* @return Last loading station's coordinates.
|
2009-10-18 17:53:34 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline TileIndex LoadedAtXY() const
|
2009-10-18 17:53:34 +00:00
|
|
|
{
|
|
|
|
return this->loaded_at_xy;
|
|
|
|
}
|
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
/**
|
|
|
|
* Gets the ID of station the cargo wants to go next.
|
|
|
|
* @return Next station for this packets.
|
|
|
|
*/
|
|
|
|
inline StationID NextStation() const
|
|
|
|
{
|
|
|
|
return this->next_station;
|
|
|
|
}
|
2009-10-06 17:23:15 +00:00
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
static void InvalidateAllFrom(SourceType src_type, SourceID src);
|
2009-10-18 17:26:10 +00:00
|
|
|
static void InvalidateAllFrom(StationID sid);
|
2009-10-18 17:47:38 +00:00
|
|
|
static void AfterLoad();
|
2020-01-01 23:14:00 +00:00
|
|
|
static void PostVehiclesAfterLoad();
|
2019-06-08 11:52:34 +00:00
|
|
|
static bool ValidateDeferredCargoPayments();
|
2007-06-22 11:58:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Simple collection class for a list of cargo packets.
|
2013-01-08 22:46:42 +00:00
|
|
|
* @tparam Tinst Actual instantiation of this cargo list.
|
2007-06-22 11:58:59 +00:00
|
|
|
*/
|
2013-06-09 13:03:48 +00:00
|
|
|
template <class Tinst, class Tcont>
|
2007-06-22 11:58:59 +00:00
|
|
|
class CargoList {
|
|
|
|
public:
|
2011-01-19 16:20:26 +00:00
|
|
|
/** The iterator for our container. */
|
2013-06-09 13:03:48 +00:00
|
|
|
typedef typename Tcont::iterator Iterator;
|
2013-02-17 14:17:06 +00:00
|
|
|
/** The reverse iterator for our container. */
|
2013-06-09 13:03:48 +00:00
|
|
|
typedef typename Tcont::reverse_iterator ReverseIterator;
|
2011-01-19 16:20:26 +00:00
|
|
|
/** The const iterator for our container. */
|
2013-06-09 13:03:48 +00:00
|
|
|
typedef typename Tcont::const_iterator ConstIterator;
|
2013-02-17 14:17:06 +00:00
|
|
|
/** The const reverse iterator for our container. */
|
2013-06-09 13:03:48 +00:00
|
|
|
typedef typename Tcont::const_reverse_iterator ConstReverseIterator;
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2011-01-19 16:20:26 +00:00
|
|
|
/** Kind of actions that could be done with packets on move. */
|
2007-06-22 11:58:59 +00:00
|
|
|
enum MoveToAction {
|
2013-02-17 14:54:50 +00:00
|
|
|
MTA_BEGIN = 0,
|
|
|
|
MTA_TRANSFER = 0, ///< Transfer the cargo to the station.
|
|
|
|
MTA_DELIVER, ///< Deliver the cargo to some town or industry.
|
|
|
|
MTA_KEEP, ///< Keep the cargo in the vehicle.
|
|
|
|
MTA_LOAD, ///< Load the cargo from the station.
|
|
|
|
MTA_END,
|
|
|
|
NUM_MOVE_TO_ACTION = MTA_END
|
2007-06-22 11:58:59 +00:00
|
|
|
};
|
|
|
|
|
2009-10-18 13:39:00 +00:00
|
|
|
protected:
|
2011-01-19 16:20:26 +00:00
|
|
|
uint count; ///< Cache for the number of cargo entities.
|
|
|
|
uint cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours.
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
Tcont packets; ///< The cargo packets in this list.
|
2009-10-06 21:19:20 +00:00
|
|
|
|
2009-10-07 08:25:12 +00:00
|
|
|
void AddToCache(const CargoPacket *cp);
|
2011-01-19 16:20:26 +00:00
|
|
|
|
2013-02-17 14:17:06 +00:00
|
|
|
void RemoveFromCache(const CargoPacket *cp, uint count);
|
|
|
|
|
|
|
|
static bool TryMerge(CargoPacket *cp, CargoPacket *icp);
|
2009-10-07 08:25:12 +00:00
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
public:
|
2011-01-19 16:20:26 +00:00
|
|
|
/** Create the cargo list. */
|
2009-10-19 15:36:35 +00:00
|
|
|
CargoList() {}
|
2011-01-19 16:20:26 +00:00
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
~CargoList();
|
|
|
|
|
2011-02-19 18:02:17 +00:00
|
|
|
void OnCleanPool();
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
/**
|
|
|
|
* Returns a pointer to the cargo packet list (so you can iterate over it etc).
|
2011-01-19 16:20:26 +00:00
|
|
|
* @return Pointer to the packet list.
|
2007-06-22 11:58:59 +00:00
|
|
|
*/
|
2013-06-09 13:03:48 +00:00
|
|
|
inline const Tcont *Packets() const
|
2009-10-06 19:52:27 +00:00
|
|
|
{
|
|
|
|
return &this->packets;
|
|
|
|
}
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Returns average number of days in transit for a cargo entity.
|
|
|
|
* @return The before mentioned number.
|
2007-06-22 11:58:59 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline uint DaysInTransit() const
|
2009-10-06 19:52:27 +00:00
|
|
|
{
|
2009-10-06 21:24:03 +00:00
|
|
|
return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
|
2009-10-06 19:52:27 +00:00
|
|
|
}
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
void InvalidateCache();
|
|
|
|
};
|
|
|
|
|
2016-09-12 19:32:51 +00:00
|
|
|
typedef std::deque<CargoPacket *> CargoPacketList;
|
2013-06-09 13:03:48 +00:00
|
|
|
|
2009-10-18 13:39:00 +00:00
|
|
|
/**
|
|
|
|
* CargoList that is used for vehicles.
|
|
|
|
*/
|
2013-06-09 13:03:48 +00:00
|
|
|
class VehicleCargoList : public CargoList<VehicleCargoList, CargoPacketList> {
|
2009-10-19 01:12:51 +00:00
|
|
|
protected:
|
2011-01-19 16:20:26 +00:00
|
|
|
/** The (direct) parent of this class. */
|
2013-06-09 13:03:48 +00:00
|
|
|
typedef CargoList<VehicleCargoList, CargoPacketList> Parent;
|
2009-10-19 01:12:51 +00:00
|
|
|
|
2013-02-17 14:54:50 +00:00
|
|
|
Money feeder_share; ///< Cache for the feeder share.
|
2019-09-29 20:27:32 +00:00
|
|
|
uint action_counts[NUM_MOVE_TO_ACTION]; ///< Counts of cargo to be transferred, delivered, kept and loaded.
|
2013-02-17 14:54:50 +00:00
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
template<class Taction>
|
|
|
|
void ShiftCargo(Taction action);
|
|
|
|
|
2018-04-30 15:55:07 +00:00
|
|
|
template<class Taction>
|
|
|
|
void ShiftCargoWithFrontInsert(Taction action);
|
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
template<class Taction>
|
|
|
|
void PopCargo(Taction action);
|
|
|
|
|
2018-08-05 10:32:05 +00:00
|
|
|
inline uint RecalculateCargoTotal() const
|
|
|
|
{
|
|
|
|
uint total = 0;
|
|
|
|
for (const auto &cp : this->packets) {
|
|
|
|
total += cp->Count();
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2018-05-13 17:56:31 +00:00
|
|
|
public:
|
|
|
|
|
2013-02-17 14:54:50 +00:00
|
|
|
/**
|
|
|
|
* Assert that the designation counts add up.
|
|
|
|
*/
|
|
|
|
inline void AssertCountConsistency() const
|
|
|
|
{
|
2018-05-13 17:56:31 +00:00
|
|
|
assert_msg(this->action_counts[MTA_KEEP] +
|
2013-02-17 14:54:50 +00:00
|
|
|
this->action_counts[MTA_DELIVER] +
|
|
|
|
this->action_counts[MTA_TRANSFER] +
|
2018-05-13 17:56:31 +00:00
|
|
|
this->action_counts[MTA_LOAD] == this->count,
|
2018-08-05 10:32:05 +00:00
|
|
|
"%u + %u + %u + %u != %u, (%u in %u packets)",
|
2018-05-13 17:56:31 +00:00
|
|
|
this->action_counts[MTA_KEEP],
|
|
|
|
this->action_counts[MTA_DELIVER],
|
|
|
|
this->action_counts[MTA_TRANSFER],
|
|
|
|
this->action_counts[MTA_LOAD],
|
2018-08-05 10:32:05 +00:00
|
|
|
this->count,
|
|
|
|
this->RecalculateCargoTotal(),
|
|
|
|
(uint) this->packets.size());
|
2013-02-17 14:54:50 +00:00
|
|
|
}
|
2009-10-19 01:12:51 +00:00
|
|
|
|
2018-05-13 17:56:31 +00:00
|
|
|
protected:
|
2009-10-19 01:12:51 +00:00
|
|
|
void AddToCache(const CargoPacket *cp);
|
2013-02-17 14:17:06 +00:00
|
|
|
void RemoveFromCache(const CargoPacket *cp, uint count);
|
2009-10-19 01:12:51 +00:00
|
|
|
|
2013-02-17 14:54:50 +00:00
|
|
|
void AddToMeta(const CargoPacket *cp, MoveToAction action);
|
|
|
|
void RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count);
|
|
|
|
|
2013-10-22 16:15:59 +00:00
|
|
|
static MoveToAction ChooseAction(const CargoPacket *cp, StationID cargo_next,
|
|
|
|
StationID current_station, bool accepted, StationIDStack next_station);
|
|
|
|
|
2009-10-18 13:39:00 +00:00
|
|
|
public:
|
2013-06-09 13:03:48 +00:00
|
|
|
/** The station cargo list needs to control the unloading. */
|
|
|
|
friend class StationCargoList;
|
2011-01-19 16:20:26 +00:00
|
|
|
/** The super class ought to know what it's doing. */
|
2013-06-09 13:03:48 +00:00
|
|
|
friend class CargoList<VehicleCargoList, CargoPacketList>;
|
2009-10-18 13:39:00 +00:00
|
|
|
/** The vehicles have a cargo list (and we want that saved). */
|
|
|
|
friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
|
|
|
|
|
2013-02-17 14:36:40 +00:00
|
|
|
friend class CargoShift;
|
|
|
|
friend class CargoTransfer;
|
|
|
|
friend class CargoDelivery;
|
|
|
|
template<class Tsource>
|
|
|
|
friend class CargoRemoval;
|
2013-02-17 14:54:50 +00:00
|
|
|
friend class CargoReturn;
|
2013-06-23 08:28:53 +00:00
|
|
|
friend class VehicleCargoReroute;
|
2013-02-17 14:36:40 +00:00
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
/**
|
|
|
|
* Returns source of the first cargo packet in this list.
|
|
|
|
* @return The before mentioned source.
|
|
|
|
*/
|
|
|
|
inline StationID Source() const
|
|
|
|
{
|
|
|
|
return this->count == 0 ? INVALID_STATION : this->packets.front()->source;
|
|
|
|
}
|
|
|
|
|
2009-10-19 01:12:51 +00:00
|
|
|
/**
|
2011-01-19 16:20:26 +00:00
|
|
|
* Returns total sum of the feeder share for all packets.
|
|
|
|
* @return The before mentioned number.
|
2009-10-19 01:12:51 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline Money FeederShare() const
|
2009-10-19 01:12:51 +00:00
|
|
|
{
|
|
|
|
return this->feeder_share;
|
|
|
|
}
|
|
|
|
|
2013-02-17 14:54:50 +00:00
|
|
|
/**
|
|
|
|
* Returns the amount of cargo designated for a given purpose.
|
|
|
|
* @param action Action the cargo is designated for.
|
|
|
|
* @return Amount of cargo designated for the given action.
|
|
|
|
*/
|
|
|
|
inline uint ActionCount(MoveToAction action) const
|
|
|
|
{
|
|
|
|
return this->action_counts[action];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns sum of cargo on board the vehicle (ie not only
|
|
|
|
* reserved).
|
|
|
|
* @return Cargo on board the vehicle.
|
|
|
|
*/
|
2013-04-13 13:42:08 +00:00
|
|
|
inline uint StoredCount() const
|
2013-02-17 14:54:50 +00:00
|
|
|
{
|
|
|
|
return this->count - this->action_counts[MTA_LOAD];
|
|
|
|
}
|
|
|
|
|
2013-04-13 13:42:08 +00:00
|
|
|
/**
|
|
|
|
* Returns sum of cargo, including reserved cargo.
|
|
|
|
* @return Sum of cargo.
|
|
|
|
*/
|
|
|
|
inline uint TotalCount() const
|
|
|
|
{
|
|
|
|
return this->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns sum of reserved cargo.
|
|
|
|
* @return Sum of reserved cargo.
|
|
|
|
*/
|
|
|
|
inline uint ReservedCount() const
|
|
|
|
{
|
|
|
|
return this->action_counts[MTA_LOAD];
|
|
|
|
}
|
|
|
|
|
2013-02-17 14:54:50 +00:00
|
|
|
/**
|
|
|
|
* Returns sum of cargo to be moved out of the vehicle at the current station.
|
|
|
|
* @return Cargo to be moved.
|
|
|
|
*/
|
|
|
|
inline uint UnloadCount() const
|
|
|
|
{
|
|
|
|
return this->action_counts[MTA_TRANSFER] + this->action_counts[MTA_DELIVER];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the sum of cargo to be kept in the vehicle at the current station.
|
|
|
|
* @return Cargo to be kept or loaded.
|
|
|
|
*/
|
|
|
|
inline uint RemainingCount() const
|
|
|
|
{
|
|
|
|
return this->action_counts[MTA_KEEP] + this->action_counts[MTA_LOAD];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Append(CargoPacket *cp, MoveToAction action = MTA_KEEP);
|
|
|
|
|
2009-10-18 13:39:00 +00:00
|
|
|
void AgeCargo();
|
2009-10-18 14:30:37 +00:00
|
|
|
|
2009-10-19 01:12:51 +00:00
|
|
|
void InvalidateCache();
|
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
void SetTransferLoadPlace(TileIndex xy);
|
|
|
|
|
2013-10-20 13:47:58 +00:00
|
|
|
bool Stage(bool accepted, StationID current_station, StationIDStack next_station, uint8 order_flags, const GoodsEntry *ge, CargoPayment *payment);
|
2013-02-17 14:54:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks all cargo in the vehicle as to be kept. This is mostly useful for
|
|
|
|
* loading old savegames. When loading is aborted the reserved cargo has
|
|
|
|
* to be returned first.
|
|
|
|
*/
|
|
|
|
inline void KeepAll()
|
|
|
|
{
|
|
|
|
this->action_counts[MTA_DELIVER] = this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_LOAD] = 0;
|
|
|
|
this->action_counts[MTA_KEEP] = this->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods for moving cargo around. First parameter is always maximum
|
|
|
|
* amount of cargo to be moved. Second parameter is destination (if
|
|
|
|
* applicable), return value is amount of cargo actually moved. */
|
|
|
|
|
2014-02-09 21:10:25 +00:00
|
|
|
template<MoveToAction Tfrom, MoveToAction Tto>
|
|
|
|
uint Reassign(uint max_move, TileOrStationID update = INVALID_TILE);
|
2013-06-09 13:03:48 +00:00
|
|
|
uint Return(uint max_move, StationCargoList *dest, StationID next_station);
|
2013-02-17 14:54:50 +00:00
|
|
|
uint Unload(uint max_move, StationCargoList *dest, CargoPayment *payment);
|
|
|
|
uint Shift(uint max_move, VehicleCargoList *dest);
|
2013-06-09 13:03:48 +00:00
|
|
|
uint Truncate(uint max_move = UINT_MAX);
|
2013-06-23 08:28:53 +00:00
|
|
|
uint Reroute(uint max_move, VehicleCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge);
|
2013-02-17 14:54:50 +00:00
|
|
|
|
2009-10-18 14:30:37 +00:00
|
|
|
/**
|
2020-06-06 04:15:23 +00:00
|
|
|
* Are the two CargoPackets mergeable in the context of
|
2009-10-18 14:30:37 +00:00
|
|
|
* a list of CargoPackets for a Vehicle?
|
2011-01-19 16:20:26 +00:00
|
|
|
* @param cp1 First CargoPacket.
|
|
|
|
* @param cp2 Second CargoPacket.
|
|
|
|
* @return True if they are mergeable.
|
2009-10-18 14:30:37 +00:00
|
|
|
*/
|
|
|
|
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
|
|
|
|
{
|
|
|
|
return cp1->source_xy == cp2->source_xy &&
|
|
|
|
cp1->days_in_transit == cp2->days_in_transit &&
|
|
|
|
cp1->source_type == cp2->source_type &&
|
|
|
|
cp1->source_id == cp2->source_id &&
|
|
|
|
cp1->loaded_at_xy == cp2->loaded_at_xy;
|
|
|
|
}
|
2009-10-18 13:39:00 +00:00
|
|
|
};
|
|
|
|
|
2016-09-12 19:32:51 +00:00
|
|
|
typedef MultiMap<StationID, CargoPacket *, CargoPacketList> StationCargoPacketMap;
|
2013-06-09 13:04:32 +00:00
|
|
|
typedef std::map<StationID, uint> StationCargoAmountMap;
|
2013-06-09 13:03:48 +00:00
|
|
|
|
2009-10-18 13:39:00 +00:00
|
|
|
/**
|
|
|
|
* CargoList that is used for stations.
|
|
|
|
*/
|
2013-06-09 13:03:48 +00:00
|
|
|
class StationCargoList : public CargoList<StationCargoList, StationCargoPacketMap> {
|
2013-02-17 14:54:50 +00:00
|
|
|
protected:
|
|
|
|
/** The (direct) parent of this class. */
|
2013-06-09 13:03:48 +00:00
|
|
|
typedef CargoList<StationCargoList, StationCargoPacketMap> Parent;
|
2013-02-17 14:54:50 +00:00
|
|
|
|
|
|
|
uint reserved_count; ///< Amount of cargo being reserved for loading.
|
|
|
|
|
2009-10-18 13:39:00 +00:00
|
|
|
public:
|
2011-01-19 16:20:26 +00:00
|
|
|
/** The super class ought to know what it's doing. */
|
2013-06-09 13:03:48 +00:00
|
|
|
friend class CargoList<StationCargoList, StationCargoPacketMap>;
|
2009-10-18 13:39:00 +00:00
|
|
|
/** The stations, via GoodsEntry, have a CargoList. */
|
|
|
|
friend const struct SaveLoad *GetGoodsDesc();
|
2009-10-18 14:30:37 +00:00
|
|
|
|
2013-02-17 14:36:40 +00:00
|
|
|
friend class CargoLoad;
|
|
|
|
friend class CargoTransfer;
|
|
|
|
template<class Tsource>
|
|
|
|
friend class CargoRemoval;
|
2013-02-17 14:54:50 +00:00
|
|
|
friend class CargoReservation;
|
|
|
|
friend class CargoReturn;
|
2013-06-23 08:28:53 +00:00
|
|
|
friend class StationCargoReroute;
|
2013-06-09 13:03:48 +00:00
|
|
|
|
|
|
|
static void InvalidateAllFrom(SourceType src_type, SourceID src);
|
|
|
|
|
|
|
|
template<class Taction>
|
|
|
|
bool ShiftCargo(Taction &action, StationID next);
|
|
|
|
|
|
|
|
template<class Taction>
|
2013-10-20 13:47:58 +00:00
|
|
|
uint ShiftCargo(Taction action, StationIDStack next, bool include_invalid);
|
2013-06-09 13:03:48 +00:00
|
|
|
|
|
|
|
void Append(CargoPacket *cp, StationID next);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for cargo headed for a specific station.
|
|
|
|
* @param next Station the cargo is headed for.
|
|
|
|
* @return If there is any cargo for that station.
|
|
|
|
*/
|
2013-10-20 13:47:58 +00:00
|
|
|
inline bool HasCargoFor(StationIDStack next) const
|
2013-06-09 13:03:48 +00:00
|
|
|
{
|
2013-10-20 13:47:58 +00:00
|
|
|
while (!next.IsEmpty()) {
|
|
|
|
if (this->packets.find(next.Pop()) != this->packets.end()) return true;
|
|
|
|
}
|
2020-06-06 04:15:23 +00:00
|
|
|
/* Packets for INVALID_STATION can go anywhere. */
|
2013-10-20 13:47:58 +00:00
|
|
|
return this->packets.find(INVALID_STATION) != this->packets.end();
|
2013-06-09 13:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns source of the first cargo packet in this list.
|
|
|
|
* @return The before mentioned source.
|
|
|
|
*/
|
|
|
|
inline StationID Source() const
|
|
|
|
{
|
|
|
|
return this->count == 0 ? INVALID_STATION : this->packets.begin()->second.front()->source;
|
|
|
|
}
|
2013-02-17 14:54:50 +00:00
|
|
|
|
2013-04-13 13:42:08 +00:00
|
|
|
/**
|
|
|
|
* Returns sum of cargo still available for loading at the sation.
|
|
|
|
* (i.e. not counting cargo which is already reserved for loading)
|
|
|
|
* @return Cargo on board the vehicle.
|
|
|
|
*/
|
|
|
|
inline uint AvailableCount() const
|
|
|
|
{
|
|
|
|
return this->count;
|
|
|
|
}
|
|
|
|
|
2020-05-10 14:21:03 +00:00
|
|
|
uint AvailableViaCount(StationID next) const;
|
|
|
|
|
2013-02-17 14:59:27 +00:00
|
|
|
/**
|
|
|
|
* Returns sum of cargo reserved for loading onto vehicles.
|
|
|
|
* @return Cargo reserved for loading.
|
|
|
|
*/
|
|
|
|
inline uint ReservedCount() const
|
|
|
|
{
|
|
|
|
return this->reserved_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-13 13:42:08 +00:00
|
|
|
* Returns total count of cargo at the station, including
|
|
|
|
* cargo which is already reserved for loading.
|
2013-02-17 14:59:27 +00:00
|
|
|
* @return Total cargo count.
|
|
|
|
*/
|
|
|
|
inline uint TotalCount() const
|
|
|
|
{
|
|
|
|
return this->count + this->reserved_count;
|
|
|
|
}
|
|
|
|
|
2013-02-17 14:54:50 +00:00
|
|
|
/* Methods for moving cargo around. First parameter is always maximum
|
|
|
|
* amount of cargo to be moved. Second parameter is destination (if
|
|
|
|
* applicable), return value is amount of cargo actually moved. */
|
|
|
|
|
2013-10-20 13:47:58 +00:00
|
|
|
uint Reserve(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next);
|
|
|
|
uint Load(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationIDStack next);
|
2019-04-10 21:07:06 +00:00
|
|
|
uint Truncate(uint max_move = UINT_MAX, StationCargoAmountMap *cargo_per_source = nullptr);
|
2013-06-09 13:03:48 +00:00
|
|
|
uint Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge);
|
2013-02-17 14:36:40 +00:00
|
|
|
|
2020-01-01 23:14:00 +00:00
|
|
|
void AfterLoadIncreaseReservationCount(uint count)
|
|
|
|
{
|
|
|
|
this->reserved_count += count;
|
|
|
|
}
|
|
|
|
|
2009-10-18 14:30:37 +00:00
|
|
|
/**
|
2020-06-04 11:07:19 +00:00
|
|
|
* Are the two CargoPackets mergeable in the context of
|
|
|
|
* a list of CargoPackets for a Station?
|
2011-01-19 16:20:26 +00:00
|
|
|
* @param cp1 First CargoPacket.
|
|
|
|
* @param cp2 Second CargoPacket.
|
|
|
|
* @return True if they are mergeable.
|
2009-10-18 14:30:37 +00:00
|
|
|
*/
|
|
|
|
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
|
|
|
|
{
|
|
|
|
return cp1->source_xy == cp2->source_xy &&
|
|
|
|
cp1->days_in_transit == cp2->days_in_transit &&
|
|
|
|
cp1->source_type == cp2->source_type &&
|
|
|
|
cp1->source_id == cp2->source_id;
|
|
|
|
}
|
2009-10-18 13:39:00 +00:00
|
|
|
};
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
#endif /* CARGOPACKET_H */
|