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