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