glx@9629: /* $Id$ */ glx@9629: rubidium@10455: /** @file cargopacket.h Base class for cargo packets. */ glx@9629: glx@9629: #ifndef CARGOPACKET_H glx@9629: #define CARGOPACKET_H glx@9629: rubidium@9837: #include "oldpool.h" rubidium@9723: #include "economy_type.h" rubidium@9724: #include "tile_type.h" rubidium@9837: #include "station_type.h" glx@9629: #include glx@9629: rubidium@10455: struct BackuppedVehicle; rubidium@10455: rubidium@9694: typedef uint32 CargoPacketID; rubidium@9694: struct CargoPacket; rubidium@9694: rubidium@9694: /** We want to use a pool */ rubidium@9694: DECLARE_OLD_POOL(CargoPacket, CargoPacket, 10, 1000) rubidium@9694: rubidium@9694: glx@9629: /** glx@9629: * Container for cargo from the same location and time glx@9629: */ rubidium@9694: struct CargoPacket : PoolItem { glx@10294: Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo glx@9629: TileIndex source_xy; ///< The origin of the cargo (first station in feeder chain) glx@9629: TileIndex loaded_at_xy; ///< Location where this cargo has been loaded into the vehicle glx@10294: StationID source; ///< The station where the cargo came from first glx@9629: glx@9629: uint16 count; ///< The amount of cargo in this packet glx@9629: byte days_in_transit; ///< Amount of days this packet has been in transit glx@9629: bool paid_for; ///< Have we been paid for this cargo packet? glx@9629: glx@9629: /** glx@9629: * Creates a new cargo packet glx@9629: * @param source the source of the packet glx@9629: * @param count the number of cargo entities to put in this packet glx@9629: * @pre count != 0 || source == INVALID_STATION glx@9629: */ glx@9629: CargoPacket(StationID source = INVALID_STATION, uint16 count = 0); glx@9629: glx@9629: /** Destroy the packet */ rubidium@9694: virtual ~CargoPacket(); glx@9629: glx@9629: glx@9629: /** glx@9629: * Is this a valid cargo packet ? glx@9629: * @return true if and only it is valid glx@9629: */ rubidium@9701: inline bool IsValid() const { return this->count != 0; } glx@9629: glx@9629: /** glx@9629: * Checks whether the cargo packet is from (exactly) the same source glx@9629: * in time and location. glx@9629: * @param cp the cargo packet to compare to glx@9629: * @return true if and only if days_in_transit and source_xy are equal glx@9629: */ glx@9732: bool SameSource(const CargoPacket *cp) const; rubidium@10455: rubidium@10455: void RestoreBackup() const; glx@9629: }; glx@9629: glx@9629: /** glx@9629: * Iterate over all _valid_ cargo packets from the given start glx@9629: * @param cp the variable used as "iterator" glx@9629: * @param start the cargo packet ID of the first packet to iterate over glx@9629: */ glx@9629: #define FOR_ALL_CARGOPACKETS_FROM(cp, start) for (cp = GetCargoPacket(start); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) if (cp->IsValid()) glx@9629: glx@9629: /** glx@9629: * Iterate over all _valid_ cargo packets from the begin of the pool glx@9629: * @param cp the variable used as "iterator" glx@9629: */ glx@9629: #define FOR_ALL_CARGOPACKETS(cp) FOR_ALL_CARGOPACKETS_FROM(cp, 0) glx@9629: rubidium@9701: extern void SaveLoad_STNS(Station *st); rubidium@9701: glx@9629: /** glx@9629: * Simple collection class for a list of cargo packets glx@9629: */ glx@9629: class CargoList { glx@9629: public: glx@9629: /** List of cargo packets */ glx@9629: typedef std::list List; glx@9629: glx@9629: /** Kind of actions that could be done with packets on move */ glx@9629: enum MoveToAction { glx@9629: MTA_FINAL_DELIVERY, ///< "Deliver" the packet to the final destination, i.e. destroy the packet glx@9629: MTA_CARGO_LOAD, ///< Load the packet onto a vehicle, i.e. set the last loaded station ID glx@9629: MTA_OTHER ///< "Just" move the packet to another cargo list glx@9629: }; glx@9629: glx@9629: private: glx@9629: List packets; ///< The cargo packets in this list glx@9629: glx@9629: bool empty; ///< Cache for whether this list is empty or not glx@9629: uint count; ///< Cache for the number of cargo entities glx@9629: bool unpaid_cargo; ///< Cache for the unpaid cargo glx@9629: Money feeder_share; ///< Cache for the feeder share glx@9629: StationID source; ///< Cache for the source of the packet glx@9629: uint days_in_transit; ///< Cache for the number of days in transit glx@9629: glx@9629: public: rubidium@10455: friend struct BackuppedVehicle; rubidium@9701: friend void SaveLoad_STNS(Station *st); glx@9629: glx@9629: /** Create the cargo list */ glx@9629: CargoList() { this->InvalidateCache(); } glx@9629: /** And destroy it ("frees" all cargo packets) */ glx@9629: ~CargoList(); glx@9629: glx@9629: /** glx@9629: * Returns a pointer to the cargo packet list (so you can iterate over it etc). glx@9629: * @return pointer to the packet list glx@9629: */ glx@9629: const CargoList::List *Packets() const; glx@9629: glx@9629: /** glx@9629: * Ages the all cargo in this list glx@9629: */ glx@9629: void AgeCargo(); glx@9629: glx@9629: /** glx@9629: * Checks whether this list is empty glx@9629: * @return true if and only if the list is empty glx@9629: */ glx@9629: bool Empty() const; glx@9629: glx@9629: /** glx@9629: * Returns the number of cargo entities in this list glx@9629: * @return the before mentioned number glx@9629: */ glx@9629: uint Count() const; glx@9629: glx@9629: /** glx@9629: * Is there some cargo that has not been paid for? glx@9629: * @return true if and only if there is such a cargo glx@9629: */ glx@9629: bool UnpaidCargo() const; glx@9629: glx@9629: /** glx@9629: * Returns total sum of the feeder share for all packets glx@9629: * @return the before mentioned number glx@9629: */ glx@9629: Money FeederShare() const; glx@9629: glx@9629: /** glx@9629: * Returns source of the first cargo packet in this list glx@9629: * @return the before mentioned source glx@9629: */ glx@9629: StationID Source() const; glx@9629: glx@9629: /** glx@9629: * Returns average number of days in transit for a cargo entity glx@9629: * @return the before mentioned number glx@9629: */ glx@9629: uint DaysInTransit() const; glx@9629: glx@9629: glx@9629: /** glx@9629: * Appends the given cargo packet glx@9629: * @warning After appending this packet may not exist anymore! glx@9629: * @note Do not use the cargo packet anymore after it has been appended to this CargoList! glx@9629: * @param cp the cargo packet to add glx@9629: * @pre cp != NULL glx@9629: */ glx@9629: void Append(CargoPacket *cp); glx@9629: glx@9629: /** glx@9629: * Truncates the cargo in this list to the given amount. It leaves the glx@9629: * first count cargo entities and removes the rest. glx@9629: * @param count the maximum amount of entities to be in the list after the command glx@9629: */ glx@9629: void Truncate(uint count); glx@9629: glx@9629: /** glx@9629: * Moves the given amount of cargo to another list. glx@9629: * Depending on the value of mta the side effects of this function differ: glx@9629: * - MTA_FINAL_DELIVERY: destroys the packets that do not originate from a specific station glx@9629: * - MTA_CARGO_LOAD: sets the loaded_at_xy value of the moved packets glx@9629: * - MTA_OTHER: just move without side effects glx@9629: * @param dest the destination to move the cargo to glx@9629: * @param count the amount of cargo entities to move glx@9629: * @param mta how to handle the moving (side effects) glx@9629: * @param data Depending on mta the data of this variable differs: glx@9629: * - MTA_FINAL_DELIVERY - station ID of packet's origin not to remove glx@9629: * - MTA_CARGO_LOAD - station's tile index of load glx@9629: * - MTA_OTHER - unused glx@9629: * @param mta == MTA_FINAL_DELIVERY || dest != NULL glx@9629: * @return true if there are still packets that might be moved from this cargo list glx@9629: */ glx@9629: bool MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta = MTA_OTHER, uint data = 0); glx@9629: glx@9629: /** Invalidates the cached data and rebuild it */ glx@9629: void InvalidateCache(); glx@9629: }; glx@9629: glx@9629: #endif /* CARGOPACKET_H */